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