1 ;;; char-db-util.el --- Character Database utility
3 ;; Copyright (C) 1998,1999,2000,2001 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 (t (if (>= (charset-iso-final-char kb) ?0)
99 (> (charset-id ka)(charset-id kb))))))
100 ((<= (charset-chars ka)(charset-chars kb)))))
102 (< (charset-dimension ka)
103 (charset-dimension kb))
113 (string< (symbol-name ka)
119 (defun char-db-insert-alist (alist &optional readable column)
121 (setq column (current-column)))
123 (concat "\n" (make-string (1+ column) ?\ )))
126 lbs cell rest separator)
129 (setq name (car (car alist))
130 value (cdr (car alist)))
131 (cond ((eq name 'char)
133 (if (setq ret (condition-case nil
140 (setq key (car (car value)))
141 (if (find-charset key)
142 (setq cal (cons key cal))
143 (setq al (cons key al)))
144 (setq value (cdr value)))
145 (insert-char-attributes ret
148 (insert (prin1-to-string value)))
150 (insert line-breaking))
152 (insert (format "(%-18s " name))
153 (setq lbs (concat "\n" (make-string (current-column) ?\ )))
155 (setq cell (car value))
156 (if (and (consp cell)
158 (setq ret (condition-case nil
167 (setq key (car (car rest)))
168 (if (find-charset key)
169 (setq cal (cons key cal))
170 (setq al (cons key al)))
171 (setq rest (cdr rest)))
174 (insert-char-attributes ret
177 (setq separator lbs))
180 (insert (prin1-to-string cell))
181 (setq separator " "))
182 (setq value (cdr value)))
184 (insert line-breaking))
186 (insert (format "(%-18s . %S)%s"
189 (setq alist (cdr alist))))
192 (defun insert-char-attributes (char &optional readable
193 attributes ccs-attributes
197 (if (consp attributes)
198 (copy-sequence attributes))
199 (char-attribute-list))
200 #'char-attribute-name<))
202 (sort (if ccs-attributes
203 (copy-sequence ccs-attributes)
205 #'char-attribute-name<))
207 (setq column (current-column)))
208 (let (name value has-long-ccs-name rest
211 (concat "\n" (make-string (1+ column) ?\ )))
212 lbs cell separator ret
215 (when (and (memq 'name attributes)
216 (setq value (get-char-attribute char 'name)))
218 (if (> (length value) 47)
221 value line-breaking))
222 (setq attributes (delq 'name attributes))
224 (when (and (memq 'script attributes)
225 (setq value (get-char-attribute char 'script)))
226 (insert (format "(script\t\t%s)%s"
227 (mapconcat (function prin1-to-string)
230 (setq attributes (delq 'script attributes))
232 (when (and (memq '->ucs attributes)
233 (setq value (get-char-attribute char '->ucs)))
234 (insert (format "(->ucs\t\t. #x%04X)\t; %c%s"
235 value (decode-char 'ucs value)
237 (setq attributes (delq '->ucs attributes))
239 (when (and (memq 'general-category attributes)
240 (setq value (get-char-attribute char 'general-category)))
242 "(general-category\t%s) ; %s%s"
243 (mapconcat (lambda (cell)
246 (cond ((rassoc value unidata-normative-category-alist)
247 "Normative Category")
248 ((rassoc value unidata-informative-category-alist)
249 "Informative Category")
253 (setq attributes (delq 'general-category attributes))
255 (when (and (memq 'bidi-category attributes)
256 (setq value (get-char-attribute char 'bidi-category)))
257 (insert (format "(bidi-category\t. %S)%s"
260 (setq attributes (delq 'bidi-category attributes))
262 (unless (or (not (memq 'mirrored attributes))
263 (eq (setq value (get-char-attribute char 'mirrored 'empty))
265 (insert (format "(mirrored\t\t. %S)%s"
268 (setq attributes (delq 'mirrored attributes))
271 ((and (memq 'decimal-digit-value attributes)
272 (setq value (get-char-attribute char 'decimal-digit-value)))
273 (insert (format "(decimal-digit-value . %S)%s"
276 (setq attributes (delq 'decimal-digit-value attributes))
277 (when (and (memq 'digit-value attributes)
278 (setq value (get-char-attribute char 'digit-value)))
279 (insert (format "(digit-value\t . %S)%s"
282 (setq attributes (delq 'digit-value attributes))
284 (when (and (memq 'numeric-value attributes)
285 (setq value (get-char-attribute char 'numeric-value)))
286 (insert (format "(numeric-value\t . %S)%s"
289 (setq attributes (delq 'numeric-value attributes))
293 (when (and (memq 'digit-value attributes)
294 (setq value (get-char-attribute char 'digit-value)))
295 (insert (format "(digit-value\t. %S)%s"
298 (setq attributes (delq 'digit-value attributes))
300 (when (and (memq 'numeric-value attributes)
301 (setq value (get-char-attribute char 'numeric-value)))
302 (insert (format "(numeric-value\t. %S)%s"
305 (setq attributes (delq 'numeric-value attributes))
307 (when (and (memq 'iso-10646-comment attributes)
308 (setq value (get-char-attribute char 'iso-10646-comment)))
309 (insert (format "(iso-10646-comment\t. %S)%s"
312 (setq attributes (delq 'iso-10646-comment attributes))
314 (when (and (memq 'morohashi-daikanwa attributes)
315 (setq value (get-char-attribute char 'morohashi-daikanwa)))
316 (insert (format "(morohashi-daikanwa\t%s)%s"
317 (mapconcat (function prin1-to-string) value " ")
319 (setq attributes (delq 'morohashi-daikanwa attributes))
323 (when (and (memq 'ideographic-radical attributes)
324 (setq value (get-char-attribute char 'ideographic-radical)))
326 (insert (format "(ideographic-radical . %S)\t; %c%s"
328 (aref ideographic-radicals radical)
330 (setq attributes (delq 'ideographic-radical attributes))
332 (when (and (memq 'ideographic-strokes attributes)
333 (setq value (get-char-attribute char 'ideographic-strokes)))
335 (insert (format "(ideographic-strokes . %S)%s"
338 (setq attributes (delq 'ideographic-strokes attributes))
340 (when (and (memq 'kangxi-radical attributes)
341 (setq value (get-char-attribute char 'kangxi-radical)))
342 (unless (eq value radical)
343 (insert (format "(kangxi-radical\t . %S)\t; %c%s"
345 (aref ideographic-radicals value)
348 (setq radical value)))
349 (setq attributes (delq 'kangxi-radical attributes))
351 (when (and (memq 'kangxi-strokes attributes)
352 (setq value (get-char-attribute char 'kangxi-strokes)))
353 (unless (eq value strokes)
354 (insert (format "(kangxi-strokes\t . %S)%s"
358 (setq strokes value)))
359 (setq attributes (delq 'kangxi-strokes attributes))
361 (when (and (memq 'japanese-radical attributes)
362 (setq value (get-char-attribute char 'japanese-radical)))
363 (unless (eq value radical)
364 (insert (format "(japanese-radical\t . %S)\t; %c%s"
366 (aref ideographic-radicals value)
369 (setq radical value)))
370 (setq attributes (delq 'japanese-radical attributes))
372 (when (and (memq 'japanese-strokes attributes)
373 (setq value (get-char-attribute char 'japanese-strokes)))
374 (unless (eq value strokes)
375 (insert (format "(japanese-strokes\t . %S)%s"
379 (setq strokes value)))
380 (setq attributes (delq 'japanese-strokes attributes))
382 (when (and (memq 'cns-radical attributes)
383 (setq value (get-char-attribute char 'cns-radical)))
384 (insert (format "(cns-radical\t . %S)\t; %c%s"
386 (aref ideographic-radicals value)
388 (setq attributes (delq 'cns-radical attributes))
390 (when (and (memq 'cns-strokes attributes)
391 (setq value (get-char-attribute char 'cns-strokes)))
392 (unless (eq value strokes)
393 (insert (format "(cns-strokes\t . %S)%s"
397 (setq strokes value)))
398 (setq attributes (delq 'cns-strokes attributes))
400 (when (and (memq 'shinjigen-1-radical attributes)
401 (setq value (get-char-attribute char 'shinjigen-1-radical)))
402 (unless (eq value radical)
403 (insert (format "(shinjigen-1-radical . %S)\t; %c%s"
405 (aref ideographic-radicals value)
408 (setq radical value)))
409 (setq attributes (delq 'shinjigen-1-radical attributes))
411 (when (and (memq 'total-strokes attributes)
412 (setq value (get-char-attribute char 'total-strokes)))
413 (insert (format "(total-strokes . %S)%s"
416 (setq attributes (delq 'total-strokes attributes))
418 (when (and (memq '->ideograph attributes)
419 (setq value (get-char-attribute char '->ideograph)))
420 (insert (format "(->ideograph\t%s)%s"
421 (mapconcat (lambda (code)
422 (cond ((symbolp code)
425 (format "#x%04X" code))
427 (format "%s%S" line-breaking code))))
430 (setq attributes (delq '->ideograph attributes))
432 (when (and (memq '->decomposition attributes)
433 (setq value (get-char-attribute char '->decomposition)))
434 (insert (format "(->decomposition\t%s)%s"
435 (mapconcat (lambda (code)
436 (cond ((symbolp code)
445 (format "#x%04X" code))
447 (format "%s%S" line-breaking code))))
450 (setq attributes (delq '->decomposition attributes))
452 (when (and (memq '->uppercase attributes)
453 (setq value (get-char-attribute char '->uppercase)))
454 (insert (format "(->uppercase\t%s)%s"
455 (mapconcat (lambda (code)
456 (cond ((symbolp code)
459 (format "#x%04X" code))
462 line-breaking code))))
465 (setq attributes (delq '->uppercase attributes))
467 (when (and (memq '->lowercase attributes)
468 (setq value (get-char-attribute char '->lowercase)))
469 (insert (format "(->lowercase\t%s)%s"
470 (mapconcat (lambda (code)
471 (cond ((symbolp code)
474 (format "#x%04X" code))
477 line-breaking code))))
480 (setq attributes (delq '->lowercase attributes))
482 (when (and (memq '->titlecase attributes)
483 (setq value (get-char-attribute char '->titlecase)))
484 (insert (format "(->titlecase\t%s)%s"
485 (mapconcat (lambda (code)
486 (cond ((symbolp code)
489 (format "#x%04X" code))
492 line-breaking code))))
495 (setq attributes (delq '->titlecase attributes))
497 (when (and (memq '->mojikyo attributes)
498 (setq value (get-char-attribute char '->mojikyo)))
499 (insert (format "(->mojikyo\t\t. %06d)\t; %c%s"
500 value (decode-char 'mojikyo value)
502 (setq attributes (delq '->mojikyo attributes))
504 (setq rest ccs-attributes)
507 (setq value (get-char-attribute char (car rest)))
509 (if (>= (length (symbol-name (car rest))) 19)
511 (setq has-long-ccs-name t)
515 (setq rest (cdr rest)))
517 (setq name (car attributes))
518 (if (setq value (get-char-attribute char name))
519 (cond ((eq name 'jisx0208-1978/4X)
520 (insert (format "(%-18s . #x%04X)%s"
523 ((string-match "^->" (symbol-name name))
525 (format "(%-18s %s)%s"
527 (mapconcat (lambda (code)
528 (cond ((symbolp code)
531 (format "#x%04X" code))
534 line-breaking code))))
537 ((memq name '(ideograph=
538 original-ideograph-of
542 simplified-ideograph-of
544 ideographic-different-form-of))
545 (insert (format "(%-18s%s " name line-breaking))
546 (setq lbs (concat "\n" (make-string (current-column) ?\ ))
549 (setq cell (car value))
550 (if (and (consp cell)
555 (char-db-insert-alist cell readable)
556 (setq separator lbs))
559 (insert (prin1-to-string cell))
560 (setq separator " "))
561 (setq value (cdr value)))
563 (insert line-breaking))
565 (insert (format "(%-18s " name))
566 (setq lbs (concat "\n" (make-string (current-column) ?\ ))
569 (setq cell (car value))
570 (if (and (consp cell)
572 (setq ret (condition-case nil
580 (setq key (car (car rest)))
581 (if (find-charset key)
582 (setq cal (cons key cal))
583 (setq al (cons key al)))
584 (setq rest (cdr rest)))
587 (insert-char-attributes ret
590 (setq separator lbs))
593 (insert (prin1-to-string cell))
594 (setq separator " "))
595 (setq value (cdr value)))
597 (insert line-breaking))
599 (insert (format "(%-18s . %S)%s"
603 (setq attributes (cdr attributes)))
604 (while ccs-attributes
605 (setq name (car ccs-attributes))
606 (if (and (eq name (charset-name name))
607 (setq value (get-char-attribute char name)))
610 (cond ((memq name '(ideograph-daikanwa ideograph-gt))
611 (if has-long-ccs-name
612 "(%-26s . %05d)\t; %c%s"
613 "(%-18s . %05d)\t; %c%s"))
615 (if has-long-ccs-name
616 "(%-26s . %06d)\t; %c%s"
617 "(%-18s . %06d)\t; %c%s"))
619 (if has-long-ccs-name
620 "(%-26s . #x%X)\t; %c%s"
621 "(%-18s . #x%X)\t; %c%s")))
623 (if (= (charset-iso-graphic-plane name) 1)
625 (cond ((= (charset-dimension name) 1)
627 ((= (charset-dimension name) 2)
629 ((= (charset-dimension name) 3)
633 (if (and (memq name '(ideograph-gt-pj-1
644 (setq ret (decode-char name value))
645 (setq ret (get-char-attribute ret 'ideograph-gt)))
646 (decode-builtin-char 'ideograph-gt ret)
647 (decode-builtin-char name value))
649 (setq ccs-attributes (cdr ccs-attributes)))
652 (defun insert-char-data (char &optional readable
653 attributes ccs-attributes)
655 (narrow-to-region (point)(point))
656 (insert "(define-char
658 (insert-char-attributes char readable
659 attributes ccs-attributes)
661 (goto-char (point-min))
662 (while (re-search-forward "[ \t]+$" nil t)
664 (goto-char (point-max))
665 (tabify (point-min)(point-max))
669 (defun char-db-update-comment ()
672 (goto-char (point-min))
673 (let (cdef table char)
674 (while (re-search-forward "^[ \t]*\\(([^.()]+)\\)" nil t)
675 (goto-char (match-beginning 1))
676 (setq cdef (read (current-buffer)))
677 (when (find-charset (car cdef))
678 (goto-char (match-end 0))
681 (not (eq (car cdef) 'ideograph-daikanwa))
682 (or (memq (car cdef) '(ascii latin-viscii-upper
686 japanese-jisx0213-2))
687 (= (char-int (charset-iso-final-char (car cdef)))
689 (apply (function make-char) cdef)
690 (if (setq table (charset-mapping-table (car cdef)))
691 (set-charset-mapping-table (car cdef) nil))
693 (apply (function make-char) cdef)
695 (set-charset-mapping-table (car cdef) table)))))
696 (when (not (or (< (char-int char) 32)
697 (and (<= 128 (char-int char))
698 (< (char-int char) 160))))
699 (delete-region (point) (point-at-eol))
700 (insert (format "\t; %c" char)))
703 (defun insert-char-data-with-variant (char &optional script printable
705 (insert-char-data char printable)
706 (let ((variants (or (char-variants char)
707 (let ((ucs (get-char-attribute char '->ucs)))
709 (delete char (char-variants (int-char ucs)))))))
712 (setq variant (car variants))
713 (if (or (null script)
714 (null (setq vs (get-char-attribute variant 'script)))
716 (or (and no-ucs-variant (get-char-attribute variant 'ucs))
717 (insert-char-data variant printable)))
718 (setq variants (cdr variants))
721 (defun insert-char-range-data (min max &optional script)
725 (setq char (decode-char 'ucs code))
726 (if (get-char-attribute char 'ucs)
727 (insert-char-data-with-variant char script nil 'no-ucs-variant))
728 (setq code (1+ code))
731 (defun write-char-range-data-to-file (min max file &optional script)
732 (let ((coding-system-for-write 'utf-8))
734 (insert-char-range-data min max script)
735 (write-region (point-min)(point-max) file))))
737 (defvar what-character-original-window-configuration)
740 (defun what-char-definition (char)
741 (interactive (list (char-after)))
742 (let ((buf (get-buffer-create "*Character Description*"))
743 (the-buf (current-buffer))
744 (win-conf (current-window-configuration)))
746 (make-local-variable 'what-character-original-window-configuration)
747 (setq what-character-original-window-configuration win-conf)
748 (setq buffer-read-only nil)
752 (insert-char-data-with-variant char nil 'printable)
753 ;; (char-db-update-comment)
754 (set-buffer-modified-p nil)
755 (view-mode the-buf (lambda (buf)
756 (set-window-configuration
757 what-character-original-window-configuration)
759 (goto-char (point-min)))
761 (set-window-configuration
762 what-character-original-window-configuration)
763 (signal (car err) (cdr err)))))))
765 (provide 'char-db-util)
767 ;;; char-db-util.el ends here