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