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 ((<= (charset-chars ka)(charset-chars kb)))))
99 (< (charset-dimension ka)
100 (charset-dimension kb))
110 (string< (symbol-name ka)
116 (defun char-db-insert-alist (alist &optional readable column)
118 (setq column (current-column)))
120 (concat "\n" (make-string (1+ column) ?\ )))
123 lbs cell rest separator)
126 (setq name (car (car alist))
127 value (cdr (car alist)))
128 (cond ((eq name 'char)
130 (if (setq ret (condition-case nil
137 (setq key (car (car value)))
138 (if (find-charset key)
139 (setq cal (cons key cal))
140 (setq al (cons key al)))
141 (setq value (cdr value)))
142 (insert-char-attributes ret
145 (insert (prin1-to-string value)))
147 (insert line-breaking))
149 (insert (format "(%-18s " name))
150 (setq lbs (concat "\n" (make-string (current-column) ?\ )))
152 (setq cell (car value))
153 (if (and (consp cell)
155 (setq ret (condition-case nil
164 (setq key (car (car rest)))
165 (if (find-charset key)
166 (setq cal (cons key cal))
167 (setq al (cons key al)))
168 (setq rest (cdr rest)))
171 (insert-char-attributes ret
174 (setq separator lbs))
177 (insert (prin1-to-string cell))
178 (setq separator " "))
179 (setq value (cdr value)))
181 (insert line-breaking))
183 (insert (format "(%-18s . %S)%s"
186 (setq alist (cdr alist))))
189 (defun insert-char-attributes (char &optional readable
190 attributes ccs-attributes
194 (if (consp attributes)
195 (copy-sequence attributes))
196 (sort (char-attribute-list) #'char-attribute-name<)))
199 (copy-sequence ccs-attributes)
200 (sort (charset-list) #'char-attribute-name<)))
202 (setq column (current-column)))
203 (let (name value has-long-ccs-name rest
206 (concat "\n" (make-string (1+ column) ?\ )))
207 lbs cell separator ret
210 (when (and (memq 'name attributes)
211 (setq value (get-char-attribute char 'name)))
213 (if (> (length value) 47)
216 value line-breaking))
217 (setq attributes (delq 'name attributes))
219 (when (and (memq 'script attributes)
220 (setq value (get-char-attribute char 'script)))
221 (insert (format "(script\t\t%s)%s"
222 (mapconcat (function prin1-to-string)
225 (setq attributes (delq 'script attributes))
227 (when (and (memq '->ucs attributes)
228 (setq value (get-char-attribute char '->ucs)))
229 (insert (format "(->ucs\t\t. #x%04X)\t; %c%s"
230 value (decode-char 'ucs value)
232 (setq attributes (delq '->ucs attributes))
234 (when (and (memq 'general-category attributes)
235 (setq value (get-char-attribute char 'general-category)))
237 "(general-category\t%s) ; %s%s"
238 (mapconcat (lambda (cell)
241 (cond ((rassoc value unidata-normative-category-alist)
242 "Normative Category")
243 ((rassoc value unidata-informative-category-alist)
244 "Informative Category")
248 (setq attributes (delq 'general-category attributes))
250 (when (and (memq 'bidi-category attributes)
251 (setq value (get-char-attribute char 'bidi-category)))
252 (insert (format "(bidi-category\t. %S)%s"
255 (setq attributes (delq 'bidi-category attributes))
257 (unless (or (not (memq 'mirrored attributes))
258 (eq (setq value (get-char-attribute char 'mirrored 'empty))
260 (insert (format "(mirrored\t\t. %S)%s"
263 (setq attributes (delq 'mirrored attributes))
266 ((and (memq 'decimal-digit-value attributes)
267 (setq value (get-char-attribute char 'decimal-digit-value)))
268 (insert (format "(decimal-digit-value . %S)%s"
271 (setq attributes (delq 'decimal-digit-value attributes))
272 (when (and (memq 'digit-value attributes)
273 (setq value (get-char-attribute char 'digit-value)))
274 (insert (format "(digit-value\t . %S)%s"
277 (setq attributes (delq 'digit-value attributes))
279 (when (and (memq 'numeric-value attributes)
280 (setq value (get-char-attribute char 'numeric-value)))
281 (insert (format "(numeric-value\t . %S)%s"
284 (setq attributes (delq 'numeric-value attributes))
288 (when (and (memq 'digit-value attributes)
289 (setq value (get-char-attribute char 'digit-value)))
290 (insert (format "(digit-value\t. %S)%s"
293 (setq attributes (delq 'digit-value attributes))
295 (when (and (memq 'numeric-value attributes)
296 (setq value (get-char-attribute char 'numeric-value)))
297 (insert (format "(numeric-value\t. %S)%s"
300 (setq attributes (delq 'numeric-value attributes))
302 (when (and (memq 'iso-10646-comment attributes)
303 (setq value (get-char-attribute char 'iso-10646-comment)))
304 (insert (format "(iso-10646-comment\t. %S)%s"
307 (setq attributes (delq 'iso-10646-comment attributes))
309 (when (and (memq 'morohashi-daikanwa attributes)
310 (setq value (get-char-attribute char 'morohashi-daikanwa)))
311 (insert (format "(morohashi-daikanwa\t%s)%s"
312 (mapconcat (function prin1-to-string) value " ")
314 (setq attributes (delq 'morohashi-daikanwa attributes))
318 (when (and (memq 'ideographic-radical attributes)
319 (setq value (get-char-attribute char 'ideographic-radical)))
321 (insert (format "(ideographic-radical . %S)\t; %c%s"
323 (aref ideographic-radicals radical)
325 (setq attributes (delq 'ideographic-radical attributes))
327 (when (and (memq 'ideographic-strokes attributes)
328 (setq value (get-char-attribute char 'ideographic-strokes)))
330 (insert (format "(ideographic-strokes . %S)%s"
333 (setq attributes (delq 'ideographic-strokes attributes))
335 (when (and (memq 'kangxi-radical attributes)
336 (setq value (get-char-attribute char 'kangxi-radical)))
337 (unless (eq value radical)
338 (insert (format "(kangxi-radical\t . %S)\t; %c%s"
340 (aref ideographic-radicals value)
343 (setq radical value)))
344 (setq attributes (delq 'kangxi-radical attributes))
346 (when (and (memq 'kangxi-strokes attributes)
347 (setq value (get-char-attribute char 'kangxi-strokes)))
348 (unless (eq value strokes)
349 (insert (format "(kangxi-strokes\t . %S)%s"
353 (setq strokes value)))
354 (setq attributes (delq 'kangxi-strokes attributes))
356 (when (and (memq 'japanese-radical attributes)
357 (setq value (get-char-attribute char 'japanese-radical)))
358 (unless (eq value radical)
359 (insert (format "(japanese-radical\t . %S)\t; %c%s"
361 (aref ideographic-radicals value)
364 (setq radical value)))
365 (setq attributes (delq 'japanese-radical attributes))
367 (when (and (memq 'japanese-strokes attributes)
368 (setq value (get-char-attribute char 'japanese-strokes)))
369 (unless (eq value strokes)
370 (insert (format "(japanese-strokes\t . %S)%s"
374 (setq strokes value)))
375 (setq attributes (delq 'japanese-strokes attributes))
377 (when (and (memq 'cns-radical attributes)
378 (setq value (get-char-attribute char 'cns-radical)))
379 (insert (format "(cns-radical\t . %S)\t; %c%s"
381 (aref ideographic-radicals value)
383 (setq attributes (delq 'cns-radical attributes))
385 (when (and (memq 'cns-strokes attributes)
386 (setq value (get-char-attribute char 'cns-strokes)))
387 (unless (eq value strokes)
388 (insert (format "(cns-strokes\t . %S)%s"
392 (setq strokes value)))
393 (setq attributes (delq 'cns-strokes attributes))
395 (when (and (memq 'shinjigen-1-radical attributes)
396 (setq value (get-char-attribute char 'shinjigen-1-radical)))
397 (unless (eq value radical)
398 (insert (format "(shinjigen-1-radical . %S)\t; %c%s"
400 (aref ideographic-radicals value)
403 (setq radical value)))
404 (setq attributes (delq 'shinjigen-1-radical attributes))
406 (when (and (memq 'total-strokes attributes)
407 (setq value (get-char-attribute char 'total-strokes)))
408 (insert (format "(total-strokes . %S)%s"
411 (setq attributes (delq 'total-strokes attributes))
413 (when (and (memq '->ideograph attributes)
414 (setq value (get-char-attribute char '->ideograph)))
415 (insert (format "(->ideograph\t%s)%s"
416 (mapconcat (lambda (code)
417 (cond ((symbolp code)
420 (format "#x%04X" code))
422 (format "%s%S" line-breaking code))))
425 (setq attributes (delq '->ideograph attributes))
427 (when (and (memq '->decomposition attributes)
428 (setq value (get-char-attribute char '->decomposition)))
429 (insert (format "(->decomposition\t%s)%s"
430 (mapconcat (lambda (code)
431 (cond ((symbolp code)
440 (format "#x%04X" code))
442 (format "%s%S" line-breaking code))))
445 (setq attributes (delq '->decomposition attributes))
447 (when (and (memq '->uppercase attributes)
448 (setq value (get-char-attribute char '->uppercase)))
449 (insert (format "(->uppercase\t%s)%s"
450 (mapconcat (lambda (code)
451 (cond ((symbolp code)
454 (format "#x%04X" code))
456 (format "%s%S" line-breaking code))))
459 (setq attributes (delq '->uppercase attributes))
461 (when (and (memq '->lowercase attributes)
462 (setq value (get-char-attribute char '->lowercase)))
463 (insert (format "(->lowercase\t%s)%s"
464 (mapconcat (lambda (code)
465 (cond ((symbolp code)
468 (format "#x%04X" code))
470 (format "%s%S" line-breaking code))))
473 (setq attributes (delq '->lowercase attributes))
475 (when (and (memq '->titlecase attributes)
476 (setq value (get-char-attribute char '->titlecase)))
477 (insert (format "(->titlecase\t%s)%s"
478 (mapconcat (lambda (code)
479 (cond ((symbolp code)
482 (format "#x%04X" code))
484 (format "%s%S" line-breaking code))))
487 (setq attributes (delq '->titlecase attributes))
489 (when (and (memq '->mojikyo attributes)
490 (setq value (get-char-attribute char '->mojikyo)))
491 (insert (format "(->mojikyo\t\t. %06d)\t; %c%s"
492 value (decode-char 'mojikyo value)
494 (setq attributes (delq '->mojikyo attributes))
496 (setq rest ccs-attributes)
499 (setq value (get-char-attribute char (car rest)))
501 (if (>= (length (symbol-name (car rest))) 19)
503 (setq has-long-ccs-name t)
507 (setq rest (cdr rest)))
509 (setq name (car attributes))
510 (if (setq value (get-char-attribute char name))
511 (cond ((eq name 'jisx0208-1978/4X)
512 (insert (format "(%-18s . #x%04X)%s"
515 ((string-match "^->" (symbol-name name))
517 (format "(%-18s %s)%s"
519 (mapconcat (lambda (code)
520 (cond ((symbolp code)
523 (format "#x%04X" code))
526 line-breaking code))))
529 ((memq name '(ideograph=
530 original-ideograph-of
532 vulgar-ideograph-of))
533 (insert (format "(%-18s%s " name line-breaking))
534 (setq lbs (concat "\n" (make-string (current-column) ?\ ))
537 (setq cell (car value))
538 (if (and (consp cell)
543 (char-db-insert-alist cell readable)
544 (setq separator lbs))
547 (insert (prin1-to-string cell))
548 (setq separator " "))
549 (setq value (cdr value)))
551 (insert line-breaking))
553 (insert (format "(%-18s " name))
554 (setq lbs (concat "\n" (make-string (current-column) ?\ ))
557 (setq cell (car value))
558 (if (and (consp cell)
560 (setq ret (condition-case nil
568 (setq key (car (car rest)))
569 (if (find-charset key)
570 (setq cal (cons key cal))
571 (setq al (cons key al)))
572 (setq rest (cdr rest)))
575 (insert-char-attributes ret
578 (setq separator lbs))
581 (insert (prin1-to-string cell))
582 (setq separator " "))
583 (setq value (cdr value)))
585 (insert line-breaking))
587 (insert (format "(%-18s . %S)%s"
591 (setq attributes (cdr attributes)))
592 (while ccs-attributes
593 (setq name (car ccs-attributes))
594 (if (setq value (get-char-attribute char name))
597 (if has-long-ccs-name
598 (cond ((eq name 'ideograph-daikanwa)
599 "(%-26s . %05d)\t; %c%s"
602 "(%-26s . %06d)\t; %c%s"
605 "(%-26s . #x%X)\t; %c%s"
607 (cond ((eq name 'ideograph-daikanwa)
608 "(%-18s . %05d)\t; %c%s"
611 "(%-18s . %06d)\t; %c%s"
614 "(%-18s . #x%X)\t; %c%s"
617 (if (= (charset-iso-graphic-plane name) 1)
619 (cond ((= (charset-dimension name) 1)
621 ((= (charset-dimension name) 2)
623 ((= (charset-dimension name) 3)
627 (decode-builtin-char name value)
629 (setq ccs-attributes (cdr ccs-attributes)))
632 (defun insert-char-data (char &optional readable
633 attributes ccs-attributes)
635 (narrow-to-region (point)(point))
636 (insert "(define-char
638 (insert-char-attributes char readable
639 attributes ccs-attributes)
641 (goto-char (point-min))
642 (while (re-search-forward "[ \t]+$" nil t)
644 (goto-char (point-max))
645 (tabify (point-min)(point-max))
649 (defun char-db-update-comment ()
652 (goto-char (point-min))
653 (let (cdef table char)
654 (while (re-search-forward "^[ \t]*\\(([^.()]+)\\)" nil t)
655 (goto-char (match-beginning 1))
656 (setq cdef (read (current-buffer)))
657 (when (find-charset (car cdef))
658 (goto-char (match-end 0))
661 (not (eq (car cdef) 'ideograph-daikanwa))
662 (or (memq (car cdef) '(ascii latin-viscii-upper
666 japanese-jisx0213-2))
667 (= (char-int (charset-iso-final-char (car cdef)))
669 (apply (function make-char) cdef)
670 (if (setq table (charset-mapping-table (car cdef)))
671 (set-charset-mapping-table (car cdef) nil))
673 (apply (function make-char) cdef)
675 (set-charset-mapping-table (car cdef) table)))))
676 (when (not (or (< (char-int char) 32)
677 (and (<= 128 (char-int char))
678 (< (char-int char) 160))))
679 (delete-region (point) (point-at-eol))
680 (insert (format "\t; %c" char)))
683 (defun insert-char-data-with-variant (char &optional script printable
685 (insert-char-data char printable)
686 (let ((variants (or (char-variants char)
687 (let ((ucs (get-char-attribute char '->ucs)))
689 (delete char (char-variants (int-char ucs)))))))
692 (setq variant (car variants))
693 (if (or (null script)
694 (null (setq vs (get-char-attribute variant 'script)))
696 (or (and no-ucs-variant (get-char-attribute variant 'ucs))
697 (insert-char-data variant printable)))
698 (setq variants (cdr variants))
701 (defun insert-char-range-data (min max &optional script)
705 (setq char (decode-char 'ucs code))
706 (if (get-char-attribute char 'ucs)
707 (insert-char-data-with-variant char script nil 'no-ucs-variant))
708 (setq code (1+ code))
711 (defun write-char-range-data-to-file (min max file &optional script)
712 (let ((coding-system-for-write 'utf-8))
714 (insert-char-range-data min max script)
715 (write-region (point-min)(point-max) file))))
717 (defvar what-character-original-window-configuration)
720 (defun what-char-definition (char)
721 (interactive (list (char-after)))
722 (let ((buf (get-buffer-create "*Character Description*"))
723 (the-buf (current-buffer))
724 (win-conf (current-window-configuration)))
726 (make-local-variable 'what-character-original-window-configuration)
727 (setq what-character-original-window-configuration win-conf)
728 (setq buffer-read-only nil)
732 (insert-char-data-with-variant char nil 'printable)
733 ;; (char-db-update-comment)
734 (set-buffer-modified-p nil)
735 (view-mode the-buf (lambda (buf)
736 (set-window-configuration
737 what-character-original-window-configuration)
739 (goto-char (point-min)))
741 (set-window-configuration
742 what-character-original-window-configuration)
743 (signal (car err) (cdr err)))))))
745 (provide 'char-db-util)
747 ;;; char-db-util.el ends here