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