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 (char-attribute-list))
197 #'char-attribute-name<))
199 (sort (if ccs-attributes
200 (copy-sequence ccs-attributes)
202 #'char-attribute-name<))
204 (setq column (current-column)))
205 (let (name value has-long-ccs-name rest
208 (concat "\n" (make-string (1+ column) ?\ )))
209 lbs cell separator ret
212 (when (and (memq 'name attributes)
213 (setq value (get-char-attribute char 'name)))
215 (if (> (length value) 47)
218 value line-breaking))
219 (setq attributes (delq 'name attributes))
221 (when (and (memq 'script attributes)
222 (setq value (get-char-attribute char 'script)))
223 (insert (format "(script\t\t%s)%s"
224 (mapconcat (function prin1-to-string)
227 (setq attributes (delq 'script attributes))
229 (when (and (memq '->ucs attributes)
230 (setq value (get-char-attribute char '->ucs)))
231 (insert (format "(->ucs\t\t. #x%04X)\t; %c%s"
232 value (decode-char 'ucs value)
234 (setq attributes (delq '->ucs attributes))
236 (when (and (memq 'general-category attributes)
237 (setq value (get-char-attribute char 'general-category)))
239 "(general-category\t%s) ; %s%s"
240 (mapconcat (lambda (cell)
243 (cond ((rassoc value unidata-normative-category-alist)
244 "Normative Category")
245 ((rassoc value unidata-informative-category-alist)
246 "Informative Category")
250 (setq attributes (delq 'general-category attributes))
252 (when (and (memq 'bidi-category attributes)
253 (setq value (get-char-attribute char 'bidi-category)))
254 (insert (format "(bidi-category\t. %S)%s"
257 (setq attributes (delq 'bidi-category attributes))
259 (unless (or (not (memq 'mirrored attributes))
260 (eq (setq value (get-char-attribute char 'mirrored 'empty))
262 (insert (format "(mirrored\t\t. %S)%s"
265 (setq attributes (delq 'mirrored attributes))
268 ((and (memq 'decimal-digit-value attributes)
269 (setq value (get-char-attribute char 'decimal-digit-value)))
270 (insert (format "(decimal-digit-value . %S)%s"
273 (setq attributes (delq 'decimal-digit-value attributes))
274 (when (and (memq 'digit-value attributes)
275 (setq value (get-char-attribute char 'digit-value)))
276 (insert (format "(digit-value\t . %S)%s"
279 (setq attributes (delq 'digit-value attributes))
281 (when (and (memq 'numeric-value attributes)
282 (setq value (get-char-attribute char 'numeric-value)))
283 (insert (format "(numeric-value\t . %S)%s"
286 (setq attributes (delq 'numeric-value attributes))
290 (when (and (memq 'digit-value attributes)
291 (setq value (get-char-attribute char 'digit-value)))
292 (insert (format "(digit-value\t. %S)%s"
295 (setq attributes (delq 'digit-value attributes))
297 (when (and (memq 'numeric-value attributes)
298 (setq value (get-char-attribute char 'numeric-value)))
299 (insert (format "(numeric-value\t. %S)%s"
302 (setq attributes (delq 'numeric-value attributes))
304 (when (and (memq 'iso-10646-comment attributes)
305 (setq value (get-char-attribute char 'iso-10646-comment)))
306 (insert (format "(iso-10646-comment\t. %S)%s"
309 (setq attributes (delq 'iso-10646-comment attributes))
311 (when (and (memq 'morohashi-daikanwa attributes)
312 (setq value (get-char-attribute char 'morohashi-daikanwa)))
313 (insert (format "(morohashi-daikanwa\t%s)%s"
314 (mapconcat (function prin1-to-string) value " ")
316 (setq attributes (delq 'morohashi-daikanwa attributes))
320 (when (and (memq 'ideographic-radical attributes)
321 (setq value (get-char-attribute char 'ideographic-radical)))
323 (insert (format "(ideographic-radical . %S)\t; %c%s"
325 (aref ideographic-radicals radical)
327 (setq attributes (delq 'ideographic-radical attributes))
329 (when (and (memq 'ideographic-strokes attributes)
330 (setq value (get-char-attribute char 'ideographic-strokes)))
332 (insert (format "(ideographic-strokes . %S)%s"
335 (setq attributes (delq 'ideographic-strokes attributes))
337 (when (and (memq 'kangxi-radical attributes)
338 (setq value (get-char-attribute char 'kangxi-radical)))
339 (unless (eq value radical)
340 (insert (format "(kangxi-radical\t . %S)\t; %c%s"
342 (aref ideographic-radicals value)
345 (setq radical value)))
346 (setq attributes (delq 'kangxi-radical attributes))
348 (when (and (memq 'kangxi-strokes attributes)
349 (setq value (get-char-attribute char 'kangxi-strokes)))
350 (unless (eq value strokes)
351 (insert (format "(kangxi-strokes\t . %S)%s"
355 (setq strokes value)))
356 (setq attributes (delq 'kangxi-strokes attributes))
358 (when (and (memq 'japanese-radical attributes)
359 (setq value (get-char-attribute char 'japanese-radical)))
360 (unless (eq value radical)
361 (insert (format "(japanese-radical\t . %S)\t; %c%s"
363 (aref ideographic-radicals value)
366 (setq radical value)))
367 (setq attributes (delq 'japanese-radical attributes))
369 (when (and (memq 'japanese-strokes attributes)
370 (setq value (get-char-attribute char 'japanese-strokes)))
371 (unless (eq value strokes)
372 (insert (format "(japanese-strokes\t . %S)%s"
376 (setq strokes value)))
377 (setq attributes (delq 'japanese-strokes attributes))
379 (when (and (memq 'cns-radical attributes)
380 (setq value (get-char-attribute char 'cns-radical)))
381 (insert (format "(cns-radical\t . %S)\t; %c%s"
383 (aref ideographic-radicals value)
385 (setq attributes (delq 'cns-radical attributes))
387 (when (and (memq 'cns-strokes attributes)
388 (setq value (get-char-attribute char 'cns-strokes)))
389 (unless (eq value strokes)
390 (insert (format "(cns-strokes\t . %S)%s"
394 (setq strokes value)))
395 (setq attributes (delq 'cns-strokes attributes))
397 (when (and (memq 'shinjigen-1-radical attributes)
398 (setq value (get-char-attribute char 'shinjigen-1-radical)))
399 (unless (eq value radical)
400 (insert (format "(shinjigen-1-radical . %S)\t; %c%s"
402 (aref ideographic-radicals value)
405 (setq radical value)))
406 (setq attributes (delq 'shinjigen-1-radical attributes))
408 (when (and (memq 'total-strokes attributes)
409 (setq value (get-char-attribute char 'total-strokes)))
410 (insert (format "(total-strokes . %S)%s"
413 (setq attributes (delq 'total-strokes attributes))
415 (when (and (memq '->ideograph attributes)
416 (setq value (get-char-attribute char '->ideograph)))
417 (insert (format "(->ideograph\t%s)%s"
418 (mapconcat (lambda (code)
419 (cond ((symbolp code)
422 (format "#x%04X" code))
424 (format "%s%S" line-breaking code))))
427 (setq attributes (delq '->ideograph attributes))
429 (when (and (memq '->decomposition attributes)
430 (setq value (get-char-attribute char '->decomposition)))
431 (insert (format "(->decomposition\t%s)%s"
432 (mapconcat (lambda (code)
433 (cond ((symbolp code)
442 (format "#x%04X" code))
444 (format "%s%S" line-breaking code))))
447 (setq attributes (delq '->decomposition attributes))
449 (when (and (memq '->uppercase attributes)
450 (setq value (get-char-attribute char '->uppercase)))
451 (insert (format "(->uppercase\t%s)%s"
452 (mapconcat (lambda (code)
453 (cond ((symbolp code)
456 (format "#x%04X" code))
458 (format "%s%S" line-breaking code))))
461 (setq attributes (delq '->uppercase attributes))
463 (when (and (memq '->lowercase attributes)
464 (setq value (get-char-attribute char '->lowercase)))
465 (insert (format "(->lowercase\t%s)%s"
466 (mapconcat (lambda (code)
467 (cond ((symbolp code)
470 (format "#x%04X" code))
472 (format "%s%S" line-breaking code))))
475 (setq attributes (delq '->lowercase attributes))
477 (when (and (memq '->titlecase attributes)
478 (setq value (get-char-attribute char '->titlecase)))
479 (insert (format "(->titlecase\t%s)%s"
480 (mapconcat (lambda (code)
481 (cond ((symbolp code)
484 (format "#x%04X" code))
486 (format "%s%S" line-breaking code))))
489 (setq attributes (delq '->titlecase attributes))
491 (when (and (memq '->mojikyo attributes)
492 (setq value (get-char-attribute char '->mojikyo)))
493 (insert (format "(->mojikyo\t\t. %06d)\t; %c%s"
494 value (decode-char 'mojikyo value)
496 (setq attributes (delq '->mojikyo attributes))
498 (setq rest ccs-attributes)
501 (setq value (get-char-attribute char (car rest)))
503 (if (>= (length (symbol-name (car rest))) 19)
505 (setq has-long-ccs-name t)
509 (setq rest (cdr rest)))
511 (setq name (car attributes))
512 (if (setq value (get-char-attribute char name))
513 (cond ((eq name 'jisx0208-1978/4X)
514 (insert (format "(%-18s . #x%04X)%s"
517 ((string-match "^->" (symbol-name name))
519 (format "(%-18s %s)%s"
521 (mapconcat (lambda (code)
522 (cond ((symbolp code)
525 (format "#x%04X" code))
528 line-breaking code))))
531 ((memq name '(ideograph=
532 original-ideograph-of
536 simplified-ideograph-of
538 ideographic-different-form-of))
539 (insert (format "(%-18s%s " name line-breaking))
540 (setq lbs (concat "\n" (make-string (current-column) ?\ ))
543 (setq cell (car value))
544 (if (and (consp cell)
549 (char-db-insert-alist cell readable)
550 (setq separator lbs))
553 (insert (prin1-to-string cell))
554 (setq separator " "))
555 (setq value (cdr value)))
557 (insert line-breaking))
559 (insert (format "(%-18s " name))
560 (setq lbs (concat "\n" (make-string (current-column) ?\ ))
563 (setq cell (car value))
564 (if (and (consp cell)
566 (setq ret (condition-case nil
574 (setq key (car (car rest)))
575 (if (find-charset key)
576 (setq cal (cons key cal))
577 (setq al (cons key al)))
578 (setq rest (cdr rest)))
581 (insert-char-attributes ret
584 (setq separator lbs))
587 (insert (prin1-to-string cell))
588 (setq separator " "))
589 (setq value (cdr value)))
591 (insert line-breaking))
593 (insert (format "(%-18s . %S)%s"
597 (setq attributes (cdr attributes)))
598 (while ccs-attributes
599 (setq name (car ccs-attributes))
600 (if (setq value (get-char-attribute char name))
603 (if has-long-ccs-name
604 (cond ((eq name 'ideograph-daikanwa)
605 "(%-26s . %05d)\t; %c%s"
608 "(%-26s . %06d)\t; %c%s"
611 "(%-26s . #x%X)\t; %c%s"
613 (cond ((eq name 'ideograph-daikanwa)
614 "(%-18s . %05d)\t; %c%s"
617 "(%-18s . %06d)\t; %c%s"
620 "(%-18s . #x%X)\t; %c%s"
623 (if (= (charset-iso-graphic-plane name) 1)
625 (cond ((= (charset-dimension name) 1)
627 ((= (charset-dimension name) 2)
629 ((= (charset-dimension name) 3)
633 (decode-builtin-char name value)
635 (setq ccs-attributes (cdr ccs-attributes)))
638 (defun insert-char-data (char &optional readable
639 attributes ccs-attributes)
641 (narrow-to-region (point)(point))
642 (insert "(define-char
644 (insert-char-attributes char readable
645 attributes ccs-attributes)
647 (goto-char (point-min))
648 (while (re-search-forward "[ \t]+$" nil t)
650 (goto-char (point-max))
651 (tabify (point-min)(point-max))
655 (defun char-db-update-comment ()
658 (goto-char (point-min))
659 (let (cdef table char)
660 (while (re-search-forward "^[ \t]*\\(([^.()]+)\\)" nil t)
661 (goto-char (match-beginning 1))
662 (setq cdef (read (current-buffer)))
663 (when (find-charset (car cdef))
664 (goto-char (match-end 0))
667 (not (eq (car cdef) 'ideograph-daikanwa))
668 (or (memq (car cdef) '(ascii latin-viscii-upper
672 japanese-jisx0213-2))
673 (= (char-int (charset-iso-final-char (car cdef)))
675 (apply (function make-char) cdef)
676 (if (setq table (charset-mapping-table (car cdef)))
677 (set-charset-mapping-table (car cdef) nil))
679 (apply (function make-char) cdef)
681 (set-charset-mapping-table (car cdef) table)))))
682 (when (not (or (< (char-int char) 32)
683 (and (<= 128 (char-int char))
684 (< (char-int char) 160))))
685 (delete-region (point) (point-at-eol))
686 (insert (format "\t; %c" char)))
689 (defun insert-char-data-with-variant (char &optional script printable
691 (insert-char-data char printable)
692 (let ((variants (or (char-variants char)
693 (let ((ucs (get-char-attribute char '->ucs)))
695 (delete char (char-variants (int-char ucs)))))))
698 (setq variant (car variants))
699 (if (or (null script)
700 (null (setq vs (get-char-attribute variant 'script)))
702 (or (and no-ucs-variant (get-char-attribute variant 'ucs))
703 (insert-char-data variant printable)))
704 (setq variants (cdr variants))
707 (defun insert-char-range-data (min max &optional script)
711 (setq char (decode-char 'ucs code))
712 (if (get-char-attribute char 'ucs)
713 (insert-char-data-with-variant char script nil 'no-ucs-variant))
714 (setq code (1+ code))
717 (defun write-char-range-data-to-file (min max file &optional script)
718 (let ((coding-system-for-write 'utf-8))
720 (insert-char-range-data min max script)
721 (write-region (point-min)(point-max) file))))
723 (defvar what-character-original-window-configuration)
726 (defun what-char-definition (char)
727 (interactive (list (char-after)))
728 (let ((buf (get-buffer-create "*Character Description*"))
729 (the-buf (current-buffer))
730 (win-conf (current-window-configuration)))
732 (make-local-variable 'what-character-original-window-configuration)
733 (setq what-character-original-window-configuration win-conf)
734 (setq buffer-read-only nil)
738 (insert-char-data-with-variant char nil 'printable)
739 ;; (char-db-update-comment)
740 (set-buffer-modified-p nil)
741 (view-mode the-buf (lambda (buf)
742 (set-window-configuration
743 what-character-original-window-configuration)
745 (goto-char (point-min)))
747 (set-window-configuration
748 what-character-original-window-configuration)
749 (signal (car err) (cdr err)))))))
751 (provide 'char-db-util)
753 ;;; char-db-util.el ends here