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