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, 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., 51 Franklin Street, Fifth Floor,
21 ;; Boston, MA 02110-1301, USA.
27 ;; This file must be byte-compilable/loadable by `temacs' and also
28 ;; the entry function `unidata-gen-files' must be runnable by
31 ;; FILES TO BE GENERATED
33 ;; The entry function `unidata-gen-files' generates these files in
34 ;; the current directory.
37 ;; It contains a series of forms of this format:
38 ;; (char-code-property-register PROP FILE)
39 ;; where PROP is a symbol representing a character property
40 ;; (name, generic-category, etc), and FILE is a name of one of
41 ;; the following files.
43 ;; uni-name.el, uni-category.el, uni-combining.el, uni-bidi.el,
44 ;; uni-decomposition.el, uni-decimal.el, uni-digit.el, uni-numeric.el,
45 ;; uni-mirrored.el, uni-old-name.el, uni-comment.el, uni-uppercase.el,
46 ;; uni-lowercase.el, uni-titlecase.el
47 ;; They each contain a single form of this format:
48 ;; (char-code-property-register PROP CHAR-TABLE)
49 ;; where PROP is the same as above, and CHAR-TABLE is a
50 ;; char-table containing property values in a compressed format.
52 ;; When they are installed in .../lisp/international/, the file
53 ;; "charprop.el" is preloaded in loadup.el. The other files are
54 ;; automatically loaded when the functions `get-char-code-property'
55 ;; and `put-char-code-property' are called.
57 ;; FORMAT OF A CHAR TABLE
59 ;; We want to make a file size containing a char-table small. We
60 ;; also want to load the file and get a property value fast. We
61 ;; also want to reduce the used memory after loading it. So,
62 ;; instead of naively storing a property value for each character in
63 ;; a char-table (and write it out into a file), we store compressed
64 ;; data in a char-table as below.
66 ;; If succeeding 128*N characters have the same property value, we
67 ;; store that value for them. Otherwise, compress values for
68 ;; succeeding 128 characters into a single string and store it as a
69 ;; value for those characters. The way of compression depends on a
70 ;; property. See the section "SIMPLE TABLE", "RUN-LENGTH TABLE",
71 ;; and "WORD-LIST TABLE".
73 ;; The char table has four extra slots:
74 ;; 1st: property symbol
75 ;; 2nd: function to call to get a property value
76 ;; 3nd: function to call to put a property value
77 ;; 4th: function to call to get a description of a property value
78 ;; 5th: data referred by the above functions
80 ;; List of elements of this form:
81 ;; (CHAR-or-RANGE PROP1 PROP2 ... PROPn)
82 ;; CHAR-or-RANGE: a character code or a cons of character codes
83 ;; PROPn: string representing the nth property value
85 (defvar unidata-list nil
)
87 (defun unidata-setup-list (unidata-text-file)
88 (let* ((table (list nil
))
90 (block-names '(("^<CJK Ideograph" . CJK\ IDEOGRAPH
)
91 ("^<Hangul Syllable" . HANGUL\ SYLLABLE
)
92 ("^<.*Surrogate" . nil
)
93 ("^<.*Private Use" . PRIVATE\ USE
)))
95 (or (file-readable-p unidata-text-file
)
96 (error "File not readable: %s" unidata-text-file
))
98 (insert-file-contents unidata-text-file
)
99 (goto-char (point-min))
102 (setq val
(read (current-buffer))
106 ;; Check this kind of block.
107 ;; 4E00;<CJK Ideograph, First>;Lo;0;L;;;;;N;;;;;
108 ;; 9FA5;<CJK Ideograph, Last>;Lo;0;L;;;;;N;;;;;
109 (if (and (= (aref name
0) ?
<)
110 (string-match ", First>$" name
))
114 (setq val
(read (current-buffer))
116 block-name
(cadr val
)
119 (if (string-match (caar l
) block-name
)
120 (setq name
(cdar l
) l nil
)
123 ;; As this is a surrogate pair range, ignore it.
125 (setcar val
(cons first char
))
126 (setcar (cdr val
) name
))))
129 (setcdr tail
(list val
))
130 (setq tail
(cdr tail
))))
132 (setq unidata-list
(cdr table
))))
134 ;; Alist of this form:
135 ;; (PROP INDEX GENERATOR FILENAME)
136 ;; PROP: character property
137 ;; INDEX: index to each element of unidata-list for PROP
138 ;; GENERATOR: function to generate a char-table
139 ;; FILENAME: filename to store the char-table
140 ;; DESCRIBER: function to call to get a description string of property value
142 (defconst unidata-prop-alist
144 1 unidata-gen-table-name
"uni-name.el"
145 "Unicode character name.
146 Property value is a string.")
148 2 unidata-gen-table-symbol
"uni-category.el"
149 "Unicode general category.
150 Property value is one of the following symbols:
151 Lu, Ll, Lt, Lm, Lo, Mn, Mc, Me, Nd, Nl, No, Pc, Pd, Ps, Pe, Pi, Pf, Po,
152 Sm, Sc, Sk, So, Zs, Zl, Zp, Cc, Cf, Cs, Co, Cn"
153 unidata-describe-general-category
)
154 (canonical-combining-class
155 3 unidata-gen-table-integer
"uni-combining.el"
156 "Unicode canonical combining class.
157 Property value is an integer."
158 unidata-describe-canonical-combining-class
)
160 4 unidata-gen-table-symbol
"uni-bidi.el"
162 Property value is one of the following symbols:
163 L, LRE, LRO, R, AL, RLE, RLO, PDF, EN, ES, ET,
164 AN, CS, NSM, BN, B, S, WS, ON"
165 unidata-describe-bidi-class
)
167 5 unidata-gen-table-decomposition
"uni-decomposition.el"
168 "Unicode decomposition mapping.
169 Property value is a list of characters. The first element may be
170 one of these symbols representing compatibility formatting tag:
171 <font>, <noBreak>, <initial>, <medial>, <final>, <isolated>, <circle>,
172 <super>, <sub>, <vertical>, <wide>, <narrow>, <small>, <square>, <fraction>,
174 unidata-describe-decomposition
)
176 6 unidata-gen-table-integer
"uni-decimal.el"
177 "Unicode numeric value (decimal digit).
178 Property value is an integer.")
180 7 unidata-gen-table-integer
"uni-digit.el"
181 "Unicode numeric value (digit).
182 Property value is an integer.")
184 8 unidata-gen-table-symbol
"uni-numeric.el"
185 "Unicode numeric value (numeric).
186 Property value is a symbol.")
188 9 unidata-gen-table-symbol
"uni-mirrored.el"
189 "Unicode bidi mirrored flag.
190 Property value is a symbol `Y' or `N'.")
192 10 unidata-gen-table-name
"uni-old-name.el"
193 "Unicode old names as published in Unicode 1.0.
194 Property value is a string.")
196 11 unidata-gen-table-name
"uni-comment.el"
197 "Unicode ISO 10646 comment.
198 Property value is a string.")
200 12 unidata-gen-table-character
"uni-uppercase.el"
201 "Unicode simple uppercase mapping.
202 Property value is a character."
205 13 unidata-gen-table-character
"uni-lowercase.el"
206 "Unicode simple lowercase mapping.
207 Property value is a character."
210 14 unidata-gen-table-character
"uni-titlecase.el"
211 "Unicode simple titlecase mapping.
212 Property value is a character."
215 ;; Functions to access the above data.
216 (defsubst unidata-prop-index
(prop) (nth 1 (assq prop unidata-prop-alist
)))
217 (defsubst unidata-prop-generator
(prop) (nth 2 (assq prop unidata-prop-alist
)))
218 (defsubst unidata-prop-file
(prop) (nth 3 (assq prop unidata-prop-alist
)))
219 (defsubst unidata-prop-docstring
(prop) (nth 4 (assq prop unidata-prop-alist
)))
220 (defsubst unidata-prop-describer
(prop) (nth 5 (assq prop unidata-prop-alist
)))
225 ;; If the type of character property value is character, and the
226 ;; values of succeeding character codes are usually different, we use
227 ;; a char-table described here to store such values.
229 ;; If succeeding 128 characters has no property, a char-table has the
230 ;; symbol t is for them. Otherwise a char-table has a string of the
231 ;; following format for them.
233 ;; The first character of the string is FIRST-INDEX.
234 ;; The Nth (N > 0) character of the string is a property value of the
235 ;; character (BLOCK-HEAD + FIRST-INDEX + N - 1), where BLOCK-HEAD is
236 ;; the first of the characters in the block.
238 ;; The 4th extra slot of a char-table is nil.
240 (defun unidata-get-character (char val table
)
246 (let* ((len (length val
))
247 (block-head (lsh (lsh char -
7) 7))
248 (vec (make-vector 128 nil
))
249 (first-index (aref val
0)))
250 (dotimes (i (1- len
))
251 (let ((elt (aref val
(1+ i
))))
253 (aset vec
(+ first-index i
) elt
))))
255 (aset table
(+ block-head i
) (aref vec i
)))
256 (aref vec
(- char block-head
))))))
258 (defun unidata-put-character (char val table
)
261 (error "Not a character nor nil: %S" val
))
262 (let ((current-val (aref table char
)))
263 (unless (eq current-val val
)
264 (if (stringp current-val
)
265 (funcall (char-table-extra-slot table
1) char current-val table
))
266 (aset table char val
))))
268 (defun unidata-gen-table-character (prop)
269 (let ((table (make-char-table 'char-code-property-table
))
270 (prop-idx (unidata-prop-index prop
))
271 (vec (make-vector 128 0))
273 elt range val idx slot
)
274 (set-char-table-range table
(cons 0 (max-char)) t
)
276 (setq elt
(car tail
) tail
(cdr tail
))
277 (setq range
(car elt
)
278 val
(nth prop-idx elt
))
279 (if (= (length val
) 0)
281 (setq val
(string-to-number val
16)))
284 (set-char-table-range table range val
))
285 (let* ((start (lsh (lsh range -
7) 7))
286 (limit (+ start
127))
287 first-index last-index
)
290 (aset vec
(setq last-index
(setq first-index
(- range start
)))
292 (while (and (setq elt
(car tail
) range
(car elt
))
295 (setq val
(nth prop-idx elt
))
296 (when (> (length val
) 0)
297 (aset vec
(setq last-index
(- range start
))
298 (string-to-number val
16))
300 (setq first-index last-index
)))
301 (setq tail
(cdr tail
)))
303 (let ((str (string first-index
))
305 (while (<= first-index last-index
)
306 (setq str
(format "%s%c" str
(or (aref vec first-index
) 0))
307 first-index
(1+ first-index
)))
308 (set-char-table-range table
(cons start limit
) str
))))))
310 (set-char-table-extra-slot table
0 prop
)
311 (byte-compile 'unidata-get-character
)
312 (byte-compile 'unidata-put-character
)
313 (set-char-table-extra-slot table
1 (symbol-function 'unidata-get-character
))
314 (set-char-table-extra-slot table
2 (symbol-function 'unidata-put-character
))
322 ;; If the type of character property value is symbol, integer,
323 ;; boolean, or character, we use a char-table described here to store
326 ;; The 4th extra slot is a vector of property values (VAL-TABLE), and
327 ;; values for succeeding 128 characters are encoded into this
328 ;; character sequence:
329 ;; ( VAL-CODE RUN-LENGTH ? ) +
331 ;; VAL-CODE (0..127):
332 ;; (VAL-CODE - 1) is an index into VAL-TABLE.
333 ;; The value 0 means no-value.
334 ;; RUN-LENGTH (130..255):
335 ;; (RUN-LENGTH - 128) specifies how many characters have the same
336 ;; value. If omitted, it means 1.
339 ;; Return a symbol-type character property value of CHAR. VAL is the
340 ;; current value of (aref TABLE CHAR).
342 (defun unidata-get-symbol (char val table
)
343 (let ((val-table (char-table-extra-slot table
4)))
347 (let ((first-char (lsh (lsh char -
7) 7))
352 (set-char-table-range table
(cons first-char
(+ first-char
127))
355 (setq val
(aref str idx
) idx
(1+ idx
)
356 count
(if (< idx len
) (aref str idx
) 1))
357 (setq val
(and (> val
0) (aref val-table
(1- val
)))
358 count
(if (< count
128)
360 (prog1 (- count
128) (setq idx
(1+ idx
)))))
363 (aset table first-char val
))
364 (if (= first-char char
)
366 (setq first-char
(1+ first-char
))))
369 (aref val-table
(1- val
))))))
371 ;; Return a integer-type character property value of CHAR. VAL is the
372 ;; current value of (aref TABLE CHAR).
374 (defun unidata-get-integer (char val table
)
375 (let ((val-table (char-table-extra-slot table
4)))
376 (cond ((integerp val
)
379 (let ((first-char (lsh (lsh char -
7) 7))
385 (setq val
(aref str idx
) idx
(1+ idx
)
386 count
(if (< idx len
) (aref str idx
) 1))
387 (setq val
(and (> val
0) (aref val-table
(1- val
)))
388 count
(if (< count
128)
390 (prog1 (- count
128) (setq idx
(1+ idx
)))))
392 (aset table first-char val
)
393 (if (= first-char char
)
395 (setq first-char
(1+ first-char
))))
398 ;; Store VAL (symbol) as a character property value of CHAR in TABLE.
400 (defun unidata-put-symbol (char val table
)
402 (error "Not a symbol: %S" val
))
403 (let ((current-val (aref table char
)))
404 (unless (eq current-val val
)
405 (if (stringp current-val
)
406 (funcall (char-table-extra-slot table
1) char current-val table
))
407 (aset table char val
))))
409 ;; Store VAL (integer) as a character property value of CHAR in TABLE.
411 (defun unidata-put-integer (char val table
)
414 (error "Not an integer nor nil: %S" val
))
415 (let ((current-val (aref table char
)))
416 (unless (eq current-val val
)
417 (if (stringp current-val
)
418 (funcall (char-table-extra-slot table
1) char current-val table
))
419 (aset table char val
))))
421 ;; Encode the character property value VAL into an integer value by
422 ;; VAL-LIST. By side effect, VAL-LIST is modified.
423 ;; VAL-LIST has this form:
424 ;; (t (VAL1 . VAL-CODE1) (VAL2 . VAL-CODE2) ...)
425 ;; If VAL is one of VALn, just return VAL-CODEn. Otherwise,
426 ;; VAL-LIST is modified to this:
427 ;; (t (VAL . (1+ VAL-CODE1)) (VAL1 . VAL-CODE1) (VAL2 . VAL-CODE2) ...)
429 (defun unidata-encode-val (val-list val
)
430 (let ((slot (assq val val-list
))
434 (setq val-code
(if (cdr val-list
) (1+ (cdr (nth 1 val-list
))) 1))
435 (setcdr val-list
(cons (cons val val-code
) (cdr val-list
)))
438 ;; Generate a char-table for the character property PROP.
440 (defun unidata-gen-table (prop val-func default-value
)
441 (let ((table (make-char-table 'char-code-property-table
))
442 (prop-idx (unidata-prop-index prop
))
444 (vec (make-vector 128 0))
445 tail elt range val val-code idx slot
)
446 (set-char-table-range table
(cons 0 (max-char)) default-value
)
447 (setq tail unidata-list
)
449 (setq elt
(car tail
) tail
(cdr tail
))
450 (setq range
(car elt
)
451 val
(funcall val-func
(nth prop-idx elt
)))
452 (setq val-code
(if val
(unidata-encode-val val-list val
)))
455 (set-char-table-range table range val
))
456 (let* ((start (lsh (lsh range -
7) 7))
457 (limit (+ start
127))
461 (aset vec
(- range start
) val-code
))
462 (while (and (setq elt
(car tail
) range
(car elt
))
465 (setq new-val
(funcall val-func
(nth prop-idx elt
)))
466 (if (not (eq val new-val
))
468 val-code
(if val
(unidata-encode-val val-list val
))))
470 (aset vec
(- range start
) val-code
))
471 (setq tail
(cdr tail
)))
472 (setq str
"" val-code -
1 count
0)
475 (setq count
(1+ count
))
477 (setq str
(concat str
(string val-code
480 (setq str
(concat str
(string val-code val-code
)))
482 (setq str
(concat str
(string val-code
))))))
483 (setq val-code x count
1)))
487 (set-char-table-range table
(cons start limit
) val
))
489 (set-char-table-range table
(cons start limit
) str
)
491 (setq str
(concat str
(string val-code
(+ count
128))))
493 (setq str
(concat str
(string val-code val-code
)))
494 (setq str
(concat str
(string val-code
)))))
495 (set-char-table-range table
(cons start limit
) str
))))))
497 (setq val-list
(nreverse (cdr val-list
)))
498 (set-char-table-extra-slot table
0 prop
)
499 (set-char-table-extra-slot table
4 (vconcat (mapcar 'car val-list
)))
502 (defun unidata-gen-table-symbol (prop)
503 (let ((table (unidata-gen-table prop
504 #'(lambda (x) (and (> (length x
) 0)
507 (byte-compile 'unidata-get-symbol
)
508 (byte-compile 'unidata-put-symbol
)
509 (set-char-table-extra-slot table
1 (symbol-function 'unidata-get-symbol
))
510 (set-char-table-extra-slot table
2 (symbol-function 'unidata-put-symbol
))
513 (defun unidata-gen-table-integer (prop)
514 (let ((table (unidata-gen-table prop
515 #'(lambda (x) (and (> (length x
) 0)
516 (string-to-number x
)))
518 (byte-compile 'unidata-get-integer
)
519 (byte-compile 'unidata-put-integer
)
520 (set-char-table-extra-slot table
1 (symbol-function 'unidata-get-integer
))
521 (set-char-table-extra-slot table
2 (symbol-function 'unidata-put-integer
))
527 ;; If the table is for `name' property, each character in the string
529 ;; DIFF-HEAD-CODE (0, 1, or 2):
530 ;; specifies how to decode the following characters.
531 ;; WORD-CODE (3..#x7FF excluding '-', '0'..'9', 'A'..'Z'):
532 ;; specifies an index number into WORD-TABLE (see below)
533 ;; Otherwise (' ', '-', '0'..'9', 'A'..'Z'):
534 ;; specifies a literal word.
536 ;; The 4th slots is a vector:
537 ;; [ WORD-TABLE BLOCK-NAME HANGUL-JAMO-TABLE ]
538 ;; WORD-TABLE is a vector of word symbols.
539 ;; BLOCK-NAME is a vector of name symbols for a block of characters.
540 ;; HANGUL-JAMO-TABLE is `unidata-name-jamo-name-table'.
542 ;; Return the difference of symbol list L1 and L2 in this form:
543 ;; (DIFF-HEAD SYM1 SYM2 ...)
544 ;; DIFF-HEAD is ((SAME-HEAD-LENGTH * 16) + SAME-TAIL-LENGTH).
545 ;; Ex: If L1 is (a b c d e f) and L2 is (a g h e f), this function
546 ;; returns ((+ (* 1 16) 2) g h).
547 ;; It means that we can get L2 from L1 by prepending the first element
548 ;; of L1 and appending the last 2 elements of L1 to the list (g h).
549 ;; If L1 and L2 don't have common elements at the head and tail,
550 ;; set DIFF-HEAD to -1 and SYM1 ... to the elements of L2.
552 (defun unidata-word-list-diff (l1 l2
)
559 (while (and l1
(eq (car l1
) (car l2
)))
561 l1
(cdr l1
) len1
(1- len1
) l2
(cdr l2
) len2
(1- len2
)))
562 (while (and (< end len1
) (< end len2
)
563 (eq (nth (- len1 end
1) l1
) (nth (- len2 end
1) l2
)))
564 (setq end
(1+ end
))))
565 (if (= (+ beg end
) 0)
566 (setq result
(list -
1))
567 (setq result
(list (+ (* beg
16) (+ beg
(- len1 end
))))))
569 (setcdr result
(cons (nth (- len2 end
1) l2
) (cdr result
)))
573 ;; Return a compressed form of the vector VEC. Each element of VEC is
574 ;; a list of symbols of which names can be concatenated to form a
575 ;; character name. This function changes those elements into
576 ;; compressed forms by utilizing the fact that diff of consecutive
577 ;; elements is usually small.
579 (defun unidata-word-list-compress (vec)
580 (let (last-elt last-idx diff-head tail elt val
)
582 (setq elt
(aref vec i
))
587 (setq val
(unidata-word-list-diff last-elt elt
))
590 val
(cons 0 (cdr val
)))
591 (if (eq diff-head
(car val
))
592 (setq val
(cons 2 (cdr val
)))
593 (setq diff-head
(car val
))
595 (setq val
(cons 1 val
))))))
597 (setq last-idx i last-elt elt
)))
601 (let ((shorter (make-vector (1+ last-idx
) nil
)))
602 (dotimes (i (1+ last-idx
))
603 (aset shorter i
(aref vec i
)))
604 (setq vec shorter
))))
607 ;; Encode the word index IDX into a characters code that can be
608 ;; embedded in a string.
610 (defsubst unidata-encode-word
(idx)
614 ;; Decode the character code CODE (that is embedded in a string) into
615 ;; the corresponding word name by looking up WORD-TABLE.
617 (defsubst unidata-decode-word
(code word-table
)
618 (setq code
(- code
3))
619 (if (< code
(length word-table
))
620 (aref word-table code
)))
622 ;; Table of short transliterated name symbols of Hangul Jamo divided
623 ;; into Choseong, Jungseong, and Jongseong.
625 (defconst unidata-name-jamo-name-table
626 [[G GG N D DD R M B BB S SS nil J JJ C K T P H
]
627 [A AE YA YAE EO E YEO YE O WA WAE OE YO U WEO WE WI YU EU YI I
]
628 [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
]])
630 ;; Return a name of CHAR. VAL is the current value of (aref TABLE
633 (defun unidata-get-name (char val table
)
636 (if (> (aref val
0) 0)
638 (let* ((first-char (lsh (lsh char -
7) 7))
639 (word-table (aref (char-table-extra-slot table
4) 0))
642 (vec (make-vector 128 nil
))
644 (case-fold-search nil
)
645 c word-list tail-list last-list word diff-head
)
647 (setq c
(aref val i
))
650 (if (or word-list tail-list
)
652 (setq last-list
(nconc word-list tail-list
))))
653 (setq i
(1+ i
) idx
(1+ idx
)
654 word-list nil tail-list nil
)
659 (prog1 (aref val i
) (setq i
(1+ i
)))))
660 (setq tail-list
(nthcdr (% diff-head
16) last-list
))
661 (dotimes (i (/ diff-head
16))
662 (setq word-list
(nconc word-list
(list (car l
)))
667 (unidata-decode-word c word-table
))))
669 (if (or word-list tail-list
)
670 (aset vec idx
(nconc word-list tail-list
)))
673 (setq c
(+ first-char i
))
674 (let ((name (aref vec i
)))
676 (let ((tail (cdr (setq name
(copy-sequence name
))))
679 (setq elt
(car tail
))
680 (or (string= elt
"-")
682 (setcdr tail
(cons elt
(cdr tail
)))
684 (setq tail
(cddr tail
)))
685 (setq name
(apply 'concat name
))))
691 ((and (integerp val
) (> val
0))
692 (let* ((symbol-table (aref (char-table-extra-slot table
4) 1))
693 (sym (aref symbol-table
(1- val
))))
694 (cond ((eq sym
'HANGUL\ SYLLABLE
)
695 (let ((jamo-name-table (aref (char-table-extra-slot table
4) 2)))
696 ;; SIndex = S - SBase
697 (setq char
(- char
#xAC00
))
698 (let ( ;; LIndex = SIndex / NCount
700 ;; VIndex = (SIndex % NCount) * TCount
701 (V (/ (% char
588) 28))
702 ;; TIndex = SIndex % TCount
704 (format "HANGUL SYLLABLE %s%s%s"
705 ;; U+110B is nil in this table.
706 (or (aref (aref jamo-name-table
0) L
) "")
707 (aref (aref jamo-name-table
1) V
)
709 (aref (aref jamo-name-table
2) (1- T
)))))))
710 ((eq sym
'CJK\ IDEOGRAPH
)
711 (format "%s-%04X" sym char
))
712 ((eq sym
'CJK\ COMPATIBILITY\ IDEOGRAPH
)
713 (format "%s-%04X" sym char
))
714 ((eq sym
'VARIATION\ SELECTOR
)
715 (format "%s-%d" sym
(+ (- char
#xe0100
) 17))))))))
717 ;; Store VAL as the name of CHAR in TABLE.
719 (defun unidata-put-name (char val table
)
720 (let ((current-val (aref table char
)))
721 (if (and (stringp current-val
) (= (aref current-val
0) 0))
722 (funcall (char-table-extra-slot table
1) char current-val table
))
723 (aset table char val
)))
725 (defun unidata-get-decomposition (char val table
)
731 (if (> (aref val
0) 0)
733 (let* ((first-char (lsh (lsh char -
7) 7))
734 (word-table (char-table-extra-slot table
4))
737 (vec (make-vector 128 nil
))
739 (case-fold-search nil
)
740 c word-list tail-list last-list word diff-head
)
742 (setq c
(aref val i
))
745 (if (or word-list tail-list
)
747 (setq last-list
(nconc word-list tail-list
))))
748 (setq i
(1+ i
) idx
(1+ idx
)
749 word-list nil tail-list nil
)
754 (prog1 (aref val i
) (setq i
(1+ i
)))))
755 (setq tail-list
(nthcdr (% diff-head
16) last-list
))
756 (dotimes (i (/ diff-head
16))
757 (setq word-list
(nconc word-list
(list (car l
)))
761 (list (or (unidata-decode-word c word-table
) c
)))
763 (if (or word-list tail-list
)
764 (aset vec idx
(nconc word-list tail-list
)))
766 (aset table
(+ first-char i
) (aref vec i
)))
767 (aref vec
(- char first-char
)))))
770 ((and (eq val
0) (>= char
#xAC00
) (<= char
#xD7A3
))
771 ;; SIndex = S (char) - SBase (#xAC00)
772 (setq char
(- char
#xAC00
))
773 (let (;; L = LBase + SIndex / NCount
774 (L (+ #x1100
(/ char
588)))
775 ;; V = VBase + (SIndex % NCount) * TCount
776 (V (+ #x1161
(/ (% char
588) 28)))
777 ;; T = TBase + SIndex % TCount
778 (T (+ #x11A7
(% char
28))))
785 ;; Store VAL as the decomposition information of CHAR in TABLE.
787 (defun unidata-put-decomposition (char val table
)
788 (let ((current-val (aref table char
)))
789 (if (and (stringp current-val
) (= (aref current-val
0) 0))
790 (funcall (char-table-extra-slot table
1) char current-val table
))
791 (aset table char val
)))
793 ;; UnicodeData.txt contains these lines:
794 ;; 0000;<control>;Cc;0;BN;;;;;N;NULL;;;;
796 ;; 0020;SPACE;Zs;0;WS;;;;;N;;;;;
798 ;; The following command yields a file of about 96K bytes.
799 ;; % gawk -F ';' '{print $1,$2;}' < UnicodeData.txt | gzip > temp.gz
800 ;; With the following function, we can get a file of almost the same
803 ;; Generate a char-table for character names.
805 (defun unidata-gen-table-word-list (prop val-func
)
806 (let ((table (make-char-table 'char-code-property-table
))
807 (prop-idx (unidata-prop-index prop
))
808 (word-list (list nil
))
810 block-list block-word-table block-end
811 tail elt range val idx slot
)
812 (set-char-table-range table
(cons 0 (max-char)) 0)
813 (setq tail unidata-list
)
816 (setq elt
(car tail
) tail
(cdr tail
))
817 (setq range
(car elt
)
818 val
(funcall val-func
(nth prop-idx elt
)))
819 ;; Treat the sequence of "CJK COMPATIBILITY IDEOGRAPH-XXXX" and
820 ;; "VARIATION SELECTOR-XXX" as a block.
821 (if (and (consp val
) (eq prop
'name
)
822 (or (and (eq (car val
) 'CJK
)
823 (eq (nth 1 val
) 'COMPATIBILITY
))
824 (and (>= range
#xe0100
)
825 (eq (car val
) 'VARIATION
)
826 (eq (nth 1 val
) 'SELECTOR
))))
827 (let ((first (car val
))
830 (while (and (setq elt
(car tail
) range
(car elt
)
831 val
(funcall val-func
(nth prop-idx elt
)))
834 (eq second
(nth 1 val
)))
835 (setq block-end range
837 (setq range
(cons start block-end
)
838 val
(if (eq first
'CJK
) 'CJK\ COMPATIBILITY\ IDEOGRAPH
839 'VARIATION\ SELECTOR
))))
843 (let ((slot (assq val block-list
)))
844 (setq range
(cons (car range
) (cdr range
)))
845 (setq block-end
(cdr range
))
847 (nconc slot
(list range
))
848 (push (list val range
) block-list
))))
849 (let* ((start (lsh (lsh range -
7) 7))
850 (limit (+ start
127))
852 (vec (make-vector 128 nil
))
854 (if (<= start block-end
)
855 ;; START overlap with the previous block.
856 (aset table range
(nth prop-idx elt
))
858 (aset vec
(- range start
) val
))
859 (while (and (setq elt
(car tail
) range
(car elt
))
862 (setq val
(funcall val-func
(nth prop-idx elt
)))
864 (aset vec
(- range start
) val
))
865 (setq tail
(cdr tail
)))
866 (setq vec
(unidata-word-list-compress vec
))
868 (dotimes (i (length vec
))
869 (dolist (elt (aref vec i
))
871 (let ((slot (assq elt word-list
)))
873 (setcdr slot
(1+ (cdr slot
)))
875 (cons (cons elt
1) (cdr word-list
))))))))
876 (set-char-table-range table
(cons start limit
) vec
))))))
877 (setq word-list
(sort (cdr word-list
)
878 #'(lambda (x y
) (> (cdr x
) (cdr y
)))))
879 (setq tail word-list idx
0)
881 (setcdr (car tail
) (unidata-encode-word idx
))
882 (setq idx
(1+ idx
) tail
(cdr tail
)))
883 (setq word-table
(make-vector (length word-list
) nil
))
885 (dolist (elt word-list
)
886 (aset word-table idx
(car elt
))
889 (if (and (eq prop
'decomposition
)
891 (error "Too many symbols in decomposition data"))
893 (dotimes (i (/ #x110000
128))
894 (let* ((idx (* i
128))
895 (vec (aref table idx
)))
897 (dotimes (i (length vec
))
898 (let ((tail (aref vec i
))
904 code
(if (integerp elt
) elt
905 (cdr (assq elt word-list
))))
906 (setcar tail
(string code
))
907 (setq tail
(cdr tail
)))
908 (aset vec i
(mapconcat 'identity
(aref vec i
) "")))))
909 (set-char-table-range
910 table
(cons idx
(+ idx
127))
911 (mapconcat 'identity vec
"")))))
913 (setq block-word-table
(make-vector (length block-list
) nil
))
915 (dolist (elt block-list
)
916 (dolist (e (cdr elt
))
917 (set-char-table-range table e
(1+ idx
)))
918 (aset block-word-table idx
(car elt
))
921 (set-char-table-extra-slot table
0 prop
)
922 (set-char-table-extra-slot table
4 (cons word-table block-word-table
))
925 (defun unidata-split-name (str)
928 (let ((len (length str
))
935 (setq c
(aref str i
))
937 (setq l
(cons (intern (substring str idx i
)) l
)
939 (if (and (= c ?-
) (< idx i
)
940 (< (1+ i
) len
) (/= (aref str
(1+ i
)) 32))
941 (setq l
(cons '-
(cons (intern (substring str idx i
)) l
))
943 (nreverse (cons (intern (substring str idx
)) l
))))))
945 (defun unidata-gen-table-name (prop)
946 (let* ((table (unidata-gen-table-word-list prop
'unidata-split-name
))
947 (word-tables (char-table-extra-slot table
4)))
948 (byte-compile 'unidata-get-name
)
949 (byte-compile 'unidata-put-name
)
950 (set-char-table-extra-slot table
1 (symbol-function 'unidata-get-name
))
951 (set-char-table-extra-slot table
2 (symbol-function 'unidata-put-name
))
954 (set-char-table-extra-slot table
4
955 (vector (car word-tables
)
957 unidata-name-jamo-name-table
))
958 (set-char-table-extra-slot table
4
959 (vector (car word-tables
))))
962 (defun unidata-split-decomposition (str)
965 (let ((len (length str
))
972 (setq c
(aref str i
))
974 (setq l
(if (= (aref str idx
) ?
<)
975 (cons (intern (substring str idx i
)) l
)
976 (cons (string-to-number (substring str idx i
) 16) l
))
978 (if (= (aref str idx
) ?
<)
979 (setq l
(cons (intern (substring str idx len
)) l
))
980 (setq l
(cons (string-to-number (substring str idx len
) 16) l
)))
984 (defun unidata-gen-table-decomposition (prop)
985 (let* ((table (unidata-gen-table-word-list prop
'unidata-split-decomposition
))
986 (word-tables (char-table-extra-slot table
4)))
987 (byte-compile 'unidata-get-decomposition
)
988 (byte-compile 'unidata-put-decomposition
)
989 (set-char-table-extra-slot table
1
990 (symbol-function 'unidata-get-decomposition
))
991 (set-char-table-extra-slot table
2
992 (symbol-function 'unidata-put-decomposition
))
993 (set-char-table-extra-slot table
4 (car word-tables
))
998 (defun unidata-describe-general-category (val)
1000 '((Lu .
"Letter, Uppercase")
1001 (Ll .
"Letter, Lowercase")
1002 (Lt .
"Letter, Titlecase")
1003 (Lm .
"Letter, Modifier")
1004 (Lo .
"Letter, Other")
1005 (Mn .
"Mark, Nonspacing")
1006 (Mc .
"Mark, Spacing Combining")
1007 (Me .
"Mark, Enclosing")
1008 (Nd .
"Number, Decimal Digit")
1009 (Nl .
"Number, Letter")
1010 (No .
"Number, Other")
1011 (Pc .
"Punctuation, Connector")
1012 (Pd .
"Punctuation, Dash")
1013 (Ps .
"Punctuation, Open")
1014 (Pe .
"Punctuation, Close")
1015 (Pi .
"Punctuation, Initial quote")
1016 (Pf .
"Punctuation, Final quote")
1017 (Po .
"Punctuation, Other")
1018 (Sm .
"Symbol, Math")
1019 (Sc .
"Symbol, Currency")
1020 (Sk .
"Symbol, Modifier")
1021 (So .
"Symbol, Other")
1022 (Zs .
"Separator, Space")
1023 (Zl .
"Separator, Line")
1024 (Zp .
"Separator, Paragraph")
1025 (Cc .
"Other, Control")
1026 (Cf .
"Other, Format")
1027 (Cs .
"Other, Surrogate")
1028 (Co .
"Other, Private Use")
1029 (Cn .
"Other, Not Assigned")))))
1031 (defun unidata-describe-canonical-combining-class (val)
1033 '((0 .
"Spacing, split, enclosing, reordrant, and Tibetan subjoined")
1034 (1 .
"Overlays and interior")
1036 (8 .
"Hiragana/Katakana voicing marks")
1038 (10 .
"Start of fixed position classes")
1039 (199 .
"End of fixed position classes")
1040 (200 .
"Below left attached")
1041 (202 .
"Below attached")
1042 (204 .
"Below right attached")
1043 (208 .
"Left attached (reordrant around single base character)")
1044 (210 .
"Right attached")
1045 (212 .
"Above left attached")
1046 (214 .
"Above attached")
1047 (216 .
"Above right attached")
1048 (218 .
"Below left")
1050 (222 .
"Below right")
1051 (224 .
"Left (reordrant around single base character)")
1053 (228 .
"Above left")
1055 (232 .
"Above right")
1056 (233 .
"Double below")
1057 (234 .
"Double above")
1058 (240 .
"Below (iota subscript)")))))
1060 (defun unidata-describe-bidi-class (val)
1062 '((L .
"Left-to-Right")
1063 (LRE .
"Left-to-Right Embedding")
1064 (LRO .
"Left-to-Right Override")
1065 (R .
"Right-to-Left")
1066 (AL .
"Right-to-Left Arabic")
1067 (RLE .
"Right-to-Left Embedding")
1068 (RLO .
"Right-to-Left Override")
1069 (PDF .
"Pop Directional Format")
1070 (EN .
"European Number")
1071 (ES .
"European Number Separator")
1072 (ET .
"European Number Terminator")
1073 (AN .
"Arabic Number")
1074 (CS .
"Common Number Separator")
1075 (NSM .
"Non-Spacing Mark")
1076 (BN .
"Boundary Neutral")
1077 (B .
"Paragraph Separator")
1078 (S .
"Segment Separator")
1080 (ON .
"Other Neutrals")))))
1082 (defun unidata-describe-decomposition (val)
1083 (mapconcat #'(lambda (x) (if (symbolp x
) (symbol-name x
) (string ?
' x ?
')))
1086 ;; Verify if we can retrieve correct values from the generated
1089 (defun unidata-check ()
1090 (dolist (elt unidata-prop-alist
)
1091 (let* ((prop (car elt
))
1092 (index (unidata-prop-index prop
))
1093 (generator (unidata-prop-generator prop
))
1095 (message "Generating %S table..." prop
)
1096 (funcall generator prop
)))
1097 (decoder (char-table-extra-slot table
1))
1099 (dolist (e unidata-list
)
1100 (let ((char (car e
))
1101 (val1 (nth index e
))
1103 (if (and (stringp val1
) (= (length val1
) 0))
1105 (unless (consp char
)
1106 (setq val2
(funcall decoder char
(aref table char
) table
))
1108 (cond ((eq generator
'unidata-gen-table-symbol
)
1109 (setq val1
(intern val1
)))
1110 ((eq generator
'unidata-gen-table-integer
)
1111 (setq val1
(string-to-number val1
)))
1112 ((eq generator
'unidata-gen-table-character
)
1113 (setq val1
(string-to-number val1
16)))
1114 ((eq generator
'unidata-gen-table-decomposition
)
1115 (setq val1
(unidata-split-decomposition val1
)))))
1116 (when (>= char check
)
1117 (message "%S %04X" prop check
)
1118 (setq check
(+ check
#x400
)))
1119 (or (equal val1 val2
)
1120 (insert (format "> %04X %S\n< %04X %S\n"
1121 char val1 char val2
)))
1124 ;; The entry function. It generates files described in the header
1125 ;; comment of this file.
1127 (defun unidata-gen-files (&optional unidata-text-file
)
1128 (or unidata-text-file
1129 (setq unidata-text-file
(car command-line-args-left
)
1130 command-line-args-left
(cdr command-line-args-left
)))
1131 (unidata-setup-list unidata-text-file
)
1132 (let ((coding-system-for-write 'utf-8-unix
)
1133 (charprop-file "charprop.el"))
1134 (with-temp-file charprop-file
1135 (insert ";; Automatically generated by unidata-gen.el.\n")
1136 (dolist (elt unidata-prop-alist
)
1137 (let* ((prop (car elt
))
1138 (generator (unidata-prop-generator prop
))
1139 (file (unidata-prop-file prop
))
1140 (docstring (unidata-prop-docstring prop
))
1141 (describer (unidata-prop-describer prop
))
1143 ;; Filename in this comment line is extracted by sed in
1145 (insert (format ";; FILE: %s\n" file
))
1146 (insert (format "(define-char-code-property '%S %S\n %S)\n"
1147 prop file docstring
))
1148 (with-temp-file file
1149 (message "Generating %s..." file
)
1150 (setq table
(funcall generator prop
))
1152 (unless (subrp (symbol-function describer
))
1153 (byte-compile describer
)
1154 (setq describer
(symbol-function describer
)))
1155 (set-char-table-extra-slot table
3 describer
))
1156 (insert ";; Automatically generated from UnicodeData.txt.\n"
1157 (format "(define-char-code-property '%S %S %S)\n"
1158 prop table docstring
)
1159 ";; Local Variables:\n"
1160 ";; coding: utf-8\n"
1161 ";; no-byte-compile: t\n"
1163 (format ";; %s ends here\n" file
)))))
1164 (message "Writing %s..." charprop-file
)
1165 (insert ";; Local Variables:\n"
1166 ";; coding: utf-8\n"
1167 ";; no-byte-compile: t\n"
1169 (format ";; %s ends here\n" charprop-file
)))))
1173 ;; arch-tag: 961c862e-b821-447e-9b8a-bfbab9c2d525
1174 ;;; unidata-gen.el ends here