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