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
531 vulgar-ideograph-of))
532 (insert (format "(%-18s%s " name line-breaking))
533 (setq lbs (concat "\n" (make-string (current-column) ?\ ))
536 (setq cell (car value))
537 (if (and (consp cell)
542 (char-db-insert-alist cell readable)
543 (setq separator lbs))
546 (insert (prin1-to-string cell))
547 (setq separator " "))
548 (setq value (cdr value)))
550 (insert line-breaking))
552 (insert (format "(%-18s " name))
553 (setq lbs (concat "\n" (make-string (current-column) ?\ ))
556 (setq cell (car value))
557 (if (and (consp cell)
559 (setq ret (condition-case nil
567 (setq key (car (car rest)))
568 (if (find-charset key)
569 (setq cal (cons key cal))
570 (setq al (cons key al)))
571 (setq rest (cdr rest)))
574 (insert-char-attributes ret
577 (setq separator lbs))
580 (insert (prin1-to-string cell))
581 (setq separator " "))
582 (setq value (cdr value)))
584 (insert line-breaking))
586 (insert (format "(%-18s . %S)%s"
590 (setq attributes (cdr attributes)))
591 (while ccs-attributes
592 (setq name (car ccs-attributes))
593 (if (setq value (get-char-attribute char name))
596 (if has-long-ccs-name
597 (cond ((eq name 'ideograph-daikanwa)
598 "(%-26s . %05d)\t; %c%s"
601 "(%-26s . %06d)\t; %c%s"
604 "(%-26s . #x%X)\t; %c%s"
606 (cond ((eq name 'ideograph-daikanwa)
607 "(%-18s . %05d)\t; %c%s"
610 "(%-18s . %06d)\t; %c%s"
613 "(%-18s . #x%X)\t; %c%s"
616 (if (= (charset-iso-graphic-plane name) 1)
618 (cond ((= (charset-dimension name) 1)
620 ((= (charset-dimension name) 2)
622 ((= (charset-dimension name) 3)
626 (decode-builtin-char name value)
628 (setq ccs-attributes (cdr ccs-attributes)))
631 (defun insert-char-data (char &optional readable
632 attributes ccs-attributes)
634 (narrow-to-region (point)(point))
635 (insert "(define-char
637 (insert-char-attributes char readable
638 attributes ccs-attributes)
640 (goto-char (point-min))
641 (while (re-search-forward "[ \t]+$" nil t)
643 (goto-char (point-max))
644 (tabify (point-min)(point-max))
648 (defun char-db-update-comment ()
651 (goto-char (point-min))
652 (let (cdef table char)
653 (while (re-search-forward "^[ \t]*\\(([^.()]+)\\)" nil t)
654 (goto-char (match-beginning 1))
655 (setq cdef (read (current-buffer)))
656 (when (find-charset (car cdef))
657 (goto-char (match-end 0))
660 (not (eq (car cdef) 'ideograph-daikanwa))
661 (or (memq (car cdef) '(ascii latin-viscii-upper
665 japanese-jisx0213-2))
666 (= (char-int (charset-iso-final-char (car cdef)))
668 (apply (function make-char) cdef)
669 (if (setq table (charset-mapping-table (car cdef)))
670 (set-charset-mapping-table (car cdef) nil))
672 (apply (function make-char) cdef)
674 (set-charset-mapping-table (car cdef) table)))))
675 (when (not (or (< (char-int char) 32)
676 (and (<= 128 (char-int char))
677 (< (char-int char) 160))))
678 (delete-region (point) (point-at-eol))
679 (insert (format "\t; %c" char)))
682 (defun insert-char-data-with-variant (char &optional script printable
684 (insert-char-data char printable)
685 (let ((variants (or (char-variants char)
686 (let ((ucs (get-char-attribute char '->ucs)))
688 (delete char (char-variants (int-char ucs)))))))
691 (setq variant (car variants))
692 (if (or (null script)
693 (null (setq vs (get-char-attribute variant 'script)))
695 (or (and no-ucs-variant (get-char-attribute variant 'ucs))
696 (insert-char-data variant printable)))
697 (setq variants (cdr variants))
700 (defun insert-char-range-data (min max &optional script)
704 (setq char (decode-char 'ucs code))
705 (if (get-char-attribute char 'ucs)
706 (insert-char-data-with-variant char script nil 'no-ucs-variant))
707 (setq code (1+ code))
710 (defun write-char-range-data-to-file (min max file &optional script)
711 (let ((coding-system-for-write 'utf-8))
713 (insert-char-range-data min max script)
714 (write-region (point-min)(point-max) file))))
716 (defvar what-character-original-window-configuration)
719 (defun what-char-definition (char)
720 (interactive (list (char-after)))
721 (let ((buf (get-buffer-create "*Character Description*"))
722 (the-buf (current-buffer))
723 (win-conf (current-window-configuration)))
725 (make-local-variable 'what-character-original-window-configuration)
726 (setq what-character-original-window-configuration win-conf)
727 (setq buffer-read-only nil)
731 (insert-char-data-with-variant char nil 'printable)
732 ;; (char-db-update-comment)
733 (set-buffer-modified-p nil)
734 (view-mode the-buf (lambda (buf)
735 (set-window-configuration
736 what-character-original-window-configuration)
738 (goto-char (point-min)))
740 (set-window-configuration
741 what-character-original-window-configuration)
742 (signal (car err) (cdr err)))))))
744 (provide 'char-db-util)
746 ;;; char-db-util.el ends here