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 DEFAULT 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. It may have the form
159 ;; (VAL0 (FROM1 TO1 VAL1) ...) which indicates that the default
160 ;; value is VAL0 except for characters in the ranges specified by
161 ;; FROMn and TOn (inclusive). The default value of characters
162 ;; between FROMn and TOn is VALn.
163 ;; VAL-LIST: list of specially ordered property values
165 (defconst unidata-prop-alist
167 1 unidata-gen-table-name
"uni-name.el"
168 "Unicode character name.
169 Property value is a string or nil.
170 The value nil stands for the default value \"null string\")."
174 2 unidata-gen-table-symbol
"uni-category.el"
175 "Unicode general category.
176 Property value is one of the following symbols:
177 Lu, Ll, Lt, Lm, Lo, Mn, Mc, Me, Nd, Nl, No, Pc, Pd, Ps, Pe, Pi, Pf, Po,
178 Sm, Sc, Sk, So, Zs, Zl, Zp, Cc, Cf, Cs, Co, Cn"
179 unidata-describe-general-category
181 ;; The order of elements must be in sync with unicode_category_t
182 ;; in src/character.h.
183 (Lu Ll Lt Lm Lo Mn Mc Me Nd Nl No Pc Pd Ps Pe Pi Pf Po
184 Sm Sc Sk So Zs Zl Zp Cc Cf Cs Co Cn
))
185 (canonical-combining-class
186 3 unidata-gen-table-integer
"uni-combining.el"
187 "Unicode canonical combining class.
188 Property value is an integer."
189 unidata-describe-canonical-combining-class
192 4 unidata-gen-table-symbol
"uni-bidi.el"
194 Property value is one of the following symbols:
195 L, LRE, LRO, R, AL, RLE, RLO, PDF, EN, ES, ET,
196 AN, CS, NSM, BN, B, S, WS, ON"
197 unidata-describe-bidi-class
198 ;; The assignment of default values to blocks of code points
199 ;; follows the file DerivedBidiClass.txt from the Unicode
200 ;; Character Database (UCD).
201 (L (#x0600
#x06FF AL
) (#xFB50
#xFDFF AL
) (#xFE70
#xFEFF AL
)
202 (#x0590
#x05FF R
) (#x07C0
#x08FF R
)
203 (#xFB1D
#xFB4F R
) (#x10800
#x10FFF R
) (#x1E800
#x1EFFF R
))
204 ;; The order of elements must be in sync with bidi_type_t in
206 (L R EN AN BN B AL LRE LRO RLE RLO PDF ES ET CS NSM S WS ON
))
208 5 unidata-gen-table-decomposition
"uni-decomposition.el"
209 "Unicode decomposition mapping.
210 Property value is a list of characters. The first element may be
211 one of these symbols representing compatibility formatting tag:
212 font, noBreak, initial, medial, final, isolated, circle, super,
213 sub, vertical, wide, narrow, small, square, fraction, compat"
214 unidata-describe-decomposition
)
216 6 unidata-gen-table-integer
"uni-decimal.el"
217 "Unicode numeric value (decimal digit).
218 Property value is an integer 0..9, or nil.
219 The value nil stands for NaN \"Numeric_Value\".")
221 7 unidata-gen-table-integer
"uni-digit.el"
222 "Unicode numeric value (digit).
223 Property value is an integer 0..9, or nil.
224 The value nil stands for NaN \"Numeric_Value\".")
226 8 unidata-gen-table-numeric
"uni-numeric.el"
227 "Unicode numeric value (numeric).
228 Property value is an integer, a floating point, or nil.
229 The value nil stands for NaN \"Numeric_Value\".")
231 9 unidata-gen-table-symbol
"uni-mirrored.el"
232 "Unicode bidi mirrored flag.
233 Property value is a symbol `Y' or `N'. See also the property `mirroring'."
237 10 unidata-gen-table-name
"uni-old-name.el"
238 "Unicode old names as published in Unicode 1.0.
239 Property value is a string or nil.
240 The value nil stands for the default value \"null string\").")
242 11 unidata-gen-table-name
"uni-comment.el"
243 "Unicode ISO 10646 comment.
244 Property value is a string.")
246 12 unidata-gen-table-character
"uni-uppercase.el"
247 "Unicode simple uppercase mapping.
248 Property value is a character or nil.
249 The value nil means that the actual property value of a character
250 is the character itself."
253 13 unidata-gen-table-character
"uni-lowercase.el"
254 "Unicode simple lowercase mapping.
255 Property value is a character or nil.
256 The value nil means that the actual property value of a character
257 is the character itself."
260 14 unidata-gen-table-character
"uni-titlecase.el"
261 "Unicode simple titlecase mapping.
262 Property value is a character or nil.
263 The value nil means that the actual property value of a character
264 is the character itself."
267 unidata-gen-mirroring-list unidata-gen-table-character
"uni-mirrored.el"
268 "Unicode bidi-mirroring characters.
269 Property value is a character that has the corresponding mirroring image or nil.
270 The value nil means that the actual property value of a character
271 is the character itself.")))
273 ;; Functions to access the above data.
274 (defsubst unidata-prop-index
(prop) (nth 1 (assq prop unidata-prop-alist
)))
275 (defsubst unidata-prop-generator
(prop) (nth 2 (assq prop unidata-prop-alist
)))
276 (defsubst unidata-prop-file
(prop) (nth 3 (assq prop unidata-prop-alist
)))
277 (defsubst unidata-prop-docstring
(prop) (nth 4 (assq prop unidata-prop-alist
)))
278 (defsubst unidata-prop-describer
(prop) (nth 5 (assq prop unidata-prop-alist
)))
279 (defsubst unidata-prop-default
(prop) (nth 6 (assq prop unidata-prop-alist
)))
280 (defsubst unidata-prop-val-list
(prop) (nth 7 (assq prop unidata-prop-alist
)))
285 ;; If the type of character property value is character, and the
286 ;; values of succeeding character codes are usually different, we use
287 ;; a char-table described here to store such values.
289 ;; A char-table divides character code space (#x0..#x3FFFFF) into
290 ;; #x8000 blocks (each block contains 128 characters).
292 ;; If all characters of a block have no property, a char-table has the
293 ;; symbol nil for that block. Otherwise a char-table has a string of
294 ;; the following format for it.
296 ;; The first character of the string is ?\001.
297 ;; The second character of the string is FIRST-INDEX.
298 ;; The Nth (N > 1) character of the string is a property value of the
299 ;; character (BLOCK-HEAD + FIRST-INDEX + N - 2), where BLOCK-HEAD is
300 ;; the first character of the block.
302 ;; This kind of char-table has these extra slots:
303 ;; 1st: the property symbol
305 ;; 3rd: 0 (corresponding to uniprop_encode_character in chartab.c)
308 (defun unidata-gen-table-character (prop &rest ignore
)
309 (let ((table (make-char-table 'char-code-property-table
))
310 (prop-idx (unidata-prop-index prop
))
311 (vec (make-vector 128 0))
313 elt range val idx slot
)
314 (if (functionp prop-idx
)
315 (setq tail
(funcall prop-idx
)
318 (setq elt
(car tail
) tail
(cdr tail
))
319 (setq range
(car elt
)
320 val
(nth prop-idx elt
))
321 (if (= (length val
) 0)
323 (setq val
(string-to-number val
16)))
326 (set-char-table-range table range val
))
327 (let* ((start (lsh (lsh range -
7) 7))
328 (limit (+ start
127))
329 first-index last-index
)
332 (aset vec
(setq last-index
(setq first-index
(- range start
)))
334 (while (and (setq elt
(car tail
) range
(car elt
))
337 (setq val
(nth prop-idx elt
))
338 (when (> (length val
) 0)
339 (aset vec
(setq last-index
(- range start
))
340 (string-to-number val
16))
342 (setq first-index last-index
)))
343 (setq tail
(cdr tail
)))
345 (let ((str (string 1 first-index
))
347 (while (<= first-index last-index
)
348 (setq str
(format "%s%c" str
(or (aref vec first-index
) 0))
349 first-index
(1+ first-index
)))
350 (set-char-table-range table
(cons start limit
) str
))))))
352 (set-char-table-extra-slot table
0 prop
)
353 (set-char-table-extra-slot table
2 0)
360 ;; If many characters of successive character codes have the same
361 ;; property value, we use a char-table described here to store the
364 ;; At first, instead of a value itself, we store an index number to
365 ;; the VAL-TABLE (5th extra slot) in the table. We call that index
366 ;; number as VAL-CODE here after.
368 ;; A char-table divides character code space (#x0..#x3FFFFF) into
369 ;; #x8000 blocks (each block contains 128 characters).
371 ;; If all characters of a block have the same value, a char-table has
372 ;; VAL-CODE for that block. Otherwise a char-table has a string of
373 ;; the following format for that block.
375 ;; The first character of the string is ?\002.
376 ;; The following characters has this form:
377 ;; ( VAL-CODE RUN-LENGTH ? ) +
379 ;; VAL-CODE (0..127): index into VAL-TABLE.
380 ;; RUN-LENGTH (130..255):
381 ;; (RUN-LENGTH - 128) specifies how many characters have the same
382 ;; value. If omitted, it means 1.
384 ;; This kind of char-table has these extra slots:
385 ;; 1st: the property symbol
386 ;; 2nd: 0 (corresponding to uniprop_decode_value in chartab.c)
387 ;; 3rd: 1..3 (corresponding to uniprop_encode_xxx in chartab.c)
388 ;; 4th: function or nil
391 ;; Encode the character property value VAL into an integer value by
392 ;; VAL-LIST. By side effect, VAL-LIST is modified.
393 ;; VAL-LIST has this form:
394 ;; ((nil . 0) (VAL1 . 1) (VAL2 . 2) ...)
395 ;; If VAL is one of VALn, just return n.
396 ;; Otherwise, VAL-LIST is modified to this:
397 ;; ((nil . 0) (VAL1 . 1) (VAL2 . 2) ... (VAL . n+1))
399 (defun unidata-encode-val (val-list val
)
400 (let ((slot (assoc val val-list
))
404 (setq val-code
(length val-list
))
405 (nconc val-list
(list (cons val val-code
)))
408 ;; Generate a char-table for the character property PROP.
410 (defun unidata-gen-table (prop val-func default-value val-list
)
411 (let ((table (make-char-table 'char-code-property-table
))
412 (prop-idx (unidata-prop-index prop
))
413 (vec (make-vector 128 0))
414 tail elt range val val-code idx slot
416 (setq val-list
(cons nil
(copy-sequence val-list
)))
417 (setq tail val-list val-code
0)
418 ;; Convert (nil A B ...) to ((nil . 0) (A . 1) (B . 2) ...)
420 (setcar tail
(cons (car tail
) val-code
))
421 (setq tail
(cdr tail
) val-code
(1+ val-code
)))
422 (if (consp default-value
)
423 (setq default-value
(copy-sequence default-value
))
424 (setq default-value
(list default-value
)))
425 (setcar default-value
426 (unidata-encode-val val-list
(car default-value
)))
427 (set-char-table-range table t
(car default-value
))
428 (set-char-table-range table nil
(car default-value
))
429 (dolist (elm (cdr default-value
))
430 (setcar (nthcdr 2 elm
)
431 (unidata-encode-val val-list
(nth 2 elm
)))
432 (set-char-table-range table
(cons (car elm
) (nth 1 elm
)) (nth 2 elm
)))
434 (setq tail unidata-list
)
436 (setq elt
(car tail
) tail
(cdr tail
))
437 (setq range
(car elt
)
438 val
(funcall val-func
(nth prop-idx elt
)))
439 (setq val-code
(if val
(unidata-encode-val val-list val
)))
442 (set-char-table-range table range val-code
)
443 (let ((from (car range
)) (to (cdr range
)))
444 ;; If RANGE doesn't end at the char-table boundary (each
445 ;; 128 characters), we may have to carry over the data
446 ;; for the last several characters (at most 127 chars)
447 ;; to the next loop. In that case, set PREV-RANGE-DATA
448 ;; to ((FROM . TO) . VAL-CODE) where (FROM . TO)
449 ;; specifies the range of characters handled in the next
451 (when (< (logand to
#x7F
) #x7F
)
452 (if (< from
(logand to
#x1FFF80
))
453 (setq from
(logand to
#x1FFF80
)))
454 (setq prev-range-data
(cons (cons from to
) val-code
)))))
455 (let* ((start (lsh (lsh range -
7) 7))
456 (limit (+ start
127))
457 str count new-val from to vcode
)
458 (fillarray vec
(car default-value
))
459 (dolist (elm (cdr default-value
))
460 (setq from
(car elm
) to
(nth 1 elm
))
461 (when (and (<= from limit
)
462 (or (>= from start
) (>= to start
)))
463 (setq from
(max from start
)
467 (aset vec
(- from start
) vcode
)
468 (setq from
(1+ from
)))))
469 ;; See the comment above.
470 (when (and prev-range-data
471 (>= (cdr (car prev-range-data
)) start
))
472 (setq from
(car (car prev-range-data
))
473 to
(cdr (car prev-range-data
))
474 vcode
(cdr prev-range-data
))
476 (aset vec
(- from start
) vcode
)
477 (setq from
(1+ from
))))
478 (setq prev-range-data nil
)
480 (aset vec
(- range start
) val-code
))
481 (while (and (setq elt
(car tail
) range
(car elt
))
484 (setq new-val
(funcall val-func
(nth prop-idx elt
)))
485 (if (not (eq val new-val
))
487 val-code
(if val
(unidata-encode-val val-list val
))))
489 (aset vec
(- range start
) val-code
))
490 (setq tail
(cdr tail
)))
491 (setq str
"\002" val-code -
1 count
0)
494 (setq count
(1+ count
))
496 (setq str
(concat str
(string val-code
499 (setq str
(concat str
(string val-code val-code
)))
501 (setq str
(concat str
(string val-code
))))))
502 (setq val-code x count
1)))
506 (set-char-table-range table
(cons start limit
) val-code
))
508 (set-char-table-range table
(cons start limit
) str
)
510 (setq str
(concat str
(string val-code
(+ count
128))))
512 (setq str
(concat str
(string val-code val-code
)))
513 (setq str
(concat str
(string val-code
)))))
514 (set-char-table-range table
(cons start limit
) str
))))))
516 (set-char-table-extra-slot table
0 prop
)
517 (set-char-table-extra-slot table
4 (vconcat (mapcar 'car val-list
)))
520 (defun unidata-gen-table-symbol (prop default-value val-list
)
521 (let ((table (unidata-gen-table prop
522 #'(lambda (x) (and (> (length x
) 0)
524 default-value val-list
)))
525 (set-char-table-extra-slot table
1 0)
526 (set-char-table-extra-slot table
2 1)
529 (defun unidata-gen-table-integer (prop default-value val-list
)
530 (let ((table (unidata-gen-table prop
531 #'(lambda (x) (and (> (length x
) 0)
532 (string-to-number x
)))
533 default-value val-list
)))
534 (set-char-table-extra-slot table
1 0)
535 (set-char-table-extra-slot table
2 1)
538 (defun unidata-gen-table-numeric (prop default-value val-list
)
539 (let ((table (unidata-gen-table prop
541 (if (string-match "/" x
)
542 (/ (float (string-to-number x
))
544 (substring x
(match-end 0))))
546 (string-to-number x
))))
547 default-value val-list
)))
548 (set-char-table-extra-slot table
1 0)
549 (set-char-table-extra-slot table
2 2)
555 ;; If the table is for `name' property, each character in the string
557 ;; DIFF-HEAD-CODE (0, 1, or 2):
558 ;; specifies how to decode the following characters.
559 ;; WORD-CODE (3..#x7FF excluding '-', '0'..'9', 'A'..'Z'):
560 ;; specifies an index number into WORD-TABLE (see below)
561 ;; Otherwise (' ', '-', '0'..'9', 'A'..'Z'):
562 ;; specifies a literal word.
564 ;; The 4th slots is a vector:
565 ;; [ WORD-TABLE BLOCK-NAME HANGUL-JAMO-TABLE ]
566 ;; WORD-TABLE is a vector of word symbols.
567 ;; BLOCK-NAME is a vector of name symbols for a block of characters.
568 ;; HANGUL-JAMO-TABLE is `unidata-name-jamo-name-table'.
570 ;; Return the difference of symbol list L1 and L2 in this form:
571 ;; (DIFF-HEAD SYM1 SYM2 ...)
572 ;; DIFF-HEAD is ((SAME-HEAD-LENGTH * 16) + SAME-TAIL-LENGTH).
573 ;; Ex: If L1 is (a b c d e f) and L2 is (a g h e f), this function
574 ;; returns ((+ (* 1 16) 2) g h).
575 ;; It means that we can get L2 from L1 by prepending the first element
576 ;; of L1 and appending the last 2 elements of L1 to the list (g h).
577 ;; If L1 and L2 don't have common elements at the head and tail,
578 ;; set DIFF-HEAD to -1 and SYM1 ... to the elements of L2.
580 (defun unidata-word-list-diff (l1 l2
)
587 (while (and l1
(eq (car l1
) (car l2
)))
589 l1
(cdr l1
) len1
(1- len1
) l2
(cdr l2
) len2
(1- len2
)))
590 (while (and (< end len1
) (< end len2
)
591 (eq (nth (- len1 end
1) l1
) (nth (- len2 end
1) l2
)))
592 (setq end
(1+ end
))))
593 (if (= (+ beg end
) 0)
594 (setq result
(list -
1))
595 (setq result
(list (+ (* beg
16) (+ beg
(- len1 end
))))))
597 (setcdr result
(cons (nth (- len2 end
1) l2
) (cdr result
)))
601 ;; Return a compressed form of the vector VEC. Each element of VEC is
602 ;; a list of symbols of which names can be concatenated to form a
603 ;; character name. This function changes those elements into
604 ;; compressed forms by utilizing the fact that diff of consecutive
605 ;; elements is usually small.
607 (defun unidata-word-list-compress (vec)
608 (let (last-elt last-idx diff-head tail elt val
)
610 (setq elt
(aref vec i
))
615 (setq val
(unidata-word-list-diff last-elt elt
))
618 val
(cons 0 (cdr val
)))
619 (if (eq diff-head
(car val
))
620 (setq val
(cons 2 (cdr val
)))
621 (setq diff-head
(car val
))
623 (setq val
(cons 1 val
))))))
625 (setq last-idx i last-elt elt
)))
629 (let ((shorter (make-vector (1+ last-idx
) nil
)))
630 (dotimes (i (1+ last-idx
))
631 (aset shorter i
(aref vec i
)))
632 (setq vec shorter
))))
635 ;; Encode the word index IDX into a characters code that can be
636 ;; embedded in a string.
638 (defsubst unidata-encode-word
(idx)
642 ;; Decode the character code CODE (that is embedded in a string) into
643 ;; the corresponding word name by looking up WORD-TABLE.
645 (defsubst unidata-decode-word
(code word-table
)
646 (setq code
(- code
3))
647 (if (< code
(length word-table
))
648 (aref word-table code
)))
650 ;; Table of short transliterated name symbols of Hangul Jamo divided
651 ;; into Choseong, Jungseong, and Jongseong.
653 (defconst unidata-name-jamo-name-table
654 [[G GG N D DD R M B BB S SS nil J JJ C K T P H
]
655 [A AE YA YAE EO E YEO YE O WA WAE OE YO U WEO WE WI YU EU YI I
]
656 [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
]])
658 ;; Return a name of CHAR. VAL is the current value of (aref TABLE
661 (defun unidata-get-name (char val table
)
664 (if (> (aref val
0) 0)
666 (let* ((first-char (lsh (lsh char -
7) 7))
667 (word-table (aref (char-table-extra-slot table
4) 0))
670 (vec (make-vector 128 nil
))
672 (case-fold-search nil
)
673 c word-list tail-list last-list word diff-head
)
675 (setq c
(aref val i
))
678 (if (or word-list tail-list
)
680 (setq last-list
(nconc word-list tail-list
))))
681 (setq i
(1+ i
) idx
(1+ idx
)
682 word-list nil tail-list nil
)
687 (prog1 (aref val i
) (setq i
(1+ i
)))))
688 (setq tail-list
(nthcdr (% diff-head
16) last-list
))
689 (dotimes (i (/ diff-head
16))
690 (setq word-list
(nconc word-list
(list (car l
)))
695 (unidata-decode-word c word-table
))))
697 (if (or word-list tail-list
)
698 (aset vec idx
(nconc word-list tail-list
)))
701 (setq c
(+ first-char i
))
702 (let ((name (aref vec i
)))
704 (let ((tail (cdr (setq name
(copy-sequence name
))))
707 (setq elt
(car tail
))
708 (or (string= elt
"-")
710 (setcdr tail
(cons elt
(cdr tail
)))
712 (setq tail
(cddr tail
)))
713 (setq name
(apply 'concat name
))))
719 ((and (integerp val
) (> val
0))
720 (let* ((symbol-table (aref (char-table-extra-slot table
4) 1))
721 (sym (aref symbol-table
(1- val
))))
722 (cond ((eq sym
'HANGUL\ SYLLABLE
)
723 (let ((jamo-name-table (aref (char-table-extra-slot table
4) 2)))
724 ;; SIndex = S - SBase
725 (setq char
(- char
#xAC00
))
726 (let ( ;; LIndex = SIndex / NCount
728 ;; VIndex = (SIndex % NCount) * TCount
729 (V (/ (% char
588) 28))
730 ;; TIndex = SIndex % TCount
732 (format "HANGUL SYLLABLE %s%s%s"
733 ;; U+110B is nil in this table.
734 (or (aref (aref jamo-name-table
0) L
) "")
735 (aref (aref jamo-name-table
1) V
)
737 (aref (aref jamo-name-table
2) (1- T
)))))))
738 ((eq sym
'CJK\ IDEOGRAPH
)
739 (format "%s-%04X" sym char
))
740 ((eq sym
'CJK\ COMPATIBILITY\ IDEOGRAPH
)
741 (format "%s-%04X" sym char
))
742 ((eq sym
'VARIATION\ SELECTOR
)
743 (format "%s-%d" sym
(+ (- char
#xe0100
) 17))))))))
745 ;; Store VAL as the name of CHAR in TABLE.
747 (defun unidata-put-name (char val table
)
748 (let ((current-val (aref table char
)))
749 (if (and (stringp current-val
) (= (aref current-val
0) 0))
750 (funcall (char-table-extra-slot table
1) char current-val table
))
751 (aset table char val
)))
753 (defun unidata-get-decomposition (char val table
)
762 (if (> (aref val
0) 0)
764 (let* ((first-char (lsh (lsh char -
7) 7))
765 (word-table (char-table-extra-slot table
4))
768 (vec (make-vector 128 nil
))
770 (case-fold-search nil
)
771 c word-list tail-list last-list word diff-head
)
773 (setq c
(aref val i
))
776 (if (or word-list tail-list
)
778 (setq last-list
(nconc word-list tail-list
))))
779 (setq i
(1+ i
) idx
(1+ idx
)
780 word-list nil tail-list nil
)
785 (prog1 (aref val i
) (setq i
(1+ i
)))))
786 (setq tail-list
(nthcdr (% diff-head
16) last-list
))
787 (dotimes (i (/ diff-head
16))
788 (setq word-list
(nconc word-list
(list (car l
)))
792 (list (or (unidata-decode-word c word-table
) c
)))
794 (if (or word-list tail-list
)
795 (aset vec idx
(nconc word-list tail-list
)))
797 (aset table
(+ first-char i
) (aref vec i
)))
798 (setq val
(aref vec
(- char first-char
)))
799 (or val
(list char
)))))
802 ((and (eq val
0) (>= char
#xAC00
) (<= char
#xD7A3
))
803 ;; SIndex = S (char) - SBase (#xAC00)
804 (setq char
(- char
#xAC00
))
805 (let (;; L = LBase + SIndex / NCount
806 (L (+ #x1100
(/ char
588)))
807 ;; V = VBase + (SIndex % NCount) * TCount
808 (V (+ #x1161
(/ (% char
588) 28)))
809 ;; LV = SBase + (SIndex / TCount) * TCount
810 (LV (+ #xAC00
(* (/ char
28) 28)))
811 ;; T = TBase + SIndex % TCount
812 (T (+ #x11A7
(% char
28))))
819 ;; Store VAL as the decomposition information of CHAR in TABLE.
821 (defun unidata-put-decomposition (char val table
)
822 (let ((current-val (aref table char
)))
823 (if (and (stringp current-val
) (= (aref current-val
0) 0))
824 (funcall (char-table-extra-slot table
1) char current-val table
))
825 (aset table char val
)))
827 ;; UnicodeData.txt contains these lines:
828 ;; 0000;<control>;Cc;0;BN;;;;;N;NULL;;;;
830 ;; 0020;SPACE;Zs;0;WS;;;;;N;;;;;
832 ;; The following command yields a file of about 96K bytes.
833 ;; % gawk -F ';' '{print $1,$2;}' < UnicodeData.txt | gzip > temp.gz
834 ;; With the following function, we can get a file of almost the same
837 ;; Generate a char-table for character names.
839 (defun unidata-gen-table-word-list (prop val-func
)
840 (let ((table (make-char-table 'char-code-property-table
))
841 (prop-idx (unidata-prop-index prop
))
842 (word-list (list nil
))
844 block-list block-word-table block-end
845 tail elt range val idx slot
)
846 (setq tail unidata-list
)
849 (setq elt
(car tail
) tail
(cdr tail
))
850 (setq range
(car elt
)
851 val
(funcall val-func
(nth prop-idx elt
)))
852 ;; Treat the sequence of "CJK COMPATIBILITY IDEOGRAPH-XXXX" and
853 ;; "VARIATION SELECTOR-XXX" as a block.
854 (if (and (consp val
) (eq prop
'name
)
855 (or (and (eq (car val
) 'CJK
)
856 (eq (nth 1 val
) 'COMPATIBILITY
))
857 (and (>= range
#xe0100
)
858 (eq (car val
) 'VARIATION
)
859 (eq (nth 1 val
) 'SELECTOR
))))
860 (let ((first (car val
))
863 (while (and (setq elt
(car tail
) range
(car elt
)
864 val
(funcall val-func
(nth prop-idx elt
)))
867 (eq second
(nth 1 val
)))
868 (setq block-end range
870 (setq range
(cons start block-end
)
871 val
(if (eq first
'CJK
) 'CJK\ COMPATIBILITY\ IDEOGRAPH
872 'VARIATION\ SELECTOR
))))
876 (let ((slot (assq val block-list
)))
877 (setq range
(cons (car range
) (cdr range
)))
878 (setq block-end
(cdr range
))
880 (nconc slot
(list range
))
881 (push (list val range
) block-list
))))
882 (let* ((start (lsh (lsh range -
7) 7))
883 (limit (+ start
127))
885 (vec (make-vector 128 nil
))
887 (if (<= start block-end
)
888 ;; START overlap with the previous block.
889 (aset table range
(nth prop-idx elt
))
891 (aset vec
(- range start
) val
))
892 (while (and (setq elt
(car tail
) range
(car elt
))
895 (setq val
(funcall val-func
(nth prop-idx elt
)))
897 (aset vec
(- range start
) val
))
898 (setq tail
(cdr tail
)))
899 (setq vec
(unidata-word-list-compress vec
))
901 (dotimes (i (length vec
))
902 (dolist (elt (aref vec i
))
904 (let ((slot (assq elt word-list
)))
906 (setcdr slot
(1+ (cdr slot
)))
908 (cons (cons elt
1) (cdr word-list
))))))))
909 (set-char-table-range table
(cons start limit
) vec
))))))
910 (setq word-list
(sort (cdr word-list
)
911 #'(lambda (x y
) (> (cdr x
) (cdr y
)))))
912 (setq tail word-list idx
0)
914 (setcdr (car tail
) (unidata-encode-word idx
))
915 (setq idx
(1+ idx
) tail
(cdr tail
)))
916 (setq word-table
(make-vector (length word-list
) nil
))
918 (dolist (elt word-list
)
919 (aset word-table idx
(car elt
))
922 (if (and (eq prop
'decomposition
)
924 (error "Too many symbols in decomposition data"))
926 (dotimes (i (/ #x110000
128))
927 (let* ((idx (* i
128))
928 (vec (aref table idx
)))
930 (dotimes (i (length vec
))
931 (let ((tail (aref vec i
))
937 code
(if (integerp elt
) elt
938 (cdr (assq elt word-list
))))
939 (setcar tail
(string code
))
940 (setq tail
(cdr tail
)))
941 (aset vec i
(mapconcat 'identity
(aref vec i
) "")))))
942 (set-char-table-range
943 table
(cons idx
(+ idx
127))
944 (mapconcat 'identity vec
"")))))
946 (setq block-word-table
(make-vector (length block-list
) nil
))
948 (dolist (elt block-list
)
949 (dolist (e (cdr elt
))
950 (set-char-table-range table e
(1+ idx
)))
951 (aset block-word-table idx
(car elt
))
954 (set-char-table-extra-slot table
0 prop
)
955 (set-char-table-extra-slot table
4 (cons word-table block-word-table
))
958 (defun unidata-split-name (str)
961 (let ((len (length str
))
968 (setq c
(aref str i
))
970 (setq l
(cons (intern (substring str idx i
)) l
)
972 (if (and (= c ?-
) (< idx i
)
973 (< (1+ i
) len
) (/= (aref str
(1+ i
)) 32))
974 (setq l
(cons '-
(cons (intern (substring str idx i
)) l
))
976 (nreverse (cons (intern (substring str idx
)) l
))))))
978 (defun unidata-gen-table-name (prop &rest ignore
)
979 (let* ((table (unidata-gen-table-word-list prop
'unidata-split-name
))
980 (word-tables (char-table-extra-slot table
4)))
981 (byte-compile 'unidata-get-name
)
982 (byte-compile 'unidata-put-name
)
983 (set-char-table-extra-slot table
1 (symbol-function 'unidata-get-name
))
984 (set-char-table-extra-slot table
2 (symbol-function 'unidata-put-name
))
987 (set-char-table-extra-slot table
4
988 (vector (car word-tables
)
990 unidata-name-jamo-name-table
))
991 (set-char-table-extra-slot table
4
992 (vector (car word-tables
))))
995 (defun unidata-split-decomposition (str)
998 (let ((len (length str
))
1005 (setq c
(aref str i
))
1007 (setq l
(if (= (aref str idx
) ?
<)
1008 (cons (intern (substring str
(1+ idx
) (1- i
))) l
)
1009 (cons (string-to-number (substring str idx i
) 16) l
))
1011 (if (= (aref str idx
) ?
<)
1012 (setq l
(cons (intern (substring str
(1+ idx
) (1- len
))) l
))
1013 (setq l
(cons (string-to-number (substring str idx len
) 16) l
)))
1017 (defun unidata-gen-table-decomposition (prop &rest ignore
)
1018 (let* ((table (unidata-gen-table-word-list prop
'unidata-split-decomposition
))
1019 (word-tables (char-table-extra-slot table
4)))
1020 (byte-compile 'unidata-get-decomposition
)
1021 (byte-compile 'unidata-put-decomposition
)
1022 (set-char-table-extra-slot table
1
1023 (symbol-function 'unidata-get-decomposition
))
1024 (set-char-table-extra-slot table
2
1025 (symbol-function 'unidata-put-decomposition
))
1026 (set-char-table-extra-slot table
4 (car word-tables
))
1031 (defun unidata-describe-general-category (val)
1034 (Lu .
"Letter, Uppercase")
1035 (Ll .
"Letter, Lowercase")
1036 (Lt .
"Letter, Titlecase")
1037 (Lm .
"Letter, Modifier")
1038 (Lo .
"Letter, Other")
1039 (Mn .
"Mark, Nonspacing")
1040 (Mc .
"Mark, Spacing Combining")
1041 (Me .
"Mark, Enclosing")
1042 (Nd .
"Number, Decimal Digit")
1043 (Nl .
"Number, Letter")
1044 (No .
"Number, Other")
1045 (Pc .
"Punctuation, Connector")
1046 (Pd .
"Punctuation, Dash")
1047 (Ps .
"Punctuation, Open")
1048 (Pe .
"Punctuation, Close")
1049 (Pi .
"Punctuation, Initial quote")
1050 (Pf .
"Punctuation, Final quote")
1051 (Po .
"Punctuation, Other")
1052 (Sm .
"Symbol, Math")
1053 (Sc .
"Symbol, Currency")
1054 (Sk .
"Symbol, Modifier")
1055 (So .
"Symbol, Other")
1056 (Zs .
"Separator, Space")
1057 (Zl .
"Separator, Line")
1058 (Zp .
"Separator, Paragraph")
1059 (Cc .
"Other, Control")
1060 (Cf .
"Other, Format")
1061 (Cs .
"Other, Surrogate")
1062 (Co .
"Other, Private Use")
1063 (Cn .
"Other, Not Assigned")))))
1065 (defun unidata-describe-canonical-combining-class (val)
1067 '((0 .
"Spacing, split, enclosing, reordrant, and Tibetan subjoined")
1068 (1 .
"Overlays and interior")
1070 (8 .
"Hiragana/Katakana voicing marks")
1072 (10 .
"Start of fixed position classes")
1073 (199 .
"End of fixed position classes")
1074 (200 .
"Below left attached")
1075 (202 .
"Below attached")
1076 (204 .
"Below right attached")
1077 (208 .
"Left attached (reordrant around single base character)")
1078 (210 .
"Right attached")
1079 (212 .
"Above left attached")
1080 (214 .
"Above attached")
1081 (216 .
"Above right attached")
1082 (218 .
"Below left")
1084 (222 .
"Below right")
1085 (224 .
"Left (reordrant around single base character)")
1087 (228 .
"Above left")
1089 (232 .
"Above right")
1090 (233 .
"Double below")
1091 (234 .
"Double above")
1092 (240 .
"Below (iota subscript)")))))
1094 (defun unidata-describe-bidi-class (val)
1096 '((L .
"Left-to-Right")
1097 (LRE .
"Left-to-Right Embedding")
1098 (LRO .
"Left-to-Right Override")
1099 (R .
"Right-to-Left")
1100 (AL .
"Right-to-Left Arabic")
1101 (RLE .
"Right-to-Left Embedding")
1102 (RLO .
"Right-to-Left Override")
1103 (PDF .
"Pop Directional Format")
1104 (EN .
"European Number")
1105 (ES .
"European Number Separator")
1106 (ET .
"European Number Terminator")
1107 (AN .
"Arabic Number")
1108 (CS .
"Common Number Separator")
1109 (NSM .
"Non-Spacing Mark")
1110 (BN .
"Boundary Neutral")
1111 (B .
"Paragraph Separator")
1112 (S .
"Segment Separator")
1114 (ON .
"Other Neutrals")))))
1116 (defun unidata-describe-decomposition (val)
1119 (if (symbolp x
) (symbol-name x
)
1121 (compose-string (string x
) 0 1 (string ?
\t x ?
\t))
1125 (defun unidata-gen-mirroring-list ()
1126 (let ((head (list nil
))
1129 (insert-file-contents (expand-file-name "BidiMirroring.txt" unidata-dir
))
1130 (goto-char (point-min))
1132 (while (re-search-forward "^\\([0-9A-F]+\\);\\s +\\([0-9A-F]+\\)" nil t
)
1133 (let ((char (string-to-number (match-string 1) 16))
1134 (mirror (match-string 2)))
1135 (setq tail
(setcdr tail
(list (list char mirror
)))))))
1138 ;; Verify if we can retrieve correct values from the generated
1141 (defun unidata-check ()
1142 (dolist (elt unidata-prop-alist
)
1143 (let* ((prop (car elt
))
1144 (index (unidata-prop-index prop
))
1145 (generator (unidata-prop-generator prop
))
1147 (message "Generating %S table..." prop
)
1148 (funcall generator prop
)))
1149 (decoder (char-table-extra-slot table
1))
1151 (dolist (e unidata-list
)
1152 (let ((char (car e
))
1153 (val1 (nth index e
))
1155 (if (and (stringp val1
) (= (length val1
) 0))
1157 (unless (consp char
)
1158 (setq val2
(funcall decoder char
(aref table char
) table
))
1160 (cond ((eq generator
'unidata-gen-table-symbol
)
1161 (setq val1
(intern val1
)))
1162 ((eq generator
'unidata-gen-table-integer
)
1163 (setq val1
(string-to-number val1
)))
1164 ((eq generator
'unidata-gen-table-character
)
1165 (setq val1
(string-to-number val1
16)))
1166 ((eq generator
'unidata-gen-table-decomposition
)
1167 (setq val1
(unidata-split-decomposition val1
)))))
1168 (when (>= char check
)
1169 (message "%S %04X" prop check
)
1170 (setq check
(+ check
#x400
)))
1171 (or (equal val1 val2
)
1172 (insert (format "> %04X %S\n< %04X %S\n"
1173 char val1 char val2
)))
1176 ;; The entry function. It generates files described in the header
1177 ;; comment of this file.
1179 (defun unidata-gen-files (&optional data-dir unidata-text-file
)
1181 (setq data-dir
(car command-line-args-left
)
1182 command-line-args-left
(cdr command-line-args-left
)
1183 unidata-text-file
(car command-line-args-left
)
1184 command-line-args-left
(cdr command-line-args-left
)))
1185 (let ((coding-system-for-write 'utf-8-unix
)
1186 (charprop-file "charprop.el")
1187 (unidata-dir data-dir
))
1188 (dolist (elt unidata-prop-alist
)
1189 (let* ((prop (car elt
))
1190 (file (unidata-prop-file prop
)))
1191 (if (file-exists-p file
)
1192 (delete-file file
))))
1193 (unidata-setup-list unidata-text-file
)
1194 (with-temp-file charprop-file
1195 (insert ";; Automatically generated by unidata-gen.el.\n")
1196 (dolist (elt unidata-prop-alist
)
1197 (let* ((prop (car elt
))
1198 (generator (unidata-prop-generator prop
))
1199 (file (unidata-prop-file prop
))
1200 (docstring (unidata-prop-docstring prop
))
1201 (describer (unidata-prop-describer prop
))
1202 (default-value (unidata-prop-default prop
))
1203 (val-list (unidata-prop-val-list prop
))
1205 ;; Filename in this comment line is extracted by sed in
1207 (insert (format ";; FILE: %s\n" file
))
1208 (insert (format "(define-char-code-property '%S %S\n %S)\n"
1209 prop file docstring
))
1211 (message "Generating %s..." file
)
1212 (when (file-exists-p file
)
1213 (insert-file-contents file
)
1214 (goto-char (point-max))
1215 (search-backward ";; Local Variables:"))
1216 (setq table
(funcall generator prop default-value val-list
))
1218 (unless (subrp (symbol-function describer
))
1219 (byte-compile describer
)
1220 (setq describer
(symbol-function describer
)))
1221 (set-char-table-extra-slot table
3 describer
))
1223 (insert ";; Copyright (C) 1991-2009 Unicode, Inc.
1224 ;; This file was generated from the Unicode data files at
1225 ;; http://www.unicode.org/Public/UNIDATA/.
1226 ;; See lisp/international/README for the copyright and permission notice.\n"))
1227 (insert (format "(define-char-code-property '%S %S %S)\n"
1228 prop table docstring
))
1230 (insert ";; Local Variables:\n"
1231 ";; coding: utf-8\n"
1232 ";; no-byte-compile: t\n"
1234 (format ";; %s ends here\n" file
)))
1236 (message "Generating %s...done" file
))))
1237 (message "Writing %s..." charprop-file
)
1238 (insert ";; Local Variables:\n"
1239 ";; coding: utf-8\n"
1240 ";; no-byte-compile: t\n"
1242 (format ";; %s ends here\n" charprop-file
)))))
1246 ;;; unidata-gen.el ends here