Reformatted.
[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/jt
186     =hanyo-denshi/ks
187     =hanyo-denshi/tk
188     ;; ==mj
189     ;; ==adobe-japan1-0
190     ;; ==adobe-japan1-1
191     ;; ==adobe-japan1-2
192     ;; ==adobe-japan1-3
193     ;; ==adobe-japan1-4
194     ;; ==adobe-japan1-5
195     ;; ==adobe-japan1-6
196     ==jis-x0208
197     ==jis-x0213-1
198     ==jis-x0213-2
199     ==jis-x0212
200     ==hanyo-denshi/ja
201     ==hanyo-denshi/jb
202     ==hanyo-denshi/jc
203     ==hanyo-denshi/ft
204     ==hanyo-denshi/ia
205     ==hanyo-denshi/ib
206     ==hanyo-denshi/hg
207     ==hanyo-denshi/jt
208     ==hanyo-denshi/ks
209     ==hanyo-denshi/tk
210     =gt
211     =gt-k
212     =daikanwa
213     =daijiten
214     =daikanwa@rev2
215     =daikanwa@rev1
216     =daikanwa/+p
217     =daikanwa/ho
218     ==daikanwa
219     ==daijiten
220     =cbeta
221     ideograph-hanziku-1
222     ideograph-hanziku-2
223     ideograph-hanziku-3
224     ideograph-hanziku-4
225     ideograph-hanziku-5
226     ideograph-hanziku-6
227     ideograph-hanziku-7
228     ideograph-hanziku-8
229     ideograph-hanziku-9
230     ideograph-hanziku-10
231     ideograph-hanziku-11
232     ideograph-hanziku-12
233     ;; =>>>adobe-japan1-0
234     ;; =>>>adobe-japan1-1
235     ;; =>>>adobe-japan1-2
236     ;; =>>>adobe-japan1-3
237     ;; =>>>adobe-japan1-4
238     ;; =>>>adobe-japan1-5
239     ;; =>>>adobe-japan1-6
240     ;; =>>>jis-x0208
241     ;; =>>>jis-x0213-1
242     ;; =>>>jis-x0213-2
243     ;; =>>>hanyo-denshi/ja
244     ;; =>>>hanyo-denshi/jb
245     ;; =>>>hanyo-denshi/jc
246     ;; =>>>hanyo-denshi/ft
247     ;; =>>>hanyo-denshi/ib
248     ;; =>>>hanyo-denshi/hg
249     ;; =>>>hanyo-denshi/jt
250     ;; =>>>hanyo-denshi/ks
251     ;; =>>>gt
252     =>>adobe-japan1-0
253     =>>adobe-japan1-1
254     =>>adobe-japan1-2
255     =>>adobe-japan1-3
256     =>>adobe-japan1-4
257     =>>adobe-japan1-5
258     =>>adobe-japan1-6
259     =>>jis-x0208
260     =>>jis-x0213-1
261     =>>jis-x0213-1@2000
262     =>>jis-x0213-1@2004
263     =>>jis-x0213-2
264     =>>jis-x0208@1978
265     =>>hanyo-denshi/ft
266     =>>hanyo-denshi/jt
267     =>>hanyo-denshi/ks
268     =>>gt
269     =>>daikanwa
270     =>>cbeta
271     =+>jis-x0208
272     =+>jis-x0213-1
273     =+>jis-x0213-2
274     =+>adobe-japan1-0
275     =+>adobe-japan1-1
276     =+>adobe-japan1-2
277     =+>adobe-japan1-3
278     =+>adobe-japan1-4
279     =+>adobe-japan1-5
280     =+>adobe-japan1-6
281     =+>jis-x0208@1978
282     =>jis-x0208
283     =>jis-x0208@1997
284     =>jis-x0213-1
285     =>jis-x0213-1@2000
286     =>jis-x0213-1@2004
287     =>jis-x0213-2
288     ==>ucs@bucs
289     ==>daijiten
290     ;; =>ucs@hanyo-denshi
291     =>ucs@iso
292     =>ucs@unicode
293     =>ucs@jis
294     =>ucs@cns
295     =>ucs@ks
296     =+>ucs@iso
297     =+>ucs@unicode
298     =+>ucs@jis
299     =+>ucs@jis/1990
300     =+>ucs@cns
301     =+>ucs@ks
302     =>>ucs@iso
303     =>>ucs@unicode
304     =>>ucs@jis
305     =>>ucs@cns
306     =>>>ucs@iso
307     =>>>ucs@unicode
308     ==ucs@iso
309     ==ucs@unicode
310     ;; ==ucs@cns
311     ==gb2312
312     ==ks-x1001
313     ==cns11643-1
314     ==cns11643-2
315     ==cns11643-3
316     ==cns11643-4
317     ==cns11643-5
318     ==cns11643-6
319     ==cns11643-7
320     ==gt
321     ==jis-x0208@1990
322     ;; ==jis-x0208@1983
323     ==jis-x0208@1978
324     ==gt-k
325     =ucs@iso
326     =ucs@unicode
327     =ucs@cns
328     ==big5-cdp
329     ==cbeta
330     =>>big5-cdp
331     =>>gt-k
332     =+>gt
333     =+>big5-cdp
334     =>big5-cdp
335     =>big5-cdp@iwds-1
336     =>mj
337     =>hanyo-denshi/tk
338     =>iwds-1
339     =big5
340     =big5-eten
341     =zinbun-oracle
342     =>zinbun-oracle
343     =ruimoku-v6
344     =>>ruimoku-v6
345     ==ruimoku-v6
346     =>ruimoku-v6
347     =jef-china3
348     =shinjigen
349     =>cns11643-5
350     =>cns11643-7
351     =>gt
352     =>gt-k
353     =>daikanwa
354     =>daikanwa/ho
355     =>cbeta
356     =ucs-var-001
357     =ucs-var-002
358     =ucs-var-003
359     =ucs-var-004
360     =ucs-var-005
361     =ucs-var-006
362     =ucs-var-007
363     =ucs-var-008
364     =ucs-var-010
365     =ucs-itaiji-001
366     =ucs-itaiji-002
367     =ucs-itaiji-003
368     =ucs-itaiji-004
369     =ucs-itaiji-005
370     =ucs-itaiji-006
371     =ucs-itaiji-007
372     =ucs-itaiji-008
373     =ucs-itaiji-009
374     =ucs-itaiji-010
375     =ucs-itaiji-011
376     =ucs-itaiji-012
377     =ucs-itaiji-014
378     =ucs-itaiji-016
379     =+>ucs-var-003
380     =>ucs-itaiji-001
381     =>ucs-itaiji-002
382     =>ucs-itaiji-003
383     =>ucs-itaiji-004
384     =>ucs-itaiji-005
385     =>ucs-itaiji-006
386     =>ucs-itaiji-007
387     =>ucs-itaiji-008
388     =>ucs-itaiji-009
389     =>ucs-itaiji-010
390     =>ucs-itaiji-011
391     =>ucs-itaiji-012
392     =>ucs-itaiji-014
393     =>ucs-itaiji-016
394     =big5-cdp-var-001
395     =big5-cdp-var-002
396     =big5-cdp-var-003
397     =big5-cdp-var-004
398     =big5-cdp-var-005
399     =big5-cdp-var-007
400     =big5-cdp-var-008
401     =big5-cdp-var-010
402     =big5-cdp-itaiji-001
403     =big5-cdp-itaiji-002
404     =big5-cdp-itaiji-003
405     =big5-cdp-itaiji-004
406     =big5-cdp-itaiji-005
407     =>big5-cdp-itaiji-001
408     =>big5-cdp-itaiji-002
409     =>big5-cdp-itaiji-003
410     =>big5-cdp-itaiji-004
411     =>big5-cdp-itaiji-005
412     =>big5-cdp-itaiji-006
413     =>big5-cdp-var-001
414     =>ucs@iwds-1
415     =>ucs@cognate
416     =>ucs@component
417     =>ucs@iwds-1/normalized
418     =>ucs-itaiji-001@iwds-1
419     =>ucs-itaiji-002@iwds-1
420     =>ucs-itaiji-003@iwds-1
421     =>ucs-itaiji-006@iwds-1
422     =>big5-cdp@component
423     =>big5-cdp@cognate
424     ==ucs@gb
425     =ucs@gb
426     ==ucs-var-002
427     ==ucs-itaiji-002
428     =ucs@JP/hanazono
429     =koseki
430     =shuowen-jiguge
431     =>shuowen-jiguge
432     ===shuowen-jiguge5
433     ===shuowen-jiguge4
434     ===chise-hdic-ktb-seal))
435
436
437 ;;; @ char-db formatters
438 ;;;
439
440 (defun char-db-make-char-spec (char)
441   (let (ret char-spec)
442     (cond ((characterp char)
443            (cond ((and (setq ret (encode-char char '=ucs 'defined-only))
444                        (not (and (<= #xE000 ret)(<= ret #xF8FF))))
445                   (setq char-spec (list (cons '=ucs ret)))
446                   (cond ((setq ret (get-char-attribute char 'name))
447                          (setq char-spec (cons (cons 'name ret) char-spec))
448                          )
449                         ((setq ret (get-char-attribute char 'name*))
450                          (setq char-spec (cons (cons 'name* ret) char-spec))
451                          ))
452                   )
453                  ((encode-char char '=mj 'defined-only)
454                   (setq char-spec nil)
455                   (dolist (ccs (charset-list))
456                     (if (and (or (eq ccs '=mj)
457                                  ;; (eq (charset-property ccs 'iso-ir) 177)
458                                  (string-match "=ucs@" (symbol-name ccs))
459                                  )
460                              (setq ccs (charset-name ccs))
461                              (null (assq ccs char-spec))
462                              (setq ret (encode-char char ccs 'defined-only)))
463                         (setq char-spec (cons (cons ccs ret) char-spec))))
464                   )
465                  ((encode-char char '==mj 'defined-only)
466                   (setq char-spec nil)
467                   (dolist (ccs (charset-list))
468                     (if (and (or (eq ccs '==mj)
469                                  ;; (eq (charset-property ccs 'iso-ir) 177)
470                                  (string-match "=ucs@" (symbol-name ccs))
471                                  )
472                              (setq ccs (charset-name ccs))
473                              (null (assq ccs char-spec))
474                              (setq ret (encode-char char ccs 'defined-only)))
475                         (setq char-spec (cons (cons ccs ret) char-spec))))
476                   )
477                  ((encode-char char '=adobe-japan1 'defined-only)
478                   (setq char-spec nil)
479                   (dolist (ccs (charset-list))
480                     (if (and (or (memq ccs
481                                        '(=adobe-japan1-0
482                                          =adobe-japan1-1
483                                          =adobe-japan1-2
484                                          =adobe-japan1-3
485                                          =adobe-japan1-4
486                                          =adobe-japan1-5
487                                          =adobe-japan1-6
488                                          ))
489                                  ;; (eq (charset-property ccs 'iso-ir) 177)
490                                  (string-match "=ucs@" (symbol-name ccs))
491                                  )
492                              (setq ccs (charset-name ccs))
493                              (null (assq ccs char-spec))
494                              (setq ret (encode-char char ccs 'defined-only)))
495                         (setq char-spec (cons (cons ccs ret) char-spec))))
496                   )
497                  ((encode-char char '==adobe-japan1 'defined-only)
498                   (setq char-spec nil)
499                   (dolist (ccs (charset-list))
500                     (if (and (or (memq ccs
501                                        '(==adobe-japan1-0
502                                          ==adobe-japan1-1
503                                          ==adobe-japan1-2
504                                          ==adobe-japan1-3
505                                          ==adobe-japan1-4
506                                          ==adobe-japan1-5
507                                          ==adobe-japan1-6
508                                          ))
509                                  ;; (eq (charset-property ccs 'iso-ir) 177)
510                                  (string-match "=ucs@" (symbol-name ccs))
511                                  )
512                              (setq ccs (charset-name ccs))
513                              (null (assq ccs char-spec))
514                              (setq ret (encode-char char ccs 'defined-only)))
515                         (setq char-spec (cons (cons ccs ret) char-spec))))
516                   )
517                  ((setq ret
518                         (catch 'tag
519                           (let ((rest char-db-coded-charset-priority-list)
520                                 ccs)
521                             (while rest
522                               (setq ccs (charset-name
523                                          (find-charset (car rest))))
524                               (if (setq ret
525                                         (encode-char char ccs
526                                                      'defined-only))
527                                   (throw 'tag (cons ccs ret)))
528                               (setq rest (cdr rest))))))
529                   (setq char-spec (list ret))
530                   (dolist (ccs (delq (car ret) (charset-list)))
531                     (if (and (or (charset-iso-final-char ccs)
532                                  (memq ccs
533                                        '(=daikanwa
534                                          =daikanwa@rev2
535                                          ;; =gt-k
536                                          =jis-x0208@1997
537                                          ))
538                                  (and (eq (charset-property ccs 'iso-ir) 177)
539                                       (not (eq (charset-name ccs) '=ucs@big5)))
540                                  ;; (string-match "=ucs@" (symbol-name ccs))
541                                  )
542                              (setq ccs (charset-name ccs))
543                              (null (assq ccs char-spec))
544                              (setq ret (encode-char char ccs 'defined-only)))
545                         (setq char-spec (cons (cons ccs ret) char-spec))))
546                   (if (null char-spec)
547                       (setq char-spec (split-char char)))
548                   (cond ((setq ret (get-char-attribute char 'name))
549                          (setq char-spec (cons (cons 'name ret) char-spec))
550                          )
551                         ((setq ret (get-char-attribute char 'name*))
552                          (setq char-spec (cons (cons 'name* ret) char-spec))
553                          ))
554                   )
555                  ((setq ret (get-char-attribute
556                              char 'ideographic-combination))
557                   (setq char-spec
558                         (cons (cons 'ideographic-combination ret)
559                               char-spec))
560                   ))
561            char-spec)
562           ((consp char)
563            char))))
564     
565 (defun char-db-insert-char-spec (char &optional readable column
566                                       required-features)
567   (unless column
568     (setq column (current-column)))
569   (let (char-spec temp-char)
570     (setq char-spec (char-db-make-char-spec char))
571     (unless (or (characterp char) ; char
572                 (condition-case nil
573                     (setq char (find-char char-spec))
574                   (error nil)))
575       ;; define temporary character
576       ;;   Current implementation is dirty.
577       (setq temp-char (define-char (cons '(ideograph-daikanwa . 0)
578                                          char-spec)))
579       (remove-char-attribute temp-char 'ideograph-daikanwa)
580       (setq char temp-char))
581     (insert-char-attributes char
582                             readable
583                             (union (mapcar #'car char-spec)
584                                    required-features)
585                             nil 'for-sub-node)
586     (when temp-char
587       ;; undefine temporary character
588       ;;   Current implementation is dirty.
589       (setq char-spec (char-attribute-alist temp-char))
590       (while char-spec
591         (remove-char-attribute temp-char (car (car char-spec)))
592         (setq char-spec (cdr char-spec))))))
593
594 (defun char-db-insert-alist (alist &optional readable column)
595   (unless column
596     (setq column (current-column)))
597   (let ((line-breaking
598          (concat "\n" (make-string (1+ column) ?\ )))
599         name value
600         ret al ; cal
601         key
602         lbs cell rest separator)
603     (insert "(")
604     (while alist
605       (setq name (car (car alist))
606             value (cdr (car alist)))
607       (cond ((eq name 'char)
608              (insert "(char . ")
609              (if (setq ret (condition-case nil
610                                (find-char value)
611                              (error nil)))
612                  (progn
613                    (setq al nil
614                          ;; cal nil
615                          )
616                    (while value
617                      (setq key (car (car value)))
618                      ;; (if (find-charset key)
619                      ;;     (setq cal (cons key cal))
620                      (setq al (cons key al))
621                      ;; )
622                      (setq value (cdr value)))
623                    (insert-char-attributes ret
624                                            readable
625                                            (or al 'none) ; cal
626                                            nil 'for-sub-node))
627                (insert (prin1-to-string value)))
628              (insert ")")
629              (insert line-breaking))
630             ((consp value)
631              (insert (format "(%-18s " name))
632              (setq lbs (concat "\n" (make-string (current-column) ?\ )))
633              (while (consp value)
634                (setq cell (car value))
635                (if (and (consp cell)
636                         (consp (car cell))
637                         (setq ret (condition-case nil
638                                       (find-char cell)
639                                     (error nil)))
640                         )
641                    (progn
642                      (setq rest cell
643                            al nil
644                            ;; cal nil
645                            )
646                      (while rest
647                        (setq key (car (car rest)))
648                        ;; (if (find-charset key)
649                        ;;     (setq cal (cons key cal))
650                        (setq al (cons key al))
651                        ;; )
652                        (setq rest (cdr rest)))
653                      (if separator
654                          (insert lbs))
655                      (insert-char-attributes ret
656                                              readable
657                                              al ; cal
658                                              nil 'for-sub-node)
659                      (setq separator lbs))
660                  (if separator
661                      (insert separator))
662                  (insert (prin1-to-string cell))
663                  (setq separator " "))
664                (setq value (cdr value)))
665              (insert ")")
666              (insert line-breaking))
667             (t
668              (insert (format "(%-18s . %S)%s"
669                              name value
670                              line-breaking))))
671       (setq alist (cdr alist))))
672   (insert ")"))
673
674 (defun char-db-insert-char-reference (plist &optional readable column)
675   (unless column
676     (setq column (current-column)))
677   (let ((line-breaking
678          (concat "\n" (make-string (1+ column) ?\ )))
679         (separator "")
680         name value)
681     (insert "(")
682     (while plist
683       (setq name (pop plist))
684       (setq value (pop plist))
685       (cond ((eq name :char)
686              (insert separator)
687              (insert ":char\t")
688              (cond ((numberp value)
689                     (setq value (decode-char '=ucs value)))
690                    ;; ((consp value)
691                    ;;  (setq value (or (find-char value)
692                    ;;                  value)))
693                    )
694              (char-db-insert-char-spec value readable)
695              (insert line-breaking)
696              (setq separator ""))
697             ((eq name :radical)
698              (insert (format "%s%s\t%d ; %c%s"
699                              separator
700                              name value
701                              (ideographic-radical value)
702                              line-breaking))
703              (setq separator ""))
704             (t
705              (insert (format "%s%s\t%S" separator name value))
706              (setq separator line-breaking)))
707       ))
708   (insert ")"))
709
710 (defun char-db-decode-isolated-char (ccs code-point)
711   (let (ret)
712     (setq ret
713           (cond ((eq ccs 'arabic-iso8859-6)
714                  (decode-char ccs code-point))
715                 ;; ((eq ccs '=gt)
716                 ;;  (decode-builtin-char '==gt code-point))
717                 ((and (memq ccs '(=gt-pj-1
718                                   =gt-pj-2
719                                   =gt-pj-3
720                                   =gt-pj-4
721                                   =gt-pj-5
722                                   =gt-pj-6
723                                   =gt-pj-7
724                                   =gt-pj-8
725                                   =gt-pj-9
726                                   =gt-pj-10
727                                   =gt-pj-11))
728                       (setq ret (decode-char ccs code-point))
729                       (setq ret (encode-char ret '=gt 'defined-only)))
730                  (decode-builtin-char '=gt ret))
731                 (t
732                  (decode-builtin-char ccs code-point))))
733     (cond ((null ret)
734            (or (decode-char ccs code-point)
735                (define-char (list (cons ccs code-point)))))
736           ((and (<= 0 (char-int ret))
737                 (<= (char-int ret) #x1F))
738            (decode-char '=ucs (+ #x2400 (char-int ret))))
739           ((= (char-int ret) #x7F)
740            ?\u2421)
741           (t ret))))
742
743 (defvar char-db-convert-obsolete-format t)
744
745 (defun char-db-insert-ccs-feature (name value line-breaking)
746   (cond
747    ((integerp value)
748     (insert
749      (format
750       (cond
751        ((memq name '(=>iwds-1
752                      ==shinjigen
753                      =shinjigen
754                      =shinjigen@1ed ==shinjigen@1ed
755                      =shinjigen@rev ==shinjigen@rev
756                      =shinjigen/+p@rev ==shinjigen/+p@rev
757                      ===daikanwa/ho ==daikanwa/ho
758                      =daikanwa/ho =>>daikanwa/ho =>daikanwa/ho))
759         "(%-18s .  %04d)\t; %c")
760        ((eq name '=shinjigen@1ed/24pr)
761         "(%-18s . %04d)\t; %c")
762        ((or
763          (memq name
764                '(===daikanwa
765                  ==daikanwa =daikanwa =>>daikanwa =>daikanwa
766                  =daikanwa@rev1 =daikanwa@rev2
767                  =daikanwa/+p ==daikanwa/+p ===daikanwa/+p
768                  =>>daikanwa/+p
769                  =daikanwa/+2p =>>daikanwa/+2p
770                  =gt ==gt ===gt
771                  =>>gt =+>gt =>gt
772                  =gt-k ==gt-k ===gt-k
773                  =>>gt-k =>gt-k
774                  =adobe-japan1-0 ==adobe-japan1-0 ===adobe-japan1-0
775                  =adobe-japan1-1 ==adobe-japan1-1 ===adobe-japan1-1
776                  =adobe-japan1-2 ==adobe-japan1-2 ===adobe-japan1-2
777                  =adobe-japan1-3 ==adobe-japan1-3 ===adobe-japan1-3
778                  =adobe-japan1-4 ==adobe-japan1-4 ===adobe-japan1-4
779                  =adobe-japan1-5 ==adobe-japan1-5 ===adobe-japan1-5
780                  =adobe-japan1-6 ==adobe-japan1-6 ===adobe-japan1-6
781                  =>>adobe-japan1-0 =+>adobe-japan1-0
782                  =>>adobe-japan1-1 =+>adobe-japan1-1
783                  =>>adobe-japan1-2 =+>adobe-japan1-2
784                  =>>adobe-japan1-3 =+>adobe-japan1-3
785                  =>>adobe-japan1-4 =+>adobe-japan1-4
786                  =>>adobe-japan1-5 =+>adobe-japan1-5
787                  =>>adobe-japan1-6 =+>adobe-japan1-6
788                  =>cbeta =cbeta =>>cbeta ==cbeta ===cbeta
789                  =zinbun-oracle =>zinbun-oracle
790                  =daijiten ==daijiten ===daijiten ==>daijiten
791                  ===hng-jou ===hng-keg ===hng-dng ===hng-mam
792                  ===hng-drt ===hng-kgk ===hng-myz ===hng-kda
793                  ===hng-khi ===hng-khm ===hng-fhs ===hng-hok
794                  ===hng-kyd ===hng-sok
795                  ===hng-yhk ===hng-kak ===hng-kar ===hng-kae
796                  ===hng-sys ===hng-tsu ===hng-tzj
797                  ===hng-hos ===hng-kkh ===hng-nak ===hng-jhk
798                  ===hng-hod ===hng-gok ===hng-ink ===hng-nto
799                  ===hng-nkm ===hng-k24 ===hng-ini ===hng-nkk
800                  ===hng-kcc ===hng-kcj ===hng-kbk ===hng-sik
801                  ===hng-skk ===hng-kyu ===hng-ksk ===hng-wan
802                  ===hng-okd ===hng-wad ===hng-kmi
803                  ===hng-zkd ===hng-doh ===hng-jyu ===hng-tzs
804                  ===hng-sai ===hng-kad ===hng-kss
805                  ===hng-kyo ===hng-ykk ===hng-saa
806                  ===hng-sab ===hng-wks ===hng-wke ===hng-smk
807                  ===hng-sgs ===hng-sts ===hng-kkd
808                  =>shuowen-jiguge =shuowen-jiguge
809                  ===shuowen-jiguge4 ===shuowen-jiguge5 ===shuowen-jiguge-A30))
810          ;; (string-match "^=adobe-" (symbol-name name))
811          )
812         "(%-18s . %05d)\t; %c")
813        ((memq name '(=hanyo-denshi/ks
814                      ==hanyo-denshi/ks ===hanyo-denshi/ks
815                      =>>hanyo-denshi/ks
816                      =koseki ==koseki
817                      =mj ==mj ===mj =>>mj =>mj
818                      =zihai mojikyo))
819         "(%-18s . %06d)\t; %c")
820        ((memq name '(=hanyo-denshi/tk =>hanyo-denshi/tk ==hanyo-denshi/tk))
821         "(%-18s . %08d)\t; %c")
822        ((>= (charset-dimension name) 2)
823         "(%-18s . #x%04X)\t; %c")
824        (t
825         "(%-18s . #x%02X)\t; %c"))
826       name
827       (if (= (charset-iso-graphic-plane name) 1)
828           (logior value
829                   (cond ((= (charset-dimension name) 1)
830                          #x80)
831                         ((= (charset-dimension name) 2)
832                          #x8080)
833                         ((= (charset-dimension name) 3)
834                          #x808080)
835                         (t 0)))
836         value)
837       (char-db-decode-isolated-char name value)))
838     (if (and (= (charset-chars name) 94)
839              (= (charset-dimension name) 2))
840         (insert (format " [%02d-%02d]"
841                         (- (lsh value -8) 32)
842                         (- (logand value 255) 32))))
843     )
844    (t
845     (insert (format "(%-18s . %s)" name value))
846     ))
847   (insert line-breaking))
848
849 (defun char-db-insert-relation-feature (char name value line-breaking
850                                              ccss readable)
851   (insert (format "(%-18s%s " name line-breaking))
852   (let ((lbs (concat "\n" (make-string (current-column) ?\ )))
853         separator cell sources required-features
854         ret)
855     (while (consp value)
856       (setq cell (car value))
857       (if (integerp cell)
858           (setq cell (decode-char '=ucs cell)))
859       (cond
860        ((eq name '->subsumptive)
861         (when (or (not (some (lambda (atr)
862                                (get-char-attribute cell atr))
863                              char-db-ignored-attributes))
864                   (some (lambda (ccs)
865                           (encode-char cell ccs 'defined-only))
866                         ccss))
867           (if separator
868               (insert lbs))
869           (let ((char-db-ignored-attributes
870                  (cons '<-subsumptive
871                        char-db-ignored-attributes)))
872             (insert-char-attributes cell readable nil nil 'for-sub-node))
873           (setq separator lbs))
874         )
875        ((characterp cell)
876         (setq sources
877               (get-char-attribute
878                char (intern (format "%s*sources" name))))
879         (setq required-features nil)
880         (dolist (source sources)
881           (cond
882            ((memq source '(JP
883                            JP/Jouyou
884                            shinjigen shinjigen@1ed shinjigen@rev))
885             (setq required-features
886                   (union required-features
887                          '(=jis-x0208
888                            =jis-x0208@1990
889                            =jis-x0213-1@2000
890                            =jis-x0213-1@2004
891                            =jis-x0213-2
892                            =jis-x0212
893                            =jis-x0208@1983
894                            =jis-x0208@1978
895                            =shinjigen
896                            =shinjigen@1ed
897                            =shinjigen@rev
898                            =shinjigen/+p@rev))))
899            ((eq source 'CN)
900             (setq required-features
901                   (union required-features
902                          '(=gb2312
903                            =gb12345
904                            =iso-ir165)))))
905           (cond
906            ((find-charset (setq ret (intern (format "=%s" source))))
907             (setq required-features
908                   (cons ret required-features)))
909            (t (setq required-features
910                     (cons source required-features)))))
911         (cond ((string-match "@JP" (symbol-name name))
912                (setq required-features
913                      (union required-features
914                             '(=jis-x0208
915                               =jis-x0208@1990
916                               =jis-x0213-1-2000
917                               =jis-x0213-2-2000
918                               =jis-x0212
919                               =jis-x0208@1983
920                               =jis-x0208@1978))))
921               ((string-match "@CN" (symbol-name name))
922                (setq required-features
923                      (union required-features
924                             '(=gb2312
925                               =gb12345
926                               =iso-ir165)))))
927         (if separator
928             (insert lbs))
929         (if readable
930             (insert (format "%S" cell))
931           (char-db-insert-char-spec cell readable
932                                     nil
933                                     required-features))
934         (setq separator lbs))
935        ((consp cell)
936         (if separator
937             (insert lbs))
938         (if (consp (car cell))
939             (char-db-insert-char-spec cell readable)
940           (char-db-insert-char-reference cell readable))
941         (setq separator lbs))
942        (t
943         (if separator
944             (insert separator))
945         (insert (prin1-to-string cell))
946         (setq separator " ")))
947       (setq value (cdr value)))
948     (insert ")")
949     (insert line-breaking)))
950
951 (defun insert-char-attributes (char &optional readable attributes column
952                                     for-sub-node)
953   (unless column
954     (setq column (current-column)))
955   (let (name value ; has-long-ccs-name
956         rest
957         radical strokes
958         (line-breaking
959          (concat "\n" (make-string (1+ column) ?\ )))
960         lbs cell separator ret
961         key al cal
962         dest-ccss ; sources required-features
963         ccss)
964     (let (atr-d)
965       (setq attributes
966             (sort (if attributes
967                       (if (consp attributes)
968                           (progn
969                             (dolist (name attributes)
970                               (unless (memq name char-db-ignored-attributes)
971                                 (if (find-charset name)
972                                     (push name ccss))
973                                 (push name atr-d)))
974                             atr-d))
975                     (dolist (name (char-attribute-list))
976                       (unless (memq name char-db-ignored-attributes)
977                         (if (find-charset name)
978                             (push name ccss))
979                         (push name atr-d)))
980                     atr-d)
981                   #'char-attribute-name<)))
982     (insert "(")
983     (when (memq '<-subsumptive attributes)
984       (when (or readable (not for-sub-node))
985         (when (setq value (get-char-attribute char '<-subsumptive))
986           (char-db-insert-relation-feature char '<-subsumptive value
987                                            line-breaking
988                                            ccss readable)))
989       (setq attributes (delq '<-subsumptive attributes)))
990     (when (and (memq '<-denotational attributes)
991                (setq value (get-char-attribute char '<-denotational)))
992       (char-db-insert-relation-feature char '<-denotational value
993                                        line-breaking
994                                        ccss readable)
995       (setq attributes (delq '<-denotational attributes)))
996     (when (and (memq '<-denotational@component attributes)
997                (setq value (get-char-attribute char '<-denotational@component)))
998       (char-db-insert-relation-feature char '<-denotational@component value
999                                        line-breaking
1000                                        ccss readable)
1001       (setq attributes (delq '<-denotational@component attributes)))
1002     (when (and (memq '<-denotational@usage attributes)
1003                (setq value (get-char-attribute char '<-denotational@usage)))
1004       (char-db-insert-relation-feature char '<-denotational@usage value
1005                                        line-breaking
1006                                        ccss readable)
1007       (setq attributes (delq '<-denotational@usage attributes)))
1008     (when (and (memq 'name attributes)
1009                (setq value (get-char-attribute char 'name)))
1010       (insert (format
1011                (if (> (+ (current-column) (length value)) 48)
1012                    "(name . %S)%s"
1013                  "(name               . %S)%s")
1014                value line-breaking))
1015       (setq attributes (delq 'name attributes))
1016       )
1017     (when (and (memq 'name* attributes)
1018                (setq value (get-char-attribute char 'name*)))
1019       (insert (format
1020                (if (> (+ (current-column) (length value)) 48)
1021                    "(name* . %S)%s"
1022                  "(name*              . %S)%s")
1023                value line-breaking))
1024       (setq attributes (delq 'name* attributes))
1025       )
1026     (when (and (memq 'script attributes)
1027                (setq value (get-char-attribute char 'script)))
1028       (insert (format "(script\t\t%s)%s"
1029                       (mapconcat (function prin1-to-string)
1030                                  value " ")
1031                       line-breaking))
1032       (setq attributes (delq 'script attributes))
1033       )
1034     (dolist (name '(=>ucs =>ucs*))
1035       (when (and (memq name attributes)
1036                  (setq value (get-char-attribute char name)))
1037         (insert (format "(%-18s . #x%04X)\t; %c%s"
1038                         name value (decode-char '=ucs value)
1039                         line-breaking))
1040         (setq attributes (delq name attributes))))
1041     (dolist (name '(=>ucs@gb =>ucs@big5))
1042       (when (and (memq name attributes)
1043                  (setq value (get-char-attribute char name)))
1044         (insert (format "(%-18s . #x%04X)\t; %c%s"
1045                         name value
1046                         (decode-char (intern
1047                                       (concat "="
1048                                               (substring
1049                                                (symbol-name name) 2)))
1050                                      value)
1051                         line-breaking))
1052         (setq attributes (delq name attributes))
1053         ))
1054     ;; (dolist (name '(=>daikanwa))
1055     ;;   (when (and (memq name attributes)
1056     ;;              (setq value (get-char-attribute char name)))
1057     ;;     (insert
1058     ;;      (if (integerp value)
1059     ;;          (format "(%-18s . %05d)\t; %c%s"
1060     ;;                  name value (decode-char '=daikanwa value)
1061     ;;                  line-breaking)
1062     ;;        (format "(%-18s %s)\t; %c%s"
1063     ;;                name
1064     ;;                (mapconcat (function prin1-to-string)
1065     ;;                           value " ")
1066     ;;                (char-representative-of-daikanwa char)
1067     ;;                line-breaking)))
1068     ;;     (setq attributes (delq name attributes))))
1069     (when (and (memq 'general-category attributes)
1070                (setq value (get-char-attribute char 'general-category)))
1071       (insert (format
1072                "(general-category\t%s) ; %s%s"
1073                (mapconcat (lambda (cell)
1074                             (format "%S" cell))
1075                           value " ")
1076                (cond ((rassoc value unidata-normative-category-alist)
1077                       "Normative Category")
1078                      ((rassoc value unidata-informative-category-alist)
1079                       "Informative Category")
1080                      (t
1081                       "Unknown Category"))
1082                line-breaking))
1083       (setq attributes (delq 'general-category attributes))
1084       )
1085     (when (and (memq 'bidi-category attributes)
1086                (setq value (get-char-attribute char 'bidi-category)))
1087       (insert (format "(bidi-category\t. %S)%s"
1088                       value
1089                       line-breaking))
1090       (setq attributes (delq 'bidi-category attributes))
1091       )
1092     (unless (or (not (memq 'mirrored attributes))
1093                 (eq (setq value (get-char-attribute char 'mirrored 'empty))
1094                     'empty))
1095       (insert (format "(mirrored\t\t. %S)%s"
1096                       value
1097                       line-breaking))
1098       (setq attributes (delq 'mirrored attributes))
1099       )
1100     (cond
1101      ((and (memq 'decimal-digit-value attributes)
1102            (setq value (get-char-attribute char 'decimal-digit-value)))
1103       (insert (format "(decimal-digit-value . %S)%s"
1104                       value
1105                       line-breaking))
1106       (setq attributes (delq 'decimal-digit-value attributes))
1107       (when (and (memq 'digit-value attributes)
1108                  (setq value (get-char-attribute char 'digit-value)))
1109         (insert (format "(digit-value\t . %S)%s"
1110                         value
1111                         line-breaking))
1112         (setq attributes (delq 'digit-value attributes))
1113         )
1114       (when (and (memq 'numeric-value attributes)
1115                  (setq value (get-char-attribute char 'numeric-value)))
1116         (insert (format "(numeric-value\t . %S)%s"
1117                         value
1118                         line-breaking))
1119         (setq attributes (delq 'numeric-value attributes))
1120         )
1121       )
1122      (t
1123       (when (and (memq 'digit-value attributes)
1124                  (setq value (get-char-attribute char 'digit-value)))
1125         (insert (format "(digit-value\t. %S)%s"
1126                         value
1127                         line-breaking))
1128         (setq attributes (delq 'digit-value attributes))
1129         )
1130       (when (and (memq 'numeric-value attributes)
1131                  (setq value (get-char-attribute char 'numeric-value)))
1132         (insert (format "(numeric-value\t. %S)%s"
1133                         value
1134                         line-breaking))
1135         (setq attributes (delq 'numeric-value attributes))
1136         )))
1137     (when (and (memq 'iso-10646-comment attributes)
1138                (setq value (get-char-attribute char 'iso-10646-comment)))
1139       (insert (format "(iso-10646-comment\t. %S)%s"
1140                       value
1141                       line-breaking))
1142       (setq attributes (delq 'iso-10646-comment attributes))
1143       )
1144     (when (and (memq 'morohashi-daikanwa attributes)
1145                (setq value (get-char-attribute char 'morohashi-daikanwa)))
1146       (insert (format "(morohashi-daikanwa\t%s)%s"
1147                       (mapconcat (function prin1-to-string) value " ")
1148                       line-breaking))
1149       (setq attributes (delq 'morohashi-daikanwa attributes))
1150       )
1151     (setq radical nil
1152           strokes nil)
1153     (when (and (memq 'ideographic-radical attributes)
1154                (setq value (get-char-attribute char 'ideographic-radical)))
1155       (setq radical value)
1156       (insert (format "(ideographic-radical . %S)\t; %c%s"
1157                       radical
1158                       (ideographic-radical radical)
1159                       line-breaking))
1160       (setq attributes (delq 'ideographic-radical attributes))
1161       )
1162     (when (and (memq 'shuowen-radical attributes)
1163                (setq value (get-char-attribute char 'shuowen-radical)))
1164       (insert (format "(shuowen-radical\t. %S)\t; %c%s"
1165                       value
1166                       (shuowen-radical value)
1167                       line-breaking))
1168       (setq attributes (delq 'shuowen-radical attributes))
1169       )
1170     (let (key)
1171       (dolist (domain
1172                (append
1173                 char-db-feature-domains
1174                 (let (dest domain)
1175                   (dolist (feature (char-attribute-list))
1176                     (setq feature (symbol-name feature))
1177                     (when (string-match
1178                            "\\(radical\\|strokes\\)@\\([^@*]+\\)\\(\\*\\|$\\)"
1179                            feature)
1180                       (setq domain (intern (match-string 2 feature)))
1181                      (unless (memq domain dest)
1182                        (setq dest (cons domain dest)))))
1183                   (sort dest #'string<))))
1184         (setq key (intern (format "%s@%s" 'ideographic-radical domain)))
1185         (when (and (memq key attributes)
1186                    (setq value (get-char-attribute char key)))
1187           (setq radical value)
1188           (insert (format "(%s . %S)\t; %c%s"
1189                           key
1190                           radical
1191                           (ideographic-radical radical)
1192                           line-breaking))
1193           (setq attributes (delq key attributes))
1194           )
1195         (setq key (intern (format "%s@%s" 'ideographic-strokes domain)))
1196         (when (and (memq key attributes)
1197                    (setq value (get-char-attribute char key)))
1198           (setq strokes value)
1199           (insert (format "(%s . %S)%s"
1200                           key
1201                           strokes
1202                           line-breaking))
1203           (setq attributes (delq key attributes))
1204           )
1205         (setq key (intern (format "%s@%s" 'total-strokes domain)))
1206         (when (and (memq key attributes)
1207                    (setq value (get-char-attribute char key)))
1208           (insert (format "(%s       . %S)%s"
1209                           key
1210                           value
1211                           line-breaking))
1212           (setq attributes (delq key attributes))
1213           )
1214         (dolist (feature '(ideographic-radical
1215                            ideographic-strokes
1216                            total-strokes))
1217           (setq key (intern (format "%s@%s*sources" feature domain)))
1218           (when (and (memq key attributes)
1219                      (setq value (get-char-attribute char key)))
1220             (insert (format "(%s%s" key line-breaking))
1221             (dolist (cell value)
1222               (insert (format " %s" cell)))
1223             (insert ")")
1224             (insert line-breaking)
1225             (setq attributes (delq key attributes))
1226             ))
1227         ))
1228     (when (and (memq 'ideographic-strokes attributes)
1229                (setq value (get-char-attribute char 'ideographic-strokes)))
1230       (setq strokes value)
1231       (insert (format "(ideographic-strokes . %S)%s"
1232                       strokes
1233                       line-breaking))
1234       (setq attributes (delq 'ideographic-strokes attributes))
1235       )
1236     (when (and (memq 'kangxi-radical attributes)
1237                (setq value (get-char-attribute char 'kangxi-radical)))
1238       (unless (eq value radical)
1239         (insert (format "(kangxi-radical\t . %S)\t; %c%s"
1240                         value
1241                         (ideographic-radical value)
1242                         line-breaking))
1243         (or radical
1244             (setq radical value)))
1245       (setq attributes (delq 'kangxi-radical attributes))
1246       )
1247     (when (and (memq 'kangxi-strokes attributes)
1248                (setq value (get-char-attribute char 'kangxi-strokes)))
1249       (unless (eq value strokes)
1250         (insert (format "(kangxi-strokes\t . %S)%s"
1251                         value
1252                         line-breaking))
1253         (or strokes
1254             (setq strokes value)))
1255       (setq attributes (delq 'kangxi-strokes attributes))
1256       )
1257     ;; (when (and (memq 'japanese-radical attributes)
1258     ;;            (setq value (get-char-attribute char 'japanese-radical)))
1259     ;;   (unless (eq value radical)
1260     ;;     (insert (format "(japanese-radical\t . %S)\t; %c%s"
1261     ;;                     value
1262     ;;                     (ideographic-radical value)
1263     ;;                     line-breaking))
1264     ;;     (or radical
1265     ;;         (setq radical value)))
1266     ;;   (setq attributes (delq 'japanese-radical attributes))
1267     ;;   )
1268     (when (and (memq 'japanese-strokes attributes)
1269                (setq value (get-char-attribute char 'japanese-strokes)))
1270       (unless (eq value strokes)
1271         (insert (format "(japanese-strokes\t . %S)%s"
1272                         value
1273                         line-breaking))
1274         (or strokes
1275             (setq strokes value)))
1276       (setq attributes (delq 'japanese-strokes attributes))
1277       )
1278     (when (and (memq 'cns-radical attributes)
1279                (setq value (get-char-attribute char 'cns-radical)))
1280       (insert (format "(cns-radical\t . %S)\t; %c%s"
1281                       value
1282                       (ideographic-radical value)
1283                       line-breaking))
1284       (setq attributes (delq 'cns-radical attributes))
1285       )
1286     (when (and (memq 'cns-strokes attributes)
1287                (setq value (get-char-attribute char 'cns-strokes)))
1288       (unless (eq value strokes)
1289         (insert (format "(cns-strokes\t . %S)%s"
1290                         value
1291                         line-breaking))
1292         (or strokes
1293             (setq strokes value)))
1294       (setq attributes (delq 'cns-strokes attributes))
1295       )
1296     ;; (when (and (memq 'ideographic- attributes)
1297     ;;            (setq value (get-char-attribute char 'ideographic-)))
1298     ;;   (insert "(ideographic-       ")
1299     ;;   (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1300     ;;         separator nil)
1301     ;;   (while (consp value)
1302     ;;     (setq cell (car value))
1303     ;;     (if (integerp cell)
1304     ;;         (setq cell (decode-char '=ucs cell)))
1305     ;;     (cond ((characterp cell)
1306     ;;            (if separator
1307     ;;                (insert lbs))
1308     ;;            (if readable
1309     ;;                (insert (format "%S" cell))
1310     ;;              (char-db-insert-char-spec cell readable))
1311     ;;            (setq separator lbs))
1312     ;;           ((consp cell)
1313     ;;            (if separator
1314     ;;                (insert lbs))
1315     ;;            (if (consp (car cell))
1316     ;;                (char-db-insert-char-spec cell readable)
1317     ;;              (char-db-insert-char-reference cell readable))
1318     ;;            (setq separator lbs))
1319     ;;           (t
1320     ;;            (if separator
1321     ;;                (insert separator))
1322     ;;            (insert (prin1-to-string cell))
1323     ;;            (setq separator " ")))
1324     ;;     (setq value (cdr value)))
1325     ;;   (insert ")")
1326     ;;   (insert line-breaking)
1327     ;;   (setq attributes (delq 'ideographic- attributes)))
1328     (when (and (memq 'total-strokes attributes)
1329                (setq value (get-char-attribute char 'total-strokes)))
1330       (insert (format "(total-strokes       . %S)%s"
1331                       value
1332                       line-breaking))
1333       (setq attributes (delq 'total-strokes attributes))
1334       )
1335     ;; (when (and (memq '->ideograph attributes)
1336     ;;            (setq value (get-char-attribute char '->ideograph)))
1337     ;;   (insert (format "(->ideograph\t%s)%s"
1338     ;;                   (mapconcat (lambda (code)
1339     ;;                                (cond ((symbolp code)
1340     ;;                                       (symbol-name code))
1341     ;;                                      ((integerp code)
1342     ;;                                       (format "#x%04X" code))
1343     ;;                                      (t
1344     ;;                                       (format "%s %S"
1345     ;;                                               line-breaking code))))
1346     ;;                              value " ")
1347     ;;                   line-breaking))
1348     ;;   (setq attributes (delq '->ideograph attributes))
1349     ;;   )
1350     ;; (when (and (memq '->decomposition attributes)
1351     ;;            (setq value (get-char-attribute char '->decomposition)))
1352     ;;   (insert (format "(->decomposition\t%s)%s"
1353     ;;                   (mapconcat (lambda (code)
1354     ;;                                (cond ((symbolp code)
1355     ;;                                       (symbol-name code))
1356     ;;                                      ((characterp code)
1357     ;;                                       (if readable
1358     ;;                                           (format "%S" code)
1359     ;;                                         (format "#x%04X"
1360     ;;                                                 (char-int code))
1361     ;;                                         ))
1362     ;;                                      ((integerp code)
1363     ;;                                       (format "#x%04X" code))
1364     ;;                                      (t
1365     ;;                                       (format "%s%S" line-breaking code))))
1366     ;;                              value " ")
1367     ;;                   line-breaking))
1368     ;;   (setq attributes (delq '->decomposition attributes))
1369     ;;   )
1370     (if (equal (get-char-attribute char '->titlecase)
1371                (get-char-attribute char '->uppercase))
1372         (setq attributes (delq '->titlecase attributes)))
1373     (when (and (memq '->mojikyo attributes)
1374                (setq value (get-char-attribute char '->mojikyo)))
1375       (insert (format "(->mojikyo\t\t. %06d)\t; %c%s"
1376                       value (decode-char 'mojikyo value)
1377                       line-breaking))
1378       (setq attributes (delq '->mojikyo attributes))
1379       )
1380     (unless readable
1381       (dolist (ignored '(composition
1382                          ->denotational <-subsumptive ->ucs-unified
1383                          ;; ->ideographic-component-forms
1384                          ))
1385         (setq attributes (delq ignored attributes))))
1386     (while attributes
1387       (setq name (car attributes))
1388       (unless (eq (setq value (get-char-attribute char name 'value-is-empty))
1389                   'value-is-empty)
1390         (cond ((setq ret (find-charset name))
1391                (setq name (charset-name ret))
1392                (when (not (memq name dest-ccss))
1393                  (setq dest-ccss (cons name dest-ccss))
1394                  (char-db-insert-ccs-feature name value line-breaking))
1395                )
1396               ((string-match "^=>ucs@" (symbol-name name))
1397                (insert (format "(%-18s . #x%04X)\t; %c%s"
1398                                name value (decode-char '=ucs value)
1399                                line-breaking))
1400                )
1401               ((eq name 'jisx0208-1978/4X)
1402                (insert (format "(%-18s . #x%04X)%s"
1403                                name value
1404                                line-breaking))
1405                )
1406               ((and
1407                 (not readable)
1408                 (not (eq name '->subsumptive))
1409                 (not (eq name '->uppercase))
1410                 (not (eq name '->lowercase))
1411                 (not (eq name '->titlecase))
1412                 (not (eq name '->canonical))
1413                 (not (eq name '->Bopomofo))
1414                 (not (eq name '->mistakable))
1415                 (not (eq name '->ideographic-variants))
1416                 (null (get-char-attribute
1417                        char (intern (format "%s*sources" name))))
1418                 (not (string-match "\\*sources$" (symbol-name name)))
1419                 (null (get-char-attribute
1420                        char (intern (format "%s*note" name))))
1421                 (not (string-match "\\*note$" (symbol-name name)))
1422                 (or (eq name '<-identical)
1423                     (eq name '<-uppercase)
1424                     (eq name '<-lowercase)
1425                     (eq name '<-titlecase)
1426                     (eq name '<-canonical)
1427                     (eq name '<-ideographic-variants)
1428                     ;; (eq name '<-synonyms)
1429                     (string-match "^<-synonyms" (symbol-name name))
1430                     (eq name '<-mistakable)
1431                     (when (string-match "^->" (symbol-name name))
1432                       (cond
1433                        ((string-match "^->fullwidth" (symbol-name name))
1434                         (not (and (consp value)
1435                                   (characterp (car value))
1436                                   (encode-char
1437                                    (car value) '=ucs 'defined-only)))
1438                         )
1439                        (t)))
1440                     ))
1441                )
1442               ((or (eq name 'ideographic-structure)
1443                    (eq name 'ideographic-structure@apparent)
1444                    (eq name 'ideographic-structure@apparent/leftmost)
1445                    (eq name 'ideographic-structure@apparent/rightmost)
1446                    (eq name 'ideographic-combination)
1447                    ;; (eq name 'ideographic-)
1448                    (eq name '=decomposition)
1449                    (char-feature-base-name= '=decomposition name)
1450                    (char-feature-base-name= '=>decomposition name)
1451                    ;; (string-match "^=>*decomposition\\(@[^*]+\\)?$"
1452                    ;;               (symbol-name name))
1453                    (string-match "^\\(->\\|<-\\)[^*]*$" (symbol-name name))
1454                    (string-match "^\\(->\\|<-\\)[^*]*\\*sources$"
1455                                  (symbol-name name))
1456                    )
1457                (char-db-insert-relation-feature char name value
1458                                                 line-breaking
1459                                                 ccss readable))
1460               ;; ((memq name '(ideograph=
1461               ;;               original-ideograph-of
1462               ;;               ancient-ideograph-of
1463               ;;               vulgar-ideograph-of
1464               ;;               wrong-ideograph-of
1465               ;;               ;; simplified-ideograph-of
1466               ;;               ideographic-variants
1467               ;;               ;; ideographic-different-form-of
1468               ;;               ))
1469               ;;  (insert (format "(%-18s%s " name line-breaking))
1470               ;;  (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1471               ;;        separator nil)
1472               ;;  (while (consp value)
1473               ;;    (setq cell (car value))
1474               ;;    (if (and (consp cell)
1475               ;;             (consp (car cell)))
1476               ;;        (progn
1477               ;;          (if separator
1478               ;;              (insert lbs))
1479               ;;          (char-db-insert-alist cell readable)
1480               ;;          (setq separator lbs))
1481               ;;      (if separator
1482               ;;          (insert separator))
1483               ;;      (insert (prin1-to-string cell))
1484               ;;      (setq separator " "))
1485               ;;    (setq value (cdr value)))
1486               ;;  (insert ")")
1487               ;;  (insert line-breaking))
1488               ((consp value)
1489                (insert (format "(%-18s " name))
1490                (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1491                      separator nil)
1492                (while (consp value)
1493                  (setq cell (car value))
1494                  (if (and (consp cell)
1495                           (consp (car cell))
1496                           (setq ret (condition-case nil
1497                                         (find-char cell)
1498                                       (error nil))))
1499                      (progn
1500                        (setq rest cell
1501                              al nil
1502                              cal nil)
1503                        (while rest
1504                          (setq key (car (car rest)))
1505                          (if (find-charset key)
1506                              (setq cal (cons key cal))
1507                            (setq al (cons key al)))
1508                          (setq rest (cdr rest)))
1509                        (if separator
1510                            (insert lbs))
1511                        (insert-char-attributes ret
1512                                                readable
1513                                                al ; cal
1514                                                nil 'for-sub-node)
1515                        (setq separator lbs))
1516                    (setq ret (prin1-to-string cell))
1517                    (if separator
1518                        (if (< (+ (current-column)
1519                                  (length ret)
1520                                  (length separator))
1521                               76)
1522                            (insert separator)
1523                          (insert lbs)))
1524                    (insert ret)
1525                    (setq separator " "))
1526                  (setq value (cdr value)))
1527                (insert ")")
1528                (insert line-breaking))
1529               (t
1530                (insert (format "(%-18s" name))
1531                (setq ret (prin1-to-string value))
1532                (unless (< (+ (current-column)
1533                              (length ret)
1534                              3)
1535                           76)
1536                  (insert line-breaking))
1537                (insert " . " ret ")" line-breaking)
1538                ;; (insert (format "(%-18s . %S)%s"
1539                ;;                 name value
1540                ;;                 line-breaking))
1541                )
1542               ))
1543       (setq attributes (cdr attributes)))
1544     (insert ")")))
1545
1546 (defun insert-char-data (char &optional readable
1547                               attributes)
1548   (save-restriction
1549     (narrow-to-region (point)(point))
1550     (insert "(define-char
1551   '")
1552     (insert-char-attributes char readable attributes)
1553     (insert ")\n")
1554     (goto-char (point-min))
1555     (while (re-search-forward "[ \t]+$" nil t)
1556       (replace-match ""))
1557     ;; from tabify.
1558     (goto-char (point-min))
1559     (while (re-search-forward "[ \t][ \t][ \t]*" nil t)
1560       (let ((column (current-column))
1561             (indent-tabs-mode t))
1562         (delete-region (match-beginning 0) (point))
1563         (indent-to column)))
1564     (goto-char (point-max))
1565     ;; (tabify (point-min)(point-max))
1566     ))
1567
1568 (defun insert-char-data-with-variant (char &optional printable
1569                                            no-ucs-unified
1570                                            script excluded-script)
1571   (insert-char-data char printable)
1572   (let ((variants (char-variants char))
1573         rest
1574         variant vs ret)
1575     (setq variants (sort variants #'<))
1576     (setq rest variants)
1577     (setq variants (cons char variants))
1578     (while rest
1579       (setq variant (car rest))
1580       (unless (get-char-attribute variant '<-subsumptive)
1581         (if (and (or (null script)
1582                      (null (setq vs (get-char-attribute variant 'script)))
1583                      (memq script vs))
1584                  (or (null excluded-script)
1585                      (null (setq vs (get-char-attribute variant 'script)))
1586                      (not (memq excluded-script vs))))
1587             (unless (and no-ucs-unified (get-char-attribute variant '=ucs))
1588               (insert-char-data variant printable)
1589               (if (setq ret (char-variants variant))
1590                   (while ret
1591                     (or (memq (car ret) variants)
1592                         ;; (get-char-attribute (car ret) '<-subsumptive)
1593                         (setq rest (nconc rest (list (car ret)))))
1594                     (setq ret (cdr ret)))))))
1595       (setq rest (cdr rest)))))
1596
1597 (defun insert-char-range-data (min max &optional script excluded-script)
1598   (let ((code min)
1599         char)
1600     (while (<= code max)
1601       (setq char (decode-char '=ucs code))
1602       (if (encode-char char '=ucs 'defined-only)
1603           (insert-char-data-with-variant char nil 'no-ucs-unified
1604                                          script excluded-script))
1605       (setq code (1+ code)))))
1606
1607 (defun write-char-range-data-to-file (min max file
1608                                           &optional script excluded-script)
1609   (let ((coding-system-for-write char-db-file-coding-system))
1610     (with-temp-buffer
1611       (insert (format ";; -*- coding: %s -*-\n"
1612                       char-db-file-coding-system))
1613       (insert-char-range-data min max script excluded-script)
1614       (write-region (point-min)(point-max) file))))
1615
1616 (defvar what-character-original-window-configuration)
1617
1618 ;;;###autoload
1619 (defun what-char-definition (char)
1620   (interactive (list (char-after)))
1621   (let ((buf (get-buffer-create "*Character Description*"))
1622         (the-buf (current-buffer))
1623         (win-conf (current-window-configuration)))
1624     (pop-to-buffer buf)
1625     (make-local-variable 'what-character-original-window-configuration)
1626     (setq what-character-original-window-configuration win-conf)
1627     (setq buffer-read-only nil)
1628     (erase-buffer)
1629     (condition-case err
1630         (progn
1631           (insert-char-data-with-variant char 'printable)
1632           (unless (char-attribute-alist char)
1633             (insert (format ";; = %c\n"
1634                             (let* ((rest (split-char char))
1635                                    (ccs (pop rest))
1636                                    (code (pop rest)))
1637                               (while rest
1638                                 (setq code (logior (lsh code 8)
1639                                                    (pop rest))))
1640                               (decode-char ccs code)))))
1641           ;; (char-db-update-comment)
1642           (set-buffer-modified-p nil)
1643           (view-mode the-buf (lambda (buf)
1644                                (set-window-configuration
1645                                 what-character-original-window-configuration)
1646                                ))
1647           (goto-char (point-min)))
1648       (error (progn
1649                (set-window-configuration
1650                 what-character-original-window-configuration)
1651                (signal (car err) (cdr err)))))))
1652
1653
1654 ;;; @ end
1655 ;;;
1656
1657 (provide 'char-db-util)
1658
1659 ;;; char-db-util.el ends here