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