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 XEmacs UTF-2000.
10 ;; XEmacs UTF-2000 is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
15 ;; XEmacs UTF-2000 is distributed in the hope that it will be useful,
16 ;; but 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 UTF-2000; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
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-char-spec (char &optional readable column)
121 (setq column (current-column)))
122 (let (char-spec ret al cal key temp-char)
123 (cond ((characterp char)
124 (cond ((setq ret (get-char-attribute char 'ucs))
125 (unless (and (<= #xE000 ret)(<= ret #xF8FF))
126 (setq char-spec (list (cons 'ucs ret))))
127 (if (setq ret (get-char-attribute char 'chinese-big5-cdp))
128 (setq char-spec (cons (cons 'chinese-big5-cdp ret)
130 (if (setq ret (get-char-attribute char 'name))
131 (setq char-spec (cons (cons 'name ret) char-spec)))
133 ((setq ret (split-char char))
134 (setq char-spec (list ret))
135 (dolist (ccs (delq (car ret) (charset-list)))
136 (if (and (>= (charset-iso-final-char ccs) ?0)
137 (setq ret (get-char-attribute char ccs)))
138 (setq char-spec (cons (cons ccs ret) char-spec))))
139 (if (setq ret (get-char-attribute char 'name))
140 (setq char-spec (cons (cons 'name ret) char-spec)))
143 (setq char-spec char)
147 (setq char (find-char char-spec))
149 ;; define temporary character
150 ;; Current implementation is dirty.
151 (setq temp-char (define-char (cons '(ideograph-daikanwa . 0)
153 (remove-char-attribute temp-char 'ideograph-daikanwa)
154 (setq char temp-char))
158 (setq key (car (car char-spec)))
159 (if (find-charset key)
160 (setq cal (cons key cal))
161 (setq al (cons key al)))
162 (setq char-spec (cdr char-spec)))
163 (insert-char-attributes char
167 ;; undefine temporary character
168 ;; Current implementation is dirty.
169 (setq char-spec (char-attribute-alist temp-char))
171 (remove-char-attribute temp-char (car (car char-spec)))
172 (setq char-spec (cdr char-spec))))))
174 (defun char-db-insert-alist (alist &optional readable column)
176 (setq column (current-column)))
178 (concat "\n" (make-string (1+ column) ?\ )))
181 lbs cell rest separator)
184 (setq name (car (car alist))
185 value (cdr (car alist)))
186 (cond ((eq name 'char)
188 (if (setq ret (condition-case nil
195 (setq key (car (car value)))
196 (if (find-charset key)
197 (setq cal (cons key cal))
198 (setq al (cons key al)))
199 (setq value (cdr value)))
200 (insert-char-attributes ret
203 (insert (prin1-to-string value)))
205 (insert line-breaking))
207 (insert (format "(%-18s " name))
208 (setq lbs (concat "\n" (make-string (current-column) ?\ )))
210 (setq cell (car value))
211 (if (and (consp cell)
213 (setq ret (condition-case nil
222 (setq key (car (car rest)))
223 (if (find-charset key)
224 (setq cal (cons key cal))
225 (setq al (cons key al)))
226 (setq rest (cdr rest)))
229 (insert-char-attributes ret
232 (setq separator lbs))
235 (insert (prin1-to-string cell))
236 (setq separator " "))
237 (setq value (cdr value)))
239 (insert line-breaking))
241 (insert (format "(%-18s . %S)%s"
244 (setq alist (cdr alist))))
247 (defun char-db-insert-char-reference (plist &optional readable column)
249 (setq column (current-column)))
251 (concat "\n" (make-string (1+ column) ?\ )))
255 (setq name (pop plist))
256 (setq value (pop plist))
257 (cond ((eq name :char)
259 (cond ((numberp value)
260 (setq value (decode-char 'ucs value)))
262 ;; (setq value (or (find-char value)
265 (char-db-insert-char-spec value readable)
266 (insert line-breaking))
268 (insert (format "%s\t%S%s"
274 (defun char-db-decode-isolated-char (ccs code-point)
277 (cond ((eq ccs 'arabic-iso8859-6)
278 (decode-char ccs code-point))
279 ((and (memq ccs '(ideograph-gt-pj-1
290 (setq ret (decode-char ccs code-point))
291 (setq ret (get-char-attribute ret 'ideograph-gt)))
292 (decode-builtin-char 'ideograph-gt ret))
294 (decode-builtin-char ccs code-point))))
295 (cond ((and (<= 0 (char-int ret))
296 (<= (char-int ret) #x1F))
297 (decode-char 'ucs (+ #x2400 (char-int ret))))
298 ((= (char-int ret) #x7F)
302 (defvar char-db-convert-obsolete-format t)
304 (defun insert-char-attributes (char &optional readable
305 attributes ccs-attributes
309 (if (consp attributes)
310 (copy-sequence attributes))
311 (char-attribute-list))
312 #'char-attribute-name<))
314 (sort (if ccs-attributes
315 (copy-sequence ccs-attributes)
317 #'char-attribute-name<))
319 (setq column (current-column)))
320 (let (name value has-long-ccs-name rest
323 (concat "\n" (make-string (1+ column) ?\ )))
324 lbs cell separator ret
327 (when (and (memq 'name attributes)
328 (setq value (get-char-attribute char 'name)))
330 (if (> (+ (current-column) (length value)) 48)
333 value line-breaking))
334 (setq attributes (delq 'name attributes))
336 (when (and (memq 'script attributes)
337 (setq value (get-char-attribute char 'script)))
338 (insert (format "(script\t\t%s)%s"
339 (mapconcat (function prin1-to-string)
342 (setq attributes (delq 'script attributes))
344 (when (and (memq '=>ucs attributes)
345 (setq value (get-char-attribute char '=>ucs)))
346 (insert (format "(=>ucs\t\t. #x%04X)\t; %c%s"
347 value (decode-char 'ucs value)
349 (setq attributes (delq '=>ucs attributes))
351 (when (and (memq '->ucs attributes)
352 (setq value (get-char-attribute char '->ucs)))
353 (insert (format (if char-db-convert-obsolete-format
354 "(=>ucs\t\t. #x%04X)\t; %c%s"
355 "(->ucs\t\t. #x%04X)\t; %c%s")
356 value (decode-char 'ucs value)
358 (setq attributes (delq '->ucs attributes))
360 (when (and (memq 'general-category attributes)
361 (setq value (get-char-attribute char 'general-category)))
363 "(general-category\t%s) ; %s%s"
364 (mapconcat (lambda (cell)
367 (cond ((rassoc value unidata-normative-category-alist)
368 "Normative Category")
369 ((rassoc value unidata-informative-category-alist)
370 "Informative Category")
374 (setq attributes (delq 'general-category attributes))
376 (when (and (memq 'bidi-category attributes)
377 (setq value (get-char-attribute char 'bidi-category)))
378 (insert (format "(bidi-category\t. %S)%s"
381 (setq attributes (delq 'bidi-category attributes))
383 (unless (or (not (memq 'mirrored attributes))
384 (eq (setq value (get-char-attribute char 'mirrored 'empty))
386 (insert (format "(mirrored\t\t. %S)%s"
389 (setq attributes (delq 'mirrored attributes))
392 ((and (memq 'decimal-digit-value attributes)
393 (setq value (get-char-attribute char 'decimal-digit-value)))
394 (insert (format "(decimal-digit-value . %S)%s"
397 (setq attributes (delq 'decimal-digit-value attributes))
398 (when (and (memq 'digit-value attributes)
399 (setq value (get-char-attribute char 'digit-value)))
400 (insert (format "(digit-value\t . %S)%s"
403 (setq attributes (delq 'digit-value attributes))
405 (when (and (memq 'numeric-value attributes)
406 (setq value (get-char-attribute char 'numeric-value)))
407 (insert (format "(numeric-value\t . %S)%s"
410 (setq attributes (delq 'numeric-value attributes))
414 (when (and (memq 'digit-value attributes)
415 (setq value (get-char-attribute char 'digit-value)))
416 (insert (format "(digit-value\t. %S)%s"
419 (setq attributes (delq 'digit-value attributes))
421 (when (and (memq 'numeric-value attributes)
422 (setq value (get-char-attribute char 'numeric-value)))
423 (insert (format "(numeric-value\t. %S)%s"
426 (setq attributes (delq 'numeric-value attributes))
428 (when (and (memq 'iso-10646-comment attributes)
429 (setq value (get-char-attribute char 'iso-10646-comment)))
430 (insert (format "(iso-10646-comment\t. %S)%s"
433 (setq attributes (delq 'iso-10646-comment attributes))
435 (when (and (memq 'morohashi-daikanwa attributes)
436 (setq value (get-char-attribute char 'morohashi-daikanwa)))
437 (insert (format "(morohashi-daikanwa\t%s)%s"
438 (mapconcat (function prin1-to-string) value " ")
440 (setq attributes (delq 'morohashi-daikanwa attributes))
444 (when (and (memq 'ideographic-radical attributes)
445 (setq value (get-char-attribute char 'ideographic-radical)))
447 (insert (format "(ideographic-radical . %S)\t; %c%s"
449 (aref ideographic-radicals radical)
451 (setq attributes (delq 'ideographic-radical attributes))
453 (when (and (memq 'ideographic-strokes attributes)
454 (setq value (get-char-attribute char 'ideographic-strokes)))
456 (insert (format "(ideographic-strokes . %S)%s"
459 (setq attributes (delq 'ideographic-strokes attributes))
461 (when (and (memq 'kangxi-radical attributes)
462 (setq value (get-char-attribute char 'kangxi-radical)))
463 (unless (eq value radical)
464 (insert (format "(kangxi-radical\t . %S)\t; %c%s"
466 (aref ideographic-radicals value)
469 (setq radical value)))
470 (setq attributes (delq 'kangxi-radical attributes))
472 (when (and (memq 'kangxi-strokes attributes)
473 (setq value (get-char-attribute char 'kangxi-strokes)))
474 (unless (eq value strokes)
475 (insert (format "(kangxi-strokes\t . %S)%s"
479 (setq strokes value)))
480 (setq attributes (delq 'kangxi-strokes attributes))
482 (when (and (memq 'japanese-radical attributes)
483 (setq value (get-char-attribute char 'japanese-radical)))
484 (unless (eq value radical)
485 (insert (format "(japanese-radical\t . %S)\t; %c%s"
487 (aref ideographic-radicals value)
490 (setq radical value)))
491 (setq attributes (delq 'japanese-radical attributes))
493 (when (and (memq 'japanese-strokes attributes)
494 (setq value (get-char-attribute char 'japanese-strokes)))
495 (unless (eq value strokes)
496 (insert (format "(japanese-strokes\t . %S)%s"
500 (setq strokes value)))
501 (setq attributes (delq 'japanese-strokes attributes))
503 (when (and (memq 'cns-radical attributes)
504 (setq value (get-char-attribute char 'cns-radical)))
505 (insert (format "(cns-radical\t . %S)\t; %c%s"
507 (aref ideographic-radicals value)
509 (setq attributes (delq 'cns-radical attributes))
511 (when (and (memq 'cns-strokes attributes)
512 (setq value (get-char-attribute char 'cns-strokes)))
513 (unless (eq value strokes)
514 (insert (format "(cns-strokes\t . %S)%s"
518 (setq strokes value)))
519 (setq attributes (delq 'cns-strokes attributes))
521 (when (and (memq 'shinjigen-1-radical attributes)
522 (setq value (get-char-attribute char 'shinjigen-1-radical)))
523 (unless (eq value radical)
524 (insert (format "(shinjigen-1-radical . %S)\t; %c%s"
526 (aref ideographic-radicals value)
529 (setq radical value)))
530 (setq attributes (delq 'shinjigen-1-radical attributes))
532 (when (and (memq 'total-strokes attributes)
533 (setq value (get-char-attribute char 'total-strokes)))
534 (insert (format "(total-strokes . %S)%s"
537 (setq attributes (delq 'total-strokes attributes))
539 (when (and (memq '->ideograph attributes)
540 (setq value (get-char-attribute char '->ideograph)))
541 (insert (format "(->ideograph\t%s)%s"
542 (mapconcat (lambda (code)
543 (cond ((symbolp code)
546 (format "#x%04X" code))
549 line-breaking code))))
552 (setq attributes (delq '->ideograph attributes))
554 (when (and (memq '->decomposition attributes)
555 (setq value (get-char-attribute char '->decomposition)))
556 (insert (format "(->decomposition\t%s)%s"
557 (mapconcat (lambda (code)
558 (cond ((symbolp code)
567 (format "#x%04X" code))
569 (format "%s%S" line-breaking code))))
572 (setq attributes (delq '->decomposition attributes))
574 (if (equal (get-char-attribute char '->titlecase)
575 (get-char-attribute char '->uppercase))
576 (setq attributes (delq '->titlecase attributes)))
577 (when (and (memq '->mojikyo attributes)
578 (setq value (get-char-attribute char '->mojikyo)))
579 (insert (format "(->mojikyo\t\t. %06d)\t; %c%s"
580 value (decode-char 'mojikyo value)
582 (setq attributes (delq '->mojikyo attributes))
584 (setq rest ccs-attributes)
587 (setq value (get-char-attribute char (car rest)))
589 (if (>= (length (symbol-name (car rest))) 19)
591 (setq has-long-ccs-name t)
595 (setq rest (cdr rest)))
597 (setq name (car attributes))
598 (if (setq value (get-char-attribute char name))
599 (cond ((eq name 'jisx0208-1978/4X)
600 (insert (format "(%-18s . #x%04X)%s"
603 ((memq name '(->lowercase
604 ->uppercase ->titlecase
605 ->fullwidth <-fullwidth
606 ->vulgar-ideograph <-vulgar-ideograph
607 ->ancient-ideograph <-ancient-ideograph
608 ->original-ideograph <-original-ideograph
609 ->simplified-ideograph <-simplified-ideograph
610 ->wrong-ideograph <-wrong-ideograph
612 ->ideographic-variants
615 ->bopomofo <-bopomofo
616 ->ideographic <-ideographic
617 ideographic-structure))
618 (insert (format "(%-18s%s " name line-breaking))
619 (setq lbs (concat "\n" (make-string (current-column) ?\ ))
622 (setq cell (car value))
624 (setq cell (decode-char 'ucs cell)))
625 (cond ((characterp cell)
628 (char-db-insert-char-spec cell readable)
629 (setq separator lbs))
633 (if (consp (car cell))
634 (char-db-insert-char-spec cell readable)
635 (char-db-insert-char-reference cell readable))
636 (setq separator lbs))
640 (insert (prin1-to-string cell))
641 (setq separator " ")))
642 (setq value (cdr value)))
644 (insert line-breaking))
645 ((memq name '(ideograph=
646 original-ideograph-of
650 simplified-ideograph-of
652 ideographic-different-form-of))
653 (insert (format "(%-18s%s " name line-breaking))
654 (setq lbs (concat "\n" (make-string (current-column) ?\ ))
657 (setq cell (car value))
658 (if (and (consp cell)
663 (char-db-insert-alist cell readable)
664 (setq separator lbs))
667 (insert (prin1-to-string cell))
668 (setq separator " "))
669 (setq value (cdr value)))
671 (insert line-breaking))
672 ((string-match "^->" (symbol-name name))
674 (format "(%-18s %s)%s"
676 (mapconcat (lambda (code)
677 (cond ((symbolp code)
680 (format "#x%04X" code))
683 line-breaking code))))
687 (insert (format "(%-18s " name))
688 (setq lbs (concat "\n" (make-string (current-column) ?\ ))
691 (setq cell (car value))
692 (if (and (consp cell)
694 (setq ret (condition-case nil
702 (setq key (car (car rest)))
703 (if (find-charset key)
704 (setq cal (cons key cal))
705 (setq al (cons key al)))
706 (setq rest (cdr rest)))
709 (insert-char-attributes ret
712 (setq separator lbs))
715 (insert (prin1-to-string cell))
716 (setq separator " "))
717 (setq value (cdr value)))
719 (insert line-breaking))
721 (insert (format "(%-18s . %S)%s"
725 (setq attributes (cdr attributes)))
726 (while ccs-attributes
727 (setq name (car ccs-attributes))
728 (if (and (eq name (charset-name name))
729 (setq value (get-char-attribute char name)))
732 (cond ((memq name '(ideograph-daikanwa ideograph-gt))
733 (if has-long-ccs-name
734 "(%-26s . %05d)\t; %c%s"
735 "(%-18s . %05d)\t; %c%s"))
737 (if has-long-ccs-name
738 "(%-26s . %06d)\t; %c%s"
739 "(%-18s . %06d)\t; %c%s"))
741 (if has-long-ccs-name
742 "(%-26s . #x%04X)\t; %c%s"
743 "(%-18s . #x%04X)\t; %c%s"))
745 (if has-long-ccs-name
746 "(%-26s . #x%02X)\t; %c%s"
747 "(%-18s . #x%02X)\t; %c%s")))
749 (if (= (charset-iso-graphic-plane name) 1)
751 (cond ((= (charset-dimension name) 1)
753 ((= (charset-dimension name) 2)
755 ((= (charset-dimension name) 3)
759 (char-db-decode-isolated-char name value)
761 (setq ccs-attributes (cdr ccs-attributes)))
764 (defun insert-char-data (char &optional readable
765 attributes ccs-attributes)
767 (narrow-to-region (point)(point))
768 (insert "(define-char
770 (insert-char-attributes char readable
771 attributes ccs-attributes)
773 (goto-char (point-min))
774 (while (re-search-forward "[ \t]+$" nil t)
776 (goto-char (point-max))
777 (tabify (point-min)(point-max))
781 (defun char-db-update-comment ()
784 (goto-char (point-min))
785 (let (cdef table char)
786 (while (re-search-forward "^[ \t]*\\(([^.()]+)\\)" nil t)
787 (goto-char (match-beginning 1))
788 (setq cdef (read (current-buffer)))
789 (when (find-charset (car cdef))
790 (goto-char (match-end 0))
793 (not (eq (car cdef) 'ideograph-daikanwa))
794 (or (memq (car cdef) '(ascii latin-viscii-upper
798 japanese-jisx0213-2))
799 (= (char-int (charset-iso-final-char (car cdef)))
801 (apply (function make-char) cdef)
802 (if (setq table (charset-mapping-table (car cdef)))
803 (set-charset-mapping-table (car cdef) nil))
805 (apply (function make-char) cdef)
807 (set-charset-mapping-table (car cdef) table)))))
808 (when (not (or (< (char-int char) 32)
809 (and (<= 128 (char-int char))
810 (< (char-int char) 160))))
811 (delete-region (point) (point-at-eol))
812 (insert (format "\t; %c" char)))
815 (defun insert-char-data-with-variant (char &optional printable
817 script excluded-script)
818 (insert-char-data char printable)
819 (let ((variants (or (char-variants char)
820 (let ((ucs (get-char-attribute char '->ucs)))
822 (delete char (char-variants (int-char ucs)))))))
824 (setq variants (sort variants #'<))
826 (setq variant (car variants))
827 (if (and (or (null script)
828 (null (setq vs (get-char-attribute variant 'script)))
830 (or (null excluded-script)
831 (null (setq vs (get-char-attribute variant 'script)))
832 (not (memq excluded-script vs))))
833 (or (and no-ucs-variant (get-char-attribute variant 'ucs))
834 (insert-char-data variant printable)))
835 (setq variants (cdr variants))
838 (defun insert-char-range-data (min max &optional script excluded-script)
842 (setq char (decode-char 'ucs code))
843 (if (get-char-attribute char 'ucs)
844 (insert-char-data-with-variant char nil 'no-ucs-variant
845 script excluded-script))
846 (setq code (1+ code))
849 (defun write-char-range-data-to-file (min max file
850 &optional script excluded-script)
851 (let ((coding-system-for-write 'utf-8))
853 (insert-char-range-data min max script excluded-script)
854 (write-region (point-min)(point-max) file))))
856 (defvar what-character-original-window-configuration)
859 (defun what-char-definition (char)
860 (interactive (list (char-after)))
861 (let ((buf (get-buffer-create "*Character Description*"))
862 (the-buf (current-buffer))
863 (win-conf (current-window-configuration)))
865 (make-local-variable 'what-character-original-window-configuration)
866 (setq what-character-original-window-configuration win-conf)
867 (setq buffer-read-only nil)
871 (insert-char-data-with-variant char 'printable)
872 ;; (char-db-update-comment)
873 (set-buffer-modified-p nil)
874 (view-mode the-buf (lambda (buf)
875 (set-window-configuration
876 what-character-original-window-configuration)
878 (goto-char (point-min)))
880 (set-window-configuration
881 what-character-original-window-configuration)
882 (signal (car err) (cdr err)))))))
884 (provide 'char-db-util)
886 ;;; char-db-util.el ends here