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