1 ;;; char-db-util.el --- Character Database utility
3 ;; Copyright (C) 1998,1999,2000 MORIOKA Tomohiko.
5 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
6 ;; Keywords: UTF-2000, ISO/IEC 10646, Unicode, UCS-4, MULE.
8 ;; This file is part of UTF-2000.
10 ;; UTF-2000 is free software; you can redistribute it and/or modify it
11 ;; under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; UTF-2000 is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with XEmacs; see the file COPYING. If not, write to the Free
22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
29 (defconst unidata-normative-category-alist
30 '(("Lu" letter uppercase)
31 ("Ll" letter lowercase)
32 ("Lt" letter titlecase)
33 ("Mn" mark non-spacing)
34 ("Mc" mark spacing-combining)
36 ("Nd" number decimal-digit)
39 ("Zs" separator space)
41 ("Zp" separator paragraph)
44 ("Cs" other surrogate)
45 ("Co" other private-use)
46 ("Cn" other not-assigned)))
48 (defconst unidata-informative-category-alist
49 '(("Lm" letter modifier)
51 ("Pc" punctuation connector)
52 ("Pd" punctuation dash)
53 ("Ps" punctuation open)
54 ("Pe" punctuation close)
55 ("Pi" punctuation initial-quote)
56 ("Pf" punctuation final-quote)
57 ("Po" punctuation other)
59 ("Sc" symbol currency)
60 ("Sk" symbol modifier)
64 (defconst ideographic-radicals
65 (let ((v (make-vector 215 nil))
68 (aset v i (int-char (+ #x2EFF i)))
70 (if (< (charset-iso-final-char (car (split-char (aref v 34)))) ?0)
71 (aset v 34 (make-char 'chinese-gb2312 #x62 #x3A)))
74 (defun char-attribute-name< (ka kb)
80 ((= (charset-dimension ka)
81 (charset-dimension kb))
82 (cond ((= (charset-chars ka)(charset-chars kb))
84 ((>= (charset-iso-final-char ka) ?@)
85 (if (>= (charset-iso-final-char kb) ?@)
86 (< (charset-iso-final-char ka)
87 (charset-iso-final-char kb))
89 ((>= (charset-iso-final-char ka) ?0)
91 ((>= (charset-iso-final-char kb) ?@)
93 ((>= (charset-iso-final-char kb) ?0)
94 (< (charset-iso-final-char ka)
95 (charset-iso-final-char kb)))
97 ((<= (charset-chars ka)(charset-chars kb)))))
99 (< (charset-dimension ka)
100 (charset-dimension kb))
110 (string< (symbol-name ka)
116 (defun insert-char-data (char &optional readable
117 attributes ccs-attributes)
120 (copy-sequence attributes)
121 (sort (char-attribute-list) #'char-attribute-name<)))
124 (copy-sequence ccs-attributes)
125 (sort (charset-list) #'char-attribute-name<)))
126 (let (name value has-long-ccs-name rest
129 (narrow-to-region (point)(point))
130 (insert "(define-char
132 (when (setq value (get-char-attribute char 'name))
134 (if (> (length value) 47)
140 (setq attributes (delq 'name attributes))
142 (when (setq value (get-char-attribute char 'script))
143 (insert (format "(script\t\t%s)
145 (mapconcat (function prin1-to-string)
147 (setq attributes (delq 'script attributes))
149 (when (setq value (get-char-attribute char '->ucs))
150 (insert (format "(->ucs\t\t. #x%04X)\t; %c
152 value (decode-char 'ucs value)))
153 (setq attributes (delq '->ucs attributes))
155 (when (setq value (get-char-attribute char 'general-category))
157 "(general-category\t%s) ; %s
159 (mapconcat (lambda (cell)
162 (cond ((rassoc value unidata-normative-category-alist)
163 "Normative Category")
164 ((rassoc value unidata-informative-category-alist)
165 "Informative Category")
167 "Unknown Category"))))
168 (setq attributes (delq 'general-category attributes))
170 (when (setq value (get-char-attribute char 'bidi-category))
171 (insert (format "(bidi-category\t. %S)
174 (setq attributes (delq 'bidi-category attributes))
176 (unless (eq (setq value (get-char-attribute char 'mirrored 'empty))
178 (insert (format "(mirrored\t\t. %S)
181 (setq attributes (delq 'mirrored attributes))
184 ((setq value (get-char-attribute char 'decimal-digit-value))
185 (insert (format "(decimal-digit-value . %S)
188 (setq attributes (delq 'decimal-digit-value attributes))
189 (when (setq value (get-char-attribute char 'digit-value))
190 (insert (format "(digit-value\t . %S)
193 (setq attributes (delq 'digit-value attributes))
195 (when (setq value (get-char-attribute char 'numeric-value))
196 (insert (format "(numeric-value\t . %S)
199 (setq attributes (delq 'numeric-value attributes))
203 (when (setq value (get-char-attribute char 'digit-value))
204 (insert (format "(digit-value\t. %S)
207 (setq attributes (delq 'digit-value attributes))
209 (when (setq value (get-char-attribute char 'numeric-value))
210 (insert (format "(numeric-value\t. %S)
213 (setq attributes (delq 'numeric-value attributes))
215 (when (setq value (get-char-attribute char 'iso-10646-comment))
216 (insert (format "(iso-10646-comment\t. %S)
219 (setq attributes (delq 'iso-10646-comment attributes))
221 (when (setq value (get-char-attribute char 'morohashi-daikanwa))
222 (insert (format "(morohashi-daikanwa\t%s)
224 (mapconcat (function prin1-to-string) value " ")))
225 (setq attributes (delq 'morohashi-daikanwa attributes))
229 (when (setq value (get-char-attribute char 'ideographic-radical))
231 (insert (format "(ideographic-radical . %S)\t; %c
234 (aref ideographic-radicals radical)))
235 (setq attributes (delq 'ideographic-radical attributes))
237 (when (setq value (get-char-attribute char 'ideographic-strokes))
239 (insert (format "(ideographic-strokes . %S)
242 (setq attributes (delq 'ideographic-strokes attributes))
244 (when (setq value (get-char-attribute char 'kangxi-radical))
245 (unless (eq value radical)
246 (insert (format "(kangxi-radical\t . %S)\t; %c
249 (aref ideographic-radicals value)))
251 (setq radical value)))
252 (setq attributes (delq 'kangxi-radical attributes))
254 (when (setq value (get-char-attribute char 'kangxi-strokes))
255 (unless (eq value strokes)
256 (insert (format "(kangxi-strokes\t . %S)
260 (setq strokes value)))
261 (setq attributes (delq 'kangxi-strokes attributes))
263 (when (setq value (get-char-attribute char 'japanese-radical))
264 (unless (eq value radical)
265 (insert (format "(japanese-radical\t . %S)\t; %c
268 (aref ideographic-radicals value)))
270 (setq radical value)))
271 (setq attributes (delq 'japanese-radical attributes))
273 (when (setq value (get-char-attribute char 'japanese-strokes))
274 (unless (eq value strokes)
275 (insert (format "(japanese-strokes\t . %S)
279 (setq strokes value)))
280 (setq attributes (delq 'japanese-strokes attributes))
282 (when (setq value (get-char-attribute char 'cns-radical))
283 (insert (format "(cns-radical\t . %S)\t; %c
286 (aref ideographic-radicals value)))
287 (setq attributes (delq 'cns-radical attributes))
289 (when (setq value (get-char-attribute char 'cns-strokes))
290 (unless (eq value strokes)
291 (insert (format "(cns-strokes\t . %S)
295 (setq strokes value)))
296 (setq attributes (delq 'cns-strokes attributes))
298 (when (setq value (get-char-attribute char 'shinjigen-1-radical))
299 (unless (eq value radical)
300 (insert (format "(shinjigen-1-radical . %S)\t; %c
303 (aref ideographic-radicals value)))
305 (setq radical value)))
306 (setq attributes (delq 'shinjigen-1-radical attributes))
308 (when (setq value (get-char-attribute char 'total-strokes))
309 (insert (format "(total-strokes\t . %S)
312 (setq attributes (delq 'total-strokes attributes))
314 (when (setq value (get-char-attribute char '->ideograph))
315 (insert (format "(->ideograph\t%s)
317 (mapconcat (lambda (code)
318 (cond ((symbolp code)
321 (format "#x%04X" code))
323 (format "\n %S" code))))
325 (setq attributes (delq '->ideograph attributes))
327 (when (setq value (get-char-attribute char '->decomposition))
328 (insert (format "(->decomposition\t%s)
330 (mapconcat (lambda (code)
331 (cond ((symbolp code)
340 (format "#x%04X" code))
342 (format "\n %S" code))))
344 (setq attributes (delq '->decomposition attributes))
346 (when (setq value (get-char-attribute char '->uppercase))
347 (insert (format "(->uppercase\t%s)
349 (mapconcat (lambda (code)
350 (cond ((symbolp code)
353 (format "#x%04X" code))
355 (format "\n %S" code))))
357 (setq attributes (delq '->uppercase attributes))
359 (when (setq value (get-char-attribute char '->lowercase))
360 (insert (format "(->lowercase\t%s)
362 (mapconcat (lambda (code)
363 (cond ((symbolp code)
366 (format "#x%04X" code))
368 (format "\n %S" code))))
370 (setq attributes (delq '->lowercase attributes))
372 (when (setq value (get-char-attribute char '->titlecase))
373 (insert (format "(->titlecase\t%s)
375 (mapconcat (lambda (code)
376 (cond ((symbolp code)
379 (format "#x%04X" code))
381 (format "\n %S" code))))
383 (setq attributes (delq '->titlecase attributes))
385 (when (setq value (get-char-attribute char '->mojikyo))
386 (insert (format "(->mojikyo\t\t. %06d)\t; %c
388 value (decode-char 'mojikyo value)))
389 (setq attributes (delq '->mojikyo attributes))
391 (setq rest ccs-attributes)
394 (setq value (get-char-attribute char (car rest)))
396 (if (>= (length (symbol-name (car rest))) 19)
398 (setq has-long-ccs-name t)
402 (setq rest (cdr rest)))
404 (setq name (car attributes))
405 (if (setq value (get-char-attribute char name))
406 (cond ((string-match "^->" (symbol-name name))
411 (mapconcat (lambda (code)
412 (cond ((symbolp code)
415 (format "#x%04X" code))
417 (format "\n %S" code))))
420 (insert (format "(%-18s %s)
423 (mapconcat (function prin1-to-string)
425 ((eq name 'jisx0208-1978/4X)
426 (insert (format "(%-18s . #x%04X)
430 (insert (format "(%-18s . %S)
434 (setq attributes (cdr attributes)))
435 (while ccs-attributes
436 (setq name (car ccs-attributes))
437 (if (setq value (get-char-attribute char name))
440 (if has-long-ccs-name
441 (cond ((eq name 'ideograph-daikanwa)
442 "(%-26s . %05d)\t; %c
446 "(%-26s . %06d)\t; %c
450 "(%-26s . #x%X)\t; %c
453 (cond ((eq name 'ideograph-daikanwa)
454 "(%-18s . %05d)\t; %c
458 "(%-18s . %06d)\t; %c
462 "(%-18s . #x%X)\t; %c
466 (if (= (charset-iso-graphic-plane name) 1)
468 (cond ((= (charset-dimension name) 1)
470 ((= (charset-dimension name) 2)
472 ((= (charset-dimension name) 3)
476 (decode-builtin-char name value))))
477 (setq ccs-attributes (cdr ccs-attributes)))
479 (goto-char (point-min))
480 (while (re-search-forward "[ \t]+$" nil t)
482 (goto-char (point-max))
483 (tabify (point-min)(point-max))
487 (defun char-db-update-comment ()
490 (goto-char (point-min))
491 (let (cdef table char)
492 (while (re-search-forward "^[ \t]*\\(([^.()]+)\\)" nil t)
493 (goto-char (match-beginning 1))
494 (setq cdef (read (current-buffer)))
495 (when (find-charset (car cdef))
496 (goto-char (match-end 0))
499 (not (eq (car cdef) 'ideograph-daikanwa))
500 (or (memq (car cdef) '(ascii latin-viscii-upper
504 japanese-jisx0213-2))
505 (= (char-int (charset-iso-final-char (car cdef)))
507 (apply (function make-char) cdef)
508 (if (setq table (charset-mapping-table (car cdef)))
509 (set-charset-mapping-table (car cdef) nil))
511 (apply (function make-char) cdef)
513 (set-charset-mapping-table (car cdef) table)))))
514 (when (not (or (< (char-int char) 32)
515 (and (<= 128 (char-int char))
516 (< (char-int char) 160))))
517 (delete-region (point) (point-at-eol))
518 (insert (format "\t; %c" char)))
521 (defun insert-char-data-with-variant (char &optional script printable
523 (insert-char-data char printable)
524 (let ((variants (or (char-variants char)
525 (let ((ucs (get-char-attribute char '->ucs)))
527 (delete char (char-variants (int-char ucs)))))))
530 (setq variant (car variants))
531 (if (or (null script)
532 (null (setq vs (get-char-attribute variant 'script)))
534 (or (and no-ucs-variant (get-char-attribute variant 'ucs))
535 (insert-char-data variant printable)))
536 (setq variants (cdr variants))
539 (defun insert-char-range-data (min max &optional script)
543 (setq char (decode-char 'ucs code))
544 (if (get-char-attribute char 'ucs)
545 (insert-char-data-with-variant char script nil 'no-ucs-variant))
546 (setq code (1+ code))
549 (defun write-char-range-data-to-file (min max file &optional script)
550 (let ((coding-system-for-write 'utf-8))
552 (insert-char-range-data min max script)
553 (write-region (point-min)(point-max) file))))
555 (defvar what-character-original-window-configuration)
558 (defun what-char-definition (char)
559 (interactive (list (char-after)))
560 (let ((buf (get-buffer-create "*Character Description*"))
561 (the-buf (current-buffer))
562 (win-conf (current-window-configuration)))
564 (make-local-variable 'what-character-original-window-configuration)
565 (setq what-character-original-window-configuration win-conf)
566 (setq buffer-read-only nil)
570 (insert-char-data-with-variant char nil 'printable)
571 ;; (char-db-update-comment)
572 (set-buffer-modified-p nil)
573 (view-mode the-buf (lambda (buf)
574 (set-window-configuration
575 what-character-original-window-configuration)
577 (goto-char (point-min)))
579 (set-window-configuration
580 what-character-original-window-configuration)
581 (signal (car err) (cdr err)))))))
583 (provide 'char-db-util)
585 ;;; char-db-util.el ends here