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