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))
461 (format "%s%S" line-breaking code))))
464 (setq attributes (delq '->uppercase attributes))
466 (when (and (memq '->lowercase attributes)
467 (setq value (get-char-attribute char '->lowercase)))
468 (insert (format "(->lowercase\t%s)%s"
469 (mapconcat (lambda (code)
470 (cond ((symbolp code)
473 (format "#x%04X" code))
475 (format "%s%S" line-breaking code))))
478 (setq attributes (delq '->lowercase attributes))
480 (when (and (memq '->titlecase attributes)
481 (setq value (get-char-attribute char '->titlecase)))
482 (insert (format "(->titlecase\t%s)%s"
483 (mapconcat (lambda (code)
484 (cond ((symbolp code)
487 (format "#x%04X" code))
489 (format "%s%S" line-breaking code))))
492 (setq attributes (delq '->titlecase attributes))
494 (when (and (memq '->mojikyo attributes)
495 (setq value (get-char-attribute char '->mojikyo)))
496 (insert (format "(->mojikyo\t\t. %06d)\t; %c%s"
497 value (decode-char 'mojikyo value)
499 (setq attributes (delq '->mojikyo attributes))
501 (setq rest ccs-attributes)
504 (setq value (get-char-attribute char (car rest)))
506 (if (>= (length (symbol-name (car rest))) 19)
508 (setq has-long-ccs-name t)
512 (setq rest (cdr rest)))
514 (setq name (car attributes))
515 (if (setq value (get-char-attribute char name))
516 (cond ((eq name 'jisx0208-1978/4X)
517 (insert (format "(%-18s . #x%04X)%s"
520 ((string-match "^->" (symbol-name name))
522 (format "(%-18s %s)%s"
524 (mapconcat (lambda (code)
525 (cond ((symbolp code)
528 (format "#x%04X" code))
531 line-breaking code))))
534 ((memq name '(ideograph=
535 original-ideograph-of
539 simplified-ideograph-of
541 ideographic-different-form-of))
542 (insert (format "(%-18s%s " name line-breaking))
543 (setq lbs (concat "\n" (make-string (current-column) ?\ ))
546 (setq cell (car value))
547 (if (and (consp cell)
552 (char-db-insert-alist cell readable)
553 (setq separator lbs))
556 (insert (prin1-to-string cell))
557 (setq separator " "))
558 (setq value (cdr value)))
560 (insert line-breaking))
562 (insert (format "(%-18s " name))
563 (setq lbs (concat "\n" (make-string (current-column) ?\ ))
566 (setq cell (car value))
567 (if (and (consp cell)
569 (setq ret (condition-case nil
577 (setq key (car (car rest)))
578 (if (find-charset key)
579 (setq cal (cons key cal))
580 (setq al (cons key al)))
581 (setq rest (cdr rest)))
584 (insert-char-attributes ret
587 (setq separator lbs))
590 (insert (prin1-to-string cell))
591 (setq separator " "))
592 (setq value (cdr value)))
594 (insert line-breaking))
596 (insert (format "(%-18s . %S)%s"
600 (setq attributes (cdr attributes)))
601 (while ccs-attributes
602 (setq name (car ccs-attributes))
603 (if (setq value (get-char-attribute char name))
606 (cond ((memq name '(ideograph-daikanwa ideograph-gt))
607 (if has-long-ccs-name
608 "(%-26s . %05d)\t; %c%s"
609 "(%-18s . %05d)\t; %c%s"))
611 (if has-long-ccs-name
612 "(%-26s . %06d)\t; %c%s"
613 "(%-18s . %06d)\t; %c%s"))
615 (if has-long-ccs-name
616 "(%-26s . #x%X)\t; %c%s"
617 "(%-18s . #x%X)\t; %c%s")))
619 (if (= (charset-iso-graphic-plane name) 1)
621 (cond ((= (charset-dimension name) 1)
623 ((= (charset-dimension name) 2)
625 ((= (charset-dimension name) 3)
629 (if (and (memq name '(ideograph-gt-pj-1
640 (setq ret (decode-char name value))
641 (setq ret (get-char-attribute ret 'ideograph-gt)))
642 (decode-builtin-char 'ideograph-gt ret)
643 (decode-builtin-char name value))
645 (setq ccs-attributes (cdr ccs-attributes)))
648 (defun insert-char-data (char &optional readable
649 attributes ccs-attributes)
651 (narrow-to-region (point)(point))
652 (insert "(define-char
654 (insert-char-attributes char readable
655 attributes ccs-attributes)
657 (goto-char (point-min))
658 (while (re-search-forward "[ \t]+$" nil t)
660 (goto-char (point-max))
661 (tabify (point-min)(point-max))
665 (defun char-db-update-comment ()
668 (goto-char (point-min))
669 (let (cdef table char)
670 (while (re-search-forward "^[ \t]*\\(([^.()]+)\\)" nil t)
671 (goto-char (match-beginning 1))
672 (setq cdef (read (current-buffer)))
673 (when (find-charset (car cdef))
674 (goto-char (match-end 0))
677 (not (eq (car cdef) 'ideograph-daikanwa))
678 (or (memq (car cdef) '(ascii latin-viscii-upper
682 japanese-jisx0213-2))
683 (= (char-int (charset-iso-final-char (car cdef)))
685 (apply (function make-char) cdef)
686 (if (setq table (charset-mapping-table (car cdef)))
687 (set-charset-mapping-table (car cdef) nil))
689 (apply (function make-char) cdef)
691 (set-charset-mapping-table (car cdef) table)))))
692 (when (not (or (< (char-int char) 32)
693 (and (<= 128 (char-int char))
694 (< (char-int char) 160))))
695 (delete-region (point) (point-at-eol))
696 (insert (format "\t; %c" char)))
699 (defun insert-char-data-with-variant (char &optional script printable
701 (insert-char-data char printable)
702 (let ((variants (or (char-variants char)
703 (let ((ucs (get-char-attribute char '->ucs)))
705 (delete char (char-variants (int-char ucs)))))))
708 (setq variant (car variants))
709 (if (or (null script)
710 (null (setq vs (get-char-attribute variant 'script)))
712 (or (and no-ucs-variant (get-char-attribute variant 'ucs))
713 (insert-char-data variant printable)))
714 (setq variants (cdr variants))
717 (defun insert-char-range-data (min max &optional script)
721 (setq char (decode-char 'ucs code))
722 (if (get-char-attribute char 'ucs)
723 (insert-char-data-with-variant char script nil 'no-ucs-variant))
724 (setq code (1+ code))
727 (defun write-char-range-data-to-file (min max file &optional script)
728 (let ((coding-system-for-write 'utf-8))
730 (insert-char-range-data min max script)
731 (write-region (point-min)(point-max) file))))
733 (defvar what-character-original-window-configuration)
736 (defun what-char-definition (char)
737 (interactive (list (char-after)))
738 (let ((buf (get-buffer-create "*Character Description*"))
739 (the-buf (current-buffer))
740 (win-conf (current-window-configuration)))
742 (make-local-variable 'what-character-original-window-configuration)
743 (setq what-character-original-window-configuration win-conf)
744 (setq buffer-read-only nil)
748 (insert-char-data-with-variant char nil 'printable)
749 ;; (char-db-update-comment)
750 (set-buffer-modified-p nil)
751 (view-mode the-buf (lambda (buf)
752 (set-window-configuration
753 what-character-original-window-configuration)
755 (goto-char (point-min)))
757 (set-window-configuration
758 what-character-original-window-configuration)
759 (signal (car err) (cdr err)))))))
761 (provide 'char-db-util)
763 ;;; char-db-util.el ends here