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-ref (char &optional readable column)
121 (setq column (current-column)))
122 (let (char-ref ret al cal key)
123 (cond ((characterp char)
124 (cond ((setq ret (get-char-attribute char 'ucs))
125 (setq char-ref (list (cons 'ucs ret)))
126 (if (setq ret (get-char-attribute char 'name))
127 (setq char-ref (cons (cons 'name ret) char-ref)))
129 ((setq ret (split-char char))
130 (setq char-ref (list ret))
131 (dolist (ccs (delq (car ret) (charset-list)))
132 (if (and (>= (charset-iso-final-char ccs) ?0)
133 (setq ret (get-char-attribute char ccs)))
134 (setq char-ref (cons (cons ccs ret) char-ref))))
135 (if (setq ret (get-char-attribute char 'name))
136 (setq char-ref (cons (cons 'name ret) char-ref)))
142 (setq char (condition-case nil
143 (define-char char-ref)
149 (setq key (car (car char-ref)))
150 (if (find-charset key)
151 (setq cal (cons key cal))
152 (setq al (cons key al)))
153 (setq char-ref (cdr char-ref)))
154 (insert-char-attributes char
157 (insert (prin1-to-string char-ref)))))
159 (defun char-db-insert-alist (alist &optional readable column)
161 (setq column (current-column)))
163 (concat "\n" (make-string (1+ column) ?\ )))
166 lbs cell rest separator)
169 (setq name (car (car alist))
170 value (cdr (car alist)))
171 (cond ((eq name 'char)
173 (if (setq ret (condition-case nil
180 (setq key (car (car value)))
181 (if (find-charset key)
182 (setq cal (cons key cal))
183 (setq al (cons key al)))
184 (setq value (cdr value)))
185 (insert-char-attributes ret
188 (insert (prin1-to-string value)))
190 (insert line-breaking))
192 (insert (format "(%-18s " name))
193 (setq lbs (concat "\n" (make-string (current-column) ?\ )))
195 (setq cell (car value))
196 (if (and (consp cell)
198 (setq ret (condition-case nil
207 (setq key (car (car rest)))
208 (if (find-charset key)
209 (setq cal (cons key cal))
210 (setq al (cons key al)))
211 (setq rest (cdr rest)))
214 (insert-char-attributes ret
217 (setq separator lbs))
220 (insert (prin1-to-string cell))
221 (setq separator " "))
222 (setq value (cdr value)))
224 (insert line-breaking))
226 (insert (format "(%-18s . %S)%s"
229 (setq alist (cdr alist))))
232 (defun char-db-insert-char-map (plist &optional readable column)
234 (setq column (current-column)))
236 (concat "\n" (make-string (1+ column) ?\ )))
240 (setq name (pop plist))
241 (setq value (pop plist))
242 (cond ((eq name :char)
244 (char-db-insert-char-ref value readable)
245 (insert line-breaking))
247 (insert (format "%s\t%S%s"
253 (defun char-db-decode-isolated-char (ccs code-point)
256 (if (and (memq ccs '(ideograph-gt-pj-1
267 (setq ret (decode-char ccs code-point))
268 (setq ret (get-char-attribute ret 'ideograph-gt)))
269 (decode-builtin-char 'ideograph-gt ret)
270 (decode-builtin-char ccs code-point)))
271 (cond ((and (<= 0 (char-int ret))
272 (<= (char-int ret) #x1F))
273 (decode-char 'ucs (+ #x2400 (char-int ret))))
274 ((= (char-int ret) #x7F)
278 (defvar char-db-convert-obsolete-format t)
280 (defun insert-char-attributes (char &optional readable
281 attributes ccs-attributes
285 (if (consp attributes)
286 (copy-sequence attributes))
287 (char-attribute-list))
288 #'char-attribute-name<))
290 (sort (if ccs-attributes
291 (copy-sequence ccs-attributes)
293 #'char-attribute-name<))
295 (setq column (current-column)))
296 (let (name value has-long-ccs-name rest
299 (concat "\n" (make-string (1+ column) ?\ )))
300 lbs cell separator ret
303 (when (and (memq 'name attributes)
304 (setq value (get-char-attribute char 'name)))
306 (if (> (length value) 47)
309 value line-breaking))
310 (setq attributes (delq 'name attributes))
312 (when (and (memq 'script attributes)
313 (setq value (get-char-attribute char 'script)))
314 (insert (format "(script\t\t%s)%s"
315 (mapconcat (function prin1-to-string)
318 (setq attributes (delq 'script attributes))
320 (when (and (memq '=>ucs attributes)
321 (setq value (get-char-attribute char '=>ucs)))
322 (insert (format "(=>ucs\t\t. #x%04X)\t; %c%s"
323 value (decode-char 'ucs value)
325 (setq attributes (delq '=>ucs attributes))
327 (when (and (memq '->ucs attributes)
328 (setq value (get-char-attribute char '->ucs)))
329 (insert (format (if char-db-convert-obsolete-format
330 "(=>ucs\t\t. #x%04X)\t; %c%s"
331 "(->ucs\t\t. #x%04X)\t; %c%s")
332 value (decode-char 'ucs value)
334 (setq attributes (delq '->ucs attributes))
336 (when (and (memq 'general-category attributes)
337 (setq value (get-char-attribute char 'general-category)))
339 "(general-category\t%s) ; %s%s"
340 (mapconcat (lambda (cell)
343 (cond ((rassoc value unidata-normative-category-alist)
344 "Normative Category")
345 ((rassoc value unidata-informative-category-alist)
346 "Informative Category")
350 (setq attributes (delq 'general-category attributes))
352 (when (and (memq 'bidi-category attributes)
353 (setq value (get-char-attribute char 'bidi-category)))
354 (insert (format "(bidi-category\t. %S)%s"
357 (setq attributes (delq 'bidi-category attributes))
359 (unless (or (not (memq 'mirrored attributes))
360 (eq (setq value (get-char-attribute char 'mirrored 'empty))
362 (insert (format "(mirrored\t\t. %S)%s"
365 (setq attributes (delq 'mirrored attributes))
368 ((and (memq 'decimal-digit-value attributes)
369 (setq value (get-char-attribute char 'decimal-digit-value)))
370 (insert (format "(decimal-digit-value . %S)%s"
373 (setq attributes (delq 'decimal-digit-value attributes))
374 (when (and (memq 'digit-value attributes)
375 (setq value (get-char-attribute char 'digit-value)))
376 (insert (format "(digit-value\t . %S)%s"
379 (setq attributes (delq 'digit-value attributes))
381 (when (and (memq 'numeric-value attributes)
382 (setq value (get-char-attribute char 'numeric-value)))
383 (insert (format "(numeric-value\t . %S)%s"
386 (setq attributes (delq 'numeric-value attributes))
390 (when (and (memq 'digit-value attributes)
391 (setq value (get-char-attribute char 'digit-value)))
392 (insert (format "(digit-value\t. %S)%s"
395 (setq attributes (delq 'digit-value attributes))
397 (when (and (memq 'numeric-value attributes)
398 (setq value (get-char-attribute char 'numeric-value)))
399 (insert (format "(numeric-value\t. %S)%s"
402 (setq attributes (delq 'numeric-value attributes))
404 (when (and (memq 'iso-10646-comment attributes)
405 (setq value (get-char-attribute char 'iso-10646-comment)))
406 (insert (format "(iso-10646-comment\t. %S)%s"
409 (setq attributes (delq 'iso-10646-comment attributes))
411 (when (and (memq 'morohashi-daikanwa attributes)
412 (setq value (get-char-attribute char 'morohashi-daikanwa)))
413 (insert (format "(morohashi-daikanwa\t%s)%s"
414 (mapconcat (function prin1-to-string) value " ")
416 (setq attributes (delq 'morohashi-daikanwa attributes))
420 (when (and (memq 'ideographic-radical attributes)
421 (setq value (get-char-attribute char 'ideographic-radical)))
423 (insert (format "(ideographic-radical . %S)\t; %c%s"
425 (aref ideographic-radicals radical)
427 (setq attributes (delq 'ideographic-radical attributes))
429 (when (and (memq 'ideographic-strokes attributes)
430 (setq value (get-char-attribute char 'ideographic-strokes)))
432 (insert (format "(ideographic-strokes . %S)%s"
435 (setq attributes (delq 'ideographic-strokes attributes))
437 (when (and (memq 'kangxi-radical attributes)
438 (setq value (get-char-attribute char 'kangxi-radical)))
439 (unless (eq value radical)
440 (insert (format "(kangxi-radical\t . %S)\t; %c%s"
442 (aref ideographic-radicals value)
445 (setq radical value)))
446 (setq attributes (delq 'kangxi-radical attributes))
448 (when (and (memq 'kangxi-strokes attributes)
449 (setq value (get-char-attribute char 'kangxi-strokes)))
450 (unless (eq value strokes)
451 (insert (format "(kangxi-strokes\t . %S)%s"
455 (setq strokes value)))
456 (setq attributes (delq 'kangxi-strokes attributes))
458 (when (and (memq 'japanese-radical attributes)
459 (setq value (get-char-attribute char 'japanese-radical)))
460 (unless (eq value radical)
461 (insert (format "(japanese-radical\t . %S)\t; %c%s"
463 (aref ideographic-radicals value)
466 (setq radical value)))
467 (setq attributes (delq 'japanese-radical attributes))
469 (when (and (memq 'japanese-strokes attributes)
470 (setq value (get-char-attribute char 'japanese-strokes)))
471 (unless (eq value strokes)
472 (insert (format "(japanese-strokes\t . %S)%s"
476 (setq strokes value)))
477 (setq attributes (delq 'japanese-strokes attributes))
479 (when (and (memq 'cns-radical attributes)
480 (setq value (get-char-attribute char 'cns-radical)))
481 (insert (format "(cns-radical\t . %S)\t; %c%s"
483 (aref ideographic-radicals value)
485 (setq attributes (delq 'cns-radical attributes))
487 (when (and (memq 'cns-strokes attributes)
488 (setq value (get-char-attribute char 'cns-strokes)))
489 (unless (eq value strokes)
490 (insert (format "(cns-strokes\t . %S)%s"
494 (setq strokes value)))
495 (setq attributes (delq 'cns-strokes attributes))
497 (when (and (memq 'shinjigen-1-radical attributes)
498 (setq value (get-char-attribute char 'shinjigen-1-radical)))
499 (unless (eq value radical)
500 (insert (format "(shinjigen-1-radical . %S)\t; %c%s"
502 (aref ideographic-radicals value)
505 (setq radical value)))
506 (setq attributes (delq 'shinjigen-1-radical attributes))
508 (when (and (memq 'total-strokes attributes)
509 (setq value (get-char-attribute char 'total-strokes)))
510 (insert (format "(total-strokes . %S)%s"
513 (setq attributes (delq 'total-strokes attributes))
515 (when (and (memq '->ideograph attributes)
516 (setq value (get-char-attribute char '->ideograph)))
517 (insert (format "(->ideograph\t%s)%s"
518 (mapconcat (lambda (code)
519 (cond ((symbolp code)
522 (format "#x%04X" code))
525 line-breaking code))))
528 (setq attributes (delq '->ideograph attributes))
530 (when (and (memq '->decomposition attributes)
531 (setq value (get-char-attribute char '->decomposition)))
532 (insert (format "(->decomposition\t%s)%s"
533 (mapconcat (lambda (code)
534 (cond ((symbolp code)
543 (format "#x%04X" code))
545 (format "%s%S" line-breaking code))))
548 (setq attributes (delq '->decomposition attributes))
550 (if (equal (get-char-attribute char '->titlecase)
551 (get-char-attribute char '->uppercase))
552 (setq attributes (delq '->titlecase attributes)))
553 (when (and (memq '->mojikyo attributes)
554 (setq value (get-char-attribute char '->mojikyo)))
555 (insert (format "(->mojikyo\t\t. %06d)\t; %c%s"
556 value (decode-char 'mojikyo value)
558 (setq attributes (delq '->mojikyo attributes))
560 (setq rest ccs-attributes)
563 (setq value (get-char-attribute char (car rest)))
565 (if (>= (length (symbol-name (car rest))) 19)
567 (setq has-long-ccs-name t)
571 (setq rest (cdr rest)))
573 (setq name (car attributes))
574 (if (setq value (get-char-attribute char name))
575 (cond ((eq name 'jisx0208-1978/4X)
576 (insert (format "(%-18s . #x%04X)%s"
579 ((memq name '(->lowercase
580 ->uppercase ->titlecase
581 ->fullwidth <-fullwidth
582 ->vulgar-ideograph <-vulgar-ideograph
583 ->ancient-ideograph <-ancient-ideograph
584 ->simplified-ideograph <-simplified-ideograph
587 (insert (format "(%-18s%s " name line-breaking))
588 (setq lbs (concat "\n" (make-string (current-column) ?\ ))
591 (setq cell (car value))
593 (setq cell (decode-char 'ucs cell)))
594 (cond ((characterp cell)
597 (char-db-insert-char-ref cell readable)
598 (setq separator lbs))
602 (if (consp (car cell))
603 (char-db-insert-char-ref cell readable)
604 (char-db-insert-char-map cell readable))
605 (setq separator lbs))
609 (insert (prin1-to-string cell))
610 (setq separator " ")))
611 (setq value (cdr value)))
613 (insert line-breaking))
614 ((memq name '(ideograph=
615 original-ideograph-of
619 simplified-ideograph-of
621 ideographic-different-form-of))
622 (insert (format "(%-18s%s " name line-breaking))
623 (setq lbs (concat "\n" (make-string (current-column) ?\ ))
626 (setq cell (car value))
627 (if (and (consp cell)
632 (char-db-insert-alist cell readable)
633 (setq separator lbs))
636 (insert (prin1-to-string cell))
637 (setq separator " "))
638 (setq value (cdr value)))
640 (insert line-breaking))
641 ((string-match "^->" (symbol-name name))
643 (format "(%-18s %s)%s"
645 (mapconcat (lambda (code)
646 (cond ((symbolp code)
649 (format "#x%04X" code))
652 line-breaking code))))
656 (insert (format "(%-18s " name))
657 (setq lbs (concat "\n" (make-string (current-column) ?\ ))
660 (setq cell (car value))
661 (if (and (consp cell)
663 (setq ret (condition-case nil
671 (setq key (car (car rest)))
672 (if (find-charset key)
673 (setq cal (cons key cal))
674 (setq al (cons key al)))
675 (setq rest (cdr rest)))
678 (insert-char-attributes ret
681 (setq separator lbs))
684 (insert (prin1-to-string cell))
685 (setq separator " "))
686 (setq value (cdr value)))
688 (insert line-breaking))
690 (insert (format "(%-18s . %S)%s"
694 (setq attributes (cdr attributes)))
695 (while ccs-attributes
696 (setq name (car ccs-attributes))
697 (if (and (eq name (charset-name name))
698 (setq value (get-char-attribute char name)))
701 (cond ((memq name '(ideograph-daikanwa ideograph-gt))
702 (if has-long-ccs-name
703 "(%-26s . %05d)\t; %c%s"
704 "(%-18s . %05d)\t; %c%s"))
706 (if has-long-ccs-name
707 "(%-26s . %06d)\t; %c%s"
708 "(%-18s . %06d)\t; %c%s"))
710 (if has-long-ccs-name
711 "(%-26s . #x%04X)\t; %c%s"
712 "(%-18s . #x%04X)\t; %c%s"))
714 (if has-long-ccs-name
715 "(%-26s . #x%02X)\t; %c%s"
716 "(%-18s . #x%02X)\t; %c%s")))
718 (if (= (charset-iso-graphic-plane name) 1)
720 (cond ((= (charset-dimension name) 1)
722 ((= (charset-dimension name) 2)
724 ((= (charset-dimension name) 3)
728 (char-db-decode-isolated-char name value)
730 (setq ccs-attributes (cdr ccs-attributes)))
733 (defun insert-char-data (char &optional readable
734 attributes ccs-attributes)
736 (narrow-to-region (point)(point))
737 (insert "(define-char
739 (insert-char-attributes char readable
740 attributes ccs-attributes)
742 (goto-char (point-min))
743 (while (re-search-forward "[ \t]+$" nil t)
745 (goto-char (point-max))
746 (tabify (point-min)(point-max))
750 (defun char-db-update-comment ()
753 (goto-char (point-min))
754 (let (cdef table char)
755 (while (re-search-forward "^[ \t]*\\(([^.()]+)\\)" nil t)
756 (goto-char (match-beginning 1))
757 (setq cdef (read (current-buffer)))
758 (when (find-charset (car cdef))
759 (goto-char (match-end 0))
762 (not (eq (car cdef) 'ideograph-daikanwa))
763 (or (memq (car cdef) '(ascii latin-viscii-upper
767 japanese-jisx0213-2))
768 (= (char-int (charset-iso-final-char (car cdef)))
770 (apply (function make-char) cdef)
771 (if (setq table (charset-mapping-table (car cdef)))
772 (set-charset-mapping-table (car cdef) nil))
774 (apply (function make-char) cdef)
776 (set-charset-mapping-table (car cdef) table)))))
777 (when (not (or (< (char-int char) 32)
778 (and (<= 128 (char-int char))
779 (< (char-int char) 160))))
780 (delete-region (point) (point-at-eol))
781 (insert (format "\t; %c" char)))
784 (defun insert-char-data-with-variant (char &optional printable
786 script excluded-script)
787 (insert-char-data char printable)
788 (let ((variants (or (char-variants char)
789 (let ((ucs (get-char-attribute char '->ucs)))
791 (delete char (char-variants (int-char ucs)))))))
794 (setq variant (car variants))
795 (if (and (or (null script)
796 (null (setq vs (get-char-attribute variant 'script)))
798 (or (null excluded-script)
799 (null (setq vs (get-char-attribute variant 'script)))
800 (not (memq excluded-script vs))))
801 (or (and no-ucs-variant (get-char-attribute variant 'ucs))
802 (insert-char-data variant printable)))
803 (setq variants (cdr variants))
806 (defun insert-char-range-data (min max &optional script excluded-script)
810 (setq char (decode-char 'ucs code))
811 (if (get-char-attribute char 'ucs)
812 (insert-char-data-with-variant char nil 'no-ucs-variant
813 script excluded-script))
814 (setq code (1+ code))
817 (defun write-char-range-data-to-file (min max file
818 &optional script excluded-script)
819 (let ((coding-system-for-write 'utf-8))
821 (insert-char-range-data min max script excluded-script)
822 (write-region (point-min)(point-max) file))))
824 (defvar what-character-original-window-configuration)
827 (defun what-char-definition (char)
828 (interactive (list (char-after)))
829 (let ((buf (get-buffer-create "*Character Description*"))
830 (the-buf (current-buffer))
831 (win-conf (current-window-configuration)))
833 (make-local-variable 'what-character-original-window-configuration)
834 (setq what-character-original-window-configuration win-conf)
835 (setq buffer-read-only nil)
839 (insert-char-data-with-variant char 'printable)
840 ;; (char-db-update-comment)
841 (set-buffer-modified-p nil)
842 (view-mode the-buf (lambda (buf)
843 (set-window-configuration
844 what-character-original-window-configuration)
846 (goto-char (point-min)))
848 (set-window-configuration
849 what-character-original-window-configuration)
850 (signal (car err) (cdr err)))))))
852 (provide 'char-db-util)
854 ;;; char-db-util.el ends here