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 (or (and (>= (charset-iso-final-char ccs) ?0)
137 (setq ret (get-char-attribute char ccs)))
138 (eq ccs 'ideograph-daikanwa))
139 (setq char-spec (cons (cons ccs ret) char-spec))))
140 (if (setq ret (get-char-attribute char 'name))
141 (setq char-spec (cons (cons 'name ret) char-spec)))
144 (setq char-spec char)
148 (setq char (find-char char-spec))
150 ;; define temporary character
151 ;; Current implementation is dirty.
152 (setq temp-char (define-char (cons '(ideograph-daikanwa . 0)
154 (remove-char-attribute temp-char 'ideograph-daikanwa)
155 (setq char temp-char))
159 (setq key (car (car char-spec)))
160 (if (find-charset key)
161 (setq cal (cons key cal))
162 (setq al (cons key al)))
163 (setq char-spec (cdr char-spec)))
164 (insert-char-attributes char
168 ;; undefine temporary character
169 ;; Current implementation is dirty.
170 (setq char-spec (char-attribute-alist temp-char))
172 (remove-char-attribute temp-char (car (car char-spec)))
173 (setq char-spec (cdr char-spec))))))
175 (defun char-db-insert-alist (alist &optional readable column)
177 (setq column (current-column)))
179 (concat "\n" (make-string (1+ column) ?\ )))
182 lbs cell rest separator)
185 (setq name (car (car alist))
186 value (cdr (car alist)))
187 (cond ((eq name 'char)
189 (if (setq ret (condition-case nil
196 (setq key (car (car value)))
197 (if (find-charset key)
198 (setq cal (cons key cal))
199 (setq al (cons key al)))
200 (setq value (cdr value)))
201 (insert-char-attributes ret
204 (insert (prin1-to-string value)))
206 (insert line-breaking))
208 (insert (format "(%-18s " name))
209 (setq lbs (concat "\n" (make-string (current-column) ?\ )))
211 (setq cell (car value))
212 (if (and (consp cell)
214 (setq ret (condition-case nil
223 (setq key (car (car rest)))
224 (if (find-charset key)
225 (setq cal (cons key cal))
226 (setq al (cons key al)))
227 (setq rest (cdr rest)))
230 (insert-char-attributes ret
233 (setq separator lbs))
236 (insert (prin1-to-string cell))
237 (setq separator " "))
238 (setq value (cdr value)))
240 (insert line-breaking))
242 (insert (format "(%-18s . %S)%s"
245 (setq alist (cdr alist))))
248 (defun char-db-insert-char-reference (plist &optional readable column)
250 (setq column (current-column)))
252 (concat "\n" (make-string (1+ column) ?\ )))
256 (setq name (pop plist))
257 (setq value (pop plist))
258 (cond ((eq name :char)
260 (cond ((numberp value)
261 (setq value (decode-char 'ucs value)))
263 ;; (setq value (or (find-char value)
266 (char-db-insert-char-spec value readable)
267 (insert line-breaking))
269 (insert (format "%s\t%S%s"
275 (defun char-db-decode-isolated-char (ccs code-point)
278 (cond ((eq ccs 'arabic-iso8859-6)
279 (decode-char ccs code-point))
280 ((and (memq ccs '(ideograph-gt-pj-1
291 (setq ret (decode-char ccs code-point))
292 (setq ret (get-char-attribute ret 'ideograph-gt)))
293 (decode-builtin-char 'ideograph-gt ret))
295 (decode-builtin-char ccs code-point))))
296 (cond ((and (<= 0 (char-int ret))
297 (<= (char-int ret) #x1F))
298 (decode-char 'ucs (+ #x2400 (char-int ret))))
299 ((= (char-int ret) #x7F)
303 (defvar char-db-convert-obsolete-format t)
305 (defun insert-char-attributes (char &optional readable
306 attributes ccs-attributes
310 (if (consp attributes)
311 (copy-sequence attributes))
312 (char-attribute-list))
313 #'char-attribute-name<))
315 (sort (if ccs-attributes
316 (copy-sequence ccs-attributes)
318 #'char-attribute-name<))
320 (setq column (current-column)))
321 (let (name value has-long-ccs-name rest
324 (concat "\n" (make-string (1+ column) ?\ )))
325 lbs cell separator ret
328 (when (and (memq 'name attributes)
329 (setq value (get-char-attribute char 'name)))
331 (if (> (+ (current-column) (length value)) 48)
334 value line-breaking))
335 (setq attributes (delq 'name attributes))
337 (when (and (memq 'script attributes)
338 (setq value (get-char-attribute char 'script)))
339 (insert (format "(script\t\t%s)%s"
340 (mapconcat (function prin1-to-string)
343 (setq attributes (delq 'script attributes))
345 (when (and (memq '=>ucs attributes)
346 (setq value (get-char-attribute char '=>ucs)))
347 (insert (format "(=>ucs\t\t. #x%04X)\t; %c%s"
348 value (decode-char 'ucs value)
350 (setq attributes (delq '=>ucs attributes))
352 (when (and (memq '->ucs attributes)
353 (setq value (get-char-attribute char '->ucs)))
354 (insert (format (if char-db-convert-obsolete-format
355 "(=>ucs\t\t. #x%04X)\t; %c%s"
356 "(->ucs\t\t. #x%04X)\t; %c%s")
357 value (decode-char 'ucs value)
359 (setq attributes (delq '->ucs attributes))
361 (when (and (memq 'general-category attributes)
362 (setq value (get-char-attribute char 'general-category)))
364 "(general-category\t%s) ; %s%s"
365 (mapconcat (lambda (cell)
368 (cond ((rassoc value unidata-normative-category-alist)
369 "Normative Category")
370 ((rassoc value unidata-informative-category-alist)
371 "Informative Category")
375 (setq attributes (delq 'general-category attributes))
377 (when (and (memq 'bidi-category attributes)
378 (setq value (get-char-attribute char 'bidi-category)))
379 (insert (format "(bidi-category\t. %S)%s"
382 (setq attributes (delq 'bidi-category attributes))
384 (unless (or (not (memq 'mirrored attributes))
385 (eq (setq value (get-char-attribute char 'mirrored 'empty))
387 (insert (format "(mirrored\t\t. %S)%s"
390 (setq attributes (delq 'mirrored attributes))
393 ((and (memq 'decimal-digit-value attributes)
394 (setq value (get-char-attribute char 'decimal-digit-value)))
395 (insert (format "(decimal-digit-value . %S)%s"
398 (setq attributes (delq 'decimal-digit-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))
415 (when (and (memq 'digit-value attributes)
416 (setq value (get-char-attribute char 'digit-value)))
417 (insert (format "(digit-value\t. %S)%s"
420 (setq attributes (delq 'digit-value attributes))
422 (when (and (memq 'numeric-value attributes)
423 (setq value (get-char-attribute char 'numeric-value)))
424 (insert (format "(numeric-value\t. %S)%s"
427 (setq attributes (delq 'numeric-value attributes))
429 (when (and (memq 'iso-10646-comment attributes)
430 (setq value (get-char-attribute char 'iso-10646-comment)))
431 (insert (format "(iso-10646-comment\t. %S)%s"
434 (setq attributes (delq 'iso-10646-comment attributes))
436 (when (and (memq 'morohashi-daikanwa attributes)
437 (setq value (get-char-attribute char 'morohashi-daikanwa)))
438 (insert (format "(morohashi-daikanwa\t%s)%s"
439 (mapconcat (function prin1-to-string) value " ")
441 (setq attributes (delq 'morohashi-daikanwa attributes))
445 (when (and (memq 'ideographic-radical attributes)
446 (setq value (get-char-attribute char 'ideographic-radical)))
448 (insert (format "(ideographic-radical . %S)\t; %c%s"
450 (aref ideographic-radicals radical)
452 (setq attributes (delq 'ideographic-radical attributes))
454 (when (and (memq 'ideographic-strokes attributes)
455 (setq value (get-char-attribute char 'ideographic-strokes)))
457 (insert (format "(ideographic-strokes . %S)%s"
460 (setq attributes (delq 'ideographic-strokes attributes))
462 (when (and (memq 'kangxi-radical attributes)
463 (setq value (get-char-attribute char 'kangxi-radical)))
464 (unless (eq value radical)
465 (insert (format "(kangxi-radical\t . %S)\t; %c%s"
467 (aref ideographic-radicals value)
470 (setq radical value)))
471 (setq attributes (delq 'kangxi-radical attributes))
473 (when (and (memq 'kangxi-strokes attributes)
474 (setq value (get-char-attribute char 'kangxi-strokes)))
475 (unless (eq value strokes)
476 (insert (format "(kangxi-strokes\t . %S)%s"
480 (setq strokes value)))
481 (setq attributes (delq 'kangxi-strokes attributes))
483 (when (and (memq 'japanese-radical attributes)
484 (setq value (get-char-attribute char 'japanese-radical)))
485 (unless (eq value radical)
486 (insert (format "(japanese-radical\t . %S)\t; %c%s"
488 (aref ideographic-radicals value)
491 (setq radical value)))
492 (setq attributes (delq 'japanese-radical attributes))
494 (when (and (memq 'japanese-strokes attributes)
495 (setq value (get-char-attribute char 'japanese-strokes)))
496 (unless (eq value strokes)
497 (insert (format "(japanese-strokes\t . %S)%s"
501 (setq strokes value)))
502 (setq attributes (delq 'japanese-strokes attributes))
504 (when (and (memq 'cns-radical attributes)
505 (setq value (get-char-attribute char 'cns-radical)))
506 (insert (format "(cns-radical\t . %S)\t; %c%s"
508 (aref ideographic-radicals value)
510 (setq attributes (delq 'cns-radical attributes))
512 (when (and (memq 'cns-strokes attributes)
513 (setq value (get-char-attribute char 'cns-strokes)))
514 (unless (eq value strokes)
515 (insert (format "(cns-strokes\t . %S)%s"
519 (setq strokes value)))
520 (setq attributes (delq 'cns-strokes attributes))
522 (when (and (memq 'shinjigen-1-radical attributes)
523 (setq value (get-char-attribute char 'shinjigen-1-radical)))
524 (unless (eq value radical)
525 (insert (format "(shinjigen-1-radical . %S)\t; %c%s"
527 (aref ideographic-radicals value)
530 (setq radical value)))
531 (setq attributes (delq 'shinjigen-1-radical attributes))
533 (when (and (memq 'total-strokes attributes)
534 (setq value (get-char-attribute char 'total-strokes)))
535 (insert (format "(total-strokes . %S)%s"
538 (setq attributes (delq 'total-strokes attributes))
540 (when (and (memq '->ideograph attributes)
541 (setq value (get-char-attribute char '->ideograph)))
542 (insert (format "(->ideograph\t%s)%s"
543 (mapconcat (lambda (code)
544 (cond ((symbolp code)
547 (format "#x%04X" code))
550 line-breaking code))))
553 (setq attributes (delq '->ideograph attributes))
555 (when (and (memq '->decomposition attributes)
556 (setq value (get-char-attribute char '->decomposition)))
557 (insert (format "(->decomposition\t%s)%s"
558 (mapconcat (lambda (code)
559 (cond ((symbolp code)
568 (format "#x%04X" code))
570 (format "%s%S" line-breaking code))))
573 (setq attributes (delq '->decomposition attributes))
575 (if (equal (get-char-attribute char '->titlecase)
576 (get-char-attribute char '->uppercase))
577 (setq attributes (delq '->titlecase attributes)))
578 (when (and (memq '->mojikyo attributes)
579 (setq value (get-char-attribute char '->mojikyo)))
580 (insert (format "(->mojikyo\t\t. %06d)\t; %c%s"
581 value (decode-char 'mojikyo value)
583 (setq attributes (delq '->mojikyo attributes))
585 (setq rest ccs-attributes)
588 (setq value (get-char-attribute char (car rest)))
590 (if (>= (length (symbol-name (car rest))) 19)
592 (setq has-long-ccs-name t)
596 (setq rest (cdr rest)))
598 (setq name (car attributes))
599 (if (setq value (get-char-attribute char name))
600 (cond ((eq name 'jisx0208-1978/4X)
601 (insert (format "(%-18s . #x%04X)%s"
604 ((memq name '(->lowercase
605 ->uppercase ->titlecase
606 ->fullwidth <-fullwidth
607 ->vulgar-ideograph <-vulgar-ideograph
608 ->ancient-ideograph <-ancient-ideograph
609 ->original-ideograph <-original-ideograph
610 ->simplified-ideograph <-simplified-ideograph
611 ->wrong-ideograph <-wrong-ideograph
613 ->ideographic-variants
616 ->bopomofo <-bopomofo
617 ->ideographic <-ideographic
618 ideographic-structure))
619 (insert (format "(%-18s%s " name line-breaking))
620 (setq lbs (concat "\n" (make-string (current-column) ?\ ))
623 (setq cell (car value))
625 (setq cell (decode-char 'ucs cell)))
626 (cond ((characterp cell)
629 (char-db-insert-char-spec cell readable)
630 (setq separator lbs))
634 (if (consp (car cell))
635 (char-db-insert-char-spec cell readable)
636 (char-db-insert-char-reference cell readable))
637 (setq separator lbs))
641 (insert (prin1-to-string cell))
642 (setq separator " ")))
643 (setq value (cdr value)))
645 (insert line-breaking))
646 ((memq name '(ideograph=
647 original-ideograph-of
651 simplified-ideograph-of
653 ideographic-different-form-of))
654 (insert (format "(%-18s%s " name line-breaking))
655 (setq lbs (concat "\n" (make-string (current-column) ?\ ))
658 (setq cell (car value))
659 (if (and (consp cell)
664 (char-db-insert-alist cell readable)
665 (setq separator lbs))
668 (insert (prin1-to-string cell))
669 (setq separator " "))
670 (setq value (cdr value)))
672 (insert line-breaking))
673 ((string-match "^->" (symbol-name name))
675 (format "(%-18s %s)%s"
677 (mapconcat (lambda (code)
678 (cond ((symbolp code)
681 (format "#x%04X" code))
684 line-breaking code))))
688 (insert (format "(%-18s " name))
689 (setq lbs (concat "\n" (make-string (current-column) ?\ ))
692 (setq cell (car value))
693 (if (and (consp cell)
695 (setq ret (condition-case nil
703 (setq key (car (car rest)))
704 (if (find-charset key)
705 (setq cal (cons key cal))
706 (setq al (cons key al)))
707 (setq rest (cdr rest)))
710 (insert-char-attributes ret
713 (setq separator lbs))
716 (insert (prin1-to-string cell))
717 (setq separator " "))
718 (setq value (cdr value)))
720 (insert line-breaking))
722 (insert (format "(%-18s . %S)%s"
726 (setq attributes (cdr attributes)))
727 (while ccs-attributes
728 (setq name (car ccs-attributes))
729 (if (and (eq name (charset-name name))
730 (setq value (get-char-attribute char name)))
733 (cond ((memq name '(ideograph-daikanwa ideograph-gt))
734 (if has-long-ccs-name
735 "(%-26s . %05d)\t; %c%s"
736 "(%-18s . %05d)\t; %c%s"))
738 (if has-long-ccs-name
739 "(%-26s . %06d)\t; %c%s"
740 "(%-18s . %06d)\t; %c%s"))
742 (if has-long-ccs-name
743 "(%-26s . #x%04X)\t; %c%s"
744 "(%-18s . #x%04X)\t; %c%s"))
746 (if has-long-ccs-name
747 "(%-26s . #x%02X)\t; %c%s"
748 "(%-18s . #x%02X)\t; %c%s")))
750 (if (= (charset-iso-graphic-plane name) 1)
752 (cond ((= (charset-dimension name) 1)
754 ((= (charset-dimension name) 2)
756 ((= (charset-dimension name) 3)
760 (char-db-decode-isolated-char name value)
762 (setq ccs-attributes (cdr ccs-attributes)))
765 (defun insert-char-data (char &optional readable
766 attributes ccs-attributes)
768 (narrow-to-region (point)(point))
769 (insert "(define-char
771 (insert-char-attributes char readable
772 attributes ccs-attributes)
774 (goto-char (point-min))
775 (while (re-search-forward "[ \t]+$" nil t)
777 (goto-char (point-max))
778 (tabify (point-min)(point-max))
782 (defun char-db-update-comment ()
785 (goto-char (point-min))
786 (let (cdef table char)
787 (while (re-search-forward "^[ \t]*\\(([^.()]+)\\)" nil t)
788 (goto-char (match-beginning 1))
789 (setq cdef (read (current-buffer)))
790 (when (find-charset (car cdef))
791 (goto-char (match-end 0))
794 (not (eq (car cdef) 'ideograph-daikanwa))
795 (or (memq (car cdef) '(ascii latin-viscii-upper
799 japanese-jisx0213-2))
800 (= (char-int (charset-iso-final-char (car cdef)))
802 (apply (function make-char) cdef)
803 (if (setq table (charset-mapping-table (car cdef)))
804 (set-charset-mapping-table (car cdef) nil))
806 (apply (function make-char) cdef)
808 (set-charset-mapping-table (car cdef) table)))))
809 (when (not (or (< (char-int char) 32)
810 (and (<= 128 (char-int char))
811 (< (char-int char) 160))))
812 (delete-region (point) (point-at-eol))
813 (insert (format "\t; %c" char)))
816 (defun insert-char-data-with-variant (char &optional printable
818 script excluded-script)
819 (insert-char-data char printable)
820 (let ((variants (or (char-variants char)
821 (let ((ucs (get-char-attribute char '->ucs)))
823 (delete char (char-variants (int-char ucs)))))))
825 (setq variants (sort variants #'<))
827 (setq variant (car variants))
828 (if (and (or (null script)
829 (null (setq vs (get-char-attribute variant 'script)))
831 (or (null excluded-script)
832 (null (setq vs (get-char-attribute variant 'script)))
833 (not (memq excluded-script vs))))
834 (or (and no-ucs-variant (get-char-attribute variant 'ucs))
835 (insert-char-data variant printable)))
836 (setq variants (cdr variants))
839 (defun insert-char-range-data (min max &optional script excluded-script)
843 (setq char (decode-char 'ucs code))
844 (if (get-char-attribute char 'ucs)
845 (insert-char-data-with-variant char nil 'no-ucs-variant
846 script excluded-script))
847 (setq code (1+ code))
850 (defun write-char-range-data-to-file (min max file
851 &optional script excluded-script)
852 (let ((coding-system-for-write 'utf-8))
854 (insert-char-range-data min max script excluded-script)
855 (write-region (point-min)(point-max) file))))
857 (defvar what-character-original-window-configuration)
860 (defun what-char-definition (char)
861 (interactive (list (char-after)))
862 (let ((buf (get-buffer-create "*Character Description*"))
863 (the-buf (current-buffer))
864 (win-conf (current-window-configuration)))
866 (make-local-variable 'what-character-original-window-configuration)
867 (setq what-character-original-window-configuration win-conf)
868 (setq buffer-read-only nil)
872 (insert-char-data-with-variant char 'printable)
873 ;; (char-db-update-comment)
874 (set-buffer-modified-p nil)
875 (view-mode the-buf (lambda (buf)
876 (set-window-configuration
877 what-character-original-window-configuration)
879 (goto-char (point-min)))
881 (set-window-configuration
882 what-character-original-window-configuration)
883 (signal (car err) (cdr err)))))))
885 (provide 'char-db-util)
887 ;;; char-db-util.el ends here