]> code.delx.au - gnu-emacs/blob - admin/unidata/unidata-gen.el
Update copyright year to 2015
[gnu-emacs] / admin / unidata / unidata-gen.el
1 ;; unidata-gen.el -- Create files containing character property data.
2
3 ;; Copyright (C) 2008-2015 Free Software Foundation, Inc.
4
5 ;; Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011
6 ;; National Institute of Advanced Industrial Science and Technology (AIST)
7 ;; Registration Number H13PRO009
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; SPECIAL NOTICE
27 ;;
28 ;; This file must be byte-compilable/loadable by `temacs' and also
29 ;; the entry function `unidata-gen-files' must be runnable by `temacs'.
30
31 ;; FILES TO BE GENERATED
32 ;;
33 ;; The entry function `unidata-gen-files' generates these files in
34 ;; in directory specified by its dest-dir argument.
35 ;;
36 ;; charprop.el
37 ;; It contains a series of forms of this format:
38 ;; (define-char-code-property PROP FILE)
39 ;; where PROP is a symbol representing a character property
40 ;; (name, general-category, etc), and FILE is a name of one of
41 ;; the following files.
42 ;;
43 ;; uni-name.el, uni-category.el, uni-combining.el, uni-bidi.el,
44 ;; uni-decomposition.el, uni-decimal.el, uni-digit.el, uni-numeric.el,
45 ;; uni-mirrored.el, uni-old-name.el, uni-comment.el, uni-uppercase.el,
46 ;; uni-lowercase.el, uni-titlecase.el
47 ;; They contain one or more forms of this format:
48 ;; (define-char-code-property PROP CHAR-TABLE)
49 ;; where PROP is the same as above, and CHAR-TABLE is a
50 ;; char-table containing property values in a compressed format.
51 ;;
52 ;; When they are installed in .../lisp/international/, the file
53 ;; "charprop.el" is preloaded in loadup.el. The other files are
54 ;; automatically loaded when the Lisp functions
55 ;; `get-char-code-property' and `put-char-code-property', and C
56 ;; function uniprop_table are called.
57 ;;
58 ;; FORMAT OF A CHAR TABLE
59 ;;
60 ;; We want to make a file size containing a char-table small. We
61 ;; also want to load the file and get a property value fast. We
62 ;; also want to reduce the used memory after loading it. So,
63 ;; instead of naively storing a property value for each character in
64 ;; a char-table (and write it out into a file), we store compressed
65 ;; data in a char-table as below.
66 ;;
67 ;; If succeeding 128*N characters have the same property value, we
68 ;; store that value (or the encoded one) for them. Otherwise,
69 ;; compress values (or the encoded ones) for succeeding 128
70 ;; characters into a single string and store it for those
71 ;; characters. The way of compression depends on a property. See
72 ;; the section "SIMPLE TABLE", "RUN-LENGTH TABLE", and "WORD-LIST
73 ;; TABLE".
74
75 ;; The char table has five extra slots:
76 ;; 1st: property symbol
77 ;; 2nd: function to call to get a property value,
78 ;; or an index number of C function to decode the value,
79 ;; or nil if the value can be directly got from the table.
80 ;; 3nd: function to call to put a property value,
81 ;; or an index number of C function to encode the value,
82 ;; or nil if the value can be directly stored in the table.
83 ;; 4th: function to call to get a description of a property value, or nil
84 ;; 5th: data referred by the above functions
85
86 ;; List of elements of this form:
87 ;; (CHAR-or-RANGE PROP1 PROP2 ... PROPn)
88 ;; CHAR-or-RANGE: a character code or a cons of character codes
89 ;; PROPn: string representing the nth property value
90
91 (eval-when-compile (require 'cl-lib))
92
93 (defvar unidata-list nil)
94
95 ;; Name of the directory containing files of Unicode Character Database.
96
97 ;; Dynamically bound in unidata-gen-files.
98 (defvar unidata-dir nil)
99
100 (defun unidata-setup-list (unidata-text-file)
101 (let* ((table (list nil))
102 (tail table)
103 (block-names '(("^<CJK Ideograph" . CJK\ IDEOGRAPH)
104 ("^<Hangul Syllable" . HANGUL\ SYLLABLE)
105 ("^<.*Surrogate" . nil)
106 ("^<.*Private Use" . PRIVATE\ USE)))
107 val char name)
108 (setq unidata-text-file (expand-file-name unidata-text-file unidata-dir))
109 (or (file-readable-p unidata-text-file)
110 (error "File not readable: %s" unidata-text-file))
111 (with-temp-buffer
112 ;; Insert a file of this format:
113 ;; (CHAR NAME CATEGORY ...)
114 ;; where CHAR is a character code, the following elements are strings
115 ;; representing character properties.
116 (insert-file-contents unidata-text-file)
117 (goto-char (point-min))
118 (condition-case nil
119 (while t
120 (setq val (read (current-buffer))
121 char (car val)
122 name (cadr val))
123
124 ;; Check this kind of block.
125 ;; 4E00;<CJK Ideograph, First>;Lo;0;L;;;;;N;;;;;
126 ;; 9FCB;<CJK Ideograph, Last>;Lo;0;L;;;;;N;;;;;
127 (if (and (= (aref name 0) ?<)
128 (string-match ", First>$" name))
129 (let ((first char)
130 (l block-names)
131 block-name)
132 (setq val (read (current-buffer))
133 char (car val)
134 block-name (cadr val)
135 name nil)
136 (while l
137 (if (string-match (caar l) block-name)
138 (setq name (cdar l) l nil)
139 (setq l (cdr l))))
140 (if (not name)
141 ;; As this is a surrogate pair range, ignore it.
142 (setq val nil)
143 (setcar val (cons first char))
144 (setcar (cdr val) name))))
145
146 (when val
147 (setcdr tail (list val))
148 (setq tail (cdr tail))))
149 (error nil)))
150 (setq unidata-list (cdr table))))
151
152 ;; Alist of this form:
153 ;; (PROP INDEX GENERATOR FILENAME DOCSTRING DESCRIBER DEFAULT VAL-LIST)
154 ;; PROP: character property
155 ;; INDEX: index to each element of unidata-list for PROP.
156 ;; It may be a function that generates an alist of character codes
157 ;; vs. the corresponding property values. Currently, only character
158 ;; codepoints or symbol values are supported in this case.
159 ;; GENERATOR: function to generate a char-table
160 ;; FILENAME: filename to store the char-table
161 ;; DOCSTRING: docstring for the property
162 ;; DESCRIBER: function to call to get a description string of property value
163 ;; DEFAULT: the default value of the property. It may have the form
164 ;; (VAL0 (FROM1 TO1 VAL1) ...) which indicates that the default
165 ;; value is VAL0 except for characters in the ranges specified by
166 ;; FROMn and TOn (inclusive). The default value of characters
167 ;; between FROMn and TOn is VALn.
168 ;; VAL-LIST: list of specially ordered property values
169
170 (defconst unidata-prop-alist
171 '((name
172 1 unidata-gen-table-name "uni-name.el"
173 "Unicode character name.
174 Property value is a string or nil.
175 The value nil stands for the default value \"null string\")."
176 nil
177 nil)
178 (general-category
179 2 unidata-gen-table-symbol "uni-category.el"
180 "Unicode general category.
181 Property value is one of the following symbols:
182 Lu, Ll, Lt, Lm, Lo, Mn, Mc, Me, Nd, Nl, No, Pc, Pd, Ps, Pe, Pi, Pf, Po,
183 Sm, Sc, Sk, So, Zs, Zl, Zp, Cc, Cf, Cs, Co, Cn"
184 unidata-describe-general-category
185 Cn
186 ;; The order of elements must be in sync with unicode_category_t
187 ;; in src/character.h.
188 (Lu Ll Lt Lm Lo Mn Mc Me Nd Nl No Pc Pd Ps Pe Pi Pf Po
189 Sm Sc Sk So Zs Zl Zp Cc Cf Cs Co Cn))
190 (canonical-combining-class
191 3 unidata-gen-table-integer "uni-combining.el"
192 "Unicode canonical combining class.
193 Property value is an integer."
194 unidata-describe-canonical-combining-class
195 0)
196 (bidi-class
197 4 unidata-gen-table-symbol "uni-bidi.el"
198 "Unicode bidi class.
199 Property value is one of the following symbols:
200 L, LRE, LRO, LRI, R, AL, RLE, RLO, RLI, FSI, PDF, PDI,
201 EN, ES, ET, AN, CS, NSM, BN, B, S, WS, ON"
202 unidata-describe-bidi-class
203 ;; The assignment of default values to blocks of code points
204 ;; follows the file DerivedBidiClass.txt from the Unicode
205 ;; Character Database (UCD).
206 (L (#x0600 #x06FF AL) (#xFB50 #xFDFF AL) (#xFE70 #xFEFF AL)
207 (#x0590 #x05FF R) (#x07C0 #x08FF R)
208 (#xFB1D #xFB4F R) (#x10800 #x10FFF R) (#x1E800 #x1EFFF R))
209 ;; The order of elements must be in sync with bidi_type_t in
210 ;; src/dispextern.h.
211 (L R EN AN BN B AL LRE LRO RLE RLO PDF LRI RLI FSI PDI
212 ES ET CS NSM S WS ON))
213 (decomposition
214 5 unidata-gen-table-decomposition "uni-decomposition.el"
215 "Unicode decomposition mapping.
216 Property value is a list of characters. The first element may be
217 one of these symbols representing compatibility formatting tag:
218 font, noBreak, initial, medial, final, isolated, circle, super,
219 sub, vertical, wide, narrow, small, square, fraction, compat"
220 unidata-describe-decomposition)
221 (decimal-digit-value
222 6 unidata-gen-table-integer "uni-decimal.el"
223 "Unicode numeric value (decimal digit).
224 Property value is an integer 0..9, or nil.
225 The value nil stands for NaN \"Numeric_Value\".")
226 (digit-value
227 7 unidata-gen-table-integer "uni-digit.el"
228 "Unicode numeric value (digit).
229 Property value is an integer 0..9, or nil.
230 The value nil stands for NaN \"Numeric_Value\".")
231 (numeric-value
232 8 unidata-gen-table-numeric "uni-numeric.el"
233 "Unicode numeric value (numeric).
234 Property value is an integer, a floating point, or nil.
235 The value nil stands for NaN \"Numeric_Value\".")
236 (mirrored
237 9 unidata-gen-table-symbol "uni-mirrored.el"
238 "Unicode bidi mirrored flag.
239 Property value is a symbol `Y' or `N'. See also the property `mirroring'."
240 nil
241 N)
242 (old-name
243 10 unidata-gen-table-name "uni-old-name.el"
244 "Unicode old names as published in Unicode 1.0.
245 Property value is a string or nil.
246 The value nil stands for the default value \"null string\").")
247 (iso-10646-comment
248 11 unidata-gen-table-name "uni-comment.el"
249 "Unicode ISO 10646 comment.
250 Property value is a string.")
251 (uppercase
252 12 unidata-gen-table-character "uni-uppercase.el"
253 "Unicode simple uppercase mapping.
254 Property value is a character or nil.
255 The value nil means that the actual property value of a character
256 is the character itself."
257 string)
258 (lowercase
259 13 unidata-gen-table-character "uni-lowercase.el"
260 "Unicode simple lowercase mapping.
261 Property value is a character or nil.
262 The value nil means that the actual property value of a character
263 is the character itself."
264 string)
265 (titlecase
266 14 unidata-gen-table-character "uni-titlecase.el"
267 "Unicode simple titlecase mapping.
268 Property value is a character or nil.
269 The value nil means that the actual property value of a character
270 is the character itself."
271 string)
272 (mirroring
273 unidata-gen-mirroring-list unidata-gen-table-character "uni-mirrored.el"
274 "Unicode bidi-mirroring characters.
275 Property value is a character that has the corresponding mirroring image or nil.
276 The value nil means that the actual property value of a character
277 is the character itself.")
278 (paired-bracket
279 unidata-gen-brackets-list unidata-gen-table-character "uni-brackets.el"
280 "Unicode bidi paired-bracket characters.
281 Property value is the paired bracket character, or nil.
282 The value nil means that the character is neither an opening nor
283 a closing paired bracket."
284 string)
285 (bracket-type
286 unidata-gen-bracket-type-list unidata-gen-table-symbol "uni-brackets.el"
287 "Unicode bidi paired-bracket type.
288 Property value is a symbol `o' (Open), `c' (Close), or `n' (None)."
289 unidata-describe-bidi-bracket-type
290 n
291 ;; The order of elements must be in sync with bidi_bracket_type_t
292 ;; in src/dispextern.h.
293 (n o c))))
294
295 ;; Functions to access the above data.
296 (defsubst unidata-prop-index (prop) (nth 1 (assq prop unidata-prop-alist)))
297 (defsubst unidata-prop-generator (prop) (nth 2 (assq prop unidata-prop-alist)))
298 (defsubst unidata-prop-file (prop) (nth 3 (assq prop unidata-prop-alist)))
299 (defsubst unidata-prop-docstring (prop) (nth 4 (assq prop unidata-prop-alist)))
300 (defsubst unidata-prop-describer (prop) (nth 5 (assq prop unidata-prop-alist)))
301 (defsubst unidata-prop-default (prop) (nth 6 (assq prop unidata-prop-alist)))
302 (defsubst unidata-prop-val-list (prop) (nth 7 (assq prop unidata-prop-alist)))
303
304 \f
305 ;; SIMPLE TABLE
306 ;;
307 ;; If the type of character property value is character, and the
308 ;; values of succeeding character codes are usually different, we use
309 ;; a char-table described here to store such values.
310 ;;
311 ;; A char-table divides character code space (#x0..#x3FFFFF) into
312 ;; #x8000 blocks (each block contains 128 characters).
313
314 ;; If all characters of a block have no property, a char-table has the
315 ;; symbol nil for that block. Otherwise a char-table has a string of
316 ;; the following format for it.
317 ;;
318 ;; The first character of the string is ?\001.
319 ;; The second character of the string is FIRST-INDEX.
320 ;; The Nth (N > 1) character of the string is a property value of the
321 ;; character (BLOCK-HEAD + FIRST-INDEX + N - 2), where BLOCK-HEAD is
322 ;; the first character of the block.
323 ;;
324 ;; This kind of char-table has these extra slots:
325 ;; 1st: the property symbol
326 ;; 2nd: nil
327 ;; 3rd: 0 (corresponding to uniprop_encode_character in chartab.c)
328 ;; 4th to 5th: nil
329
330 (defun unidata-gen-table-character (prop &rest ignore)
331 (let ((table (make-char-table 'char-code-property-table))
332 (prop-idx (unidata-prop-index prop))
333 (vec (make-vector 128 0))
334 (tail unidata-list)
335 elt range val idx slot)
336 (if (functionp prop-idx)
337 (setq tail (funcall prop-idx)
338 prop-idx 1))
339 (while tail
340 (setq elt (car tail) tail (cdr tail))
341 (setq range (car elt)
342 val (nth prop-idx elt))
343 (if (= (length val) 0)
344 (setq val nil)
345 (setq val (string-to-number val 16)))
346 (if (consp range)
347 (if val
348 (set-char-table-range table range val))
349 (let* ((start (lsh (lsh range -7) 7))
350 (limit (+ start 127))
351 first-index last-index)
352 (fillarray vec 0)
353 (if val
354 (aset vec (setq last-index (setq first-index (- range start)))
355 val))
356 (while (and (setq elt (car tail) range (car elt))
357 (integerp range)
358 (<= range limit))
359 (setq val (nth prop-idx elt))
360 (when (> (length val) 0)
361 (aset vec (setq last-index (- range start))
362 (string-to-number val 16))
363 (or first-index
364 (setq first-index last-index)))
365 (setq tail (cdr tail)))
366 (when first-index
367 (let ((str (string 1 first-index))
368 c)
369 (while (<= first-index last-index)
370 (setq str (format "%s%c" str (or (aref vec first-index) 0))
371 first-index (1+ first-index)))
372 (set-char-table-range table (cons start limit) str))))))
373
374 (set-char-table-extra-slot table 0 prop)
375 (set-char-table-extra-slot table 2 0)
376 table))
377
378
379 \f
380 ;; RUN-LENGTH TABLE
381 ;;
382 ;; If many characters of successive character codes have the same
383 ;; property value, we use a char-table described here to store the
384 ;; values.
385 ;;
386 ;; At first, instead of a value itself, we store an index number to
387 ;; the VAL-TABLE (5th extra slot) in the table. We call that index
388 ;; number as VAL-CODE here after.
389 ;;
390 ;; A char-table divides character code space (#x0..#x3FFFFF) into
391 ;; #x8000 blocks (each block contains 128 characters).
392 ;;
393 ;; If all characters of a block have the same value, a char-table has
394 ;; VAL-CODE for that block. Otherwise a char-table has a string of
395 ;; the following format for that block.
396 ;;
397 ;; The first character of the string is ?\002.
398 ;; The following characters has this form:
399 ;; ( VAL-CODE RUN-LENGTH ? ) +
400 ;; where:
401 ;; VAL-CODE (0..127): index into VAL-TABLE.
402 ;; RUN-LENGTH (130..255):
403 ;; (RUN-LENGTH - 128) specifies how many characters have the same
404 ;; value. If omitted, it means 1.
405 ;;
406 ;; This kind of char-table has these extra slots:
407 ;; 1st: the property symbol
408 ;; 2nd: 0 (corresponding to uniprop_decode_value in chartab.c)
409 ;; 3rd: 1..3 (corresponding to uniprop_encode_xxx in chartab.c)
410 ;; 4th: function or nil
411 ;; 5th: VAL-TABLE
412
413 ;; Encode the character property value VAL into an integer value by
414 ;; VAL-LIST. By side effect, VAL-LIST is modified.
415 ;; VAL-LIST has this form:
416 ;; ((nil . 0) (VAL1 . 1) (VAL2 . 2) ...)
417 ;; If VAL is one of VALn, just return n.
418 ;; Otherwise, VAL-LIST is modified to this:
419 ;; ((nil . 0) (VAL1 . 1) (VAL2 . 2) ... (VAL . n+1))
420 ;;
421 ;; WARN is an optional warning to display when the value list is
422 ;; extended, for property values that need to be in sync with other
423 ;; parts of Emacs; currently only used for bidi-class.
424
425 (defun unidata-encode-val (val-list val &optional warn)
426 (let ((slot (assoc val val-list))
427 val-code)
428 (if slot
429 (cdr slot)
430 (if warn (message warn val))
431 (setq val-code (length val-list))
432 (nconc val-list (list (cons val val-code)))
433 val-code)))
434
435 ;; Generate a char-table for the character property PROP.
436
437 (defun unidata-gen-table (prop val-func default-value val-list)
438 (let ((table (make-char-table 'char-code-property-table))
439 (prop-idx (unidata-prop-index prop))
440 (vec (make-vector 128 0))
441 ;; When this warning is printed, there's a need to make the
442 ;; following changes:
443 ;; (1) update unidata-prop-alist with the new bidi-class values;
444 ;; (2) extend bidi_type_t enumeration on src/dispextern.h to
445 ;; include the new classes;
446 ;; (3) possibly update the assertion in bidi.c:bidi_check_type; and
447 ;; (4) possibly update the switch cases in
448 ;; bidi.c:bidi_get_type and bidi.c:bidi_get_category.
449 (bidi-warning "\
450 ** Found new bidi-class '%s', please update bidi.c and dispextern.h")
451 tail elt range val val-code idx slot
452 prev-range-data)
453 (setq val-list (cons nil (copy-sequence val-list)))
454 (setq tail val-list val-code 0)
455 ;; Convert (nil A B ...) to ((nil . 0) (A . 1) (B . 2) ...)
456 (while tail
457 (setcar tail (cons (car tail) val-code))
458 (setq tail (cdr tail) val-code (1+ val-code)))
459 (if (consp default-value)
460 (setq default-value (copy-sequence default-value))
461 (setq default-value (list default-value)))
462 (setcar default-value
463 (unidata-encode-val val-list (car default-value)))
464 (set-char-table-range table t (car default-value))
465 (set-char-table-range table nil (car default-value))
466 (dolist (elm (cdr default-value))
467 (setcar (nthcdr 2 elm)
468 (unidata-encode-val val-list (nth 2 elm)))
469 (set-char-table-range table (cons (car elm) (nth 1 elm)) (nth 2 elm)))
470
471 (if (functionp prop-idx)
472 (setq tail (funcall prop-idx)
473 prop-idx 1)
474 (setq tail unidata-list))
475 (while tail
476 (setq elt (car tail) tail (cdr tail))
477 (setq range (car elt)
478 val (funcall val-func (nth prop-idx elt)))
479 (setq val-code (if val (unidata-encode-val val-list val
480 (and (eq prop 'bidi-class)
481 bidi-warning))))
482 (if (consp range)
483 (when val-code
484 (set-char-table-range table range val-code)
485 (let ((from (car range)) (to (cdr range)))
486 ;; If RANGE doesn't end at the char-table boundary (each
487 ;; 128 characters), we may have to carry over the data
488 ;; for the last several characters (at most 127 chars)
489 ;; to the next loop. In that case, set PREV-RANGE-DATA
490 ;; to ((FROM . TO) . VAL-CODE) where (FROM . TO)
491 ;; specifies the range of characters handled in the next
492 ;; loop.
493 (when (< (logand to #x7F) #x7F)
494 (if (< from (logand to #x1FFF80))
495 (setq from (logand to #x1FFF80)))
496 (setq prev-range-data (cons (cons from to) val-code)))))
497 (let* ((start (lsh (lsh range -7) 7))
498 (limit (+ start 127))
499 str count new-val from to vcode)
500 (fillarray vec (car default-value))
501 (dolist (elm (cdr default-value))
502 (setq from (car elm) to (nth 1 elm))
503 (when (and (<= from limit)
504 (or (>= from start) (>= to start)))
505 (setq from (max from start)
506 to (min to limit)
507 vcode (nth 2 elm))
508 (while (<= from to)
509 (aset vec (- from start) vcode)
510 (setq from (1+ from)))))
511 ;; See the comment above.
512 (when (and prev-range-data
513 (>= (cdr (car prev-range-data)) start))
514 (setq from (car (car prev-range-data))
515 to (cdr (car prev-range-data))
516 vcode (cdr prev-range-data))
517 (while (<= from to)
518 (aset vec (- from start) vcode)
519 (setq from (1+ from))))
520 (setq prev-range-data nil)
521 (if val-code
522 (aset vec (- range start) val-code))
523 (while (and (setq elt (car tail) range (car elt))
524 (integerp range)
525 (<= range limit))
526 (setq new-val (funcall val-func (nth prop-idx elt)))
527 (if (not (eq val new-val))
528 (setq val new-val
529 val-code (if val (unidata-encode-val
530 val-list val (and (eq prop 'bidi-class)
531 bidi-warning)))))
532 (if val-code
533 (aset vec (- range start) val-code))
534 (setq tail (cdr tail)))
535 (setq str "\002" val-code -1 count 0)
536 (mapc #'(lambda (x)
537 (if (= val-code x)
538 (setq count (1+ count))
539 (if (> count 2)
540 (setq str (concat str (string val-code
541 (+ count 128))))
542 (if (= count 2)
543 (setq str (concat str (string val-code val-code)))
544 (if (= count 1)
545 (setq str (concat str (string val-code))))))
546 (setq val-code x count 1)))
547 vec)
548 (if (= count 128)
549 (if val
550 (set-char-table-range table (cons start limit) val-code))
551 (if (= val-code 0)
552 (set-char-table-range table (cons start limit) str)
553 (if (> count 2)
554 (setq str (concat str (string val-code (+ count 128))))
555 (if (= count 2)
556 (setq str (concat str (string val-code val-code)))
557 (setq str (concat str (string val-code)))))
558 (set-char-table-range table (cons start limit) str))))))
559
560 (set-char-table-extra-slot table 0 prop)
561 (set-char-table-extra-slot table 4 (vconcat (mapcar 'car val-list)))
562 table))
563
564 (defun unidata-gen-table-symbol (prop default-value val-list)
565 (let ((table (unidata-gen-table prop
566 #'(lambda (x) (and (> (length x) 0)
567 (intern x)))
568 default-value val-list)))
569 (set-char-table-extra-slot table 1 0)
570 (set-char-table-extra-slot table 2 1)
571 table))
572
573 (defun unidata-gen-table-integer (prop default-value val-list)
574 (let ((table (unidata-gen-table prop
575 #'(lambda (x) (and (> (length x) 0)
576 (string-to-number x)))
577 default-value val-list)))
578 (set-char-table-extra-slot table 1 0)
579 (set-char-table-extra-slot table 2 1)
580 table))
581
582 (defun unidata-gen-table-numeric (prop default-value val-list)
583 (let ((table (unidata-gen-table prop
584 #'(lambda (x)
585 (if (string-match "/" x)
586 (/ (float (string-to-number x))
587 (string-to-number
588 (substring x (match-end 0))))
589 (if (> (length x) 0)
590 (string-to-number x))))
591 default-value val-list)))
592 (set-char-table-extra-slot table 1 0)
593 (set-char-table-extra-slot table 2 2)
594 table))
595
596 \f
597 ;; WORD-LIST TABLE
598
599 ;; If the table is for `name' property, each character in the string
600 ;; is one of these:
601 ;; DIFF-HEAD-CODE (0, 1, or 2):
602 ;; specifies how to decode the following characters.
603 ;; WORD-CODE (3..#x7FF excluding '-', '0'..'9', 'A'..'Z'):
604 ;; specifies an index number into WORD-TABLE (see below)
605 ;; Otherwise (' ', '-', '0'..'9', 'A'..'Z'):
606 ;; specifies a literal word.
607 ;;
608 ;; The 4th slots is a vector:
609 ;; [ WORD-TABLE BLOCK-NAME HANGUL-JAMO-TABLE ]
610 ;; WORD-TABLE is a vector of word symbols.
611 ;; BLOCK-NAME is a vector of name symbols for a block of characters.
612 ;; HANGUL-JAMO-TABLE is `unidata-name-jamo-name-table'.
613
614 ;; Return the difference of symbol list L1 and L2 in this form:
615 ;; (DIFF-HEAD SYM1 SYM2 ...)
616 ;; DIFF-HEAD is ((SAME-HEAD-LENGTH * 16) + SAME-TAIL-LENGTH).
617 ;; Ex: If L1 is (a b c d e f) and L2 is (a g h e f), this function
618 ;; returns ((+ (* 1 16) 2) g h).
619 ;; It means that we can get L2 from L1 by prepending the first element
620 ;; of L1 and appending the last 2 elements of L1 to the list (g h).
621 ;; If L1 and L2 don't have common elements at the head and tail,
622 ;; set DIFF-HEAD to -1 and SYM1 ... to the elements of L2.
623
624 (defun unidata-word-list-diff (l1 l2)
625 (let ((beg 0)
626 (end 0)
627 (len1 (length l1))
628 (len2 (length l2))
629 result)
630 (when (< len1 16)
631 (while (and l1 (eq (car l1) (car l2)))
632 (setq beg (1+ beg)
633 l1 (cdr l1) len1 (1- len1) l2 (cdr l2) len2 (1- len2)))
634 (while (and (< end len1) (< end len2)
635 (eq (nth (- len1 end 1) l1) (nth (- len2 end 1) l2)))
636 (setq end (1+ end))))
637 (if (= (+ beg end) 0)
638 (setq result (list -1))
639 (setq result (list (+ (* beg 16) (+ beg (- len1 end))))))
640 (while (< end len2)
641 (setcdr result (cons (nth (- len2 end 1) l2) (cdr result)))
642 (setq end (1+ end)))
643 result))
644
645 ;; Return a compressed form of the vector VEC. Each element of VEC is
646 ;; a list of symbols of which names can be concatenated to form a
647 ;; character name. This function changes those elements into
648 ;; compressed forms by utilizing the fact that diff of consecutive
649 ;; elements is usually small.
650
651 (defun unidata-word-list-compress (vec)
652 (let (last-elt last-idx diff-head tail elt val)
653 (dotimes (i 128)
654 (setq elt (aref vec i))
655 (when elt
656 (if (null last-elt)
657 (setq diff-head -1
658 val (cons 0 elt))
659 (setq val (unidata-word-list-diff last-elt elt))
660 (if (= (car val) -1)
661 (setq diff-head -1
662 val (cons 0 (cdr val)))
663 (if (eq diff-head (car val))
664 (setq val (cons 2 (cdr val)))
665 (setq diff-head (car val))
666 (if (>= diff-head 0)
667 (setq val (cons 1 val))))))
668 (aset vec i val)
669 (setq last-idx i last-elt elt)))
670 (if (not last-idx)
671 (setq vec nil)
672 (if (< last-idx 127)
673 (let ((shorter (make-vector (1+ last-idx) nil)))
674 (dotimes (i (1+ last-idx))
675 (aset shorter i (aref vec i)))
676 (setq vec shorter))))
677 vec))
678
679 ;; Encode the word index IDX into a characters code that can be
680 ;; embedded in a string.
681
682 (defsubst unidata-encode-word (idx)
683 ;; Exclude 0, 1, 2.
684 (+ idx 3))
685
686 ;; Decode the character code CODE (that is embedded in a string) into
687 ;; the corresponding word name by looking up WORD-TABLE.
688
689 (defsubst unidata-decode-word (code word-table)
690 (setq code (- code 3))
691 (if (< code (length word-table))
692 (aref word-table code)))
693
694 ;; Table of short transliterated name symbols of Hangul Jamo divided
695 ;; into Choseong, Jungseong, and Jongseong.
696
697 (defconst unidata-name-jamo-name-table
698 [[G GG N D DD R M B BB S SS nil J JJ C K T P H]
699 [A AE YA YAE EO E YEO YE O WA WAE OE YO U WEO WE WI YU EU YI I]
700 [G GG GS N NJ NH D L LG LM LB LS LT LP LH M B BS S SS NG J C K T P H]])
701
702 ;; Return a name of CHAR. VAL is the current value of (aref TABLE
703 ;; CHAR).
704
705 (defun unidata-get-name (char val table)
706 (cond
707 ((stringp val)
708 (if (> (aref val 0) 0)
709 val
710 (let* ((first-char (lsh (lsh char -7) 7))
711 (word-table (aref (char-table-extra-slot table 4) 0))
712 (i 1)
713 (len (length val))
714 (vec (make-vector 128 nil))
715 (idx 0)
716 (case-fold-search nil)
717 c word-list tail-list last-list word diff-head)
718 (while (< i len)
719 (setq c (aref val i))
720 (if (< c 3)
721 (progn
722 (if (or word-list tail-list)
723 (aset vec idx
724 (setq last-list (nconc word-list tail-list))))
725 (setq i (1+ i) idx (1+ idx)
726 word-list nil tail-list nil)
727 (if (> c 0)
728 (let ((l last-list))
729 (if (= c 1)
730 (setq diff-head
731 (prog1 (aref val i) (setq i (1+ i)))))
732 (setq tail-list (nthcdr (% diff-head 16) last-list))
733 (dotimes (i (/ diff-head 16))
734 (setq word-list (nconc word-list (list (car l)))
735 l (cdr l))))))
736 (setq word-list
737 (nconc word-list
738 (list (symbol-name
739 (unidata-decode-word c word-table))))
740 i (1+ i))))
741 (if (or word-list tail-list)
742 (aset vec idx (nconc word-list tail-list)))
743 (setq val nil)
744 (dotimes (i 128)
745 (setq c (+ first-char i))
746 (let ((name (aref vec i)))
747 (if name
748 (let ((tail (cdr (setq name (copy-sequence name))))
749 elt)
750 (while tail
751 (setq elt (car tail))
752 (or (string= elt "-")
753 (progn
754 (setcdr tail (cons elt (cdr tail)))
755 (setcar tail " ")))
756 (setq tail (cddr tail)))
757 (setq name (apply 'concat name))))
758 (aset table c name)
759 (if (= c char)
760 (setq val name))))
761 val)))
762
763 ((and (integerp val) (> val 0))
764 (let* ((symbol-table (aref (char-table-extra-slot table 4) 1))
765 (sym (aref symbol-table (1- val))))
766 (cond ((eq sym 'HANGUL\ SYLLABLE)
767 (let ((jamo-name-table (aref (char-table-extra-slot table 4) 2)))
768 ;; SIndex = S - SBase
769 (setq char (- char #xAC00))
770 (let ( ;; LIndex = SIndex / NCount
771 (L (/ char 588))
772 ;; VIndex = (SIndex % NCount) * TCount
773 (V (/ (% char 588) 28))
774 ;; TIndex = SIndex % TCount
775 (T (% char 28)))
776 (format "HANGUL SYLLABLE %s%s%s"
777 ;; U+110B is nil in this table.
778 (or (aref (aref jamo-name-table 0) L) "")
779 (aref (aref jamo-name-table 1) V)
780 (if (= T 0) ""
781 (aref (aref jamo-name-table 2) (1- T)))))))
782 ((eq sym 'CJK\ IDEOGRAPH)
783 (format "%s-%04X" sym char))
784 ((eq sym 'CJK\ COMPATIBILITY\ IDEOGRAPH)
785 (format "%s-%04X" sym char))
786 ((eq sym 'VARIATION\ SELECTOR)
787 (format "%s-%d" sym (+ (- char #xe0100) 17))))))))
788
789 ;; Store VAL as the name of CHAR in TABLE.
790
791 (defun unidata-put-name (char val table)
792 (let ((current-val (aref table char)))
793 (if (and (stringp current-val) (= (aref current-val 0) 0))
794 (funcall (char-table-extra-slot table 1) char current-val table))
795 (aset table char val)))
796
797 (defun unidata-get-decomposition (char val table)
798 (cond
799 ((not val)
800 (list char))
801
802 ((consp val)
803 val)
804
805 ((stringp val)
806 (if (> (aref val 0) 0)
807 val
808 (let* ((first-char (lsh (lsh char -7) 7))
809 (word-table (char-table-extra-slot table 4))
810 (i 1)
811 (len (length val))
812 (vec (make-vector 128 nil))
813 (idx 0)
814 (case-fold-search nil)
815 c word-list tail-list last-list word diff-head)
816 (while (< i len)
817 (setq c (aref val i))
818 (if (< c 3)
819 (progn
820 (if (or word-list tail-list)
821 (aset vec idx
822 (setq last-list (nconc word-list tail-list))))
823 (setq i (1+ i) idx (1+ idx)
824 word-list nil tail-list nil)
825 (if (> c 0)
826 (let ((l last-list))
827 (if (= c 1)
828 (setq diff-head
829 (prog1 (aref val i) (setq i (1+ i)))))
830 (setq tail-list (nthcdr (% diff-head 16) last-list))
831 (dotimes (i (/ diff-head 16))
832 (setq word-list (nconc word-list (list (car l)))
833 l (cdr l))))))
834 (setq word-list
835 (nconc word-list
836 (list (or (unidata-decode-word c word-table) c)))
837 i (1+ i))))
838 (if (or word-list tail-list)
839 (aset vec idx (nconc word-list tail-list)))
840 (dotimes (i 128)
841 (aset table (+ first-char i) (aref vec i)))
842 (setq val (aref vec (- char first-char)))
843 (or val (list char)))))
844
845 ;; Hangul syllable
846 ((and (eq val 0) (>= char #xAC00) (<= char #xD7A3))
847 ;; SIndex = S (char) - SBase (#xAC00)
848 (setq char (- char #xAC00))
849 (let (;; L = LBase + SIndex / NCount
850 (L (+ #x1100 (/ char 588)))
851 ;; V = VBase + (SIndex % NCount) * TCount
852 (V (+ #x1161 (/ (% char 588) 28)))
853 ;; LV = SBase + (SIndex / TCount) * TCount
854 (LV (+ #xAC00 (* (/ char 28) 28)))
855 ;; T = TBase + SIndex % TCount
856 (T (+ #x11A7 (% char 28))))
857 (if (= T #x11A7)
858 (list L V)
859 (list LV T))))
860
861 ))
862
863 ;; Store VAL as the decomposition information of CHAR in TABLE.
864
865 (defun unidata-put-decomposition (char val table)
866 (let ((current-val (aref table char)))
867 (if (and (stringp current-val) (= (aref current-val 0) 0))
868 (funcall (char-table-extra-slot table 1) char current-val table))
869 (aset table char val)))
870
871 ;; UnicodeData.txt contains these lines:
872 ;; 0000;<control>;Cc;0;BN;;;;;N;NULL;;;;
873 ;; ...
874 ;; 0020;SPACE;Zs;0;WS;;;;;N;;;;;
875 ;; ...
876 ;; The following command yields a file of about 96K bytes.
877 ;; % gawk -F ';' '{print $1,$2;}' < UnicodeData.txt | gzip > temp.gz
878 ;; With the following function, we can get a file of almost the same
879 ;; size.
880
881 ;; Generate a char-table for character names.
882
883 (defun unidata-gen-table-word-list (prop val-func)
884 (let ((table (make-char-table 'char-code-property-table))
885 (prop-idx (unidata-prop-index prop))
886 (word-list (list nil))
887 word-table
888 block-list block-word-table block-end
889 tail elt range val idx slot)
890 (setq tail unidata-list)
891 (setq block-end -1)
892 (while tail
893 (setq elt (car tail) tail (cdr tail))
894 (setq range (car elt)
895 val (funcall val-func (nth prop-idx elt)))
896 ;; Treat the sequence of "CJK COMPATIBILITY IDEOGRAPH-XXXX" and
897 ;; "VARIATION SELECTOR-XXX" as a block.
898 (if (and (consp val) (eq prop 'name)
899 (or (and (eq (car val) 'CJK)
900 (eq (nth 1 val) 'COMPATIBILITY))
901 (and (>= range #xe0100)
902 (eq (car val) 'VARIATION)
903 (eq (nth 1 val) 'SELECTOR))))
904 (let ((first (car val))
905 (second (nth 1 val))
906 (start range))
907 (while (and (setq elt (car tail) range (car elt)
908 val (funcall val-func (nth prop-idx elt)))
909 (consp val)
910 (eq first (car val))
911 (eq second (nth 1 val)))
912 (setq block-end range
913 tail (cdr tail)))
914 (setq range (cons start block-end)
915 val (if (eq first 'CJK) 'CJK\ COMPATIBILITY\ IDEOGRAPH
916 'VARIATION\ SELECTOR))))
917
918 (if (consp range)
919 (if val
920 (let ((slot (assq val block-list)))
921 (setq range (cons (car range) (cdr range)))
922 (setq block-end (cdr range))
923 (if slot
924 (nconc slot (list range))
925 (push (list val range) block-list))))
926 (let* ((start (lsh (lsh range -7) 7))
927 (limit (+ start 127))
928 (first tail)
929 (vec (make-vector 128 nil))
930 c name len)
931 (if (<= start block-end)
932 ;; START overlap with the previous block.
933 (aset table range (nth prop-idx elt))
934 (if val
935 (aset vec (- range start) val))
936 (while (and (setq elt (car tail) range (car elt))
937 (integerp range)
938 (<= range limit))
939 (setq val (funcall val-func (nth prop-idx elt)))
940 (if val
941 (aset vec (- range start) val))
942 (setq tail (cdr tail)))
943 (setq vec (unidata-word-list-compress vec))
944 (when vec
945 (dotimes (i (length vec))
946 (dolist (elt (aref vec i))
947 (if (symbolp elt)
948 (cl-incf (alist-get elt (cdr word-list) 0)))))
949 (set-char-table-range table (cons start limit) vec))))))
950 (setq word-list (sort (cdr word-list)
951 #'(lambda (x y) (> (cdr x) (cdr y)))))
952 (setq tail word-list idx 0)
953 (while tail
954 (setcdr (car tail) (unidata-encode-word idx))
955 (setq idx (1+ idx) tail (cdr tail)))
956 (setq word-table (make-vector (length word-list) nil))
957 (setq idx 0)
958 (dolist (elt word-list)
959 (aset word-table idx (car elt))
960 (setq idx (1+ idx)))
961
962 (if (and (eq prop 'decomposition)
963 (> idx 32))
964 (error "Too many symbols in decomposition data"))
965
966 (dotimes (i (/ #x110000 128))
967 (let* ((idx (* i 128))
968 (vec (aref table idx)))
969 (when (vectorp vec)
970 (dotimes (i (length vec))
971 (let ((tail (aref vec i))
972 elt code)
973 (if (not tail)
974 (aset vec i "\0")
975 (while tail
976 (setq elt (car tail)
977 code (if (integerp elt) elt
978 (cdr (assq elt word-list))))
979 (setcar tail (string code))
980 (setq tail (cdr tail)))
981 (aset vec i (mapconcat 'identity (aref vec i) "")))))
982 (set-char-table-range
983 table (cons idx (+ idx 127))
984 (mapconcat 'identity vec "")))))
985
986 (setq block-word-table (make-vector (length block-list) nil))
987 (setq idx 0)
988 (dolist (elt block-list)
989 (dolist (e (cdr elt))
990 (set-char-table-range table e (1+ idx)))
991 (aset block-word-table idx (car elt))
992 (setq idx (1+ idx)))
993
994 (set-char-table-extra-slot table 0 prop)
995 (set-char-table-extra-slot table 4 (cons word-table block-word-table))
996 table))
997
998 (defun unidata-split-name (str)
999 (if (symbolp str)
1000 str
1001 (let ((len (length str))
1002 (l nil)
1003 (idx 0)
1004 c)
1005 (if (or (= len 0)
1006 ;; Unicode Standard, paragraph 4.8: "For all other
1007 ;; Unicode code points of all other types (Control,
1008 ;; Private-Use, Surrogate, Noncharacter, and Reserved),
1009 ;; the value of the Name property is the null string."
1010 ;; We already handle elsewhere all the characters except
1011 ;; Cc, Control characters, which are handled here.
1012 (string= str "<control>"))
1013 nil
1014 (dotimes (i len)
1015 (setq c (aref str i))
1016 (if (= c 32)
1017 (setq l (cons (intern (substring str idx i)) l)
1018 idx (1+ i))
1019 (if (and (= c ?-) (< idx i)
1020 (< (1+ i) len) (/= (aref str (1+ i)) 32))
1021 (setq l (cons '- (cons (intern (substring str idx i)) l))
1022 idx (1+ i)))))
1023 (nreverse (cons (intern (substring str idx)) l))))))
1024
1025 (defun unidata--ensure-compiled (&rest funcs)
1026 (dolist (fun funcs)
1027 (or (byte-code-function-p (symbol-function fun))
1028 (byte-compile fun))))
1029
1030 (defun unidata-gen-table-name (prop &rest ignore)
1031 (let* ((table (unidata-gen-table-word-list prop 'unidata-split-name))
1032 (word-tables (char-table-extra-slot table 4)))
1033 (unidata--ensure-compiled 'unidata-get-name 'unidata-put-name)
1034 (set-char-table-extra-slot table 1 (symbol-function 'unidata-get-name))
1035 (set-char-table-extra-slot table 2 (symbol-function 'unidata-put-name))
1036
1037 (if (eq prop 'name)
1038 (set-char-table-extra-slot table 4
1039 (vector (car word-tables)
1040 (cdr word-tables)
1041 unidata-name-jamo-name-table))
1042 (set-char-table-extra-slot table 4
1043 (vector (car word-tables))))
1044 table))
1045
1046 (defun unidata-split-decomposition (str)
1047 (if (symbolp str)
1048 str
1049 (let ((len (length str))
1050 (l nil)
1051 (idx 0)
1052 c)
1053 (if (= len 0)
1054 nil
1055 (dotimes (i len)
1056 (setq c (aref str i))
1057 (if (= c 32)
1058 (setq l (if (= (aref str idx) ?<)
1059 (cons (intern (substring str (1+ idx) (1- i))) l)
1060 (cons (string-to-number (substring str idx i) 16) l))
1061 idx (1+ i))))
1062 (if (= (aref str idx) ?<)
1063 (setq l (cons (intern (substring str (1+ idx) (1- len))) l))
1064 (setq l (cons (string-to-number (substring str idx len) 16) l)))
1065 (nreverse l)))))
1066
1067
1068 (defun unidata-gen-table-decomposition (prop &rest ignore)
1069 (let* ((table (unidata-gen-table-word-list prop 'unidata-split-decomposition))
1070 (word-tables (char-table-extra-slot table 4)))
1071 (unidata--ensure-compiled 'unidata-get-decomposition
1072 'unidata-put-decomposition)
1073 (set-char-table-extra-slot table 1
1074 (symbol-function 'unidata-get-decomposition))
1075 (set-char-table-extra-slot table 2
1076 (symbol-function 'unidata-put-decomposition))
1077 (set-char-table-extra-slot table 4 (car word-tables))
1078 table))
1079
1080
1081 \f
1082 (defun unidata-describe-general-category (val)
1083 (cdr (assq val
1084 '((nil . "Uknown")
1085 (Lu . "Letter, Uppercase")
1086 (Ll . "Letter, Lowercase")
1087 (Lt . "Letter, Titlecase")
1088 (Lm . "Letter, Modifier")
1089 (Lo . "Letter, Other")
1090 (Mn . "Mark, Nonspacing")
1091 (Mc . "Mark, Spacing Combining")
1092 (Me . "Mark, Enclosing")
1093 (Nd . "Number, Decimal Digit")
1094 (Nl . "Number, Letter")
1095 (No . "Number, Other")
1096 (Pc . "Punctuation, Connector")
1097 (Pd . "Punctuation, Dash")
1098 (Ps . "Punctuation, Open")
1099 (Pe . "Punctuation, Close")
1100 (Pi . "Punctuation, Initial quote")
1101 (Pf . "Punctuation, Final quote")
1102 (Po . "Punctuation, Other")
1103 (Sm . "Symbol, Math")
1104 (Sc . "Symbol, Currency")
1105 (Sk . "Symbol, Modifier")
1106 (So . "Symbol, Other")
1107 (Zs . "Separator, Space")
1108 (Zl . "Separator, Line")
1109 (Zp . "Separator, Paragraph")
1110 (Cc . "Other, Control")
1111 (Cf . "Other, Format")
1112 (Cs . "Other, Surrogate")
1113 (Co . "Other, Private Use")
1114 (Cn . "Other, Not Assigned")))))
1115
1116 (defun unidata-describe-canonical-combining-class (val)
1117 (cdr (assq val
1118 '((0 . "Spacing, split, enclosing, reordrant, and Tibetan subjoined")
1119 (1 . "Overlays and interior")
1120 (7 . "Nuktas")
1121 (8 . "Hiragana/Katakana voicing marks")
1122 (9 . "Viramas")
1123 (10 . "Start of fixed position classes")
1124 (199 . "End of fixed position classes")
1125 (200 . "Below left attached")
1126 (202 . "Below attached")
1127 (204 . "Below right attached")
1128 (208 . "Left attached (reordrant around single base character)")
1129 (210 . "Right attached")
1130 (212 . "Above left attached")
1131 (214 . "Above attached")
1132 (216 . "Above right attached")
1133 (218 . "Below left")
1134 (220 . "Below")
1135 (222 . "Below right")
1136 (224 . "Left (reordrant around single base character)")
1137 (226 . "Right")
1138 (228 . "Above left")
1139 (230 . "Above")
1140 (232 . "Above right")
1141 (233 . "Double below")
1142 (234 . "Double above")
1143 (240 . "Below (iota subscript)")))))
1144
1145 (defun unidata-describe-bidi-class (val)
1146 (cdr (assq val
1147 '((L . "Left-to-Right")
1148 (LRE . "Left-to-Right Embedding")
1149 (LRO . "Left-to-Right Override")
1150 (R . "Right-to-Left")
1151 (AL . "Right-to-Left Arabic")
1152 (RLE . "Right-to-Left Embedding")
1153 (RLO . "Right-to-Left Override")
1154 (PDF . "Pop Directional Format")
1155 (LRI . "Left-to-Right Isolate")
1156 (RLI . "Right-to-Left Isolate")
1157 (FSI . "First Strong Isolate")
1158 (PDI . "Pop Directional Isolate")
1159 (EN . "European Number")
1160 (ES . "European Number Separator")
1161 (ET . "European Number Terminator")
1162 (AN . "Arabic Number")
1163 (CS . "Common Number Separator")
1164 (NSM . "Non-Spacing Mark")
1165 (BN . "Boundary Neutral")
1166 (B . "Paragraph Separator")
1167 (S . "Segment Separator")
1168 (WS . "Whitespace")
1169 (ON . "Other Neutrals")))))
1170
1171 (defun unidata-describe-decomposition (val)
1172 (mapconcat
1173 #'(lambda (x)
1174 (if (symbolp x) (symbol-name x)
1175 (concat (string ?')
1176 (compose-string (string x) 0 1 (string ?\t x ?\t))
1177 (string ?'))))
1178 val " "))
1179
1180 (defun unidata-describe-bidi-bracket-type (val)
1181 (cdr (assq val
1182 '((n . "Not a paired bracket character.")
1183 (o . "Opening paired bracket character.")
1184 (c . "Closing paired bracket character.")))))
1185
1186 (defun unidata-gen-mirroring-list ()
1187 (let ((head (list nil))
1188 tail)
1189 (with-temp-buffer
1190 (insert-file-contents (expand-file-name "BidiMirroring.txt" unidata-dir))
1191 (goto-char (point-min))
1192 (setq tail head)
1193 (while (re-search-forward "^\\([0-9A-F]+\\);\\s +\\([0-9A-F]+\\)" nil t)
1194 (let ((char (string-to-number (match-string 1) 16))
1195 (mirror (match-string 2)))
1196 (setq tail (setcdr tail (list (list char mirror)))))))
1197 (cdr head)))
1198
1199 (defun unidata-gen-brackets-list ()
1200 (let ((head (list nil))
1201 tail)
1202 (with-temp-buffer
1203 (insert-file-contents (expand-file-name "BidiBrackets.txt" unidata-dir))
1204 (goto-char (point-min))
1205 (setq tail head)
1206 (while (re-search-forward
1207 "^\\([0-9A-F]+\\);\\s +\\([0-9A-F]+\\);\\s +\\([oc]\\)"
1208 nil t)
1209 (let ((char (string-to-number (match-string 1) 16))
1210 (paired (match-string 2)))
1211 (setq tail (setcdr tail (list (list char paired)))))))
1212 (cdr head)))
1213
1214 (defun unidata-gen-bracket-type-list ()
1215 (let ((head (list nil))
1216 tail)
1217 (with-temp-buffer
1218 (insert-file-contents (expand-file-name "BidiBrackets.txt" unidata-dir))
1219 (goto-char (point-min))
1220 (setq tail head)
1221 (while (re-search-forward
1222 "^\\([0-9A-F]+\\);\\s +\\([0-9A-F]+\\);\\s +\\([oc]\\)"
1223 nil t)
1224 (let ((char (string-to-number (match-string 1) 16))
1225 (type (match-string 3)))
1226 (setq tail (setcdr tail (list (list char type)))))))
1227 (cdr head)))
1228
1229 ;; Verify if we can retrieve correct values from the generated
1230 ;; char-tables.
1231 ;;
1232 ;; Use like this:
1233 ;;
1234 ;; (let ((unidata-dir "/path/to/admin/unidata"))
1235 ;; (unidata-setup-list "unidata.txt")
1236 ;; (unidata-check))
1237
1238 (defun unidata-check ()
1239 (dolist (elt unidata-prop-alist)
1240 (let* ((prop (car elt))
1241 (index (unidata-prop-index prop))
1242 (generator (unidata-prop-generator prop))
1243 (default-value (unidata-prop-default prop))
1244 (val-list (unidata-prop-val-list prop))
1245 (table (progn
1246 (message "Generating %S table..." prop)
1247 (funcall generator prop default-value val-list)))
1248 (decoder (char-table-extra-slot table 1))
1249 (alist (and (functionp index)
1250 (funcall index)))
1251 (check #x400))
1252 (dolist (e unidata-list)
1253 (let* ((char (car e))
1254 (val1
1255 (if alist (nth 1 (assoc char alist))
1256 (nth index e)))
1257 val2)
1258 (if (and (stringp val1) (= (length val1) 0))
1259 (setq val1 nil))
1260 (unless (or (consp char)
1261 (integerp decoder))
1262 (setq val2
1263 (cond ((functionp decoder)
1264 (funcall decoder char (aref table char) table))
1265 (t ; must be nil
1266 (aref table char))))
1267 (if val1
1268 (cond ((eq generator 'unidata-gen-table-symbol)
1269 (setq val1 (intern val1)))
1270 ((eq generator 'unidata-gen-table-integer)
1271 (setq val1 (string-to-number val1)))
1272 ((eq generator 'unidata-gen-table-character)
1273 (setq val1 (string-to-number val1 16)))
1274 ((eq generator 'unidata-gen-table-decomposition)
1275 (setq val1 (unidata-split-decomposition val1))))
1276 (cond ((eq prop 'decomposition)
1277 (setq val1 (list char)))
1278 ((eq prop 'bracket-type)
1279 (setq val1 'n))))
1280 (when (>= char check)
1281 (message "%S %04X" prop check)
1282 (setq check (+ check #x400)))
1283 (or (equal val1 val2)
1284 ;; <control> characters get a 'name' property of nil
1285 (and (eq prop 'name) (string= val1 "<control>") (null val2))
1286 (insert (format "> %04X %S\n< %04X %S\n"
1287 char val1 char val2)))
1288 (sit-for 0)))))))
1289
1290 ;; The entry function. It generates files described in the header
1291 ;; comment of this file.
1292
1293 ;; Write files (charprop.el, uni-*.el) to dest-dir (default PWD),
1294 ;; using as input files from data-dir, and
1295 ;; unidata-text-file (default "unidata.txt" in PWD).
1296 (defun unidata-gen-files (&optional data-dir dest-dir unidata-text-file)
1297 (or data-dir
1298 (setq data-dir (pop command-line-args-left)
1299 dest-dir (or (pop command-line-args-left) default-directory)
1300 unidata-text-file (or (pop command-line-args-left)
1301 (expand-file-name "unidata.txt"))))
1302 (let ((coding-system-for-write 'utf-8-unix)
1303 (charprop-file (expand-file-name "charprop.el" dest-dir))
1304 (unidata-dir data-dir))
1305 (dolist (elt unidata-prop-alist)
1306 (let* ((prop (car elt))
1307 (file (expand-file-name (unidata-prop-file prop) dest-dir)))
1308 (if (file-exists-p file)
1309 (delete-file file))))
1310 (unidata-setup-list unidata-text-file)
1311 (with-temp-file charprop-file
1312 (insert ";; Automatically generated by unidata-gen.el.\n")
1313 (dolist (elt unidata-prop-alist)
1314 (let* ((prop (car elt))
1315 (generator (unidata-prop-generator prop))
1316 (file (expand-file-name (unidata-prop-file prop) dest-dir))
1317 (basename (file-name-nondirectory file))
1318 (docstring (unidata-prop-docstring prop))
1319 (describer (unidata-prop-describer prop))
1320 (default-value (unidata-prop-default prop))
1321 (val-list (unidata-prop-val-list prop))
1322 ;; Avoid creating backup files for those uni-*.el files
1323 ;; that hold more than one table.
1324 (backup-inhibited t)
1325 table)
1326 ;; Filename in this comment line is extracted by sed in
1327 ;; Makefile.
1328 (insert (format ";; FILE: %s\n" basename))
1329 (insert (format "(define-char-code-property '%S %S\n %S)\n"
1330 prop basename docstring))
1331 (with-temp-buffer
1332 (message "Generating %s..." file)
1333 (when (file-exists-p file)
1334 (insert-file-contents file)
1335 (goto-char (point-max))
1336 (search-backward ";; Local Variables:"))
1337 (setq table (funcall generator prop default-value val-list))
1338 (when describer
1339 (unless (subrp (symbol-function describer))
1340 (unidata--ensure-compiled describer)
1341 (setq describer (symbol-function describer)))
1342 (set-char-table-extra-slot table 3 describer))
1343 (if (bobp)
1344 (insert ";; Copyright (C) 1991-2014 Unicode, Inc.
1345 ;; This file was generated from the Unicode data files at
1346 ;; http://www.unicode.org/Public/UNIDATA/.
1347 ;; See lisp/international/README for the copyright and permission notice.\n"))
1348 (insert (format "(define-char-code-property '%S\n %S\n %S)\n"
1349 prop table docstring))
1350 (if (eobp)
1351 (insert ";; Local Variables:\n"
1352 ";; coding: utf-8\n"
1353 ";; version-control: never\n"
1354 ";; no-byte-compile: t\n"
1355 ";; no-update-autoloads: t\n"
1356 ";; End:\n\n"
1357 (format ";; %s ends here\n" basename)))
1358 (write-file file)
1359 (message "Generating %s...done" file))))
1360 (message "Writing %s..." charprop-file)
1361 (insert ";; Local Variables:\n"
1362 ";; coding: utf-8\n"
1363 ";; version-control: never\n"
1364 ";; no-byte-compile: t\n"
1365 ";; no-update-autoloads: t\n"
1366 ";; End:\n\n"
1367 (format ";; %s ends here\n"
1368 (file-name-nondirectory charprop-file))))))
1369
1370 \f
1371
1372 ;;; unidata-gen.el ends here