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