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