1 ;; unidata-gen.el -- Create files containing character property data.
3 ;; National Institute of Advanced Industrial Science and Technology (AIST)
4 ;; Registration Number H13PRO009
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
25 ;; FILES TO BE GENERATED
27 ;; The entry function `unidata-gen-files' generated these filese in
28 ;; the current directory.
31 ;; It contains a series of forms of this format:
32 ;; (char-code-property-register PROP FILE)
33 ;; where PROP is a symbol representing a character property
34 ;; (name, geneirc-category, etc), and FILE is a name of one of
35 ;; the following files.
37 ;; uni-name.el, uni-cat.el, uni-comb.el, uni-bidi.el
38 ;; It contains a single form of this format:
39 ;; (char-code-property-register PROP CHAR-TABLE)
40 ;; where PROP is the same as above, and CHAR-TABLE is a
41 ;; char-table containing property values in a comporessed format.
43 ;; When they are installed in .../lisp/international/, the file
44 ;; "charprop.el" is preloaded in loadup.el. The other files are
45 ;; automatically loaded when the functions `get-char-code-property'
46 ;; and `put-char-code-property' are called.
48 ;; FORMAT OF A CHAR TABLE
50 ;; We want to make a file size containing a char-table small. We
51 ;; also want to load the file and get a property value fast. We
52 ;; also want to reduce the used memory after loading it. So,
53 ;; instead of naively storing a property value for each character in
54 ;; a char-table (and write it out into a file), we store compressed
55 ;; data in a char-table as below.
57 ;; If succeeding 128*N characters have the same property value, we
58 ;; store that value for them. Otherwise, comporess values for
59 ;; succeeding 128 characters into a single string and store it as a
60 ;; value for those characters. The way of compression depends on a
61 ;; property. See the section "SIMPLE TABLE", "RUN-LENGTH TABLE",
62 ;; and "WORD-LIST TABLE".
64 ;; The char table has four extra slots:
65 ;; 1st: property symbol
66 ;; 2nd: function to call to get a property value
67 ;; 3nd: function to call to put a property value
68 ;; 4th: function to call to get a description of a property value
69 ;; 5th: data referred by the above functions
72 ;; For more detail, see the comments in the section "SIMPLE TABLE"
75 ;; The name of the file UnicodeData.txt.
76 (defconst unidata-text-file
77 (expand-file-name "admin/unidata/UnicodeData.txt" source-directory))
79 ;; List of elements of this form:
80 ;; (CHAR-or-RANGE PROP1 PROP2 ... PROPn)
81 ;; CHAR-or-RANGE: a character code or a cons of character codes
82 ;; PROPn: string representing the nth property value
85 (let* ((table (list nil))
87 (block-names '(("^<CJK Ideograph" . CJK\ IDEOGRAPH)
88 ("^<Hangul Syllable" . HANGUL\ SYLLABLE)
89 ("^<.*Surrogate" . nil)
90 ("^<.*Private Use" . PRIVATE\ USE)))
92 (or (file-readable-p unidata-text-file)
93 (error "File not readable: %s" unidata-text-file))
95 (call-process "sed" unidata-text-file t nil
96 "-e" "s/\\([^;]*\\);\\(.*\\)/(#x\\1 \\\"\\2\\\")/"
97 "-e" "s/;/\\\" \\\"/g")
98 (goto-char (point-min))
101 (setq val (read (current-buffer))
105 ;; Check this kind of block.
106 ;; 4E00;<CJK Ideograph, First>;Lo;0;L;;;;;N;;;;;
107 ;; 9FA5;<CJK Ideograph, Last>;Lo;0;L;;;;;N;;;;;
108 (if (and (= (aref name 0) ?<)
109 (string-match ", First>$" name))
113 (setq val (read (current-buffer))
115 block-name (cadr val)
118 (if (string-match (caar l) block-name)
119 (setq name (cdar l) l nil)
122 ;; As this is a surrogate pair range, ignore it.
124 (setcar val (cons first char))
125 (setcar (cdr val) name))))
128 (setcdr tail (list val))
129 (setq tail (cdr tail))))
133 ;; Alist of this form:
134 ;; (PROP INDEX GENERATOR FILENAME)
135 ;; PROP: character property
136 ;; INDEX: index to each element of unidata-list for PROP
137 ;; GENERATOR: function to generate a char-table
138 ;; FILENAME: filename to store the char-table
139 ;; DESCRIBER: function to call to get a description string of property value
141 (defconst unidata-prop-alist
143 1 unidata-gen-table-name "uni-name.el"
144 "Unicode character name.
145 Property value is a string.")
147 2 unidata-gen-table-symbol "uni-category.el"
148 "Unicode general category.
149 Property value is one of the following symbols:
150 Lu, Ll, Lt, Lm, Lo, Mn, Mc, Me, Nd, Nl, No, Pc, Pd, Ps, Pe, Pi, Pf, Po,
151 Sm, Sc, Sk, So, Zs, Zl, Zp, Cc, Cf, Cs, Co, Cn"
152 unidata-describe-general-category)
153 (canonical-combining-class
154 3 unidata-gen-table-integer "uni-combining.el"
155 "Unicode canonical combining class.
156 Property value is an integer."
157 unidata-describe-canonical-combining-class)
159 4 unidata-gen-table-symbol "uni-bidi.el"
161 Property value is one of the following symbols:
162 L, LRE, LRO, R, AL, RLE, RLO, PDF, EN, ES, ET,
163 AN, CS, NSM, BN, B, S, WS, ON"
164 unidata-describe-bidi-class)
166 5 unidata-gen-table-decomposition "uni-decomposition.el"
167 "Unicode decomposition mapping.
168 Property value is a list of characters. The first element may be
169 one of these symbols representing compatiblity formatting tag:
170 <font>, <noBreak>, <initial>, <medial>, <final>, <isolated>, <circle>,
171 <super>, <sub>, <vertical>, <wide>, <narrow>, <small>, <square>, <fraction>,
173 unidata-describe-decomposition)
175 6 unidata-gen-table-integer "uni-decimal.el"
176 "Unicode numeric value (decimal digit).
177 Property value is an integer.")
179 7 unidata-gen-table-integer "uni-digit.el"
180 "Unicode numeric value (digit).
181 Property value is an integer.")
183 8 unidata-gen-table-symbol "uni-numeric.el"
184 "Unicode numeric value (numeric).
185 Property value is an symbol.")
187 9 unidata-gen-table-symbol "uni-mirrored.el"
188 "Unicode bidi mirrored flag.
189 Property value is a symbol `Y' or `N'.")
191 10 unidata-gen-table-name "uni-old-name.el"
192 "Unicode old names as published in Unicode 1.0.
193 Property value is a string.")
195 11 unidata-gen-table-name "uni-comment.el"
196 "Unicode ISO 10646 comment.
197 Property value is a string.")
199 12 unidata-gen-table-character "uni-uppercase.el"
200 "Unicode simple uppercase mapping.
201 Property value is a character."
204 13 unidata-gen-table-character "uni-lowercase.el"
205 "Unicode simple lowercase mapping.
206 Property value is a character."
209 14 unidata-gen-table-character "uni-titlecase.el"
210 "Unicode simple titlecase mapping.
211 Property value is a character."
214 ;; Functions to access the above data.
215 (defsubst unidata-prop-index (prop) (nth 1 (assq prop unidata-prop-alist)))
216 (defsubst unidata-prop-generator (prop) (nth 2 (assq prop unidata-prop-alist)))
217 (defsubst unidata-prop-file (prop) (nth 3 (assq prop unidata-prop-alist)))
218 (defsubst unidata-prop-docstring (prop) (nth 4 (assq prop unidata-prop-alist)))
219 (defsubst unidata-prop-describer (prop) (nth 5 (assq prop unidata-prop-alist)))
224 ;; If the type of character property value is character, and the
225 ;; values of succeeding character codes are usually different, we use
226 ;; a char-table described here to store such values.
228 ;; If succeeding 128 characters has no property, a char-table has the
229 ;; symbol t is for them. Otherwise a char-table has a string of the
230 ;; following format for them.
232 ;; The first character of the string is FIRST-INDEX.
233 ;; The Nth (N > 0) character of the string is a property value of the
234 ;; character (BLOCk-HEAD + FIRST-INDEX + N - 1), where BLOCK-HEAD is
235 ;; the first of the characters in the block.
237 ;; The 4th extra slot of a char-table is nil.
239 (defun unidata-get-character (char val table)
245 (let* ((len (length val))
246 (block-head (lsh (lsh char -7) 7))
247 (vec (make-vector 128 nil))
248 (first-index (aref val 0)))
249 (dotimes (i (1- len))
250 (let ((elt (aref val (1+ i))))
252 (aset vec (+ first-index i) elt))))
254 (aset table (+ block-head i) (aref vec i)))
255 (aref vec (- char block-head))))))
257 (defun unidata-put-character (char val table)
260 (error "Not an character nor nil: %S" val))
261 (let ((current-val (aref table char)))
262 (unless (eq current-val val)
263 (if (stringp current-val)
264 (funcall (char-table-extra-slot table 1) char current-val table))
265 (aset table char val))))
267 (defun unidata-gen-table-character (prop)
268 (let ((table (make-char-table 'char-code-property-table))
269 (prop-idx (unidata-prop-index prop))
270 (vec (make-vector 128 0))
272 elt range val idx slot)
273 (set-char-table-range table (cons 0 (max-char)) t)
275 (setq elt (car tail) tail (cdr tail))
276 (setq range (car elt)
277 val (nth prop-idx elt))
278 (if (= (length val) 0)
280 (setq val (string-to-number val 16)))
283 (set-char-table-range table range val))
284 (let* ((start (lsh (lsh range -7) 7))
285 (limit (+ start 127))
286 first-index last-index)
289 (aset vec (setq last-index (setq first-index (- range start)))
291 (while (and (setq elt (car tail) range (car elt))
294 (setq val (nth prop-idx elt))
295 (when (> (length val) 0)
296 (aset vec (setq last-index (- range start))
297 (string-to-number val 16))
299 (setq first-index last-index)))
300 (setq tail (cdr tail)))
302 (let ((str (string first-index))
304 (while (<= first-index last-index)
305 (setq str (format "%s%c" str (or (aref vec first-index) 0))
306 first-index (1+ first-index)))
307 (set-char-table-range table (cons start limit) str))))))
309 (set-char-table-extra-slot table 0 prop)
310 (byte-compile 'unidata-get-character)
311 (byte-compile 'unidata-put-character)
312 (set-char-table-extra-slot table 1 (symbol-function 'unidata-get-character))
313 (set-char-table-extra-slot table 2 (symbol-function 'unidata-put-character))
321 ;; If the type of character property value is symbol, integer,
322 ;; boolean, or character, we use a char-table described here to store
325 ;; The 4th extra slot is a vector of property values (VAL-TABLE), and
326 ;; values for succeeding 128 characters are encoded into this
327 ;; character sequence:
328 ;; ( VAL-CODE RUN-LENGTH ? ) +
330 ;; VAL-CODE (0..127):
331 ;; (VAL-CODE - 1) is an index into VAL-TABLE.
332 ;; The value 0 means no-value.
333 ;; RUN-LENGTH (130..255):
334 ;; (RUN-LENGTH - 128) specifies how many characters have the same
335 ;; value. If omitted, it means 1.
338 ;; Return a symbol-type character property value of CHAR. VAL is the
339 ;; current value of (aref TABLE CHAR).
341 (defun unidata-get-symbol (char val table)
342 (let ((val-table (char-table-extra-slot table 4)))
346 (let ((first-char (lsh (lsh char -7) 7))
351 (set-char-table-range table (cons first-char (+ first-char 127))
354 (setq val (aref str idx) idx (1+ idx)
355 count (if (< idx len) (aref str idx) 1))
356 (setq val (and (> val 0) (aref val-table (1- val)))
357 count (if (< count 128)
359 (prog1 (- count 128) (setq idx (1+ idx)))))
362 (aset table first-char val))
363 (if (= first-char char)
365 (setq first-char (1+ first-char))))
368 (aref val-table (1- val))))))
370 ;; Return a integer-type character property value of CHAR. VAL is the
371 ;; current value of (aref TABLE CHAR).
373 (defun unidata-get-integer (char val table)
374 (let ((val-table (char-table-extra-slot table 4)))
375 (cond ((integerp val)
378 (let ((first-char (lsh (lsh char -7) 7))
384 (setq val (aref str idx) idx (1+ idx)
385 count (if (< idx len) (aref str idx) 1))
386 (setq val (and (> val 0) (aref val-table (1- val)))
387 count (if (< count 128)
389 (prog1 (- count 128) (setq idx (1+ idx)))))
391 (aset table first-char val)
392 (if (= first-char char)
394 (setq first-char (1+ first-char))))
397 ;; Store VAL (symbol) as a character property value of CHAR in TABLE.
399 (defun unidata-put-symbol (char val table)
401 (error "Not a symbol: %S" val))
402 (let ((current-val (aref table char)))
403 (unless (eq current-val val)
404 (if (stringp current-val)
405 (funcall (char-table-extra-slot table 1) char current-val table))
406 (aset table char val))))
408 ;; Store VAL (integer) as a character property value of CHAR in TABLE.
410 (defun unidata-put-integer (char val table)
413 (error "Not an integer nor nil: %S" val))
414 (let ((current-val (aref table char)))
415 (unless (eq current-val val)
416 (if (stringp current-val)
417 (funcall (char-table-extra-slot table 1) char current-val table))
418 (aset table char val))))
420 ;; Encode the character property value VAL into an integer value by
421 ;; VAL-LIST. By side effect, VAL-LIST is modified.
422 ;; VAL-LIST has this form:
423 ;; (t (VAL1 . VAL-CODE1) (VAL2 . VAL-CODE2) ...)
424 ;; If VAL is one of VALn, just return VAL-CODEn. Otherwise,
425 ;; VAL-LIST is modified to this:
426 ;; (t (VAL . (1+ VAL-CODE1)) (VAL1 . VAL-CODE1) (VAL2 . VAL-CODE2) ...)
428 (defun unidata-encode-val (val-list val)
429 (let ((slot (assq val val-list))
433 (setq val-code (if (cdr val-list) (1+ (cdr (nth 1 val-list))) 1))
434 (setcdr val-list (cons (cons val val-code) (cdr val-list)))
437 ;; Generate a char-table for the character property PROP.
439 (defun unidata-gen-table (prop val-func default-value)
440 (let ((table (make-char-table 'char-code-property-table))
441 (prop-idx (unidata-prop-index prop))
443 (vec (make-vector 128 0))
444 tail elt range val val-code idx slot)
445 (set-char-table-range table (cons 0 (max-char)) default-value)
446 (setq tail unidata-list)
448 (setq elt (car tail) tail (cdr tail))
449 (setq range (car elt)
450 val (funcall val-func (nth prop-idx elt)))
451 (setq val-code (if val (unidata-encode-val val-list val)))
454 (set-char-table-range table range val))
455 (let* ((start (lsh (lsh range -7) 7))
456 (limit (+ start 127))
460 (aset vec (- range start) val-code))
461 (while (and (setq elt (car tail) range (car elt))
464 (setq new-val (funcall val-func (nth prop-idx elt)))
465 (if (not (eq val new-val))
467 val-code (if val (unidata-encode-val val-list val))))
469 (aset vec (- range start) val-code))
470 (setq tail (cdr tail)))
471 (setq str "" val-code -1 count 0)
474 (setq count (1+ count))
476 (setq str (concat str (string val-code
479 (setq str (concat str (string val-code val-code)))
481 (setq str (concat str (string val-code))))))
482 (setq val-code x count 1)))
486 (set-char-table-range table (cons start limit) val))
488 (set-char-table-range table (cons start limit) str)
490 (setq str (concat str (string val-code (+ count 128))))
492 (setq str (concat str (string val-code val-code)))
493 (setq str (concat str (string val-code)))))
494 (set-char-table-range table (cons start limit) str))))))
496 (setq val-list (nreverse (cdr val-list)))
497 (set-char-table-extra-slot table 0 prop)
498 (set-char-table-extra-slot table 4 (vconcat (mapcar 'car val-list)))
501 (defun unidata-gen-table-symbol (prop)
502 (let ((table (unidata-gen-table prop
503 #'(lambda (x) (and (> (length x) 0)
506 (byte-compile 'unidata-get-symbol)
507 (byte-compile 'unidata-put-symbol)
508 (set-char-table-extra-slot table 1 (symbol-function 'unidata-get-symbol))
509 (set-char-table-extra-slot table 2 (symbol-function 'unidata-put-symbol))
512 (defun unidata-gen-table-integer (prop)
513 (let ((table (unidata-gen-table prop
514 #'(lambda (x) (and (> (length x) 0)
515 (string-to-number x)))
517 (byte-compile 'unidata-get-integer)
518 (byte-compile 'unidata-put-integer)
519 (set-char-table-extra-slot table 1 (symbol-function 'unidata-get-integer))
520 (set-char-table-extra-slot table 2 (symbol-function 'unidata-put-integer))
526 ;; If the table is for `name' property, each character in the string
528 ;; DIFF-HEAD-CODE (0, 1, or 2):
529 ;; specifies how to decode the following characters.
530 ;; WORD-CODE (3..#x7FF excluding '-', '0'..'9', 'A'..'Z'):
531 ;; specifies an index number into WORD-TABLE (see below)
532 ;; Otherwise (' ', '-', '0'..'9', 'A'..'Z'):
533 ;; specifies a literal word.
535 ;; The 4th slots is a vector:
536 ;; [ WORD-TABLE BLOCK-NAME HANGUL-JAMO-TABLE ]
537 ;; WORD-TABLE is a vector of word symbols.
538 ;; BLOCK-NAME is a vector of name symbols for a block of characters.
539 ;; HANGUL-JAMO-TABLE is `unidata-name-jamo-name-table'.
541 ;; Return the difference of symbol list L1 and L2 in this form:
542 ;; (DIFF-HEAD SYM1 SYM2 ...)
543 ;; DIFF-HEAD is ((SAME-HEAD-LENGTH * 16) + SAME-TAIL-LENGTH).
544 ;; Ex: If L1 is (a b c d e f) and L2 is (a g h e f), this function
545 ;; returns ((+ (* 1 16) 2) g h).
546 ;; It means that we can get L2 from L1 by prepending the first element
547 ;; of L1 and appending the last 2 elements of L1 to the list (g h).
548 ;; If L1 and L2 don't have common elements at the head and tail,
549 ;; set DIFF-HEAD to -1 and SYM1 ... to the elements of L2.
551 (defun unidata-word-list-diff (l1 l2)
558 (while (and l1 (eq (car l1) (car l2)))
560 l1 (cdr l1) len1 (1- len1) l2 (cdr l2) len2 (1- len2)))
561 (while (and (< end len1) (< end len2)
562 (eq (nth (- len1 end 1) l1) (nth (- len2 end 1) l2)))
563 (setq end (1+ end))))
564 (if (= (+ beg end) 0)
565 (setq result (list -1))
566 (setq result (list (+ (* beg 16) (+ beg (- len1 end))))))
568 (setcdr result (cons (nth (- len2 end 1) l2) (cdr result)))
572 ;; Return a compressed form of the vector VEC. Each element of VEC is
573 ;; a list of symbols of which names can be concatenated to form a
574 ;; character name. This function changes those elements into
575 ;; compressed forms by utilizing the fact that diff of consecutive
576 ;; elements is usually small.
578 (defun unidata-word-list-compress (vec)
579 (let (last-elt last-idx diff-head tail elt val)
581 (setq elt (aref vec i))
586 (setq val (unidata-word-list-diff last-elt elt))
589 val (cons 0 (cdr val)))
590 (if (eq diff-head (car val))
591 (setq val (cons 2 (cdr val)))
592 (setq diff-head (car val))
594 (setq val (cons 1 val))))))
596 (setq last-idx i last-elt elt)))
600 (let ((shorter (make-vector (1+ last-idx) nil)))
601 (dotimes (i (1+ last-idx))
602 (aset shorter i (aref vec i)))
603 (setq vec shorter))))
606 ;; Encode the word index IDX into a characters code that can be
607 ;; embedded in a string.
609 (defsubst unidata-encode-word (idx)
613 ;; Decode the character code CODE (that is embedded in a string) into
614 ;; the corresponding word name by looking up WORD-TABLE.
616 (defsubst unidata-decode-word (code word-table)
617 (setq code (- code 3))
618 (if (< code (length word-table))
619 (aref word-table code)))
621 ;; Table of short transliterated name symbols of Hangul Jamo divided
622 ;; into Choseong, Jungseong, and Jongseong.
624 (defconst unidata-name-jamo-name-table
625 [[G GG N D DD R M B BB S SS nil J JJ C K T P H]
626 [A AE YA YAE EO E YEO YE O WA WAE OE YO U WEO WE WI YU EU YI I]
627 [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]])
629 ;; Return a name of CHAR. VAL is the current value of (aref TABLE
632 (defun unidata-get-name (char val table)
635 (if (> (aref val 0) 0)
637 (let* ((first-char (lsh (lsh char -7) 7))
638 (word-table (aref (char-table-extra-slot table 4) 0))
641 (vec (make-vector 128 nil))
643 (case-fold-search nil)
644 c word-list tail-list last-list word diff-head)
646 (setq c (aref val i))
649 (if (or word-list tail-list)
651 (setq last-list (nconc word-list tail-list))))
652 (setq i (1+ i) idx (1+ idx)
653 word-list nil tail-list nil)
658 (prog1 (aref val i) (setq i (1+ i)))))
659 (setq tail-list (nthcdr (% diff-head 16) last-list))
660 (dotimes (i (/ diff-head 16))
661 (setq word-list (nconc word-list (list (car l)))
666 (unidata-decode-word c word-table))))
668 (if (or word-list tail-list)
669 (aset vec idx (nconc word-list tail-list)))
672 (setq c (+ first-char i))
673 (let ((name (aref vec i)))
675 (let ((tail (cdr (setq name (copy-sequence name))))
678 (setq elt (car tail))
679 (or (string= elt "-")
681 (setcdr tail (cons elt (cdr tail)))
683 (setq tail (cddr tail)))
684 (setq name (apply 'concat name))))
690 ((and (integerp val) (> val 0))
691 (let* ((symbol-table (aref (char-table-extra-slot table 4) 1))
692 (sym (aref symbol-table (1- val))))
693 (cond ((eq sym 'HANGUL\ SYLLABLE)
694 (let ((jamo-name-table (aref (char-table-extra-slot table 4) 2)))
695 ;; SIndex = S - SBase
696 (setq char (- char #xAC00))
697 (let ( ;; LIndex = SIndex / NCount
699 ;; VIndex = (SIndex % NCount) * TCount
700 (V (/ (% char 588) 28))
701 ;; TIndex = SIndex % TCount
703 (format "HANGUL SYLLABLE %s%s%s"
704 ;; U+110B is nil in this table.
705 (or (aref (aref jamo-name-table 0) L) "")
706 (aref (aref jamo-name-table 1) V)
708 (aref (aref jamo-name-table 2) (1- T)))))))
709 ((eq sym 'CJK\ IDEOGRAPH)
710 (format "%s-%04X" sym char))
711 ((eq sym 'CJK\ COMPATIBILITY\ IDEOGRAPH)
712 (format "%s-%04X" sym char))
713 ((eq sym 'VARIATION\ SELECTOR)
714 (format "%s-%d" sym (+ (- char #xe0100) 17))))))))
716 ;; Store VAL as the name of CHAR in TABLE.
718 (defun unidata-put-name (char val table)
719 (let ((current-val (aref table char)))
720 (if (and (stringp current-val) (= (aref current-val 0) 0))
721 (funcall (char-table-extra-slot table 1) char current-val table))
722 (aset table char val)))
724 (defun unidata-get-decomposition (char val table)
730 (if (> (aref val 0) 0)
732 (let* ((first-char (lsh (lsh char -7) 7))
733 (word-table (char-table-extra-slot table 4))
736 (vec (make-vector 128 nil))
738 (case-fold-search nil)
739 c word-list tail-list last-list word diff-head)
741 (setq c (aref val i))
744 (if (or word-list tail-list)
746 (setq last-list (nconc word-list tail-list))))
747 (setq i (1+ i) idx (1+ idx)
748 word-list nil tail-list nil)
753 (prog1 (aref val i) (setq i (1+ i)))))
754 (setq tail-list (nthcdr (% diff-head 16) last-list))
755 (dotimes (i (/ diff-head 16))
756 (setq word-list (nconc word-list (list (car l)))
760 (list (or (unidata-decode-word c word-table) c)))
762 (if (or word-list tail-list)
763 (aset vec idx (nconc word-list tail-list)))
765 (aset table (+ first-char i) (aref vec i)))
766 (aref vec (- char first-char)))))))
768 ;; Store VAL as the name of CHAR in TABLE.
770 (defun unidata-put-decomposition (char val table)
771 (let ((current-val (aref table char)))
772 (if (and (stringp current-val) (= (aref current-val 0) 0))
773 (funcall (char-table-extra-slot table 1) char current-val table))
774 (aset table char val)))
776 ;; UnicodeData.txt contains these lines:
777 ;; 0000;<control>;Cc;0;BN;;;;;N;NULL;;;;
779 ;; 0020;SPACE;Zs;0;WS;;;;;N;;;;;
781 ;; The following command yields a file of about 96K bytes.
782 ;; % gawk -F ';' '{print $1,$2;}' < UnicodeData.txt | gzip > temp.gz
783 ;; With the following function, we can get a file of almost the same
786 ;; Generate a char-table for character names.
788 (defun unidata-gen-table-word-list (prop val-func)
789 (let ((table (make-char-table 'char-code-property-table))
790 (prop-idx (unidata-prop-index prop))
791 (word-list (list nil))
793 block-list block-word-table block-end
794 tail elt range val idx slot)
795 (set-char-table-range table (cons 0 (max-char)) 0)
796 (setq tail unidata-list)
799 (setq elt (car tail) tail (cdr tail))
800 (setq range (car elt)
801 val (funcall val-func (nth prop-idx elt)))
802 ;; Treat the sequence of "CJK COMPATIBILITY IDEOGRAPH-XXXX" and
803 ;; "VARIATION SELECTOR-XXX" as a block.
804 (if (and (consp val) (eq prop 'name)
805 (or (and (eq (car val) 'CJK)
806 (eq (nth 1 val) 'COMPATIBILITY))
807 (and (>= range #xe0100)
808 (eq (car val) 'VARIATION)
809 (eq (nth 1 val) 'SELECTOR))))
810 (let ((first (car val))
813 (while (and (setq elt (car tail) range (car elt)
814 val (funcall val-func (nth prop-idx elt)))
817 (eq second (nth 1 val)))
818 (setq block-end range
820 (setq range (cons start block-end)
821 val (if (eq first 'CJK) 'CJK\ COMPATIBILITY\ IDEOGRAPH
822 'VARIATION\ SELECTOR))))
826 (let ((slot (assq val block-list)))
827 (setq range (cons (car range) (cdr range)))
828 (setq block-end (cdr range))
830 (nconc slot (list range))
831 (push (list val range) block-list))))
832 (let* ((start (lsh (lsh range -7) 7))
833 (limit (+ start 127))
835 (vec (make-vector 128 nil))
837 (if (<= start block-end)
838 ;; START overlap with the previous block.
839 (aset table range (nth prop-idx elt))
841 (aset vec (- range start) val))
842 (while (and (setq elt (car tail) range (car elt))
845 (setq val (funcall val-func (nth prop-idx elt)))
847 (aset vec (- range start) val))
848 (setq tail (cdr tail)))
849 (setq vec (unidata-word-list-compress vec))
851 (dotimes (i (length vec))
852 (dolist (elt (aref vec i))
854 (let ((slot (assq elt word-list)))
856 (setcdr slot (1+ (cdr slot)))
858 (cons (cons elt 1) (cdr word-list))))))))
859 (set-char-table-range table (cons start limit) vec))))))
860 (setq word-list (sort (cdr word-list)
861 #'(lambda (x y) (> (cdr x) (cdr y)))))
862 (setq tail word-list idx 0)
864 (setcdr (car tail) (unidata-encode-word idx))
865 (setq idx (1+ idx) tail (cdr tail)))
866 (setq word-table (make-vector (length word-list) nil))
868 (dolist (elt word-list)
869 (aset word-table idx (car elt))
872 (if (and (eq prop 'decomposition)
874 (error "Too many symobls in decomposition data"))
876 (dotimes (i (/ #x110000 128))
877 (let* ((idx (* i 128))
878 (vec (aref table idx)))
880 (dotimes (i (length vec))
881 (let ((tail (aref vec i))
887 code (if (integerp elt) elt
888 (cdr (assq elt word-list))))
889 (setcar tail (string code))
890 (setq tail (cdr tail)))
891 (aset vec i (mapconcat 'identity (aref vec i) "")))))
892 (set-char-table-range
893 table (cons idx (+ idx 127))
894 (mapconcat 'identity vec "")))))
896 (setq block-word-table (make-vector (length block-list) nil))
898 (dolist (elt block-list)
899 (dolist (e (cdr elt))
900 (set-char-table-range table e (1+ idx)))
901 (aset block-word-table idx (car elt))
904 (set-char-table-extra-slot table 0 prop)
905 (set-char-table-extra-slot table 4 (cons word-table block-word-table))
908 (defun unidata-split-name (str)
911 (let ((len (length str))
918 (setq c (aref str i))
920 (setq l (cons (intern (substring str idx i)) l)
922 (if (and (= c ?-) (< idx i)
923 (< (1+ i) len) (/= (aref str (1+ i)) 32))
924 (setq l (cons '- (cons (intern (substring str idx i)) l))
926 (nreverse (cons (intern (substring str idx)) l))))))
928 (defun unidata-gen-table-name (prop)
929 (let* ((table (unidata-gen-table-word-list prop 'unidata-split-name))
930 (word-tables (char-table-extra-slot table 4)))
931 (byte-compile 'unidata-get-name)
932 (byte-compile 'unidata-put-name)
933 (set-char-table-extra-slot table 1 (symbol-function 'unidata-get-name))
934 (set-char-table-extra-slot table 2 (symbol-function 'unidata-put-name))
937 (set-char-table-extra-slot table 4
938 (vector (car word-tables)
940 unidata-name-jamo-name-table))
941 (set-char-table-extra-slot table 4
942 (vector (car word-tables))))
945 (defun unidata-split-decomposition (str)
948 (let ((len (length str))
955 (setq c (aref str i))
957 (setq l (if (= (aref str idx) ?<)
958 (cons (intern (substring str idx i)) l)
959 (cons (string-to-int (substring str idx i) 16) l))
961 (if (= (aref str idx) ?<)
962 (setq l (cons (intern (substring str idx len)) l))
963 (setq l (cons (string-to-int (substring str idx len) 16) l)))
967 (defun unidata-gen-table-decomposition (prop)
968 (let* ((table (unidata-gen-table-word-list prop 'unidata-split-decomposition))
969 (word-tables (char-table-extra-slot table 4)))
970 (byte-compile 'unidata-get-decomposition)
971 (byte-compile 'unidata-put-decomposition)
972 (set-char-table-extra-slot table 1
973 (symbol-function 'unidata-get-decomposition))
974 (set-char-table-extra-slot table 2
975 (symbol-function 'unidata-put-decomposition))
976 (set-char-table-extra-slot table 4 (car word-tables))
981 (defun unidata-describe-general-category (val)
983 '((Lu . "Letter, Uppercase")
984 (Ll . "Letter, Lowercase")
985 (Lt . "Letter, Titlecase")
986 (Lm . "Letter, Modifier")
987 (Lo . "Letter, Other")
988 (Mn . "Mark, Nonspacing")
989 (Mc . "Mark, Spacing Combining")
990 (Me . "Mark, Enclosing")
991 (Nd . "Number, Decimal Digit")
992 (Nl . "Number, Letter")
993 (No . "Number, Other")
994 (Pc . "Punctuation, Connector")
995 (Pd . "Punctuation, Dash")
996 (Ps . "Punctuation, Open")
997 (Pe . "Punctuation, Close")
998 (Pi . "Punctuation, Initial quote")
999 (Pf . "Punctuation, Final quote")
1000 (Po . "Punctuation, Other")
1001 (Sm . "Symbol, Math")
1002 (Sc . "Symbol, Currency")
1003 (Sk . "Symbol, Modifier")
1004 (So . "Symbol, Other")
1005 (Zs . "Separator, Space")
1006 (Zl . "Separator, Line")
1007 (Zp . "Separator, Paragraph")
1008 (Cc . "Other, Control")
1009 (Cf . "Other, Format")
1010 (Cs . "Other, Surrogate")
1011 (Co . "Other, Private Use")
1012 (Cn . "Other, Not Assigned")))))
1014 (defun unidata-describe-canonical-combining-class (val)
1016 '((0 . "Spacing, split, enclosing, reordrant, and Tibetan subjoined")
1017 (1 . "Overlays and interior")
1019 (8 . "Hiragana/Katakana voicing marks")
1021 (10 . "Start of fixed position classes")
1022 (199 . "End of fixed position classes")
1023 (200 . "Below left attached")
1024 (202 . "Below attached")
1025 (204 . "Below right attached")
1026 (208 . "Left attached (reordrant around single base character)")
1027 (210 . "Right attached")
1028 (212 . "Above left attached")
1029 (214 . "Above attached")
1030 (216 . "Above right attached")
1031 (218 . "Below left")
1033 (222 . "Below right")
1034 (224 . "Left (reordrant around single base character)")
1036 (228 . "Above left")
1038 (232 . "Above right")
1039 (233 . "Double below")
1040 (234 . "Double above")
1041 (240 . "Below (iota subscript)")))))
1043 (defun unidata-describe-bidi-class (val)
1045 '((L . "Left-to-Right")
1046 (LRE . "Left-to-Right Embedding")
1047 (LRO . "Left-to-Right Override")
1048 (R . "Right-to-Left")
1049 (AL . "Right-to-Left Arabic")
1050 (RLE . "Right-to-Left Embedding")
1051 (RLO . "Right-to-Left Override")
1052 (PDF . "Pop Directional Format")
1053 (EN . "European Number")
1054 (ES . "European Number Separator")
1055 (ET . "European Number Terminator")
1056 (AN . "Arabic Number")
1057 (CS . "Common Number Separator")
1058 (NSM . "Non-Spacing Mark")
1059 (BN . "Boundary Neutral")
1060 (B . "Paragraph Separator")
1061 (S . "Segment Separator")
1063 (ON . "Other Neutrals")))))
1065 (defun unidata-describe-decomposition (val)
1066 (mapconcat #'(lambda (x) (if (symbolp x) (symbol-name x) (string ?' x ?')))
1069 ;; Verify if we can retrieve correct values from the generated
1072 (defun unidata-check ()
1073 (dolist (elt unidata-prop-alist)
1074 (let* ((prop (car elt))
1075 (index (unidata-prop-index prop))
1076 (generator (unidata-prop-generator prop))
1078 (message "Generating %S table..." prop)
1079 (funcall generator prop)))
1080 (decoder (char-table-extra-slot table 1))
1082 (dolist (e unidata-list)
1083 (let ((char (car e))
1084 (val1 (nth index e))
1086 (if (and (stringp val1) (= (length val1) 0))
1088 (unless (consp char)
1089 (setq val2 (funcall decoder char (aref table char) table))
1091 (cond ((eq generator 'unidata-gen-table-symbol)
1092 (setq val1 (intern val1)))
1093 ((eq generator 'unidata-gen-table-integer)
1094 (setq val1 (string-to-int val1)))
1095 ((eq generator 'unidata-gen-table-character)
1096 (setq val1 (string-to-int val1 16)))
1097 ((eq generator 'unidata-gen-table-decomposition)
1098 (setq val1 (unidata-split-decomposition val1)))))
1099 (when (>= char check)
1100 (message "%S %04X" prop check)
1101 (setq check (+ check #x400)))
1102 (or (equal val1 val2)
1103 (insert (format "> %04X %S\n< %04X %S\n"
1104 char val1 char val2)))
1107 ;; The entry function. It generates files described in the header
1108 ;; comment of this file.
1110 (defun unidata-gen-files ()
1112 (let ((coding-system-for-write 'utf-8)
1113 (charprop-file "charprop.el"))
1114 (with-temp-file charprop-file
1115 (insert ";; Automatically generated by unidata-gen.el.\n")
1116 (dolist (elt unidata-prop-alist)
1117 (let* ((prop (car elt))
1118 (generator (unidata-prop-generator prop))
1119 (file (unidata-prop-file prop))
1120 (docstring (unidata-prop-docstring prop))
1121 (describer (unidata-prop-describer prop))
1123 ;; Filename in this comment line is extracted by sed in
1125 (insert (format ";; FILE: %s\n" file))
1126 (insert (format "(define-char-code-property '%S %S\n %S)\n"
1127 prop file docstring))
1128 (with-temp-file file
1129 (message "Generating %s..." file)
1130 (setq table (funcall generator prop))
1132 (unless (subrp (symbol-function describer))
1133 (byte-compile describer)
1134 (setq describer (symbol-function describer)))
1135 (set-char-table-extra-slot table 3 describer))
1136 (insert ";; Automatically generated from UnicodeData.txt.\n"
1137 (format "(define-char-code-property '%S %S %S)\n"
1138 prop table docstring)
1139 ;; \040 below is to avoid error on reading this file.
1140 ";; Local\040Variables:\n"
1141 ";; coding: utf-8\n"
1142 ";; no-byte-compile: t\n"
1144 (format ";; %s ends here\n" file)))))
1145 (message "Writing %s..." charprop-file)
1146 ;; \040 below is to avoid error on reading this file.
1147 (insert ";; Local\040Variables:\n"
1148 ";; coding: utf-8\n"
1149 ";; no-byte-compile: t\n"
1151 (format ";; %s ends here\n" charprop-file)))))
1153 ;; arch-tag: 961c862e-b821-447e-9b8a-bfbab9c2d525
1154 ;;; unidata-gen.el ends here