* src/nsfont.m (ns_otf_to_script): Fix typo.
[bpt/emacs.git] / admin / unidata / unidata-gen.el
CommitLineData
f600cf3a 1;; unidata-gen.el -- Create files containing character property data.
5df4f04c 2;; Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011
f600cf3a
KH
3;; National Institute of Advanced Industrial Science and Technology (AIST)
4;; Registration Number H13PRO009
5
6;; This file is part of GNU Emacs.
7
9ad5de0c 8;; GNU Emacs is free software: you can redistribute it and/or modify
f600cf3a 9;; it under the terms of the GNU General Public License as published by
9ad5de0c
GM
10;; the Free Software Foundation, either version 3 of the License, or
11;; (at your option) any later version.
f600cf3a
KH
12
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.
17
18;; You should have received a copy of the GNU General Public License
9ad5de0c 19;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
f600cf3a
KH
20
21;;; Commentary:
22
1275c1dc
KH
23;; SPECIAL NOTICE
24;;
25;; This file must be byte-compilable/loadable by `temacs' and also
26;; the entry function `unidata-gen-files' must be runnable by
27;; `temacs'.
28
f600cf3a
KH
29;; FILES TO BE GENERATED
30;;
1275c1dc 31;; The entry function `unidata-gen-files' generates these files in
f600cf3a
KH
32;; the current directory.
33;;
34;; charprop.el
35;; It contains a series of forms of this format:
c805dec0 36;; (define-char-code-property PROP FILE)
f600cf3a 37;; where PROP is a symbol representing a character property
c805dec0 38;; (name, general-category, etc), and FILE is a name of one of
f600cf3a
KH
39;; the following files.
40;;
dd7d19d7
JB
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
c805dec0
KH
45;; They contain one or more forms of this format:
46;; (define-char-code-property PROP CHAR-TABLE)
f600cf3a 47;; where PROP is the same as above, and CHAR-TABLE is a
1275c1dc 48;; char-table containing property values in a compressed format.
f600cf3a
KH
49;;
50;; When they are installed in .../lisp/international/, the file
51;; "charprop.el" is preloaded in loadup.el. The other files are
c805dec0
KH
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.
f600cf3a
KH
55;;
56;; FORMAT OF A CHAR TABLE
57;;
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.
64;;
65;; If succeeding 128*N characters have the same property value, we
c805dec0
KH
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
71;; TABLE".
72
73;; The char table has five extra slots:
f600cf3a 74;; 1st: property symbol
c805dec0
KH
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
f600cf3a 82;; 5th: data referred by the above functions
f600cf3a 83
f600cf3a
KH
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
88
3921ea21
KH
89(defvar unidata-list nil)
90
c805dec0
KH
91;; Name of the directory containing files of Unicode Character
92;; Database.
93
94(defvar unidata-dir nil)
95
3921ea21 96(defun unidata-setup-list (unidata-text-file)
f600cf3a
KH
97 (let* ((table (list nil))
98 (tail table)
99 (block-names '(("^<CJK Ideograph" . CJK\ IDEOGRAPH)
100 ("^<Hangul Syllable" . HANGUL\ SYLLABLE)
101 ("^<.*Surrogate" . nil)
102 ("^<.*Private Use" . PRIVATE\ USE)))
103 val char name)
c805dec0 104 (setq unidata-text-file (expand-file-name unidata-text-file unidata-dir))
f600cf3a
KH
105 (or (file-readable-p unidata-text-file)
106 (error "File not readable: %s" unidata-text-file))
107 (with-temp-buffer
149353a4
KH
108 ;; Insert a file of this format:
109 ;; (CHAR NAME CATEGORY ...)
c0943d3d 110 ;; where CHAR is a character code, the following elements are strings
149353a4 111 ;; representing character properties.
1275c1dc 112 (insert-file-contents unidata-text-file)
f600cf3a
KH
113 (goto-char (point-min))
114 (condition-case nil
115 (while t
116 (setq val (read (current-buffer))
117 char (car val)
118 name (cadr val))
119
120 ;; Check this kind of block.
121 ;; 4E00;<CJK Ideograph, First>;Lo;0;L;;;;;N;;;;;
149353a4 122 ;; 9FCB;<CJK Ideograph, Last>;Lo;0;L;;;;;N;;;;;
f600cf3a
KH
123 (if (and (= (aref name 0) ?<)
124 (string-match ", First>$" name))
125 (let ((first char)
126 (l block-names)
127 block-name)
128 (setq val (read (current-buffer))
129 char (car val)
130 block-name (cadr val)
131 name nil)
132 (while l
133 (if (string-match (caar l) block-name)
134 (setq name (cdar l) l nil)
135 (setq l (cdr l))))
136 (if (not name)
137 ;; As this is a surrogate pair range, ignore it.
138 (setq val nil)
139 (setcar val (cons first char))
140 (setcar (cdr val) name))))
141
142 (when val
143 (setcdr tail (list val))
144 (setq tail (cdr tail))))
145 (error nil)))
3921ea21 146 (setq unidata-list (cdr table))))
f600cf3a
KH
147
148;; Alist of this form:
0902a04e 149;; (PROP INDEX GENERATOR FILENAME DOCSTRING DESCRIBER DEFAULT VAL-LIST)
f600cf3a 150;; PROP: character property
c805dec0
KH
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.
f600cf3a
KH
154;; GENERATOR: function to generate a char-table
155;; FILENAME: filename to store the char-table
c805dec0 156;; DOCSTRING: docstring for the property
f600cf3a 157;; DESCRIBER: function to call to get a description string of property value
0902a04e
KH
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 (incusive). The default value of characters
162;; between FROMn and TOn is VALn.
c805dec0 163;; VAL-LIST: list of specially ordered property values
f600cf3a
KH
164
165(defconst unidata-prop-alist
166 '((name
167 1 unidata-gen-table-name "uni-name.el"
168 "Unicode character name.
0902a04e
KH
169Property value is a string."
170 nil
171 "")
f600cf3a
KH
172 (general-category
173 2 unidata-gen-table-symbol "uni-category.el"
174 "Unicode general category.
175Property value is one of the following symbols:
176 Lu, Ll, Lt, Lm, Lo, Mn, Mc, Me, Nd, Nl, No, Pc, Pd, Ps, Pe, Pi, Pf, Po,
177 Sm, Sc, Sk, So, Zs, Zl, Zp, Cc, Cf, Cs, Co, Cn"
c805dec0 178 unidata-describe-general-category
0902a04e 179 Cn
c805dec0
KH
180 ;; The order of elements must be in sync with unicode_category_t
181 ;; in src/character.h.
182 (Lu Ll Lt Lm Lo Mn Mc Me Nd Nl No Pc Pd Ps Pe Pi Pf Po
183 Sm Sc Sk So Zs Zl Zp Cc Cf Cs Co Cn))
f600cf3a
KH
184 (canonical-combining-class
185 3 unidata-gen-table-integer "uni-combining.el"
186 "Unicode canonical combining class.
187Property value is an integer."
0902a04e
KH
188 unidata-describe-canonical-combining-class
189 0)
f600cf3a
KH
190 (bidi-class
191 4 unidata-gen-table-symbol "uni-bidi.el"
192 "Unicode bidi class.
193Property value is one of the following symbols:
194 L, LRE, LRO, R, AL, RLE, RLO, PDF, EN, ES, ET,
195 AN, CS, NSM, BN, B, S, WS, ON"
c805dec0 196 unidata-describe-bidi-class
0902a04e
KH
197 (L (#x0600 #x06FF AL) (#x0590 #x05FF R) (#x07C0 #x08FF R)
198 (#xFB1D #xFB4F R) (#x10800 #x10FFF R) (#x1E800 #x1EFFF R))
c805dec0
KH
199 ;; The order of elements must be in sync with bidi_type_t in
200 ;; src/dispextern.h.
201 (L R EN AN BN B AL LRE LRO RLE RLO PDF ES ET CS NSM S WS ON))
f600cf3a
KH
202 (decomposition
203 5 unidata-gen-table-decomposition "uni-decomposition.el"
204 "Unicode decomposition mapping.
205Property value is a list of characters. The first element may be
1275c1dc 206one of these symbols representing compatibility formatting tag:
f1478738
KH
207 font, noBreak, initial, medial, final, isolated, circle, super,
208 sub, vertical, wide, narrow, small, square, fraction, compat"
f600cf3a
KH
209 unidata-describe-decomposition)
210 (decimal-digit-value
211 6 unidata-gen-table-integer "uni-decimal.el"
212 "Unicode numeric value (decimal digit).
0902a04e
KH
213Property value is an integer 0..9, or nil.
214The value nil stands for NaN \"Numeric_Value\".")
f600cf3a
KH
215 (digit-value
216 7 unidata-gen-table-integer "uni-digit.el"
217 "Unicode numeric value (digit).
0902a04e
KH
218Property value is an integer 0..9, or nil.
219The value nil stands for NaN \"Numeric_Value\".")
f600cf3a 220 (numeric-value
dd787998 221 8 unidata-gen-table-numeric "uni-numeric.el"
f600cf3a 222 "Unicode numeric value (numeric).
0902a04e
KH
223Property value is an integer, a floating point, or nil.
224The value nil stands for NaN \"Numeric_Value\".")
f600cf3a
KH
225 (mirrored
226 9 unidata-gen-table-symbol "uni-mirrored.el"
227 "Unicode bidi mirrored flag.
0902a04e
KH
228Property value is a symbol `Y' or `N'. See also the property `mirroring'."
229 nil
230 N)
f600cf3a
KH
231 (old-name
232 10 unidata-gen-table-name "uni-old-name.el"
233 "Unicode old names as published in Unicode 1.0.
234Property value is a string.")
235 (iso-10646-comment
236 11 unidata-gen-table-name "uni-comment.el"
237 "Unicode ISO 10646 comment.
238Property value is a string.")
239 (uppercase
240 12 unidata-gen-table-character "uni-uppercase.el"
241 "Unicode simple uppercase mapping.
0902a04e
KH
242Property value is a character or nil.
243The value nil means that the actual property value of a character
244is the character itself."
f600cf3a
KH
245 string)
246 (lowercase
247 13 unidata-gen-table-character "uni-lowercase.el"
248 "Unicode simple lowercase mapping.
0902a04e
KH
249Property value is a character or nil.
250The value nil means that the actual property value of a character
251is the character itself."
f600cf3a
KH
252 string)
253 (titlecase
254 14 unidata-gen-table-character "uni-titlecase.el"
255 "Unicode simple titlecase mapping.
0902a04e
KH
256Property value is a character or nil.
257The value nil means that the actual property value of a character
258is the character itself."
c805dec0
KH
259 string)
260 (mirroring
261 unidata-gen-mirroring-list unidata-gen-table-character "uni-mirrored.el"
262 "Unicode bidi-mirroring characters.
0902a04e
KH
263Property value is a character that has the corresponding mirroring image or nil.
264The value nil means that the actual property value of a character
265is the character itself.")))
f600cf3a
KH
266
267;; Functions to access the above data.
268(defsubst unidata-prop-index (prop) (nth 1 (assq prop unidata-prop-alist)))
269(defsubst unidata-prop-generator (prop) (nth 2 (assq prop unidata-prop-alist)))
270(defsubst unidata-prop-file (prop) (nth 3 (assq prop unidata-prop-alist)))
271(defsubst unidata-prop-docstring (prop) (nth 4 (assq prop unidata-prop-alist)))
272(defsubst unidata-prop-describer (prop) (nth 5 (assq prop unidata-prop-alist)))
c805dec0
KH
273(defsubst unidata-prop-default (prop) (nth 6 (assq prop unidata-prop-alist)))
274(defsubst unidata-prop-val-list (prop) (nth 7 (assq prop unidata-prop-alist)))
f600cf3a
KH
275
276\f
277;; SIMPLE TABLE
278;;
279;; If the type of character property value is character, and the
280;; values of succeeding character codes are usually different, we use
281;; a char-table described here to store such values.
282;;
c805dec0
KH
283;; A char-table divides character code space (#x0..#x3FFFFF) into
284;; #x8000 blocks (each block contains 128 characters).
285
286;; If all characters of a block have no property, a char-table has the
287;; symbol nil for that block. Otherwise a char-table has a string of
288;; the following format for it.
f600cf3a 289;;
c805dec0
KH
290;; The first character of the string is ?\001.
291;; The second character of the string is FIRST-INDEX.
292;; The Nth (N > 1) character of the string is a property value of the
293;; character (BLOCK-HEAD + FIRST-INDEX + N - 2), where BLOCK-HEAD is
294;; the first character of the block.
f600cf3a 295;;
c805dec0
KH
296;; This kind of char-table has these extra slots:
297;; 1st: the property symbol
298;; 2nd: nil
299;; 3rd: 0 (corresponding to uniprop_encode_character in chartab.c)
300;; 4th to 5th: nil
f600cf3a 301
c805dec0 302(defun unidata-gen-table-character (prop &rest ignore)
f600cf3a
KH
303 (let ((table (make-char-table 'char-code-property-table))
304 (prop-idx (unidata-prop-index prop))
305 (vec (make-vector 128 0))
306 (tail unidata-list)
307 elt range val idx slot)
c805dec0
KH
308 (if (functionp prop-idx)
309 (setq tail (funcall prop-idx)
310 prop-idx 1))
f600cf3a
KH
311 (while tail
312 (setq elt (car tail) tail (cdr tail))
313 (setq range (car elt)
314 val (nth prop-idx elt))
315 (if (= (length val) 0)
316 (setq val nil)
317 (setq val (string-to-number val 16)))
318 (if (consp range)
319 (if val
320 (set-char-table-range table range val))
321 (let* ((start (lsh (lsh range -7) 7))
322 (limit (+ start 127))
323 first-index last-index)
324 (fillarray vec 0)
325 (if val
326 (aset vec (setq last-index (setq first-index (- range start)))
327 val))
328 (while (and (setq elt (car tail) range (car elt))
329 (integerp range)
330 (<= range limit))
331 (setq val (nth prop-idx elt))
332 (when (> (length val) 0)
333 (aset vec (setq last-index (- range start))
334 (string-to-number val 16))
335 (or first-index
336 (setq first-index last-index)))
337 (setq tail (cdr tail)))
338 (when first-index
c805dec0 339 (let ((str (string 1 first-index))
f600cf3a
KH
340 c)
341 (while (<= first-index last-index)
342 (setq str (format "%s%c" str (or (aref vec first-index) 0))
343 first-index (1+ first-index)))
344 (set-char-table-range table (cons start limit) str))))))
345
346 (set-char-table-extra-slot table 0 prop)
c805dec0 347 (set-char-table-extra-slot table 2 0)
f600cf3a
KH
348 table))
349
350
351\f
352;; RUN-LENGTH TABLE
353;;
c805dec0
KH
354;; If many characters of successive character codes have the same
355;; property value, we use a char-table described here to store the
356;; values.
f600cf3a 357;;
c805dec0
KH
358;; At first, instead of a value itself, we store an index number to
359;; the VAL-TABLE (5th extra slot) in the table. We call that index
360;; number as VAL-CODE here after.
361;;
362;; A char-table divides character code space (#x0..#x3FFFFF) into
363;; #x8000 blocks (each block contains 128 characters).
364;;
365;; If all characters of a block have the same value, a char-table has
366;; VAL-CODE for that block. Otherwise a char-table has a string of
367;; the following format for that block.
368;;
369;; The first character of the string is ?\002.
370;; The following characters has this form:
f600cf3a
KH
371;; ( VAL-CODE RUN-LENGTH ? ) +
372;; where:
c805dec0 373;; VAL-CODE (0..127): index into VAL-TABLE.
f600cf3a
KH
374;; RUN-LENGTH (130..255):
375;; (RUN-LENGTH - 128) specifies how many characters have the same
376;; value. If omitted, it means 1.
c805dec0
KH
377;;
378;; This kind of char-table has these extra slots:
379;; 1st: the property symbol
380;; 2nd: 0 (corresponding to uniprop_decode_value in chartab.c)
381;; 3rd: 1..3 (corresponding to uniprop_encode_xxx in chartab.c)
382;; 4th: function or nil
383;; 5th: VAL-TABLE
dd787998 384
f600cf3a
KH
385;; Encode the character property value VAL into an integer value by
386;; VAL-LIST. By side effect, VAL-LIST is modified.
387;; VAL-LIST has this form:
c805dec0
KH
388;; ((nil . 0) (VAL1 . 1) (VAL2 . 2) ...)
389;; If VAL is one of VALn, just return n.
390;; Otherwise, VAL-LIST is modified to this:
391;; ((nil . 0) (VAL1 . 1) (VAL2 . 2) ... (VAL . n+1))
f600cf3a
KH
392
393(defun unidata-encode-val (val-list val)
dd787998 394 (let ((slot (assoc val val-list))
f600cf3a
KH
395 val-code)
396 (if slot
397 (cdr slot)
c805dec0
KH
398 (setq val-code (length val-list))
399 (nconc val-list (list (cons val val-code)))
f600cf3a
KH
400 val-code)))
401
402;; Generate a char-table for the character property PROP.
403
c805dec0 404(defun unidata-gen-table (prop val-func default-value val-list)
f600cf3a
KH
405 (let ((table (make-char-table 'char-code-property-table))
406 (prop-idx (unidata-prop-index prop))
f600cf3a 407 (vec (make-vector 128 0))
149353a4
KH
408 tail elt range val val-code idx slot
409 prev-range-data)
c805dec0
KH
410 (setq val-list (cons nil (copy-sequence val-list)))
411 (setq tail val-list val-code 0)
412 ;; Convert (nil A B ...) to ((nil . 0) (A . 1) (B . 2) ...)
413 (while tail
414 (setcar tail (cons (car tail) val-code))
415 (setq tail (cdr tail) val-code (1+ val-code)))
0902a04e
KH
416 (if (consp default-value)
417 (setq default-value (copy-sequence default-value))
418 (setq default-value (list default-value)))
419 (setcar default-value
420 (unidata-encode-val val-list (car default-value)))
421 (set-char-table-range table t (car default-value))
422 (set-char-table-range table nil (car default-value))
423 (dolist (elm (cdr default-value))
424 (setcar (nthcdr 2 elm)
425 (unidata-encode-val val-list (nth 2 elm)))
426 (set-char-table-range table (cons (car elm) (nth 1 elm)) (nth 2 elm)))
427
f600cf3a
KH
428 (setq tail unidata-list)
429 (while tail
430 (setq elt (car tail) tail (cdr tail))
431 (setq range (car elt)
432 val (funcall val-func (nth prop-idx elt)))
433 (setq val-code (if val (unidata-encode-val val-list val)))
434 (if (consp range)
149353a4 435 (when val-code
c805dec0 436 (set-char-table-range table range val-code)
149353a4
KH
437 (let ((from (car range)) (to (cdr range)))
438 ;; If RANGE doesn't end at the char-table boundary (each
439 ;; 128 characters), we may have to carry over the data
440 ;; for the last several characters (at most 127 chars)
441 ;; to the next loop. In that case, set PREV-RANGE-DATA
442 ;; to ((FROM . TO) . VAL-CODE) where (FROM . TO)
443 ;; specifies the range of characters handled in the next
444 ;; loop.
445 (when (< (logand to #x7F) #x7F)
446 (if (< from (logand to #x1FFF80))
447 (setq from (logand to #x1FFF80)))
448 (setq prev-range-data (cons (cons from to) val-code)))))
f600cf3a
KH
449 (let* ((start (lsh (lsh range -7) 7))
450 (limit (+ start 127))
0902a04e
KH
451 str count new-val from to vcode)
452 (fillarray vec (car default-value))
453 (dolist (elm (cdr default-value))
454 (setq from (car elm) to (nth 1 elm))
455 (when (and (<= from limit)
456 (or (>= from start) (>= to start)))
457 (setq from (max from start)
458 to (min to limit)
459 vcode (nth 2 elm))
149353a4
KH
460 (while (<= from to)
461 (aset vec (- from start) vcode)
462 (setq from (1+ from)))))
0902a04e
KH
463 ;; See the comment above.
464 (when (and prev-range-data
465 (>= (cdr (car prev-range-data)) start))
466 (setq from (car (car prev-range-data))
467 to (cdr (car prev-range-data))
468 vcode (cdr prev-range-data))
469 (while (<= from to)
470 (aset vec (- from start) vcode)
471 (setq from (1+ from))))
149353a4 472 (setq prev-range-data nil)
f600cf3a
KH
473 (if val-code
474 (aset vec (- range start) val-code))
475 (while (and (setq elt (car tail) range (car elt))
476 (integerp range)
477 (<= range limit))
478 (setq new-val (funcall val-func (nth prop-idx elt)))
479 (if (not (eq val new-val))
480 (setq val new-val
481 val-code (if val (unidata-encode-val val-list val))))
482 (if val-code
483 (aset vec (- range start) val-code))
484 (setq tail (cdr tail)))
c805dec0 485 (setq str "\002" val-code -1 count 0)
f600cf3a
KH
486 (mapc #'(lambda (x)
487 (if (= val-code x)
488 (setq count (1+ count))
489 (if (> count 2)
490 (setq str (concat str (string val-code
491 (+ count 128))))
492 (if (= count 2)
493 (setq str (concat str (string val-code val-code)))
494 (if (= count 1)
495 (setq str (concat str (string val-code))))))
496 (setq val-code x count 1)))
497 vec)
498 (if (= count 128)
499 (if val
c805dec0 500 (set-char-table-range table (cons start limit) val-code))
f600cf3a
KH
501 (if (= val-code 0)
502 (set-char-table-range table (cons start limit) str)
503 (if (> count 2)
504 (setq str (concat str (string val-code (+ count 128))))
505 (if (= count 2)
506 (setq str (concat str (string val-code val-code)))
507 (setq str (concat str (string val-code)))))
508 (set-char-table-range table (cons start limit) str))))))
509
dd7d19d7 510 (set-char-table-extra-slot table 0 prop)
f600cf3a
KH
511 (set-char-table-extra-slot table 4 (vconcat (mapcar 'car val-list)))
512 table))
513
c805dec0 514(defun unidata-gen-table-symbol (prop default-value val-list)
f600cf3a
KH
515 (let ((table (unidata-gen-table prop
516 #'(lambda (x) (and (> (length x) 0)
517 (intern x)))
c805dec0
KH
518 default-value val-list)))
519 (set-char-table-extra-slot table 1 0)
520 (set-char-table-extra-slot table 2 1)
f600cf3a
KH
521 table))
522
c805dec0 523(defun unidata-gen-table-integer (prop default-value val-list)
f600cf3a
KH
524 (let ((table (unidata-gen-table prop
525 #'(lambda (x) (and (> (length x) 0)
526 (string-to-number x)))
c805dec0
KH
527 default-value val-list)))
528 (set-char-table-extra-slot table 1 0)
529 (set-char-table-extra-slot table 2 1)
f600cf3a
KH
530 table))
531
c805dec0 532(defun unidata-gen-table-numeric (prop default-value val-list)
dd787998
KH
533 (let ((table (unidata-gen-table prop
534 #'(lambda (x)
535 (if (string-match "/" x)
536 (/ (float (string-to-number x))
537 (string-to-number
538 (substring x (match-end 0))))
539 (if (> (length x) 0)
540 (string-to-number x))))
c805dec0
KH
541 default-value val-list)))
542 (set-char-table-extra-slot table 1 0)
543 (set-char-table-extra-slot table 2 2)
dd787998
KH
544 table))
545
f600cf3a
KH
546\f
547;; WORD-LIST TABLE
548
549;; If the table is for `name' property, each character in the string
550;; is one of these:
551;; DIFF-HEAD-CODE (0, 1, or 2):
552;; specifies how to decode the following characters.
553;; WORD-CODE (3..#x7FF excluding '-', '0'..'9', 'A'..'Z'):
554;; specifies an index number into WORD-TABLE (see below)
555;; Otherwise (' ', '-', '0'..'9', 'A'..'Z'):
556;; specifies a literal word.
557;;
558;; The 4th slots is a vector:
559;; [ WORD-TABLE BLOCK-NAME HANGUL-JAMO-TABLE ]
560;; WORD-TABLE is a vector of word symbols.
561;; BLOCK-NAME is a vector of name symbols for a block of characters.
562;; HANGUL-JAMO-TABLE is `unidata-name-jamo-name-table'.
563
564;; Return the difference of symbol list L1 and L2 in this form:
565;; (DIFF-HEAD SYM1 SYM2 ...)
566;; DIFF-HEAD is ((SAME-HEAD-LENGTH * 16) + SAME-TAIL-LENGTH).
567;; Ex: If L1 is (a b c d e f) and L2 is (a g h e f), this function
568;; returns ((+ (* 1 16) 2) g h).
569;; It means that we can get L2 from L1 by prepending the first element
570;; of L1 and appending the last 2 elements of L1 to the list (g h).
571;; If L1 and L2 don't have common elements at the head and tail,
572;; set DIFF-HEAD to -1 and SYM1 ... to the elements of L2.
573
574(defun unidata-word-list-diff (l1 l2)
575 (let ((beg 0)
576 (end 0)
577 (len1 (length l1))
578 (len2 (length l2))
579 result)
580 (when (< len1 16)
581 (while (and l1 (eq (car l1) (car l2)))
582 (setq beg (1+ beg)
583 l1 (cdr l1) len1 (1- len1) l2 (cdr l2) len2 (1- len2)))
dd7d19d7 584 (while (and (< end len1) (< end len2)
f600cf3a
KH
585 (eq (nth (- len1 end 1) l1) (nth (- len2 end 1) l2)))
586 (setq end (1+ end))))
587 (if (= (+ beg end) 0)
588 (setq result (list -1))
589 (setq result (list (+ (* beg 16) (+ beg (- len1 end))))))
590 (while (< end len2)
591 (setcdr result (cons (nth (- len2 end 1) l2) (cdr result)))
592 (setq end (1+ end)))
593 result))
594
595;; Return a compressed form of the vector VEC. Each element of VEC is
596;; a list of symbols of which names can be concatenated to form a
597;; character name. This function changes those elements into
598;; compressed forms by utilizing the fact that diff of consecutive
599;; elements is usually small.
600
601(defun unidata-word-list-compress (vec)
602 (let (last-elt last-idx diff-head tail elt val)
603 (dotimes (i 128)
604 (setq elt (aref vec i))
605 (when elt
606 (if (null last-elt)
607 (setq diff-head -1
608 val (cons 0 elt))
609 (setq val (unidata-word-list-diff last-elt elt))
610 (if (= (car val) -1)
611 (setq diff-head -1
612 val (cons 0 (cdr val)))
613 (if (eq diff-head (car val))
614 (setq val (cons 2 (cdr val)))
615 (setq diff-head (car val))
616 (if (>= diff-head 0)
617 (setq val (cons 1 val))))))
618 (aset vec i val)
619 (setq last-idx i last-elt elt)))
620 (if (not last-idx)
621 (setq vec nil)
622 (if (< last-idx 127)
623 (let ((shorter (make-vector (1+ last-idx) nil)))
624 (dotimes (i (1+ last-idx))
625 (aset shorter i (aref vec i)))
626 (setq vec shorter))))
627 vec))
628
629;; Encode the word index IDX into a characters code that can be
630;; embedded in a string.
631
632(defsubst unidata-encode-word (idx)
633 ;; Exclude 0, 1, 2.
634 (+ idx 3))
635
636;; Decode the character code CODE (that is embedded in a string) into
637;; the corresponding word name by looking up WORD-TABLE.
638
639(defsubst unidata-decode-word (code word-table)
640 (setq code (- code 3))
641 (if (< code (length word-table))
642 (aref word-table code)))
643
644;; Table of short transliterated name symbols of Hangul Jamo divided
645;; into Choseong, Jungseong, and Jongseong.
646
647(defconst unidata-name-jamo-name-table
648 [[G GG N D DD R M B BB S SS nil J JJ C K T P H]
649 [A AE YA YAE EO E YEO YE O WA WAE OE YO U WEO WE WI YU EU YI I]
650 [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]])
651
652;; Return a name of CHAR. VAL is the current value of (aref TABLE
653;; CHAR).
654
655(defun unidata-get-name (char val table)
dd7d19d7 656 (cond
f600cf3a
KH
657 ((stringp val)
658 (if (> (aref val 0) 0)
659 val
660 (let* ((first-char (lsh (lsh char -7) 7))
661 (word-table (aref (char-table-extra-slot table 4) 0))
662 (i 1)
663 (len (length val))
664 (vec (make-vector 128 nil))
665 (idx 0)
666 (case-fold-search nil)
667 c word-list tail-list last-list word diff-head)
668 (while (< i len)
669 (setq c (aref val i))
670 (if (< c 3)
671 (progn
672 (if (or word-list tail-list)
673 (aset vec idx
674 (setq last-list (nconc word-list tail-list))))
675 (setq i (1+ i) idx (1+ idx)
676 word-list nil tail-list nil)
677 (if (> c 0)
678 (let ((l last-list))
679 (if (= c 1)
680 (setq diff-head
681 (prog1 (aref val i) (setq i (1+ i)))))
682 (setq tail-list (nthcdr (% diff-head 16) last-list))
683 (dotimes (i (/ diff-head 16))
684 (setq word-list (nconc word-list (list (car l)))
685 l (cdr l))))))
686 (setq word-list
dd7d19d7 687 (nconc word-list
f600cf3a
KH
688 (list (symbol-name
689 (unidata-decode-word c word-table))))
690 i (1+ i))))
691 (if (or word-list tail-list)
692 (aset vec idx (nconc word-list tail-list)))
693 (setq val nil)
694 (dotimes (i 128)
695 (setq c (+ first-char i))
696 (let ((name (aref vec i)))
697 (if name
698 (let ((tail (cdr (setq name (copy-sequence name))))
699 elt)
700 (while tail
701 (setq elt (car tail))
702 (or (string= elt "-")
703 (progn
704 (setcdr tail (cons elt (cdr tail)))
705 (setcar tail " ")))
706 (setq tail (cddr tail)))
707 (setq name (apply 'concat name))))
708 (aset table c name)
709 (if (= c char)
710 (setq val name))))
0902a04e 711 (or val ""))))
f600cf3a
KH
712
713 ((and (integerp val) (> val 0))
714 (let* ((symbol-table (aref (char-table-extra-slot table 4) 1))
715 (sym (aref symbol-table (1- val))))
716 (cond ((eq sym 'HANGUL\ SYLLABLE)
717 (let ((jamo-name-table (aref (char-table-extra-slot table 4) 2)))
718 ;; SIndex = S - SBase
719 (setq char (- char #xAC00))
720 (let ( ;; LIndex = SIndex / NCount
721 (L (/ char 588))
722 ;; VIndex = (SIndex % NCount) * TCount
723 (V (/ (% char 588) 28))
724 ;; TIndex = SIndex % TCount
725 (T (% char 28)))
dd7d19d7 726 (format "HANGUL SYLLABLE %s%s%s"
752fc44b
KH
727 ;; U+110B is nil in this table.
728 (or (aref (aref jamo-name-table 0) L) "")
f600cf3a
KH
729 (aref (aref jamo-name-table 1) V)
730 (if (= T 0) ""
731 (aref (aref jamo-name-table 2) (1- T)))))))
e60b06d7
KH
732 ((eq sym 'CJK\ IDEOGRAPH)
733 (format "%s-%04X" sym char))
f600cf3a
KH
734 ((eq sym 'CJK\ COMPATIBILITY\ IDEOGRAPH)
735 (format "%s-%04X" sym char))
736 ((eq sym 'VARIATION\ SELECTOR)
0902a04e
KH
737 (format "%s-%d" sym (+ (- char #xe0100) 17))))))
738
739 (t "")))
f600cf3a
KH
740
741;; Store VAL as the name of CHAR in TABLE.
742
743(defun unidata-put-name (char val table)
744 (let ((current-val (aref table char)))
745 (if (and (stringp current-val) (= (aref current-val 0) 0))
746 (funcall (char-table-extra-slot table 1) char current-val table))
747 (aset table char val)))
748
749(defun unidata-get-decomposition (char val table)
750 (cond
0902a04e
KH
751 ((not val)
752 (list char))
753
f600cf3a
KH
754 ((consp val)
755 val)
756
757 ((stringp val)
758 (if (> (aref val 0) 0)
759 val
760 (let* ((first-char (lsh (lsh char -7) 7))
761 (word-table (char-table-extra-slot table 4))
762 (i 1)
763 (len (length val))
764 (vec (make-vector 128 nil))
765 (idx 0)
766 (case-fold-search nil)
767 c word-list tail-list last-list word diff-head)
768 (while (< i len)
769 (setq c (aref val i))
770 (if (< c 3)
771 (progn
772 (if (or word-list tail-list)
773 (aset vec idx
774 (setq last-list (nconc word-list tail-list))))
775 (setq i (1+ i) idx (1+ idx)
776 word-list nil tail-list nil)
777 (if (> c 0)
778 (let ((l last-list))
779 (if (= c 1)
780 (setq diff-head
781 (prog1 (aref val i) (setq i (1+ i)))))
782 (setq tail-list (nthcdr (% diff-head 16) last-list))
783 (dotimes (i (/ diff-head 16))
784 (setq word-list (nconc word-list (list (car l)))
785 l (cdr l))))))
786 (setq word-list
dd7d19d7 787 (nconc word-list
f600cf3a
KH
788 (list (or (unidata-decode-word c word-table) c)))
789 i (1+ i))))
790 (if (or word-list tail-list)
791 (aset vec idx (nconc word-list tail-list)))
792 (dotimes (i 128)
793 (aset table (+ first-char i) (aref vec i)))
0902a04e
KH
794 (setq val (aref vec (- char first-char)))
795 (or val (list char)))))
f600cf3a 796
1275c1dc
KH
797 ;; Hangul syllable
798 ((and (eq val 0) (>= char #xAC00) (<= char #xD7A3))
799 ;; SIndex = S (char) - SBase (#xAC00)
800 (setq char (- char #xAC00))
801 (let (;; L = LBase + SIndex / NCount
802 (L (+ #x1100 (/ char 588)))
803 ;; V = VBase + (SIndex % NCount) * TCount
804 (V (+ #x1161 (/ (% char 588) 28)))
0b4ff5df
KH
805 ;; LV = SBase + (SIndex / TCount) * TCount
806 (LV (+ #xAC00 (* (/ char 28) 28)))
1275c1dc
KH
807 ;; T = TBase + SIndex % TCount
808 (T (+ #x11A7 (% char 28))))
d636247a
KH
809 (if (= T #x11A7)
810 (list L V)
88758986 811 (list LV T))))
1275c1dc
KH
812
813 ))
814
815;; Store VAL as the decomposition information of CHAR in TABLE.
f600cf3a
KH
816
817(defun unidata-put-decomposition (char val table)
818 (let ((current-val (aref table char)))
819 (if (and (stringp current-val) (= (aref current-val 0) 0))
820 (funcall (char-table-extra-slot table 1) char current-val table))
821 (aset table char val)))
822
823;; UnicodeData.txt contains these lines:
824;; 0000;<control>;Cc;0;BN;;;;;N;NULL;;;;
825;; ...
826;; 0020;SPACE;Zs;0;WS;;;;;N;;;;;
827;; ...
828;; The following command yields a file of about 96K bytes.
829;; % gawk -F ';' '{print $1,$2;}' < UnicodeData.txt | gzip > temp.gz
830;; With the following function, we can get a file of almost the same
831;; the size.
832
833;; Generate a char-table for character names.
834
835(defun unidata-gen-table-word-list (prop val-func)
836 (let ((table (make-char-table 'char-code-property-table))
837 (prop-idx (unidata-prop-index prop))
838 (word-list (list nil))
839 word-table
840 block-list block-word-table block-end
841 tail elt range val idx slot)
f600cf3a
KH
842 (setq tail unidata-list)
843 (setq block-end -1)
844 (while tail
845 (setq elt (car tail) tail (cdr tail))
846 (setq range (car elt)
847 val (funcall val-func (nth prop-idx elt)))
848 ;; Treat the sequence of "CJK COMPATIBILITY IDEOGRAPH-XXXX" and
849 ;; "VARIATION SELECTOR-XXX" as a block.
850 (if (and (consp val) (eq prop 'name)
851 (or (and (eq (car val) 'CJK)
852 (eq (nth 1 val) 'COMPATIBILITY))
853 (and (>= range #xe0100)
854 (eq (car val) 'VARIATION)
855 (eq (nth 1 val) 'SELECTOR))))
856 (let ((first (car val))
857 (second (nth 1 val))
858 (start range))
859 (while (and (setq elt (car tail) range (car elt)
860 val (funcall val-func (nth prop-idx elt)))
861 (consp val)
862 (eq first (car val))
863 (eq second (nth 1 val)))
864 (setq block-end range
865 tail (cdr tail)))
866 (setq range (cons start block-end)
867 val (if (eq first 'CJK) 'CJK\ COMPATIBILITY\ IDEOGRAPH
868 'VARIATION\ SELECTOR))))
869
870 (if (consp range)
871 (if val
872 (let ((slot (assq val block-list)))
873 (setq range (cons (car range) (cdr range)))
874 (setq block-end (cdr range))
875 (if slot
876 (nconc slot (list range))
877 (push (list val range) block-list))))
878 (let* ((start (lsh (lsh range -7) 7))
879 (limit (+ start 127))
880 (first tail)
881 (vec (make-vector 128 nil))
882 c name len)
883 (if (<= start block-end)
884 ;; START overlap with the previous block.
885 (aset table range (nth prop-idx elt))
886 (if val
887 (aset vec (- range start) val))
888 (while (and (setq elt (car tail) range (car elt))
889 (integerp range)
890 (<= range limit))
891 (setq val (funcall val-func (nth prop-idx elt)))
892 (if val
893 (aset vec (- range start) val))
894 (setq tail (cdr tail)))
895 (setq vec (unidata-word-list-compress vec))
896 (when vec
897 (dotimes (i (length vec))
898 (dolist (elt (aref vec i))
899 (if (symbolp elt)
900 (let ((slot (assq elt word-list)))
901 (if slot
902 (setcdr slot (1+ (cdr slot)))
903 (setcdr word-list
904 (cons (cons elt 1) (cdr word-list))))))))
905 (set-char-table-range table (cons start limit) vec))))))
906 (setq word-list (sort (cdr word-list)
907 #'(lambda (x y) (> (cdr x) (cdr y)))))
908 (setq tail word-list idx 0)
909 (while tail
910 (setcdr (car tail) (unidata-encode-word idx))
911 (setq idx (1+ idx) tail (cdr tail)))
912 (setq word-table (make-vector (length word-list) nil))
913 (setq idx 0)
914 (dolist (elt word-list)
915 (aset word-table idx (car elt))
916 (setq idx (1+ idx)))
917
918 (if (and (eq prop 'decomposition)
919 (> idx 32))
1275c1dc 920 (error "Too many symbols in decomposition data"))
f600cf3a
KH
921
922 (dotimes (i (/ #x110000 128))
923 (let* ((idx (* i 128))
924 (vec (aref table idx)))
925 (when (vectorp vec)
926 (dotimes (i (length vec))
927 (let ((tail (aref vec i))
928 elt code)
929 (if (not tail)
930 (aset vec i "\0")
931 (while tail
932 (setq elt (car tail)
933 code (if (integerp elt) elt
934 (cdr (assq elt word-list))))
935 (setcar tail (string code))
936 (setq tail (cdr tail)))
937 (aset vec i (mapconcat 'identity (aref vec i) "")))))
938 (set-char-table-range
939 table (cons idx (+ idx 127))
940 (mapconcat 'identity vec "")))))
941
942 (setq block-word-table (make-vector (length block-list) nil))
943 (setq idx 0)
944 (dolist (elt block-list)
945 (dolist (e (cdr elt))
946 (set-char-table-range table e (1+ idx)))
947 (aset block-word-table idx (car elt))
948 (setq idx (1+ idx)))
949
950 (set-char-table-extra-slot table 0 prop)
951 (set-char-table-extra-slot table 4 (cons word-table block-word-table))
952 table))
953
954(defun unidata-split-name (str)
955 (if (symbolp str)
956 str
957 (let ((len (length str))
958 (l nil)
959 (idx 0)
960 c)
961 (if (= len 0)
962 nil
963 (dotimes (i len)
964 (setq c (aref str i))
965 (if (= c 32)
966 (setq l (cons (intern (substring str idx i)) l)
967 idx (1+ i))
dd7d19d7 968 (if (and (= c ?-) (< idx i)
f600cf3a
KH
969 (< (1+ i) len) (/= (aref str (1+ i)) 32))
970 (setq l (cons '- (cons (intern (substring str idx i)) l))
971 idx (1+ i)))))
972 (nreverse (cons (intern (substring str idx)) l))))))
973
c805dec0 974(defun unidata-gen-table-name (prop &rest ignore)
f600cf3a
KH
975 (let* ((table (unidata-gen-table-word-list prop 'unidata-split-name))
976 (word-tables (char-table-extra-slot table 4)))
977 (byte-compile 'unidata-get-name)
978 (byte-compile 'unidata-put-name)
979 (set-char-table-extra-slot table 1 (symbol-function 'unidata-get-name))
980 (set-char-table-extra-slot table 2 (symbol-function 'unidata-put-name))
981
982 (if (eq prop 'name)
983 (set-char-table-extra-slot table 4
984 (vector (car word-tables)
985 (cdr word-tables)
986 unidata-name-jamo-name-table))
987 (set-char-table-extra-slot table 4
988 (vector (car word-tables))))
989 table))
990
991(defun unidata-split-decomposition (str)
992 (if (symbolp str)
993 str
994 (let ((len (length str))
995 (l nil)
996 (idx 0)
997 c)
998 (if (= len 0)
999 nil
1000 (dotimes (i len)
1001 (setq c (aref str i))
1002 (if (= c 32)
1003 (setq l (if (= (aref str idx) ?<)
f1478738 1004 (cons (intern (substring str (1+ idx) (1- i))) l)
1275c1dc 1005 (cons (string-to-number (substring str idx i) 16) l))
f600cf3a
KH
1006 idx (1+ i))))
1007 (if (= (aref str idx) ?<)
f1478738 1008 (setq l (cons (intern (substring str (1+ idx) (1- len))) l))
1275c1dc 1009 (setq l (cons (string-to-number (substring str idx len) 16) l)))
f600cf3a
KH
1010 (nreverse l)))))
1011
1012
c805dec0 1013(defun unidata-gen-table-decomposition (prop &rest ignore)
f600cf3a
KH
1014 (let* ((table (unidata-gen-table-word-list prop 'unidata-split-decomposition))
1015 (word-tables (char-table-extra-slot table 4)))
1016 (byte-compile 'unidata-get-decomposition)
1017 (byte-compile 'unidata-put-decomposition)
1018 (set-char-table-extra-slot table 1
1019 (symbol-function 'unidata-get-decomposition))
dd7d19d7 1020 (set-char-table-extra-slot table 2
f600cf3a
KH
1021 (symbol-function 'unidata-put-decomposition))
1022 (set-char-table-extra-slot table 4 (car word-tables))
1023 table))
1024
1025
1026\f
1027(defun unidata-describe-general-category (val)
1028 (cdr (assq val
c805dec0
KH
1029 '((nil . "Uknown")
1030 (Lu . "Letter, Uppercase")
f600cf3a
KH
1031 (Ll . "Letter, Lowercase")
1032 (Lt . "Letter, Titlecase")
1033 (Lm . "Letter, Modifier")
1034 (Lo . "Letter, Other")
1035 (Mn . "Mark, Nonspacing")
1036 (Mc . "Mark, Spacing Combining")
1037 (Me . "Mark, Enclosing")
1038 (Nd . "Number, Decimal Digit")
1039 (Nl . "Number, Letter")
1040 (No . "Number, Other")
1041 (Pc . "Punctuation, Connector")
1042 (Pd . "Punctuation, Dash")
1043 (Ps . "Punctuation, Open")
1044 (Pe . "Punctuation, Close")
1045 (Pi . "Punctuation, Initial quote")
1046 (Pf . "Punctuation, Final quote")
1047 (Po . "Punctuation, Other")
1048 (Sm . "Symbol, Math")
1049 (Sc . "Symbol, Currency")
1050 (Sk . "Symbol, Modifier")
1051 (So . "Symbol, Other")
1052 (Zs . "Separator, Space")
1053 (Zl . "Separator, Line")
1054 (Zp . "Separator, Paragraph")
1055 (Cc . "Other, Control")
1056 (Cf . "Other, Format")
1057 (Cs . "Other, Surrogate")
1058 (Co . "Other, Private Use")
1059 (Cn . "Other, Not Assigned")))))
1060
1061(defun unidata-describe-canonical-combining-class (val)
1062 (cdr (assq val
1063 '((0 . "Spacing, split, enclosing, reordrant, and Tibetan subjoined")
1064 (1 . "Overlays and interior")
1065 (7 . "Nuktas")
1066 (8 . "Hiragana/Katakana voicing marks")
1067 (9 . "Viramas")
1068 (10 . "Start of fixed position classes")
1069 (199 . "End of fixed position classes")
1070 (200 . "Below left attached")
1071 (202 . "Below attached")
1072 (204 . "Below right attached")
1073 (208 . "Left attached (reordrant around single base character)")
1074 (210 . "Right attached")
1075 (212 . "Above left attached")
1076 (214 . "Above attached")
1077 (216 . "Above right attached")
1078 (218 . "Below left")
1079 (220 . "Below")
1080 (222 . "Below right")
1081 (224 . "Left (reordrant around single base character)")
1082 (226 . "Right")
1083 (228 . "Above left")
1084 (230 . "Above")
1085 (232 . "Above right")
1086 (233 . "Double below")
1087 (234 . "Double above")
1088 (240 . "Below (iota subscript)")))))
1089
1090(defun unidata-describe-bidi-class (val)
1091 (cdr (assq val
1092 '((L . "Left-to-Right")
1093 (LRE . "Left-to-Right Embedding")
1094 (LRO . "Left-to-Right Override")
1095 (R . "Right-to-Left")
1096 (AL . "Right-to-Left Arabic")
1097 (RLE . "Right-to-Left Embedding")
1098 (RLO . "Right-to-Left Override")
1099 (PDF . "Pop Directional Format")
1100 (EN . "European Number")
1101 (ES . "European Number Separator")
1102 (ET . "European Number Terminator")
1103 (AN . "Arabic Number")
1104 (CS . "Common Number Separator")
1105 (NSM . "Non-Spacing Mark")
1106 (BN . "Boundary Neutral")
1107 (B . "Paragraph Separator")
1108 (S . "Segment Separator")
1109 (WS . "Whitespace")
1110 (ON . "Other Neutrals")))))
1111
1112(defun unidata-describe-decomposition (val)
0af3fedf
KH
1113 (mapconcat
1114 #'(lambda (x)
1115 (if (symbolp x) (symbol-name x)
1116 (concat (string ?')
1117 (compose-string (string x) 0 1 (string ?\t x ?\t))
1118 (string ?'))))
1119 val " "))
f600cf3a 1120
c805dec0
KH
1121(defun unidata-gen-mirroring-list ()
1122 (let ((head (list nil))
1123 tail)
1124 (with-temp-buffer
1125 (insert-file-contents (expand-file-name "BidiMirroring.txt" unidata-dir))
1126 (goto-char (point-min))
1127 (setq tail head)
1128 (while (re-search-forward "^\\([0-9A-F]+\\);\\s +\\([0-9A-F]+\\)" nil t)
1129 (let ((char (string-to-number (match-string 1) 16))
1130 (mirror (match-string 2)))
1131 (setq tail (setcdr tail (list (list char mirror)))))))
1132 (cdr head)))
1133
f600cf3a
KH
1134;; Verify if we can retrieve correct values from the generated
1135;; char-tables.
1136
1137(defun unidata-check ()
1138 (dolist (elt unidata-prop-alist)
1139 (let* ((prop (car elt))
1140 (index (unidata-prop-index prop))
1141 (generator (unidata-prop-generator prop))
dd7d19d7 1142 (table (progn
f600cf3a
KH
1143 (message "Generating %S table..." prop)
1144 (funcall generator prop)))
1145 (decoder (char-table-extra-slot table 1))
1146 (check #x400))
1147 (dolist (e unidata-list)
1148 (let ((char (car e))
1149 (val1 (nth index e))
1150 val2)
1151 (if (and (stringp val1) (= (length val1) 0))
1152 (setq val1 nil))
1153 (unless (consp char)
1154 (setq val2 (funcall decoder char (aref table char) table))
1155 (if val1
1156 (cond ((eq generator 'unidata-gen-table-symbol)
1157 (setq val1 (intern val1)))
1158 ((eq generator 'unidata-gen-table-integer)
1275c1dc 1159 (setq val1 (string-to-number val1)))
f600cf3a 1160 ((eq generator 'unidata-gen-table-character)
1275c1dc 1161 (setq val1 (string-to-number val1 16)))
f600cf3a
KH
1162 ((eq generator 'unidata-gen-table-decomposition)
1163 (setq val1 (unidata-split-decomposition val1)))))
1164 (when (>= char check)
1165 (message "%S %04X" prop check)
1166 (setq check (+ check #x400)))
1167 (or (equal val1 val2)
dd7d19d7 1168 (insert (format "> %04X %S\n< %04X %S\n"
f600cf3a
KH
1169 char val1 char val2)))
1170 (sit-for 0)))))))
1171
1172;; The entry function. It generates files described in the header
1173;; comment of this file.
1174
c805dec0
KH
1175(defun unidata-gen-files (&optional data-dir unidata-text-file)
1176 (or data-dir
1177 (setq data-dir (car command-line-args-left)
1178 command-line-args-left (cdr command-line-args-left)
1179 unidata-text-file (car command-line-args-left)
3921ea21 1180 command-line-args-left (cdr command-line-args-left)))
db516472 1181 (let ((coding-system-for-write 'utf-8-unix)
c805dec0
KH
1182 (charprop-file "charprop.el")
1183 (unidata-dir data-dir))
1184 (dolist (elt unidata-prop-alist)
1185 (let* ((prop (car elt))
1186 (file (unidata-prop-file prop)))
1187 (if (file-exists-p file)
1188 (delete-file file))))
1189 (unidata-setup-list unidata-text-file)
f600cf3a
KH
1190 (with-temp-file charprop-file
1191 (insert ";; Automatically generated by unidata-gen.el.\n")
1192 (dolist (elt unidata-prop-alist)
1193 (let* ((prop (car elt))
1194 (generator (unidata-prop-generator prop))
1195 (file (unidata-prop-file prop))
1196 (docstring (unidata-prop-docstring prop))
1197 (describer (unidata-prop-describer prop))
c805dec0
KH
1198 (default-value (unidata-prop-default prop))
1199 (val-list (unidata-prop-val-list prop))
f600cf3a
KH
1200 table)
1201 ;; Filename in this comment line is extracted by sed in
1202 ;; Makefile.
1203 (insert (format ";; FILE: %s\n" file))
1204 (insert (format "(define-char-code-property '%S %S\n %S)\n"
1205 prop file docstring))
c805dec0 1206 (with-temp-buffer
f600cf3a 1207 (message "Generating %s..." file)
c805dec0
KH
1208 (when (file-exists-p file)
1209 (insert-file-contents file)
1210 (goto-char (point-max))
1211 (search-backward ";; Local Variables:"))
1212 (setq table (funcall generator prop default-value val-list))
f600cf3a
KH
1213 (when describer
1214 (unless (subrp (symbol-function describer))
1215 (byte-compile describer)
1216 (setq describer (symbol-function describer)))
1217 (set-char-table-extra-slot table 3 describer))
c805dec0
KH
1218 (if (bobp)
1219 (insert ";; Copyright (C) 1991-2009 Unicode, Inc.
1220;; This file was generated from the Unicode data files at
1221;; http://www.unicode.org/Public/UNIDATA/.
1222;; See lisp/international/README for the copyright and permission notice.\n"))
1223 (insert (format "(define-char-code-property '%S %S %S)\n"
1224 prop table docstring))
1225 (if (eobp)
1226 (insert ";; Local Variables:\n"
1227 ";; coding: utf-8\n"
1228 ";; no-byte-compile: t\n"
1229 ";; End:\n\n"
1230 (format ";; %s ends here\n" file)))
1231 (write-file file)
1232 (message "Generating %s...done" file))))
f600cf3a 1233 (message "Writing %s..." charprop-file)
1275c1dc 1234 (insert ";; Local Variables:\n"
f600cf3a
KH
1235 ";; coding: utf-8\n"
1236 ";; no-byte-compile: t\n"
1237 ";; End:\n\n"
1238 (format ";; %s ends here\n" charprop-file)))))
1239
1275c1dc
KH
1240\f
1241
f600cf3a 1242;;; unidata-gen.el ends here