1 ;; unidata-gen.el -- Create files containing character property data.
2 ;; Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011
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 ;; (define-char-code-property PROP FILE)
37 ;; where PROP is a symbol representing a character property
38 ;; (name, general-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 contain one or more forms of this format:
46 ;; (define-char-code-property 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 Lisp functions
53 ;; `get-char-code-property' and `put-char-code-property', and C
54 ;; function uniprop_table are called.
56 ;; FORMAT OF A CHAR TABLE
58 ;; We want to make a file size containing a char-table small. We
59 ;; also want to load the file and get a property value fast. We
60 ;; also want to reduce the used memory after loading it. So,
61 ;; instead of naively storing a property value for each character in
62 ;; a char-table (and write it out into a file), we store compressed
63 ;; data in a char-table as below.
65 ;; If succeeding 128*N characters have the same property value, we
66 ;; store that value (or the encoded one) for them. Otherwise,
67 ;; compress values (or the encoded ones) for succeeding 128
68 ;; characters into a single string and store it for those
69 ;; characters. The way of compression depends on a property. See
70 ;; the section "SIMPLE TABLE", "RUN-LENGTH TABLE", and "WORD-LIST
73 ;; The char table has five extra slots:
74 ;; 1st: property symbol
75 ;; 2nd: function to call to get a property value,
76 ;; or an index number of C function to decode the value,
77 ;; or nil if the value can be directly got from the table.
78 ;; 3nd: function to call to put a property value,
79 ;; or an index number of C function to encode the value,
80 ;; or nil if the value can be directly stored in the table.
81 ;; 4th: function to call to get a description of a property value, or nil
82 ;; 5th: data referred by the above functions
84 ;; List of elements of this form:
85 ;; (CHAR-or-RANGE PROP1 PROP2 ... PROPn)
86 ;; CHAR-or-RANGE: a character code or a cons of character codes
87 ;; PROPn: string representing the nth property value
89 (defvar unidata-list nil
)
91 ;; Name of the directory containing files of Unicode Character
94 (defvar unidata-dir nil
)
96 (defun unidata-setup-list (unidata-text-file)
97 (let* ((table (list nil
))
99 (block-names '(("^<CJK Ideograph" . CJK\ IDEOGRAPH
)
100 ("^<Hangul Syllable" . HANGUL\ SYLLABLE
)
101 ("^<.*Surrogate" . nil
)
102 ("^<.*Private Use" . PRIVATE\ USE
)))
104 (setq unidata-text-file
(expand-file-name unidata-text-file unidata-dir
))
105 (or (file-readable-p unidata-text-file
)
106 (error "File not readable: %s" unidata-text-file
))
108 ;; Insert a file of this format:
109 ;; (CHAR NAME CATEGORY ...)
110 ;; where CHAR is a character code, the following elements are strings
111 ;; representing character properties.
112 (insert-file-contents unidata-text-file
)
113 (goto-char (point-min))
116 (setq val
(read (current-buffer))
120 ;; Check this kind of block.
121 ;; 4E00;<CJK Ideograph, First>;Lo;0;L;;;;;N;;;;;
122 ;; 9FCB;<CJK Ideograph, Last>;Lo;0;L;;;;;N;;;;;
123 (if (and (= (aref name
0) ?
<)
124 (string-match ", First>$" name
))
128 (setq val
(read (current-buffer))
130 block-name
(cadr val
)
133 (if (string-match (caar l
) block-name
)
134 (setq name
(cdar l
) l nil
)
137 ;; As this is a surrogate pair range, ignore it.
139 (setcar val
(cons first char
))
140 (setcar (cdr val
) name
))))
143 (setcdr tail
(list val
))
144 (setq tail
(cdr tail
))))
146 (setq unidata-list
(cdr table
))))
148 ;; Alist of this form:
149 ;; (PROP INDEX GENERATOR FILENAME DOCSTRING DESCRIBER VAL-LIST)
150 ;; PROP: character property
151 ;; INDEX: index to each element of unidata-list for PROP.
152 ;; It may be a function that generates an alist of character codes
153 ;; vs. the corresponding property values.
154 ;; GENERATOR: function to generate a char-table
155 ;; FILENAME: filename to store the char-table
156 ;; DOCSTRING: docstring for the property
157 ;; DESCRIBER: function to call to get a description string of property value
158 ;; DEFAULT: the default value of the property
159 ;; VAL-LIST: list of specially ordered property values
161 (defconst unidata-prop-alist
163 1 unidata-gen-table-name
"uni-name.el"
164 "Unicode character name.
165 Property value is a string.")
167 2 unidata-gen-table-symbol
"uni-category.el"
168 "Unicode general category.
169 Property value is one of the following symbols:
170 Lu, Ll, Lt, Lm, Lo, Mn, Mc, Me, Nd, Nl, No, Pc, Pd, Ps, Pe, Pi, Pf, Po,
171 Sm, Sc, Sk, So, Zs, Zl, Zp, Cc, Cf, Cs, Co, Cn"
172 unidata-describe-general-category
174 ;; The order of elements must be in sync with unicode_category_t
175 ;; in src/character.h.
176 (Lu Ll Lt Lm Lo Mn Mc Me Nd Nl No Pc Pd Ps Pe Pi Pf Po
177 Sm Sc Sk So Zs Zl Zp Cc Cf Cs Co Cn
))
178 (canonical-combining-class
179 3 unidata-gen-table-integer
"uni-combining.el"
180 "Unicode canonical combining class.
181 Property value is an integer."
182 unidata-describe-canonical-combining-class
)
184 4 unidata-gen-table-symbol
"uni-bidi.el"
186 Property value is one of the following symbols:
187 L, LRE, LRO, R, AL, RLE, RLO, PDF, EN, ES, ET,
188 AN, CS, NSM, BN, B, S, WS, ON"
189 unidata-describe-bidi-class
191 ;; The order of elements must be in sync with bidi_type_t in
193 (L R EN AN BN B AL LRE LRO RLE RLO PDF ES ET CS NSM S WS ON
))
195 5 unidata-gen-table-decomposition
"uni-decomposition.el"
196 "Unicode decomposition mapping.
197 Property value is a list of characters. The first element may be
198 one of these symbols representing compatibility formatting tag:
199 font, noBreak, initial, medial, final, isolated, circle, super,
200 sub, vertical, wide, narrow, small, square, fraction, compat"
201 unidata-describe-decomposition
)
203 6 unidata-gen-table-integer
"uni-decimal.el"
204 "Unicode numeric value (decimal digit).
205 Property value is an integer.")
207 7 unidata-gen-table-integer
"uni-digit.el"
208 "Unicode numeric value (digit).
209 Property value is an integer.")
211 8 unidata-gen-table-numeric
"uni-numeric.el"
212 "Unicode numeric value (numeric).
213 Property value is an integer or a floating point.")
215 9 unidata-gen-table-symbol
"uni-mirrored.el"
216 "Unicode bidi mirrored flag.
217 Property value is a symbol `Y' or `N'. See also the property `mirroring'.")
219 10 unidata-gen-table-name
"uni-old-name.el"
220 "Unicode old names as published in Unicode 1.0.
221 Property value is a string.")
223 11 unidata-gen-table-name
"uni-comment.el"
224 "Unicode ISO 10646 comment.
225 Property value is a string.")
227 12 unidata-gen-table-character
"uni-uppercase.el"
228 "Unicode simple uppercase mapping.
229 Property value is a character."
232 13 unidata-gen-table-character
"uni-lowercase.el"
233 "Unicode simple lowercase mapping.
234 Property value is a character."
237 14 unidata-gen-table-character
"uni-titlecase.el"
238 "Unicode simple titlecase mapping.
239 Property value is a character."
242 unidata-gen-mirroring-list unidata-gen-table-character
"uni-mirrored.el"
243 "Unicode bidi-mirroring characters.
244 Property value is a character that has the corresponding mirroring image,
245 or nil for non-mirrored character.")))
247 ;; Functions to access the above data.
248 (defsubst unidata-prop-index
(prop) (nth 1 (assq prop unidata-prop-alist
)))
249 (defsubst unidata-prop-generator
(prop) (nth 2 (assq prop unidata-prop-alist
)))
250 (defsubst unidata-prop-file
(prop) (nth 3 (assq prop unidata-prop-alist
)))
251 (defsubst unidata-prop-docstring
(prop) (nth 4 (assq prop unidata-prop-alist
)))
252 (defsubst unidata-prop-describer
(prop) (nth 5 (assq prop unidata-prop-alist
)))
253 (defsubst unidata-prop-default
(prop) (nth 6 (assq prop unidata-prop-alist
)))
254 (defsubst unidata-prop-val-list
(prop) (nth 7 (assq prop unidata-prop-alist
)))
259 ;; If the type of character property value is character, and the
260 ;; values of succeeding character codes are usually different, we use
261 ;; a char-table described here to store such values.
263 ;; A char-table divides character code space (#x0..#x3FFFFF) into
264 ;; #x8000 blocks (each block contains 128 characters).
266 ;; If all characters of a block have no property, a char-table has the
267 ;; symbol nil for that block. Otherwise a char-table has a string of
268 ;; the following format for it.
270 ;; The first character of the string is ?\001.
271 ;; The second character of the string is FIRST-INDEX.
272 ;; The Nth (N > 1) character of the string is a property value of the
273 ;; character (BLOCK-HEAD + FIRST-INDEX + N - 2), where BLOCK-HEAD is
274 ;; the first character of the block.
276 ;; This kind of char-table has these extra slots:
277 ;; 1st: the property symbol
279 ;; 3rd: 0 (corresponding to uniprop_encode_character in chartab.c)
282 (defun unidata-gen-table-character (prop &rest ignore
)
283 (let ((table (make-char-table 'char-code-property-table
))
284 (prop-idx (unidata-prop-index prop
))
285 (vec (make-vector 128 0))
287 elt range val idx slot
)
288 (if (functionp prop-idx
)
289 (setq tail
(funcall prop-idx
)
292 (setq elt
(car tail
) tail
(cdr tail
))
293 (setq range
(car elt
)
294 val
(nth prop-idx elt
))
295 (if (= (length val
) 0)
297 (setq val
(string-to-number val
16)))
300 (set-char-table-range table range val
))
301 (let* ((start (lsh (lsh range -
7) 7))
302 (limit (+ start
127))
303 first-index last-index
)
306 (aset vec
(setq last-index
(setq first-index
(- range start
)))
308 (while (and (setq elt
(car tail
) range
(car elt
))
311 (setq val
(nth prop-idx elt
))
312 (when (> (length val
) 0)
313 (aset vec
(setq last-index
(- range start
))
314 (string-to-number val
16))
316 (setq first-index last-index
)))
317 (setq tail
(cdr tail
)))
319 (let ((str (string 1 first-index
))
321 (while (<= first-index last-index
)
322 (setq str
(format "%s%c" str
(or (aref vec first-index
) 0))
323 first-index
(1+ first-index
)))
324 (set-char-table-range table
(cons start limit
) str
))))))
326 (set-char-table-extra-slot table
0 prop
)
327 (set-char-table-extra-slot table
2 0)
334 ;; If many characters of successive character codes have the same
335 ;; property value, we use a char-table described here to store the
338 ;; At first, instead of a value itself, we store an index number to
339 ;; the VAL-TABLE (5th extra slot) in the table. We call that index
340 ;; number as VAL-CODE here after.
342 ;; A char-table divides character code space (#x0..#x3FFFFF) into
343 ;; #x8000 blocks (each block contains 128 characters).
345 ;; If all characters of a block have the same value, a char-table has
346 ;; VAL-CODE for that block. Otherwise a char-table has a string of
347 ;; the following format for that block.
349 ;; The first character of the string is ?\002.
350 ;; The following characters has this form:
351 ;; ( VAL-CODE RUN-LENGTH ? ) +
353 ;; VAL-CODE (0..127): index into VAL-TABLE.
354 ;; RUN-LENGTH (130..255):
355 ;; (RUN-LENGTH - 128) specifies how many characters have the same
356 ;; value. If omitted, it means 1.
358 ;; This kind of char-table has these extra slots:
359 ;; 1st: the property symbol
360 ;; 2nd: 0 (corresponding to uniprop_decode_value in chartab.c)
361 ;; 3rd: 1..3 (corresponding to uniprop_encode_xxx in chartab.c)
362 ;; 4th: function or nil
365 ;; Encode the character property value VAL into an integer value by
366 ;; VAL-LIST. By side effect, VAL-LIST is modified.
367 ;; VAL-LIST has this form:
368 ;; ((nil . 0) (VAL1 . 1) (VAL2 . 2) ...)
369 ;; If VAL is one of VALn, just return n.
370 ;; Otherwise, VAL-LIST is modified to this:
371 ;; ((nil . 0) (VAL1 . 1) (VAL2 . 2) ... (VAL . n+1))
373 (defun unidata-encode-val (val-list val
)
374 (let ((slot (assoc val val-list
))
378 (setq val-code
(length val-list
))
379 (nconc val-list
(list (cons val val-code
)))
382 ;; Generate a char-table for the character property PROP.
384 (defun unidata-gen-table (prop val-func default-value val-list
)
385 (let ((table (make-char-table 'char-code-property-table
))
386 (prop-idx (unidata-prop-index prop
))
387 (vec (make-vector 128 0))
388 tail elt range val val-code idx slot
390 (setq val-list
(cons nil
(copy-sequence val-list
)))
391 (setq tail val-list val-code
0)
392 ;; Convert (nil A B ...) to ((nil . 0) (A . 1) (B . 2) ...)
394 (setcar tail
(cons (car tail
) val-code
))
395 (setq tail
(cdr tail
) val-code
(1+ val-code
)))
396 (setq default-value
(unidata-encode-val val-list default-value
))
397 (set-char-table-range table t default-value
)
398 (set-char-table-range table nil default-value
)
399 (setq tail unidata-list
)
401 (setq elt
(car tail
) tail
(cdr tail
))
402 (setq range
(car elt
)
403 val
(funcall val-func
(nth prop-idx elt
)))
404 (setq val-code
(if val
(unidata-encode-val val-list val
)))
407 (set-char-table-range table range val-code
)
408 (let ((from (car range
)) (to (cdr range
)))
409 ;; If RANGE doesn't end at the char-table boundary (each
410 ;; 128 characters), we may have to carry over the data
411 ;; for the last several characters (at most 127 chars)
412 ;; to the next loop. In that case, set PREV-RANGE-DATA
413 ;; to ((FROM . TO) . VAL-CODE) where (FROM . TO)
414 ;; specifies the range of characters handled in the next
416 (when (< (logand to
#x7F
) #x7F
)
417 (if (< from
(logand to
#x1FFF80
))
418 (setq from
(logand to
#x1FFF80
)))
419 (setq prev-range-data
(cons (cons from to
) val-code
)))))
420 (let* ((start (lsh (lsh range -
7) 7))
421 (limit (+ start
127))
424 ;; See the comment above.
425 (when (and prev-range-data
426 (>= (cdr (car prev-range-data
)) start
))
427 (let ((from (car (car prev-range-data
)))
428 (to (cdr (car prev-range-data
)))
429 (vcode (cdr prev-range-data
)))
431 (aset vec
(- from start
) vcode
)
432 (setq from
(1+ from
)))))
433 (setq prev-range-data nil
)
435 (aset vec
(- range start
) val-code
))
436 (while (and (setq elt
(car tail
) range
(car elt
))
439 (setq new-val
(funcall val-func
(nth prop-idx elt
)))
440 (if (not (eq val new-val
))
442 val-code
(if val
(unidata-encode-val val-list val
))))
444 (aset vec
(- range start
) val-code
))
445 (setq tail
(cdr tail
)))
446 (setq str
"\002" val-code -
1 count
0)
449 (setq count
(1+ count
))
451 (setq str
(concat str
(string val-code
454 (setq str
(concat str
(string val-code val-code
)))
456 (setq str
(concat str
(string val-code
))))))
457 (setq val-code x count
1)))
461 (set-char-table-range table
(cons start limit
) val-code
))
463 (set-char-table-range table
(cons start limit
) str
)
465 (setq str
(concat str
(string val-code
(+ count
128))))
467 (setq str
(concat str
(string val-code val-code
)))
468 (setq str
(concat str
(string val-code
)))))
469 (set-char-table-range table
(cons start limit
) str
))))))
471 (set-char-table-extra-slot table
0 prop
)
472 (set-char-table-extra-slot table
4 (vconcat (mapcar 'car val-list
)))
475 (defun unidata-gen-table-symbol (prop default-value val-list
)
476 (let ((table (unidata-gen-table prop
477 #'(lambda (x) (and (> (length x
) 0)
479 default-value val-list
)))
480 (set-char-table-extra-slot table
1 0)
481 (set-char-table-extra-slot table
2 1)
484 (defun unidata-gen-table-integer (prop default-value val-list
)
485 (let ((table (unidata-gen-table prop
486 #'(lambda (x) (and (> (length x
) 0)
487 (string-to-number x
)))
488 default-value val-list
)))
489 (set-char-table-extra-slot table
1 0)
490 (set-char-table-extra-slot table
2 1)
493 (defun unidata-gen-table-numeric (prop default-value val-list
)
494 (let ((table (unidata-gen-table prop
496 (if (string-match "/" x
)
497 (/ (float (string-to-number x
))
499 (substring x
(match-end 0))))
501 (string-to-number x
))))
502 default-value val-list
)))
503 (set-char-table-extra-slot table
1 0)
504 (set-char-table-extra-slot table
2 2)
510 ;; If the table is for `name' property, each character in the string
512 ;; DIFF-HEAD-CODE (0, 1, or 2):
513 ;; specifies how to decode the following characters.
514 ;; WORD-CODE (3..#x7FF excluding '-', '0'..'9', 'A'..'Z'):
515 ;; specifies an index number into WORD-TABLE (see below)
516 ;; Otherwise (' ', '-', '0'..'9', 'A'..'Z'):
517 ;; specifies a literal word.
519 ;; The 4th slots is a vector:
520 ;; [ WORD-TABLE BLOCK-NAME HANGUL-JAMO-TABLE ]
521 ;; WORD-TABLE is a vector of word symbols.
522 ;; BLOCK-NAME is a vector of name symbols for a block of characters.
523 ;; HANGUL-JAMO-TABLE is `unidata-name-jamo-name-table'.
525 ;; Return the difference of symbol list L1 and L2 in this form:
526 ;; (DIFF-HEAD SYM1 SYM2 ...)
527 ;; DIFF-HEAD is ((SAME-HEAD-LENGTH * 16) + SAME-TAIL-LENGTH).
528 ;; Ex: If L1 is (a b c d e f) and L2 is (a g h e f), this function
529 ;; returns ((+ (* 1 16) 2) g h).
530 ;; It means that we can get L2 from L1 by prepending the first element
531 ;; of L1 and appending the last 2 elements of L1 to the list (g h).
532 ;; If L1 and L2 don't have common elements at the head and tail,
533 ;; set DIFF-HEAD to -1 and SYM1 ... to the elements of L2.
535 (defun unidata-word-list-diff (l1 l2
)
542 (while (and l1
(eq (car l1
) (car l2
)))
544 l1
(cdr l1
) len1
(1- len1
) l2
(cdr l2
) len2
(1- len2
)))
545 (while (and (< end len1
) (< end len2
)
546 (eq (nth (- len1 end
1) l1
) (nth (- len2 end
1) l2
)))
547 (setq end
(1+ end
))))
548 (if (= (+ beg end
) 0)
549 (setq result
(list -
1))
550 (setq result
(list (+ (* beg
16) (+ beg
(- len1 end
))))))
552 (setcdr result
(cons (nth (- len2 end
1) l2
) (cdr result
)))
556 ;; Return a compressed form of the vector VEC. Each element of VEC is
557 ;; a list of symbols of which names can be concatenated to form a
558 ;; character name. This function changes those elements into
559 ;; compressed forms by utilizing the fact that diff of consecutive
560 ;; elements is usually small.
562 (defun unidata-word-list-compress (vec)
563 (let (last-elt last-idx diff-head tail elt val
)
565 (setq elt
(aref vec i
))
570 (setq val
(unidata-word-list-diff last-elt elt
))
573 val
(cons 0 (cdr val
)))
574 (if (eq diff-head
(car val
))
575 (setq val
(cons 2 (cdr val
)))
576 (setq diff-head
(car val
))
578 (setq val
(cons 1 val
))))))
580 (setq last-idx i last-elt elt
)))
584 (let ((shorter (make-vector (1+ last-idx
) nil
)))
585 (dotimes (i (1+ last-idx
))
586 (aset shorter i
(aref vec i
)))
587 (setq vec shorter
))))
590 ;; Encode the word index IDX into a characters code that can be
591 ;; embedded in a string.
593 (defsubst unidata-encode-word
(idx)
597 ;; Decode the character code CODE (that is embedded in a string) into
598 ;; the corresponding word name by looking up WORD-TABLE.
600 (defsubst unidata-decode-word
(code word-table
)
601 (setq code
(- code
3))
602 (if (< code
(length word-table
))
603 (aref word-table code
)))
605 ;; Table of short transliterated name symbols of Hangul Jamo divided
606 ;; into Choseong, Jungseong, and Jongseong.
608 (defconst unidata-name-jamo-name-table
609 [[G GG N D DD R M B BB S SS nil J JJ C K T P H
]
610 [A AE YA YAE EO E YEO YE O WA WAE OE YO U WEO WE WI YU EU YI I
]
611 [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
]])
613 ;; Return a name of CHAR. VAL is the current value of (aref TABLE
616 (defun unidata-get-name (char val table
)
619 (if (> (aref val
0) 0)
621 (let* ((first-char (lsh (lsh char -
7) 7))
622 (word-table (aref (char-table-extra-slot table
4) 0))
625 (vec (make-vector 128 nil
))
627 (case-fold-search nil
)
628 c word-list tail-list last-list word diff-head
)
630 (setq c
(aref val i
))
633 (if (or word-list tail-list
)
635 (setq last-list
(nconc word-list tail-list
))))
636 (setq i
(1+ i
) idx
(1+ idx
)
637 word-list nil tail-list nil
)
642 (prog1 (aref val i
) (setq i
(1+ i
)))))
643 (setq tail-list
(nthcdr (% diff-head
16) last-list
))
644 (dotimes (i (/ diff-head
16))
645 (setq word-list
(nconc word-list
(list (car l
)))
650 (unidata-decode-word c word-table
))))
652 (if (or word-list tail-list
)
653 (aset vec idx
(nconc word-list tail-list
)))
656 (setq c
(+ first-char i
))
657 (let ((name (aref vec i
)))
659 (let ((tail (cdr (setq name
(copy-sequence name
))))
662 (setq elt
(car tail
))
663 (or (string= elt
"-")
665 (setcdr tail
(cons elt
(cdr tail
)))
667 (setq tail
(cddr tail
)))
668 (setq name
(apply 'concat name
))))
674 ((and (integerp val
) (> val
0))
675 (let* ((symbol-table (aref (char-table-extra-slot table
4) 1))
676 (sym (aref symbol-table
(1- val
))))
677 (cond ((eq sym
'HANGUL\ SYLLABLE
)
678 (let ((jamo-name-table (aref (char-table-extra-slot table
4) 2)))
679 ;; SIndex = S - SBase
680 (setq char
(- char
#xAC00
))
681 (let ( ;; LIndex = SIndex / NCount
683 ;; VIndex = (SIndex % NCount) * TCount
684 (V (/ (% char
588) 28))
685 ;; TIndex = SIndex % TCount
687 (format "HANGUL SYLLABLE %s%s%s"
688 ;; U+110B is nil in this table.
689 (or (aref (aref jamo-name-table
0) L
) "")
690 (aref (aref jamo-name-table
1) V
)
692 (aref (aref jamo-name-table
2) (1- T
)))))))
693 ((eq sym
'CJK\ IDEOGRAPH
)
694 (format "%s-%04X" sym char
))
695 ((eq sym
'CJK\ COMPATIBILITY\ IDEOGRAPH
)
696 (format "%s-%04X" sym char
))
697 ((eq sym
'VARIATION\ SELECTOR
)
698 (format "%s-%d" sym
(+ (- char
#xe0100
) 17))))))))
700 ;; Store VAL as the name of CHAR in TABLE.
702 (defun unidata-put-name (char val table
)
703 (let ((current-val (aref table char
)))
704 (if (and (stringp current-val
) (= (aref current-val
0) 0))
705 (funcall (char-table-extra-slot table
1) char current-val table
))
706 (aset table char val
)))
708 (defun unidata-get-decomposition (char val table
)
714 (if (> (aref val
0) 0)
716 (let* ((first-char (lsh (lsh char -
7) 7))
717 (word-table (char-table-extra-slot table
4))
720 (vec (make-vector 128 nil
))
722 (case-fold-search nil
)
723 c word-list tail-list last-list word diff-head
)
725 (setq c
(aref val i
))
728 (if (or word-list tail-list
)
730 (setq last-list
(nconc word-list tail-list
))))
731 (setq i
(1+ i
) idx
(1+ idx
)
732 word-list nil tail-list nil
)
737 (prog1 (aref val i
) (setq i
(1+ i
)))))
738 (setq tail-list
(nthcdr (% diff-head
16) last-list
))
739 (dotimes (i (/ diff-head
16))
740 (setq word-list
(nconc word-list
(list (car l
)))
744 (list (or (unidata-decode-word c word-table
) c
)))
746 (if (or word-list tail-list
)
747 (aset vec idx
(nconc word-list tail-list
)))
749 (aset table
(+ first-char i
) (aref vec i
)))
750 (aref vec
(- char first-char
)))))
753 ((and (eq val
0) (>= char
#xAC00
) (<= char
#xD7A3
))
754 ;; SIndex = S (char) - SBase (#xAC00)
755 (setq char
(- char
#xAC00
))
756 (let (;; L = LBase + SIndex / NCount
757 (L (+ #x1100
(/ char
588)))
758 ;; V = VBase + (SIndex % NCount) * TCount
759 (V (+ #x1161
(/ (% char
588) 28)))
760 ;; LV = SBase + (SIndex / TCount) * TCount
761 (LV (+ #xAC00
(* (/ char
28) 28)))
762 ;; T = TBase + SIndex % TCount
763 (T (+ #x11A7
(% char
28))))
770 ;; Store VAL as the decomposition information of CHAR in TABLE.
772 (defun unidata-put-decomposition (char val table
)
773 (let ((current-val (aref table char
)))
774 (if (and (stringp current-val
) (= (aref current-val
0) 0))
775 (funcall (char-table-extra-slot table
1) char current-val table
))
776 (aset table char val
)))
778 ;; UnicodeData.txt contains these lines:
779 ;; 0000;<control>;Cc;0;BN;;;;;N;NULL;;;;
781 ;; 0020;SPACE;Zs;0;WS;;;;;N;;;;;
783 ;; The following command yields a file of about 96K bytes.
784 ;; % gawk -F ';' '{print $1,$2;}' < UnicodeData.txt | gzip > temp.gz
785 ;; With the following function, we can get a file of almost the same
788 ;; Generate a char-table for character names.
790 (defun unidata-gen-table-word-list (prop val-func
)
791 (let ((table (make-char-table 'char-code-property-table
))
792 (prop-idx (unidata-prop-index prop
))
793 (word-list (list nil
))
795 block-list block-word-table block-end
796 tail elt range val idx slot
)
797 (setq tail unidata-list
)
800 (setq elt
(car tail
) tail
(cdr tail
))
801 (setq range
(car elt
)
802 val
(funcall val-func
(nth prop-idx elt
)))
803 ;; Treat the sequence of "CJK COMPATIBILITY IDEOGRAPH-XXXX" and
804 ;; "VARIATION SELECTOR-XXX" as a block.
805 (if (and (consp val
) (eq prop
'name
)
806 (or (and (eq (car val
) 'CJK
)
807 (eq (nth 1 val
) 'COMPATIBILITY
))
808 (and (>= range
#xe0100
)
809 (eq (car val
) 'VARIATION
)
810 (eq (nth 1 val
) 'SELECTOR
))))
811 (let ((first (car val
))
814 (while (and (setq elt
(car tail
) range
(car elt
)
815 val
(funcall val-func
(nth prop-idx elt
)))
818 (eq second
(nth 1 val
)))
819 (setq block-end range
821 (setq range
(cons start block-end
)
822 val
(if (eq first
'CJK
) 'CJK\ COMPATIBILITY\ IDEOGRAPH
823 'VARIATION\ SELECTOR
))))
827 (let ((slot (assq val block-list
)))
828 (setq range
(cons (car range
) (cdr range
)))
829 (setq block-end
(cdr range
))
831 (nconc slot
(list range
))
832 (push (list val range
) block-list
))))
833 (let* ((start (lsh (lsh range -
7) 7))
834 (limit (+ start
127))
836 (vec (make-vector 128 nil
))
838 (if (<= start block-end
)
839 ;; START overlap with the previous block.
840 (aset table range
(nth prop-idx elt
))
842 (aset vec
(- range start
) val
))
843 (while (and (setq elt
(car tail
) range
(car elt
))
846 (setq val
(funcall val-func
(nth prop-idx elt
)))
848 (aset vec
(- range start
) val
))
849 (setq tail
(cdr tail
)))
850 (setq vec
(unidata-word-list-compress vec
))
852 (dotimes (i (length vec
))
853 (dolist (elt (aref vec i
))
855 (let ((slot (assq elt word-list
)))
857 (setcdr slot
(1+ (cdr slot
)))
859 (cons (cons elt
1) (cdr word-list
))))))))
860 (set-char-table-range table
(cons start limit
) vec
))))))
861 (setq word-list
(sort (cdr word-list
)
862 #'(lambda (x y
) (> (cdr x
) (cdr y
)))))
863 (setq tail word-list idx
0)
865 (setcdr (car tail
) (unidata-encode-word idx
))
866 (setq idx
(1+ idx
) tail
(cdr tail
)))
867 (setq word-table
(make-vector (length word-list
) nil
))
869 (dolist (elt word-list
)
870 (aset word-table idx
(car elt
))
873 (if (and (eq prop
'decomposition
)
875 (error "Too many symbols in decomposition data"))
877 (dotimes (i (/ #x110000
128))
878 (let* ((idx (* i
128))
879 (vec (aref table idx
)))
881 (dotimes (i (length vec
))
882 (let ((tail (aref vec i
))
888 code
(if (integerp elt
) elt
889 (cdr (assq elt word-list
))))
890 (setcar tail
(string code
))
891 (setq tail
(cdr tail
)))
892 (aset vec i
(mapconcat 'identity
(aref vec i
) "")))))
893 (set-char-table-range
894 table
(cons idx
(+ idx
127))
895 (mapconcat 'identity vec
"")))))
897 (setq block-word-table
(make-vector (length block-list
) nil
))
899 (dolist (elt block-list
)
900 (dolist (e (cdr elt
))
901 (set-char-table-range table e
(1+ idx
)))
902 (aset block-word-table idx
(car elt
))
905 (set-char-table-extra-slot table
0 prop
)
906 (set-char-table-extra-slot table
4 (cons word-table block-word-table
))
909 (defun unidata-split-name (str)
912 (let ((len (length str
))
919 (setq c
(aref str i
))
921 (setq l
(cons (intern (substring str idx i
)) l
)
923 (if (and (= c ?-
) (< idx i
)
924 (< (1+ i
) len
) (/= (aref str
(1+ i
)) 32))
925 (setq l
(cons '-
(cons (intern (substring str idx i
)) l
))
927 (nreverse (cons (intern (substring str idx
)) l
))))))
929 (defun unidata-gen-table-name (prop &rest ignore
)
930 (let* ((table (unidata-gen-table-word-list prop
'unidata-split-name
))
931 (word-tables (char-table-extra-slot table
4)))
932 (byte-compile 'unidata-get-name
)
933 (byte-compile 'unidata-put-name
)
934 (set-char-table-extra-slot table
1 (symbol-function 'unidata-get-name
))
935 (set-char-table-extra-slot table
2 (symbol-function 'unidata-put-name
))
938 (set-char-table-extra-slot table
4
939 (vector (car word-tables
)
941 unidata-name-jamo-name-table
))
942 (set-char-table-extra-slot table
4
943 (vector (car word-tables
))))
946 (defun unidata-split-decomposition (str)
949 (let ((len (length str
))
956 (setq c
(aref str i
))
958 (setq l
(if (= (aref str idx
) ?
<)
959 (cons (intern (substring str
(1+ idx
) (1- i
))) l
)
960 (cons (string-to-number (substring str idx i
) 16) l
))
962 (if (= (aref str idx
) ?
<)
963 (setq l
(cons (intern (substring str
(1+ idx
) (1- len
))) l
))
964 (setq l
(cons (string-to-number (substring str idx len
) 16) l
)))
968 (defun unidata-gen-table-decomposition (prop &rest ignore
)
969 (let* ((table (unidata-gen-table-word-list prop
'unidata-split-decomposition
))
970 (word-tables (char-table-extra-slot table
4)))
971 (byte-compile 'unidata-get-decomposition
)
972 (byte-compile 'unidata-put-decomposition
)
973 (set-char-table-extra-slot table
1
974 (symbol-function 'unidata-get-decomposition
))
975 (set-char-table-extra-slot table
2
976 (symbol-function 'unidata-put-decomposition
))
977 (set-char-table-extra-slot table
4 (car word-tables
))
982 (defun unidata-describe-general-category (val)
985 (Lu .
"Letter, Uppercase")
986 (Ll .
"Letter, Lowercase")
987 (Lt .
"Letter, Titlecase")
988 (Lm .
"Letter, Modifier")
989 (Lo .
"Letter, Other")
990 (Mn .
"Mark, Nonspacing")
991 (Mc .
"Mark, Spacing Combining")
992 (Me .
"Mark, Enclosing")
993 (Nd .
"Number, Decimal Digit")
994 (Nl .
"Number, Letter")
995 (No .
"Number, Other")
996 (Pc .
"Punctuation, Connector")
997 (Pd .
"Punctuation, Dash")
998 (Ps .
"Punctuation, Open")
999 (Pe .
"Punctuation, Close")
1000 (Pi .
"Punctuation, Initial quote")
1001 (Pf .
"Punctuation, Final quote")
1002 (Po .
"Punctuation, Other")
1003 (Sm .
"Symbol, Math")
1004 (Sc .
"Symbol, Currency")
1005 (Sk .
"Symbol, Modifier")
1006 (So .
"Symbol, Other")
1007 (Zs .
"Separator, Space")
1008 (Zl .
"Separator, Line")
1009 (Zp .
"Separator, Paragraph")
1010 (Cc .
"Other, Control")
1011 (Cf .
"Other, Format")
1012 (Cs .
"Other, Surrogate")
1013 (Co .
"Other, Private Use")
1014 (Cn .
"Other, Not Assigned")))))
1016 (defun unidata-describe-canonical-combining-class (val)
1018 '((0 .
"Spacing, split, enclosing, reordrant, and Tibetan subjoined")
1019 (1 .
"Overlays and interior")
1021 (8 .
"Hiragana/Katakana voicing marks")
1023 (10 .
"Start of fixed position classes")
1024 (199 .
"End of fixed position classes")
1025 (200 .
"Below left attached")
1026 (202 .
"Below attached")
1027 (204 .
"Below right attached")
1028 (208 .
"Left attached (reordrant around single base character)")
1029 (210 .
"Right attached")
1030 (212 .
"Above left attached")
1031 (214 .
"Above attached")
1032 (216 .
"Above right attached")
1033 (218 .
"Below left")
1035 (222 .
"Below right")
1036 (224 .
"Left (reordrant around single base character)")
1038 (228 .
"Above left")
1040 (232 .
"Above right")
1041 (233 .
"Double below")
1042 (234 .
"Double above")
1043 (240 .
"Below (iota subscript)")))))
1045 (defun unidata-describe-bidi-class (val)
1047 '((L .
"Left-to-Right")
1048 (LRE .
"Left-to-Right Embedding")
1049 (LRO .
"Left-to-Right Override")
1050 (R .
"Right-to-Left")
1051 (AL .
"Right-to-Left Arabic")
1052 (RLE .
"Right-to-Left Embedding")
1053 (RLO .
"Right-to-Left Override")
1054 (PDF .
"Pop Directional Format")
1055 (EN .
"European Number")
1056 (ES .
"European Number Separator")
1057 (ET .
"European Number Terminator")
1058 (AN .
"Arabic Number")
1059 (CS .
"Common Number Separator")
1060 (NSM .
"Non-Spacing Mark")
1061 (BN .
"Boundary Neutral")
1062 (B .
"Paragraph Separator")
1063 (S .
"Segment Separator")
1065 (ON .
"Other Neutrals")))))
1067 (defun unidata-describe-decomposition (val)
1070 (if (symbolp x
) (symbol-name x
)
1072 (compose-string (string x
) 0 1 (string ?
\t x ?
\t))
1076 (defun unidata-gen-mirroring-list ()
1077 (let ((head (list nil
))
1080 (insert-file-contents (expand-file-name "BidiMirroring.txt" unidata-dir
))
1081 (goto-char (point-min))
1083 (while (re-search-forward "^\\([0-9A-F]+\\);\\s +\\([0-9A-F]+\\)" nil t
)
1084 (let ((char (string-to-number (match-string 1) 16))
1085 (mirror (match-string 2)))
1086 (setq tail
(setcdr tail
(list (list char mirror
)))))))
1089 ;; Verify if we can retrieve correct values from the generated
1092 (defun unidata-check ()
1093 (dolist (elt unidata-prop-alist
)
1094 (let* ((prop (car elt
))
1095 (index (unidata-prop-index prop
))
1096 (generator (unidata-prop-generator prop
))
1098 (message "Generating %S table..." prop
)
1099 (funcall generator prop
)))
1100 (decoder (char-table-extra-slot table
1))
1102 (dolist (e unidata-list
)
1103 (let ((char (car e
))
1104 (val1 (nth index e
))
1106 (if (and (stringp val1
) (= (length val1
) 0))
1108 (unless (consp char
)
1109 (setq val2
(funcall decoder char
(aref table char
) table
))
1111 (cond ((eq generator
'unidata-gen-table-symbol
)
1112 (setq val1
(intern val1
)))
1113 ((eq generator
'unidata-gen-table-integer
)
1114 (setq val1
(string-to-number val1
)))
1115 ((eq generator
'unidata-gen-table-character
)
1116 (setq val1
(string-to-number val1
16)))
1117 ((eq generator
'unidata-gen-table-decomposition
)
1118 (setq val1
(unidata-split-decomposition val1
)))))
1119 (when (>= char check
)
1120 (message "%S %04X" prop check
)
1121 (setq check
(+ check
#x400
)))
1122 (or (equal val1 val2
)
1123 (insert (format "> %04X %S\n< %04X %S\n"
1124 char val1 char val2
)))
1127 ;; The entry function. It generates files described in the header
1128 ;; comment of this file.
1130 (defun unidata-gen-files (&optional data-dir unidata-text-file
)
1132 (setq data-dir
(car command-line-args-left
)
1133 command-line-args-left
(cdr command-line-args-left
)
1134 unidata-text-file
(car command-line-args-left
)
1135 command-line-args-left
(cdr command-line-args-left
)))
1136 (let ((coding-system-for-write 'utf-8-unix
)
1137 (charprop-file "charprop.el")
1138 (unidata-dir data-dir
))
1139 (dolist (elt unidata-prop-alist
)
1140 (let* ((prop (car elt
))
1141 (file (unidata-prop-file prop
)))
1142 (if (file-exists-p file
)
1143 (delete-file file
))))
1144 (unidata-setup-list unidata-text-file
)
1145 (with-temp-file charprop-file
1146 (insert ";; Automatically generated by unidata-gen.el.\n")
1147 (dolist (elt unidata-prop-alist
)
1148 (let* ((prop (car elt
))
1149 (generator (unidata-prop-generator prop
))
1150 (file (unidata-prop-file prop
))
1151 (docstring (unidata-prop-docstring prop
))
1152 (describer (unidata-prop-describer prop
))
1153 (default-value (unidata-prop-default prop
))
1154 (val-list (unidata-prop-val-list prop
))
1156 ;; Filename in this comment line is extracted by sed in
1158 (insert (format ";; FILE: %s\n" file
))
1159 (insert (format "(define-char-code-property '%S %S\n %S)\n"
1160 prop file docstring
))
1162 (message "Generating %s..." file
)
1163 (when (file-exists-p file
)
1164 (insert-file-contents file
)
1165 (goto-char (point-max))
1166 (search-backward ";; Local Variables:"))
1167 (setq table
(funcall generator prop default-value val-list
))
1169 (unless (subrp (symbol-function describer
))
1170 (byte-compile describer
)
1171 (setq describer
(symbol-function describer
)))
1172 (set-char-table-extra-slot table
3 describer
))
1174 (insert ";; Copyright (C) 1991-2009 Unicode, Inc.
1175 ;; This file was generated from the Unicode data files at
1176 ;; http://www.unicode.org/Public/UNIDATA/.
1177 ;; See lisp/international/README for the copyright and permission notice.\n"))
1178 (insert (format "(define-char-code-property '%S %S %S)\n"
1179 prop table docstring
))
1181 (insert ";; Local Variables:\n"
1182 ";; coding: utf-8\n"
1183 ";; no-byte-compile: t\n"
1185 (format ";; %s ends here\n" file
)))
1187 (message "Generating %s...done" file
))))
1188 (message "Writing %s..." charprop-file
)
1189 (insert ";; Local Variables:\n"
1190 ";; coding: utf-8\n"
1191 ";; no-byte-compile: t\n"
1193 (format ";; %s ends here\n" charprop-file
)))))
1197 ;;; unidata-gen.el ends here