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