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