Merge from trunk.
[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
53964682 161;; FROMn and TOn (inclusive). The default value of characters
0902a04e 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
bca633fb
EZ
197 ;; The assignment of default values to blocks of code points
198 ;; follows the file DerivedBidiClass.txt from the Unicode
199 ;; Character Database (UCD).
200 (L (#x0600 #x06FF AL) (#xFB50 #xFDFF AL) (#xFE70 #xFEFF AL)
201 (#x0590 #x05FF R) (#x07C0 #x08FF R)
0902a04e 202 (#xFB1D #xFB4F R) (#x10800 #x10FFF R) (#x1E800 #x1EFFF R))
c805dec0
KH
203 ;; The order of elements must be in sync with bidi_type_t in
204 ;; src/dispextern.h.
205 (L R EN AN BN B AL LRE LRO RLE RLO PDF ES ET CS NSM S WS ON))
f600cf3a
KH
206 (decomposition
207 5 unidata-gen-table-decomposition "uni-decomposition.el"
208 "Unicode decomposition mapping.
209Property value is a list of characters. The first element may be
1275c1dc 210one of these symbols representing compatibility formatting tag:
f1478738
KH
211 font, noBreak, initial, medial, final, isolated, circle, super,
212 sub, vertical, wide, narrow, small, square, fraction, compat"
f600cf3a
KH
213 unidata-describe-decomposition)
214 (decimal-digit-value
215 6 unidata-gen-table-integer "uni-decimal.el"
216 "Unicode numeric value (decimal digit).
0902a04e
KH
217Property value is an integer 0..9, or nil.
218The value nil stands for NaN \"Numeric_Value\".")
f600cf3a
KH
219 (digit-value
220 7 unidata-gen-table-integer "uni-digit.el"
221 "Unicode numeric value (digit).
0902a04e
KH
222Property value is an integer 0..9, or nil.
223The value nil stands for NaN \"Numeric_Value\".")
f600cf3a 224 (numeric-value
dd787998 225 8 unidata-gen-table-numeric "uni-numeric.el"
f600cf3a 226 "Unicode numeric value (numeric).
0902a04e
KH
227Property value is an integer, a floating point, or nil.
228The value nil stands for NaN \"Numeric_Value\".")
f600cf3a
KH
229 (mirrored
230 9 unidata-gen-table-symbol "uni-mirrored.el"
231 "Unicode bidi mirrored flag.
0902a04e
KH
232Property value is a symbol `Y' or `N'. See also the property `mirroring'."
233 nil
234 N)
f600cf3a
KH
235 (old-name
236 10 unidata-gen-table-name "uni-old-name.el"
237 "Unicode old names as published in Unicode 1.0.
238Property value is a string.")
239 (iso-10646-comment
240 11 unidata-gen-table-name "uni-comment.el"
241 "Unicode ISO 10646 comment.
242Property value is a string.")
243 (uppercase
244 12 unidata-gen-table-character "uni-uppercase.el"
245 "Unicode simple uppercase mapping.
0902a04e
KH
246Property value is a character or nil.
247The value nil means that the actual property value of a character
248is the character itself."
f600cf3a
KH
249 string)
250 (lowercase
251 13 unidata-gen-table-character "uni-lowercase.el"
252 "Unicode simple lowercase mapping.
0902a04e
KH
253Property value is a character or nil.
254The value nil means that the actual property value of a character
255is the character itself."
f600cf3a
KH
256 string)
257 (titlecase
258 14 unidata-gen-table-character "uni-titlecase.el"
259 "Unicode simple titlecase mapping.
0902a04e
KH
260Property value is a character or nil.
261The value nil means that the actual property value of a character
262is the character itself."
c805dec0
KH
263 string)
264 (mirroring
265 unidata-gen-mirroring-list unidata-gen-table-character "uni-mirrored.el"
266 "Unicode bidi-mirroring characters.
0902a04e
KH
267Property value is a character that has the corresponding mirroring image or nil.
268The value nil means that the actual property value of a character
269is the character itself.")))
f600cf3a
KH
270
271;; Functions to access the above data.
272(defsubst unidata-prop-index (prop) (nth 1 (assq prop unidata-prop-alist)))
273(defsubst unidata-prop-generator (prop) (nth 2 (assq prop unidata-prop-alist)))
274(defsubst unidata-prop-file (prop) (nth 3 (assq prop unidata-prop-alist)))
275(defsubst unidata-prop-docstring (prop) (nth 4 (assq prop unidata-prop-alist)))
276(defsubst unidata-prop-describer (prop) (nth 5 (assq prop unidata-prop-alist)))
c805dec0
KH
277(defsubst unidata-prop-default (prop) (nth 6 (assq prop unidata-prop-alist)))
278(defsubst unidata-prop-val-list (prop) (nth 7 (assq prop unidata-prop-alist)))
f600cf3a
KH
279
280\f
281;; SIMPLE TABLE
282;;
283;; If the type of character property value is character, and the
284;; values of succeeding character codes are usually different, we use
285;; a char-table described here to store such values.
286;;
c805dec0
KH
287;; A char-table divides character code space (#x0..#x3FFFFF) into
288;; #x8000 blocks (each block contains 128 characters).
289
290;; If all characters of a block have no property, a char-table has the
291;; symbol nil for that block. Otherwise a char-table has a string of
292;; the following format for it.
f600cf3a 293;;
c805dec0
KH
294;; The first character of the string is ?\001.
295;; The second character of the string is FIRST-INDEX.
296;; The Nth (N > 1) character of the string is a property value of the
297;; character (BLOCK-HEAD + FIRST-INDEX + N - 2), where BLOCK-HEAD is
298;; the first character of the block.
f600cf3a 299;;
c805dec0
KH
300;; This kind of char-table has these extra slots:
301;; 1st: the property symbol
302;; 2nd: nil
303;; 3rd: 0 (corresponding to uniprop_encode_character in chartab.c)
304;; 4th to 5th: nil
f600cf3a 305
c805dec0 306(defun unidata-gen-table-character (prop &rest ignore)
f600cf3a
KH
307 (let ((table (make-char-table 'char-code-property-table))
308 (prop-idx (unidata-prop-index prop))
309 (vec (make-vector 128 0))
310 (tail unidata-list)
311 elt range val idx slot)
c805dec0
KH
312 (if (functionp prop-idx)
313 (setq tail (funcall prop-idx)
314 prop-idx 1))
f600cf3a
KH
315 (while tail
316 (setq elt (car tail) tail (cdr tail))
317 (setq range (car elt)
318 val (nth prop-idx elt))
319 (if (= (length val) 0)
320 (setq val nil)
321 (setq val (string-to-number val 16)))
322 (if (consp range)
323 (if val
324 (set-char-table-range table range val))
325 (let* ((start (lsh (lsh range -7) 7))
326 (limit (+ start 127))
327 first-index last-index)
328 (fillarray vec 0)
329 (if val
330 (aset vec (setq last-index (setq first-index (- range start)))
331 val))
332 (while (and (setq elt (car tail) range (car elt))
333 (integerp range)
334 (<= range limit))
335 (setq val (nth prop-idx elt))
336 (when (> (length val) 0)
337 (aset vec (setq last-index (- range start))
338 (string-to-number val 16))
339 (or first-index
340 (setq first-index last-index)))
341 (setq tail (cdr tail)))
342 (when first-index
c805dec0 343 (let ((str (string 1 first-index))
f600cf3a
KH
344 c)
345 (while (<= first-index last-index)
346 (setq str (format "%s%c" str (or (aref vec first-index) 0))
347 first-index (1+ first-index)))
348 (set-char-table-range table (cons start limit) str))))))
349
350 (set-char-table-extra-slot table 0 prop)
c805dec0 351 (set-char-table-extra-slot table 2 0)
f600cf3a
KH
352 table))
353
354
355\f
356;; RUN-LENGTH TABLE
357;;
c805dec0
KH
358;; If many characters of successive character codes have the same
359;; property value, we use a char-table described here to store the
360;; values.
f600cf3a 361;;
c805dec0
KH
362;; At first, instead of a value itself, we store an index number to
363;; the VAL-TABLE (5th extra slot) in the table. We call that index
364;; number as VAL-CODE here after.
365;;
366;; A char-table divides character code space (#x0..#x3FFFFF) into
367;; #x8000 blocks (each block contains 128 characters).
368;;
369;; If all characters of a block have the same value, a char-table has
370;; VAL-CODE for that block. Otherwise a char-table has a string of
371;; the following format for that block.
372;;
373;; The first character of the string is ?\002.
374;; The following characters has this form:
f600cf3a
KH
375;; ( VAL-CODE RUN-LENGTH ? ) +
376;; where:
c805dec0 377;; VAL-CODE (0..127): index into VAL-TABLE.
f600cf3a
KH
378;; RUN-LENGTH (130..255):
379;; (RUN-LENGTH - 128) specifies how many characters have the same
380;; value. If omitted, it means 1.
c805dec0
KH
381;;
382;; This kind of char-table has these extra slots:
383;; 1st: the property symbol
384;; 2nd: 0 (corresponding to uniprop_decode_value in chartab.c)
385;; 3rd: 1..3 (corresponding to uniprop_encode_xxx in chartab.c)
386;; 4th: function or nil
387;; 5th: VAL-TABLE
dd787998 388
f600cf3a
KH
389;; Encode the character property value VAL into an integer value by
390;; VAL-LIST. By side effect, VAL-LIST is modified.
391;; VAL-LIST has this form:
c805dec0
KH
392;; ((nil . 0) (VAL1 . 1) (VAL2 . 2) ...)
393;; If VAL is one of VALn, just return n.
394;; Otherwise, VAL-LIST is modified to this:
395;; ((nil . 0) (VAL1 . 1) (VAL2 . 2) ... (VAL . n+1))
f600cf3a
KH
396
397(defun unidata-encode-val (val-list val)
dd787998 398 (let ((slot (assoc val val-list))
f600cf3a
KH
399 val-code)
400 (if slot
401 (cdr slot)
c805dec0
KH
402 (setq val-code (length val-list))
403 (nconc val-list (list (cons val val-code)))
f600cf3a
KH
404 val-code)))
405
406;; Generate a char-table for the character property PROP.
407
c805dec0 408(defun unidata-gen-table (prop val-func default-value val-list)
f600cf3a
KH
409 (let ((table (make-char-table 'char-code-property-table))
410 (prop-idx (unidata-prop-index prop))
f600cf3a 411 (vec (make-vector 128 0))
149353a4
KH
412 tail elt range val val-code idx slot
413 prev-range-data)
c805dec0
KH
414 (setq val-list (cons nil (copy-sequence val-list)))
415 (setq tail val-list val-code 0)
416 ;; Convert (nil A B ...) to ((nil . 0) (A . 1) (B . 2) ...)
417 (while tail
418 (setcar tail (cons (car tail) val-code))
419 (setq tail (cdr tail) val-code (1+ val-code)))
0902a04e
KH
420 (if (consp default-value)
421 (setq default-value (copy-sequence default-value))
422 (setq default-value (list default-value)))
423 (setcar default-value
424 (unidata-encode-val val-list (car default-value)))
425 (set-char-table-range table t (car default-value))
426 (set-char-table-range table nil (car default-value))
427 (dolist (elm (cdr default-value))
428 (setcar (nthcdr 2 elm)
429 (unidata-encode-val val-list (nth 2 elm)))
430 (set-char-table-range table (cons (car elm) (nth 1 elm)) (nth 2 elm)))
431
f600cf3a
KH
432 (setq tail unidata-list)
433 (while tail
434 (setq elt (car tail) tail (cdr tail))
435 (setq range (car elt)
436 val (funcall val-func (nth prop-idx elt)))
437 (setq val-code (if val (unidata-encode-val val-list val)))
438 (if (consp range)
149353a4 439 (when val-code
c805dec0 440 (set-char-table-range table range val-code)
149353a4
KH
441 (let ((from (car range)) (to (cdr range)))
442 ;; If RANGE doesn't end at the char-table boundary (each
443 ;; 128 characters), we may have to carry over the data
444 ;; for the last several characters (at most 127 chars)
445 ;; to the next loop. In that case, set PREV-RANGE-DATA
446 ;; to ((FROM . TO) . VAL-CODE) where (FROM . TO)
447 ;; specifies the range of characters handled in the next
448 ;; loop.
449 (when (< (logand to #x7F) #x7F)
450 (if (< from (logand to #x1FFF80))
451 (setq from (logand to #x1FFF80)))
452 (setq prev-range-data (cons (cons from to) val-code)))))
f600cf3a
KH
453 (let* ((start (lsh (lsh range -7) 7))
454 (limit (+ start 127))
0902a04e
KH
455 str count new-val from to vcode)
456 (fillarray vec (car default-value))
457 (dolist (elm (cdr default-value))
458 (setq from (car elm) to (nth 1 elm))
459 (when (and (<= from limit)
460 (or (>= from start) (>= to start)))
461 (setq from (max from start)
462 to (min to limit)
463 vcode (nth 2 elm))
149353a4
KH
464 (while (<= from to)
465 (aset vec (- from start) vcode)
466 (setq from (1+ from)))))
0902a04e
KH
467 ;; See the comment above.
468 (when (and prev-range-data
469 (>= (cdr (car prev-range-data)) start))
470 (setq from (car (car prev-range-data))
471 to (cdr (car prev-range-data))
472 vcode (cdr prev-range-data))
473 (while (<= from to)
474 (aset vec (- from start) vcode)
475 (setq from (1+ from))))
149353a4 476 (setq prev-range-data nil)
f600cf3a
KH
477 (if val-code
478 (aset vec (- range start) val-code))
479 (while (and (setq elt (car tail) range (car elt))
480 (integerp range)
481 (<= range limit))
482 (setq new-val (funcall val-func (nth prop-idx elt)))
483 (if (not (eq val new-val))
484 (setq val new-val
485 val-code (if val (unidata-encode-val val-list val))))
486 (if val-code
487 (aset vec (- range start) val-code))
488 (setq tail (cdr tail)))
c805dec0 489 (setq str "\002" val-code -1 count 0)
f600cf3a
KH
490 (mapc #'(lambda (x)
491 (if (= val-code x)
492 (setq count (1+ count))
493 (if (> count 2)
494 (setq str (concat str (string val-code
495 (+ count 128))))
496 (if (= count 2)
497 (setq str (concat str (string val-code val-code)))
498 (if (= count 1)
499 (setq str (concat str (string val-code))))))
500 (setq val-code x count 1)))
501 vec)
502 (if (= count 128)
503 (if val
c805dec0 504 (set-char-table-range table (cons start limit) val-code))
f600cf3a
KH
505 (if (= val-code 0)
506 (set-char-table-range table (cons start limit) str)
507 (if (> count 2)
508 (setq str (concat str (string val-code (+ count 128))))
509 (if (= count 2)
510 (setq str (concat str (string val-code val-code)))
511 (setq str (concat str (string val-code)))))
512 (set-char-table-range table (cons start limit) str))))))
513
dd7d19d7 514 (set-char-table-extra-slot table 0 prop)
f600cf3a
KH
515 (set-char-table-extra-slot table 4 (vconcat (mapcar 'car val-list)))
516 table))
517
c805dec0 518(defun unidata-gen-table-symbol (prop default-value val-list)
f600cf3a
KH
519 (let ((table (unidata-gen-table prop
520 #'(lambda (x) (and (> (length x) 0)
521 (intern x)))
c805dec0
KH
522 default-value val-list)))
523 (set-char-table-extra-slot table 1 0)
524 (set-char-table-extra-slot table 2 1)
f600cf3a
KH
525 table))
526
c805dec0 527(defun unidata-gen-table-integer (prop default-value val-list)
f600cf3a
KH
528 (let ((table (unidata-gen-table prop
529 #'(lambda (x) (and (> (length x) 0)
530 (string-to-number x)))
c805dec0
KH
531 default-value val-list)))
532 (set-char-table-extra-slot table 1 0)
533 (set-char-table-extra-slot table 2 1)
f600cf3a
KH
534 table))
535
c805dec0 536(defun unidata-gen-table-numeric (prop default-value val-list)
dd787998
KH
537 (let ((table (unidata-gen-table prop
538 #'(lambda (x)
539 (if (string-match "/" x)
540 (/ (float (string-to-number x))
541 (string-to-number
542 (substring x (match-end 0))))
543 (if (> (length x) 0)
544 (string-to-number x))))
c805dec0
KH
545 default-value val-list)))
546 (set-char-table-extra-slot table 1 0)
547 (set-char-table-extra-slot table 2 2)
dd787998
KH
548 table))
549
f600cf3a
KH
550\f
551;; WORD-LIST TABLE
552
553;; If the table is for `name' property, each character in the string
554;; is one of these:
555;; DIFF-HEAD-CODE (0, 1, or 2):
556;; specifies how to decode the following characters.
557;; WORD-CODE (3..#x7FF excluding '-', '0'..'9', 'A'..'Z'):
558;; specifies an index number into WORD-TABLE (see below)
559;; Otherwise (' ', '-', '0'..'9', 'A'..'Z'):
560;; specifies a literal word.
561;;
562;; The 4th slots is a vector:
563;; [ WORD-TABLE BLOCK-NAME HANGUL-JAMO-TABLE ]
564;; WORD-TABLE is a vector of word symbols.
565;; BLOCK-NAME is a vector of name symbols for a block of characters.
566;; HANGUL-JAMO-TABLE is `unidata-name-jamo-name-table'.
567
568;; Return the difference of symbol list L1 and L2 in this form:
569;; (DIFF-HEAD SYM1 SYM2 ...)
570;; DIFF-HEAD is ((SAME-HEAD-LENGTH * 16) + SAME-TAIL-LENGTH).
571;; Ex: If L1 is (a b c d e f) and L2 is (a g h e f), this function
572;; returns ((+ (* 1 16) 2) g h).
573;; It means that we can get L2 from L1 by prepending the first element
574;; of L1 and appending the last 2 elements of L1 to the list (g h).
575;; If L1 and L2 don't have common elements at the head and tail,
576;; set DIFF-HEAD to -1 and SYM1 ... to the elements of L2.
577
578(defun unidata-word-list-diff (l1 l2)
579 (let ((beg 0)
580 (end 0)
581 (len1 (length l1))
582 (len2 (length l2))
583 result)
584 (when (< len1 16)
585 (while (and l1 (eq (car l1) (car l2)))
586 (setq beg (1+ beg)
587 l1 (cdr l1) len1 (1- len1) l2 (cdr l2) len2 (1- len2)))
dd7d19d7 588 (while (and (< end len1) (< end len2)
f600cf3a
KH
589 (eq (nth (- len1 end 1) l1) (nth (- len2 end 1) l2)))
590 (setq end (1+ end))))
591 (if (= (+ beg end) 0)
592 (setq result (list -1))
593 (setq result (list (+ (* beg 16) (+ beg (- len1 end))))))
594 (while (< end len2)
595 (setcdr result (cons (nth (- len2 end 1) l2) (cdr result)))
596 (setq end (1+ end)))
597 result))
598
599;; Return a compressed form of the vector VEC. Each element of VEC is
600;; a list of symbols of which names can be concatenated to form a
601;; character name. This function changes those elements into
602;; compressed forms by utilizing the fact that diff of consecutive
603;; elements is usually small.
604
605(defun unidata-word-list-compress (vec)
606 (let (last-elt last-idx diff-head tail elt val)
607 (dotimes (i 128)
608 (setq elt (aref vec i))
609 (when elt
610 (if (null last-elt)
611 (setq diff-head -1
612 val (cons 0 elt))
613 (setq val (unidata-word-list-diff last-elt elt))
614 (if (= (car val) -1)
615 (setq diff-head -1
616 val (cons 0 (cdr val)))
617 (if (eq diff-head (car val))
618 (setq val (cons 2 (cdr val)))
619 (setq diff-head (car val))
620 (if (>= diff-head 0)
621 (setq val (cons 1 val))))))
622 (aset vec i val)
623 (setq last-idx i last-elt elt)))
624 (if (not last-idx)
625 (setq vec nil)
626 (if (< last-idx 127)
627 (let ((shorter (make-vector (1+ last-idx) nil)))
628 (dotimes (i (1+ last-idx))
629 (aset shorter i (aref vec i)))
630 (setq vec shorter))))
631 vec))
632
633;; Encode the word index IDX into a characters code that can be
634;; embedded in a string.
635
636(defsubst unidata-encode-word (idx)
637 ;; Exclude 0, 1, 2.
638 (+ idx 3))
639
640;; Decode the character code CODE (that is embedded in a string) into
641;; the corresponding word name by looking up WORD-TABLE.
642
643(defsubst unidata-decode-word (code word-table)
644 (setq code (- code 3))
645 (if (< code (length word-table))
646 (aref word-table code)))
647
648;; Table of short transliterated name symbols of Hangul Jamo divided
649;; into Choseong, Jungseong, and Jongseong.
650
651(defconst unidata-name-jamo-name-table
652 [[G GG N D DD R M B BB S SS nil J JJ C K T P H]
653 [A AE YA YAE EO E YEO YE O WA WAE OE YO U WEO WE WI YU EU YI I]
654 [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]])
655
656;; Return a name of CHAR. VAL is the current value of (aref TABLE
657;; CHAR).
658
659(defun unidata-get-name (char val table)
dd7d19d7 660 (cond
f600cf3a
KH
661 ((stringp val)
662 (if (> (aref val 0) 0)
663 val
664 (let* ((first-char (lsh (lsh char -7) 7))
665 (word-table (aref (char-table-extra-slot table 4) 0))
666 (i 1)
667 (len (length val))
668 (vec (make-vector 128 nil))
669 (idx 0)
670 (case-fold-search nil)
671 c word-list tail-list last-list word diff-head)
672 (while (< i len)
673 (setq c (aref val i))
674 (if (< c 3)
675 (progn
676 (if (or word-list tail-list)
677 (aset vec idx
678 (setq last-list (nconc word-list tail-list))))
679 (setq i (1+ i) idx (1+ idx)
680 word-list nil tail-list nil)
681 (if (> c 0)
682 (let ((l last-list))
683 (if (= c 1)
684 (setq diff-head
685 (prog1 (aref val i) (setq i (1+ i)))))
686 (setq tail-list (nthcdr (% diff-head 16) last-list))
687 (dotimes (i (/ diff-head 16))
688 (setq word-list (nconc word-list (list (car l)))
689 l (cdr l))))))
690 (setq word-list
dd7d19d7 691 (nconc word-list
f600cf3a
KH
692 (list (symbol-name
693 (unidata-decode-word c word-table))))
694 i (1+ i))))
695 (if (or word-list tail-list)
696 (aset vec idx (nconc word-list tail-list)))
697 (setq val nil)
698 (dotimes (i 128)
699 (setq c (+ first-char i))
700 (let ((name (aref vec i)))
701 (if name
702 (let ((tail (cdr (setq name (copy-sequence name))))
703 elt)
704 (while tail
705 (setq elt (car tail))
706 (or (string= elt "-")
707 (progn
708 (setcdr tail (cons elt (cdr tail)))
709 (setcar tail " ")))
710 (setq tail (cddr tail)))
711 (setq name (apply 'concat name))))
712 (aset table c name)
713 (if (= c char)
714 (setq val name))))
0902a04e 715 (or val ""))))
f600cf3a
KH
716
717 ((and (integerp val) (> val 0))
718 (let* ((symbol-table (aref (char-table-extra-slot table 4) 1))
719 (sym (aref symbol-table (1- val))))
720 (cond ((eq sym 'HANGUL\ SYLLABLE)
721 (let ((jamo-name-table (aref (char-table-extra-slot table 4) 2)))
722 ;; SIndex = S - SBase
723 (setq char (- char #xAC00))
724 (let ( ;; LIndex = SIndex / NCount
725 (L (/ char 588))
726 ;; VIndex = (SIndex % NCount) * TCount
727 (V (/ (% char 588) 28))
728 ;; TIndex = SIndex % TCount
729 (T (% char 28)))
dd7d19d7 730 (format "HANGUL SYLLABLE %s%s%s"
752fc44b
KH
731 ;; U+110B is nil in this table.
732 (or (aref (aref jamo-name-table 0) L) "")
f600cf3a
KH
733 (aref (aref jamo-name-table 1) V)
734 (if (= T 0) ""
735 (aref (aref jamo-name-table 2) (1- T)))))))
e60b06d7
KH
736 ((eq sym 'CJK\ IDEOGRAPH)
737 (format "%s-%04X" sym char))
f600cf3a
KH
738 ((eq sym 'CJK\ COMPATIBILITY\ IDEOGRAPH)
739 (format "%s-%04X" sym char))
740 ((eq sym 'VARIATION\ SELECTOR)
0902a04e
KH
741 (format "%s-%d" sym (+ (- char #xe0100) 17))))))
742
743 (t "")))
f600cf3a
KH
744
745;; Store VAL as the name of CHAR in TABLE.
746
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)))
752
753(defun unidata-get-decomposition (char val table)
754 (cond
0902a04e
KH
755 ((not val)
756 (list char))
757
f600cf3a
KH
758 ((consp val)
759 val)
760
761 ((stringp val)
762 (if (> (aref val 0) 0)
763 val
764 (let* ((first-char (lsh (lsh char -7) 7))
765 (word-table (char-table-extra-slot table 4))
766 (i 1)
767 (len (length val))
768 (vec (make-vector 128 nil))
769 (idx 0)
770 (case-fold-search nil)
771 c word-list tail-list last-list word diff-head)
772 (while (< i len)
773 (setq c (aref val i))
774 (if (< c 3)
775 (progn
776 (if (or word-list tail-list)
777 (aset vec idx
778 (setq last-list (nconc word-list tail-list))))
779 (setq i (1+ i) idx (1+ idx)
780 word-list nil tail-list nil)
781 (if (> c 0)
782 (let ((l last-list))
783 (if (= c 1)
784 (setq diff-head
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)))
789 l (cdr l))))))
790 (setq word-list
dd7d19d7 791 (nconc word-list
f600cf3a
KH
792 (list (or (unidata-decode-word c word-table) c)))
793 i (1+ i))))
794 (if (or word-list tail-list)
795 (aset vec idx (nconc word-list tail-list)))
796 (dotimes (i 128)
797 (aset table (+ first-char i) (aref vec i)))
0902a04e
KH
798 (setq val (aref vec (- char first-char)))
799 (or val (list char)))))
f600cf3a 800
1275c1dc
KH
801 ;; Hangul syllable
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)))
0b4ff5df
KH
809 ;; LV = SBase + (SIndex / TCount) * TCount
810 (LV (+ #xAC00 (* (/ char 28) 28)))
1275c1dc
KH
811 ;; T = TBase + SIndex % TCount
812 (T (+ #x11A7 (% char 28))))
d636247a
KH
813 (if (= T #x11A7)
814 (list L V)
88758986 815 (list LV T))))
1275c1dc
KH
816
817 ))
818
819;; Store VAL as the decomposition information of CHAR in TABLE.
f600cf3a
KH
820
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)))
826
827;; UnicodeData.txt contains these lines:
828;; 0000;<control>;Cc;0;BN;;;;;N;NULL;;;;
829;; ...
830;; 0020;SPACE;Zs;0;WS;;;;;N;;;;;
831;; ...
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
835;; the size.
836
837;; Generate a char-table for character names.
838
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))
843 word-table
844 block-list block-word-table block-end
845 tail elt range val idx slot)
f600cf3a
KH
846 (setq tail unidata-list)
847 (setq block-end -1)
848 (while tail
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))
861 (second (nth 1 val))
862 (start range))
863 (while (and (setq elt (car tail) range (car elt)
864 val (funcall val-func (nth prop-idx elt)))
865 (consp val)
866 (eq first (car val))
867 (eq second (nth 1 val)))
868 (setq block-end range
869 tail (cdr tail)))
870 (setq range (cons start block-end)
871 val (if (eq first 'CJK) 'CJK\ COMPATIBILITY\ IDEOGRAPH
872 'VARIATION\ SELECTOR))))
873
874 (if (consp range)
875 (if val
876 (let ((slot (assq val block-list)))
877 (setq range (cons (car range) (cdr range)))
878 (setq block-end (cdr range))
879 (if slot
880 (nconc slot (list range))
881 (push (list val range) block-list))))
882 (let* ((start (lsh (lsh range -7) 7))
883 (limit (+ start 127))
884 (first tail)
885 (vec (make-vector 128 nil))
886 c name len)
887 (if (<= start block-end)
888 ;; START overlap with the previous block.
889 (aset table range (nth prop-idx elt))
890 (if val
891 (aset vec (- range start) val))
892 (while (and (setq elt (car tail) range (car elt))
893 (integerp range)
894 (<= range limit))
895 (setq val (funcall val-func (nth prop-idx elt)))
896 (if val
897 (aset vec (- range start) val))
898 (setq tail (cdr tail)))
899 (setq vec (unidata-word-list-compress vec))
900 (when vec
901 (dotimes (i (length vec))
902 (dolist (elt (aref vec i))
903 (if (symbolp elt)
904 (let ((slot (assq elt word-list)))
905 (if slot
906 (setcdr slot (1+ (cdr slot)))
907 (setcdr word-list
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)
913 (while tail
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))
917 (setq idx 0)
918 (dolist (elt word-list)
919 (aset word-table idx (car elt))
920 (setq idx (1+ idx)))
921
922 (if (and (eq prop 'decomposition)
923 (> idx 32))
1275c1dc 924 (error "Too many symbols in decomposition data"))
f600cf3a
KH
925
926 (dotimes (i (/ #x110000 128))
927 (let* ((idx (* i 128))
928 (vec (aref table idx)))
929 (when (vectorp vec)
930 (dotimes (i (length vec))
931 (let ((tail (aref vec i))
932 elt code)
933 (if (not tail)
934 (aset vec i "\0")
935 (while tail
936 (setq elt (car tail)
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 "")))))
945
946 (setq block-word-table (make-vector (length block-list) nil))
947 (setq idx 0)
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))
952 (setq idx (1+ idx)))
953
954 (set-char-table-extra-slot table 0 prop)
955 (set-char-table-extra-slot table 4 (cons word-table block-word-table))
956 table))
957
958(defun unidata-split-name (str)
959 (if (symbolp str)
960 str
961 (let ((len (length str))
962 (l nil)
963 (idx 0)
964 c)
965 (if (= len 0)
966 nil
967 (dotimes (i len)
968 (setq c (aref str i))
969 (if (= c 32)
970 (setq l (cons (intern (substring str idx i)) l)
971 idx (1+ i))
dd7d19d7 972 (if (and (= c ?-) (< idx i)
f600cf3a
KH
973 (< (1+ i) len) (/= (aref str (1+ i)) 32))
974 (setq l (cons '- (cons (intern (substring str idx i)) l))
975 idx (1+ i)))))
976 (nreverse (cons (intern (substring str idx)) l))))))
977
c805dec0 978(defun unidata-gen-table-name (prop &rest ignore)
f600cf3a
KH
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))
985
986 (if (eq prop 'name)
987 (set-char-table-extra-slot table 4
988 (vector (car word-tables)
989 (cdr word-tables)
990 unidata-name-jamo-name-table))
991 (set-char-table-extra-slot table 4
992 (vector (car word-tables))))
993 table))
994
995(defun unidata-split-decomposition (str)
996 (if (symbolp str)
997 str
998 (let ((len (length str))
999 (l nil)
1000 (idx 0)
1001 c)
1002 (if (= len 0)
1003 nil
1004 (dotimes (i len)
1005 (setq c (aref str i))
1006 (if (= c 32)
1007 (setq l (if (= (aref str idx) ?<)
f1478738 1008 (cons (intern (substring str (1+ idx) (1- i))) l)
1275c1dc 1009 (cons (string-to-number (substring str idx i) 16) l))
f600cf3a
KH
1010 idx (1+ i))))
1011 (if (= (aref str idx) ?<)
f1478738 1012 (setq l (cons (intern (substring str (1+ idx) (1- len))) l))
1275c1dc 1013 (setq l (cons (string-to-number (substring str idx len) 16) l)))
f600cf3a
KH
1014 (nreverse l)))))
1015
1016
c805dec0 1017(defun unidata-gen-table-decomposition (prop &rest ignore)
f600cf3a
KH
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))
dd7d19d7 1024 (set-char-table-extra-slot table 2
f600cf3a
KH
1025 (symbol-function 'unidata-put-decomposition))
1026 (set-char-table-extra-slot table 4 (car word-tables))
1027 table))
1028
1029
1030\f
1031(defun unidata-describe-general-category (val)
1032 (cdr (assq val
c805dec0
KH
1033 '((nil . "Uknown")
1034 (Lu . "Letter, Uppercase")
f600cf3a
KH
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")))))
1064
1065(defun unidata-describe-canonical-combining-class (val)
1066 (cdr (assq val
1067 '((0 . "Spacing, split, enclosing, reordrant, and Tibetan subjoined")
1068 (1 . "Overlays and interior")
1069 (7 . "Nuktas")
1070 (8 . "Hiragana/Katakana voicing marks")
1071 (9 . "Viramas")
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")
1083 (220 . "Below")
1084 (222 . "Below right")
1085 (224 . "Left (reordrant around single base character)")
1086 (226 . "Right")
1087 (228 . "Above left")
1088 (230 . "Above")
1089 (232 . "Above right")
1090 (233 . "Double below")
1091 (234 . "Double above")
1092 (240 . "Below (iota subscript)")))))
1093
1094(defun unidata-describe-bidi-class (val)
1095 (cdr (assq 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")
1113 (WS . "Whitespace")
1114 (ON . "Other Neutrals")))))
1115
1116(defun unidata-describe-decomposition (val)
0af3fedf
KH
1117 (mapconcat
1118 #'(lambda (x)
1119 (if (symbolp x) (symbol-name x)
1120 (concat (string ?')
1121 (compose-string (string x) 0 1 (string ?\t x ?\t))
1122 (string ?'))))
1123 val " "))
f600cf3a 1124
c805dec0
KH
1125(defun unidata-gen-mirroring-list ()
1126 (let ((head (list nil))
1127 tail)
1128 (with-temp-buffer
1129 (insert-file-contents (expand-file-name "BidiMirroring.txt" unidata-dir))
1130 (goto-char (point-min))
1131 (setq tail head)
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)))))))
1136 (cdr head)))
1137
f600cf3a
KH
1138;; Verify if we can retrieve correct values from the generated
1139;; char-tables.
1140
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))
dd7d19d7 1146 (table (progn
f600cf3a
KH
1147 (message "Generating %S table..." prop)
1148 (funcall generator prop)))
1149 (decoder (char-table-extra-slot table 1))
1150 (check #x400))
1151 (dolist (e unidata-list)
1152 (let ((char (car e))
1153 (val1 (nth index e))
1154 val2)
1155 (if (and (stringp val1) (= (length val1) 0))
1156 (setq val1 nil))
1157 (unless (consp char)
1158 (setq val2 (funcall decoder char (aref table char) table))
1159 (if val1
1160 (cond ((eq generator 'unidata-gen-table-symbol)
1161 (setq val1 (intern val1)))
1162 ((eq generator 'unidata-gen-table-integer)
1275c1dc 1163 (setq val1 (string-to-number val1)))
f600cf3a 1164 ((eq generator 'unidata-gen-table-character)
1275c1dc 1165 (setq val1 (string-to-number val1 16)))
f600cf3a
KH
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)
dd7d19d7 1172 (insert (format "> %04X %S\n< %04X %S\n"
f600cf3a
KH
1173 char val1 char val2)))
1174 (sit-for 0)))))))
1175
1176;; The entry function. It generates files described in the header
1177;; comment of this file.
1178
c805dec0
KH
1179(defun unidata-gen-files (&optional data-dir unidata-text-file)
1180 (or data-dir
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)
3921ea21 1184 command-line-args-left (cdr command-line-args-left)))
db516472 1185 (let ((coding-system-for-write 'utf-8-unix)
c805dec0
KH
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)
f600cf3a
KH
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))
c805dec0
KH
1202 (default-value (unidata-prop-default prop))
1203 (val-list (unidata-prop-val-list prop))
f600cf3a
KH
1204 table)
1205 ;; Filename in this comment line is extracted by sed in
1206 ;; Makefile.
1207 (insert (format ";; FILE: %s\n" file))
1208 (insert (format "(define-char-code-property '%S %S\n %S)\n"
1209 prop file docstring))
c805dec0 1210 (with-temp-buffer
f600cf3a 1211 (message "Generating %s..." file)
c805dec0
KH
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))
f600cf3a
KH
1217 (when describer
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))
c805dec0
KH
1222 (if (bobp)
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))
1229 (if (eobp)
1230 (insert ";; Local Variables:\n"
1231 ";; coding: utf-8\n"
1232 ";; no-byte-compile: t\n"
1233 ";; End:\n\n"
1234 (format ";; %s ends here\n" file)))
1235 (write-file file)
1236 (message "Generating %s...done" file))))
f600cf3a 1237 (message "Writing %s..." charprop-file)
1275c1dc 1238 (insert ";; Local Variables:\n"
f600cf3a
KH
1239 ";; coding: utf-8\n"
1240 ";; no-byte-compile: t\n"
1241 ";; End:\n\n"
1242 (format ";; %s ends here\n" charprop-file)))))
1243
1275c1dc
KH
1244\f
1245
f600cf3a 1246;;; unidata-gen.el ends here