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 'name data))
137 (setq cell (cdr cell))
139 (if (> (length cell) 47)
145 (setq data (del-alist 'name data))
147 (when (setq cell (assq 'ucs data))
148 (setq cell (cdr cell))
149 (insert (format "(ucs\t\t. #x%04X)
152 (setq data (del-alist 'ucs data))
154 (when (setq cell (assq '->ucs data))
155 (setq cell (cdr cell))
156 (insert (format "(->ucs\t\t. #x%04X)\t; %c
158 cell (decode-char 'ucs cell)))
159 (setq data (del-alist '->ucs data))
161 (when (setq cell (assq 'general-category data))
162 (setq ret (cdr cell))
164 "(general-category\t%s) ; %s
166 (mapconcat (lambda (cell)
169 (cond ((rassoc (cdr cell)
170 unidata-normative-category-alist)
171 "Normative Category")
173 unidata-informative-category-alist)
174 "Informative Category")
176 "Unknown Category"))))
177 (setq data (del-alist 'general-category data))
179 (when (setq cell (assq 'bidi-category data))
180 (setq cell (cdr cell))
181 (insert (format "(bidi-category\t. %S)
184 (setq data (del-alist 'bidi-category data))
186 (when (setq cell (assq 'mirrored data))
187 (setq cell (cdr cell))
188 (insert (format "(mirrored\t\t. %S)
191 (setq data (del-alist 'mirrored data))
194 ((setq cell (assq 'decimal-digit-value data))
195 (setq cell (cdr cell))
196 (insert (format "(decimal-digit-value . %S)
199 (setq data (del-alist 'decimal-digit-value data))
200 (when (setq cell (assq 'digit-value data))
201 (setq cell (cdr cell))
202 (insert (format "(digit-value\t . %S)
205 (setq data (del-alist 'digit-value data))
207 (when (setq cell (assq 'numeric-value data))
208 (setq cell (cdr cell))
209 (insert (format "(numeric-value\t . %S)
212 (setq data (del-alist 'numeric-value data))
216 (when (setq cell (assq 'digit-value data))
217 (setq cell (cdr cell))
218 (insert (format "(digit-value\t. %S)
221 (setq data (del-alist 'digit-value data))
223 (when (setq cell (assq 'numeric-value data))
224 (setq cell (cdr cell))
225 (insert (format "(numeric-value\t. %S)
228 (setq data (del-alist 'numeric-value data))
230 (when (setq cell (assq 'iso-10646-comment data))
231 (setq cell (cdr cell))
232 (insert (format "(iso-10646-comment\t. %S)
235 (setq data (del-alist 'iso-10646-comment data))
237 (when (setq cell (assq 'morohashi-daikanwa data))
238 (setq cell (cdr cell))
239 (insert (format "(morohashi-daikanwa\t%s)
241 (mapconcat (function prin1-to-string) cell " ")))
242 (setq data (del-alist 'morohashi-daikanwa data))
246 (when (setq cell (assq 'ideographic-radical data))
247 (setq radical (cdr cell))
248 (insert (format "(ideographic-radical . %S)\t; %c
251 (aref ideographic-radicals radical)))
252 (setq data (del-alist 'ideographic-radical data))
254 (when (setq cell (assq 'ideographic-strokes data))
255 (setq strokes (cdr cell))
256 (insert (format "(ideographic-strokes . %S)
259 (setq data (del-alist 'ideographic-strokes data))
261 (when (setq cell (assq 'kangxi-radical data))
262 (setq cell (cdr cell))
263 (unless (eq cell radical)
264 (insert (format "(kangxi-radical\t . %S)\t; %c
267 (aref ideographic-radicals cell)))
269 (setq data (del-alist 'kangxi-radical data))
271 (when (setq cell (assq 'kangxi-strokes data))
272 (setq cell (cdr cell))
273 (unless (eq cell strokes)
274 (insert (format "(kangxi-strokes\t . %S)
278 (setq data (del-alist 'kangxi-strokes data))
280 (when (setq cell (assq 'japanese-radical data))
281 (setq cell (cdr cell))
282 (unless (eq cell radical)
283 (insert (format "(japanese-radical\t . %S)\t; %c
286 (aref ideographic-radicals cell)))
288 (setq data (del-alist 'japanese-radical data))
290 (when (setq cell (assq 'japanese-strokes data))
291 (setq cell (cdr cell))
292 (unless (eq cell strokes)
293 (insert (format "(japanese-strokes\t . %S)
297 (setq data (del-alist 'japanese-strokes data))
299 (when (setq cell (assq 'cns-radical data))
300 (setq cell (cdr cell))
301 (insert (format "(cns-radical\t . %S)\t; %c
304 (aref ideographic-radicals cell)))
305 (setq data (del-alist 'cns-radical data))
307 (when (setq cell (assq 'cns-strokes data))
308 (setq cell (cdr cell))
309 (unless (eq cell strokes)
310 (insert (format "(cns-strokes\t . %S)
314 (setq data (del-alist 'cns-strokes data))
316 (when (setq cell (assq 'total-strokes data))
317 (setq cell (cdr cell))
318 (insert (format "(total-strokes\t . %S)
321 (setq data (del-alist 'total-strokes data))
323 (when (setq cell (assq '->ideograph data))
324 (setq cell (cdr cell))
325 (insert (format "(->ideograph\t%s)
327 (mapconcat (lambda (code)
328 (cond ((symbolp code)
331 (format "#x%04X" code))
333 (format "\n %S" code))))
335 (setq data (del-alist '->ideograph data))
337 (when (setq cell (assq '->decomposition data))
338 (setq cell (cdr cell))
339 (insert (format "(->decomposition\t%s)
341 (mapconcat (lambda (code)
342 (cond ((symbolp code)
345 (format "#x%04X" code))
347 (format "\n %S" code))))
349 (setq data (del-alist '->decomposition data))
351 (when (setq cell (assq '->uppercase data))
352 (setq cell (cdr cell))
353 (insert (format "(->uppercase\t%s)
355 (mapconcat (lambda (code)
356 (cond ((symbolp code)
359 (format "#x%04X" code))
361 (format "\n %S" code))))
363 (setq data (del-alist '->uppercase data))
365 (when (setq cell (assq '->lowercase data))
366 (setq cell (cdr cell))
367 (insert (format "(->lowercase\t%s)
369 (mapconcat (lambda (code)
370 (cond ((symbolp code)
373 (format "#x%04X" code))
375 (format "\n %S" code))))
377 (setq data (del-alist '->lowercase data))
379 (when (setq cell (assq '->titlecase data))
380 (setq cell (cdr cell))
381 (insert (format "(->titlecase\t%s)
383 (mapconcat (lambda (code)
384 (cond ((symbolp code)
387 (format "#x%04X" code))
389 (format "\n %S" code))))
391 (setq data (del-alist '->titlecase data))
396 (char-attribute-name< (car a)(car b)))))
400 (setq cell (car rest))
401 (if (setq ret (find-charset (car cell)))
402 (if (>= (length (symbol-name (charset-name ret))) 19)
404 (setq has-long-ccs-name t)
408 (setq rest (cdr rest)))
410 (setq cell (car data))
411 (cond ((setq ret (find-charset (car cell)))
412 (or (string-match "^mojikyo-pj-"
413 (symbol-name (charset-name ret)))
416 (if has-long-ccs-name
418 (list (find-charset 'ideograph-daikanwa)
419 (find-charset 'mojikyo)))
420 "(%-26s . %05d)\t; %c
422 "(%-26s . #x%X)\t; %c
426 (list (find-charset 'ideograph-daikanwa)
427 (find-charset 'mojikyo)))
428 "(%-18s . %05d)\t; %c
430 "(%-18s . #x%X)\t; %c
434 (if (= (charset-iso-graphic-plane ret) 1)
436 (cond ((= (charset-dimension ret) 1)
438 ((= (charset-dimension ret) 2)
440 ((= (charset-dimension ret) 3)
444 (decode-builtin-char ret (cdr cell))))))
445 ((string-match "^->" (symbol-name (car cell)))
450 (mapconcat (lambda (code)
451 (cond ((symbolp code)
454 (format "#x%04X" code))
456 (format "\n %S" code))))
459 (insert (format "(%-18s %s)
462 (mapconcat (function prin1-to-string)
464 ((eq (car cell) 'jisx0208-1978/4X)
465 (insert (format "(%-18s . #x%04X)
467 (car cell)(cdr cell))))
469 (insert (format "(%-18s . %S)
471 (car cell)(cdr cell)))
473 (setq data (cdr data)))
475 (goto-char (point-min))
476 (while (re-search-forward "[ \t]+$" nil t)
478 (goto-char (point-max))
479 (tabify (point-min)(point-max))
482 (defun decode-builtin-char (charset code-point)
483 (setq charset (get-charset charset))
484 (if (and (not (memq (charset-name charset)
485 '(ideograph-daikanwa mojikyo)))
486 (or (memq (charset-name charset)
487 '(ascii latin-viscii-upper
491 japanese-jisx0213-2))
492 (= (char-int (charset-iso-final-char charset)) 0)))
493 (decode-char charset code-point)
494 (let ((table (charset-mapping-table charset)))
497 (set-charset-mapping-table charset nil)
498 (decode-char charset code-point)
499 (set-charset-mapping-table charset table))
500 (decode-char charset code-point)))))
503 (defun char-db-update-comment ()
506 (goto-char (point-min))
507 (let (cdef table char)
508 (while (re-search-forward "^[ \t]*\\(([^.()]+)\\)" nil t)
509 (goto-char (match-beginning 1))
510 (setq cdef (read (current-buffer)))
511 (when (find-charset (car cdef))
512 (goto-char (match-end 0))
515 (not (eq (car cdef) 'ideograph-daikanwa))
516 (or (memq (car cdef) '(ascii latin-viscii-upper
520 japanese-jisx0213-2))
521 (= (char-int (charset-iso-final-char (car cdef)))
523 (apply (function make-char) cdef)
524 (if (setq table (charset-mapping-table (car cdef)))
525 (set-charset-mapping-table (car cdef) nil))
527 (apply (function make-char) cdef)
529 (set-charset-mapping-table (car cdef) table)))))
530 (when (not (or (< (char-int char) 32)
531 (and (<= 128 (char-int char))
532 (< (char-int char) 160))))
533 (delete-region (point) (point-at-eol))
534 (insert (format "\t; %c" char)))
537 (defun insert-char-data-with-variant (char)
538 (insert-char-data char)
539 (let ((variants (or (char-variants char)
540 (let ((ucs (get-char-attribute char '->ucs)))
542 (delete char (char-variants (int-char ucs))))))))
544 (insert-char-data (car variants))
545 (setq variants (cdr variants))
548 (defun insert-char-range-data (min max)
552 (setq char (int-char code))
553 (insert-char-data-with-variant char)
554 (setq code (1+ code))
557 (defun write-char-range-data-to-file (min max file)
558 (let ((coding-system-for-write 'utf-8))
560 (insert-char-range-data min max)
561 (write-region (point-min)(point-max) file))))
563 (defvar what-character-original-window-configuration)
566 (defun what-char-definition (char)
567 (interactive (list (char-after)))
568 (let ((buf (get-buffer-create "*Character Description*"))
569 (the-buf (current-buffer))
570 (win-conf (current-window-configuration)))
572 (make-local-variable 'what-character-original-window-configuration)
573 (setq what-character-original-window-configuration win-conf)
574 (setq buffer-read-only nil)
578 (insert-char-data-with-variant char)
579 ;; (char-db-update-comment)
580 (set-buffer-modified-p nil)
581 (view-mode the-buf (lambda (buf)
582 (set-window-configuration
583 what-character-original-window-configuration)
585 (goto-char (point-min)))
587 (set-window-configuration
588 what-character-original-window-configuration)
589 (signal (car err) (cdr err)))))))
591 (provide 'char-db-util)
593 ;;; char-db-util.el ends here