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