1 ;; unidata-gen.el -- Create files containing character property data.
3 ;; Copyright 2008-2013 (C) Free Software Foundation, Inc.
5 ;; Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011
6 ;; National Institute of Advanced Industrial Science and Technology (AIST)
7 ;; Registration Number H13PRO009
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This file must be byte-compilable/loadable by `temacs' and also
29 ;; the entry function `unidata-gen-files' must be runnable by `temacs'.
31 ;; FILES TO BE GENERATED
33 ;; The entry function `unidata-gen-files' generates these files in
34 ;; in directory specified by its dest-dir argument.
37 ;; It contains a series of forms of this format:
38 ;; (define-char-code-property PROP FILE)
39 ;; where PROP is a symbol representing a character property
40 ;; (name, general-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 contain one or more forms of this format:
48 ;; (define-char-code-property 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 Lisp functions
55 ;; `get-char-code-property' and `put-char-code-property', and C
56 ;; function uniprop_table are called.
58 ;; FORMAT OF A CHAR TABLE
60 ;; We want to make a file size containing a char-table small. We
61 ;; also want to load the file and get a property value fast. We
62 ;; also want to reduce the used memory after loading it. So,
63 ;; instead of naively storing a property value for each character in
64 ;; a char-table (and write it out into a file), we store compressed
65 ;; data in a char-table as below.
67 ;; If succeeding 128*N characters have the same property value, we
68 ;; store that value (or the encoded one) for them. Otherwise,
69 ;; compress values (or the encoded ones) for succeeding 128
70 ;; characters into a single string and store it for those
71 ;; characters. The way of compression depends on a property. See
72 ;; the section "SIMPLE TABLE", "RUN-LENGTH TABLE", and "WORD-LIST
75 ;; The char table has five extra slots:
76 ;; 1st: property symbol
77 ;; 2nd: function to call to get a property value,
78 ;; or an index number of C function to decode the value,
79 ;; or nil if the value can be directly got from the table.
80 ;; 3nd: function to call to put a property value,
81 ;; or an index number of C function to encode the value,
82 ;; or nil if the value can be directly stored in the table.
83 ;; 4th: function to call to get a description of a property value, or nil
84 ;; 5th: data referred by the above functions
86 ;; List of elements of this form:
87 ;; (CHAR-or-RANGE PROP1 PROP2 ... PROPn)
88 ;; CHAR-or-RANGE: a character code or a cons of character codes
89 ;; PROPn: string representing the nth property value
91 (defvar unidata-list nil
)
93 ;; Name of the directory containing files of Unicode Character Database.
95 ;; Dynamically bound in unidata-gen-files.
96 (defvar unidata-dir nil
)
98 (defun unidata-setup-list (unidata-text-file)
99 (let* ((table (list nil
))
101 (block-names '(("^<CJK Ideograph" . CJK\ IDEOGRAPH
)
102 ("^<Hangul Syllable" . HANGUL\ SYLLABLE
)
103 ("^<.*Surrogate" . nil
)
104 ("^<.*Private Use" . PRIVATE\ USE
)))
106 (setq unidata-text-file
(expand-file-name unidata-text-file unidata-dir
))
107 (or (file-readable-p unidata-text-file
)
108 (error "File not readable: %s" unidata-text-file
))
110 ;; Insert a file of this format:
111 ;; (CHAR NAME CATEGORY ...)
112 ;; where CHAR is a character code, the following elements are strings
113 ;; representing character properties.
114 (insert-file-contents unidata-text-file
)
115 (goto-char (point-min))
118 (setq val
(read (current-buffer))
122 ;; Check this kind of block.
123 ;; 4E00;<CJK Ideograph, First>;Lo;0;L;;;;;N;;;;;
124 ;; 9FCB;<CJK Ideograph, Last>;Lo;0;L;;;;;N;;;;;
125 (if (and (= (aref name
0) ?
<)
126 (string-match ", First>$" name
))
130 (setq val
(read (current-buffer))
132 block-name
(cadr val
)
135 (if (string-match (caar l
) block-name
)
136 (setq name
(cdar l
) l nil
)
139 ;; As this is a surrogate pair range, ignore it.
141 (setcar val
(cons first char
))
142 (setcar (cdr val
) name
))))
145 (setcdr tail
(list val
))
146 (setq tail
(cdr tail
))))
148 (setq unidata-list
(cdr table
))))
150 ;; Alist of this form:
151 ;; (PROP INDEX GENERATOR FILENAME DOCSTRING DESCRIBER DEFAULT VAL-LIST)
152 ;; PROP: character property
153 ;; INDEX: index to each element of unidata-list for PROP.
154 ;; It may be a function that generates an alist of character codes
155 ;; vs. the corresponding property values.
156 ;; GENERATOR: function to generate a char-table
157 ;; FILENAME: filename to store the char-table
158 ;; DOCSTRING: docstring for the property
159 ;; DESCRIBER: function to call to get a description string of property value
160 ;; DEFAULT: the default value of the property. It may have the form
161 ;; (VAL0 (FROM1 TO1 VAL1) ...) which indicates that the default
162 ;; value is VAL0 except for characters in the ranges specified by
163 ;; FROMn and TOn (inclusive). The default value of characters
164 ;; between FROMn and TOn is VALn.
165 ;; VAL-LIST: list of specially ordered property values
167 (defconst unidata-prop-alist
169 1 unidata-gen-table-name
"uni-name.el"
170 "Unicode character name.
171 Property value is a string or nil.
172 The value nil stands for the default value \"null string\")."
176 2 unidata-gen-table-symbol
"uni-category.el"
177 "Unicode general category.
178 Property value is one of the following symbols:
179 Lu, Ll, Lt, Lm, Lo, Mn, Mc, Me, Nd, Nl, No, Pc, Pd, Ps, Pe, Pi, Pf, Po,
180 Sm, Sc, Sk, So, Zs, Zl, Zp, Cc, Cf, Cs, Co, Cn"
181 unidata-describe-general-category
183 ;; The order of elements must be in sync with unicode_category_t
184 ;; in src/character.h.
185 (Lu Ll Lt Lm Lo Mn Mc Me Nd Nl No Pc Pd Ps Pe Pi Pf Po
186 Sm Sc Sk So Zs Zl Zp Cc Cf Cs Co Cn
))
187 (canonical-combining-class
188 3 unidata-gen-table-integer
"uni-combining.el"
189 "Unicode canonical combining class.
190 Property value is an integer."
191 unidata-describe-canonical-combining-class
194 4 unidata-gen-table-symbol
"uni-bidi.el"
196 Property value is one of the following symbols:
197 L, LRE, LRO, R, AL, RLE, RLO, PDF, EN, ES, ET,
198 AN, CS, NSM, BN, B, S, WS, ON"
199 unidata-describe-bidi-class
200 ;; The assignment of default values to blocks of code points
201 ;; follows the file DerivedBidiClass.txt from the Unicode
202 ;; Character Database (UCD).
203 (L (#x0600
#x06FF AL
) (#xFB50
#xFDFF AL
) (#xFE70
#xFEFF AL
)
204 (#x0590
#x05FF R
) (#x07C0
#x08FF R
)
205 (#xFB1D
#xFB4F R
) (#x10800
#x10FFF R
) (#x1E800
#x1EFFF R
))
206 ;; The order of elements must be in sync with bidi_type_t in
208 (L R EN AN BN B AL LRE LRO RLE RLO PDF ES ET CS NSM S WS ON
))
210 5 unidata-gen-table-decomposition
"uni-decomposition.el"
211 "Unicode decomposition mapping.
212 Property value is a list of characters. The first element may be
213 one of these symbols representing compatibility formatting tag:
214 font, noBreak, initial, medial, final, isolated, circle, super,
215 sub, vertical, wide, narrow, small, square, fraction, compat"
216 unidata-describe-decomposition
)
218 6 unidata-gen-table-integer
"uni-decimal.el"
219 "Unicode numeric value (decimal digit).
220 Property value is an integer 0..9, or nil.
221 The value nil stands for NaN \"Numeric_Value\".")
223 7 unidata-gen-table-integer
"uni-digit.el"
224 "Unicode numeric value (digit).
225 Property value is an integer 0..9, or nil.
226 The value nil stands for NaN \"Numeric_Value\".")
228 8 unidata-gen-table-numeric
"uni-numeric.el"
229 "Unicode numeric value (numeric).
230 Property value is an integer, a floating point, or nil.
231 The value nil stands for NaN \"Numeric_Value\".")
233 9 unidata-gen-table-symbol
"uni-mirrored.el"
234 "Unicode bidi mirrored flag.
235 Property value is a symbol `Y' or `N'. See also the property `mirroring'."
239 10 unidata-gen-table-name
"uni-old-name.el"
240 "Unicode old names as published in Unicode 1.0.
241 Property value is a string or nil.
242 The value nil stands for the default value \"null string\").")
244 11 unidata-gen-table-name
"uni-comment.el"
245 "Unicode ISO 10646 comment.
246 Property value is a string.")
248 12 unidata-gen-table-character
"uni-uppercase.el"
249 "Unicode simple uppercase mapping.
250 Property value is a character or nil.
251 The value nil means that the actual property value of a character
252 is the character itself."
255 13 unidata-gen-table-character
"uni-lowercase.el"
256 "Unicode simple lowercase mapping.
257 Property value is a character or nil.
258 The value nil means that the actual property value of a character
259 is the character itself."
262 14 unidata-gen-table-character
"uni-titlecase.el"
263 "Unicode simple titlecase mapping.
264 Property value is a character or nil.
265 The value nil means that the actual property value of a character
266 is the character itself."
269 unidata-gen-mirroring-list unidata-gen-table-character
"uni-mirrored.el"
270 "Unicode bidi-mirroring characters.
271 Property value is a character that has the corresponding mirroring image or nil.
272 The value nil means that the actual property value of a character
273 is the character itself.")))
275 ;; Functions to access the above data.
276 (defsubst unidata-prop-index
(prop) (nth 1 (assq prop unidata-prop-alist
)))
277 (defsubst unidata-prop-generator
(prop) (nth 2 (assq prop unidata-prop-alist
)))
278 (defsubst unidata-prop-file
(prop) (nth 3 (assq prop unidata-prop-alist
)))
279 (defsubst unidata-prop-docstring
(prop) (nth 4 (assq prop unidata-prop-alist
)))
280 (defsubst unidata-prop-describer
(prop) (nth 5 (assq prop unidata-prop-alist
)))
281 (defsubst unidata-prop-default
(prop) (nth 6 (assq prop unidata-prop-alist
)))
282 (defsubst unidata-prop-val-list
(prop) (nth 7 (assq prop unidata-prop-alist
)))
287 ;; If the type of character property value is character, and the
288 ;; values of succeeding character codes are usually different, we use
289 ;; a char-table described here to store such values.
291 ;; A char-table divides character code space (#x0..#x3FFFFF) into
292 ;; #x8000 blocks (each block contains 128 characters).
294 ;; If all characters of a block have no property, a char-table has the
295 ;; symbol nil for that block. Otherwise a char-table has a string of
296 ;; the following format for it.
298 ;; The first character of the string is ?\001.
299 ;; The second character of the string is FIRST-INDEX.
300 ;; The Nth (N > 1) character of the string is a property value of the
301 ;; character (BLOCK-HEAD + FIRST-INDEX + N - 2), where BLOCK-HEAD is
302 ;; the first character of the block.
304 ;; This kind of char-table has these extra slots:
305 ;; 1st: the property symbol
307 ;; 3rd: 0 (corresponding to uniprop_encode_character in chartab.c)
310 (defun unidata-gen-table-character (prop &rest ignore
)
311 (let ((table (make-char-table 'char-code-property-table
))
312 (prop-idx (unidata-prop-index prop
))
313 (vec (make-vector 128 0))
315 elt range val idx slot
)
316 (if (functionp prop-idx
)
317 (setq tail
(funcall prop-idx
)
320 (setq elt
(car tail
) tail
(cdr tail
))
321 (setq range
(car elt
)
322 val
(nth prop-idx elt
))
323 (if (= (length val
) 0)
325 (setq val
(string-to-number val
16)))
328 (set-char-table-range table range val
))
329 (let* ((start (lsh (lsh range -
7) 7))
330 (limit (+ start
127))
331 first-index last-index
)
334 (aset vec
(setq last-index
(setq first-index
(- range start
)))
336 (while (and (setq elt
(car tail
) range
(car elt
))
339 (setq val
(nth prop-idx elt
))
340 (when (> (length val
) 0)
341 (aset vec
(setq last-index
(- range start
))
342 (string-to-number val
16))
344 (setq first-index last-index
)))
345 (setq tail
(cdr tail
)))
347 (let ((str (string 1 first-index
))
349 (while (<= first-index last-index
)
350 (setq str
(format "%s%c" str
(or (aref vec first-index
) 0))
351 first-index
(1+ first-index
)))
352 (set-char-table-range table
(cons start limit
) str
))))))
354 (set-char-table-extra-slot table
0 prop
)
355 (set-char-table-extra-slot table
2 0)
362 ;; If many characters of successive character codes have the same
363 ;; property value, we use a char-table described here to store the
366 ;; At first, instead of a value itself, we store an index number to
367 ;; the VAL-TABLE (5th extra slot) in the table. We call that index
368 ;; number as VAL-CODE here after.
370 ;; A char-table divides character code space (#x0..#x3FFFFF) into
371 ;; #x8000 blocks (each block contains 128 characters).
373 ;; If all characters of a block have the same value, a char-table has
374 ;; VAL-CODE for that block. Otherwise a char-table has a string of
375 ;; the following format for that block.
377 ;; The first character of the string is ?\002.
378 ;; The following characters has this form:
379 ;; ( VAL-CODE RUN-LENGTH ? ) +
381 ;; VAL-CODE (0..127): index into VAL-TABLE.
382 ;; RUN-LENGTH (130..255):
383 ;; (RUN-LENGTH - 128) specifies how many characters have the same
384 ;; value. If omitted, it means 1.
386 ;; This kind of char-table has these extra slots:
387 ;; 1st: the property symbol
388 ;; 2nd: 0 (corresponding to uniprop_decode_value in chartab.c)
389 ;; 3rd: 1..3 (corresponding to uniprop_encode_xxx in chartab.c)
390 ;; 4th: function or nil
393 ;; Encode the character property value VAL into an integer value by
394 ;; VAL-LIST. By side effect, VAL-LIST is modified.
395 ;; VAL-LIST has this form:
396 ;; ((nil . 0) (VAL1 . 1) (VAL2 . 2) ...)
397 ;; If VAL is one of VALn, just return n.
398 ;; Otherwise, VAL-LIST is modified to this:
399 ;; ((nil . 0) (VAL1 . 1) (VAL2 . 2) ... (VAL . n+1))
401 (defun unidata-encode-val (val-list val
)
402 (let ((slot (assoc val val-list
))
406 (setq val-code
(length val-list
))
407 (nconc val-list
(list (cons val val-code
)))
410 ;; Generate a char-table for the character property PROP.
412 (defun unidata-gen-table (prop val-func default-value val-list
)
413 (let ((table (make-char-table 'char-code-property-table
))
414 (prop-idx (unidata-prop-index prop
))
415 (vec (make-vector 128 0))
416 tail elt range val val-code idx slot
418 (setq val-list
(cons nil
(copy-sequence val-list
)))
419 (setq tail val-list val-code
0)
420 ;; Convert (nil A B ...) to ((nil . 0) (A . 1) (B . 2) ...)
422 (setcar tail
(cons (car tail
) val-code
))
423 (setq tail
(cdr tail
) val-code
(1+ val-code
)))
424 (if (consp default-value
)
425 (setq default-value
(copy-sequence default-value
))
426 (setq default-value
(list default-value
)))
427 (setcar default-value
428 (unidata-encode-val val-list
(car default-value
)))
429 (set-char-table-range table t
(car default-value
))
430 (set-char-table-range table nil
(car default-value
))
431 (dolist (elm (cdr default-value
))
432 (setcar (nthcdr 2 elm
)
433 (unidata-encode-val val-list
(nth 2 elm
)))
434 (set-char-table-range table
(cons (car elm
) (nth 1 elm
)) (nth 2 elm
)))
436 (setq tail unidata-list
)
438 (setq elt
(car tail
) tail
(cdr tail
))
439 (setq range
(car elt
)
440 val
(funcall val-func
(nth prop-idx elt
)))
441 (setq val-code
(if val
(unidata-encode-val val-list val
)))
444 (set-char-table-range table range val-code
)
445 (let ((from (car range
)) (to (cdr range
)))
446 ;; If RANGE doesn't end at the char-table boundary (each
447 ;; 128 characters), we may have to carry over the data
448 ;; for the last several characters (at most 127 chars)
449 ;; to the next loop. In that case, set PREV-RANGE-DATA
450 ;; to ((FROM . TO) . VAL-CODE) where (FROM . TO)
451 ;; specifies the range of characters handled in the next
453 (when (< (logand to
#x7F
) #x7F
)
454 (if (< from
(logand to
#x1FFF80
))
455 (setq from
(logand to
#x1FFF80
)))
456 (setq prev-range-data
(cons (cons from to
) val-code
)))))
457 (let* ((start (lsh (lsh range -
7) 7))
458 (limit (+ start
127))
459 str count new-val from to vcode
)
460 (fillarray vec
(car default-value
))
461 (dolist (elm (cdr default-value
))
462 (setq from
(car elm
) to
(nth 1 elm
))
463 (when (and (<= from limit
)
464 (or (>= from start
) (>= to start
)))
465 (setq from
(max from start
)
469 (aset vec
(- from start
) vcode
)
470 (setq from
(1+ from
)))))
471 ;; See the comment above.
472 (when (and prev-range-data
473 (>= (cdr (car prev-range-data
)) start
))
474 (setq from
(car (car prev-range-data
))
475 to
(cdr (car prev-range-data
))
476 vcode
(cdr prev-range-data
))
478 (aset vec
(- from start
) vcode
)
479 (setq from
(1+ from
))))
480 (setq prev-range-data nil
)
482 (aset vec
(- range start
) val-code
))
483 (while (and (setq elt
(car tail
) range
(car elt
))
486 (setq new-val
(funcall val-func
(nth prop-idx elt
)))
487 (if (not (eq val new-val
))
489 val-code
(if val
(unidata-encode-val val-list val
))))
491 (aset vec
(- range start
) val-code
))
492 (setq tail
(cdr tail
)))
493 (setq str
"\002" val-code -
1 count
0)
496 (setq count
(1+ count
))
498 (setq str
(concat str
(string val-code
501 (setq str
(concat str
(string val-code val-code
)))
503 (setq str
(concat str
(string val-code
))))))
504 (setq val-code x count
1)))
508 (set-char-table-range table
(cons start limit
) val-code
))
510 (set-char-table-range table
(cons start limit
) str
)
512 (setq str
(concat str
(string val-code
(+ count
128))))
514 (setq str
(concat str
(string val-code val-code
)))
515 (setq str
(concat str
(string val-code
)))))
516 (set-char-table-range table
(cons start limit
) str
))))))
518 (set-char-table-extra-slot table
0 prop
)
519 (set-char-table-extra-slot table
4 (vconcat (mapcar 'car val-list
)))
522 (defun unidata-gen-table-symbol (prop default-value val-list
)
523 (let ((table (unidata-gen-table prop
524 #'(lambda (x) (and (> (length x
) 0)
526 default-value val-list
)))
527 (set-char-table-extra-slot table
1 0)
528 (set-char-table-extra-slot table
2 1)
531 (defun unidata-gen-table-integer (prop default-value val-list
)
532 (let ((table (unidata-gen-table prop
533 #'(lambda (x) (and (> (length x
) 0)
534 (string-to-number x
)))
535 default-value val-list
)))
536 (set-char-table-extra-slot table
1 0)
537 (set-char-table-extra-slot table
2 1)
540 (defun unidata-gen-table-numeric (prop default-value val-list
)
541 (let ((table (unidata-gen-table prop
543 (if (string-match "/" x
)
544 (/ (float (string-to-number x
))
546 (substring x
(match-end 0))))
548 (string-to-number x
))))
549 default-value val-list
)))
550 (set-char-table-extra-slot table
1 0)
551 (set-char-table-extra-slot table
2 2)
557 ;; If the table is for `name' property, each character in the string
559 ;; DIFF-HEAD-CODE (0, 1, or 2):
560 ;; specifies how to decode the following characters.
561 ;; WORD-CODE (3..#x7FF excluding '-', '0'..'9', 'A'..'Z'):
562 ;; specifies an index number into WORD-TABLE (see below)
563 ;; Otherwise (' ', '-', '0'..'9', 'A'..'Z'):
564 ;; specifies a literal word.
566 ;; The 4th slots is a vector:
567 ;; [ WORD-TABLE BLOCK-NAME HANGUL-JAMO-TABLE ]
568 ;; WORD-TABLE is a vector of word symbols.
569 ;; BLOCK-NAME is a vector of name symbols for a block of characters.
570 ;; HANGUL-JAMO-TABLE is `unidata-name-jamo-name-table'.
572 ;; Return the difference of symbol list L1 and L2 in this form:
573 ;; (DIFF-HEAD SYM1 SYM2 ...)
574 ;; DIFF-HEAD is ((SAME-HEAD-LENGTH * 16) + SAME-TAIL-LENGTH).
575 ;; Ex: If L1 is (a b c d e f) and L2 is (a g h e f), this function
576 ;; returns ((+ (* 1 16) 2) g h).
577 ;; It means that we can get L2 from L1 by prepending the first element
578 ;; of L1 and appending the last 2 elements of L1 to the list (g h).
579 ;; If L1 and L2 don't have common elements at the head and tail,
580 ;; set DIFF-HEAD to -1 and SYM1 ... to the elements of L2.
582 (defun unidata-word-list-diff (l1 l2
)
589 (while (and l1
(eq (car l1
) (car l2
)))
591 l1
(cdr l1
) len1
(1- len1
) l2
(cdr l2
) len2
(1- len2
)))
592 (while (and (< end len1
) (< end len2
)
593 (eq (nth (- len1 end
1) l1
) (nth (- len2 end
1) l2
)))
594 (setq end
(1+ end
))))
595 (if (= (+ beg end
) 0)
596 (setq result
(list -
1))
597 (setq result
(list (+ (* beg
16) (+ beg
(- len1 end
))))))
599 (setcdr result
(cons (nth (- len2 end
1) l2
) (cdr result
)))
603 ;; Return a compressed form of the vector VEC. Each element of VEC is
604 ;; a list of symbols of which names can be concatenated to form a
605 ;; character name. This function changes those elements into
606 ;; compressed forms by utilizing the fact that diff of consecutive
607 ;; elements is usually small.
609 (defun unidata-word-list-compress (vec)
610 (let (last-elt last-idx diff-head tail elt val
)
612 (setq elt
(aref vec i
))
617 (setq val
(unidata-word-list-diff last-elt elt
))
620 val
(cons 0 (cdr val
)))
621 (if (eq diff-head
(car val
))
622 (setq val
(cons 2 (cdr val
)))
623 (setq diff-head
(car val
))
625 (setq val
(cons 1 val
))))))
627 (setq last-idx i last-elt elt
)))
631 (let ((shorter (make-vector (1+ last-idx
) nil
)))
632 (dotimes (i (1+ last-idx
))
633 (aset shorter i
(aref vec i
)))
634 (setq vec shorter
))))
637 ;; Encode the word index IDX into a characters code that can be
638 ;; embedded in a string.
640 (defsubst unidata-encode-word
(idx)
644 ;; Decode the character code CODE (that is embedded in a string) into
645 ;; the corresponding word name by looking up WORD-TABLE.
647 (defsubst unidata-decode-word
(code word-table
)
648 (setq code
(- code
3))
649 (if (< code
(length word-table
))
650 (aref word-table code
)))
652 ;; Table of short transliterated name symbols of Hangul Jamo divided
653 ;; into Choseong, Jungseong, and Jongseong.
655 (defconst unidata-name-jamo-name-table
656 [[G GG N D DD R M B BB S SS nil J JJ C K T P H
]
657 [A AE YA YAE EO E YEO YE O WA WAE OE YO U WEO WE WI YU EU YI I
]
658 [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
]])
660 ;; Return a name of CHAR. VAL is the current value of (aref TABLE
663 (defun unidata-get-name (char val table
)
666 (if (> (aref val
0) 0)
668 (let* ((first-char (lsh (lsh char -
7) 7))
669 (word-table (aref (char-table-extra-slot table
4) 0))
672 (vec (make-vector 128 nil
))
674 (case-fold-search nil
)
675 c word-list tail-list last-list word diff-head
)
677 (setq c
(aref val i
))
680 (if (or word-list tail-list
)
682 (setq last-list
(nconc word-list tail-list
))))
683 (setq i
(1+ i
) idx
(1+ idx
)
684 word-list nil tail-list nil
)
689 (prog1 (aref val i
) (setq i
(1+ i
)))))
690 (setq tail-list
(nthcdr (% diff-head
16) last-list
))
691 (dotimes (i (/ diff-head
16))
692 (setq word-list
(nconc word-list
(list (car l
)))
697 (unidata-decode-word c word-table
))))
699 (if (or word-list tail-list
)
700 (aset vec idx
(nconc word-list tail-list
)))
703 (setq c
(+ first-char i
))
704 (let ((name (aref vec i
)))
706 (let ((tail (cdr (setq name
(copy-sequence name
))))
709 (setq elt
(car tail
))
710 (or (string= elt
"-")
712 (setcdr tail
(cons elt
(cdr tail
)))
714 (setq tail
(cddr tail
)))
715 (setq name
(apply 'concat name
))))
721 ((and (integerp val
) (> val
0))
722 (let* ((symbol-table (aref (char-table-extra-slot table
4) 1))
723 (sym (aref symbol-table
(1- val
))))
724 (cond ((eq sym
'HANGUL\ SYLLABLE
)
725 (let ((jamo-name-table (aref (char-table-extra-slot table
4) 2)))
726 ;; SIndex = S - SBase
727 (setq char
(- char
#xAC00
))
728 (let ( ;; LIndex = SIndex / NCount
730 ;; VIndex = (SIndex % NCount) * TCount
731 (V (/ (% char
588) 28))
732 ;; TIndex = SIndex % TCount
734 (format "HANGUL SYLLABLE %s%s%s"
735 ;; U+110B is nil in this table.
736 (or (aref (aref jamo-name-table
0) L
) "")
737 (aref (aref jamo-name-table
1) V
)
739 (aref (aref jamo-name-table
2) (1- T
)))))))
740 ((eq sym
'CJK\ IDEOGRAPH
)
741 (format "%s-%04X" sym char
))
742 ((eq sym
'CJK\ COMPATIBILITY\ IDEOGRAPH
)
743 (format "%s-%04X" sym char
))
744 ((eq sym
'VARIATION\ SELECTOR
)
745 (format "%s-%d" sym
(+ (- char
#xe0100
) 17))))))))
747 ;; Store VAL as the name of CHAR in TABLE.
749 (defun unidata-put-name (char val table
)
750 (let ((current-val (aref table char
)))
751 (if (and (stringp current-val
) (= (aref current-val
0) 0))
752 (funcall (char-table-extra-slot table
1) char current-val table
))
753 (aset table char val
)))
755 (defun unidata-get-decomposition (char val table
)
764 (if (> (aref val
0) 0)
766 (let* ((first-char (lsh (lsh char -
7) 7))
767 (word-table (char-table-extra-slot table
4))
770 (vec (make-vector 128 nil
))
772 (case-fold-search nil
)
773 c word-list tail-list last-list word diff-head
)
775 (setq c
(aref val i
))
778 (if (or word-list tail-list
)
780 (setq last-list
(nconc word-list tail-list
))))
781 (setq i
(1+ i
) idx
(1+ idx
)
782 word-list nil tail-list nil
)
787 (prog1 (aref val i
) (setq i
(1+ i
)))))
788 (setq tail-list
(nthcdr (% diff-head
16) last-list
))
789 (dotimes (i (/ diff-head
16))
790 (setq word-list
(nconc word-list
(list (car l
)))
794 (list (or (unidata-decode-word c word-table
) c
)))
796 (if (or word-list tail-list
)
797 (aset vec idx
(nconc word-list tail-list
)))
799 (aset table
(+ first-char i
) (aref vec i
)))
800 (setq val
(aref vec
(- char first-char
)))
801 (or val
(list char
)))))
804 ((and (eq val
0) (>= char
#xAC00
) (<= char
#xD7A3
))
805 ;; SIndex = S (char) - SBase (#xAC00)
806 (setq char
(- char
#xAC00
))
807 (let (;; L = LBase + SIndex / NCount
808 (L (+ #x1100
(/ char
588)))
809 ;; V = VBase + (SIndex % NCount) * TCount
810 (V (+ #x1161
(/ (% char
588) 28)))
811 ;; LV = SBase + (SIndex / TCount) * TCount
812 (LV (+ #xAC00
(* (/ char
28) 28)))
813 ;; T = TBase + SIndex % TCount
814 (T (+ #x11A7
(% char
28))))
821 ;; Store VAL as the decomposition information of CHAR in TABLE.
823 (defun unidata-put-decomposition (char val table
)
824 (let ((current-val (aref table char
)))
825 (if (and (stringp current-val
) (= (aref current-val
0) 0))
826 (funcall (char-table-extra-slot table
1) char current-val table
))
827 (aset table char val
)))
829 ;; UnicodeData.txt contains these lines:
830 ;; 0000;<control>;Cc;0;BN;;;;;N;NULL;;;;
832 ;; 0020;SPACE;Zs;0;WS;;;;;N;;;;;
834 ;; The following command yields a file of about 96K bytes.
835 ;; % gawk -F ';' '{print $1,$2;}' < UnicodeData.txt | gzip > temp.gz
836 ;; With the following function, we can get a file of almost the same
839 ;; Generate a char-table for character names.
841 (defun unidata-gen-table-word-list (prop val-func
)
842 (let ((table (make-char-table 'char-code-property-table
))
843 (prop-idx (unidata-prop-index prop
))
844 (word-list (list nil
))
846 block-list block-word-table block-end
847 tail elt range val idx slot
)
848 (setq tail unidata-list
)
851 (setq elt
(car tail
) tail
(cdr tail
))
852 (setq range
(car elt
)
853 val
(funcall val-func
(nth prop-idx elt
)))
854 ;; Treat the sequence of "CJK COMPATIBILITY IDEOGRAPH-XXXX" and
855 ;; "VARIATION SELECTOR-XXX" as a block.
856 (if (and (consp val
) (eq prop
'name
)
857 (or (and (eq (car val
) 'CJK
)
858 (eq (nth 1 val
) 'COMPATIBILITY
))
859 (and (>= range
#xe0100
)
860 (eq (car val
) 'VARIATION
)
861 (eq (nth 1 val
) 'SELECTOR
))))
862 (let ((first (car val
))
865 (while (and (setq elt
(car tail
) range
(car elt
)
866 val
(funcall val-func
(nth prop-idx elt
)))
869 (eq second
(nth 1 val
)))
870 (setq block-end range
872 (setq range
(cons start block-end
)
873 val
(if (eq first
'CJK
) 'CJK\ COMPATIBILITY\ IDEOGRAPH
874 'VARIATION\ SELECTOR
))))
878 (let ((slot (assq val block-list
)))
879 (setq range
(cons (car range
) (cdr range
)))
880 (setq block-end
(cdr range
))
882 (nconc slot
(list range
))
883 (push (list val range
) block-list
))))
884 (let* ((start (lsh (lsh range -
7) 7))
885 (limit (+ start
127))
887 (vec (make-vector 128 nil
))
889 (if (<= start block-end
)
890 ;; START overlap with the previous block.
891 (aset table range
(nth prop-idx elt
))
893 (aset vec
(- range start
) val
))
894 (while (and (setq elt
(car tail
) range
(car elt
))
897 (setq val
(funcall val-func
(nth prop-idx elt
)))
899 (aset vec
(- range start
) val
))
900 (setq tail
(cdr tail
)))
901 (setq vec
(unidata-word-list-compress vec
))
903 (dotimes (i (length vec
))
904 (dolist (elt (aref vec i
))
906 (let ((slot (assq elt word-list
)))
908 (setcdr slot
(1+ (cdr slot
)))
910 (cons (cons elt
1) (cdr word-list
))))))))
911 (set-char-table-range table
(cons start limit
) vec
))))))
912 (setq word-list
(sort (cdr word-list
)
913 #'(lambda (x y
) (> (cdr x
) (cdr y
)))))
914 (setq tail word-list idx
0)
916 (setcdr (car tail
) (unidata-encode-word idx
))
917 (setq idx
(1+ idx
) tail
(cdr tail
)))
918 (setq word-table
(make-vector (length word-list
) nil
))
920 (dolist (elt word-list
)
921 (aset word-table idx
(car elt
))
924 (if (and (eq prop
'decomposition
)
926 (error "Too many symbols in decomposition data"))
928 (dotimes (i (/ #x110000
128))
929 (let* ((idx (* i
128))
930 (vec (aref table idx
)))
932 (dotimes (i (length vec
))
933 (let ((tail (aref vec i
))
939 code
(if (integerp elt
) elt
940 (cdr (assq elt word-list
))))
941 (setcar tail
(string code
))
942 (setq tail
(cdr tail
)))
943 (aset vec i
(mapconcat 'identity
(aref vec i
) "")))))
944 (set-char-table-range
945 table
(cons idx
(+ idx
127))
946 (mapconcat 'identity vec
"")))))
948 (setq block-word-table
(make-vector (length block-list
) nil
))
950 (dolist (elt block-list
)
951 (dolist (e (cdr elt
))
952 (set-char-table-range table e
(1+ idx
)))
953 (aset block-word-table idx
(car elt
))
956 (set-char-table-extra-slot table
0 prop
)
957 (set-char-table-extra-slot table
4 (cons word-table block-word-table
))
960 (defun unidata-split-name (str)
963 (let ((len (length str
))
970 (setq c
(aref str i
))
972 (setq l
(cons (intern (substring str idx i
)) l
)
974 (if (and (= c ?-
) (< idx i
)
975 (< (1+ i
) len
) (/= (aref str
(1+ i
)) 32))
976 (setq l
(cons '-
(cons (intern (substring str idx i
)) l
))
978 (nreverse (cons (intern (substring str idx
)) l
))))))
980 (defun unidata--ensure-compiled (&rest funcs
)
982 (or (byte-code-function-p (symbol-function fun
))
983 (byte-compile fun
))))
985 (defun unidata-gen-table-name (prop &rest ignore
)
986 (let* ((table (unidata-gen-table-word-list prop
'unidata-split-name
))
987 (word-tables (char-table-extra-slot table
4)))
988 (unidata--ensure-compiled 'unidata-get-name
'unidata-put-name
)
989 (set-char-table-extra-slot table
1 (symbol-function 'unidata-get-name
))
990 (set-char-table-extra-slot table
2 (symbol-function 'unidata-put-name
))
993 (set-char-table-extra-slot table
4
994 (vector (car word-tables
)
996 unidata-name-jamo-name-table
))
997 (set-char-table-extra-slot table
4
998 (vector (car word-tables
))))
1001 (defun unidata-split-decomposition (str)
1004 (let ((len (length str
))
1011 (setq c
(aref str i
))
1013 (setq l
(if (= (aref str idx
) ?
<)
1014 (cons (intern (substring str
(1+ idx
) (1- i
))) l
)
1015 (cons (string-to-number (substring str idx i
) 16) l
))
1017 (if (= (aref str idx
) ?
<)
1018 (setq l
(cons (intern (substring str
(1+ idx
) (1- len
))) l
))
1019 (setq l
(cons (string-to-number (substring str idx len
) 16) l
)))
1023 (defun unidata-gen-table-decomposition (prop &rest ignore
)
1024 (let* ((table (unidata-gen-table-word-list prop
'unidata-split-decomposition
))
1025 (word-tables (char-table-extra-slot table
4)))
1026 (unidata--ensure-compiled 'unidata-get-decomposition
1027 'unidata-put-decomposition
)
1028 (set-char-table-extra-slot table
1
1029 (symbol-function 'unidata-get-decomposition
))
1030 (set-char-table-extra-slot table
2
1031 (symbol-function 'unidata-put-decomposition
))
1032 (set-char-table-extra-slot table
4 (car word-tables
))
1037 (defun unidata-describe-general-category (val)
1040 (Lu .
"Letter, Uppercase")
1041 (Ll .
"Letter, Lowercase")
1042 (Lt .
"Letter, Titlecase")
1043 (Lm .
"Letter, Modifier")
1044 (Lo .
"Letter, Other")
1045 (Mn .
"Mark, Nonspacing")
1046 (Mc .
"Mark, Spacing Combining")
1047 (Me .
"Mark, Enclosing")
1048 (Nd .
"Number, Decimal Digit")
1049 (Nl .
"Number, Letter")
1050 (No .
"Number, Other")
1051 (Pc .
"Punctuation, Connector")
1052 (Pd .
"Punctuation, Dash")
1053 (Ps .
"Punctuation, Open")
1054 (Pe .
"Punctuation, Close")
1055 (Pi .
"Punctuation, Initial quote")
1056 (Pf .
"Punctuation, Final quote")
1057 (Po .
"Punctuation, Other")
1058 (Sm .
"Symbol, Math")
1059 (Sc .
"Symbol, Currency")
1060 (Sk .
"Symbol, Modifier")
1061 (So .
"Symbol, Other")
1062 (Zs .
"Separator, Space")
1063 (Zl .
"Separator, Line")
1064 (Zp .
"Separator, Paragraph")
1065 (Cc .
"Other, Control")
1066 (Cf .
"Other, Format")
1067 (Cs .
"Other, Surrogate")
1068 (Co .
"Other, Private Use")
1069 (Cn .
"Other, Not Assigned")))))
1071 (defun unidata-describe-canonical-combining-class (val)
1073 '((0 .
"Spacing, split, enclosing, reordrant, and Tibetan subjoined")
1074 (1 .
"Overlays and interior")
1076 (8 .
"Hiragana/Katakana voicing marks")
1078 (10 .
"Start of fixed position classes")
1079 (199 .
"End of fixed position classes")
1080 (200 .
"Below left attached")
1081 (202 .
"Below attached")
1082 (204 .
"Below right attached")
1083 (208 .
"Left attached (reordrant around single base character)")
1084 (210 .
"Right attached")
1085 (212 .
"Above left attached")
1086 (214 .
"Above attached")
1087 (216 .
"Above right attached")
1088 (218 .
"Below left")
1090 (222 .
"Below right")
1091 (224 .
"Left (reordrant around single base character)")
1093 (228 .
"Above left")
1095 (232 .
"Above right")
1096 (233 .
"Double below")
1097 (234 .
"Double above")
1098 (240 .
"Below (iota subscript)")))))
1100 (defun unidata-describe-bidi-class (val)
1102 '((L .
"Left-to-Right")
1103 (LRE .
"Left-to-Right Embedding")
1104 (LRO .
"Left-to-Right Override")
1105 (R .
"Right-to-Left")
1106 (AL .
"Right-to-Left Arabic")
1107 (RLE .
"Right-to-Left Embedding")
1108 (RLO .
"Right-to-Left Override")
1109 (PDF .
"Pop Directional Format")
1110 (EN .
"European Number")
1111 (ES .
"European Number Separator")
1112 (ET .
"European Number Terminator")
1113 (AN .
"Arabic Number")
1114 (CS .
"Common Number Separator")
1115 (NSM .
"Non-Spacing Mark")
1116 (BN .
"Boundary Neutral")
1117 (B .
"Paragraph Separator")
1118 (S .
"Segment Separator")
1120 (ON .
"Other Neutrals")))))
1122 (defun unidata-describe-decomposition (val)
1125 (if (symbolp x
) (symbol-name x
)
1127 (compose-string (string x
) 0 1 (string ?
\t x ?
\t))
1131 (defun unidata-gen-mirroring-list ()
1132 (let ((head (list nil
))
1135 (insert-file-contents (expand-file-name "BidiMirroring.txt" unidata-dir
))
1136 (goto-char (point-min))
1138 (while (re-search-forward "^\\([0-9A-F]+\\);\\s +\\([0-9A-F]+\\)" nil t
)
1139 (let ((char (string-to-number (match-string 1) 16))
1140 (mirror (match-string 2)))
1141 (setq tail
(setcdr tail
(list (list char mirror
)))))))
1144 ;; Verify if we can retrieve correct values from the generated
1147 (defun unidata-check ()
1148 (dolist (elt unidata-prop-alist
)
1149 (let* ((prop (car elt
))
1150 (index (unidata-prop-index prop
))
1151 (generator (unidata-prop-generator prop
))
1153 (message "Generating %S table..." prop
)
1154 (funcall generator prop
)))
1155 (decoder (char-table-extra-slot table
1))
1157 (dolist (e unidata-list
)
1158 (let ((char (car e
))
1159 (val1 (nth index e
))
1161 (if (and (stringp val1
) (= (length val1
) 0))
1163 (unless (consp char
)
1164 (setq val2
(funcall decoder char
(aref table char
) table
))
1166 (cond ((eq generator
'unidata-gen-table-symbol
)
1167 (setq val1
(intern val1
)))
1168 ((eq generator
'unidata-gen-table-integer
)
1169 (setq val1
(string-to-number val1
)))
1170 ((eq generator
'unidata-gen-table-character
)
1171 (setq val1
(string-to-number val1
16)))
1172 ((eq generator
'unidata-gen-table-decomposition
)
1173 (setq val1
(unidata-split-decomposition val1
)))))
1174 (when (>= char check
)
1175 (message "%S %04X" prop check
)
1176 (setq check
(+ check
#x400
)))
1177 (or (equal val1 val2
)
1178 (insert (format "> %04X %S\n< %04X %S\n"
1179 char val1 char val2
)))
1182 ;; The entry function. It generates files described in the header
1183 ;; comment of this file.
1185 ;; Write files (charprop.el, uni-*.el) to dest-dir (default PWD),
1186 ;; using as input files from data-dir, and
1187 ;; unidata-text-file (default "unidata.txt" in PWD).
1188 (defun unidata-gen-files (&optional data-dir dest-dir unidata-text-file
)
1190 (setq data-dir
(pop command-line-args-left
)
1191 dest-dir
(or (pop command-line-args-left
) default-directory
)
1192 unidata-text-file
(or (pop command-line-args-left
)
1193 (expand-file-name "unidata.txt"))))
1194 (let ((coding-system-for-write 'utf-8-unix
)
1195 (charprop-file (expand-file-name "charprop.el" dest-dir
))
1196 (unidata-dir data-dir
))
1197 (dolist (elt unidata-prop-alist
)
1198 (let* ((prop (car elt
))
1199 (file (expand-file-name (unidata-prop-file prop
) dest-dir
)))
1200 (if (file-exists-p file
)
1201 (delete-file file
))))
1202 (unidata-setup-list unidata-text-file
)
1203 (with-temp-file charprop-file
1204 (insert ";; Automatically generated by unidata-gen.el.\n")
1205 (dolist (elt unidata-prop-alist
)
1206 (let* ((prop (car elt
))
1207 (generator (unidata-prop-generator prop
))
1208 (file (expand-file-name (unidata-prop-file prop
) dest-dir
))
1209 (basename (file-name-nondirectory file
))
1210 (docstring (unidata-prop-docstring prop
))
1211 (describer (unidata-prop-describer prop
))
1212 (default-value (unidata-prop-default prop
))
1213 (val-list (unidata-prop-val-list prop
))
1215 ;; Filename in this comment line is extracted by sed in
1217 (insert (format ";; FILE: %s\n" basename
))
1218 (insert (format "(define-char-code-property '%S %S\n %S)\n"
1219 prop basename docstring
))
1221 (message "Generating %s..." file
)
1222 (when (file-exists-p file
)
1223 (insert-file-contents file
)
1224 (goto-char (point-max))
1225 (search-backward ";; Local Variables:"))
1226 (setq table
(funcall generator prop default-value val-list
))
1228 (unless (subrp (symbol-function describer
))
1229 (unidata--ensure-compiled describer
)
1230 (setq describer
(symbol-function describer
)))
1231 (set-char-table-extra-slot table
3 describer
))
1233 (insert ";; Copyright (C) 1991-2013 Unicode, Inc.
1234 ;; This file was generated from the Unicode data files at
1235 ;; http://www.unicode.org/Public/UNIDATA/.
1236 ;; See lisp/international/README for the copyright and permission notice.\n"))
1237 (insert (format "(define-char-code-property '%S\n %S\n %S)\n"
1238 prop table docstring
))
1240 (insert ";; Local Variables:\n"
1241 ";; coding: utf-8\n"
1242 ";; version-control: never\n"
1243 ";; no-byte-compile: t\n"
1245 (format ";; %s ends here\n" basename
)))
1247 (message "Generating %s...done" file
))))
1248 (message "Writing %s..." charprop-file
)
1249 (insert ";; Local Variables:\n"
1250 ";; coding: utf-8\n"
1251 ";; version-control: never\n"
1252 ";; no-byte-compile: t\n"
1254 (format ";; %s ends here\n"
1255 (file-name-nondirectory charprop-file
))))))
1259 ;;; unidata-gen.el ends here