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)
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)
343 (format "#x%04X" code))
345 (format "\n %S" code))))
347 (setq data (del-alist '->decomposition data))
349 (when (setq cell (assq '->uppercase data))
350 (setq cell (cdr cell))
351 (insert (format "(->uppercase\t%s)
353 (mapconcat (lambda (code)
354 (cond ((symbolp code)
357 (format "#x%04X" code))
359 (format "\n %S" code))))
361 (setq data (del-alist '->uppercase data))
363 (when (setq cell (assq '->lowercase data))
364 (setq cell (cdr cell))
365 (insert (format "(->lowercase\t%s)
367 (mapconcat (lambda (code)
368 (cond ((symbolp code)
371 (format "#x%04X" code))
373 (format "\n %S" code))))
375 (setq data (del-alist '->lowercase data))
377 (when (setq cell (assq '->titlecase data))
378 (setq cell (cdr cell))
379 (insert (format "(->titlecase\t%s)
381 (mapconcat (lambda (code)
382 (cond ((symbolp code)
385 (format "#x%04X" code))
387 (format "\n %S" code))))
389 (setq data (del-alist '->titlecase data))
394 (char-attribute-name< (car a)(car b)))))
398 (setq cell (car rest))
399 (if (setq ret (find-charset (car cell)))
400 (if (>= (length (symbol-name (charset-name ret))) 19)
402 (setq has-long-ccs-name t)
406 (setq rest (cdr rest)))
408 (setq cell (car data))
409 (cond ((setq ret (find-charset (car cell)))
410 (or (string-match "^mojikyo-pj-"
411 (symbol-name (charset-name ret)))
414 (if has-long-ccs-name
416 (list (find-charset 'ideograph-daikanwa)
417 (find-charset 'mojikyo)))
418 "(%-26s . %05d)\t; %c
420 "(%-26s . #x%X)\t; %c
424 (list (find-charset 'ideograph-daikanwa)
425 (find-charset 'mojikyo)))
426 "(%-18s . %05d)\t; %c
428 "(%-18s . #x%X)\t; %c
432 (if (= (charset-iso-graphic-plane ret) 1)
434 (cond ((= (charset-dimension ret) 1)
436 ((= (charset-dimension ret) 2)
438 ((= (charset-dimension ret) 3)
442 (decode-builtin-char ret (cdr cell))))))
443 ((string-match "^->" (symbol-name (car cell)))
448 (mapconcat (lambda (code)
449 (cond ((symbolp code)
452 (format "#x%04X" code))
454 (format "\n %S" code))))
457 (insert (format "(%-18s %s)
460 (mapconcat (function prin1-to-string)
462 ((eq (car cell) 'jisx0208-1978/4X)
463 (insert (format "(%-18s . #x%04X)
465 (car cell)(cdr cell))))
467 (insert (format "(%-18s . %S)
469 (car cell)(cdr cell)))
471 (setq data (cdr data)))
473 (goto-char (point-min))
474 (while (re-search-forward "[ \t]+$" nil t)
476 (goto-char (point-max))
477 (tabify (point-min)(point-max))
480 (defun decode-builtin-char (charset code-point)
481 (setq charset (get-charset charset))
482 (if (and (not (memq (charset-name charset)
483 '(ideograph-daikanwa mojikyo)))
484 (or (memq (charset-name charset)
485 '(ascii latin-viscii-upper
489 japanese-jisx0213-2))
490 (= (char-int (charset-iso-final-char charset)) 0)))
491 (decode-char charset code-point)
492 (let ((table (charset-mapping-table charset)))
495 (set-charset-mapping-table charset nil)
496 (decode-char charset code-point)
497 (set-charset-mapping-table charset table))
498 (decode-char charset code-point)))))
501 (defun char-db-update-comment ()
504 (goto-char (point-min))
505 (let (cdef table char)
506 (while (re-search-forward "^[ \t]*\\(([^.()]+)\\)" nil t)
507 (goto-char (match-beginning 1))
508 (setq cdef (read (current-buffer)))
509 (when (find-charset (car cdef))
510 (goto-char (match-end 0))
513 (not (eq (car cdef) 'ideograph-daikanwa))
514 (or (memq (car cdef) '(ascii latin-viscii-upper
518 japanese-jisx0213-2))
519 (= (char-int (charset-iso-final-char (car cdef)))
521 (apply (function make-char) cdef)
522 (if (setq table (charset-mapping-table (car cdef)))
523 (set-charset-mapping-table (car cdef) nil))
525 (apply (function make-char) cdef)
527 (set-charset-mapping-table (car cdef) table)))))
528 (when (not (or (< (char-int char) 32)
529 (and (<= 128 (char-int char))
530 (< (char-int char) 160))))
531 (delete-region (point) (point-at-eol))
532 (insert (format "\t; %c" char)))
535 (defun insert-char-data-with-variant (char &optional script)
536 (insert-char-data char)
537 (let ((variants (or (char-variants char)
538 (let ((ucs (get-char-attribute char '->ucs)))
540 (delete char (char-variants (int-char ucs)))))))
543 (setq variant (car variants))
544 (if (or (null script)
545 (null (setq vs (get-char-attribute variant 'script)))
547 (insert-char-data variant))
548 (setq variants (cdr variants))
551 (defun insert-char-range-data (min max &optional script)
555 (setq char (int-char code))
556 (insert-char-data-with-variant char script)
557 (setq code (1+ code))
560 (defun write-char-range-data-to-file (min max file &optional script)
561 (let ((coding-system-for-write 'utf-8))
563 (insert-char-range-data min max script)
564 (write-region (point-min)(point-max) file))))
566 (defvar what-character-original-window-configuration)
569 (defun what-char-definition (char)
570 (interactive (list (char-after)))
571 (let ((buf (get-buffer-create "*Character Description*"))
572 (the-buf (current-buffer))
573 (win-conf (current-window-configuration)))
575 (make-local-variable 'what-character-original-window-configuration)
576 (setq what-character-original-window-configuration win-conf)
577 (setq buffer-read-only nil)
581 (insert-char-data-with-variant char)
582 ;; (char-db-update-comment)
583 (set-buffer-modified-p nil)
584 (view-mode the-buf (lambda (buf)
585 (set-window-configuration
586 what-character-original-window-configuration)
588 (goto-char (point-min)))
590 (set-window-configuration
591 what-character-original-window-configuration)
592 (signal (car err) (cdr err)))))))
594 (provide 'char-db-util)
596 ;;; char-db-util.el ends here