1 ;;; char-db-util.el --- Character Database utility
3 ;; Copyright (C) 1998,1999,2000 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 insert-char-data (char &optional readable)
117 (let ((data (char-attribute-alist char))
118 cell ret has-long-ccs-name rest
122 (narrow-to-region (point)(point))
123 (insert "(define-char
125 (when (setq cell (assq 'name data))
126 (setq cell (cdr cell))
128 (if (> (length cell) 47)
134 (setq data (del-alist 'name data))
136 (when (setq cell (assq 'script data))
137 (insert (format "(script\t\t%s)
139 (mapconcat (function prin1-to-string)
141 (setq data (del-alist 'script data))
143 (when (setq cell (assq 'ucs data))
144 (setq cell (cdr cell))
145 (insert (format "(ucs\t\t. #x%04X)
148 (setq data (del-alist 'ucs data))
150 (when (setq cell (assq '->ucs data))
151 (setq cell (cdr cell))
152 (insert (format "(->ucs\t\t. #x%04X)\t; %c
154 cell (decode-char 'ucs cell)))
155 (setq data (del-alist '->ucs data))
157 (when (setq cell (assq 'general-category data))
158 (setq ret (cdr cell))
160 "(general-category\t%s) ; %s
162 (mapconcat (lambda (cell)
165 (cond ((rassoc (cdr cell)
166 unidata-normative-category-alist)
167 "Normative Category")
169 unidata-informative-category-alist)
170 "Informative Category")
172 "Unknown Category"))))
173 (setq data (del-alist 'general-category data))
175 (when (setq cell (assq 'bidi-category data))
176 (setq cell (cdr cell))
177 (insert (format "(bidi-category\t. %S)
180 (setq data (del-alist 'bidi-category data))
182 (when (setq cell (assq 'mirrored data))
183 (setq cell (cdr cell))
184 (insert (format "(mirrored\t\t. %S)
187 (setq data (del-alist 'mirrored data))
190 ((setq cell (assq 'decimal-digit-value data))
191 (setq cell (cdr cell))
192 (insert (format "(decimal-digit-value . %S)
195 (setq data (del-alist 'decimal-digit-value data))
196 (when (setq cell (assq 'digit-value data))
197 (setq cell (cdr cell))
198 (insert (format "(digit-value\t . %S)
201 (setq data (del-alist 'digit-value data))
203 (when (setq cell (assq 'numeric-value data))
204 (setq cell (cdr cell))
205 (insert (format "(numeric-value\t . %S)
208 (setq data (del-alist 'numeric-value data))
212 (when (setq cell (assq 'digit-value data))
213 (setq cell (cdr cell))
214 (insert (format "(digit-value\t. %S)
217 (setq data (del-alist 'digit-value data))
219 (when (setq cell (assq 'numeric-value data))
220 (setq cell (cdr cell))
221 (insert (format "(numeric-value\t. %S)
224 (setq data (del-alist 'numeric-value data))
226 (when (setq cell (assq 'iso-10646-comment data))
227 (setq cell (cdr cell))
228 (insert (format "(iso-10646-comment\t. %S)
231 (setq data (del-alist 'iso-10646-comment data))
233 (when (setq cell (assq 'morohashi-daikanwa data))
234 (setq cell (cdr cell))
235 (insert (format "(morohashi-daikanwa\t%s)
237 (mapconcat (function prin1-to-string) cell " ")))
238 (setq data (del-alist 'morohashi-daikanwa data))
242 (when (setq cell (assq 'ideographic-radical data))
243 (setq radical (cdr cell))
244 (insert (format "(ideographic-radical . %S)\t; %c
247 (aref ideographic-radicals radical)))
248 (setq data (del-alist 'ideographic-radical data))
250 (when (setq cell (assq 'ideographic-strokes data))
251 (setq strokes (cdr cell))
252 (insert (format "(ideographic-strokes . %S)
255 (setq data (del-alist 'ideographic-strokes data))
257 (when (setq cell (assq 'kangxi-radical data))
258 (setq cell (cdr cell))
259 (unless (eq cell radical)
260 (insert (format "(kangxi-radical\t . %S)\t; %c
263 (aref ideographic-radicals cell)))
265 (setq data (del-alist 'kangxi-radical data))
267 (when (setq cell (assq 'kangxi-strokes data))
268 (setq cell (cdr cell))
269 (unless (eq cell strokes)
270 (insert (format "(kangxi-strokes\t . %S)
274 (setq data (del-alist 'kangxi-strokes data))
276 (when (setq cell (assq 'japanese-radical data))
277 (setq cell (cdr cell))
278 (unless (eq cell radical)
279 (insert (format "(japanese-radical\t . %S)\t; %c
282 (aref ideographic-radicals cell)))
284 (setq data (del-alist 'japanese-radical data))
286 (when (setq cell (assq 'japanese-strokes data))
287 (setq cell (cdr cell))
288 (unless (eq cell strokes)
289 (insert (format "(japanese-strokes\t . %S)
293 (setq data (del-alist 'japanese-strokes data))
295 (when (setq cell (assq 'cns-radical data))
296 (setq cell (cdr cell))
297 (insert (format "(cns-radical\t . %S)\t; %c
300 (aref ideographic-radicals cell)))
301 (setq data (del-alist 'cns-radical data))
303 (when (setq cell (assq 'cns-strokes data))
304 (setq cell (cdr cell))
305 (unless (eq cell strokes)
306 (insert (format "(cns-strokes\t . %S)
310 (setq data (del-alist 'cns-strokes data))
312 (when (setq cell (assq 'total-strokes data))
313 (setq cell (cdr cell))
314 (insert (format "(total-strokes\t . %S)
317 (setq data (del-alist 'total-strokes data))
319 (when (setq cell (assq '->ideograph data))
320 (setq cell (cdr cell))
321 (insert (format "(->ideograph\t%s)
323 (mapconcat (lambda (code)
324 (cond ((symbolp code)
327 (format "#x%04X" code))
329 (format "\n %S" code))))
331 (setq data (del-alist '->ideograph data))
333 (when (setq cell (assq '->decomposition data))
334 (setq cell (cdr cell))
335 (insert (format "(->decomposition\t%s)
337 (mapconcat (lambda (code)
338 (cond ((symbolp code)
347 (format "#x%04X" code))
349 (format "\n %S" code))))
351 (setq data (del-alist '->decomposition data))
353 (when (setq cell (assq '->uppercase data))
354 (setq cell (cdr cell))
355 (insert (format "(->uppercase\t%s)
357 (mapconcat (lambda (code)
358 (cond ((symbolp code)
361 (format "#x%04X" code))
363 (format "\n %S" code))))
365 (setq data (del-alist '->uppercase data))
367 (when (setq cell (assq '->lowercase data))
368 (setq cell (cdr cell))
369 (insert (format "(->lowercase\t%s)
371 (mapconcat (lambda (code)
372 (cond ((symbolp code)
375 (format "#x%04X" code))
377 (format "\n %S" code))))
379 (setq data (del-alist '->lowercase data))
381 (when (setq cell (assq '->titlecase data))
382 (setq cell (cdr cell))
383 (insert (format "(->titlecase\t%s)
385 (mapconcat (lambda (code)
386 (cond ((symbolp code)
389 (format "#x%04X" code))
391 (format "\n %S" code))))
393 (setq data (del-alist '->titlecase data))
398 (char-attribute-name< (car a)(car b)))))
402 (setq cell (car rest))
403 (if (setq ret (find-charset (car cell)))
404 (if (>= (length (symbol-name (charset-name ret))) 19)
406 (setq has-long-ccs-name t)
410 (setq rest (cdr rest)))
412 (setq cell (car data))
413 (cond ((setq ret (find-charset (car cell)))
414 (or (string-match "^mojikyo-pj-"
415 (symbol-name (charset-name ret)))
418 (if has-long-ccs-name
420 (list (find-charset 'ideograph-daikanwa)
421 (find-charset 'mojikyo)))
422 "(%-26s . %05d)\t; %c
424 "(%-26s . #x%X)\t; %c
428 (list (find-charset 'ideograph-daikanwa)
429 (find-charset 'mojikyo)))
430 "(%-18s . %05d)\t; %c
432 "(%-18s . #x%X)\t; %c
436 (if (= (charset-iso-graphic-plane ret) 1)
438 (cond ((= (charset-dimension ret) 1)
440 ((= (charset-dimension ret) 2)
442 ((= (charset-dimension ret) 3)
446 (decode-builtin-char ret (cdr cell))))))
447 ((string-match "^->" (symbol-name (car cell)))
452 (mapconcat (lambda (code)
453 (cond ((symbolp code)
456 (format "#x%04X" code))
458 (format "\n %S" code))))
461 (insert (format "(%-18s %s)
464 (mapconcat (function prin1-to-string)
466 ((eq (car cell) 'jisx0208-1978/4X)
467 (insert (format "(%-18s . #x%04X)
469 (car cell)(cdr cell))))
471 (insert (format "(%-18s . %S)
473 (car cell)(cdr cell)))
475 (setq data (cdr data)))
477 (goto-char (point-min))
478 (while (re-search-forward "[ \t]+$" nil t)
480 (goto-char (point-max))
481 (tabify (point-min)(point-max))
484 (defun decode-builtin-char (charset code-point)
485 (setq charset (get-charset charset))
486 (if (and (not (memq (charset-name charset)
487 '(ideograph-daikanwa mojikyo)))
488 (or (memq (charset-name charset)
489 '(ascii latin-viscii-upper
493 japanese-jisx0213-2))
494 (= (char-int (charset-iso-final-char charset)) 0)))
495 (decode-char charset code-point)
496 (let ((table (charset-mapping-table charset)))
499 (set-charset-mapping-table charset nil)
500 (decode-char charset code-point)
501 (set-charset-mapping-table charset table))
502 (decode-char charset code-point)))))
505 (defun char-db-update-comment ()
508 (goto-char (point-min))
509 (let (cdef table char)
510 (while (re-search-forward "^[ \t]*\\(([^.()]+)\\)" nil t)
511 (goto-char (match-beginning 1))
512 (setq cdef (read (current-buffer)))
513 (when (find-charset (car cdef))
514 (goto-char (match-end 0))
517 (not (eq (car cdef) 'ideograph-daikanwa))
518 (or (memq (car cdef) '(ascii latin-viscii-upper
522 japanese-jisx0213-2))
523 (= (char-int (charset-iso-final-char (car cdef)))
525 (apply (function make-char) cdef)
526 (if (setq table (charset-mapping-table (car cdef)))
527 (set-charset-mapping-table (car cdef) nil))
529 (apply (function make-char) cdef)
531 (set-charset-mapping-table (car cdef) table)))))
532 (when (not (or (< (char-int char) 32)
533 (and (<= 128 (char-int char))
534 (< (char-int char) 160))))
535 (delete-region (point) (point-at-eol))
536 (insert (format "\t; %c" char)))
539 (defun insert-char-data-with-variant (char &optional script printable)
540 (insert-char-data char printable)
541 (let ((variants (or (char-variants char)
542 (let ((ucs (get-char-attribute char '->ucs)))
544 (delete char (char-variants (int-char ucs)))))))
547 (setq variant (car variants))
548 (if (or (null script)
549 (null (setq vs (get-char-attribute variant 'script)))
551 (insert-char-data variant printable))
552 (setq variants (cdr variants))
555 (defun insert-char-range-data (min max &optional script)
559 (setq char (int-char code))
560 (insert-char-data-with-variant char script)
561 (setq code (1+ code))
564 (defun write-char-range-data-to-file (min max file &optional script)
565 (let ((coding-system-for-write 'utf-8))
567 (insert-char-range-data min max script)
568 (write-region (point-min)(point-max) file))))
570 (defvar what-character-original-window-configuration)
573 (defun what-char-definition (char)
574 (interactive (list (char-after)))
575 (let ((buf (get-buffer-create "*Character Description*"))
576 (the-buf (current-buffer))
577 (win-conf (current-window-configuration)))
579 (make-local-variable 'what-character-original-window-configuration)
580 (setq what-character-original-window-configuration win-conf)
581 (setq buffer-read-only nil)
585 (insert-char-data-with-variant char nil 'printable)
586 ;; (char-db-update-comment)
587 (set-buffer-modified-p nil)
588 (view-mode the-buf (lambda (buf)
589 (set-window-configuration
590 what-character-original-window-configuration)
592 (goto-char (point-min)))
594 (set-window-configuration
595 what-character-original-window-configuration)
596 (signal (car err) (cdr err)))))))
598 (provide 'char-db-util)
600 ;;; char-db-util.el ends here