1 ;; unidata-gen.el -- Create files containing character property data.
3 ;; Copyright (C) 2008-2014 Free Software Foundation, Inc.
5 ;; Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011
6 ;; National Institute of Advanced Industrial Science and Technology (AIST)
7 ;; Registration Number H13PRO009
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
28 ;; This file must be byte-compilable/loadable by `temacs' and also
29 ;; the entry function `unidata-gen-files' must be runnable by `temacs'.
31 ;; FILES TO BE GENERATED
33 ;; The entry function `unidata-gen-files' generates these files in
34 ;; in directory specified by its dest-dir argument.
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.
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.
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.
58 ;; FORMAT OF A CHAR TABLE
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.
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
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
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
91 (eval-when-compile (require 'cl-lib))
93 (defvar unidata-list nil)
95 ;; Name of the directory containing files of Unicode Character Database.
97 ;; Dynamically bound in unidata-gen-files.
98 (defvar unidata-dir nil)
100 (defun unidata-setup-list (unidata-text-file)
101 (let* ((table (list nil))
103 (block-names '(("^<CJK Ideograph" . CJK\ IDEOGRAPH)
104 ("^<Hangul Syllable" . HANGUL\ SYLLABLE)
105 ("^<.*Surrogate" . nil)
106 ("^<.*Private Use" . PRIVATE\ USE)))
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))
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))
120 (setq val (read (current-buffer))
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))
132 (setq val (read (current-buffer))
134 block-name (cadr val)
137 (if (string-match (caar l) block-name)
138 (setq name (cdar l) l nil)
141 ;; As this is a surrogate pair range, ignore it.
143 (setcar val (cons first char))
144 (setcar (cdr val) name))))
147 (setcdr tail (list val))
148 (setq tail (cdr tail))))
150 (setq unidata-list (cdr table))))
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
170 (defconst unidata-prop-alist
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\")."
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
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
197 4 unidata-gen-table-symbol "uni-bidi.el"
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
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))
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)
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\".")
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\".")
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\".")
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'."
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\").")
248 11 unidata-gen-table-name "uni-comment.el"
249 "Unicode ISO 10646 comment.
250 Property value is a string.")
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."
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."
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."
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.")
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."
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
291 ;; The order of elements must be in sync with bidi_bracket_type_t
292 ;; in src/dispextern.h.
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)))
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.
311 ;; A char-table divides character code space (#x0..#x3FFFFF) into
312 ;; #x8000 blocks (each block contains 128 characters).
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.
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.
324 ;; This kind of char-table has these extra slots:
325 ;; 1st: the property symbol
327 ;; 3rd: 0 (corresponding to uniprop_encode_character in chartab.c)
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))
335 elt range val idx slot)
336 (if (functionp prop-idx)
337 (setq tail (funcall prop-idx)
340 (setq elt (car tail) tail (cdr tail))
341 (setq range (car elt)
342 val (nth prop-idx elt))
343 (if (= (length val) 0)
345 (setq val (string-to-number val 16)))
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)
354 (aset vec (setq last-index (setq first-index (- range start)))
356 (while (and (setq elt (car tail) range (car elt))
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))
364 (setq first-index last-index)))
365 (setq tail (cdr tail)))
367 (let ((str (string 1 first-index))
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))))))
374 (set-char-table-extra-slot table 0 prop)
375 (set-char-table-extra-slot table 2 0)
382 ;; If many characters of successive character codes have the same
383 ;; property value, we use a char-table described here to store the
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.
390 ;; A char-table divides character code space (#x0..#x3FFFFF) into
391 ;; #x8000 blocks (each block contains 128 characters).
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.
397 ;; The first character of the string is ?\002.
398 ;; The following characters has this form:
399 ;; ( VAL-CODE RUN-LENGTH ? ) +
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.
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
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))
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.
425 (defun unidata-encode-val (val-list val &optional warn)
426 (let ((slot (assoc val val-list))
430 (if warn (message warn val))
431 (setq val-code (length val-list))
432 (nconc val-list (list (cons val val-code)))
435 ;; Generate a char-table for the character property PROP.
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.
450 ** Found new bidi-class '%s', please update bidi.c and dispextern.h")
451 tail elt range val val-code idx slot
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) ...)
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)))
471 (if (functionp prop-idx)
472 (setq tail (funcall prop-idx)
474 (setq tail unidata-list))
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)
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
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)
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))
518 (aset vec (- from start) vcode)
519 (setq from (1+ from))))
520 (setq prev-range-data nil)
522 (aset vec (- range start) val-code))
523 (while (and (setq elt (car tail) range (car elt))
526 (setq new-val (funcall val-func (nth prop-idx elt)))
527 (if (not (eq val new-val))
529 val-code (if val (unidata-encode-val
530 val-list val (and (eq prop 'bidi-class)
533 (aset vec (- range start) val-code))
534 (setq tail (cdr tail)))
535 (setq str "\002" val-code -1 count 0)
538 (setq count (1+ count))
540 (setq str (concat str (string val-code
543 (setq str (concat str (string val-code val-code)))
545 (setq str (concat str (string val-code))))))
546 (setq val-code x count 1)))
550 (set-char-table-range table (cons start limit) val-code))
552 (set-char-table-range table (cons start limit) str)
554 (setq str (concat str (string val-code (+ count 128))))
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))))))
560 (set-char-table-extra-slot table 0 prop)
561 (set-char-table-extra-slot table 4 (vconcat (mapcar 'car val-list)))
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)
568 default-value val-list)))
569 (set-char-table-extra-slot table 1 0)
570 (set-char-table-extra-slot table 2 1)
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)
582 (defun unidata-gen-table-numeric (prop default-value val-list)
583 (let ((table (unidata-gen-table prop
585 (if (string-match "/" x)
586 (/ (float (string-to-number x))
588 (substring x (match-end 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)
599 ;; If the table is for `name' property, each character in the string
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.
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'.
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.
624 (defun unidata-word-list-diff (l1 l2)
631 (while (and l1 (eq (car l1) (car l2)))
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))))))
641 (setcdr result (cons (nth (- len2 end 1) l2) (cdr result)))
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.
651 (defun unidata-word-list-compress (vec)
652 (let (last-elt last-idx diff-head tail elt val)
654 (setq elt (aref vec i))
659 (setq val (unidata-word-list-diff last-elt elt))
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))
667 (setq val (cons 1 val))))))
669 (setq last-idx i last-elt elt)))
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))))
679 ;; Encode the word index IDX into a characters code that can be
680 ;; embedded in a string.
682 (defsubst unidata-encode-word (idx)
686 ;; Decode the character code CODE (that is embedded in a string) into
687 ;; the corresponding word name by looking up WORD-TABLE.
689 (defsubst unidata-decode-word (code word-table)
690 (setq code (- code 3))
691 (if (< code (length word-table))
692 (aref word-table code)))
694 ;; Table of short transliterated name symbols of Hangul Jamo divided
695 ;; into Choseong, Jungseong, and Jongseong.
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]])
702 ;; Return a name of CHAR. VAL is the current value of (aref TABLE
705 (defun unidata-get-name (char val table)
708 (if (> (aref val 0) 0)
710 (let* ((first-char (lsh (lsh char -7) 7))
711 (word-table (aref (char-table-extra-slot table 4) 0))
714 (vec (make-vector 128 nil))
716 (case-fold-search nil)
717 c word-list tail-list last-list word diff-head)
719 (setq c (aref val i))
722 (if (or word-list tail-list)
724 (setq last-list (nconc word-list tail-list))))
725 (setq i (1+ i) idx (1+ idx)
726 word-list nil tail-list nil)
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)))
739 (unidata-decode-word c word-table))))
741 (if (or word-list tail-list)
742 (aset vec idx (nconc word-list tail-list)))
745 (setq c (+ first-char i))
746 (let ((name (aref vec i)))
748 (let ((tail (cdr (setq name (copy-sequence name))))
751 (setq elt (car tail))
752 (or (string= elt "-")
754 (setcdr tail (cons elt (cdr tail)))
756 (setq tail (cddr tail)))
757 (setq name (apply 'concat name))))
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
772 ;; VIndex = (SIndex % NCount) * TCount
773 (V (/ (% char 588) 28))
774 ;; TIndex = SIndex % TCount
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)
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))))))))
789 ;; Store VAL as the name of CHAR in TABLE.
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)))
797 (defun unidata-get-decomposition (char val table)
806 (if (> (aref val 0) 0)
808 (let* ((first-char (lsh (lsh char -7) 7))
809 (word-table (char-table-extra-slot table 4))
812 (vec (make-vector 128 nil))
814 (case-fold-search nil)
815 c word-list tail-list last-list word diff-head)
817 (setq c (aref val i))
820 (if (or word-list tail-list)
822 (setq last-list (nconc word-list tail-list))))
823 (setq i (1+ i) idx (1+ idx)
824 word-list nil tail-list nil)
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)))
836 (list (or (unidata-decode-word c word-table) c)))
838 (if (or word-list tail-list)
839 (aset vec idx (nconc word-list tail-list)))
841 (aset table (+ first-char i) (aref vec i)))
842 (setq val (aref vec (- char first-char)))
843 (or val (list char)))))
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))))
863 ;; Store VAL as the decomposition information of CHAR in TABLE.
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)))
871 ;; UnicodeData.txt contains these lines:
872 ;; 0000;<control>;Cc;0;BN;;;;;N;NULL;;;;
874 ;; 0020;SPACE;Zs;0;WS;;;;;N;;;;;
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
881 ;; Generate a char-table for character names.
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))
888 block-list block-word-table block-end
889 tail elt range val idx slot)
890 (setq tail unidata-list)
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))
907 (while (and (setq elt (car tail) range (car elt)
908 val (funcall val-func (nth prop-idx elt)))
911 (eq second (nth 1 val)))
912 (setq block-end range
914 (setq range (cons start block-end)
915 val (if (eq first 'CJK) 'CJK\ COMPATIBILITY\ IDEOGRAPH
916 'VARIATION\ SELECTOR))))
920 (let ((slot (assq val block-list)))
921 (setq range (cons (car range) (cdr range)))
922 (setq block-end (cdr range))
924 (nconc slot (list range))
925 (push (list val range) block-list))))
926 (let* ((start (lsh (lsh range -7) 7))
927 (limit (+ start 127))
929 (vec (make-vector 128 nil))
931 (if (<= start block-end)
932 ;; START overlap with the previous block.
933 (aset table range (nth prop-idx elt))
935 (aset vec (- range start) val))
936 (while (and (setq elt (car tail) range (car elt))
939 (setq val (funcall val-func (nth prop-idx elt)))
941 (aset vec (- range start) val))
942 (setq tail (cdr tail)))
943 (setq vec (unidata-word-list-compress vec))
945 (dotimes (i (length vec))
946 (dolist (elt (aref vec i))
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)
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))
958 (dolist (elt word-list)
959 (aset word-table idx (car elt))
962 (if (and (eq prop 'decomposition)
964 (error "Too many symbols in decomposition data"))
966 (dotimes (i (/ #x110000 128))
967 (let* ((idx (* i 128))
968 (vec (aref table idx)))
970 (dotimes (i (length vec))
971 (let ((tail (aref vec i))
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 "")))))
986 (setq block-word-table (make-vector (length block-list) nil))
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))
994 (set-char-table-extra-slot table 0 prop)
995 (set-char-table-extra-slot table 4 (cons word-table block-word-table))
998 (defun unidata-split-name (str)
1001 (let ((len (length str))
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>"))
1015 (setq c (aref str i))
1017 (setq l (cons (intern (substring str idx i)) l)
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))
1023 (nreverse (cons (intern (substring str idx)) l))))))
1025 (defun unidata--ensure-compiled (&rest funcs)
1027 (or (byte-code-function-p (symbol-function fun))
1028 (byte-compile fun))))
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))
1038 (set-char-table-extra-slot table 4
1039 (vector (car word-tables)
1041 unidata-name-jamo-name-table))
1042 (set-char-table-extra-slot table 4
1043 (vector (car word-tables))))
1046 (defun unidata-split-decomposition (str)
1049 (let ((len (length str))
1056 (setq c (aref str i))
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))
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)))
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))
1082 (defun unidata-describe-general-category (val)
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")))))
1116 (defun unidata-describe-canonical-combining-class (val)
1118 '((0 . "Spacing, split, enclosing, reordrant, and Tibetan subjoined")
1119 (1 . "Overlays and interior")
1121 (8 . "Hiragana/Katakana voicing marks")
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")
1135 (222 . "Below right")
1136 (224 . "Left (reordrant around single base character)")
1138 (228 . "Above left")
1140 (232 . "Above right")
1141 (233 . "Double below")
1142 (234 . "Double above")
1143 (240 . "Below (iota subscript)")))))
1145 (defun unidata-describe-bidi-class (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")
1169 (ON . "Other Neutrals")))))
1171 (defun unidata-describe-decomposition (val)
1174 (if (symbolp x) (symbol-name x)
1176 (compose-string (string x) 0 1 (string ?\t x ?\t))
1180 (defun unidata-describe-bidi-bracket-type (val)
1182 '((n . "Not a paired bracket character.")
1183 (o . "Opening paired bracket character.")
1184 (c . "Closing paired bracket character.")))))
1186 (defun unidata-gen-mirroring-list ()
1187 (let ((head (list nil))
1190 (insert-file-contents (expand-file-name "BidiMirroring.txt" unidata-dir))
1191 (goto-char (point-min))
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)))))))
1199 (defun unidata-gen-brackets-list ()
1200 (let ((head (list nil))
1203 (insert-file-contents (expand-file-name "BidiBrackets.txt" unidata-dir))
1204 (goto-char (point-min))
1206 (while (re-search-forward
1207 "^\\([0-9A-F]+\\);\\s +\\([0-9A-F]+\\);\\s +\\([oc]\\)"
1209 (let ((char (string-to-number (match-string 1) 16))
1210 (paired (match-string 2)))
1211 (setq tail (setcdr tail (list (list char paired)))))))
1214 (defun unidata-gen-bracket-type-list ()
1215 (let ((head (list nil))
1218 (insert-file-contents (expand-file-name "BidiBrackets.txt" unidata-dir))
1219 (goto-char (point-min))
1221 (while (re-search-forward
1222 "^\\([0-9A-F]+\\);\\s +\\([0-9A-F]+\\);\\s +\\([oc]\\)"
1224 (let ((char (string-to-number (match-string 1) 16))
1225 (type (match-string 3)))
1226 (setq tail (setcdr tail (list (list char type)))))))
1229 ;; Verify if we can retrieve correct values from the generated
1234 ;; (let ((unidata-dir "/path/to/admin/unidata"))
1235 ;; (unidata-setup-list "unidata.txt")
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))
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)
1252 (dolist (e unidata-list)
1253 (let* ((char (car e))
1255 (if alist (nth 1 (assoc char alist))
1258 (if (and (stringp val1) (= (length val1) 0))
1260 (unless (or (consp char)
1263 (cond ((functionp decoder)
1264 (funcall decoder char (aref table char) table))
1266 (aref table char))))
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)
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)))
1290 ;; The entry function. It generates files described in the header
1291 ;; comment of this file.
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)
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)
1326 ;; Filename in this comment line is extracted by sed in
1328 (insert (format ";; FILE: %s\n" basename))
1329 (insert (format "(define-char-code-property '%S %S\n %S)\n"
1330 prop basename docstring))
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))
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))
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))
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"
1357 (format ";; %s ends here\n" basename)))
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"
1367 (format ";; %s ends here\n"
1368 (file-name-nondirectory charprop-file))))))
1372 ;;; unidata-gen.el ends here