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