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