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