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