(char-db-insert-ccs-feature): Format `===hng-jyu' as decimal.
[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,2007,
4 ;;   2008,2009,2010,2011,2012,2013,2014,2015 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 'chise-subr)
29 (require 'ideograph-subr)
30
31 (defconst unidata-normative-category-alist
32   '(("Lu" letter        uppercase)
33     ("Ll" letter        lowercase)
34     ("Lt" letter        titlecase)
35     ("Mn" mark          non-spacing)
36     ("Mc" mark          spacing-combining)
37     ("Me" mark          enclosing)
38     ("Nd" number        decimal-digit)
39     ("Nl" number        letter)
40     ("No" number        other)
41     ("Zs" separator     space)
42     ("Zl" separator     line)
43     ("Zp" separator     paragraph)
44     ("Cc" other         control)
45     ("Cf" other         format)
46     ("Cs" other         surrogate)
47     ("Co" other         private-use)
48     ("Cn" other         not-assigned)))
49
50 (defconst unidata-informative-category-alist
51   '(("Lm" letter        modifier)
52     ("Lo" letter        other)
53     ("Pc" punctuation   connector)
54     ("Pd" punctuation   dash)
55     ("Ps" punctuation   open)
56     ("Pe" punctuation   close)
57     ("Pi" punctuation   initial-quote)
58     ("Pf" punctuation   final-quote)
59     ("Po" punctuation   other)
60     ("Sm" symbol        math)
61     ("Sc" symbol        currency)
62     ("Sk" symbol        modifier)
63     ("So" symbol        other)
64     ))
65
66 (defconst shuowen-radicals
67   [?一 ?上 ?示 ?三 ?王 ?玉 ?玨 ?气 ?士 ?丨 ; 010
68    ?屮 ?艸 ?蓐 ?茻 ?小 ?八 ?釆 ?半 ?牛 ?犛 ; 020
69    ?告 ?口 ?凵 ?吅 ?哭 ?走 ?止 ?癶 ?步 ?此 ; 030
70    ?正 ?是 ?辵 ?彳 ?廴 ?㢟 ?行 ?齒 ?牙 ?足 ; 040
71    ?疋 ?品 ?龠 ?冊 ?㗊 ?舌 ?干 ?谷 ?只 ?㕯 ; 050
72    ?句 ?丩 ?古 ?十 ?卅 ?言 ?誩 ?音 ?䇂 ?丵 ; 060
73    ?菐 ?𠬞 ?𠬜 ?共 ?異 ?舁 ?𦥑 ?䢅 ?爨 ?革 ; 070
74    ?鬲 ?䰜 ?爪 ?𠃨 ?鬥 ?又 ?𠂇 ?㕜 ?支 ?𦘒 ; 080
75    ?聿 ?畫 ?隶 ?臤 ?臣 ?殳 ?殺 ?𠘧 ?寸 ?皮 ; 090
76    ?㼱 ?攴 ?敎 ?卜 ?用 ?爻 ?㸚 ?𥄎 ?目 ?䀠 ; 100
77    ?眉 ?盾 ?自 ?白 ?鼻 ?皕 ?習 ?羽 ?隹 ?奞 ; 110
78    ?萑 ?𦫳 ?苜 ?羊 ?羴 ?瞿 ?雔 ?雥 ?鳥 ?烏 ; 120
79    ?𠦒 ?冓 ?幺 ?𢆶 ?叀 ?玄 ?予 ?放 ?𠬪 ?𣦼 ; 130
80    ?歺 ?死 ?冎 ?骨 ?肉 ?筋 ?刀 ?刃 ?㓞 ?丰 ; 140
81    ?耒 ?𧢲 ?竹 ?箕 ?丌 ?左 ?工 ?㠭 ?巫 ?甘 ; 150
82    ?曰 ?乃 ?丂 ?可 ?兮 ?号 ?亏 ?旨 ?喜 ?壴 ; 160
83 ;  ?旨 ?曰 ?乃 ?丂 ?可 ?兮 ?号 ?亏 ?喜 ?壴 ; 160
84    ?鼓 ?豈 ?豆 ?豊 ?豐 ?䖒 ?虍 ?虎 ?虤 ?皿 ; 170
85    ?𠙴 ?去 ?血 ?丶 ?丹 ?青 ?井 ?皀 ?鬯 ?食 ; 180
86    ?亼 ?會 ?倉 ?入 ?缶 ?矢 ?高 ?冂 ?𩫏 ?京 ; 190
87    ?亯 ?𣆪 ?畗 ?㐭 ?嗇 ?來 ?麥 ?夊 ?舛 ?䑞 ; 200
88    ?韋 ?弟 ?夂 ?久 ?桀 ?木 ?東 ?林 ?才 ?叒 ; 210
89    ?之 ?帀 ?出 ?𣎵 ?生 ?乇 ?𠂹 ?𠌶 ?華 ?𥝌 ; 220
90    ?稽 ?巢 ?桼 ?束 ?㯻 ?囗 ?員 ?貝 ?邑 ?𨛜 ; 230
91    ?日 ?旦 ?倝 ?㫃 ?冥 ?晶 ?月 ?有 ?明 ?囧 ; 240
92    ?夕 ?多 ?毌 ?𢎘 ?𣐺 ?卣 ?齊 ?朿 ?片 ?鼎 ; 250
93    ?克 ?彔 ?禾 ?秝 ?黍 ?香 ?米 ?毇 ?臼 ?凶 ; 260
94    ?𣎳 ?林 ?麻 ?尗 ?耑 ?韭 ?瓜 ?瓠 ?宀 ?宮 ; 270
95    ?呂 ?穴 ?㝱 ?𤕫 ?冖 ?𠔼 ?冃 ?㒳 ?网 ?襾 ; 280
96    ?巾 ?巿 ?帛 ?白 ?㡀 ?黹 ?人 ?𠤎 ?匕 ?从 ; 290
97    ?比 ?北 ?丘 ?㐺 ?𡈼 ?重 ?臥 ?身 ?㐆 ?衣 ; 300
98    ?裘 ?老 ?毛 ?毳 ?尸 ?尺 ?尾 ?履 ?舟 ?方 ; 310
99    ?儿 ?兄 ?兂 ?皃 ?𠑹 ?先 ?秃 ?見 ?覞 ?欠 ; 320
100    ?㱃 ?㳄 ?旡 ?頁 ?𦣻 ?面 ?丏 ?首 ?𥄉 ?須 ; 330
101    ?彡 ?彣 ?文 ?髟 ?后 ?司 ?卮 ?卩 ?印 ?色 ; 340
102    ?𠨍 ?辟 ?勹 ?包 ?茍 ?鬼 ?甶 ?厶 ?嵬 ?山 ; 350
103    ?屾 ?屵 ?广 ?厂 ?丸 ?危 ?石 ?長 ?勿 ?冄 ; 360
104    ?而 ?豕 ?㣇 ?彑 ?豚 ?豸 ?𤉡 ?易 ?象 ?馬 ; 370
105    ?𢊁 ?鹿 ?麤 ?㲋 ?兔 ?萈 ?犬 ?㹜 ?鼠 ?能 ; 380
106    ?熊 ?火 ?炎 ?黑 ?囪 ?焱 ?炙 ?赤 ?大 ?亦 ; 390
107    ])
108
109 (defun shuowen-radical (number)
110   (aref shuowen-radicals (1- number)))
111
112 (defvar char-db-file-coding-system 'utf-8-mcs-er)
113
114 (defvar char-db-ignored-attributes '(ideographic-products))
115
116 (defvar char-db-coded-charset-priority-list
117   '(ascii
118     control-1
119     latin-iso8859-1
120     latin-iso8859-2
121     latin-iso8859-3
122     latin-iso8859-4
123     latin-iso8859-9
124     latin-jisx0201
125     cyrillic-iso8859-5
126     greek-iso8859-7
127     thai-tis620
128     ;; =mj
129     ;; =adobe-japan1-0
130     ;; =adobe-japan1-1
131     ;; =adobe-japan1-2
132     ;; =adobe-japan1-3
133     ;; =adobe-japan1-4
134     ;; =adobe-japan1-5
135     ;; =adobe-japan1-6
136     =jis-x0208
137     =jis-x0208@1978
138     =jis-x0208@1983
139     japanese-jisx0212
140     chinese-gb2312
141     =jis-x0208@1990
142     chinese-cns11643-1
143     chinese-cns11643-2
144     chinese-cns11643-3
145     chinese-cns11643-4
146     chinese-cns11643-5
147     chinese-cns11643-6
148     chinese-cns11643-7
149     =jis-x0213-1
150     =jis-x0213-1@2000
151     =jis-x0213-1@2004
152     =jis-x0213-2
153     korean-ksc5601
154     chinese-isoir165
155     katakana-jisx0201
156     hebrew-iso8859-8
157     chinese-gb12345
158     latin-viscii
159     ethiopic-ucs
160     =big5-cdp
161     =hanyo-denshi/ja
162     =hanyo-denshi/jb
163     =hanyo-denshi/jc
164     =hanyo-denshi/jd
165     =hanyo-denshi/ft
166     =hanyo-denshi/ia
167     =hanyo-denshi/ib
168     =hanyo-denshi/hg
169     =hanyo-denshi/jt
170     =hanyo-denshi/ks
171     ;; ==mj
172     ;; ==adobe-japan1-0
173     ;; ==adobe-japan1-1
174     ;; ==adobe-japan1-2
175     ;; ==adobe-japan1-3
176     ;; ==adobe-japan1-4
177     ;; ==adobe-japan1-5
178     ;; ==adobe-japan1-6
179     ==jis-x0208
180     ==jis-x0213-1
181     ==jis-x0213-2
182     ==hanyo-denshi/ja
183     ==hanyo-denshi/jb
184     ==hanyo-denshi/jc
185     ==hanyo-denshi/ft
186     ==hanyo-denshi/ib
187     ==hanyo-denshi/hg
188     ==hanyo-denshi/jt
189     ==hanyo-denshi/ks
190     =gt
191     =gt-k
192     =daikanwa
193     =daikanwa@rev2
194     =daikanwa@rev1
195     =daikanwa/+p
196     ==daikanwa
197     =cbeta
198     ideograph-hanziku-1
199     ideograph-hanziku-2
200     ideograph-hanziku-3
201     ideograph-hanziku-4
202     ideograph-hanziku-5
203     ideograph-hanziku-6
204     ideograph-hanziku-7
205     ideograph-hanziku-8
206     ideograph-hanziku-9
207     ideograph-hanziku-10
208     ideograph-hanziku-11
209     ideograph-hanziku-12
210     ;; =>>>adobe-japan1-0
211     ;; =>>>adobe-japan1-1
212     ;; =>>>adobe-japan1-2
213     ;; =>>>adobe-japan1-3
214     ;; =>>>adobe-japan1-4
215     ;; =>>>adobe-japan1-5
216     ;; =>>>adobe-japan1-6
217     ;; =>>>jis-x0208
218     ;; =>>>jis-x0213-1
219     ;; =>>>jis-x0213-2
220     ;; =>>>hanyo-denshi/ja
221     ;; =>>>hanyo-denshi/jb
222     ;; =>>>hanyo-denshi/jc
223     ;; =>>>hanyo-denshi/ft
224     ;; =>>>hanyo-denshi/ib
225     ;; =>>>hanyo-denshi/hg
226     ;; =>>>hanyo-denshi/jt
227     ;; =>>>hanyo-denshi/ks
228     ;; =>>>gt
229     =>>adobe-japan1-0
230     =>>adobe-japan1-1
231     =>>adobe-japan1-2
232     =>>adobe-japan1-3
233     =>>adobe-japan1-4
234     =>>adobe-japan1-5
235     =>>adobe-japan1-6
236     =>>jis-x0208
237     =>>jis-x0213-1
238     =>>jis-x0213-1@2000
239     =>>jis-x0213-1@2004
240     =>>jis-x0213-2
241     =>>jis-x0208@1978
242     =>>hanyo-denshi/ft
243     =>>hanyo-denshi/jt
244     =>>hanyo-denshi/ks
245     =>>gt
246     =>>daikanwa
247     =>>cbeta
248     =+>jis-x0208
249     =+>jis-x0213-1
250     =+>jis-x0213-2
251     =+>adobe-japan1-0
252     =+>adobe-japan1-1
253     =+>adobe-japan1-2
254     =+>adobe-japan1-3
255     =+>adobe-japan1-4
256     =+>adobe-japan1-5
257     =+>adobe-japan1-6
258     =+>jis-x0208@1978
259     =>jis-x0208
260     =>jis-x0208@1997
261     =>jis-x0213-1
262     =>jis-x0213-1@2000
263     =>jis-x0213-1@2004
264     =>jis-x0213-2
265     ==>ucs@bucs
266     =>iwds-1
267     =>ucs@hanyo-denshi
268     =>ucs@iso
269     =>ucs@unicode
270     =>ucs@jis
271     =>ucs@cns
272     =>ucs@ks
273     =+>ucs@iso
274     =+>ucs@unicode
275     =+>ucs@jis
276     =+>ucs@jis/1990
277     =+>ucs@cns
278     =+>ucs@ks
279     =>>ucs@iso
280     =>>ucs@unicode
281     =>>ucs@jis
282     =>>ucs@cns
283     =>>>ucs@iso
284     =>>>ucs@unicode
285     ==ucs@iso
286     ==ucs@unicode
287     ;; ==ucs@cns
288     ==gb2312
289     ==ks-x1001
290     ==cns11643-1
291     ==cns11643-2
292     ==cns11643-3
293     ==cns11643-4
294     ==cns11643-5
295     ==cns11643-6
296     ==cns11643-7
297     ==gt
298     ==jis-x0208@1990
299     ;; ==jis-x0208@1983
300     ==jis-x0208@1978
301     ==gt-k
302     =ucs@iso
303     =ucs@unicode
304     =ucs@cns
305     ==big5-cdp
306     ==cbeta
307     =>>big5-cdp
308     =>>gt-k
309     =+>gt
310     =>gt
311     =>big5-cdp
312     =>daikanwa
313     =>daikanwa/ho
314     =>cns11643-5
315     =>cns11643-7
316     =big5
317     =big5-eten
318     =>gt-k
319     =zinbun-oracle
320     =>zinbun-oracle
321     =ruimoku-v6
322     =>>ruimoku-v6
323     ==ruimoku-v6
324     =jef-china3
325     =>cbeta
326     =shinjigen
327     =big5-cdp-var-3
328     =big5-cdp-var-5
329     =>ucs@iwds-1))
330
331
332 ;;; @ char-db formatters
333 ;;;
334
335 (defun char-db-make-char-spec (char)
336   (let (ret char-spec)
337     (cond ((characterp char)
338            (cond ((and (setq ret (encode-char char '=ucs 'defined-only))
339                        (not (and (<= #xE000 ret)(<= ret #xF8FF))))
340                   (setq char-spec (list (cons '=ucs ret)))
341                   (cond ((setq ret (get-char-attribute char 'name))
342                          (setq char-spec (cons (cons 'name ret) char-spec))
343                          )
344                         ((setq ret (get-char-attribute char 'name*))
345                          (setq char-spec (cons (cons 'name* ret) char-spec))
346                          ))
347                   )
348                  ((encode-char char '=mj 'defined-only)
349                   (setq char-spec nil)
350                   (dolist (ccs (charset-list))
351                     (if (and (or (eq ccs '=mj)
352                                  ;; (eq (charset-property ccs 'iso-ir) 177)
353                                  (string-match "=ucs@" (symbol-name ccs))
354                                  )
355                              (setq ccs (charset-name ccs))
356                              (null (assq ccs char-spec))
357                              (setq ret (encode-char char ccs 'defined-only)))
358                         (setq char-spec (cons (cons ccs ret) char-spec))))
359                   )
360                  ((encode-char char '==mj 'defined-only)
361                   (setq char-spec nil)
362                   (dolist (ccs (charset-list))
363                     (if (and (or (eq ccs '==mj)
364                                  ;; (eq (charset-property ccs 'iso-ir) 177)
365                                  (string-match "=ucs@" (symbol-name ccs))
366                                  )
367                              (setq ccs (charset-name ccs))
368                              (null (assq ccs char-spec))
369                              (setq ret (encode-char char ccs 'defined-only)))
370                         (setq char-spec (cons (cons ccs ret) char-spec))))
371                   )
372                  ((encode-char char '=adobe-japan1 'defined-only)
373                   (setq char-spec nil)
374                   (dolist (ccs (charset-list))
375                     (if (and (or (memq ccs
376                                        '(=adobe-japan1-0
377                                          =adobe-japan1-1
378                                          =adobe-japan1-2
379                                          =adobe-japan1-3
380                                          =adobe-japan1-4
381                                          =adobe-japan1-5
382                                          =adobe-japan1-6
383                                          ))
384                                  ;; (eq (charset-property ccs 'iso-ir) 177)
385                                  (string-match "=ucs@" (symbol-name ccs))
386                                  )
387                              (setq ccs (charset-name ccs))
388                              (null (assq ccs char-spec))
389                              (setq ret (encode-char char ccs 'defined-only)))
390                         (setq char-spec (cons (cons ccs ret) char-spec))))
391                   )
392                  ((encode-char char '==adobe-japan1 'defined-only)
393                   (setq char-spec nil)
394                   (dolist (ccs (charset-list))
395                     (if (and (or (memq ccs
396                                        '(==adobe-japan1-0
397                                          ==adobe-japan1-1
398                                          ==adobe-japan1-2
399                                          ==adobe-japan1-3
400                                          ==adobe-japan1-4
401                                          ==adobe-japan1-5
402                                          ==adobe-japan1-6
403                                          ))
404                                  ;; (eq (charset-property ccs 'iso-ir) 177)
405                                  (string-match "=ucs@" (symbol-name ccs))
406                                  )
407                              (setq ccs (charset-name ccs))
408                              (null (assq ccs char-spec))
409                              (setq ret (encode-char char ccs 'defined-only)))
410                         (setq char-spec (cons (cons ccs ret) char-spec))))
411                   )
412                  ((setq ret
413                         (catch 'tag
414                           (let ((rest char-db-coded-charset-priority-list)
415                                 ccs)
416                             (while rest
417                               (setq ccs (charset-name
418                                          (find-charset (car rest))))
419                               (if (setq ret
420                                         (encode-char char ccs
421                                                      'defined-only))
422                                   (throw 'tag (cons ccs ret)))
423                               (setq rest (cdr rest))))))
424                   (setq char-spec (list ret))
425                   (dolist (ccs (delq (car ret) (charset-list)))
426                     (if (and (or (charset-iso-final-char ccs)
427                                  (memq ccs
428                                        '(=daikanwa
429                                          =daikanwa@rev2
430                                          ;; =gt-k
431                                          =jis-x0208@1997
432                                          ))
433                                  (eq (charset-property ccs 'iso-ir) 177)
434                                  ;; (string-match "=ucs@" (symbol-name ccs))
435                                  )
436                              (setq ccs (charset-name ccs))
437                              (null (assq ccs char-spec))
438                              (setq ret (encode-char char ccs 'defined-only)))
439                         (setq char-spec (cons (cons ccs ret) char-spec))))
440                   (if (null char-spec)
441                       (setq char-spec (split-char char)))
442                   (cond ((setq ret (get-char-attribute char 'name))
443                          (setq char-spec (cons (cons 'name ret) char-spec))
444                          )
445                         ((setq ret (get-char-attribute char 'name*))
446                          (setq char-spec (cons (cons 'name* ret) char-spec))
447                          ))
448                   )
449                  ((setq ret (get-char-attribute
450                              char 'ideographic-combination))
451                   (setq char-spec
452                         (cons (cons 'ideographic-combination ret)
453                               char-spec))
454                   ))
455            char-spec)
456           ((consp char)
457            char))))
458     
459 (defun char-db-insert-char-spec (char &optional readable column
460                                       required-features)
461   (unless column
462     (setq column (current-column)))
463   (let (char-spec temp-char)
464     (setq char-spec (char-db-make-char-spec char))
465     (unless (or (characterp char) ; char
466                 (condition-case nil
467                     (setq char (find-char char-spec))
468                   (error nil)))
469       ;; define temporary character
470       ;;   Current implementation is dirty.
471       (setq temp-char (define-char (cons '(ideograph-daikanwa . 0)
472                                          char-spec)))
473       (remove-char-attribute temp-char 'ideograph-daikanwa)
474       (setq char temp-char))
475     (insert-char-attributes char
476                             readable
477                             (union (mapcar #'car char-spec)
478                                    required-features)
479                             nil 'for-sub-node)
480     (when temp-char
481       ;; undefine temporary character
482       ;;   Current implementation is dirty.
483       (setq char-spec (char-attribute-alist temp-char))
484       (while char-spec
485         (remove-char-attribute temp-char (car (car char-spec)))
486         (setq char-spec (cdr char-spec))))))
487
488 (defun char-db-insert-alist (alist &optional readable column)
489   (unless column
490     (setq column (current-column)))
491   (let ((line-breaking
492          (concat "\n" (make-string (1+ column) ?\ )))
493         name value
494         ret al ; cal
495         key
496         lbs cell rest separator)
497     (insert "(")
498     (while alist
499       (setq name (car (car alist))
500             value (cdr (car alist)))
501       (cond ((eq name 'char)
502              (insert "(char . ")
503              (if (setq ret (condition-case nil
504                                (find-char value)
505                              (error nil)))
506                  (progn
507                    (setq al nil
508                          ;; cal nil
509                          )
510                    (while value
511                      (setq key (car (car value)))
512                      ;; (if (find-charset key)
513                      ;;     (setq cal (cons key cal))
514                      (setq al (cons key al))
515                      ;; )
516                      (setq value (cdr value)))
517                    (insert-char-attributes ret
518                                            readable
519                                            (or al 'none) ; cal
520                                            nil 'for-sub-node))
521                (insert (prin1-to-string value)))
522              (insert ")")
523              (insert line-breaking))
524             ((consp value)
525              (insert (format "(%-18s " name))
526              (setq lbs (concat "\n" (make-string (current-column) ?\ )))
527              (while (consp value)
528                (setq cell (car value))
529                (if (and (consp cell)
530                         (consp (car cell))
531                         (setq ret (condition-case nil
532                                       (find-char cell)
533                                     (error nil)))
534                         )
535                    (progn
536                      (setq rest cell
537                            al nil
538                            ;; cal nil
539                            )
540                      (while rest
541                        (setq key (car (car rest)))
542                        ;; (if (find-charset key)
543                        ;;     (setq cal (cons key cal))
544                        (setq al (cons key al))
545                        ;; )
546                        (setq rest (cdr rest)))
547                      (if separator
548                          (insert lbs))
549                      (insert-char-attributes ret
550                                              readable
551                                              al ; cal
552                                              nil 'for-sub-node)
553                      (setq separator lbs))
554                  (if separator
555                      (insert separator))
556                  (insert (prin1-to-string cell))
557                  (setq separator " "))
558                (setq value (cdr value)))
559              (insert ")")
560              (insert line-breaking))
561             (t
562              (insert (format "(%-18s . %S)%s"
563                              name value
564                              line-breaking))))
565       (setq alist (cdr alist))))
566   (insert ")"))
567
568 (defun char-db-insert-char-reference (plist &optional readable column)
569   (unless column
570     (setq column (current-column)))
571   (let ((line-breaking
572          (concat "\n" (make-string (1+ column) ?\ )))
573         (separator "")
574         name value)
575     (insert "(")
576     (while plist
577       (setq name (pop plist))
578       (setq value (pop plist))
579       (cond ((eq name :char)
580              (insert separator)
581              (insert ":char\t")
582              (cond ((numberp value)
583                     (setq value (decode-char '=ucs value)))
584                    ;; ((consp value)
585                    ;;  (setq value (or (find-char value)
586                    ;;                  value)))
587                    )
588              (char-db-insert-char-spec value readable)
589              (insert line-breaking)
590              (setq separator ""))
591             ((eq name :radical)
592              (insert (format "%s%s\t%d ; %c%s"
593                              separator
594                              name value
595                              (ideographic-radical value)
596                              line-breaking))
597              (setq separator ""))
598             (t
599              (insert (format "%s%s\t%S" separator name value))
600              (setq separator line-breaking)))
601       ))
602   (insert ")"))
603
604 (defun char-db-decode-isolated-char (ccs code-point)
605   (let (ret)
606     (setq ret
607           (cond ((eq ccs 'arabic-iso8859-6)
608                  (decode-char ccs code-point))
609                 ;; ((eq ccs '=gt)
610                 ;;  (decode-builtin-char '==gt code-point))
611                 ((and (memq ccs '(=gt-pj-1
612                                   =gt-pj-2
613                                   =gt-pj-3
614                                   =gt-pj-4
615                                   =gt-pj-5
616                                   =gt-pj-6
617                                   =gt-pj-7
618                                   =gt-pj-8
619                                   =gt-pj-9
620                                   =gt-pj-10
621                                   =gt-pj-11))
622                       (setq ret (decode-char ccs code-point))
623                       (setq ret (encode-char ret '=gt 'defined-only)))
624                  (decode-builtin-char '=gt ret))
625                 (t
626                  (decode-builtin-char ccs code-point))))
627     (cond ((null ret)
628            (or (decode-char ccs code-point)
629                (define-char (list (cons ccs code-point)))))
630           ((and (<= 0 (char-int ret))
631                 (<= (char-int ret) #x1F))
632            (decode-char '=ucs (+ #x2400 (char-int ret))))
633           ((= (char-int ret) #x7F)
634            ?\u2421)
635           (t ret))))
636
637 (defvar char-db-convert-obsolete-format t)
638
639 (defun char-db-insert-ccs-feature (name value line-breaking)
640   (cond
641    ((integerp value)
642     (insert
643      (format
644       (cond
645        ((memq name '(=>iwds-1
646                      ==shinjigen
647                      =shinjigen
648                      =shinjigen@1ed ==shinjigen@1ed
649                      =shinjigen@rev ==shinjigen@rev
650                      =shinjigen/+p@rev ==shinjigen/+p@rev
651                      ===daikanwa/ho ==daikanwa/ho
652                      =daikanwa/ho =>>daikanwa/ho =>daikanwa/ho))
653         "(%-18s .  %04d)\t; %c")
654        ((eq name '=shinjigen@1ed/24pr)
655         "(%-18s . %04d)\t; %c")
656        ((or (memq name '(===daikanwa
657                          ==daikanwa =daikanwa =>>daikanwa =>daikanwa
658                          =daikanwa@rev1 =daikanwa@rev2
659                          =daikanwa/+p ==daikanwa/+p ===daikanwa/+p
660                          =>>daikanwa/+p
661                          =daikanwa/+2p =>>daikanwa/+2p
662                          =gt ==gt ===gt
663                          =>>gt =+>gt =>gt
664                          =gt-k ==gt-k ===gt-k
665                          =>>gt-k =>gt-k
666                          =adobe-japan1-0 ==adobe-japan1-0 ===adobe-japan1-0
667                          =adobe-japan1-1 ==adobe-japan1-1 ===adobe-japan1-1
668                          =adobe-japan1-2 ==adobe-japan1-2 ===adobe-japan1-2
669                          =adobe-japan1-3 ==adobe-japan1-3 ===adobe-japan1-3
670                          =adobe-japan1-4 ==adobe-japan1-4 ===adobe-japan1-4
671                          =adobe-japan1-5 ==adobe-japan1-5 ===adobe-japan1-5
672                          =adobe-japan1-6 ==adobe-japan1-6 ===adobe-japan1-6
673                          =>>adobe-japan1-0 =+>adobe-japan1-0
674                          =>>adobe-japan1-1 =+>adobe-japan1-1
675                          =>>adobe-japan1-2 =+>adobe-japan1-2
676                          =>>adobe-japan1-3 =+>adobe-japan1-3
677                          =>>adobe-japan1-4 =+>adobe-japan1-4
678                          =>>adobe-japan1-5 =+>adobe-japan1-5
679                          =>>adobe-japan1-6 =+>adobe-japan1-6
680                          =>cbeta =cbeta =>>cbeta ==cbeta ===cbeta
681                          =zinbun-oracle =>zinbun-oracle
682                          ===hng-jou ===hng-keg ===hng-dng ===hng-mam
683                          ===hng-drt ===hng-kgk ===hng-myz ===hng-kda
684                          ===hng-khi ===hng-khm ===hng-sok
685                          ===hng-yhk ===hng-kak
686                          ===hng-sys ===hng-tsu
687                          ===hng-hod ===hng-gok ===hng-nto
688                          ===hng-nkm ===hng-k24
689                          ===hng-kcj ===hng-kbk ===hng-sik
690                          ===hng-skk ===hng-kyu ===hng-ksk ===hng-wan
691                          ===hng-okd ===hng-kmi
692                          ===hng-zkd ===hng-jyu
693                          ===hng-kss ===hng-kyo
694                          ===hng-smk))
695             ;; (string-match "^=adobe-" (symbol-name name))
696             )
697         "(%-18s . %05d)\t; %c")
698        ((memq name '(=hanyo-denshi/ks
699                      ==hanyo-denshi/ks ===hanyo-denshi/ks
700                      =>>hanyo-denshi/ks
701                      =koseki
702                      =mj ==mj ===mj =>>mj
703                      =zihai mojikyo))
704         "(%-18s . %06d)\t; %c")
705        ((>= (charset-dimension name) 2)
706         "(%-18s . #x%04X)\t; %c")
707        (t
708         "(%-18s . #x%02X)\t; %c"))
709       name
710       (if (= (charset-iso-graphic-plane name) 1)
711           (logior value
712                   (cond ((= (charset-dimension name) 1)
713                          #x80)
714                         ((= (charset-dimension name) 2)
715                          #x8080)
716                         ((= (charset-dimension name) 3)
717                          #x808080)
718                         (t 0)))
719         value)
720       (char-db-decode-isolated-char name value)))
721     (if (and (= (charset-chars name) 94)
722              (= (charset-dimension name) 2))
723         (insert (format " [%02d-%02d]"
724                         (- (lsh value -8) 32)
725                         (- (logand value 255) 32))))
726     )
727    (t
728     (insert (format "(%-18s . %s)" name value))
729     ))
730   (insert line-breaking))
731
732 (defun char-db-insert-relation-feature (char name value line-breaking
733                                              ccss readable)
734   (insert (format "(%-18s%s " name line-breaking))
735   (let ((lbs (concat "\n" (make-string (current-column) ?\ )))
736         separator cell sources required-features
737         ret)
738     (while (consp value)
739       (setq cell (car value))
740       (if (integerp cell)
741           (setq cell (decode-char '=ucs cell)))
742       (cond
743        ((eq name '->subsumptive)
744         (when (or (not (some (lambda (atr)
745                                (get-char-attribute cell atr))
746                              char-db-ignored-attributes))
747                   (some (lambda (ccs)
748                           (encode-char cell ccs 'defined-only))
749                         ccss))
750           (if separator
751               (insert lbs))
752           (let ((char-db-ignored-attributes
753                  (cons '<-subsumptive
754                        char-db-ignored-attributes)))
755             (insert-char-attributes cell readable nil nil 'for-sub-node))
756           (setq separator lbs))
757         )
758        ((characterp cell)
759         (setq sources
760               (get-char-attribute
761                char (intern (format "%s*sources" name))))
762         (setq required-features nil)
763         (dolist (source sources)
764           (cond
765            ((memq source '(JP
766                            JP/Jouyou
767                            shinjigen shinjigen@1ed shinjigen@rev))
768             (setq required-features
769                   (union required-features
770                          '(=jis-x0208
771                            =jis-x0208@1990
772                            =jis-x0213-1@2000
773                            =jis-x0213-1@2004
774                            =jis-x0213-2
775                            =jis-x0212
776                            =jis-x0208@1983
777                            =jis-x0208@1978
778                            =shinjigen
779                            =shinjigen@1ed
780                            =shinjigen@rev
781                            =shinjigen/+p@rev))))
782            ((eq source 'CN)
783             (setq required-features
784                   (union required-features
785                          '(=gb2312
786                            =gb12345
787                            =iso-ir165)))))
788           (cond
789            ((find-charset (setq ret (intern (format "=%s" source))))
790             (setq required-features
791                   (cons ret required-features)))
792            (t (setq required-features
793                     (cons source required-features)))))
794         (cond ((string-match "@JP" (symbol-name name))
795                (setq required-features
796                      (union required-features
797                             '(=jis-x0208
798                               =jis-x0208@1990
799                               =jis-x0213-1-2000
800                               =jis-x0213-2-2000
801                               =jis-x0212
802                               =jis-x0208@1983
803                               =jis-x0208@1978))))
804               ((string-match "@CN" (symbol-name name))
805                (setq required-features
806                      (union required-features
807                             '(=gb2312
808                               =gb12345
809                               =iso-ir165)))))
810         (if separator
811             (insert lbs))
812         (if readable
813             (insert (format "%S" cell))
814           (char-db-insert-char-spec cell readable
815                                     nil
816                                     required-features))
817         (setq separator lbs))
818        ((consp cell)
819         (if separator
820             (insert lbs))
821         (if (consp (car cell))
822             (char-db-insert-char-spec cell readable)
823           (char-db-insert-char-reference cell readable))
824         (setq separator lbs))
825        (t
826         (if separator
827             (insert separator))
828         (insert (prin1-to-string cell))
829         (setq separator " ")))
830       (setq value (cdr value)))
831     (insert ")")
832     (insert line-breaking)))
833
834 (defun insert-char-attributes (char &optional readable attributes column
835                                     for-sub-node)
836   (unless column
837     (setq column (current-column)))
838   (let (name value ; has-long-ccs-name
839         rest
840         radical strokes
841         (line-breaking
842          (concat "\n" (make-string (1+ column) ?\ )))
843         lbs cell separator ret
844         key al cal
845         dest-ccss ; sources required-features
846         ccss)
847     (let (atr-d)
848       (setq attributes
849             (sort (if attributes
850                       (if (consp attributes)
851                           (progn
852                             (dolist (name attributes)
853                               (unless (memq name char-db-ignored-attributes)
854                                 (if (find-charset name)
855                                     (push name ccss))
856                                 (push name atr-d)))
857                             atr-d))
858                     (dolist (name (char-attribute-list))
859                       (unless (memq name char-db-ignored-attributes)
860                         (if (find-charset name)
861                             (push name ccss))
862                         (push name atr-d)))
863                     atr-d)
864                   #'char-attribute-name<)))
865     (insert "(")
866     (when (memq '<-subsumptive attributes)
867       (when (or readable (not for-sub-node))
868         (when (setq value (get-char-attribute char '<-subsumptive))
869           (char-db-insert-relation-feature char '<-subsumptive value
870                                            line-breaking
871                                            ccss readable)))
872       (setq attributes (delq '<-subsumptive attributes)))
873     (when (and (memq '<-denotational attributes)
874                (setq value (get-char-attribute char '<-denotational)))
875       (char-db-insert-relation-feature char '<-denotational value
876                                        line-breaking
877                                        ccss readable)
878       (setq attributes (delq '<-denotational attributes)))
879     (when (and (memq '<-denotational@component attributes)
880                (setq value (get-char-attribute char '<-denotational@component)))
881       (char-db-insert-relation-feature char '<-denotational@component value
882                                        line-breaking
883                                        ccss readable)
884       (setq attributes (delq '<-denotational@component attributes)))
885     (when (and (memq 'name attributes)
886                (setq value (get-char-attribute char 'name)))
887       (insert (format
888                (if (> (+ (current-column) (length value)) 48)
889                    "(name . %S)%s"
890                  "(name               . %S)%s")
891                value line-breaking))
892       (setq attributes (delq 'name attributes))
893       )
894     (when (and (memq 'name* attributes)
895                (setq value (get-char-attribute char 'name*)))
896       (insert (format
897                (if (> (+ (current-column) (length value)) 48)
898                    "(name* . %S)%s"
899                  "(name*              . %S)%s")
900                value line-breaking))
901       (setq attributes (delq 'name* attributes))
902       )
903     (when (and (memq 'script attributes)
904                (setq value (get-char-attribute char 'script)))
905       (insert (format "(script\t\t%s)%s"
906                       (mapconcat (function prin1-to-string)
907                                  value " ")
908                       line-breaking))
909       (setq attributes (delq 'script attributes))
910       )
911     (dolist (name '(=>ucs =>ucs*))
912       (when (and (memq name attributes)
913                  (setq value (get-char-attribute char name)))
914         (insert (format "(%-18s . #x%04X)\t; %c%s"
915                         name value (decode-char '=ucs value)
916                         line-breaking))
917         (setq attributes (delq name attributes))))
918     (dolist (name '(=>ucs@gb =>ucs@big5))
919       (when (and (memq name attributes)
920                  (setq value (get-char-attribute char name)))
921         (insert (format "(%-18s . #x%04X)\t; %c%s"
922                         name value
923                         (decode-char (intern
924                                       (concat "="
925                                               (substring
926                                                (symbol-name name) 2)))
927                                      value)
928                         line-breaking))
929         (setq attributes (delq name attributes))
930         ))
931     ;; (dolist (name '(=>daikanwa))
932     ;;   (when (and (memq name attributes)
933     ;;              (setq value (get-char-attribute char name)))
934     ;;     (insert
935     ;;      (if (integerp value)
936     ;;          (format "(%-18s . %05d)\t; %c%s"
937     ;;                  name value (decode-char '=daikanwa value)
938     ;;                  line-breaking)
939     ;;        (format "(%-18s %s)\t; %c%s"
940     ;;                name
941     ;;                (mapconcat (function prin1-to-string)
942     ;;                           value " ")
943     ;;                (char-representative-of-daikanwa char)
944     ;;                line-breaking)))
945     ;;     (setq attributes (delq name attributes))))
946     (when (and (memq 'general-category attributes)
947                (setq value (get-char-attribute char 'general-category)))
948       (insert (format
949                "(general-category\t%s) ; %s%s"
950                (mapconcat (lambda (cell)
951                             (format "%S" cell))
952                           value " ")
953                (cond ((rassoc value unidata-normative-category-alist)
954                       "Normative Category")
955                      ((rassoc value unidata-informative-category-alist)
956                       "Informative Category")
957                      (t
958                       "Unknown Category"))
959                line-breaking))
960       (setq attributes (delq 'general-category attributes))
961       )
962     (when (and (memq 'bidi-category attributes)
963                (setq value (get-char-attribute char 'bidi-category)))
964       (insert (format "(bidi-category\t. %S)%s"
965                       value
966                       line-breaking))
967       (setq attributes (delq 'bidi-category attributes))
968       )
969     (unless (or (not (memq 'mirrored attributes))
970                 (eq (setq value (get-char-attribute char 'mirrored 'empty))
971                     'empty))
972       (insert (format "(mirrored\t\t. %S)%s"
973                       value
974                       line-breaking))
975       (setq attributes (delq 'mirrored attributes))
976       )
977     (cond
978      ((and (memq 'decimal-digit-value attributes)
979            (setq value (get-char-attribute char 'decimal-digit-value)))
980       (insert (format "(decimal-digit-value . %S)%s"
981                       value
982                       line-breaking))
983       (setq attributes (delq 'decimal-digit-value attributes))
984       (when (and (memq 'digit-value attributes)
985                  (setq value (get-char-attribute char 'digit-value)))
986         (insert (format "(digit-value\t . %S)%s"
987                         value
988                         line-breaking))
989         (setq attributes (delq 'digit-value attributes))
990         )
991       (when (and (memq 'numeric-value attributes)
992                  (setq value (get-char-attribute char 'numeric-value)))
993         (insert (format "(numeric-value\t . %S)%s"
994                         value
995                         line-breaking))
996         (setq attributes (delq 'numeric-value attributes))
997         )
998       )
999      (t
1000       (when (and (memq 'digit-value attributes)
1001                  (setq value (get-char-attribute char 'digit-value)))
1002         (insert (format "(digit-value\t. %S)%s"
1003                         value
1004                         line-breaking))
1005         (setq attributes (delq 'digit-value attributes))
1006         )
1007       (when (and (memq 'numeric-value attributes)
1008                  (setq value (get-char-attribute char 'numeric-value)))
1009         (insert (format "(numeric-value\t. %S)%s"
1010                         value
1011                         line-breaking))
1012         (setq attributes (delq 'numeric-value attributes))
1013         )))
1014     (when (and (memq 'iso-10646-comment attributes)
1015                (setq value (get-char-attribute char 'iso-10646-comment)))
1016       (insert (format "(iso-10646-comment\t. %S)%s"
1017                       value
1018                       line-breaking))
1019       (setq attributes (delq 'iso-10646-comment attributes))
1020       )
1021     (when (and (memq 'morohashi-daikanwa attributes)
1022                (setq value (get-char-attribute char 'morohashi-daikanwa)))
1023       (insert (format "(morohashi-daikanwa\t%s)%s"
1024                       (mapconcat (function prin1-to-string) value " ")
1025                       line-breaking))
1026       (setq attributes (delq 'morohashi-daikanwa attributes))
1027       )
1028     (setq radical nil
1029           strokes nil)
1030     (when (and (memq 'ideographic-radical attributes)
1031                (setq value (get-char-attribute char 'ideographic-radical)))
1032       (setq radical value)
1033       (insert (format "(ideographic-radical . %S)\t; %c%s"
1034                       radical
1035                       (ideographic-radical radical)
1036                       line-breaking))
1037       (setq attributes (delq 'ideographic-radical attributes))
1038       )
1039     (when (and (memq 'shuowen-radical attributes)
1040                (setq value (get-char-attribute char 'shuowen-radical)))
1041       (insert (format "(shuowen-radical\t. %S)\t; %c%s"
1042                       value
1043                       (shuowen-radical value)
1044                       line-breaking))
1045       (setq attributes (delq 'shuowen-radical attributes))
1046       )
1047     (let (key)
1048       (dolist (domain
1049                (append
1050                 char-db-feature-domains
1051                 (let (dest domain)
1052                   (dolist (feature (char-attribute-list))
1053                     (setq feature (symbol-name feature))
1054                     (when (string-match
1055                            "\\(radical\\|strokes\\)@\\([^@*]+\\)\\(\\*\\|$\\)"
1056                            feature)
1057                       (setq domain (intern (match-string 2 feature)))
1058                      (unless (memq domain dest)
1059                        (setq dest (cons domain dest)))))
1060                   (sort dest #'string<))))
1061         (setq key (intern (format "%s@%s" 'ideographic-radical domain)))
1062         (when (and (memq key attributes)
1063                    (setq value (get-char-attribute char key)))
1064           (setq radical value)
1065           (insert (format "(%s . %S)\t; %c%s"
1066                           key
1067                           radical
1068                           (ideographic-radical radical)
1069                           line-breaking))
1070           (setq attributes (delq key attributes))
1071           )
1072         (setq key (intern (format "%s@%s" 'ideographic-strokes domain)))
1073         (when (and (memq key attributes)
1074                    (setq value (get-char-attribute char key)))
1075           (setq strokes value)
1076           (insert (format "(%s . %S)%s"
1077                           key
1078                           strokes
1079                           line-breaking))
1080           (setq attributes (delq key attributes))
1081           )
1082         (setq key (intern (format "%s@%s" 'total-strokes domain)))
1083         (when (and (memq key attributes)
1084                    (setq value (get-char-attribute char key)))
1085           (insert (format "(%s       . %S)%s"
1086                           key
1087                           value
1088                           line-breaking))
1089           (setq attributes (delq key attributes))
1090           )
1091         (dolist (feature '(ideographic-radical
1092                            ideographic-strokes
1093                            total-strokes))
1094           (setq key (intern (format "%s@%s*sources" feature domain)))
1095           (when (and (memq key attributes)
1096                      (setq value (get-char-attribute char key)))
1097             (insert (format "(%s%s" key line-breaking))
1098             (dolist (cell value)
1099               (insert (format " %s" cell)))
1100             (insert ")")
1101             (insert line-breaking)
1102             (setq attributes (delq key attributes))
1103             ))
1104         ))
1105     (when (and (memq 'ideographic-strokes attributes)
1106                (setq value (get-char-attribute char 'ideographic-strokes)))
1107       (setq strokes value)
1108       (insert (format "(ideographic-strokes . %S)%s"
1109                       strokes
1110                       line-breaking))
1111       (setq attributes (delq 'ideographic-strokes attributes))
1112       )
1113     (when (and (memq 'kangxi-radical attributes)
1114                (setq value (get-char-attribute char 'kangxi-radical)))
1115       (unless (eq value radical)
1116         (insert (format "(kangxi-radical\t . %S)\t; %c%s"
1117                         value
1118                         (ideographic-radical value)
1119                         line-breaking))
1120         (or radical
1121             (setq radical value)))
1122       (setq attributes (delq 'kangxi-radical attributes))
1123       )
1124     (when (and (memq 'kangxi-strokes attributes)
1125                (setq value (get-char-attribute char 'kangxi-strokes)))
1126       (unless (eq value strokes)
1127         (insert (format "(kangxi-strokes\t . %S)%s"
1128                         value
1129                         line-breaking))
1130         (or strokes
1131             (setq strokes value)))
1132       (setq attributes (delq 'kangxi-strokes attributes))
1133       )
1134     (when (and (memq 'japanese-radical attributes)
1135                (setq value (get-char-attribute char 'japanese-radical)))
1136       (unless (eq value radical)
1137         (insert (format "(japanese-radical\t . %S)\t; %c%s"
1138                         value
1139                         (ideographic-radical value)
1140                         line-breaking))
1141         (or radical
1142             (setq radical value)))
1143       (setq attributes (delq 'japanese-radical attributes))
1144       )
1145     (when (and (memq 'japanese-strokes attributes)
1146                (setq value (get-char-attribute char 'japanese-strokes)))
1147       (unless (eq value strokes)
1148         (insert (format "(japanese-strokes\t . %S)%s"
1149                         value
1150                         line-breaking))
1151         (or strokes
1152             (setq strokes value)))
1153       (setq attributes (delq 'japanese-strokes attributes))
1154       )
1155     (when (and (memq 'cns-radical attributes)
1156                (setq value (get-char-attribute char 'cns-radical)))
1157       (insert (format "(cns-radical\t . %S)\t; %c%s"
1158                       value
1159                       (ideographic-radical value)
1160                       line-breaking))
1161       (setq attributes (delq 'cns-radical attributes))
1162       )
1163     (when (and (memq 'cns-strokes attributes)
1164                (setq value (get-char-attribute char 'cns-strokes)))
1165       (unless (eq value strokes)
1166         (insert (format "(cns-strokes\t . %S)%s"
1167                         value
1168                         line-breaking))
1169         (or strokes
1170             (setq strokes value)))
1171       (setq attributes (delq 'cns-strokes attributes))
1172       )
1173     ;; (when (and (memq 'shinjigen-1-radical attributes)
1174     ;;            (setq value (get-char-attribute char 'shinjigen-1-radical)))
1175     ;;   (unless (eq value radical)
1176     ;;     (insert (format "(shinjigen-1-radical . %S)\t; %c%s"
1177     ;;                     value
1178     ;;                     (ideographic-radical value)
1179     ;;                     line-breaking))
1180     ;;     (or radical
1181     ;;         (setq radical value)))
1182     ;;   (setq attributes (delq 'shinjigen-1-radical attributes))
1183     ;;   )
1184     (when (and (memq 'ideographic- attributes)
1185                (setq value (get-char-attribute char 'ideographic-)))
1186       (insert "(ideographic-       ")
1187       (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1188             separator nil)
1189       (while (consp value)
1190         (setq cell (car value))
1191         (if (integerp cell)
1192             (setq cell (decode-char '=ucs cell)))
1193         (cond ((characterp cell)
1194                (if separator
1195                    (insert lbs))
1196                (if readable
1197                    (insert (format "%S" cell))
1198                  (char-db-insert-char-spec cell readable))
1199                (setq separator lbs))
1200               ((consp cell)
1201                (if separator
1202                    (insert lbs))
1203                (if (consp (car cell))
1204                    (char-db-insert-char-spec cell readable)
1205                  (char-db-insert-char-reference cell readable))
1206                (setq separator lbs))
1207               (t
1208                (if separator
1209                    (insert separator))
1210                (insert (prin1-to-string cell))
1211                (setq separator " ")))
1212         (setq value (cdr value)))
1213       (insert ")")
1214       (insert line-breaking)
1215       (setq attributes (delq 'ideographic- attributes)))
1216     (when (and (memq 'total-strokes attributes)
1217                (setq value (get-char-attribute char 'total-strokes)))
1218       (insert (format "(total-strokes       . %S)%s"
1219                       value
1220                       line-breaking))
1221       (setq attributes (delq 'total-strokes attributes))
1222       )
1223     (when (and (memq '->ideograph attributes)
1224                (setq value (get-char-attribute char '->ideograph)))
1225       (insert (format "(->ideograph\t%s)%s"
1226                       (mapconcat (lambda (code)
1227                                    (cond ((symbolp code)
1228                                           (symbol-name code))
1229                                          ((integerp code)
1230                                           (format "#x%04X" code))
1231                                          (t
1232                                           (format "%s %S"
1233                                                   line-breaking code))))
1234                                  value " ")
1235                       line-breaking))
1236       (setq attributes (delq '->ideograph attributes))
1237       )
1238     ;; (when (and (memq '->decomposition attributes)
1239     ;;            (setq value (get-char-attribute char '->decomposition)))
1240     ;;   (insert (format "(->decomposition\t%s)%s"
1241     ;;                   (mapconcat (lambda (code)
1242     ;;                                (cond ((symbolp code)
1243     ;;                                       (symbol-name code))
1244     ;;                                      ((characterp code)
1245     ;;                                       (if readable
1246     ;;                                           (format "%S" code)
1247     ;;                                         (format "#x%04X"
1248     ;;                                                 (char-int code))
1249     ;;                                         ))
1250     ;;                                      ((integerp code)
1251     ;;                                       (format "#x%04X" code))
1252     ;;                                      (t
1253     ;;                                       (format "%s%S" line-breaking code))))
1254     ;;                              value " ")
1255     ;;                   line-breaking))
1256     ;;   (setq attributes (delq '->decomposition attributes))
1257     ;;   )
1258     (if (equal (get-char-attribute char '->titlecase)
1259                (get-char-attribute char '->uppercase))
1260         (setq attributes (delq '->titlecase attributes)))
1261     (when (and (memq '->mojikyo attributes)
1262                (setq value (get-char-attribute char '->mojikyo)))
1263       (insert (format "(->mojikyo\t\t. %06d)\t; %c%s"
1264                       value (decode-char 'mojikyo value)
1265                       line-breaking))
1266       (setq attributes (delq '->mojikyo attributes))
1267       )
1268     (when (and (memq 'hanyu-dazidian-vol attributes)
1269                (setq value (get-char-attribute char 'hanyu-dazidian-vol)))
1270       (insert (format "(hanyu-dazidian-vol  . %d)%s"
1271                       value line-breaking))
1272       (setq attributes (delq 'hanyu-dazidian-vol attributes))
1273       )
1274     (when (and (memq 'hanyu-dazidian-page attributes)
1275                (setq value (get-char-attribute char 'hanyu-dazidian-page)))
1276       (insert (format "(hanyu-dazidian-page . %d)%s"
1277                       value line-breaking))
1278       (setq attributes (delq 'hanyu-dazidian-page attributes))
1279       )
1280     (when (and (memq 'hanyu-dazidian-char attributes)
1281                (setq value (get-char-attribute char 'hanyu-dazidian-char)))
1282       (insert (format "(hanyu-dazidian-char . %d)%s"
1283                       value line-breaking))
1284       (setq attributes (delq 'hanyu-dazidian-char attributes))
1285       )
1286     (unless readable
1287       (dolist (ignored '(composition
1288                          ->denotational <-subsumptive ->ucs-unified
1289                          ->ideographic-component-forms))
1290         (setq attributes (delq ignored attributes))))
1291     (while attributes
1292       (setq name (car attributes))
1293       (unless (eq (setq value (get-char-attribute char name 'value-is-empty))
1294                   'value-is-empty)
1295         (cond ((setq ret (find-charset name))
1296                (setq name (charset-name ret))
1297                (when (not (memq name dest-ccss))
1298                  (setq dest-ccss (cons name dest-ccss))
1299                  (char-db-insert-ccs-feature name value line-breaking))
1300                )
1301               ((string-match "^=>ucs@" (symbol-name name))
1302                (insert (format "(%-18s . #x%04X)\t; %c%s"
1303                                name value (decode-char '=ucs value)
1304                                line-breaking))
1305                )
1306               ((eq name 'jisx0208-1978/4X)
1307                (insert (format "(%-18s . #x%04X)%s"
1308                                name value
1309                                line-breaking))
1310                )
1311               ((and
1312                 (not readable)
1313                 (not (eq name '->subsumptive))
1314                 (not (eq name '->uppercase))
1315                 (not (eq name '->lowercase))
1316                 (not (eq name '->titlecase))
1317                 (not (eq name '->canonical))
1318                 (not (eq name '->Bopomofo))
1319                 (not (eq name '->mistakable))
1320                 (not (eq name '->ideographic-variants))
1321                 (null (get-char-attribute
1322                        char (intern (format "%s*sources" name))))
1323                 (not (string-match "\\*sources$" (symbol-name name)))
1324                 (null (get-char-attribute
1325                        char (intern (format "%s*note" name))))
1326                 (not (string-match "\\*note$" (symbol-name name)))
1327                 (or (eq name '<-identical)
1328                     (eq name '<-uppercase)
1329                     (eq name '<-lowercase)
1330                     (eq name '<-titlecase)
1331                     (eq name '<-canonical)
1332                     (eq name '<-ideographic-variants)
1333                     ;; (eq name '<-synonyms)
1334                     (string-match "^<-synonyms" (symbol-name name))
1335                     (eq name '<-mistakable)
1336                     (when (string-match "^->" (symbol-name name))
1337                       (cond
1338                        ((string-match "^->fullwidth" (symbol-name name))
1339                         (not (and (consp value)
1340                                   (characterp (car value))
1341                                   (encode-char
1342                                    (car value) '=ucs 'defined-only)))
1343                         )
1344                        (t)))
1345                     ))
1346                )
1347               ((or (eq name 'ideographic-structure)
1348                    (eq name 'ideographic-combination)
1349                    (eq name 'ideographic-)
1350                    (eq name '=decomposition)
1351                    (char-feature-base-name= '=decomposition name)
1352                    (char-feature-base-name= '=>decomposition name)
1353                    ;; (string-match "^=>*decomposition\\(@[^*]+\\)?$"
1354                    ;;               (symbol-name name))
1355                    (string-match "^\\(->\\|<-\\)[^*]*$" (symbol-name name))
1356                    (string-match "^\\(->\\|<-\\)[^*]*\\*sources$"
1357                                  (symbol-name name))
1358                    )
1359                (char-db-insert-relation-feature char name value
1360                                                 line-breaking
1361                                                 ccss readable))
1362               ((memq name '(ideograph=
1363                             original-ideograph-of
1364                             ancient-ideograph-of
1365                             vulgar-ideograph-of
1366                             wrong-ideograph-of
1367                             ;; simplified-ideograph-of
1368                             ideographic-variants
1369                             ;; ideographic-different-form-of
1370                             ))
1371                (insert (format "(%-18s%s " name line-breaking))
1372                (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1373                      separator nil)
1374                (while (consp value)
1375                  (setq cell (car value))
1376                  (if (and (consp cell)
1377                           (consp (car cell)))
1378                      (progn
1379                        (if separator
1380                            (insert lbs))
1381                        (char-db-insert-alist cell readable)
1382                        (setq separator lbs))
1383                    (if separator
1384                        (insert separator))
1385                    (insert (prin1-to-string cell))
1386                    (setq separator " "))
1387                  (setq value (cdr value)))
1388                (insert ")")
1389                (insert line-breaking))
1390               ((consp value)
1391                (insert (format "(%-18s " name))
1392                (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1393                      separator nil)
1394                (while (consp value)
1395                  (setq cell (car value))
1396                  (if (and (consp cell)
1397                           (consp (car cell))
1398                           (setq ret (condition-case nil
1399                                         (find-char cell)
1400                                       (error nil))))
1401                      (progn
1402                        (setq rest cell
1403                              al nil
1404                              cal nil)
1405                        (while rest
1406                          (setq key (car (car rest)))
1407                          (if (find-charset key)
1408                              (setq cal (cons key cal))
1409                            (setq al (cons key al)))
1410                          (setq rest (cdr rest)))
1411                        (if separator
1412                            (insert lbs))
1413                        (insert-char-attributes ret
1414                                                readable
1415                                                al ; cal
1416                                                nil 'for-sub-node)
1417                        (setq separator lbs))
1418                    (setq ret (prin1-to-string cell))
1419                    (if separator
1420                        (if (< (+ (current-column)
1421                                  (length ret)
1422                                  (length separator))
1423                               76)
1424                            (insert separator)
1425                          (insert lbs)))
1426                    (insert ret)
1427                    (setq separator " "))
1428                  (setq value (cdr value)))
1429                (insert ")")
1430                (insert line-breaking))
1431               (t
1432                (insert (format "(%-18s" name))
1433                (setq ret (prin1-to-string value))
1434                (unless (< (+ (current-column)
1435                              (length ret)
1436                              3)
1437                           76)
1438                  (insert line-breaking))
1439                (insert " . " ret ")" line-breaking)
1440                ;; (insert (format "(%-18s . %S)%s"
1441                ;;                 name value
1442                ;;                 line-breaking))
1443                )
1444               ))
1445       (setq attributes (cdr attributes)))
1446     (insert ")")))
1447
1448 (defun insert-char-data (char &optional readable
1449                               attributes)
1450   (save-restriction
1451     (narrow-to-region (point)(point))
1452     (insert "(define-char
1453   '")
1454     (insert-char-attributes char readable attributes)
1455     (insert ")\n")
1456     (goto-char (point-min))
1457     (while (re-search-forward "[ \t]+$" nil t)
1458       (replace-match ""))
1459     ;; from tabify.
1460     (goto-char (point-min))
1461     (while (re-search-forward "[ \t][ \t][ \t]*" nil t)
1462       (let ((column (current-column))
1463             (indent-tabs-mode t))
1464         (delete-region (match-beginning 0) (point))
1465         (indent-to column)))
1466     (goto-char (point-max))
1467     ;; (tabify (point-min)(point-max))
1468     ))
1469
1470 (defun insert-char-data-with-variant (char &optional printable
1471                                            no-ucs-unified
1472                                            script excluded-script)
1473   (insert-char-data char printable)
1474   (let ((variants (char-variants char))
1475         rest
1476         variant vs ret)
1477     (setq variants (sort variants #'<))
1478     (setq rest variants)
1479     (setq variants (cons char variants))
1480     (while rest
1481       (setq variant (car rest))
1482       (unless (get-char-attribute variant '<-subsumptive)
1483         (if (and (or (null script)
1484                      (null (setq vs (get-char-attribute variant 'script)))
1485                      (memq script vs))
1486                  (or (null excluded-script)
1487                      (null (setq vs (get-char-attribute variant 'script)))
1488                      (not (memq excluded-script vs))))
1489             (unless (and no-ucs-unified (get-char-attribute variant '=ucs))
1490               (insert-char-data variant printable)
1491               (if (setq ret (char-variants variant))
1492                   (while ret
1493                     (or (memq (car ret) variants)
1494                         ;; (get-char-attribute (car ret) '<-subsumptive)
1495                         (setq rest (nconc rest (list (car ret)))))
1496                     (setq ret (cdr ret)))))))
1497       (setq rest (cdr rest)))))
1498
1499 (defun insert-char-range-data (min max &optional script excluded-script)
1500   (let ((code min)
1501         char)
1502     (while (<= code max)
1503       (setq char (decode-char '=ucs code))
1504       (if (encode-char char '=ucs 'defined-only)
1505           (insert-char-data-with-variant char nil 'no-ucs-unified
1506                                          script excluded-script))
1507       (setq code (1+ code)))))
1508
1509 (defun write-char-range-data-to-file (min max file
1510                                           &optional script excluded-script)
1511   (let ((coding-system-for-write char-db-file-coding-system))
1512     (with-temp-buffer
1513       (insert (format ";; -*- coding: %s -*-\n"
1514                       char-db-file-coding-system))
1515       (insert-char-range-data min max script excluded-script)
1516       (write-region (point-min)(point-max) file))))
1517
1518 (defvar what-character-original-window-configuration)
1519
1520 ;;;###autoload
1521 (defun what-char-definition (char)
1522   (interactive (list (char-after)))
1523   (let ((buf (get-buffer-create "*Character Description*"))
1524         (the-buf (current-buffer))
1525         (win-conf (current-window-configuration)))
1526     (pop-to-buffer buf)
1527     (make-local-variable 'what-character-original-window-configuration)
1528     (setq what-character-original-window-configuration win-conf)
1529     (setq buffer-read-only nil)
1530     (erase-buffer)
1531     (condition-case err
1532         (progn
1533           (insert-char-data-with-variant char 'printable)
1534           (unless (char-attribute-alist char)
1535             (insert (format ";; = %c\n"
1536                             (let* ((rest (split-char char))
1537                                    (ccs (pop rest))
1538                                    (code (pop rest)))
1539                               (while rest
1540                                 (setq code (logior (lsh code 8)
1541                                                    (pop rest))))
1542                               (decode-char ccs code)))))
1543           ;; (char-db-update-comment)
1544           (set-buffer-modified-p nil)
1545           (view-mode the-buf (lambda (buf)
1546                                (set-window-configuration
1547                                 what-character-original-window-configuration)
1548                                ))
1549           (goto-char (point-min)))
1550       (error (progn
1551                (set-window-configuration
1552                 what-character-original-window-configuration)
1553                (signal (car err) (cdr err)))))))
1554
1555
1556 ;;; @ end
1557 ;;;
1558
1559 (provide 'char-db-util)
1560
1561 ;;; char-db-util.el ends here