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 (copy-sequence attributes)
195 (sort (char-attribute-list) #'char-attribute-name<)))
198 (copy-sequence ccs-attributes)
199 (sort (charset-list) #'char-attribute-name<)))
201 (setq column (current-column)))
202 (let (name value has-long-ccs-name rest
205 (concat "\n" (make-string (1+ column) ?\ ))))
207 (when (setq value (get-char-attribute char 'name))
209 (if (> (length value) 47)
212 value line-breaking))
213 (setq attributes (delq 'name attributes))
215 (when (setq value (get-char-attribute char 'script))
216 (insert (format "(script\t\t%s)%s"
217 (mapconcat (function prin1-to-string)
220 (setq attributes (delq 'script attributes))
222 (when (setq value (get-char-attribute char '->ucs))
223 (insert (format "(->ucs\t\t. #x%04X)\t; %c%s"
224 value (decode-char 'ucs value)
226 (setq attributes (delq '->ucs attributes))
228 (when (setq value (get-char-attribute char 'general-category))
230 "(general-category\t%s) ; %s%s"
231 (mapconcat (lambda (cell)
234 (cond ((rassoc value unidata-normative-category-alist)
235 "Normative Category")
236 ((rassoc value unidata-informative-category-alist)
237 "Informative Category")
241 (setq attributes (delq 'general-category attributes))
243 (when (setq value (get-char-attribute char 'bidi-category))
244 (insert (format "(bidi-category\t. %S)%s"
247 (setq attributes (delq 'bidi-category attributes))
249 (unless (eq (setq value (get-char-attribute char 'mirrored 'empty))
251 (insert (format "(mirrored\t\t. %S)%s"
254 (setq attributes (delq 'mirrored attributes))
257 ((setq value (get-char-attribute char 'decimal-digit-value))
258 (insert (format "(decimal-digit-value . %S)%s"
261 (setq attributes (delq 'decimal-digit-value attributes))
262 (when (setq value (get-char-attribute char 'digit-value))
263 (insert (format "(digit-value\t . %S)%s"
266 (setq attributes (delq 'digit-value attributes))
268 (when (setq value (get-char-attribute char 'numeric-value))
269 (insert (format "(numeric-value\t . %S)%s"
272 (setq attributes (delq 'numeric-value attributes))
276 (when (setq value (get-char-attribute char 'digit-value))
277 (insert (format "(digit-value\t. %S)%s"
280 (setq attributes (delq 'digit-value attributes))
282 (when (setq value (get-char-attribute char 'numeric-value))
283 (insert (format "(numeric-value\t. %S)%s"
286 (setq attributes (delq 'numeric-value attributes))
288 (when (setq value (get-char-attribute char 'iso-10646-comment))
289 (insert (format "(iso-10646-comment\t. %S)%s"
292 (setq attributes (delq 'iso-10646-comment attributes))
294 (when (setq value (get-char-attribute char 'morohashi-daikanwa))
295 (insert (format "(morohashi-daikanwa\t%s)%s"
296 (mapconcat (function prin1-to-string) value " ")
298 (setq attributes (delq 'morohashi-daikanwa attributes))
302 (when (setq value (get-char-attribute char 'ideographic-radical))
304 (insert (format "(ideographic-radical . %S)\t; %c%s"
306 (aref ideographic-radicals radical)
308 (setq attributes (delq 'ideographic-radical attributes))
310 (when (setq value (get-char-attribute char 'ideographic-strokes))
312 (insert (format "(ideographic-strokes . %S)%s"
315 (setq attributes (delq 'ideographic-strokes attributes))
317 (when (setq value (get-char-attribute char 'kangxi-radical))
318 (unless (eq value radical)
319 (insert (format "(kangxi-radical\t . %S)\t; %c%s"
321 (aref ideographic-radicals value)
324 (setq radical value)))
325 (setq attributes (delq 'kangxi-radical attributes))
327 (when (setq value (get-char-attribute char 'kangxi-strokes))
328 (unless (eq value strokes)
329 (insert (format "(kangxi-strokes\t . %S)%s"
333 (setq strokes value)))
334 (setq attributes (delq 'kangxi-strokes attributes))
336 (when (setq value (get-char-attribute char 'japanese-radical))
337 (unless (eq value radical)
338 (insert (format "(japanese-radical\t . %S)\t; %c%s"
340 (aref ideographic-radicals value)
343 (setq radical value)))
344 (setq attributes (delq 'japanese-radical attributes))
346 (when (setq value (get-char-attribute char 'japanese-strokes))
347 (unless (eq value strokes)
348 (insert (format "(japanese-strokes\t . %S)%s"
352 (setq strokes value)))
353 (setq attributes (delq 'japanese-strokes attributes))
355 (when (setq value (get-char-attribute char 'cns-radical))
356 (insert (format "(cns-radical\t . %S)\t; %c%s"
358 (aref ideographic-radicals value)
360 (setq attributes (delq 'cns-radical attributes))
362 (when (setq value (get-char-attribute char 'cns-strokes))
363 (unless (eq value strokes)
364 (insert (format "(cns-strokes\t . %S)%s"
368 (setq strokes value)))
369 (setq attributes (delq 'cns-strokes attributes))
371 (when (setq value (get-char-attribute char 'shinjigen-1-radical))
372 (unless (eq value radical)
373 (insert (format "(shinjigen-1-radical . %S)\t; %c%s"
375 (aref ideographic-radicals value)
378 (setq radical value)))
379 (setq attributes (delq 'shinjigen-1-radical attributes))
381 (when (setq value (get-char-attribute char 'total-strokes))
382 (insert (format "(total-strokes . %S)%s"
385 (setq attributes (delq 'total-strokes attributes))
387 (when (setq value (get-char-attribute char '->ideograph))
388 (insert (format "(->ideograph\t%s)%s"
389 (mapconcat (lambda (code)
390 (cond ((symbolp code)
393 (format "#x%04X" code))
395 (format "%s%S" line-breaking code))))
398 (setq attributes (delq '->ideograph attributes))
400 (when (setq value (get-char-attribute char '->decomposition))
401 (insert (format "(->decomposition\t%s)%s"
402 (mapconcat (lambda (code)
403 (cond ((symbolp code)
412 (format "#x%04X" code))
414 (format "%s%S" line-breaking code))))
417 (setq attributes (delq '->decomposition attributes))
419 (when (setq value (get-char-attribute char '->uppercase))
420 (insert (format "(->uppercase\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 '->uppercase attributes))
432 (when (setq value (get-char-attribute char '->lowercase))
433 (insert (format "(->lowercase\t%s)%s"
434 (mapconcat (lambda (code)
435 (cond ((symbolp code)
438 (format "#x%04X" code))
440 (format "%s%S" line-breaking code))))
443 (setq attributes (delq '->lowercase attributes))
445 (when (setq value (get-char-attribute char '->titlecase))
446 (insert (format "(->titlecase\t%s)%s"
447 (mapconcat (lambda (code)
448 (cond ((symbolp code)
451 (format "#x%04X" code))
453 (format "%s%S" line-breaking code))))
456 (setq attributes (delq '->titlecase attributes))
458 (when (setq value (get-char-attribute char '->mojikyo))
459 (insert (format "(->mojikyo\t\t. %06d)\t; %c%s"
460 value (decode-char 'mojikyo value)
462 (setq attributes (delq '->mojikyo attributes))
464 (setq rest ccs-attributes)
467 (setq value (get-char-attribute char (car rest)))
469 (if (>= (length (symbol-name (car rest))) 19)
471 (setq has-long-ccs-name t)
475 (setq rest (cdr rest)))
477 (setq name (car attributes))
478 (if (setq value (get-char-attribute char name))
479 (cond ((eq name 'jisx0208-1978/4X)
480 (insert (format "(%-18s . #x%04X)%s"
483 ((string-match "^->" (symbol-name name))
485 (format "(%-18s %s)%s"
487 (mapconcat (lambda (code)
488 (cond ((symbolp code)
491 (format "#x%04X" code))
494 line-breaking code))))
497 ((memq name '(ideograph=
498 original-ideograph-of
499 vulgar-ideograph-of))
500 (insert (format "(%-18s%s " name line-breaking))
501 (let ((lbs (concat "\n" (make-string (current-column) ?\ )))
506 (setq cell (car value))
507 (if (and (consp cell)
512 (char-db-insert-alist cell readable)
513 (setq separator lbs))
516 (insert (prin1-to-string cell))
517 (setq separator " "))
518 (setq value (cdr value))))
520 (insert line-breaking))
522 (insert (format "(%-18s " name))
523 (let ((lbs (concat "\n" (make-string (current-column) ?\ )))
528 (setq cell (car value))
529 (if (and (consp cell)
531 (setq ret (condition-case nil
539 (setq key (car (car rest)))
540 (if (find-charset key)
541 (setq cal (cons key cal))
542 (setq al (cons key al)))
543 (setq rest (cdr rest)))
546 (insert-char-attributes ret
549 (setq separator lbs))
552 (insert (prin1-to-string cell))
553 (setq separator " "))
554 (setq value (cdr value))))
556 (insert line-breaking))
558 (insert (format "(%-18s . %S)%s"
562 (setq attributes (cdr attributes)))
563 (while ccs-attributes
564 (setq name (car ccs-attributes))
565 (if (setq value (get-char-attribute char name))
568 (if has-long-ccs-name
569 (cond ((eq name 'ideograph-daikanwa)
570 "(%-26s . %05d)\t; %c%s"
573 "(%-26s . %06d)\t; %c%s"
576 "(%-26s . #x%X)\t; %c%s"
578 (cond ((eq name 'ideograph-daikanwa)
579 "(%-18s . %05d)\t; %c%s"
582 "(%-18s . %06d)\t; %c%s"
585 "(%-18s . #x%X)\t; %c%s"
588 (if (= (charset-iso-graphic-plane name) 1)
590 (cond ((= (charset-dimension name) 1)
592 ((= (charset-dimension name) 2)
594 ((= (charset-dimension name) 3)
598 (decode-builtin-char name value)
600 (setq ccs-attributes (cdr ccs-attributes)))
603 (defun insert-char-data (char &optional readable
604 attributes ccs-attributes)
606 (narrow-to-region (point)(point))
607 (insert "(define-char
609 (insert-char-attributes char readable
610 attributes ccs-attributes)
612 (goto-char (point-min))
613 (while (re-search-forward "[ \t]+$" nil t)
615 (goto-char (point-max))
616 (tabify (point-min)(point-max))
620 (defun char-db-update-comment ()
623 (goto-char (point-min))
624 (let (cdef table char)
625 (while (re-search-forward "^[ \t]*\\(([^.()]+)\\)" nil t)
626 (goto-char (match-beginning 1))
627 (setq cdef (read (current-buffer)))
628 (when (find-charset (car cdef))
629 (goto-char (match-end 0))
632 (not (eq (car cdef) 'ideograph-daikanwa))
633 (or (memq (car cdef) '(ascii latin-viscii-upper
637 japanese-jisx0213-2))
638 (= (char-int (charset-iso-final-char (car cdef)))
640 (apply (function make-char) cdef)
641 (if (setq table (charset-mapping-table (car cdef)))
642 (set-charset-mapping-table (car cdef) nil))
644 (apply (function make-char) cdef)
646 (set-charset-mapping-table (car cdef) table)))))
647 (when (not (or (< (char-int char) 32)
648 (and (<= 128 (char-int char))
649 (< (char-int char) 160))))
650 (delete-region (point) (point-at-eol))
651 (insert (format "\t; %c" char)))
654 (defun insert-char-data-with-variant (char &optional script printable
656 (insert-char-data char printable)
657 (let ((variants (or (char-variants char)
658 (let ((ucs (get-char-attribute char '->ucs)))
660 (delete char (char-variants (int-char ucs)))))))
663 (setq variant (car variants))
664 (if (or (null script)
665 (null (setq vs (get-char-attribute variant 'script)))
667 (or (and no-ucs-variant (get-char-attribute variant 'ucs))
668 (insert-char-data variant printable)))
669 (setq variants (cdr variants))
672 (defun insert-char-range-data (min max &optional script)
676 (setq char (decode-char 'ucs code))
677 (if (get-char-attribute char 'ucs)
678 (insert-char-data-with-variant char script nil 'no-ucs-variant))
679 (setq code (1+ code))
682 (defun write-char-range-data-to-file (min max file &optional script)
683 (let ((coding-system-for-write 'utf-8))
685 (insert-char-range-data min max script)
686 (write-region (point-min)(point-max) file))))
688 (defvar what-character-original-window-configuration)
691 (defun what-char-definition (char)
692 (interactive (list (char-after)))
693 (let ((buf (get-buffer-create "*Character Description*"))
694 (the-buf (current-buffer))
695 (win-conf (current-window-configuration)))
697 (make-local-variable 'what-character-original-window-configuration)
698 (setq what-character-original-window-configuration win-conf)
699 (setq buffer-read-only nil)
703 (insert-char-data-with-variant char nil 'printable)
704 ;; (char-db-update-comment)
705 (set-buffer-modified-p nil)
706 (view-mode the-buf (lambda (buf)
707 (set-window-configuration
708 what-character-original-window-configuration)
710 (goto-char (point-min)))
712 (set-window-configuration
713 what-character-original-window-configuration)
714 (signal (car err) (cdr err)))))))
716 (provide 'char-db-util)
718 ;;; char-db-util.el ends here