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