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