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) ?\ )))
206 lbs cell separator ret
209 (when (and (memq 'name attributes)
210 (setq value (get-char-attribute char 'name)))
212 (if (> (length value) 47)
215 value line-breaking))
216 (setq attributes (delq 'name attributes))
218 (when (and (memq 'script attributes)
219 (setq value (get-char-attribute char 'script)))
220 (insert (format "(script\t\t%s)%s"
221 (mapconcat (function prin1-to-string)
224 (setq attributes (delq 'script attributes))
226 (when (and (memq '->ucs attributes)
227 (setq value (get-char-attribute char '->ucs)))
228 (insert (format "(->ucs\t\t. #x%04X)\t; %c%s"
229 value (decode-char 'ucs value)
231 (setq attributes (delq '->ucs attributes))
233 (when (and (memq 'general-category attributes)
234 (setq value (get-char-attribute char 'general-category)))
236 "(general-category\t%s) ; %s%s"
237 (mapconcat (lambda (cell)
240 (cond ((rassoc value unidata-normative-category-alist)
241 "Normative Category")
242 ((rassoc value unidata-informative-category-alist)
243 "Informative Category")
247 (setq attributes (delq 'general-category attributes))
249 (when (and (memq 'bidi-category attributes)
250 (setq value (get-char-attribute char 'bidi-category)))
251 (insert (format "(bidi-category\t. %S)%s"
254 (setq attributes (delq 'bidi-category attributes))
256 (unless (or (not (memq 'mirrored attributes))
257 (eq (setq value (get-char-attribute char 'mirrored 'empty))
259 (insert (format "(mirrored\t\t. %S)%s"
262 (setq attributes (delq 'mirrored attributes))
265 ((and (memq 'decimal-digit-value attributes)
266 (setq value (get-char-attribute char 'decimal-digit-value)))
267 (insert (format "(decimal-digit-value . %S)%s"
270 (setq attributes (delq 'decimal-digit-value attributes))
271 (when (and (memq 'digit-value attributes)
272 (setq value (get-char-attribute char 'digit-value)))
273 (insert (format "(digit-value\t . %S)%s"
276 (setq attributes (delq 'digit-value attributes))
278 (when (and (memq 'numeric-value attributes)
279 (setq value (get-char-attribute char 'numeric-value)))
280 (insert (format "(numeric-value\t . %S)%s"
283 (setq attributes (delq 'numeric-value attributes))
287 (when (and (memq 'digit-value attributes)
288 (setq value (get-char-attribute char 'digit-value)))
289 (insert (format "(digit-value\t. %S)%s"
292 (setq attributes (delq 'digit-value attributes))
294 (when (and (memq 'numeric-value attributes)
295 (setq value (get-char-attribute char 'numeric-value)))
296 (insert (format "(numeric-value\t. %S)%s"
299 (setq attributes (delq 'numeric-value attributes))
301 (when (and (memq 'iso-10646-comment attributes)
302 (setq value (get-char-attribute char 'iso-10646-comment)))
303 (insert (format "(iso-10646-comment\t. %S)%s"
306 (setq attributes (delq 'iso-10646-comment attributes))
308 (when (and (memq 'morohashi-daikanwa attributes)
309 (setq value (get-char-attribute char 'morohashi-daikanwa)))
310 (insert (format "(morohashi-daikanwa\t%s)%s"
311 (mapconcat (function prin1-to-string) value " ")
313 (setq attributes (delq 'morohashi-daikanwa attributes))
317 (when (and (memq 'ideographic-radical attributes)
318 (setq value (get-char-attribute char 'ideographic-radical)))
320 (insert (format "(ideographic-radical . %S)\t; %c%s"
322 (aref ideographic-radicals radical)
324 (setq attributes (delq 'ideographic-radical attributes))
326 (when (and (memq 'ideographic-strokes attributes)
327 (setq value (get-char-attribute char 'ideographic-strokes)))
329 (insert (format "(ideographic-strokes . %S)%s"
332 (setq attributes (delq 'ideographic-strokes attributes))
334 (when (and (memq 'kangxi-radical attributes)
335 (setq value (get-char-attribute char 'kangxi-radical)))
336 (unless (eq value radical)
337 (insert (format "(kangxi-radical\t . %S)\t; %c%s"
339 (aref ideographic-radicals value)
342 (setq radical value)))
343 (setq attributes (delq 'kangxi-radical attributes))
345 (when (and (memq 'kangxi-strokes attributes)
346 (setq value (get-char-attribute char 'kangxi-strokes)))
347 (unless (eq value strokes)
348 (insert (format "(kangxi-strokes\t . %S)%s"
352 (setq strokes value)))
353 (setq attributes (delq 'kangxi-strokes attributes))
355 (when (and (memq 'japanese-radical attributes)
356 (setq value (get-char-attribute char 'japanese-radical)))
357 (unless (eq value radical)
358 (insert (format "(japanese-radical\t . %S)\t; %c%s"
360 (aref ideographic-radicals value)
363 (setq radical value)))
364 (setq attributes (delq 'japanese-radical attributes))
366 (when (and (memq 'japanese-strokes attributes)
367 (setq value (get-char-attribute char 'japanese-strokes)))
368 (unless (eq value strokes)
369 (insert (format "(japanese-strokes\t . %S)%s"
373 (setq strokes value)))
374 (setq attributes (delq 'japanese-strokes attributes))
376 (when (and (memq 'cns-radical attributes)
377 (setq value (get-char-attribute char 'cns-radical)))
378 (insert (format "(cns-radical\t . %S)\t; %c%s"
380 (aref ideographic-radicals value)
382 (setq attributes (delq 'cns-radical attributes))
384 (when (and (memq 'cns-strokes attributes)
385 (setq value (get-char-attribute char 'cns-strokes)))
386 (unless (eq value strokes)
387 (insert (format "(cns-strokes\t . %S)%s"
391 (setq strokes value)))
392 (setq attributes (delq 'cns-strokes attributes))
394 (when (and (memq 'shinjigen-1-radical attributes)
395 (setq value (get-char-attribute char 'shinjigen-1-radical)))
396 (unless (eq value radical)
397 (insert (format "(shinjigen-1-radical . %S)\t; %c%s"
399 (aref ideographic-radicals value)
402 (setq radical value)))
403 (setq attributes (delq 'shinjigen-1-radical attributes))
405 (when (and (memq 'total-strokes attributes)
406 (setq value (get-char-attribute char 'total-strokes)))
407 (insert (format "(total-strokes . %S)%s"
410 (setq attributes (delq 'total-strokes attributes))
412 (when (and (memq '->ideograph attributes)
413 (setq value (get-char-attribute char '->ideograph)))
414 (insert (format "(->ideograph\t%s)%s"
415 (mapconcat (lambda (code)
416 (cond ((symbolp code)
419 (format "#x%04X" code))
421 (format "%s%S" line-breaking code))))
424 (setq attributes (delq '->ideograph attributes))
426 (when (and (memq '->decomposition attributes)
427 (setq value (get-char-attribute char '->decomposition)))
428 (insert (format "(->decomposition\t%s)%s"
429 (mapconcat (lambda (code)
430 (cond ((symbolp code)
439 (format "#x%04X" code))
441 (format "%s%S" line-breaking code))))
444 (setq attributes (delq '->decomposition attributes))
446 (when (and (memq '->uppercase attributes)
447 (setq value (get-char-attribute char '->uppercase)))
448 (insert (format "(->uppercase\t%s)%s"
449 (mapconcat (lambda (code)
450 (cond ((symbolp code)
453 (format "#x%04X" code))
455 (format "%s%S" line-breaking code))))
458 (setq attributes (delq '->uppercase attributes))
460 (when (and (memq '->lowercase attributes)
461 (setq value (get-char-attribute char '->lowercase)))
462 (insert (format "(->lowercase\t%s)%s"
463 (mapconcat (lambda (code)
464 (cond ((symbolp code)
467 (format "#x%04X" code))
469 (format "%s%S" line-breaking code))))
472 (setq attributes (delq '->lowercase attributes))
474 (when (and (memq '->titlecase attributes)
475 (setq value (get-char-attribute char '->titlecase)))
476 (insert (format "(->titlecase\t%s)%s"
477 (mapconcat (lambda (code)
478 (cond ((symbolp code)
481 (format "#x%04X" code))
483 (format "%s%S" line-breaking code))))
486 (setq attributes (delq '->titlecase attributes))
488 (when (and (memq '->mojikyo attributes)
489 (setq value (get-char-attribute char '->mojikyo)))
490 (insert (format "(->mojikyo\t\t. %06d)\t; %c%s"
491 value (decode-char 'mojikyo value)
493 (setq attributes (delq '->mojikyo attributes))
495 (setq rest ccs-attributes)
498 (setq value (get-char-attribute char (car rest)))
500 (if (>= (length (symbol-name (car rest))) 19)
502 (setq has-long-ccs-name t)
506 (setq rest (cdr rest)))
508 (setq name (car attributes))
509 (if (setq value (get-char-attribute char name))
510 (cond ((eq name 'jisx0208-1978/4X)
511 (insert (format "(%-18s . #x%04X)%s"
514 ((string-match "^->" (symbol-name name))
516 (format "(%-18s %s)%s"
518 (mapconcat (lambda (code)
519 (cond ((symbolp code)
522 (format "#x%04X" code))
525 line-breaking code))))
528 ((memq name '(ideograph=
529 original-ideograph-of
530 vulgar-ideograph-of))
531 (insert (format "(%-18s%s " name line-breaking))
532 (setq lbs (concat "\n" (make-string (current-column) ?\ ))
535 (setq cell (car value))
536 (if (and (consp cell)
541 (char-db-insert-alist cell readable)
542 (setq separator lbs))
545 (insert (prin1-to-string cell))
546 (setq separator " "))
547 (setq value (cdr value)))
549 (insert line-breaking))
551 (insert (format "(%-18s " name))
552 (setq lbs (concat "\n" (make-string (current-column) ?\ ))
555 (setq cell (car value))
556 (if (and (consp cell)
558 (setq ret (condition-case nil
566 (setq key (car (car rest)))
567 (if (find-charset key)
568 (setq cal (cons key cal))
569 (setq al (cons key al)))
570 (setq rest (cdr rest)))
573 (insert-char-attributes ret
576 (setq separator lbs))
579 (insert (prin1-to-string cell))
580 (setq separator " "))
581 (setq value (cdr value)))
583 (insert line-breaking))
585 (insert (format "(%-18s . %S)%s"
589 (setq attributes (cdr attributes)))
590 (while ccs-attributes
591 (setq name (car ccs-attributes))
592 (if (setq value (get-char-attribute char name))
595 (if has-long-ccs-name
596 (cond ((eq name 'ideograph-daikanwa)
597 "(%-26s . %05d)\t; %c%s"
600 "(%-26s . %06d)\t; %c%s"
603 "(%-26s . #x%X)\t; %c%s"
605 (cond ((eq name 'ideograph-daikanwa)
606 "(%-18s . %05d)\t; %c%s"
609 "(%-18s . %06d)\t; %c%s"
612 "(%-18s . #x%X)\t; %c%s"
615 (if (= (charset-iso-graphic-plane name) 1)
617 (cond ((= (charset-dimension name) 1)
619 ((= (charset-dimension name) 2)
621 ((= (charset-dimension name) 3)
625 (decode-builtin-char name value)
627 (setq ccs-attributes (cdr ccs-attributes)))
630 (defun insert-char-data (char &optional readable
631 attributes ccs-attributes)
633 (narrow-to-region (point)(point))
634 (insert "(define-char
636 (insert-char-attributes char readable
637 attributes ccs-attributes)
639 (goto-char (point-min))
640 (while (re-search-forward "[ \t]+$" nil t)
642 (goto-char (point-max))
643 (tabify (point-min)(point-max))
647 (defun char-db-update-comment ()
650 (goto-char (point-min))
651 (let (cdef table char)
652 (while (re-search-forward "^[ \t]*\\(([^.()]+)\\)" nil t)
653 (goto-char (match-beginning 1))
654 (setq cdef (read (current-buffer)))
655 (when (find-charset (car cdef))
656 (goto-char (match-end 0))
659 (not (eq (car cdef) 'ideograph-daikanwa))
660 (or (memq (car cdef) '(ascii latin-viscii-upper
664 japanese-jisx0213-2))
665 (= (char-int (charset-iso-final-char (car cdef)))
667 (apply (function make-char) cdef)
668 (if (setq table (charset-mapping-table (car cdef)))
669 (set-charset-mapping-table (car cdef) nil))
671 (apply (function make-char) cdef)
673 (set-charset-mapping-table (car cdef) table)))))
674 (when (not (or (< (char-int char) 32)
675 (and (<= 128 (char-int char))
676 (< (char-int char) 160))))
677 (delete-region (point) (point-at-eol))
678 (insert (format "\t; %c" char)))
681 (defun insert-char-data-with-variant (char &optional script printable
683 (insert-char-data char printable)
684 (let ((variants (or (char-variants char)
685 (let ((ucs (get-char-attribute char '->ucs)))
687 (delete char (char-variants (int-char ucs)))))))
690 (setq variant (car variants))
691 (if (or (null script)
692 (null (setq vs (get-char-attribute variant 'script)))
694 (or (and no-ucs-variant (get-char-attribute variant 'ucs))
695 (insert-char-data variant printable)))
696 (setq variants (cdr variants))
699 (defun insert-char-range-data (min max &optional script)
703 (setq char (decode-char 'ucs code))
704 (if (get-char-attribute char 'ucs)
705 (insert-char-data-with-variant char script nil 'no-ucs-variant))
706 (setq code (1+ code))
709 (defun write-char-range-data-to-file (min max file &optional script)
710 (let ((coding-system-for-write 'utf-8))
712 (insert-char-range-data min max script)
713 (write-region (point-min)(point-max) file))))
715 (defvar what-character-original-window-configuration)
718 (defun what-char-definition (char)
719 (interactive (list (char-after)))
720 (let ((buf (get-buffer-create "*Character Description*"))
721 (the-buf (current-buffer))
722 (win-conf (current-window-configuration)))
724 (make-local-variable 'what-character-original-window-configuration)
725 (setq what-character-original-window-configuration win-conf)
726 (setq buffer-read-only nil)
730 (insert-char-data-with-variant char nil 'printable)
731 ;; (char-db-update-comment)
732 (set-buffer-modified-p nil)
733 (view-mode the-buf (lambda (buf)
734 (set-window-configuration
735 what-character-original-window-configuration)
737 (goto-char (point-min)))
739 (set-window-configuration
740 what-character-original-window-configuration)
741 (signal (car err) (cdr err)))))))
743 (provide 'char-db-util)
745 ;;; char-db-util.el ends here