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