(shuowen-radicals): Add ?烏; add ?丶, ?丹, ?青, ?井, ?皀, ?鬯, ?食,
[chise/xemacs-chise.git.1] / lisp / utf-2000 / char-db-util.el
1 ;;; char-db-util.el --- Character Database utility -*- coding: utf-8-er; -*-
2
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;;   2007 MORIOKA Tomohiko.
5
6 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
7 ;; Keywords: CHISE, Character Database, ISO/IEC 10646, UCS, Unicode, MULE.
8
9 ;; This file is part of XEmacs CHISE.
10
11 ;; XEmacs CHISE is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; XEmacs CHISE is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs CHISE; see the file COPYING.  If not, write to
23 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Code:
27
28 (require 'alist)
29
30 (defconst unidata-normative-category-alist
31   '(("Lu" letter        uppercase)
32     ("Ll" letter        lowercase)
33     ("Lt" letter        titlecase)
34     ("Mn" mark          non-spacing)
35     ("Mc" mark          spacing-combining)
36     ("Me" mark          enclosing)
37     ("Nd" number        decimal-digit)
38     ("Nl" number        letter)
39     ("No" number        other)
40     ("Zs" separator     space)
41     ("Zl" separator     line)
42     ("Zp" separator     paragraph)
43     ("Cc" other         control)
44     ("Cf" other         format)
45     ("Cs" other         surrogate)
46     ("Co" other         private-use)
47     ("Cn" other         not-assigned)))
48
49 (defconst unidata-informative-category-alist
50   '(("Lm" letter        modifier)
51     ("Lo" letter        other)
52     ("Pc" punctuation   connector)
53     ("Pd" punctuation   dash)
54     ("Ps" punctuation   open)
55     ("Pe" punctuation   close)
56     ("Pi" punctuation   initial-quote)
57     ("Pf" punctuation   final-quote)
58     ("Po" punctuation   other)
59     ("Sm" symbol        math)
60     ("Sc" symbol        currency)
61     ("Sk" symbol        modifier)
62     ("So" symbol        other)
63     ))
64
65 (defconst ideographic-radicals
66   (let ((v (make-vector 215 nil))
67         (i 1))
68     (while (< i 215)
69       (aset v i (decode-char '=ucs (+ #x2EFF i)))
70       (setq i (1+ i)))
71     v))
72
73 (defun ideographic-radical (number)
74   (aref ideographic-radicals number))
75
76 (defconst shuowen-radicals
77   [?一 ?上 ?示 ?三 ?王 ?玉 ?玨 ?气 ?士 ?丨 ; 010
78    ?屮 ?艸 ?蓐 ?茻 ?小 ?八 ?釆 ?半 ?牛 ?犛 ; 020
79    ?告 ?口 ?凵 ?吅 ?哭 ?走 ?止 ?癶 ?步 ?此 ; 030
80    ?正 ?是 ?辵 ?彳 ?廴 ?㢟 ?行 ?齒 ?牙 ?足 ; 040
81    ?疋 ?品 ?龠 ?冊 ?㗊 ?舌 ?干 ?谷 ?只 ?㕯 ; 050
82    ?句 ?丩 ?古 ?十 ?卅 ?言 ?誩 ?音 ?䇂 ?丵 ; 060
83    ?菐 ?𠬞 ?廾 ?共 ?異 ?舁 ?𦥑 ?䢅 ?爨 ?革 ; 070
84    ?鬲 ?䰜 ?爪 ?𠃨 ?鬥 ?又 ?𠂇 ?㕜 ?支 ?𦘒 ; 080
85    ?聿 ?畫 ?隶 ?堅 ?臣 ?殳 ?殺 ?𠘧 ?寸 ?皮 ; 090
86    ?㼱 ?攴 ?敎 ?卜 ?用 ?爻 ?㸚 ?𥄎 ?目 ?䀠 ; 100
87    ?眉 ?盾 ?自 ?白 ?鼻 ?皕 ?習 ?羽 ?隹 ?奞 ; 110
88    ?萑 ?𦫳 ?苜 ?羊 ?羴 ?瞿 ?雔 ?雥 ?鳥 ?烏 ; 120
89    ?𠦒 ?冓 ?幺 ?𢆶 ?叀 ?玄 ?予 ?放 ?𠬪 ?𣦼 ; 130
90    ?歺 ?死 ?冎 ?骨 ?肉 ?筋 ?刀 ?刃 ?㓞 ?丰 ; 140
91    ?耒 ?𧢲 ?竹 ?箕 ?丌 ?左 ?工 ?㠭 ?巫 ?甘 ; 150
92    ?曰 ?乃 ?丂 ?可 ?兮 ?号 ?亏 ?旨 ?喜 ?壴 ; 160
93    ?鼓 ?豈 ?豆 ?豊 ?豐 ?䖒 ?虍 ?虎 ?虤 ?皿 ; 170
94    ?𠙴 ?去 ?血 ?丶 ?丹 ?青 ?井 ?皀 ?鬯 ?食 ; 180
95    ?亼 ?會 ?倉 ?入 ?缶 ?矢 ?高 ?冂 ?𩫏 ?京 ; 190
96    ?亯 ?畗 ?𣆪 ?㐭 ?𠾂 ?來 ?麥 ?夊 ?舛 ?䑞 ; 200
97    ?韋 ?弟 ?夂 ?久 ?桀
98    ])
99
100 (defun shuowen-radical (number)
101   (aref shuowen-radicals (1- number)))
102
103 (defvar char-db-file-coding-system 'utf-8-mcs-er)
104
105 (defvar char-db-feature-domains
106   '(ucs ucs/compat daikanwa cns gt jis jis/alt jis/a jis/b
107         jis-x0212 jis-x0213 cdp shinjigen misc unknown))
108
109 (defvar char-db-ignored-attributes '(ideographic-products))
110
111 (defun char-attribute-name< (ka kb)
112   (cond
113    ((eq '->denotational kb)
114     t)
115    ((eq '->subsumptive kb)
116     (not (eq '->denotational ka)))
117    ((eq '->denotational ka)
118     nil)
119    ((eq '->subsumptive ka)
120     nil)
121    ((and (symbolp ka)
122          (string-match "^->" (symbol-name ka)))
123     (cond ((and (symbolp kb)
124                 (string-match "^->" (symbol-name kb)))
125            (string< (symbol-name ka)
126                     (symbol-name kb))
127            ))
128     )
129    ((and (symbolp kb)
130          (string-match "^->" (symbol-name kb)))
131     t)
132    ((and (symbolp ka)
133          (string-match "^<-" (symbol-name ka)))
134     (cond ((symbolp kb)
135            (cond ((string-match "^<-" (symbol-name kb))
136                   (string< (symbol-name ka)
137                            (symbol-name kb))
138                   )
139                  ;; ((string-match "^->" (symbol-name kb))
140                  ;;  t)
141                  )))
142     )
143    ((and (symbolp kb)
144          (string-match "^<-" (symbol-name kb)))
145     t
146     ;; (not (string-match "^->" (symbol-name ka)))
147     )
148    ((find-charset ka)
149     (if (find-charset kb)
150         (if (<= (charset-id ka) 1)
151             (if (<= (charset-id kb) 1)
152                 (cond
153                  ((= (charset-dimension ka)
154                      (charset-dimension kb))
155                   (> (charset-id ka)(charset-id kb)))
156                  (t
157                   (> (charset-dimension ka)
158                      (charset-dimension kb))
159                   ))
160               t)
161           (if (<= (charset-id kb) 1)
162               nil
163             (< (charset-id ka)(charset-id kb))))
164       nil))
165    ((find-charset kb)
166     t)
167    ((symbolp ka)
168     (cond ((symbolp kb)
169            (string< (symbol-name ka)
170                     (symbol-name kb)))
171           (t)))
172    ((symbolp kb)
173     nil)))
174
175 (defvar char-db-coded-charset-priority-list
176   '(ascii
177     control-1
178     latin-iso8859-1
179     latin-iso8859-2
180     latin-iso8859-3
181     latin-iso8859-4
182     latin-iso8859-9
183     latin-jisx0201
184     cyrillic-iso8859-5
185     greek-iso8859-7
186     thai-tis620
187     =jis-x0208
188     =jis-x0208@1978
189     =jis-x0208@1983
190     japanese-jisx0212
191     chinese-gb2312
192     =jis-x0208@1990
193     chinese-cns11643-1
194     chinese-cns11643-2
195     chinese-cns11643-3
196     chinese-cns11643-4
197     chinese-cns11643-5
198     chinese-cns11643-6
199     chinese-cns11643-7
200     =jis-x0213-1-2000
201     =jis-x0213-2-2000
202     korean-ksc5601
203     chinese-isoir165
204     katakana-jisx0201
205     hebrew-iso8859-8
206     chinese-gb12345
207     latin-viscii
208     ethiopic-ucs
209     =big5-cdp
210     =gt
211     ideograph-daikanwa-2
212     ideograph-daikanwa
213     =cbeta
214     ideograph-hanziku-1
215     ideograph-hanziku-2
216     ideograph-hanziku-3
217     ideograph-hanziku-4
218     ideograph-hanziku-5
219     ideograph-hanziku-6
220     ideograph-hanziku-7
221     ideograph-hanziku-8
222     ideograph-hanziku-9
223     ideograph-hanziku-10
224     ideograph-hanziku-11
225     ideograph-hanziku-12
226     =gt-k
227     =ucs@iso
228     =ucs@unicode
229     =big5
230     =big5-eten
231     =jis-x0208@1997
232     =zinbun-oracle
233     =ruimoku-v6
234     =jef-china3))
235
236 (defun char-db-make-char-spec (char)
237   (let (ret char-spec)
238     (cond ((characterp char)
239            (cond ((and (setq ret (encode-char char '=ucs 'defined-only))
240                        (not (and (<= #xE000 ret)(<= ret #xF8FF))))
241                   (setq char-spec (list (cons '=ucs ret)))
242                   (cond ((setq ret (get-char-attribute char 'name))
243                          (setq char-spec (cons (cons 'name ret) char-spec))
244                          )
245                         ((setq ret (get-char-attribute char 'name*))
246                          (setq char-spec (cons (cons 'name* ret) char-spec))
247                          ))
248                   )
249                  ((setq ret
250                         (catch 'tag
251                           (let ((rest char-db-coded-charset-priority-list)
252                                 ccs)
253                             (while rest
254                               (setq ccs (charset-name
255                                          (find-charset (car rest))))
256                               (if (setq ret
257                                         (encode-char char ccs
258                                                      'defined-only))
259                                   (throw 'tag (cons ccs ret)))
260                               (setq rest (cdr rest))))))
261                   (setq char-spec (list ret))
262                   (dolist (ccs (delq (car ret) (charset-list)))
263                     (if (and (or (charset-iso-final-char ccs)
264                                  (memq ccs
265                                        '(=daikanwa
266                                          =daikanwa@rev2
267                                          ;; =gt-k
268                                          =jis-x0208@1997
269                                          )))
270                              (setq ccs (charset-name ccs))
271                              (null (assq ccs char-spec))
272                              (setq ret (encode-char char ccs 'defined-only)))
273                         (setq char-spec (cons (cons ccs ret) char-spec))))
274                   (if (null char-spec)
275                       (setq char-spec (split-char char)))
276                   (cond ((setq ret (get-char-attribute char 'name))
277                          (setq char-spec (cons (cons 'name ret) char-spec))
278                          )
279                         ((setq ret (get-char-attribute char 'name*))
280                          (setq char-spec (cons (cons 'name* ret) char-spec))
281                          ))
282                   ))
283            char-spec)
284           ((consp char)
285            char))))
286     
287 (defun char-db-insert-char-spec (char &optional readable column
288                                       required-features)
289   (unless column
290     (setq column (current-column)))
291   (let (char-spec temp-char)
292     (setq char-spec (char-db-make-char-spec char))
293     (unless (or (characterp char) ; char
294                 (condition-case nil
295                     (setq char (find-char char-spec))
296                   (error nil)))
297       ;; define temporary character
298       ;;   Current implementation is dirty.
299       (setq temp-char (define-char (cons '(ideograph-daikanwa . 0)
300                                          char-spec)))
301       (remove-char-attribute temp-char 'ideograph-daikanwa)
302       (setq char temp-char))
303     (insert-char-attributes char
304                             readable
305                             (union (mapcar #'car char-spec)
306                                    required-features))
307     (when temp-char
308       ;; undefine temporary character
309       ;;   Current implementation is dirty.
310       (setq char-spec (char-attribute-alist temp-char))
311       (while char-spec
312         (remove-char-attribute temp-char (car (car char-spec)))
313         (setq char-spec (cdr char-spec))))))
314
315 (defun char-db-insert-alist (alist &optional readable column)
316   (unless column
317     (setq column (current-column)))
318   (let ((line-breaking
319          (concat "\n" (make-string (1+ column) ?\ )))
320         name value
321         ret al ; cal
322         key
323         lbs cell rest separator)
324     (insert "(")
325     (while alist
326       (setq name (car (car alist))
327             value (cdr (car alist)))
328       (cond ((eq name 'char)
329              (insert "(char . ")
330              (if (setq ret (condition-case nil
331                                (find-char value)
332                              (error nil)))
333                  (progn
334                    (setq al nil
335                          ;; cal nil
336                          )
337                    (while value
338                      (setq key (car (car value)))
339                      ;; (if (find-charset key)
340                      ;;     (setq cal (cons key cal))
341                      (setq al (cons key al))
342                      ;; )
343                      (setq value (cdr value)))
344                    (insert-char-attributes ret
345                                            readable
346                                            (or al 'none) ; cal
347                                            ))
348                (insert (prin1-to-string value)))
349              (insert ")")
350              (insert line-breaking))
351             ((consp value)
352              (insert (format "(%-18s " name))
353              (setq lbs (concat "\n" (make-string (current-column) ?\ )))
354              (while (consp value)
355                (setq cell (car value))
356                (if (and (consp cell)
357                         (consp (car cell))
358                         (setq ret (condition-case nil
359                                       (find-char cell)
360                                     (error nil)))
361                         )
362                    (progn
363                      (setq rest cell
364                            al nil
365                            ;; cal nil
366                            )
367                      (while rest
368                        (setq key (car (car rest)))
369                        ;; (if (find-charset key)
370                        ;;     (setq cal (cons key cal))
371                        (setq al (cons key al))
372                        ;; )
373                        (setq rest (cdr rest)))
374                      (if separator
375                          (insert lbs))
376                      (insert-char-attributes ret
377                                              readable
378                                              al ; cal
379                                              )
380                      (setq separator lbs))
381                  (if separator
382                      (insert separator))
383                  (insert (prin1-to-string cell))
384                  (setq separator " "))
385                (setq value (cdr value)))
386              (insert ")")
387              (insert line-breaking))
388             (t
389              (insert (format "(%-18s . %S)%s"
390                              name value
391                              line-breaking))))
392       (setq alist (cdr alist))))
393   (insert ")"))
394
395 (defun char-db-insert-char-reference (plist &optional readable column)
396   (unless column
397     (setq column (current-column)))
398   (let ((line-breaking
399          (concat "\n" (make-string (1+ column) ?\ )))
400         (separator "")
401         name value)
402     (insert "(")
403     (while plist
404       (setq name (pop plist))
405       (setq value (pop plist))
406       (cond ((eq name :char)
407              (insert separator)
408              (insert ":char\t")
409              (cond ((numberp value)
410                     (setq value (decode-char '=ucs value)))
411                    ;; ((consp value)
412                    ;;  (setq value (or (find-char value)
413                    ;;                  value)))
414                    )
415              (char-db-insert-char-spec value readable)
416              (insert line-breaking)
417              (setq separator ""))
418             ((eq name :radical)
419              (insert (format "%s%s\t%d ; %c%s"
420                              separator
421                              name value
422                              (ideographic-radical value)
423                              line-breaking))
424              (setq separator ""))
425             (t
426              (insert (format "%s%s\t%S" separator name value))
427              (setq separator line-breaking)))
428       ))
429   (insert ")"))
430
431 (defun char-db-decode-isolated-char (ccs code-point)
432   (let (ret)
433     (setq ret
434           (cond ((eq ccs 'arabic-iso8859-6)
435                  (decode-char ccs code-point))
436                 ((and (memq ccs '(=gt-pj-1
437                                   =gt-pj-2
438                                   =gt-pj-3
439                                   =gt-pj-4
440                                   =gt-pj-5
441                                   =gt-pj-6
442                                   =gt-pj-7
443                                   =gt-pj-8
444                                   =gt-pj-9
445                                   =gt-pj-10
446                                   =gt-pj-11))
447                       (setq ret (decode-char ccs code-point))
448                       (setq ret (encode-char ret '=gt 'defined-only)))
449                  (decode-builtin-char '=gt ret))
450                 (t
451                  (decode-builtin-char ccs code-point))))
452     (cond ((and (<= 0 (char-int ret))
453                 (<= (char-int ret) #x1F))
454            (decode-char '=ucs (+ #x2400 (char-int ret))))
455           ((= (char-int ret) #x7F)
456            ?\u2421)
457           (t ret))))
458
459 (defvar char-db-convert-obsolete-format t)
460
461 (defun char-db-insert-ccs-feature (name value line-breaking)
462   (insert
463    (format
464     (cond ((or (memq name '(=daikanwa
465                             =daikanwa@rev1 =daikanwa@rev2
466                             =gt =gt-k =cbeta =zinbun-oracle))
467                (string-match "^=adobe-" (symbol-name name)))
468            "(%-18s . %05d)\t; %c")
469           ((eq name 'mojikyo)
470            "(%-18s . %06d)\t; %c")
471           ((>= (charset-dimension name) 2)
472            "(%-18s . #x%04X)\t; %c")
473           (t
474            "(%-18s . #x%02X)\t; %c"))
475     name
476     (if (= (charset-iso-graphic-plane name) 1)
477         (logior value
478                 (cond ((= (charset-dimension name) 1)
479                        #x80)
480                       ((= (charset-dimension name) 2)
481                        #x8080)
482                       ((= (charset-dimension name) 3)
483                        #x808080)
484                       (t 0)))
485       value)
486     (char-db-decode-isolated-char name value)))
487   (if (and (= (charset-chars name) 94)
488            (= (charset-dimension name) 2))
489       (insert (format " [%02d-%02d]"
490                       (- (lsh value -8) 32)
491                       (- (logand value 255) 32))))
492   (insert line-breaking))
493
494 (defun char-db-insert-relation-feature (char name value line-breaking
495                                              ccss readable)
496   (insert (format "(%-18s%s " name line-breaking))
497   (let ((lbs (concat "\n" (make-string (current-column) ?\ )))
498         separator cell sources required-features
499         ret)
500     (while (consp value)
501       (setq cell (car value))
502       (if (integerp cell)
503           (setq cell (decode-char '=ucs cell)))
504       (cond
505        ((eq name '->subsumptive)
506         (when (or (not (some (lambda (atr)
507                                (get-char-attribute cell atr))
508                              char-db-ignored-attributes))
509                   (some (lambda (ccs)
510                           (encode-char cell ccs 'defined-only))
511                         ccss))
512           (if separator
513               (insert lbs))
514           (let ((char-db-ignored-attributes
515                  (cons '<-subsumptive
516                        char-db-ignored-attributes)))
517             (insert-char-attributes cell readable))
518           (setq separator lbs))
519         )
520        ((characterp cell)
521         (setq sources
522               (get-char-attribute
523                char (intern (format "%s*sources" name))))
524         (setq required-features nil)
525         (dolist (source sources)
526           (cond
527            ((memq source '(JP JP/Jouyou shinjigen-1))
528             (setq required-features
529                   (union required-features
530                          '(=jis-x0208
531                            =jis-x0208@1990
532                            =jis-x0213-1-2000
533                            =jis-x0213-2-2000
534                            =jis-x0212
535                            =jis-x0208@1983
536                            =jis-x0208@1978))))
537            ((eq source 'CN)
538             (setq required-features
539                   (union required-features
540                          '(=gb2312
541                            =gb12345
542                            =iso-ir165)))))
543           (cond
544            ((find-charset (setq ret (intern (format "=%s" source))))
545             (setq required-features
546                   (cons ret required-features)))
547            (t (setq required-features
548                     (cons source required-features)))))
549         (cond ((string-match "@JP" (symbol-name name))
550                (setq required-features
551                      (union required-features
552                             '(=jis-x0208
553                               =jis-x0208@1990
554                               =jis-x0213-1-2000
555                               =jis-x0213-2-2000
556                               =jis-x0212
557                               =jis-x0208@1983
558                               =jis-x0208@1978))))
559               ((string-match "@CN" (symbol-name name))
560                (setq required-features
561                      (union required-features
562                             '(=gb2312
563                               =gb12345
564                               =iso-ir165)))))
565         (if separator
566             (insert lbs))
567         (if readable
568             (insert (format "%S" cell))
569           (char-db-insert-char-spec cell readable
570                                     nil
571                                     required-features))
572         (setq separator lbs))
573        ((consp cell)
574         (if separator
575             (insert lbs))
576         (if (consp (car cell))
577             (char-db-insert-char-spec cell readable)
578           (char-db-insert-char-reference cell readable))
579         (setq separator lbs))
580        (t
581         (if separator
582             (insert separator))
583         (insert (prin1-to-string cell))
584         (setq separator " ")))
585       (setq value (cdr value)))
586     (insert ")")
587     (insert line-breaking)))
588
589 (defun insert-char-attributes (char &optional readable attributes column)
590   (unless column
591     (setq column (current-column)))
592   (let (name value ; has-long-ccs-name
593         rest
594         radical strokes
595         (line-breaking
596          (concat "\n" (make-string (1+ column) ?\ )))
597         lbs cell separator ret
598         key al cal
599         dest-ccss ; sources required-features
600         ccss)
601     (let (atr-d)
602       (setq attributes
603             (sort (if attributes
604                       (if (consp attributes)
605                           (progn
606                             (dolist (name attributes)
607                               (unless (memq name char-db-ignored-attributes)
608                                 (if (find-charset name)
609                                     (push name ccss))
610                                 (push name atr-d)))
611                             atr-d))
612                     (dolist (name (char-attribute-list))
613                       (unless (memq name char-db-ignored-attributes)
614                         (if (find-charset name)
615                             (push name ccss))
616                         (push name atr-d)))
617                     atr-d)
618                   #'char-attribute-name<)))
619     (insert "(")
620     (when (memq '<-subsumptive attributes)
621       (when readable
622         (when (setq value (get-char-attribute char '<-subsumptive))
623           (char-db-insert-relation-feature char '<-subsumptive value
624                                            line-breaking
625                                            ccss readable)))
626       (setq attributes (delq '<-subsumptive attributes)))
627     (when (and (memq '<-denotational attributes)
628                (setq value (get-char-attribute char '<-denotational)))
629       (char-db-insert-relation-feature char '<-denotational value
630                                        line-breaking
631                                        ccss readable)
632       (setq attributes (delq '<-denotational attributes)))
633     (when (and (memq 'name attributes)
634                (setq value (get-char-attribute char 'name)))
635       (insert (format
636                (if (> (+ (current-column) (length value)) 48)
637                    "(name . %S)%s"
638                  "(name               . %S)%s")
639                value line-breaking))
640       (setq attributes (delq 'name attributes))
641       )
642     (when (and (memq 'name* attributes)
643                (setq value (get-char-attribute char 'name*)))
644       (insert (format
645                (if (> (+ (current-column) (length value)) 48)
646                    "(name* . %S)%s"
647                  "(name*              . %S)%s")
648                value line-breaking))
649       (setq attributes (delq 'name* attributes))
650       )
651     (when (and (memq 'script attributes)
652                (setq value (get-char-attribute char 'script)))
653       (insert (format "(script\t\t%s)%s"
654                       (mapconcat (function prin1-to-string)
655                                  value " ")
656                       line-breaking))
657       (setq attributes (delq 'script attributes))
658       )
659     (dolist (name '(=>ucs =>ucs*))
660       (when (and (memq name attributes)
661                  (setq value (get-char-attribute char name)))
662         (insert (format "(%-18s . #x%04X)\t; %c%s"
663                         name value (decode-char '=ucs value)
664                         line-breaking))
665         (setq attributes (delq name attributes))))
666     (dolist (name '(=>ucs@gb =>ucs@cns =>ucs@jis =>ucs@ks =>ucs@big5))
667       (when (and (memq name attributes)
668                  (setq value (get-char-attribute char name)))
669         (insert (format "(%-18s . #x%04X)\t; %c%s"
670                         name value
671                         (decode-char (intern
672                                       (concat "="
673                                               (substring
674                                                (symbol-name name) 2)))
675                                      value)
676                         line-breaking))
677         (setq attributes (delq name attributes))
678         ))
679     (dolist (name '(=>daikanwa))
680       (when (and (memq name attributes)
681                  (setq value (get-char-attribute char name)))
682         (insert
683          (if (integerp value)
684              (format "(%-18s . %05d)\t; %c%s"
685                      name value (decode-char '=daikanwa value)
686                      line-breaking)
687            (format "(%-18s %s)\t; %c%s"
688                    name
689                    (mapconcat (function prin1-to-string)
690                               value " ")
691                    (char-representative-of-daikanwa char)
692                    line-breaking)))
693         (setq attributes (delq name attributes))))
694     (when (and (memq 'general-category attributes)
695                (setq value (get-char-attribute char 'general-category)))
696       (insert (format
697                "(general-category\t%s) ; %s%s"
698                (mapconcat (lambda (cell)
699                             (format "%S" cell))
700                           value " ")
701                (cond ((rassoc value unidata-normative-category-alist)
702                       "Normative Category")
703                      ((rassoc value unidata-informative-category-alist)
704                       "Informative Category")
705                      (t
706                       "Unknown Category"))
707                line-breaking))
708       (setq attributes (delq 'general-category attributes))
709       )
710     (when (and (memq 'bidi-category attributes)
711                (setq value (get-char-attribute char 'bidi-category)))
712       (insert (format "(bidi-category\t. %S)%s"
713                       value
714                       line-breaking))
715       (setq attributes (delq 'bidi-category attributes))
716       )
717     (unless (or (not (memq 'mirrored attributes))
718                 (eq (setq value (get-char-attribute char 'mirrored 'empty))
719                     'empty))
720       (insert (format "(mirrored\t\t. %S)%s"
721                       value
722                       line-breaking))
723       (setq attributes (delq 'mirrored attributes))
724       )
725     (cond
726      ((and (memq 'decimal-digit-value attributes)
727            (setq value (get-char-attribute char 'decimal-digit-value)))
728       (insert (format "(decimal-digit-value . %S)%s"
729                       value
730                       line-breaking))
731       (setq attributes (delq 'decimal-digit-value attributes))
732       (when (and (memq 'digit-value attributes)
733                  (setq value (get-char-attribute char 'digit-value)))
734         (insert (format "(digit-value\t . %S)%s"
735                         value
736                         line-breaking))
737         (setq attributes (delq 'digit-value attributes))
738         )
739       (when (and (memq 'numeric-value attributes)
740                  (setq value (get-char-attribute char 'numeric-value)))
741         (insert (format "(numeric-value\t . %S)%s"
742                         value
743                         line-breaking))
744         (setq attributes (delq 'numeric-value attributes))
745         )
746       )
747      (t
748       (when (and (memq 'digit-value attributes)
749                  (setq value (get-char-attribute char 'digit-value)))
750         (insert (format "(digit-value\t. %S)%s"
751                         value
752                         line-breaking))
753         (setq attributes (delq 'digit-value attributes))
754         )
755       (when (and (memq 'numeric-value attributes)
756                  (setq value (get-char-attribute char 'numeric-value)))
757         (insert (format "(numeric-value\t. %S)%s"
758                         value
759                         line-breaking))
760         (setq attributes (delq 'numeric-value attributes))
761         )))
762     (when (and (memq 'iso-10646-comment attributes)
763                (setq value (get-char-attribute char 'iso-10646-comment)))
764       (insert (format "(iso-10646-comment\t. %S)%s"
765                       value
766                       line-breaking))
767       (setq attributes (delq 'iso-10646-comment attributes))
768       )
769     (when (and (memq 'morohashi-daikanwa attributes)
770                (setq value (get-char-attribute char 'morohashi-daikanwa)))
771       (insert (format "(morohashi-daikanwa\t%s)%s"
772                       (mapconcat (function prin1-to-string) value " ")
773                       line-breaking))
774       (setq attributes (delq 'morohashi-daikanwa attributes))
775       )
776     (setq radical nil
777           strokes nil)
778     (when (and (memq 'ideographic-radical attributes)
779                (setq value (get-char-attribute char 'ideographic-radical)))
780       (setq radical value)
781       (insert (format "(ideographic-radical . %S)\t; %c%s"
782                       radical
783                       (ideographic-radical radical)
784                       line-breaking))
785       (setq attributes (delq 'ideographic-radical attributes))
786       )
787     (when (and (memq 'shuowen-radical attributes)
788                (setq value (get-char-attribute char 'shuowen-radical)))
789       (insert (format "(shuowen-radical\t. %S)\t; %c%s"
790                       value
791                       (shuowen-radical value)
792                       line-breaking))
793       (setq attributes (delq 'shuowen-radical attributes))
794       )
795     (let (key)
796       (dolist (domain
797                (append
798                 char-db-feature-domains
799                 (let (dest domain)
800                   (dolist (feature (char-attribute-list))
801                     (setq feature (symbol-name feature))
802                     (when (string-match
803                            "\\(radical\\|strokes\\)@\\([^@*]+\\)\\(\\*\\|$\\)"
804                            feature)
805                       (setq domain (intern (match-string 2 feature)))
806                      (unless (memq domain dest)
807                        (setq dest (cons domain dest)))))
808                   (sort dest #'string<))))
809         (setq key (intern (format "%s@%s" 'ideographic-radical domain)))
810         (when (and (memq key attributes)
811                    (setq value (get-char-attribute char key)))
812           (setq radical value)
813           (insert (format "(%s . %S)\t; %c%s"
814                           key
815                           radical
816                           (ideographic-radical radical)
817                           line-breaking))
818           (setq attributes (delq key attributes))
819           )
820         (setq key (intern (format "%s@%s" 'ideographic-strokes domain)))
821         (when (and (memq key attributes)
822                    (setq value (get-char-attribute char key)))
823           (setq strokes value)
824           (insert (format "(%s . %S)%s"
825                           key
826                           strokes
827                           line-breaking))
828           (setq attributes (delq key attributes))
829           )
830         (setq key (intern (format "%s@%s" 'total-strokes domain)))
831         (when (and (memq key attributes)
832                    (setq value (get-char-attribute char key)))
833           (insert (format "(%s       . %S)%s"
834                           key
835                           value
836                           line-breaking))
837           (setq attributes (delq key attributes))
838           )
839         (dolist (feature '(ideographic-radical
840                            ideographic-strokes
841                            total-strokes))
842           (setq key (intern (format "%s@%s*sources" feature domain)))
843           (when (and (memq key attributes)
844                      (setq value (get-char-attribute char key)))
845             (insert (format "(%s%s" key line-breaking))
846             (dolist (cell value)
847               (insert (format " %s" cell)))
848             (insert ")")
849             (insert line-breaking)
850             (setq attributes (delq key attributes))
851             ))
852         ))
853     (when (and (memq 'ideographic-strokes attributes)
854                (setq value (get-char-attribute char 'ideographic-strokes)))
855       (setq strokes value)
856       (insert (format "(ideographic-strokes . %S)%s"
857                       strokes
858                       line-breaking))
859       (setq attributes (delq 'ideographic-strokes attributes))
860       )
861     (when (and (memq 'kangxi-radical attributes)
862                (setq value (get-char-attribute char 'kangxi-radical)))
863       (unless (eq value radical)
864         (insert (format "(kangxi-radical\t . %S)\t; %c%s"
865                         value
866                         (ideographic-radical value)
867                         line-breaking))
868         (or radical
869             (setq radical value)))
870       (setq attributes (delq 'kangxi-radical attributes))
871       )
872     (when (and (memq 'kangxi-strokes attributes)
873                (setq value (get-char-attribute char 'kangxi-strokes)))
874       (unless (eq value strokes)
875         (insert (format "(kangxi-strokes\t . %S)%s"
876                         value
877                         line-breaking))
878         (or strokes
879             (setq strokes value)))
880       (setq attributes (delq 'kangxi-strokes attributes))
881       )
882     (when (and (memq 'japanese-radical attributes)
883                (setq value (get-char-attribute char 'japanese-radical)))
884       (unless (eq value radical)
885         (insert (format "(japanese-radical\t . %S)\t; %c%s"
886                         value
887                         (ideographic-radical value)
888                         line-breaking))
889         (or radical
890             (setq radical value)))
891       (setq attributes (delq 'japanese-radical attributes))
892       )
893     (when (and (memq 'japanese-strokes attributes)
894                (setq value (get-char-attribute char 'japanese-strokes)))
895       (unless (eq value strokes)
896         (insert (format "(japanese-strokes\t . %S)%s"
897                         value
898                         line-breaking))
899         (or strokes
900             (setq strokes value)))
901       (setq attributes (delq 'japanese-strokes attributes))
902       )
903     (when (and (memq 'cns-radical attributes)
904                (setq value (get-char-attribute char 'cns-radical)))
905       (insert (format "(cns-radical\t . %S)\t; %c%s"
906                       value
907                       (ideographic-radical value)
908                       line-breaking))
909       (setq attributes (delq 'cns-radical attributes))
910       )
911     (when (and (memq 'cns-strokes attributes)
912                (setq value (get-char-attribute char 'cns-strokes)))
913       (unless (eq value strokes)
914         (insert (format "(cns-strokes\t . %S)%s"
915                         value
916                         line-breaking))
917         (or strokes
918             (setq strokes value)))
919       (setq attributes (delq 'cns-strokes attributes))
920       )
921     (when (and (memq 'shinjigen-1-radical attributes)
922                (setq value (get-char-attribute char 'shinjigen-1-radical)))
923       (unless (eq value radical)
924         (insert (format "(shinjigen-1-radical . %S)\t; %c%s"
925                         value
926                         (ideographic-radical value)
927                         line-breaking))
928         (or radical
929             (setq radical value)))
930       (setq attributes (delq 'shinjigen-1-radical attributes))
931       )
932     (when (and (memq 'ideographic- attributes)
933                (setq value (get-char-attribute char 'ideographic-)))
934       (insert "(ideographic-       ")
935       (setq lbs (concat "\n" (make-string (current-column) ?\ ))
936             separator nil)
937       (while (consp value)
938         (setq cell (car value))
939         (if (integerp cell)
940             (setq cell (decode-char '=ucs cell)))
941         (cond ((characterp cell)
942                (if separator
943                    (insert lbs))
944                (if readable
945                    (insert (format "%S" cell))
946                  (char-db-insert-char-spec cell readable))
947                (setq separator lbs))
948               ((consp cell)
949                (if separator
950                    (insert lbs))
951                (if (consp (car cell))
952                    (char-db-insert-char-spec cell readable)
953                  (char-db-insert-char-reference cell readable))
954                (setq separator lbs))
955               (t
956                (if separator
957                    (insert separator))
958                (insert (prin1-to-string cell))
959                (setq separator " ")))
960         (setq value (cdr value)))
961       (insert ")")
962       (insert line-breaking)
963       (setq attributes (delq 'ideographic- attributes)))
964     (when (and (memq 'total-strokes attributes)
965                (setq value (get-char-attribute char 'total-strokes)))
966       (insert (format "(total-strokes       . %S)%s"
967                       value
968                       line-breaking))
969       (setq attributes (delq 'total-strokes attributes))
970       )
971     (when (and (memq '->ideograph attributes)
972                (setq value (get-char-attribute char '->ideograph)))
973       (insert (format "(->ideograph\t%s)%s"
974                       (mapconcat (lambda (code)
975                                    (cond ((symbolp code)
976                                           (symbol-name code))
977                                          ((integerp code)
978                                           (format "#x%04X" code))
979                                          (t
980                                           (format "%s %S"
981                                                   line-breaking code))))
982                                  value " ")
983                       line-breaking))
984       (setq attributes (delq '->ideograph attributes))
985       )
986     ;; (when (and (memq '->decomposition attributes)
987     ;;            (setq value (get-char-attribute char '->decomposition)))
988     ;;   (insert (format "(->decomposition\t%s)%s"
989     ;;                   (mapconcat (lambda (code)
990     ;;                                (cond ((symbolp code)
991     ;;                                       (symbol-name code))
992     ;;                                      ((characterp code)
993     ;;                                       (if readable
994     ;;                                           (format "%S" code)
995     ;;                                         (format "#x%04X"
996     ;;                                                 (char-int code))
997     ;;                                         ))
998     ;;                                      ((integerp code)
999     ;;                                       (format "#x%04X" code))
1000     ;;                                      (t
1001     ;;                                       (format "%s%S" line-breaking code))))
1002     ;;                              value " ")
1003     ;;                   line-breaking))
1004     ;;   (setq attributes (delq '->decomposition attributes))
1005     ;;   )
1006     (if (equal (get-char-attribute char '->titlecase)
1007                (get-char-attribute char '->uppercase))
1008         (setq attributes (delq '->titlecase attributes)))
1009     (when (and (memq '->mojikyo attributes)
1010                (setq value (get-char-attribute char '->mojikyo)))
1011       (insert (format "(->mojikyo\t\t. %06d)\t; %c%s"
1012                       value (decode-char 'mojikyo value)
1013                       line-breaking))
1014       (setq attributes (delq '->mojikyo attributes))
1015       )
1016     (when (and (memq 'hanyu-dazidian-vol attributes)
1017                (setq value (get-char-attribute char 'hanyu-dazidian-vol)))
1018       (insert (format "(hanyu-dazidian-vol  . %d)%s"
1019                       value line-breaking))
1020       (setq attributes (delq 'hanyu-dazidian-vol attributes))
1021       )
1022     (when (and (memq 'hanyu-dazidian-page attributes)
1023                (setq value (get-char-attribute char 'hanyu-dazidian-page)))
1024       (insert (format "(hanyu-dazidian-page . %d)%s"
1025                       value line-breaking))
1026       (setq attributes (delq 'hanyu-dazidian-page attributes))
1027       )
1028     (when (and (memq 'hanyu-dazidian-char attributes)
1029                (setq value (get-char-attribute char 'hanyu-dazidian-char)))
1030       (insert (format "(hanyu-dazidian-char . %d)%s"
1031                       value line-breaking))
1032       (setq attributes (delq 'hanyu-dazidian-char attributes))
1033       )
1034     (unless readable
1035       (dolist (ignored '(composition
1036                          ->denotational <-subsumptive ->ucs-unified
1037                          ->ideographic-component-forms))
1038         (setq attributes (delq ignored attributes))))
1039     (while attributes
1040       (setq name (car attributes))
1041       (if (setq value (get-char-attribute char name))
1042           (cond ((setq ret (find-charset name))
1043                  (setq name (charset-name ret))
1044                  (if (and (not (memq name dest-ccss))
1045                           (prog1
1046                               (setq value (get-char-attribute char name))
1047                             (setq dest-ccss (cons name dest-ccss))))
1048                      (char-db-insert-ccs-feature name value line-breaking))
1049                  )
1050                 ((string-match "^=>ucs@" (symbol-name name))
1051                  (insert (format "(%-18s . #x%04X)\t; %c%s"
1052                                  name value (decode-char '=ucs value)
1053                                  line-breaking))
1054                  )
1055                 ((eq name 'jisx0208-1978/4X)
1056                  (insert (format "(%-18s . #x%04X)%s"
1057                                  name value
1058                                  line-breaking))
1059                  )
1060                 ((and
1061                   (not readable)
1062                   (not (eq name '->subsumptive))
1063                   (not (eq name '->uppercase))
1064                   (not (eq name '->lowercase))
1065                   (not (eq name '->titlecase))
1066                   (not (eq name '->canonical))
1067                   (not (eq name '->Bopomofo))
1068                   (not (eq name '->mistakable))
1069                   (not (eq name '->ideographic-variants))
1070                   (null (get-char-attribute
1071                          char (intern (format "%s*sources" name))))
1072                   (not (string-match "\\*sources$" (symbol-name name)))
1073                   (null (get-char-attribute
1074                          char (intern (format "%s*note" name))))
1075                   (not (string-match "\\*note$" (symbol-name name)))
1076                   (or (eq name '<-identical)
1077                       (eq name '<-uppercase)
1078                       (eq name '<-lowercase)
1079                       (eq name '<-titlecase)
1080                       (eq name '<-canonical)
1081                       (eq name '<-ideographic-variants)
1082                       ;; (eq name '<-synonyms)
1083                       (string-match "^<-synonyms" (symbol-name name))
1084                       (eq name '<-mistakable)
1085                       (when (string-match "^->" (symbol-name name))
1086                         (cond
1087                          ((string-match "^->fullwidth" (symbol-name name))
1088                           (not (and (consp value)
1089                                     (characterp (car value))
1090                                     (encode-char
1091                                      (car value) '=ucs 'defined-only)))
1092                           )
1093                          (t)))
1094                       ))
1095                  )
1096                 ((or (eq name 'ideographic-structure)
1097                      (eq name 'ideographic-combination)
1098                      (eq name 'ideographic-)
1099                      (eq name '=decomposition)
1100                      (string-match "^=>decomposition" (symbol-name name))
1101                      (string-match "^\\(->\\|<-\\)[^*]*$" (symbol-name name))
1102                      (string-match "^\\(->\\|<-\\)[^*]*\\*sources$"
1103                                    (symbol-name name))
1104                      )
1105                  (char-db-insert-relation-feature char name value
1106                                                   line-breaking
1107                                                   ccss readable))
1108                 ((memq name '(ideograph=
1109                               original-ideograph-of
1110                               ancient-ideograph-of
1111                               vulgar-ideograph-of
1112                               wrong-ideograph-of
1113                               ;; simplified-ideograph-of
1114                               ideographic-variants
1115                               ;; ideographic-different-form-of
1116                               ))
1117                  (insert (format "(%-18s%s " name line-breaking))
1118                  (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1119                        separator nil)
1120                  (while (consp value)
1121                    (setq cell (car value))
1122                    (if (and (consp cell)
1123                             (consp (car cell)))
1124                        (progn
1125                          (if separator
1126                              (insert lbs))
1127                          (char-db-insert-alist cell readable)
1128                          (setq separator lbs))
1129                      (if separator
1130                          (insert separator))
1131                      (insert (prin1-to-string cell))
1132                      (setq separator " "))
1133                    (setq value (cdr value)))
1134                  (insert ")")
1135                  (insert line-breaking))
1136                 ((consp value)
1137                  (insert (format "(%-18s " name))
1138                  (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1139                        separator nil)
1140                  (while (consp value)
1141                    (setq cell (car value))
1142                    (if (and (consp cell)
1143                             (consp (car cell))
1144                             (setq ret (condition-case nil
1145                                           (find-char cell)
1146                                         (error nil))))
1147                        (progn
1148                          (setq rest cell
1149                                al nil
1150                                cal nil)
1151                          (while rest
1152                            (setq key (car (car rest)))
1153                            (if (find-charset key)
1154                                (setq cal (cons key cal))
1155                              (setq al (cons key al)))
1156                            (setq rest (cdr rest)))
1157                          (if separator
1158                              (insert lbs))
1159                          (insert-char-attributes ret
1160                                                  readable
1161                                                  al cal)
1162                          (setq separator lbs))
1163                      (setq ret (prin1-to-string cell))
1164                      (if separator
1165                          (if (< (+ (current-column)
1166                                    (length ret)
1167                                    (length separator))
1168                                 76)
1169                              (insert separator)
1170                            (insert lbs)))
1171                      (insert ret)
1172                      (setq separator " "))
1173                    (setq value (cdr value)))
1174                  (insert ")")
1175                  (insert line-breaking))
1176                 (t
1177                  (insert (format "(%-18s" name))
1178                  (setq ret (prin1-to-string value))
1179                  (unless (< (+ (current-column)
1180                                (length ret)
1181                                3)
1182                             76)
1183                    (insert line-breaking))
1184                  (insert " . " ret ")" line-breaking)
1185                  ;; (insert (format "(%-18s . %S)%s"
1186                  ;;                 name value
1187                  ;;                 line-breaking))
1188                  )
1189                 ))
1190       (setq attributes (cdr attributes)))
1191     (insert ")")))
1192
1193 (defun insert-char-data (char &optional readable
1194                               attributes)
1195   (save-restriction
1196     (narrow-to-region (point)(point))
1197     (insert "(define-char
1198   '")
1199     (insert-char-attributes char readable attributes)
1200     (insert ")\n")
1201     (goto-char (point-min))
1202     (while (re-search-forward "[ \t]+$" nil t)
1203       (replace-match ""))
1204     ;; from tabify.
1205     (goto-char (point-min))
1206     (while (re-search-forward "[ \t][ \t][ \t]*" nil t)
1207       (let ((column (current-column))
1208             (indent-tabs-mode t))
1209         (delete-region (match-beginning 0) (point))
1210         (indent-to column)))
1211     (goto-char (point-max))
1212     ;; (tabify (point-min)(point-max))
1213     ))
1214
1215 (defun insert-char-data-with-variant (char &optional printable
1216                                            no-ucs-unified
1217                                            script excluded-script)
1218   (insert-char-data char printable)
1219   (let ((variants (char-variants char))
1220         rest
1221         variant vs ret)
1222     (setq variants (sort variants #'<))
1223     (setq rest variants)
1224     (setq variants (cons char variants))
1225     (while rest
1226       (setq variant (car rest))
1227       (unless (get-char-attribute variant '<-subsumptive)
1228         (if (and (or (null script)
1229                      (null (setq vs (get-char-attribute variant 'script)))
1230                      (memq script vs))
1231                  (or (null excluded-script)
1232                      (null (setq vs (get-char-attribute variant 'script)))
1233                      (not (memq excluded-script vs))))
1234             (unless (and no-ucs-unified (get-char-attribute variant '=ucs))
1235               (insert-char-data variant printable)
1236               (if (setq ret (char-variants variant))
1237                   (while ret
1238                     (or (memq (car ret) variants)
1239                         ;; (get-char-attribute (car ret) '<-subsumptive)
1240                         (setq rest (nconc rest (list (car ret)))))
1241                     (setq ret (cdr ret)))))))
1242       (setq rest (cdr rest)))))
1243
1244 (defun insert-char-range-data (min max &optional script excluded-script)
1245   (let ((code min)
1246         char)
1247     (while (<= code max)
1248       (setq char (decode-char '=ucs code))
1249       (if (encode-char char '=ucs 'defined-only)
1250           (insert-char-data-with-variant char nil 'no-ucs-unified
1251                                          script excluded-script))
1252       (setq code (1+ code)))))
1253
1254 (defun write-char-range-data-to-file (min max file
1255                                           &optional script excluded-script)
1256   (let ((coding-system-for-write char-db-file-coding-system))
1257     (with-temp-buffer
1258       (insert (format ";; -*- coding: %s -*-\n"
1259                       char-db-file-coding-system))
1260       (insert-char-range-data min max script excluded-script)
1261       (write-region (point-min)(point-max) file))))
1262
1263 (defvar what-character-original-window-configuration)
1264
1265 ;;;###autoload
1266 (defun what-char-definition (char)
1267   (interactive (list (char-after)))
1268   (let ((buf (get-buffer-create "*Character Description*"))
1269         (the-buf (current-buffer))
1270         (win-conf (current-window-configuration)))
1271     (pop-to-buffer buf)
1272     (make-local-variable 'what-character-original-window-configuration)
1273     (setq what-character-original-window-configuration win-conf)
1274     (setq buffer-read-only nil)
1275     (erase-buffer)
1276     (condition-case err
1277         (progn
1278           (insert-char-data-with-variant char 'printable)
1279           (unless (char-attribute-alist char)
1280             (insert (format ";; = %c\n"
1281                             (let* ((rest (split-char char))
1282                                    (ccs (pop rest))
1283                                    (code (pop rest)))
1284                               (while rest
1285                                 (setq code (logior (lsh code 8)
1286                                                    (pop rest))))
1287                               (decode-char ccs code)))))
1288           ;; (char-db-update-comment)
1289           (set-buffer-modified-p nil)
1290           (view-mode the-buf (lambda (buf)
1291                                (set-window-configuration
1292                                 what-character-original-window-configuration)
1293                                ))
1294           (goto-char (point-min)))
1295       (error (progn
1296                (set-window-configuration
1297                 what-character-original-window-configuration)
1298                (signal (car err) (cdr err)))))))
1299
1300 (provide 'char-db-util)
1301
1302 ;;; char-db-util.el ends here