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