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