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)
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)
146 (setq char (condition-case nil
147 (find-char char-spec)
153 (setq key (car (car char-spec)))
154 (if (find-charset key)
155 (setq cal (cons key cal))
156 (setq al (cons key al)))
157 (setq char-spec (cdr char-spec)))
158 (insert-char-attributes char
161 (insert (prin1-to-string char-spec)))))
163 (defun char-db-insert-alist (alist &optional readable column)
165 (setq column (current-column)))
167 (concat "\n" (make-string (1+ column) ?\ )))
170 lbs cell rest separator)
173 (setq name (car (car alist))
174 value (cdr (car alist)))
175 (cond ((eq name 'char)
177 (if (setq ret (condition-case nil
184 (setq key (car (car value)))
185 (if (find-charset key)
186 (setq cal (cons key cal))
187 (setq al (cons key al)))
188 (setq value (cdr value)))
189 (insert-char-attributes ret
192 (insert (prin1-to-string value)))
194 (insert line-breaking))
196 (insert (format "(%-18s " name))
197 (setq lbs (concat "\n" (make-string (current-column) ?\ )))
199 (setq cell (car value))
200 (if (and (consp cell)
202 (setq ret (condition-case nil
211 (setq key (car (car rest)))
212 (if (find-charset key)
213 (setq cal (cons key cal))
214 (setq al (cons key al)))
215 (setq rest (cdr rest)))
218 (insert-char-attributes ret
221 (setq separator lbs))
224 (insert (prin1-to-string cell))
225 (setq separator " "))
226 (setq value (cdr value)))
228 (insert line-breaking))
230 (insert (format "(%-18s . %S)%s"
233 (setq alist (cdr alist))))
236 (defun char-db-insert-char-reference (plist &optional readable column)
238 (setq column (current-column)))
240 (concat "\n" (make-string (1+ column) ?\ )))
244 (setq name (pop plist))
245 (setq value (pop plist))
246 (cond ((eq name :char)
249 (setq value (decode-char 'ucs value)))
250 (char-db-insert-char-spec value readable)
251 (insert line-breaking))
253 (insert (format "%s\t%S%s"
259 (defun char-db-decode-isolated-char (ccs code-point)
262 (cond ((eq ccs 'arabic-iso8859-6)
263 (decode-char ccs code-point))
264 ((and (memq ccs '(ideograph-gt-pj-1
275 (setq ret (decode-char ccs code-point))
276 (setq ret (get-char-attribute ret 'ideograph-gt)))
277 (decode-builtin-char 'ideograph-gt ret))
279 (decode-builtin-char ccs code-point))))
280 (cond ((and (<= 0 (char-int ret))
281 (<= (char-int ret) #x1F))
282 (decode-char 'ucs (+ #x2400 (char-int ret))))
283 ((= (char-int ret) #x7F)
287 (defvar char-db-convert-obsolete-format t)
289 (defun insert-char-attributes (char &optional readable
290 attributes ccs-attributes
294 (if (consp attributes)
295 (copy-sequence attributes))
296 (char-attribute-list))
297 #'char-attribute-name<))
299 (sort (if ccs-attributes
300 (copy-sequence ccs-attributes)
302 #'char-attribute-name<))
304 (setq column (current-column)))
305 (let (name value has-long-ccs-name rest
308 (concat "\n" (make-string (1+ column) ?\ )))
309 lbs cell separator ret
312 (when (and (memq 'name attributes)
313 (setq value (get-char-attribute char 'name)))
315 (if (> (+ (current-column) (length value)) 48)
318 value line-breaking))
319 (setq attributes (delq 'name attributes))
321 (when (and (memq 'script attributes)
322 (setq value (get-char-attribute char 'script)))
323 (insert (format "(script\t\t%s)%s"
324 (mapconcat (function prin1-to-string)
327 (setq attributes (delq 'script attributes))
329 (when (and (memq '=>ucs attributes)
330 (setq value (get-char-attribute char '=>ucs)))
331 (insert (format "(=>ucs\t\t. #x%04X)\t; %c%s"
332 value (decode-char 'ucs value)
334 (setq attributes (delq '=>ucs attributes))
336 (when (and (memq '->ucs attributes)
337 (setq value (get-char-attribute char '->ucs)))
338 (insert (format (if char-db-convert-obsolete-format
339 "(=>ucs\t\t. #x%04X)\t; %c%s"
340 "(->ucs\t\t. #x%04X)\t; %c%s")
341 value (decode-char 'ucs value)
343 (setq attributes (delq '->ucs attributes))
345 (when (and (memq 'general-category attributes)
346 (setq value (get-char-attribute char 'general-category)))
348 "(general-category\t%s) ; %s%s"
349 (mapconcat (lambda (cell)
352 (cond ((rassoc value unidata-normative-category-alist)
353 "Normative Category")
354 ((rassoc value unidata-informative-category-alist)
355 "Informative Category")
359 (setq attributes (delq 'general-category attributes))
361 (when (and (memq 'bidi-category attributes)
362 (setq value (get-char-attribute char 'bidi-category)))
363 (insert (format "(bidi-category\t. %S)%s"
366 (setq attributes (delq 'bidi-category attributes))
368 (unless (or (not (memq 'mirrored attributes))
369 (eq (setq value (get-char-attribute char 'mirrored 'empty))
371 (insert (format "(mirrored\t\t. %S)%s"
374 (setq attributes (delq 'mirrored attributes))
377 ((and (memq 'decimal-digit-value attributes)
378 (setq value (get-char-attribute char 'decimal-digit-value)))
379 (insert (format "(decimal-digit-value . %S)%s"
382 (setq attributes (delq 'decimal-digit-value attributes))
383 (when (and (memq 'digit-value attributes)
384 (setq value (get-char-attribute char 'digit-value)))
385 (insert (format "(digit-value\t . %S)%s"
388 (setq attributes (delq 'digit-value attributes))
390 (when (and (memq 'numeric-value attributes)
391 (setq value (get-char-attribute char 'numeric-value)))
392 (insert (format "(numeric-value\t . %S)%s"
395 (setq attributes (delq 'numeric-value attributes))
399 (when (and (memq 'digit-value attributes)
400 (setq value (get-char-attribute char 'digit-value)))
401 (insert (format "(digit-value\t. %S)%s"
404 (setq attributes (delq 'digit-value attributes))
406 (when (and (memq 'numeric-value attributes)
407 (setq value (get-char-attribute char 'numeric-value)))
408 (insert (format "(numeric-value\t. %S)%s"
411 (setq attributes (delq 'numeric-value attributes))
413 (when (and (memq 'iso-10646-comment attributes)
414 (setq value (get-char-attribute char 'iso-10646-comment)))
415 (insert (format "(iso-10646-comment\t. %S)%s"
418 (setq attributes (delq 'iso-10646-comment attributes))
420 (when (and (memq 'morohashi-daikanwa attributes)
421 (setq value (get-char-attribute char 'morohashi-daikanwa)))
422 (insert (format "(morohashi-daikanwa\t%s)%s"
423 (mapconcat (function prin1-to-string) value " ")
425 (setq attributes (delq 'morohashi-daikanwa attributes))
429 (when (and (memq 'ideographic-radical attributes)
430 (setq value (get-char-attribute char 'ideographic-radical)))
432 (insert (format "(ideographic-radical . %S)\t; %c%s"
434 (aref ideographic-radicals radical)
436 (setq attributes (delq 'ideographic-radical attributes))
438 (when (and (memq 'ideographic-strokes attributes)
439 (setq value (get-char-attribute char 'ideographic-strokes)))
441 (insert (format "(ideographic-strokes . %S)%s"
444 (setq attributes (delq 'ideographic-strokes attributes))
446 (when (and (memq 'kangxi-radical attributes)
447 (setq value (get-char-attribute char 'kangxi-radical)))
448 (unless (eq value radical)
449 (insert (format "(kangxi-radical\t . %S)\t; %c%s"
451 (aref ideographic-radicals value)
454 (setq radical value)))
455 (setq attributes (delq 'kangxi-radical attributes))
457 (when (and (memq 'kangxi-strokes attributes)
458 (setq value (get-char-attribute char 'kangxi-strokes)))
459 (unless (eq value strokes)
460 (insert (format "(kangxi-strokes\t . %S)%s"
464 (setq strokes value)))
465 (setq attributes (delq 'kangxi-strokes attributes))
467 (when (and (memq 'japanese-radical attributes)
468 (setq value (get-char-attribute char 'japanese-radical)))
469 (unless (eq value radical)
470 (insert (format "(japanese-radical\t . %S)\t; %c%s"
472 (aref ideographic-radicals value)
475 (setq radical value)))
476 (setq attributes (delq 'japanese-radical attributes))
478 (when (and (memq 'japanese-strokes attributes)
479 (setq value (get-char-attribute char 'japanese-strokes)))
480 (unless (eq value strokes)
481 (insert (format "(japanese-strokes\t . %S)%s"
485 (setq strokes value)))
486 (setq attributes (delq 'japanese-strokes attributes))
488 (when (and (memq 'cns-radical attributes)
489 (setq value (get-char-attribute char 'cns-radical)))
490 (insert (format "(cns-radical\t . %S)\t; %c%s"
492 (aref ideographic-radicals value)
494 (setq attributes (delq 'cns-radical attributes))
496 (when (and (memq 'cns-strokes attributes)
497 (setq value (get-char-attribute char 'cns-strokes)))
498 (unless (eq value strokes)
499 (insert (format "(cns-strokes\t . %S)%s"
503 (setq strokes value)))
504 (setq attributes (delq 'cns-strokes attributes))
506 (when (and (memq 'shinjigen-1-radical attributes)
507 (setq value (get-char-attribute char 'shinjigen-1-radical)))
508 (unless (eq value radical)
509 (insert (format "(shinjigen-1-radical . %S)\t; %c%s"
511 (aref ideographic-radicals value)
514 (setq radical value)))
515 (setq attributes (delq 'shinjigen-1-radical attributes))
517 (when (and (memq 'total-strokes attributes)
518 (setq value (get-char-attribute char 'total-strokes)))
519 (insert (format "(total-strokes . %S)%s"
522 (setq attributes (delq 'total-strokes attributes))
524 (when (and (memq '->ideograph attributes)
525 (setq value (get-char-attribute char '->ideograph)))
526 (insert (format "(->ideograph\t%s)%s"
527 (mapconcat (lambda (code)
528 (cond ((symbolp code)
531 (format "#x%04X" code))
534 line-breaking code))))
537 (setq attributes (delq '->ideograph attributes))
539 (when (and (memq '->decomposition attributes)
540 (setq value (get-char-attribute char '->decomposition)))
541 (insert (format "(->decomposition\t%s)%s"
542 (mapconcat (lambda (code)
543 (cond ((symbolp code)
552 (format "#x%04X" code))
554 (format "%s%S" line-breaking code))))
557 (setq attributes (delq '->decomposition attributes))
559 (if (equal (get-char-attribute char '->titlecase)
560 (get-char-attribute char '->uppercase))
561 (setq attributes (delq '->titlecase attributes)))
562 (when (and (memq '->mojikyo attributes)
563 (setq value (get-char-attribute char '->mojikyo)))
564 (insert (format "(->mojikyo\t\t. %06d)\t; %c%s"
565 value (decode-char 'mojikyo value)
567 (setq attributes (delq '->mojikyo attributes))
569 (setq rest ccs-attributes)
572 (setq value (get-char-attribute char (car rest)))
574 (if (>= (length (symbol-name (car rest))) 19)
576 (setq has-long-ccs-name t)
580 (setq rest (cdr rest)))
582 (setq name (car attributes))
583 (if (setq value (get-char-attribute char name))
584 (cond ((eq name 'jisx0208-1978/4X)
585 (insert (format "(%-18s . #x%04X)%s"
588 ((memq name '(->lowercase
589 ->uppercase ->titlecase
590 ->fullwidth <-fullwidth
591 ->vulgar-ideograph <-vulgar-ideograph
592 ->ancient-ideograph <-ancient-ideograph
593 ->original-ideograph <-original-ideograph
594 ->simplified-ideograph <-simplified-ideograph
595 ->wrong-ideograph <-wrong-ideograph
597 ->ideographic-variants
600 ->bopomofo <-bopomofo
601 ->ideographic <-ideographic
602 ideographic-structure))
603 (insert (format "(%-18s%s " name line-breaking))
604 (setq lbs (concat "\n" (make-string (current-column) ?\ ))
607 (setq cell (car value))
609 (setq cell (decode-char 'ucs cell)))
610 (cond ((characterp cell)
613 (char-db-insert-char-spec cell readable)
614 (setq separator lbs))
618 (if (consp (car cell))
619 (char-db-insert-char-spec cell readable)
620 (char-db-insert-char-reference cell readable))
621 (setq separator lbs))
625 (insert (prin1-to-string cell))
626 (setq separator " ")))
627 (setq value (cdr value)))
629 (insert line-breaking))
630 ((memq name '(ideograph=
631 original-ideograph-of
635 simplified-ideograph-of
637 ideographic-different-form-of))
638 (insert (format "(%-18s%s " name line-breaking))
639 (setq lbs (concat "\n" (make-string (current-column) ?\ ))
642 (setq cell (car value))
643 (if (and (consp cell)
648 (char-db-insert-alist cell readable)
649 (setq separator lbs))
652 (insert (prin1-to-string cell))
653 (setq separator " "))
654 (setq value (cdr value)))
656 (insert line-breaking))
657 ((string-match "^->" (symbol-name name))
659 (format "(%-18s %s)%s"
661 (mapconcat (lambda (code)
662 (cond ((symbolp code)
665 (format "#x%04X" code))
668 line-breaking code))))
672 (insert (format "(%-18s " name))
673 (setq lbs (concat "\n" (make-string (current-column) ?\ ))
676 (setq cell (car value))
677 (if (and (consp cell)
679 (setq ret (condition-case nil
687 (setq key (car (car rest)))
688 (if (find-charset key)
689 (setq cal (cons key cal))
690 (setq al (cons key al)))
691 (setq rest (cdr rest)))
694 (insert-char-attributes ret
697 (setq separator lbs))
700 (insert (prin1-to-string cell))
701 (setq separator " "))
702 (setq value (cdr value)))
704 (insert line-breaking))
706 (insert (format "(%-18s . %S)%s"
710 (setq attributes (cdr attributes)))
711 (while ccs-attributes
712 (setq name (car ccs-attributes))
713 (if (and (eq name (charset-name name))
714 (setq value (get-char-attribute char name)))
717 (cond ((memq name '(ideograph-daikanwa ideograph-gt))
718 (if has-long-ccs-name
719 "(%-26s . %05d)\t; %c%s"
720 "(%-18s . %05d)\t; %c%s"))
722 (if has-long-ccs-name
723 "(%-26s . %06d)\t; %c%s"
724 "(%-18s . %06d)\t; %c%s"))
726 (if has-long-ccs-name
727 "(%-26s . #x%04X)\t; %c%s"
728 "(%-18s . #x%04X)\t; %c%s"))
730 (if has-long-ccs-name
731 "(%-26s . #x%02X)\t; %c%s"
732 "(%-18s . #x%02X)\t; %c%s")))
734 (if (= (charset-iso-graphic-plane name) 1)
736 (cond ((= (charset-dimension name) 1)
738 ((= (charset-dimension name) 2)
740 ((= (charset-dimension name) 3)
744 (char-db-decode-isolated-char name value)
746 (setq ccs-attributes (cdr ccs-attributes)))
749 (defun insert-char-data (char &optional readable
750 attributes ccs-attributes)
752 (narrow-to-region (point)(point))
753 (insert "(define-char
755 (insert-char-attributes char readable
756 attributes ccs-attributes)
758 (goto-char (point-min))
759 (while (re-search-forward "[ \t]+$" nil t)
761 (goto-char (point-max))
762 (tabify (point-min)(point-max))
766 (defun char-db-update-comment ()
769 (goto-char (point-min))
770 (let (cdef table char)
771 (while (re-search-forward "^[ \t]*\\(([^.()]+)\\)" nil t)
772 (goto-char (match-beginning 1))
773 (setq cdef (read (current-buffer)))
774 (when (find-charset (car cdef))
775 (goto-char (match-end 0))
778 (not (eq (car cdef) 'ideograph-daikanwa))
779 (or (memq (car cdef) '(ascii latin-viscii-upper
783 japanese-jisx0213-2))
784 (= (char-int (charset-iso-final-char (car cdef)))
786 (apply (function make-char) cdef)
787 (if (setq table (charset-mapping-table (car cdef)))
788 (set-charset-mapping-table (car cdef) nil))
790 (apply (function make-char) cdef)
792 (set-charset-mapping-table (car cdef) table)))))
793 (when (not (or (< (char-int char) 32)
794 (and (<= 128 (char-int char))
795 (< (char-int char) 160))))
796 (delete-region (point) (point-at-eol))
797 (insert (format "\t; %c" char)))
800 (defun insert-char-data-with-variant (char &optional printable
802 script excluded-script)
803 (insert-char-data char printable)
804 (let ((variants (or (char-variants char)
805 (let ((ucs (get-char-attribute char '->ucs)))
807 (delete char (char-variants (int-char ucs)))))))
809 (setq variants (sort variants #'<))
811 (setq variant (car variants))
812 (if (and (or (null script)
813 (null (setq vs (get-char-attribute variant 'script)))
815 (or (null excluded-script)
816 (null (setq vs (get-char-attribute variant 'script)))
817 (not (memq excluded-script vs))))
818 (or (and no-ucs-variant (get-char-attribute variant 'ucs))
819 (insert-char-data variant printable)))
820 (setq variants (cdr variants))
823 (defun insert-char-range-data (min max &optional script excluded-script)
827 (setq char (decode-char 'ucs code))
828 (if (get-char-attribute char 'ucs)
829 (insert-char-data-with-variant char nil 'no-ucs-variant
830 script excluded-script))
831 (setq code (1+ code))
834 (defun write-char-range-data-to-file (min max file
835 &optional script excluded-script)
836 (let ((coding-system-for-write 'utf-8))
838 (insert-char-range-data min max script excluded-script)
839 (write-region (point-min)(point-max) file))))
841 (defvar what-character-original-window-configuration)
844 (defun what-char-definition (char)
845 (interactive (list (char-after)))
846 (let ((buf (get-buffer-create "*Character Description*"))
847 (the-buf (current-buffer))
848 (win-conf (current-window-configuration)))
850 (make-local-variable 'what-character-original-window-configuration)
851 (setq what-character-original-window-configuration win-conf)
852 (setq buffer-read-only nil)
856 (insert-char-data-with-variant char 'printable)
857 ;; (char-db-update-comment)
858 (set-buffer-modified-p nil)
859 (view-mode the-buf (lambda (buf)
860 (set-window-configuration
861 what-character-original-window-configuration)
863 (goto-char (point-min)))
865 (set-window-configuration
866 what-character-original-window-configuration)
867 (signal (car err) (cdr err)))))))
869 (provide 'char-db-util)
871 ;;; char-db-util.el ends here