1 ;; unidata-gen.el -- Create files containing character property data.
2 ;; Copyright (C) 2005, 2006, 2007, 2008
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 3 of the License, or
11 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
25 ;; This file must be byte-compilable/loadable by `temacs' and also
26 ;; the entry function `unidata-gen-files' must be runnable by
29 ;; FILES TO BE GENERATED
31 ;; The entry function `unidata-gen-files' generates these files in
32 ;; the current directory.
35 ;; It contains a series of forms of this format:
36 ;; (char-code-property-register PROP FILE)
37 ;; where PROP is a symbol representing a character property
38 ;; (name, generic-category, etc), and FILE is a name of one of
39 ;; the following files.
41 ;; uni-name.el, uni-category.el, uni-combining.el, uni-bidi.el,
42 ;; uni-decomposition.el, uni-decimal.el, uni-digit.el, uni-numeric.el,
43 ;; uni-mirrored.el, uni-old-name.el, uni-comment.el, uni-uppercase.el,
44 ;; uni-lowercase.el, uni-titlecase.el
45 ;; They each contain a single form of this format:
46 ;; (char-code-property-register PROP CHAR-TABLE)
47 ;; where PROP is the same as above, and CHAR-TABLE is a
48 ;; char-table containing property values in a compressed format.
50 ;; When they are installed in .../lisp/international/, the file
51 ;; "charprop.el" is preloaded in loadup.el. The other files are
52 ;; automatically loaded when the functions `get-char-code-property'
53 ;; and `put-char-code-property' are called.
55 ;; FORMAT OF A CHAR TABLE
57 ;; We want to make a file size containing a char-table small. We
58 ;; also want to load the file and get a property value fast. We
59 ;; also want to reduce the used memory after loading it. So,
60 ;; instead of naively storing a property value for each character in
61 ;; a char-table (and write it out into a file), we store compressed
62 ;; data in a char-table as below.
64 ;; If succeeding 128*N characters have the same property value, we
65 ;; store that value for them. Otherwise, compress values for
66 ;; succeeding 128 characters into a single string and store it as a
67 ;; value for those characters. The way of compression depends on a
68 ;; property. See the section "SIMPLE TABLE", "RUN-LENGTH TABLE",
69 ;; and "WORD-LIST TABLE".
71 ;; The char table has four extra slots:
72 ;; 1st: property symbol
73 ;; 2nd: function to call to get a property value
74 ;; 3nd: function to call to put a property value
75 ;; 4th: function to call to get a description of a property value
76 ;; 5th: data referred by the above functions
78 ;; List of elements of this form:
79 ;; (CHAR-or-RANGE PROP1 PROP2 ... PROPn)
80 ;; CHAR-or-RANGE: a character code or a cons of character codes
81 ;; PROPn: string representing the nth property value
83 (defvar unidata-list nil
)
85 (defun unidata-setup-list (unidata-text-file)
86 (let* ((table (list nil
))
88 (block-names '(("^<CJK Ideograph" . CJK\ IDEOGRAPH
)
89 ("^<Hangul Syllable" . HANGUL\ SYLLABLE
)
90 ("^<.*Surrogate" . nil
)
91 ("^<.*Private Use" . PRIVATE\ USE
)))
93 (or (file-readable-p unidata-text-file
)
94 (error "File not readable: %s" unidata-text-file
))
96 (insert-file-contents unidata-text-file
)
97 (goto-char (point-min))
100 (setq val
(read (current-buffer))
104 ;; Check this kind of block.
105 ;; 4E00;<CJK Ideograph, First>;Lo;0;L;;;;;N;;;;;
106 ;; 9FA5;<CJK Ideograph, Last>;Lo;0;L;;;;;N;;;;;
107 (if (and (= (aref name
0) ?
<)
108 (string-match ", First>$" name
))
112 (setq val
(read (current-buffer))
114 block-name
(cadr val
)
117 (if (string-match (caar l
) block-name
)
118 (setq name
(cdar l
) l nil
)
121 ;; As this is a surrogate pair range, ignore it.
123 (setcar val
(cons first char
))
124 (setcar (cdr val
) name
))))
127 (setcdr tail
(list val
))
128 (setq tail
(cdr tail
))))
130 (setq unidata-list
(cdr table
))))
132 ;; Alist of this form:
133 ;; (PROP INDEX GENERATOR FILENAME)
134 ;; PROP: character property
135 ;; INDEX: index to each element of unidata-list for PROP
136 ;; GENERATOR: function to generate a char-table
137 ;; FILENAME: filename to store the char-table
138 ;; DESCRIBER: function to call to get a description string of property value
140 (defconst unidata-prop-alist
142 1 unidata-gen-table-name
"uni-name.el"
143 "Unicode character name.
144 Property value is a string.")
146 2 unidata-gen-table-symbol
"uni-category.el"
147 "Unicode general category.
148 Property value is one of the following symbols:
149 Lu, Ll, Lt, Lm, Lo, Mn, Mc, Me, Nd, Nl, No, Pc, Pd, Ps, Pe, Pi, Pf, Po,
150 Sm, Sc, Sk, So, Zs, Zl, Zp, Cc, Cf, Cs, Co, Cn"
151 unidata-describe-general-category
)
152 (canonical-combining-class
153 3 unidata-gen-table-integer
"uni-combining.el"
154 "Unicode canonical combining class.
155 Property value is an integer."
156 unidata-describe-canonical-combining-class
)
158 4 unidata-gen-table-symbol
"uni-bidi.el"
160 Property value is one of the following symbols:
161 L, LRE, LRO, R, AL, RLE, RLO, PDF, EN, ES, ET,
162 AN, CS, NSM, BN, B, S, WS, ON"
163 unidata-describe-bidi-class
)
165 5 unidata-gen-table-decomposition
"uni-decomposition.el"
166 "Unicode decomposition mapping.
167 Property value is a list of characters. The first element may be
168 one of these symbols representing compatibility formatting tag:
169 <font>, <noBreak>, <initial>, <medial>, <final>, <isolated>, <circle>,
170 <super>, <sub>, <vertical>, <wide>, <narrow>, <small>, <square>, <fraction>,
172 unidata-describe-decomposition
)
174 6 unidata-gen-table-integer
"uni-decimal.el"
175 "Unicode numeric value (decimal digit).
176 Property value is an integer.")
178 7 unidata-gen-table-integer
"uni-digit.el"
179 "Unicode numeric value (digit).
180 Property value is an integer.")
182 8 unidata-gen-table-symbol
"uni-numeric.el"
183 "Unicode numeric value (numeric).
184 Property value is a symbol.")
186 9 unidata-gen-table-symbol
"uni-mirrored.el"
187 "Unicode bidi mirrored flag.
188 Property value is a symbol `Y' or `N'.")
190 10 unidata-gen-table-name
"uni-old-name.el"
191 "Unicode old names as published in Unicode 1.0.
192 Property value is a string.")
194 11 unidata-gen-table-name
"uni-comment.el"
195 "Unicode ISO 10646 comment.
196 Property value is a string.")
198 12 unidata-gen-table-character
"uni-uppercase.el"
199 "Unicode simple uppercase mapping.
200 Property value is a character."
203 13 unidata-gen-table-character
"uni-lowercase.el"
204 "Unicode simple lowercase mapping.
205 Property value is a character."
208 14 unidata-gen-table-character
"uni-titlecase.el"
209 "Unicode simple titlecase mapping.
210 Property value is a character."
213 ;; Functions to access the above data.
214 (defsubst unidata-prop-index
(prop) (nth 1 (assq prop unidata-prop-alist
)))
215 (defsubst unidata-prop-generator
(prop) (nth 2 (assq prop unidata-prop-alist
)))
216 (defsubst unidata-prop-file
(prop) (nth 3 (assq prop unidata-prop-alist
)))
217 (defsubst unidata-prop-docstring
(prop) (nth 4 (assq prop unidata-prop-alist
)))
218 (defsubst unidata-prop-describer
(prop) (nth 5 (assq prop unidata-prop-alist
)))
223 ;; If the type of character property value is character, and the
224 ;; values of succeeding character codes are usually different, we use
225 ;; a char-table described here to store such values.
227 ;; If succeeding 128 characters has no property, a char-table has the
228 ;; symbol t is for them. Otherwise a char-table has a string of the
229 ;; following format for them.
231 ;; The first character of the string is FIRST-INDEX.
232 ;; The Nth (N > 0) character of the string is a property value of the
233 ;; character (BLOCK-HEAD + FIRST-INDEX + N - 1), where BLOCK-HEAD is
234 ;; the first of the characters in the block.
236 ;; The 4th extra slot of a char-table is nil.
238 (defun unidata-get-character (char val table
)
244 (let* ((len (length val
))
245 (block-head (lsh (lsh char -
7) 7))
246 (vec (make-vector 128 nil
))
247 (first-index (aref val
0)))
248 (dotimes (i (1- len
))
249 (let ((elt (aref val
(1+ i
))))
251 (aset vec
(+ first-index i
) elt
))))
253 (aset table
(+ block-head i
) (aref vec i
)))
254 (aref vec
(- char block-head
))))))
256 (defun unidata-put-character (char val table
)
259 (error "Not a character nor nil: %S" val
))
260 (let ((current-val (aref table char
)))
261 (unless (eq current-val val
)
262 (if (stringp current-val
)
263 (funcall (char-table-extra-slot table
1) char current-val table
))
264 (aset table char val
))))
266 (defun unidata-gen-table-character (prop)
267 (let ((table (make-char-table 'char-code-property-table
))
268 (prop-idx (unidata-prop-index prop
))
269 (vec (make-vector 128 0))
271 elt range val idx slot
)
272 (set-char-table-range table
(cons 0 (max-char)) t
)
274 (setq elt
(car tail
) tail
(cdr tail
))
275 (setq range
(car elt
)
276 val
(nth prop-idx elt
))
277 (if (= (length val
) 0)
279 (setq val
(string-to-number val
16)))
282 (set-char-table-range table range val
))
283 (let* ((start (lsh (lsh range -
7) 7))
284 (limit (+ start
127))
285 first-index last-index
)
288 (aset vec
(setq last-index
(setq first-index
(- range start
)))
290 (while (and (setq elt
(car tail
) range
(car elt
))
293 (setq val
(nth prop-idx elt
))
294 (when (> (length val
) 0)
295 (aset vec
(setq last-index
(- range start
))
296 (string-to-number val
16))
298 (setq first-index last-index
)))
299 (setq tail
(cdr tail
)))
301 (let ((str (string first-index
))
303 (while (<= first-index last-index
)
304 (setq str
(format "%s%c" str
(or (aref vec first-index
) 0))
305 first-index
(1+ first-index
)))
306 (set-char-table-range table
(cons start limit
) str
))))))
308 (set-char-table-extra-slot table
0 prop
)
309 (byte-compile 'unidata-get-character
)
310 (byte-compile 'unidata-put-character
)
311 (set-char-table-extra-slot table
1 (symbol-function 'unidata-get-character
))
312 (set-char-table-extra-slot table
2 (symbol-function 'unidata-put-character
))
320 ;; If the type of character property value is symbol, integer,
321 ;; boolean, or character, we use a char-table described here to store
324 ;; The 4th extra slot is a vector of property values (VAL-TABLE), and
325 ;; values for succeeding 128 characters are encoded into this
326 ;; character sequence:
327 ;; ( VAL-CODE RUN-LENGTH ? ) +
329 ;; VAL-CODE (0..127):
330 ;; (VAL-CODE - 1) is an index into VAL-TABLE.
331 ;; The value 0 means no-value.
332 ;; RUN-LENGTH (130..255):
333 ;; (RUN-LENGTH - 128) specifies how many characters have the same
334 ;; value. If omitted, it means 1.
337 ;; Return a symbol-type character property value of CHAR. VAL is the
338 ;; current value of (aref TABLE CHAR).
340 (defun unidata-get-symbol (char val table
)
341 (let ((val-table (char-table-extra-slot table
4)))
345 (let ((first-char (lsh (lsh char -
7) 7))
350 (set-char-table-range table
(cons first-char
(+ first-char
127))
353 (setq val
(aref str idx
) idx
(1+ idx
)
354 count
(if (< idx len
) (aref str idx
) 1))
355 (setq val
(and (> val
0) (aref val-table
(1- val
)))
356 count
(if (< count
128)
358 (prog1 (- count
128) (setq idx
(1+ idx
)))))
361 (aset table first-char val
))
362 (if (= first-char char
)
364 (setq first-char
(1+ first-char
))))
367 (aref val-table
(1- val
))))))
369 ;; Return a integer-type character property value of CHAR. VAL is the
370 ;; current value of (aref TABLE CHAR).
372 (defun unidata-get-integer (char val table
)
373 (let ((val-table (char-table-extra-slot table
4)))
374 (cond ((integerp val
)
377 (let ((first-char (lsh (lsh char -
7) 7))
383 (setq val
(aref str idx
) idx
(1+ idx
)
384 count
(if (< idx len
) (aref str idx
) 1))
385 (setq val
(and (> val
0) (aref val-table
(1- val
)))
386 count
(if (< count
128)
388 (prog1 (- count
128) (setq idx
(1+ idx
)))))
390 (aset table first-char val
)
391 (if (= first-char char
)
393 (setq first-char
(1+ first-char
))))
396 ;; Store VAL (symbol) as a character property value of CHAR in TABLE.
398 (defun unidata-put-symbol (char val table
)
400 (error "Not a symbol: %S" val
))
401 (let ((current-val (aref table char
)))
402 (unless (eq current-val val
)
403 (if (stringp current-val
)
404 (funcall (char-table-extra-slot table
1) char current-val table
))
405 (aset table char val
))))
407 ;; Store VAL (integer) as a character property value of CHAR in TABLE.
409 (defun unidata-put-integer (char val table
)
412 (error "Not an integer nor nil: %S" val
))
413 (let ((current-val (aref table char
)))
414 (unless (eq current-val val
)
415 (if (stringp current-val
)
416 (funcall (char-table-extra-slot table
1) char current-val table
))
417 (aset table char val
))))
419 ;; Encode the character property value VAL into an integer value by
420 ;; VAL-LIST. By side effect, VAL-LIST is modified.
421 ;; VAL-LIST has this form:
422 ;; (t (VAL1 . VAL-CODE1) (VAL2 . VAL-CODE2) ...)
423 ;; If VAL is one of VALn, just return VAL-CODEn. Otherwise,
424 ;; VAL-LIST is modified to this:
425 ;; (t (VAL . (1+ VAL-CODE1)) (VAL1 . VAL-CODE1) (VAL2 . VAL-CODE2) ...)
427 (defun unidata-encode-val (val-list val
)
428 (let ((slot (assq val val-list
))
432 (setq val-code
(if (cdr val-list
) (1+ (cdr (nth 1 val-list
))) 1))
433 (setcdr val-list
(cons (cons val val-code
) (cdr val-list
)))
436 ;; Generate a char-table for the character property PROP.
438 (defun unidata-gen-table (prop val-func default-value
)
439 (let ((table (make-char-table 'char-code-property-table
))
440 (prop-idx (unidata-prop-index prop
))
442 (vec (make-vector 128 0))
443 tail elt range val val-code idx slot
)
444 (set-char-table-range table
(cons 0 (max-char)) default-value
)
445 (setq tail unidata-list
)
447 (setq elt
(car tail
) tail
(cdr tail
))
448 (setq range
(car elt
)
449 val
(funcall val-func
(nth prop-idx elt
)))
450 (setq val-code
(if val
(unidata-encode-val val-list val
)))
453 (set-char-table-range table range val
))
454 (let* ((start (lsh (lsh range -
7) 7))
455 (limit (+ start
127))
459 (aset vec
(- range start
) val-code
))
460 (while (and (setq elt
(car tail
) range
(car elt
))
463 (setq new-val
(funcall val-func
(nth prop-idx elt
)))
464 (if (not (eq val new-val
))
466 val-code
(if val
(unidata-encode-val val-list val
))))
468 (aset vec
(- range start
) val-code
))
469 (setq tail
(cdr tail
)))
470 (setq str
"" val-code -
1 count
0)
473 (setq count
(1+ count
))
475 (setq str
(concat str
(string val-code
478 (setq str
(concat str
(string val-code val-code
)))
480 (setq str
(concat str
(string val-code
))))))
481 (setq val-code x count
1)))
485 (set-char-table-range table
(cons start limit
) val
))
487 (set-char-table-range table
(cons start limit
) str
)
489 (setq str
(concat str
(string val-code
(+ count
128))))
491 (setq str
(concat str
(string val-code val-code
)))
492 (setq str
(concat str
(string val-code
)))))
493 (set-char-table-range table
(cons start limit
) str
))))))
495 (setq val-list
(nreverse (cdr val-list
)))
496 (set-char-table-extra-slot table
0 prop
)
497 (set-char-table-extra-slot table
4 (vconcat (mapcar 'car val-list
)))
500 (defun unidata-gen-table-symbol (prop)
501 (let ((table (unidata-gen-table prop
502 #'(lambda (x) (and (> (length x
) 0)
505 (byte-compile 'unidata-get-symbol
)
506 (byte-compile 'unidata-put-symbol
)
507 (set-char-table-extra-slot table
1 (symbol-function 'unidata-get-symbol
))
508 (set-char-table-extra-slot table
2 (symbol-function 'unidata-put-symbol
))
511 (defun unidata-gen-table-integer (prop)
512 (let ((table (unidata-gen-table prop
513 #'(lambda (x) (and (> (length x
) 0)
514 (string-to-number x
)))
516 (byte-compile 'unidata-get-integer
)
517 (byte-compile 'unidata-put-integer
)
518 (set-char-table-extra-slot table
1 (symbol-function 'unidata-get-integer
))
519 (set-char-table-extra-slot table
2 (symbol-function 'unidata-put-integer
))
525 ;; If the table is for `name' property, each character in the string
527 ;; DIFF-HEAD-CODE (0, 1, or 2):
528 ;; specifies how to decode the following characters.
529 ;; WORD-CODE (3..#x7FF excluding '-', '0'..'9', 'A'..'Z'):
530 ;; specifies an index number into WORD-TABLE (see below)
531 ;; Otherwise (' ', '-', '0'..'9', 'A'..'Z'):
532 ;; specifies a literal word.
534 ;; The 4th slots is a vector:
535 ;; [ WORD-TABLE BLOCK-NAME HANGUL-JAMO-TABLE ]
536 ;; WORD-TABLE is a vector of word symbols.
537 ;; BLOCK-NAME is a vector of name symbols for a block of characters.
538 ;; HANGUL-JAMO-TABLE is `unidata-name-jamo-name-table'.
540 ;; Return the difference of symbol list L1 and L2 in this form:
541 ;; (DIFF-HEAD SYM1 SYM2 ...)
542 ;; DIFF-HEAD is ((SAME-HEAD-LENGTH * 16) + SAME-TAIL-LENGTH).
543 ;; Ex: If L1 is (a b c d e f) and L2 is (a g h e f), this function
544 ;; returns ((+ (* 1 16) 2) g h).
545 ;; It means that we can get L2 from L1 by prepending the first element
546 ;; of L1 and appending the last 2 elements of L1 to the list (g h).
547 ;; If L1 and L2 don't have common elements at the head and tail,
548 ;; set DIFF-HEAD to -1 and SYM1 ... to the elements of L2.
550 (defun unidata-word-list-diff (l1 l2
)
557 (while (and l1
(eq (car l1
) (car l2
)))
559 l1
(cdr l1
) len1
(1- len1
) l2
(cdr l2
) len2
(1- len2
)))
560 (while (and (< end len1
) (< end len2
)
561 (eq (nth (- len1 end
1) l1
) (nth (- len2 end
1) l2
)))
562 (setq end
(1+ end
))))
563 (if (= (+ beg end
) 0)
564 (setq result
(list -
1))
565 (setq result
(list (+ (* beg
16) (+ beg
(- len1 end
))))))
567 (setcdr result
(cons (nth (- len2 end
1) l2
) (cdr result
)))
571 ;; Return a compressed form of the vector VEC. Each element of VEC is
572 ;; a list of symbols of which names can be concatenated to form a
573 ;; character name. This function changes those elements into
574 ;; compressed forms by utilizing the fact that diff of consecutive
575 ;; elements is usually small.
577 (defun unidata-word-list-compress (vec)
578 (let (last-elt last-idx diff-head tail elt val
)
580 (setq elt
(aref vec i
))
585 (setq val
(unidata-word-list-diff last-elt elt
))
588 val
(cons 0 (cdr val
)))
589 (if (eq diff-head
(car val
))
590 (setq val
(cons 2 (cdr val
)))
591 (setq diff-head
(car val
))
593 (setq val
(cons 1 val
))))))
595 (setq last-idx i last-elt elt
)))
599 (let ((shorter (make-vector (1+ last-idx
) nil
)))
600 (dotimes (i (1+ last-idx
))
601 (aset shorter i
(aref vec i
)))
602 (setq vec shorter
))))
605 ;; Encode the word index IDX into a characters code that can be
606 ;; embedded in a string.
608 (defsubst unidata-encode-word
(idx)
612 ;; Decode the character code CODE (that is embedded in a string) into
613 ;; the corresponding word name by looking up WORD-TABLE.
615 (defsubst unidata-decode-word
(code word-table
)
616 (setq code
(- code
3))
617 (if (< code
(length word-table
))
618 (aref word-table code
)))
620 ;; Table of short transliterated name symbols of Hangul Jamo divided
621 ;; into Choseong, Jungseong, and Jongseong.
623 (defconst unidata-name-jamo-name-table
624 [[G GG N D DD R M B BB S SS nil J JJ C K T P H
]
625 [A AE YA YAE EO E YEO YE O WA WAE OE YO U WEO WE WI YU EU YI I
]
626 [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
]])
628 ;; Return a name of CHAR. VAL is the current value of (aref TABLE
631 (defun unidata-get-name (char val table
)
634 (if (> (aref val
0) 0)
636 (let* ((first-char (lsh (lsh char -
7) 7))
637 (word-table (aref (char-table-extra-slot table
4) 0))
640 (vec (make-vector 128 nil
))
642 (case-fold-search nil
)
643 c word-list tail-list last-list word diff-head
)
645 (setq c
(aref val i
))
648 (if (or word-list tail-list
)
650 (setq last-list
(nconc word-list tail-list
))))
651 (setq i
(1+ i
) idx
(1+ idx
)
652 word-list nil tail-list nil
)
657 (prog1 (aref val i
) (setq i
(1+ i
)))))
658 (setq tail-list
(nthcdr (% diff-head
16) last-list
))
659 (dotimes (i (/ diff-head
16))
660 (setq word-list
(nconc word-list
(list (car l
)))
665 (unidata-decode-word c word-table
))))
667 (if (or word-list tail-list
)
668 (aset vec idx
(nconc word-list tail-list
)))
671 (setq c
(+ first-char i
))
672 (let ((name (aref vec i
)))
674 (let ((tail (cdr (setq name
(copy-sequence name
))))
677 (setq elt
(car tail
))
678 (or (string= elt
"-")
680 (setcdr tail
(cons elt
(cdr tail
)))
682 (setq tail
(cddr tail
)))
683 (setq name
(apply 'concat name
))))
689 ((and (integerp val
) (> val
0))
690 (let* ((symbol-table (aref (char-table-extra-slot table
4) 1))
691 (sym (aref symbol-table
(1- val
))))
692 (cond ((eq sym
'HANGUL\ SYLLABLE
)
693 (let ((jamo-name-table (aref (char-table-extra-slot table
4) 2)))
694 ;; SIndex = S - SBase
695 (setq char
(- char
#xAC00
))
696 (let ( ;; LIndex = SIndex / NCount
698 ;; VIndex = (SIndex % NCount) * TCount
699 (V (/ (% char
588) 28))
700 ;; TIndex = SIndex % TCount
702 (format "HANGUL SYLLABLE %s%s%s"
703 ;; U+110B is nil in this table.
704 (or (aref (aref jamo-name-table
0) L
) "")
705 (aref (aref jamo-name-table
1) V
)
707 (aref (aref jamo-name-table
2) (1- T
)))))))
708 ((eq sym
'CJK\ IDEOGRAPH
)
709 (format "%s-%04X" sym char
))
710 ((eq sym
'CJK\ COMPATIBILITY\ IDEOGRAPH
)
711 (format "%s-%04X" sym char
))
712 ((eq sym
'VARIATION\ SELECTOR
)
713 (format "%s-%d" sym
(+ (- char
#xe0100
) 17))))))))
715 ;; Store VAL as the name of CHAR in TABLE.
717 (defun unidata-put-name (char val table
)
718 (let ((current-val (aref table char
)))
719 (if (and (stringp current-val
) (= (aref current-val
0) 0))
720 (funcall (char-table-extra-slot table
1) char current-val table
))
721 (aset table char val
)))
723 (defun unidata-get-decomposition (char val table
)
729 (if (> (aref val
0) 0)
731 (let* ((first-char (lsh (lsh char -
7) 7))
732 (word-table (char-table-extra-slot table
4))
735 (vec (make-vector 128 nil
))
737 (case-fold-search nil
)
738 c word-list tail-list last-list word diff-head
)
740 (setq c
(aref val i
))
743 (if (or word-list tail-list
)
745 (setq last-list
(nconc word-list tail-list
))))
746 (setq i
(1+ i
) idx
(1+ idx
)
747 word-list nil tail-list nil
)
752 (prog1 (aref val i
) (setq i
(1+ i
)))))
753 (setq tail-list
(nthcdr (% diff-head
16) last-list
))
754 (dotimes (i (/ diff-head
16))
755 (setq word-list
(nconc word-list
(list (car l
)))
759 (list (or (unidata-decode-word c word-table
) c
)))
761 (if (or word-list tail-list
)
762 (aset vec idx
(nconc word-list tail-list
)))
764 (aset table
(+ first-char i
) (aref vec i
)))
765 (aref vec
(- char first-char
)))))
768 ((and (eq val
0) (>= char
#xAC00
) (<= char
#xD7A3
))
769 ;; SIndex = S (char) - SBase (#xAC00)
770 (setq char
(- char
#xAC00
))
771 (let (;; L = LBase + SIndex / NCount
772 (L (+ #x1100
(/ char
588)))
773 ;; V = VBase + (SIndex % NCount) * TCount
774 (V (+ #x1161
(/ (% char
588) 28)))
775 ;; T = TBase + SIndex % TCount
776 (T (+ #x11A7
(% char
28))))
783 ;; Store VAL as the decomposition information of CHAR in TABLE.
785 (defun unidata-put-decomposition (char val table
)
786 (let ((current-val (aref table char
)))
787 (if (and (stringp current-val
) (= (aref current-val
0) 0))
788 (funcall (char-table-extra-slot table
1) char current-val table
))
789 (aset table char val
)))
791 ;; UnicodeData.txt contains these lines:
792 ;; 0000;<control>;Cc;0;BN;;;;;N;NULL;;;;
794 ;; 0020;SPACE;Zs;0;WS;;;;;N;;;;;
796 ;; The following command yields a file of about 96K bytes.
797 ;; % gawk -F ';' '{print $1,$2;}' < UnicodeData.txt | gzip > temp.gz
798 ;; With the following function, we can get a file of almost the same
801 ;; Generate a char-table for character names.
803 (defun unidata-gen-table-word-list (prop val-func
)
804 (let ((table (make-char-table 'char-code-property-table
))
805 (prop-idx (unidata-prop-index prop
))
806 (word-list (list nil
))
808 block-list block-word-table block-end
809 tail elt range val idx slot
)
810 (set-char-table-range table
(cons 0 (max-char)) 0)
811 (setq tail unidata-list
)
814 (setq elt
(car tail
) tail
(cdr tail
))
815 (setq range
(car elt
)
816 val
(funcall val-func
(nth prop-idx elt
)))
817 ;; Treat the sequence of "CJK COMPATIBILITY IDEOGRAPH-XXXX" and
818 ;; "VARIATION SELECTOR-XXX" as a block.
819 (if (and (consp val
) (eq prop
'name
)
820 (or (and (eq (car val
) 'CJK
)
821 (eq (nth 1 val
) 'COMPATIBILITY
))
822 (and (>= range
#xe0100
)
823 (eq (car val
) 'VARIATION
)
824 (eq (nth 1 val
) 'SELECTOR
))))
825 (let ((first (car val
))
828 (while (and (setq elt
(car tail
) range
(car elt
)
829 val
(funcall val-func
(nth prop-idx elt
)))
832 (eq second
(nth 1 val
)))
833 (setq block-end range
835 (setq range
(cons start block-end
)
836 val
(if (eq first
'CJK
) 'CJK\ COMPATIBILITY\ IDEOGRAPH
837 'VARIATION\ SELECTOR
))))
841 (let ((slot (assq val block-list
)))
842 (setq range
(cons (car range
) (cdr range
)))
843 (setq block-end
(cdr range
))
845 (nconc slot
(list range
))
846 (push (list val range
) block-list
))))
847 (let* ((start (lsh (lsh range -
7) 7))
848 (limit (+ start
127))
850 (vec (make-vector 128 nil
))
852 (if (<= start block-end
)
853 ;; START overlap with the previous block.
854 (aset table range
(nth prop-idx elt
))
856 (aset vec
(- range start
) val
))
857 (while (and (setq elt
(car tail
) range
(car elt
))
860 (setq val
(funcall val-func
(nth prop-idx elt
)))
862 (aset vec
(- range start
) val
))
863 (setq tail
(cdr tail
)))
864 (setq vec
(unidata-word-list-compress vec
))
866 (dotimes (i (length vec
))
867 (dolist (elt (aref vec i
))
869 (let ((slot (assq elt word-list
)))
871 (setcdr slot
(1+ (cdr slot
)))
873 (cons (cons elt
1) (cdr word-list
))))))))
874 (set-char-table-range table
(cons start limit
) vec
))))))
875 (setq word-list
(sort (cdr word-list
)
876 #'(lambda (x y
) (> (cdr x
) (cdr y
)))))
877 (setq tail word-list idx
0)
879 (setcdr (car tail
) (unidata-encode-word idx
))
880 (setq idx
(1+ idx
) tail
(cdr tail
)))
881 (setq word-table
(make-vector (length word-list
) nil
))
883 (dolist (elt word-list
)
884 (aset word-table idx
(car elt
))
887 (if (and (eq prop
'decomposition
)
889 (error "Too many symbols in decomposition data"))
891 (dotimes (i (/ #x110000
128))
892 (let* ((idx (* i
128))
893 (vec (aref table idx
)))
895 (dotimes (i (length vec
))
896 (let ((tail (aref vec i
))
902 code
(if (integerp elt
) elt
903 (cdr (assq elt word-list
))))
904 (setcar tail
(string code
))
905 (setq tail
(cdr tail
)))
906 (aset vec i
(mapconcat 'identity
(aref vec i
) "")))))
907 (set-char-table-range
908 table
(cons idx
(+ idx
127))
909 (mapconcat 'identity vec
"")))))
911 (setq block-word-table
(make-vector (length block-list
) nil
))
913 (dolist (elt block-list
)
914 (dolist (e (cdr elt
))
915 (set-char-table-range table e
(1+ idx
)))
916 (aset block-word-table idx
(car elt
))
919 (set-char-table-extra-slot table
0 prop
)
920 (set-char-table-extra-slot table
4 (cons word-table block-word-table
))
923 (defun unidata-split-name (str)
926 (let ((len (length str
))
933 (setq c
(aref str i
))
935 (setq l
(cons (intern (substring str idx i
)) l
)
937 (if (and (= c ?-
) (< idx i
)
938 (< (1+ i
) len
) (/= (aref str
(1+ i
)) 32))
939 (setq l
(cons '-
(cons (intern (substring str idx i
)) l
))
941 (nreverse (cons (intern (substring str idx
)) l
))))))
943 (defun unidata-gen-table-name (prop)
944 (let* ((table (unidata-gen-table-word-list prop
'unidata-split-name
))
945 (word-tables (char-table-extra-slot table
4)))
946 (byte-compile 'unidata-get-name
)
947 (byte-compile 'unidata-put-name
)
948 (set-char-table-extra-slot table
1 (symbol-function 'unidata-get-name
))
949 (set-char-table-extra-slot table
2 (symbol-function 'unidata-put-name
))
952 (set-char-table-extra-slot table
4
953 (vector (car word-tables
)
955 unidata-name-jamo-name-table
))
956 (set-char-table-extra-slot table
4
957 (vector (car word-tables
))))
960 (defun unidata-split-decomposition (str)
963 (let ((len (length str
))
970 (setq c
(aref str i
))
972 (setq l
(if (= (aref str idx
) ?
<)
973 (cons (intern (substring str idx i
)) l
)
974 (cons (string-to-number (substring str idx i
) 16) l
))
976 (if (= (aref str idx
) ?
<)
977 (setq l
(cons (intern (substring str idx len
)) l
))
978 (setq l
(cons (string-to-number (substring str idx len
) 16) l
)))
982 (defun unidata-gen-table-decomposition (prop)
983 (let* ((table (unidata-gen-table-word-list prop
'unidata-split-decomposition
))
984 (word-tables (char-table-extra-slot table
4)))
985 (byte-compile 'unidata-get-decomposition
)
986 (byte-compile 'unidata-put-decomposition
)
987 (set-char-table-extra-slot table
1
988 (symbol-function 'unidata-get-decomposition
))
989 (set-char-table-extra-slot table
2
990 (symbol-function 'unidata-put-decomposition
))
991 (set-char-table-extra-slot table
4 (car word-tables
))
996 (defun unidata-describe-general-category (val)
998 '((Lu .
"Letter, Uppercase")
999 (Ll .
"Letter, Lowercase")
1000 (Lt .
"Letter, Titlecase")
1001 (Lm .
"Letter, Modifier")
1002 (Lo .
"Letter, Other")
1003 (Mn .
"Mark, Nonspacing")
1004 (Mc .
"Mark, Spacing Combining")
1005 (Me .
"Mark, Enclosing")
1006 (Nd .
"Number, Decimal Digit")
1007 (Nl .
"Number, Letter")
1008 (No .
"Number, Other")
1009 (Pc .
"Punctuation, Connector")
1010 (Pd .
"Punctuation, Dash")
1011 (Ps .
"Punctuation, Open")
1012 (Pe .
"Punctuation, Close")
1013 (Pi .
"Punctuation, Initial quote")
1014 (Pf .
"Punctuation, Final quote")
1015 (Po .
"Punctuation, Other")
1016 (Sm .
"Symbol, Math")
1017 (Sc .
"Symbol, Currency")
1018 (Sk .
"Symbol, Modifier")
1019 (So .
"Symbol, Other")
1020 (Zs .
"Separator, Space")
1021 (Zl .
"Separator, Line")
1022 (Zp .
"Separator, Paragraph")
1023 (Cc .
"Other, Control")
1024 (Cf .
"Other, Format")
1025 (Cs .
"Other, Surrogate")
1026 (Co .
"Other, Private Use")
1027 (Cn .
"Other, Not Assigned")))))
1029 (defun unidata-describe-canonical-combining-class (val)
1031 '((0 .
"Spacing, split, enclosing, reordrant, and Tibetan subjoined")
1032 (1 .
"Overlays and interior")
1034 (8 .
"Hiragana/Katakana voicing marks")
1036 (10 .
"Start of fixed position classes")
1037 (199 .
"End of fixed position classes")
1038 (200 .
"Below left attached")
1039 (202 .
"Below attached")
1040 (204 .
"Below right attached")
1041 (208 .
"Left attached (reordrant around single base character)")
1042 (210 .
"Right attached")
1043 (212 .
"Above left attached")
1044 (214 .
"Above attached")
1045 (216 .
"Above right attached")
1046 (218 .
"Below left")
1048 (222 .
"Below right")
1049 (224 .
"Left (reordrant around single base character)")
1051 (228 .
"Above left")
1053 (232 .
"Above right")
1054 (233 .
"Double below")
1055 (234 .
"Double above")
1056 (240 .
"Below (iota subscript)")))))
1058 (defun unidata-describe-bidi-class (val)
1060 '((L .
"Left-to-Right")
1061 (LRE .
"Left-to-Right Embedding")
1062 (LRO .
"Left-to-Right Override")
1063 (R .
"Right-to-Left")
1064 (AL .
"Right-to-Left Arabic")
1065 (RLE .
"Right-to-Left Embedding")
1066 (RLO .
"Right-to-Left Override")
1067 (PDF .
"Pop Directional Format")
1068 (EN .
"European Number")
1069 (ES .
"European Number Separator")
1070 (ET .
"European Number Terminator")
1071 (AN .
"Arabic Number")
1072 (CS .
"Common Number Separator")
1073 (NSM .
"Non-Spacing Mark")
1074 (BN .
"Boundary Neutral")
1075 (B .
"Paragraph Separator")
1076 (S .
"Segment Separator")
1078 (ON .
"Other Neutrals")))))
1080 (defun unidata-describe-decomposition (val)
1081 (mapconcat #'(lambda (x) (if (symbolp x
) (symbol-name x
) (string ?
' x ?
')))
1084 ;; Verify if we can retrieve correct values from the generated
1087 (defun unidata-check ()
1088 (dolist (elt unidata-prop-alist
)
1089 (let* ((prop (car elt
))
1090 (index (unidata-prop-index prop
))
1091 (generator (unidata-prop-generator prop
))
1093 (message "Generating %S table..." prop
)
1094 (funcall generator prop
)))
1095 (decoder (char-table-extra-slot table
1))
1097 (dolist (e unidata-list
)
1098 (let ((char (car e
))
1099 (val1 (nth index e
))
1101 (if (and (stringp val1
) (= (length val1
) 0))
1103 (unless (consp char
)
1104 (setq val2
(funcall decoder char
(aref table char
) table
))
1106 (cond ((eq generator
'unidata-gen-table-symbol
)
1107 (setq val1
(intern val1
)))
1108 ((eq generator
'unidata-gen-table-integer
)
1109 (setq val1
(string-to-number val1
)))
1110 ((eq generator
'unidata-gen-table-character
)
1111 (setq val1
(string-to-number val1
16)))
1112 ((eq generator
'unidata-gen-table-decomposition
)
1113 (setq val1
(unidata-split-decomposition val1
)))))
1114 (when (>= char check
)
1115 (message "%S %04X" prop check
)
1116 (setq check
(+ check
#x400
)))
1117 (or (equal val1 val2
)
1118 (insert (format "> %04X %S\n< %04X %S\n"
1119 char val1 char val2
)))
1122 ;; The entry function. It generates files described in the header
1123 ;; comment of this file.
1125 (defun unidata-gen-files (&optional unidata-text-file
)
1126 (or unidata-text-file
1127 (setq unidata-text-file
(car command-line-args-left
)
1128 command-line-args-left
(cdr command-line-args-left
)))
1129 (unidata-setup-list unidata-text-file
)
1130 (let ((coding-system-for-write 'utf-8-unix
)
1131 (charprop-file "charprop.el"))
1132 (with-temp-file charprop-file
1133 (insert ";; Automatically generated by unidata-gen.el.\n")
1134 (dolist (elt unidata-prop-alist
)
1135 (let* ((prop (car elt
))
1136 (generator (unidata-prop-generator prop
))
1137 (file (unidata-prop-file prop
))
1138 (docstring (unidata-prop-docstring prop
))
1139 (describer (unidata-prop-describer prop
))
1141 ;; Filename in this comment line is extracted by sed in
1143 (insert (format ";; FILE: %s\n" file
))
1144 (insert (format "(define-char-code-property '%S %S\n %S)\n"
1145 prop file docstring
))
1146 (with-temp-file file
1147 (message "Generating %s..." file
)
1148 (setq table
(funcall generator prop
))
1150 (unless (subrp (symbol-function describer
))
1151 (byte-compile describer
)
1152 (setq describer
(symbol-function describer
)))
1153 (set-char-table-extra-slot table
3 describer
))
1154 (insert ";; Automatically generated from UnicodeData.txt.\n"
1155 (format "(define-char-code-property '%S %S %S)\n"
1156 prop table docstring
)
1157 ";; Local Variables:\n"
1158 ";; coding: utf-8\n"
1159 ";; no-byte-compile: t\n"
1161 (format ";; %s ends here\n" file
)))))
1162 (message "Writing %s..." charprop-file
)
1163 (insert ";; Local Variables:\n"
1164 ";; coding: utf-8\n"
1165 ";; no-byte-compile: t\n"
1167 (format ";; %s ends here\n" charprop-file
)))))
1171 ;; arch-tag: 961c862e-b821-447e-9b8a-bfbab9c2d525
1172 ;;; unidata-gen.el ends here