(A-compU+706B): New abstract component.
[chise/xemacs-chise.git.1] / 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 MORIOKA Tomohiko.
5
6 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
7 ;; Keywords: CHISE, Character Database, ISO/IEC 10646, UCS, Unicode, MULE.
8
9 ;; This file is part of XEmacs CHISE.
10
11 ;; XEmacs CHISE is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; XEmacs CHISE is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs CHISE; see the file COPYING.  If not, write to
23 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Code:
27
28 (require 'chise-subr)
29 (require 'ideograph-subr)
30
31 (defconst unidata-normative-category-alist
32   '(("Lu" letter        uppercase)
33     ("Ll" letter        lowercase)
34     ("Lt" letter        titlecase)
35     ("Mn" mark          non-spacing)
36     ("Mc" mark          spacing-combining)
37     ("Me" mark          enclosing)
38     ("Nd" number        decimal-digit)
39     ("Nl" number        letter)
40     ("No" number        other)
41     ("Zs" separator     space)
42     ("Zl" separator     line)
43     ("Zp" separator     paragraph)
44     ("Cc" other         control)
45     ("Cf" other         format)
46     ("Cs" other         surrogate)
47     ("Co" other         private-use)
48     ("Cn" other         not-assigned)))
49
50 (defconst unidata-informative-category-alist
51   '(("Lm" letter        modifier)
52     ("Lo" letter        other)
53     ("Pc" punctuation   connector)
54     ("Pd" punctuation   dash)
55     ("Ps" punctuation   open)
56     ("Pe" punctuation   close)
57     ("Pi" punctuation   initial-quote)
58     ("Pf" punctuation   final-quote)
59     ("Po" punctuation   other)
60     ("Sm" symbol        math)
61     ("Sc" symbol        currency)
62     ("Sk" symbol        modifier)
63     ("So" symbol        other)
64     ))
65
66 (defconst shuowen-radicals
67   [?一 ?上 ?示 ?三 ?王 ?玉 ?玨 ?气 ?士 ?丨 ; 010
68    ?屮 ?艸 ?蓐 ?茻 ?小 ?八 ?釆 ?半 ?牛 ?犛 ; 020
69    ?告 ?口 ?凵 ?吅 ?哭 ?走 ?止 ?癶 ?步 ?此 ; 030
70    ?正 ?是 ?辵 ?彳 ?廴 ?㢟 ?行 ?齒 ?牙 ?足 ; 040
71    ?疋 ?品 ?龠 ?冊 ?㗊 ?舌 ?干 ?谷 ?只 ?㕯 ; 050
72    ?句 ?丩 ?古 ?十 ?卅 ?言 ?誩 ?音 ?䇂 ?丵 ; 060
73    ?菐 ?𠬞 ?𠬜 ?共 ?異 ?舁 ?𦥑 ?䢅 ?爨 ?革 ; 070
74    ?鬲 ?䰜 ?爪 ?𠃨 ?鬥 ?又 ?𠂇 ?㕜 ?支 ?𦘒 ; 080
75    ?聿 ?畫 ?隶 ?臤 ?臣 ?殳 ?殺 ?𠘧 ?寸 ?皮 ; 090
76    ?㼱 ?攴 ?敎 ?卜 ?用 ?爻 ?㸚 ?𥄎 ?目 ?䀠 ; 100
77    ?眉 ?盾 ?自 ?白 ?鼻 ?皕 ?習 ?羽 ?隹 ?奞 ; 110
78    ?萑 ?𦫳 ?苜 ?羊 ?羴 ?瞿 ?雔 ?雥 ?鳥 ?烏 ; 120
79    ?𠦒 ?冓 ?幺 ?𢆶 ?叀 ?玄 ?予 ?放 ?𠬪 ?𣦼 ; 130
80    ?歺 ?死 ?冎 ?骨 ?肉 ?筋 ?刀 ?刃 ?㓞 ?丰 ; 140
81    ?耒 ?𧢲 ?竹 ?箕 ?丌 ?左 ?工 ?㠭 ?巫 ?甘 ; 150
82    ?曰 ?乃 ?丂 ?可 ?兮 ?号 ?亏 ?旨 ?喜 ?壴 ; 160
83 ;  ?旨 ?曰 ?乃 ?丂 ?可 ?兮 ?号 ?亏 ?喜 ?壴 ; 160
84    ?鼓 ?豈 ?豆 ?豊 ?豐 ?䖒 ?虍 ?虎 ?虤 ?皿 ; 170
85    ?𠙴 ?去 ?血 ?丶 ?丹 ?青 ?井 ?皀 ?鬯 ?食 ; 180
86    ?亼 ?會 ?倉 ?入 ?缶 ?矢 ?高 ?冂 ?𩫏 ?京 ; 190
87    ?亯 ?𣆪 ?畗 ?㐭 ?嗇 ?來 ?麥 ?夊 ?舛 ?䑞 ; 200
88    ?韋 ?弟 ?夂 ?久 ?桀 ?木 ?東 ?林 ?才 ?叒 ; 210
89    ?之 ?帀 ?出 ?𣎵 ?生 ?乇 ?𠂹 ?𠌶 ?華 ?𥝌 ; 220
90    ?稽 ?巢 ?桼 ?束 ?㯻 ?囗 ?員 ?貝 ?邑 ?𨛜 ; 230
91    ?日 ?旦 ?倝 ?㫃 ?冥 ?晶 ?月 ?有 ?明 ?囧 ; 240
92    ?夕 ?多 ?毌 ?𢎘 ?𣐺 ?卣 ?齊 ?朿 ?片 ?鼎 ; 250
93    ?克 ?彔 ?禾 ?秝 ?黍 ?香 ?米 ?毇 ?臼 ?凶 ; 260
94    ?𣎳 ?林 ?麻 ?尗 ?耑 ?韭 ?瓜 ?瓠 ?宀 ?宮 ; 270
95    ?呂 ?穴 ?㝱 ?𤕫 ?冖 ?𠔼 ?冃 ?㒳 ?网 ?襾 ; 280
96    ?巾 ?巿 ?帛 ?白 ?㡀 ?黹 ?人 ?𠤎 ?匕 ?从 ; 290
97    ?比 ?北 ?丘 ?㐺 ?𡈼 ?重 ?臥 ?身 ?㐆 ?衣 ; 300
98    ?裘 ?老 ?毛 ?毳 ?尸 ?尺 ?尾 ?履 ?舟 ?方 ; 310
99    ?儿 ?兄 ?兂 ?皃 ?𠑹 ?先 ?秃 ?見 ?覞 ?欠 ; 320
100    ?㱃 ?㳄 ?旡 ?頁 ?𦣻 ?面 ?丏 ?首 ?𥄉 ?須 ; 330
101    ?彡 ?彣 ?文 ?髟 ?后 ?司 ?卮 ?卩 ?印 ?色 ; 340
102    ?𠨍 ?辟 ?勹 ?包 ?茍 ?鬼 ?甶 ?厶 ?嵬 ?山 ; 350
103    ?屾 ?屵 ?广 ?厂 ?丸 ?危 ?石 ?長 ?勿 ?冄 ; 360
104    ?而 ?豕 ?㣇 ?彑 ?豚 ?豸 ?𤉡 ?易 ?象 ?馬 ; 370
105    ?𢊁 ?鹿 ?麤 ?㲋 ?兔 ?萈 ?犬 ?㹜 ?鼠 ?能 ; 380
106    ?熊 ?火 ?炎 ?黑 ?囪 ?焱 ?炙 ?赤 ?大 ?亦 ; 390
107    ])
108
109 (defun shuowen-radical (number)
110   (aref shuowen-radicals (1- number)))
111
112 (defvar char-db-file-coding-system 'utf-8-mcs-er)
113
114 (defvar char-db-ignored-attributes '(ideographic-products))
115
116 (defvar char-db-coded-charset-priority-list
117   '(ascii
118     control-1
119     latin-iso8859-1
120     latin-iso8859-2
121     latin-iso8859-3
122     latin-iso8859-4
123     latin-iso8859-9
124     latin-jisx0201
125     cyrillic-iso8859-5
126     greek-iso8859-7
127     thai-tis620
128     ;; =mj
129     ;; =adobe-japan1-0
130     ;; =adobe-japan1-1
131     ;; =adobe-japan1-2
132     ;; =adobe-japan1-3
133     ;; =adobe-japan1-4
134     ;; =adobe-japan1-5
135     ;; =adobe-japan1-6
136     =jis-x0208
137     =jis-x0208@1978
138     =jis-x0208@1983
139     japanese-jisx0212
140     chinese-gb2312
141     =jis-x0208@1990
142     chinese-cns11643-1
143     chinese-cns11643-2
144     chinese-cns11643-3
145     chinese-cns11643-4
146     chinese-cns11643-5
147     chinese-cns11643-6
148     chinese-cns11643-7
149     =jis-x0213-1
150     =jis-x0213-1@2000
151     =jis-x0213-1@2004
152     =jis-x0213-2
153     korean-ksc5601
154     chinese-isoir165
155     katakana-jisx0201
156     hebrew-iso8859-8
157     chinese-gb12345
158     latin-viscii
159     ethiopic-ucs
160     =big5-cdp
161     =hanyo-denshi/ja
162     =hanyo-denshi/jb
163     =hanyo-denshi/jc
164     =hanyo-denshi/jd
165     =hanyo-denshi/ft
166     =hanyo-denshi/ia
167     =hanyo-denshi/ib
168     =hanyo-denshi/hg
169     =hanyo-denshi/jt
170     =hanyo-denshi/ks
171     =hanyo-denshi/tk
172     ;; ==mj
173     ;; ==adobe-japan1-0
174     ;; ==adobe-japan1-1
175     ;; ==adobe-japan1-2
176     ;; ==adobe-japan1-3
177     ;; ==adobe-japan1-4
178     ;; ==adobe-japan1-5
179     ;; ==adobe-japan1-6
180     ==jis-x0208
181     ==jis-x0213-1
182     ==jis-x0213-2
183     ==jis-x0212
184     ==hanyo-denshi/ja
185     ==hanyo-denshi/jb
186     ==hanyo-denshi/jc
187     ==hanyo-denshi/ft
188     ==hanyo-denshi/ia
189     ==hanyo-denshi/ib
190     ==hanyo-denshi/hg
191     ==hanyo-denshi/jt
192     ==hanyo-denshi/ks
193     ==hanyo-denshi/tk
194     =gt
195     =gt-k
196     =daikanwa
197     =daikanwa@rev2
198     =daikanwa@rev1
199     =daikanwa/+p
200     ==daikanwa
201     =cbeta
202     ideograph-hanziku-1
203     ideograph-hanziku-2
204     ideograph-hanziku-3
205     ideograph-hanziku-4
206     ideograph-hanziku-5
207     ideograph-hanziku-6
208     ideograph-hanziku-7
209     ideograph-hanziku-8
210     ideograph-hanziku-9
211     ideograph-hanziku-10
212     ideograph-hanziku-11
213     ideograph-hanziku-12
214     ;; =>>>adobe-japan1-0
215     ;; =>>>adobe-japan1-1
216     ;; =>>>adobe-japan1-2
217     ;; =>>>adobe-japan1-3
218     ;; =>>>adobe-japan1-4
219     ;; =>>>adobe-japan1-5
220     ;; =>>>adobe-japan1-6
221     ;; =>>>jis-x0208
222     ;; =>>>jis-x0213-1
223     ;; =>>>jis-x0213-2
224     ;; =>>>hanyo-denshi/ja
225     ;; =>>>hanyo-denshi/jb
226     ;; =>>>hanyo-denshi/jc
227     ;; =>>>hanyo-denshi/ft
228     ;; =>>>hanyo-denshi/ib
229     ;; =>>>hanyo-denshi/hg
230     ;; =>>>hanyo-denshi/jt
231     ;; =>>>hanyo-denshi/ks
232     ;; =>>>gt
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-1@2000
243     =>>jis-x0213-1@2004
244     =>>jis-x0213-2
245     =>>jis-x0208@1978
246     =>>hanyo-denshi/ft
247     =>>hanyo-denshi/jt
248     =>>hanyo-denshi/ks
249     =>>gt
250     =>>daikanwa
251     =>>cbeta
252     =+>jis-x0208
253     =+>jis-x0213-1
254     =+>jis-x0213-2
255     =+>adobe-japan1-0
256     =+>adobe-japan1-1
257     =+>adobe-japan1-2
258     =+>adobe-japan1-3
259     =+>adobe-japan1-4
260     =+>adobe-japan1-5
261     =+>adobe-japan1-6
262     =+>jis-x0208@1978
263     =>jis-x0208
264     =>jis-x0208@1997
265     =>jis-x0213-1
266     =>jis-x0213-1@2000
267     =>jis-x0213-1@2004
268     =>jis-x0213-2
269     ==>ucs@bucs
270     =>iwds-1
271     =>ucs@hanyo-denshi
272     =>ucs@iso
273     =>ucs@unicode
274     =>ucs@jis
275     =>ucs@cns
276     =>ucs@ks
277     =+>ucs@iso
278     =+>ucs@unicode
279     =+>ucs@jis
280     =+>ucs@jis/1990
281     =+>ucs@cns
282     =+>ucs@ks
283     =>>ucs@iso
284     =>>ucs@unicode
285     =>>ucs@jis
286     =>>ucs@cns
287     =>>>ucs@iso
288     =>>>ucs@unicode
289     ==ucs@iso
290     ==ucs@unicode
291     ;; ==ucs@cns
292     ==gb2312
293     ==ks-x1001
294     ==cns11643-1
295     ==cns11643-2
296     ==cns11643-3
297     ==cns11643-4
298     ==cns11643-5
299     ==cns11643-6
300     ==cns11643-7
301     ==gt
302     ==jis-x0208@1990
303     ;; ==jis-x0208@1983
304     ==jis-x0208@1978
305     ==gt-k
306     =ucs@iso
307     =ucs@unicode
308     =ucs@cns
309     ==big5-cdp
310     ==cbeta
311     =>>big5-cdp
312     =>>gt-k
313     =+>gt
314     =>gt
315     =>mj
316     =>big5-cdp
317     =>daikanwa
318     =>daikanwa/ho
319     =>cns11643-5
320     =>cns11643-7
321     =big5
322     =big5-eten
323     =>gt-k
324     =zinbun-oracle
325     =>zinbun-oracle
326     =ruimoku-v6
327     =>>ruimoku-v6
328     ==ruimoku-v6
329     =jef-china3
330     =>cbeta
331     =shinjigen
332     =ucs-var-001
333     =ucs-var-002
334     =ucs-var-003
335     =ucs-var-004
336     =ucs-var-005
337     =ucs-var-006
338     =ucs-var-008
339     =ucs-var-010
340     =ucs-itaiji-001
341     =ucs-itaiji-002
342     =ucs-itaiji-003
343     =ucs-itaiji-004
344     =ucs-itaiji-005
345     =ucs-itaiji-006
346     =ucs-itaiji-007
347     =ucs-itaiji-008
348     =ucs-itaiji-009
349     =ucs-itaiji-011
350     =>ucs-itaiji-001
351     =>ucs-itaiji-002
352     =>ucs-itaiji-003
353     =>ucs-itaiji-005
354     =>ucs-itaiji-006
355     =>ucs-itaiji-007
356     =big5-cdp-var-001
357     =big5-cdp-var-002
358     =big5-cdp-var-003
359     =big5-cdp-var-004
360     =big5-cdp-var-005
361     =big5-cdp-itaiji-001
362     =big5-cdp-itaiji-002
363     =big5-cdp-itaiji-003
364     =>big5-cdp-itaiji-001
365     =>ucs@iwds-1
366     =>ucs@component
367     ==ucs@gb
368     =ucs@gb
369     =ucs@JP/hanazono))
370
371
372 ;;; @ char-db formatters
373 ;;;
374
375 (defun char-db-make-char-spec (char)
376   (let (ret char-spec)
377     (cond ((characterp char)
378            (cond ((and (setq ret (encode-char char '=ucs 'defined-only))
379                        (not (and (<= #xE000 ret)(<= ret #xF8FF))))
380                   (setq char-spec (list (cons '=ucs ret)))
381                   (cond ((setq ret (get-char-attribute char 'name))
382                          (setq char-spec (cons (cons 'name ret) char-spec))
383                          )
384                         ((setq ret (get-char-attribute char 'name*))
385                          (setq char-spec (cons (cons 'name* ret) char-spec))
386                          ))
387                   )
388                  ((encode-char char '=mj 'defined-only)
389                   (setq char-spec nil)
390                   (dolist (ccs (charset-list))
391                     (if (and (or (eq ccs '=mj)
392                                  ;; (eq (charset-property ccs 'iso-ir) 177)
393                                  (string-match "=ucs@" (symbol-name ccs))
394                                  )
395                              (setq ccs (charset-name ccs))
396                              (null (assq ccs char-spec))
397                              (setq ret (encode-char char ccs 'defined-only)))
398                         (setq char-spec (cons (cons ccs ret) char-spec))))
399                   )
400                  ((encode-char char '==mj 'defined-only)
401                   (setq char-spec nil)
402                   (dolist (ccs (charset-list))
403                     (if (and (or (eq ccs '==mj)
404                                  ;; (eq (charset-property ccs 'iso-ir) 177)
405                                  (string-match "=ucs@" (symbol-name ccs))
406                                  )
407                              (setq ccs (charset-name ccs))
408                              (null (assq ccs char-spec))
409                              (setq ret (encode-char char ccs 'defined-only)))
410                         (setq char-spec (cons (cons ccs ret) char-spec))))
411                   )
412                  ((encode-char char '=adobe-japan1 'defined-only)
413                   (setq char-spec nil)
414                   (dolist (ccs (charset-list))
415                     (if (and (or (memq ccs
416                                        '(=adobe-japan1-0
417                                          =adobe-japan1-1
418                                          =adobe-japan1-2
419                                          =adobe-japan1-3
420                                          =adobe-japan1-4
421                                          =adobe-japan1-5
422                                          =adobe-japan1-6
423                                          ))
424                                  ;; (eq (charset-property ccs 'iso-ir) 177)
425                                  (string-match "=ucs@" (symbol-name ccs))
426                                  )
427                              (setq ccs (charset-name ccs))
428                              (null (assq ccs char-spec))
429                              (setq ret (encode-char char ccs 'defined-only)))
430                         (setq char-spec (cons (cons ccs ret) char-spec))))
431                   )
432                  ((encode-char char '==adobe-japan1 'defined-only)
433                   (setq char-spec nil)
434                   (dolist (ccs (charset-list))
435                     (if (and (or (memq ccs
436                                        '(==adobe-japan1-0
437                                          ==adobe-japan1-1
438                                          ==adobe-japan1-2
439                                          ==adobe-japan1-3
440                                          ==adobe-japan1-4
441                                          ==adobe-japan1-5
442                                          ==adobe-japan1-6
443                                          ))
444                                  ;; (eq (charset-property ccs 'iso-ir) 177)
445                                  (string-match "=ucs@" (symbol-name ccs))
446                                  )
447                              (setq ccs (charset-name ccs))
448                              (null (assq ccs char-spec))
449                              (setq ret (encode-char char ccs 'defined-only)))
450                         (setq char-spec (cons (cons ccs ret) char-spec))))
451                   )
452                  ((setq ret
453                         (catch 'tag
454                           (let ((rest char-db-coded-charset-priority-list)
455                                 ccs)
456                             (while rest
457                               (setq ccs (charset-name
458                                          (find-charset (car rest))))
459                               (if (setq ret
460                                         (encode-char char ccs
461                                                      'defined-only))
462                                   (throw 'tag (cons ccs ret)))
463                               (setq rest (cdr rest))))))
464                   (setq char-spec (list ret))
465                   (dolist (ccs (delq (car ret) (charset-list)))
466                     (if (and (or (charset-iso-final-char ccs)
467                                  (memq ccs
468                                        '(=daikanwa
469                                          =daikanwa@rev2
470                                          ;; =gt-k
471                                          =jis-x0208@1997
472                                          ))
473                                  (eq (charset-property ccs 'iso-ir) 177)
474                                  ;; (string-match "=ucs@" (symbol-name ccs))
475                                  )
476                              (setq ccs (charset-name ccs))
477                              (null (assq ccs char-spec))
478                              (setq ret (encode-char char ccs 'defined-only)))
479                         (setq char-spec (cons (cons ccs ret) char-spec))))
480                   (if (null char-spec)
481                       (setq char-spec (split-char char)))
482                   (cond ((setq ret (get-char-attribute char 'name))
483                          (setq char-spec (cons (cons 'name ret) char-spec))
484                          )
485                         ((setq ret (get-char-attribute char 'name*))
486                          (setq char-spec (cons (cons 'name* ret) char-spec))
487                          ))
488                   )
489                  ((setq ret (get-char-attribute
490                              char 'ideographic-combination))
491                   (setq char-spec
492                         (cons (cons 'ideographic-combination ret)
493                               char-spec))
494                   ))
495            char-spec)
496           ((consp char)
497            char))))
498     
499 (defun char-db-insert-char-spec (char &optional readable column
500                                       required-features)
501   (unless column
502     (setq column (current-column)))
503   (let (char-spec temp-char)
504     (setq char-spec (char-db-make-char-spec char))
505     (unless (or (characterp char) ; char
506                 (condition-case nil
507                     (setq char (find-char char-spec))
508                   (error nil)))
509       ;; define temporary character
510       ;;   Current implementation is dirty.
511       (setq temp-char (define-char (cons '(ideograph-daikanwa . 0)
512                                          char-spec)))
513       (remove-char-attribute temp-char 'ideograph-daikanwa)
514       (setq char temp-char))
515     (insert-char-attributes char
516                             readable
517                             (union (mapcar #'car char-spec)
518                                    required-features)
519                             nil 'for-sub-node)
520     (when temp-char
521       ;; undefine temporary character
522       ;;   Current implementation is dirty.
523       (setq char-spec (char-attribute-alist temp-char))
524       (while char-spec
525         (remove-char-attribute temp-char (car (car char-spec)))
526         (setq char-spec (cdr char-spec))))))
527
528 (defun char-db-insert-alist (alist &optional readable column)
529   (unless column
530     (setq column (current-column)))
531   (let ((line-breaking
532          (concat "\n" (make-string (1+ column) ?\ )))
533         name value
534         ret al ; cal
535         key
536         lbs cell rest separator)
537     (insert "(")
538     (while alist
539       (setq name (car (car alist))
540             value (cdr (car alist)))
541       (cond ((eq name 'char)
542              (insert "(char . ")
543              (if (setq ret (condition-case nil
544                                (find-char value)
545                              (error nil)))
546                  (progn
547                    (setq al nil
548                          ;; cal nil
549                          )
550                    (while value
551                      (setq key (car (car value)))
552                      ;; (if (find-charset key)
553                      ;;     (setq cal (cons key cal))
554                      (setq al (cons key al))
555                      ;; )
556                      (setq value (cdr value)))
557                    (insert-char-attributes ret
558                                            readable
559                                            (or al 'none) ; cal
560                                            nil 'for-sub-node))
561                (insert (prin1-to-string value)))
562              (insert ")")
563              (insert line-breaking))
564             ((consp value)
565              (insert (format "(%-18s " name))
566              (setq lbs (concat "\n" (make-string (current-column) ?\ )))
567              (while (consp value)
568                (setq cell (car value))
569                (if (and (consp cell)
570                         (consp (car cell))
571                         (setq ret (condition-case nil
572                                       (find-char cell)
573                                     (error nil)))
574                         )
575                    (progn
576                      (setq rest cell
577                            al nil
578                            ;; cal nil
579                            )
580                      (while rest
581                        (setq key (car (car rest)))
582                        ;; (if (find-charset key)
583                        ;;     (setq cal (cons key cal))
584                        (setq al (cons key al))
585                        ;; )
586                        (setq rest (cdr rest)))
587                      (if separator
588                          (insert lbs))
589                      (insert-char-attributes ret
590                                              readable
591                                              al ; cal
592                                              nil 'for-sub-node)
593                      (setq separator lbs))
594                  (if separator
595                      (insert separator))
596                  (insert (prin1-to-string cell))
597                  (setq separator " "))
598                (setq value (cdr value)))
599              (insert ")")
600              (insert line-breaking))
601             (t
602              (insert (format "(%-18s . %S)%s"
603                              name value
604                              line-breaking))))
605       (setq alist (cdr alist))))
606   (insert ")"))
607
608 (defun char-db-insert-char-reference (plist &optional readable column)
609   (unless column
610     (setq column (current-column)))
611   (let ((line-breaking
612          (concat "\n" (make-string (1+ column) ?\ )))
613         (separator "")
614         name value)
615     (insert "(")
616     (while plist
617       (setq name (pop plist))
618       (setq value (pop plist))
619       (cond ((eq name :char)
620              (insert separator)
621              (insert ":char\t")
622              (cond ((numberp value)
623                     (setq value (decode-char '=ucs value)))
624                    ;; ((consp value)
625                    ;;  (setq value (or (find-char value)
626                    ;;                  value)))
627                    )
628              (char-db-insert-char-spec value readable)
629              (insert line-breaking)
630              (setq separator ""))
631             ((eq name :radical)
632              (insert (format "%s%s\t%d ; %c%s"
633                              separator
634                              name value
635                              (ideographic-radical value)
636                              line-breaking))
637              (setq separator ""))
638             (t
639              (insert (format "%s%s\t%S" separator name value))
640              (setq separator line-breaking)))
641       ))
642   (insert ")"))
643
644 (defun char-db-decode-isolated-char (ccs code-point)
645   (let (ret)
646     (setq ret
647           (cond ((eq ccs 'arabic-iso8859-6)
648                  (decode-char ccs code-point))
649                 ;; ((eq ccs '=gt)
650                 ;;  (decode-builtin-char '==gt code-point))
651                 ((and (memq ccs '(=gt-pj-1
652                                   =gt-pj-2
653                                   =gt-pj-3
654                                   =gt-pj-4
655                                   =gt-pj-5
656                                   =gt-pj-6
657                                   =gt-pj-7
658                                   =gt-pj-8
659                                   =gt-pj-9
660                                   =gt-pj-10
661                                   =gt-pj-11))
662                       (setq ret (decode-char ccs code-point))
663                       (setq ret (encode-char ret '=gt 'defined-only)))
664                  (decode-builtin-char '=gt ret))
665                 (t
666                  (decode-builtin-char ccs code-point))))
667     (cond ((null ret)
668            (or (decode-char ccs code-point)
669                (define-char (list (cons ccs code-point)))))
670           ((and (<= 0 (char-int ret))
671                 (<= (char-int ret) #x1F))
672            (decode-char '=ucs (+ #x2400 (char-int ret))))
673           ((= (char-int ret) #x7F)
674            ?\u2421)
675           (t ret))))
676
677 (defvar char-db-convert-obsolete-format t)
678
679 (defun char-db-insert-ccs-feature (name value line-breaking)
680   (cond
681    ((integerp value)
682     (insert
683      (format
684       (cond
685        ((memq name '(=>iwds-1
686                      ==shinjigen
687                      =shinjigen
688                      =shinjigen@1ed ==shinjigen@1ed
689                      =shinjigen@rev ==shinjigen@rev
690                      =shinjigen/+p@rev ==shinjigen/+p@rev
691                      ===daikanwa/ho ==daikanwa/ho
692                      =daikanwa/ho =>>daikanwa/ho =>daikanwa/ho))
693         "(%-18s .  %04d)\t; %c")
694        ((eq name '=shinjigen@1ed/24pr)
695         "(%-18s . %04d)\t; %c")
696        ((or
697          (memq name
698                '(===daikanwa
699                  ==daikanwa =daikanwa =>>daikanwa =>daikanwa
700                  =daikanwa@rev1 =daikanwa@rev2
701                  =daikanwa/+p ==daikanwa/+p ===daikanwa/+p
702                  =>>daikanwa/+p
703                  =daikanwa/+2p =>>daikanwa/+2p
704                  =gt ==gt ===gt
705                  =>>gt =+>gt =>gt
706                  =gt-k ==gt-k ===gt-k
707                  =>>gt-k =>gt-k
708                  =adobe-japan1-0 ==adobe-japan1-0 ===adobe-japan1-0
709                  =adobe-japan1-1 ==adobe-japan1-1 ===adobe-japan1-1
710                  =adobe-japan1-2 ==adobe-japan1-2 ===adobe-japan1-2
711                  =adobe-japan1-3 ==adobe-japan1-3 ===adobe-japan1-3
712                  =adobe-japan1-4 ==adobe-japan1-4 ===adobe-japan1-4
713                  =adobe-japan1-5 ==adobe-japan1-5 ===adobe-japan1-5
714                  =adobe-japan1-6 ==adobe-japan1-6 ===adobe-japan1-6
715                  =>>adobe-japan1-0 =+>adobe-japan1-0
716                  =>>adobe-japan1-1 =+>adobe-japan1-1
717                  =>>adobe-japan1-2 =+>adobe-japan1-2
718                  =>>adobe-japan1-3 =+>adobe-japan1-3
719                  =>>adobe-japan1-4 =+>adobe-japan1-4
720                  =>>adobe-japan1-5 =+>adobe-japan1-5
721                  =>>adobe-japan1-6 =+>adobe-japan1-6
722                  =>cbeta =cbeta =>>cbeta ==cbeta ===cbeta
723                  =zinbun-oracle =>zinbun-oracle
724                  ===hng-jou ===hng-keg ===hng-dng ===hng-mam
725                  ===hng-drt ===hng-kgk ===hng-myz ===hng-kda
726                  ===hng-khi ===hng-khm ===hng-hok ===hng-kyd ===hng-sok
727                  ===hng-yhk ===hng-kak ===hng-kar ===hng-kae
728                  ===hng-sys ===hng-tsu ===hng-tzj
729                  ===hng-hos ===hng-nak ===hng-jhk
730                  ===hng-hod ===hng-gok ===hng-ink ===hng-nto
731                  ===hng-nkm ===hng-k24 ===hng-nkk
732                  ===hng-kcc ===hng-kcj ===hng-kbk ===hng-sik
733                  ===hng-skk ===hng-kyu ===hng-ksk ===hng-wan
734                  ===hng-okd ===hng-wad ===hng-kmi
735                  ===hng-zkd ===hng-doh ===hng-jyu
736                  ===hng-tzs ===hng-kss ===hng-kyo
737                  ===hng-smk))
738          ;; (string-match "^=adobe-" (symbol-name name))
739          )
740         "(%-18s . %05d)\t; %c")
741        ((memq name '(=hanyo-denshi/ks
742                      ==hanyo-denshi/ks ===hanyo-denshi/ks
743                      =>>hanyo-denshi/ks
744                      =koseki ==koseki
745                      =mj ==mj ===mj =>>mj =>mj
746                      =zihai mojikyo))
747         "(%-18s . %06d)\t; %c")
748        ((memq name '(=hanyo-denshi/tk ==hanyo-denshi/tk))
749         "(%-18s . %08d)\t; %c")
750        ((>= (charset-dimension name) 2)
751         "(%-18s . #x%04X)\t; %c")
752        (t
753         "(%-18s . #x%02X)\t; %c"))
754       name
755       (if (= (charset-iso-graphic-plane name) 1)
756           (logior value
757                   (cond ((= (charset-dimension name) 1)
758                          #x80)
759                         ((= (charset-dimension name) 2)
760                          #x8080)
761                         ((= (charset-dimension name) 3)
762                          #x808080)
763                         (t 0)))
764         value)
765       (char-db-decode-isolated-char name value)))
766     (if (and (= (charset-chars name) 94)
767              (= (charset-dimension name) 2))
768         (insert (format " [%02d-%02d]"
769                         (- (lsh value -8) 32)
770                         (- (logand value 255) 32))))
771     )
772    (t
773     (insert (format "(%-18s . %s)" name value))
774     ))
775   (insert line-breaking))
776
777 (defun char-db-insert-relation-feature (char name value line-breaking
778                                              ccss readable)
779   (insert (format "(%-18s%s " name line-breaking))
780   (let ((lbs (concat "\n" (make-string (current-column) ?\ )))
781         separator cell sources required-features
782         ret)
783     (while (consp value)
784       (setq cell (car value))
785       (if (integerp cell)
786           (setq cell (decode-char '=ucs cell)))
787       (cond
788        ((eq name '->subsumptive)
789         (when (or (not (some (lambda (atr)
790                                (get-char-attribute cell atr))
791                              char-db-ignored-attributes))
792                   (some (lambda (ccs)
793                           (encode-char cell ccs 'defined-only))
794                         ccss))
795           (if separator
796               (insert lbs))
797           (let ((char-db-ignored-attributes
798                  (cons '<-subsumptive
799                        char-db-ignored-attributes)))
800             (insert-char-attributes cell readable nil nil 'for-sub-node))
801           (setq separator lbs))
802         )
803        ((characterp cell)
804         (setq sources
805               (get-char-attribute
806                char (intern (format "%s*sources" name))))
807         (setq required-features nil)
808         (dolist (source sources)
809           (cond
810            ((memq source '(JP
811                            JP/Jouyou
812                            shinjigen shinjigen@1ed shinjigen@rev))
813             (setq required-features
814                   (union required-features
815                          '(=jis-x0208
816                            =jis-x0208@1990
817                            =jis-x0213-1@2000
818                            =jis-x0213-1@2004
819                            =jis-x0213-2
820                            =jis-x0212
821                            =jis-x0208@1983
822                            =jis-x0208@1978
823                            =shinjigen
824                            =shinjigen@1ed
825                            =shinjigen@rev
826                            =shinjigen/+p@rev))))
827            ((eq source 'CN)
828             (setq required-features
829                   (union required-features
830                          '(=gb2312
831                            =gb12345
832                            =iso-ir165)))))
833           (cond
834            ((find-charset (setq ret (intern (format "=%s" source))))
835             (setq required-features
836                   (cons ret required-features)))
837            (t (setq required-features
838                     (cons source required-features)))))
839         (cond ((string-match "@JP" (symbol-name name))
840                (setq required-features
841                      (union required-features
842                             '(=jis-x0208
843                               =jis-x0208@1990
844                               =jis-x0213-1-2000
845                               =jis-x0213-2-2000
846                               =jis-x0212
847                               =jis-x0208@1983
848                               =jis-x0208@1978))))
849               ((string-match "@CN" (symbol-name name))
850                (setq required-features
851                      (union required-features
852                             '(=gb2312
853                               =gb12345
854                               =iso-ir165)))))
855         (if separator
856             (insert lbs))
857         (if readable
858             (insert (format "%S" cell))
859           (char-db-insert-char-spec cell readable
860                                     nil
861                                     required-features))
862         (setq separator lbs))
863        ((consp cell)
864         (if separator
865             (insert lbs))
866         (if (consp (car cell))
867             (char-db-insert-char-spec cell readable)
868           (char-db-insert-char-reference cell readable))
869         (setq separator lbs))
870        (t
871         (if separator
872             (insert separator))
873         (insert (prin1-to-string cell))
874         (setq separator " ")))
875       (setq value (cdr value)))
876     (insert ")")
877     (insert line-breaking)))
878
879 (defun insert-char-attributes (char &optional readable attributes column
880                                     for-sub-node)
881   (unless column
882     (setq column (current-column)))
883   (let (name value ; has-long-ccs-name
884         rest
885         radical strokes
886         (line-breaking
887          (concat "\n" (make-string (1+ column) ?\ )))
888         lbs cell separator ret
889         key al cal
890         dest-ccss ; sources required-features
891         ccss)
892     (let (atr-d)
893       (setq attributes
894             (sort (if attributes
895                       (if (consp attributes)
896                           (progn
897                             (dolist (name attributes)
898                               (unless (memq name char-db-ignored-attributes)
899                                 (if (find-charset name)
900                                     (push name ccss))
901                                 (push name atr-d)))
902                             atr-d))
903                     (dolist (name (char-attribute-list))
904                       (unless (memq name char-db-ignored-attributes)
905                         (if (find-charset name)
906                             (push name ccss))
907                         (push name atr-d)))
908                     atr-d)
909                   #'char-attribute-name<)))
910     (insert "(")
911     (when (memq '<-subsumptive attributes)
912       (when (or readable (not for-sub-node))
913         (when (setq value (get-char-attribute char '<-subsumptive))
914           (char-db-insert-relation-feature char '<-subsumptive value
915                                            line-breaking
916                                            ccss readable)))
917       (setq attributes (delq '<-subsumptive attributes)))
918     (when (and (memq '<-denotational attributes)
919                (setq value (get-char-attribute char '<-denotational)))
920       (char-db-insert-relation-feature char '<-denotational value
921                                        line-breaking
922                                        ccss readable)
923       (setq attributes (delq '<-denotational attributes)))
924     (when (and (memq '<-denotational@component attributes)
925                (setq value (get-char-attribute char '<-denotational@component)))
926       (char-db-insert-relation-feature char '<-denotational@component value
927                                        line-breaking
928                                        ccss readable)
929       (setq attributes (delq '<-denotational@component attributes)))
930     (when (and (memq 'name attributes)
931                (setq value (get-char-attribute char 'name)))
932       (insert (format
933                (if (> (+ (current-column) (length value)) 48)
934                    "(name . %S)%s"
935                  "(name               . %S)%s")
936                value line-breaking))
937       (setq attributes (delq 'name attributes))
938       )
939     (when (and (memq 'name* attributes)
940                (setq value (get-char-attribute char 'name*)))
941       (insert (format
942                (if (> (+ (current-column) (length value)) 48)
943                    "(name* . %S)%s"
944                  "(name*              . %S)%s")
945                value line-breaking))
946       (setq attributes (delq 'name* attributes))
947       )
948     (when (and (memq 'script attributes)
949                (setq value (get-char-attribute char 'script)))
950       (insert (format "(script\t\t%s)%s"
951                       (mapconcat (function prin1-to-string)
952                                  value " ")
953                       line-breaking))
954       (setq attributes (delq 'script attributes))
955       )
956     (dolist (name '(=>ucs =>ucs*))
957       (when (and (memq name attributes)
958                  (setq value (get-char-attribute char name)))
959         (insert (format "(%-18s . #x%04X)\t; %c%s"
960                         name value (decode-char '=ucs value)
961                         line-breaking))
962         (setq attributes (delq name attributes))))
963     (dolist (name '(=>ucs@gb =>ucs@big5))
964       (when (and (memq name attributes)
965                  (setq value (get-char-attribute char name)))
966         (insert (format "(%-18s . #x%04X)\t; %c%s"
967                         name value
968                         (decode-char (intern
969                                       (concat "="
970                                               (substring
971                                                (symbol-name name) 2)))
972                                      value)
973                         line-breaking))
974         (setq attributes (delq name attributes))
975         ))
976     ;; (dolist (name '(=>daikanwa))
977     ;;   (when (and (memq name attributes)
978     ;;              (setq value (get-char-attribute char name)))
979     ;;     (insert
980     ;;      (if (integerp value)
981     ;;          (format "(%-18s . %05d)\t; %c%s"
982     ;;                  name value (decode-char '=daikanwa value)
983     ;;                  line-breaking)
984     ;;        (format "(%-18s %s)\t; %c%s"
985     ;;                name
986     ;;                (mapconcat (function prin1-to-string)
987     ;;                           value " ")
988     ;;                (char-representative-of-daikanwa char)
989     ;;                line-breaking)))
990     ;;     (setq attributes (delq name attributes))))
991     (when (and (memq 'general-category attributes)
992                (setq value (get-char-attribute char 'general-category)))
993       (insert (format
994                "(general-category\t%s) ; %s%s"
995                (mapconcat (lambda (cell)
996                             (format "%S" cell))
997                           value " ")
998                (cond ((rassoc value unidata-normative-category-alist)
999                       "Normative Category")
1000                      ((rassoc value unidata-informative-category-alist)
1001                       "Informative Category")
1002                      (t
1003                       "Unknown Category"))
1004                line-breaking))
1005       (setq attributes (delq 'general-category attributes))
1006       )
1007     (when (and (memq 'bidi-category attributes)
1008                (setq value (get-char-attribute char 'bidi-category)))
1009       (insert (format "(bidi-category\t. %S)%s"
1010                       value
1011                       line-breaking))
1012       (setq attributes (delq 'bidi-category attributes))
1013       )
1014     (unless (or (not (memq 'mirrored attributes))
1015                 (eq (setq value (get-char-attribute char 'mirrored 'empty))
1016                     'empty))
1017       (insert (format "(mirrored\t\t. %S)%s"
1018                       value
1019                       line-breaking))
1020       (setq attributes (delq 'mirrored attributes))
1021       )
1022     (cond
1023      ((and (memq 'decimal-digit-value attributes)
1024            (setq value (get-char-attribute char 'decimal-digit-value)))
1025       (insert (format "(decimal-digit-value . %S)%s"
1026                       value
1027                       line-breaking))
1028       (setq attributes (delq 'decimal-digit-value attributes))
1029       (when (and (memq 'digit-value attributes)
1030                  (setq value (get-char-attribute char 'digit-value)))
1031         (insert (format "(digit-value\t . %S)%s"
1032                         value
1033                         line-breaking))
1034         (setq attributes (delq 'digit-value attributes))
1035         )
1036       (when (and (memq 'numeric-value attributes)
1037                  (setq value (get-char-attribute char 'numeric-value)))
1038         (insert (format "(numeric-value\t . %S)%s"
1039                         value
1040                         line-breaking))
1041         (setq attributes (delq 'numeric-value attributes))
1042         )
1043       )
1044      (t
1045       (when (and (memq 'digit-value attributes)
1046                  (setq value (get-char-attribute char 'digit-value)))
1047         (insert (format "(digit-value\t. %S)%s"
1048                         value
1049                         line-breaking))
1050         (setq attributes (delq 'digit-value attributes))
1051         )
1052       (when (and (memq 'numeric-value attributes)
1053                  (setq value (get-char-attribute char 'numeric-value)))
1054         (insert (format "(numeric-value\t. %S)%s"
1055                         value
1056                         line-breaking))
1057         (setq attributes (delq 'numeric-value attributes))
1058         )))
1059     (when (and (memq 'iso-10646-comment attributes)
1060                (setq value (get-char-attribute char 'iso-10646-comment)))
1061       (insert (format "(iso-10646-comment\t. %S)%s"
1062                       value
1063                       line-breaking))
1064       (setq attributes (delq 'iso-10646-comment attributes))
1065       )
1066     (when (and (memq 'morohashi-daikanwa attributes)
1067                (setq value (get-char-attribute char 'morohashi-daikanwa)))
1068       (insert (format "(morohashi-daikanwa\t%s)%s"
1069                       (mapconcat (function prin1-to-string) value " ")
1070                       line-breaking))
1071       (setq attributes (delq 'morohashi-daikanwa attributes))
1072       )
1073     (setq radical nil
1074           strokes nil)
1075     (when (and (memq 'ideographic-radical attributes)
1076                (setq value (get-char-attribute char 'ideographic-radical)))
1077       (setq radical value)
1078       (insert (format "(ideographic-radical . %S)\t; %c%s"
1079                       radical
1080                       (ideographic-radical radical)
1081                       line-breaking))
1082       (setq attributes (delq 'ideographic-radical attributes))
1083       )
1084     (when (and (memq 'shuowen-radical attributes)
1085                (setq value (get-char-attribute char 'shuowen-radical)))
1086       (insert (format "(shuowen-radical\t. %S)\t; %c%s"
1087                       value
1088                       (shuowen-radical value)
1089                       line-breaking))
1090       (setq attributes (delq 'shuowen-radical attributes))
1091       )
1092     (let (key)
1093       (dolist (domain
1094                (append
1095                 char-db-feature-domains
1096                 (let (dest domain)
1097                   (dolist (feature (char-attribute-list))
1098                     (setq feature (symbol-name feature))
1099                     (when (string-match
1100                            "\\(radical\\|strokes\\)@\\([^@*]+\\)\\(\\*\\|$\\)"
1101                            feature)
1102                       (setq domain (intern (match-string 2 feature)))
1103                      (unless (memq domain dest)
1104                        (setq dest (cons domain dest)))))
1105                   (sort dest #'string<))))
1106         (setq key (intern (format "%s@%s" 'ideographic-radical domain)))
1107         (when (and (memq key attributes)
1108                    (setq value (get-char-attribute char key)))
1109           (setq radical value)
1110           (insert (format "(%s . %S)\t; %c%s"
1111                           key
1112                           radical
1113                           (ideographic-radical radical)
1114                           line-breaking))
1115           (setq attributes (delq key attributes))
1116           )
1117         (setq key (intern (format "%s@%s" 'ideographic-strokes domain)))
1118         (when (and (memq key attributes)
1119                    (setq value (get-char-attribute char key)))
1120           (setq strokes value)
1121           (insert (format "(%s . %S)%s"
1122                           key
1123                           strokes
1124                           line-breaking))
1125           (setq attributes (delq key attributes))
1126           )
1127         (setq key (intern (format "%s@%s" 'total-strokes domain)))
1128         (when (and (memq key attributes)
1129                    (setq value (get-char-attribute char key)))
1130           (insert (format "(%s       . %S)%s"
1131                           key
1132                           value
1133                           line-breaking))
1134           (setq attributes (delq key attributes))
1135           )
1136         (dolist (feature '(ideographic-radical
1137                            ideographic-strokes
1138                            total-strokes))
1139           (setq key (intern (format "%s@%s*sources" feature domain)))
1140           (when (and (memq key attributes)
1141                      (setq value (get-char-attribute char key)))
1142             (insert (format "(%s%s" key line-breaking))
1143             (dolist (cell value)
1144               (insert (format " %s" cell)))
1145             (insert ")")
1146             (insert line-breaking)
1147             (setq attributes (delq key attributes))
1148             ))
1149         ))
1150     (when (and (memq 'ideographic-strokes attributes)
1151                (setq value (get-char-attribute char 'ideographic-strokes)))
1152       (setq strokes value)
1153       (insert (format "(ideographic-strokes . %S)%s"
1154                       strokes
1155                       line-breaking))
1156       (setq attributes (delq 'ideographic-strokes attributes))
1157       )
1158     (when (and (memq 'kangxi-radical attributes)
1159                (setq value (get-char-attribute char 'kangxi-radical)))
1160       (unless (eq value radical)
1161         (insert (format "(kangxi-radical\t . %S)\t; %c%s"
1162                         value
1163                         (ideographic-radical value)
1164                         line-breaking))
1165         (or radical
1166             (setq radical value)))
1167       (setq attributes (delq 'kangxi-radical attributes))
1168       )
1169     (when (and (memq 'kangxi-strokes attributes)
1170                (setq value (get-char-attribute char 'kangxi-strokes)))
1171       (unless (eq value strokes)
1172         (insert (format "(kangxi-strokes\t . %S)%s"
1173                         value
1174                         line-breaking))
1175         (or strokes
1176             (setq strokes value)))
1177       (setq attributes (delq 'kangxi-strokes attributes))
1178       )
1179     ;; (when (and (memq 'japanese-radical attributes)
1180     ;;            (setq value (get-char-attribute char 'japanese-radical)))
1181     ;;   (unless (eq value radical)
1182     ;;     (insert (format "(japanese-radical\t . %S)\t; %c%s"
1183     ;;                     value
1184     ;;                     (ideographic-radical value)
1185     ;;                     line-breaking))
1186     ;;     (or radical
1187     ;;         (setq radical value)))
1188     ;;   (setq attributes (delq 'japanese-radical attributes))
1189     ;;   )
1190     (when (and (memq 'japanese-strokes attributes)
1191                (setq value (get-char-attribute char 'japanese-strokes)))
1192       (unless (eq value strokes)
1193         (insert (format "(japanese-strokes\t . %S)%s"
1194                         value
1195                         line-breaking))
1196         (or strokes
1197             (setq strokes value)))
1198       (setq attributes (delq 'japanese-strokes attributes))
1199       )
1200     (when (and (memq 'cns-radical attributes)
1201                (setq value (get-char-attribute char 'cns-radical)))
1202       (insert (format "(cns-radical\t . %S)\t; %c%s"
1203                       value
1204                       (ideographic-radical value)
1205                       line-breaking))
1206       (setq attributes (delq 'cns-radical attributes))
1207       )
1208     (when (and (memq 'cns-strokes attributes)
1209                (setq value (get-char-attribute char 'cns-strokes)))
1210       (unless (eq value strokes)
1211         (insert (format "(cns-strokes\t . %S)%s"
1212                         value
1213                         line-breaking))
1214         (or strokes
1215             (setq strokes value)))
1216       (setq attributes (delq 'cns-strokes attributes))
1217       )
1218     ;; (when (and (memq 'shinjigen-1-radical attributes)
1219     ;;            (setq value (get-char-attribute char 'shinjigen-1-radical)))
1220     ;;   (unless (eq value radical)
1221     ;;     (insert (format "(shinjigen-1-radical . %S)\t; %c%s"
1222     ;;                     value
1223     ;;                     (ideographic-radical value)
1224     ;;                     line-breaking))
1225     ;;     (or radical
1226     ;;         (setq radical value)))
1227     ;;   (setq attributes (delq 'shinjigen-1-radical attributes))
1228     ;;   )
1229     ;; (when (and (memq 'ideographic- attributes)
1230     ;;            (setq value (get-char-attribute char 'ideographic-)))
1231     ;;   (insert "(ideographic-       ")
1232     ;;   (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1233     ;;         separator nil)
1234     ;;   (while (consp value)
1235     ;;     (setq cell (car value))
1236     ;;     (if (integerp cell)
1237     ;;         (setq cell (decode-char '=ucs cell)))
1238     ;;     (cond ((characterp cell)
1239     ;;            (if separator
1240     ;;                (insert lbs))
1241     ;;            (if readable
1242     ;;                (insert (format "%S" cell))
1243     ;;              (char-db-insert-char-spec cell readable))
1244     ;;            (setq separator lbs))
1245     ;;           ((consp cell)
1246     ;;            (if separator
1247     ;;                (insert lbs))
1248     ;;            (if (consp (car cell))
1249     ;;                (char-db-insert-char-spec cell readable)
1250     ;;              (char-db-insert-char-reference cell readable))
1251     ;;            (setq separator lbs))
1252     ;;           (t
1253     ;;            (if separator
1254     ;;                (insert separator))
1255     ;;            (insert (prin1-to-string cell))
1256     ;;            (setq separator " ")))
1257     ;;     (setq value (cdr value)))
1258     ;;   (insert ")")
1259     ;;   (insert line-breaking)
1260     ;;   (setq attributes (delq 'ideographic- attributes)))
1261     (when (and (memq 'total-strokes attributes)
1262                (setq value (get-char-attribute char 'total-strokes)))
1263       (insert (format "(total-strokes       . %S)%s"
1264                       value
1265                       line-breaking))
1266       (setq attributes (delq 'total-strokes attributes))
1267       )
1268     ;; (when (and (memq '->ideograph attributes)
1269     ;;            (setq value (get-char-attribute char '->ideograph)))
1270     ;;   (insert (format "(->ideograph\t%s)%s"
1271     ;;                   (mapconcat (lambda (code)
1272     ;;                                (cond ((symbolp code)
1273     ;;                                       (symbol-name code))
1274     ;;                                      ((integerp code)
1275     ;;                                       (format "#x%04X" code))
1276     ;;                                      (t
1277     ;;                                       (format "%s %S"
1278     ;;                                               line-breaking code))))
1279     ;;                              value " ")
1280     ;;                   line-breaking))
1281     ;;   (setq attributes (delq '->ideograph attributes))
1282     ;;   )
1283     ;; (when (and (memq '->decomposition attributes)
1284     ;;            (setq value (get-char-attribute char '->decomposition)))
1285     ;;   (insert (format "(->decomposition\t%s)%s"
1286     ;;                   (mapconcat (lambda (code)
1287     ;;                                (cond ((symbolp code)
1288     ;;                                       (symbol-name code))
1289     ;;                                      ((characterp code)
1290     ;;                                       (if readable
1291     ;;                                           (format "%S" code)
1292     ;;                                         (format "#x%04X"
1293     ;;                                                 (char-int code))
1294     ;;                                         ))
1295     ;;                                      ((integerp code)
1296     ;;                                       (format "#x%04X" code))
1297     ;;                                      (t
1298     ;;                                       (format "%s%S" line-breaking code))))
1299     ;;                              value " ")
1300     ;;                   line-breaking))
1301     ;;   (setq attributes (delq '->decomposition attributes))
1302     ;;   )
1303     (if (equal (get-char-attribute char '->titlecase)
1304                (get-char-attribute char '->uppercase))
1305         (setq attributes (delq '->titlecase attributes)))
1306     (when (and (memq '->mojikyo attributes)
1307                (setq value (get-char-attribute char '->mojikyo)))
1308       (insert (format "(->mojikyo\t\t. %06d)\t; %c%s"
1309                       value (decode-char 'mojikyo value)
1310                       line-breaking))
1311       (setq attributes (delq '->mojikyo attributes))
1312       )
1313     ;; (when (and (memq 'hanyu-dazidian-vol attributes)
1314     ;;            (setq value (get-char-attribute char 'hanyu-dazidian-vol)))
1315     ;;   (insert (format "(hanyu-dazidian-vol  . %d)%s"
1316     ;;                   value line-breaking))
1317     ;;   (setq attributes (delq 'hanyu-dazidian-vol attributes))
1318     ;;   )
1319     ;; (when (and (memq 'hanyu-dazidian-page attributes)
1320     ;;            (setq value (get-char-attribute char 'hanyu-dazidian-page)))
1321     ;;   (insert (format "(hanyu-dazidian-page . %d)%s"
1322     ;;                   value line-breaking))
1323     ;;   (setq attributes (delq 'hanyu-dazidian-page attributes))
1324     ;;   )
1325     ;; (when (and (memq 'hanyu-dazidian-char attributes)
1326     ;;            (setq value (get-char-attribute char 'hanyu-dazidian-char)))
1327     ;;   (insert (format "(hanyu-dazidian-char . %d)%s"
1328     ;;                   value line-breaking))
1329     ;;   (setq attributes (delq 'hanyu-dazidian-char attributes))
1330     ;;   )
1331     (unless readable
1332       (dolist (ignored '(composition
1333                          ->denotational <-subsumptive ->ucs-unified
1334                          ->ideographic-component-forms))
1335         (setq attributes (delq ignored attributes))))
1336     (while attributes
1337       (setq name (car attributes))
1338       (unless (eq (setq value (get-char-attribute char name 'value-is-empty))
1339                   'value-is-empty)
1340         (cond ((setq ret (find-charset name))
1341                (setq name (charset-name ret))
1342                (when (not (memq name dest-ccss))
1343                  (setq dest-ccss (cons name dest-ccss))
1344                  (char-db-insert-ccs-feature name value line-breaking))
1345                )
1346               ((string-match "^=>ucs@" (symbol-name name))
1347                (insert (format "(%-18s . #x%04X)\t; %c%s"
1348                                name value (decode-char '=ucs value)
1349                                line-breaking))
1350                )
1351               ((eq name 'jisx0208-1978/4X)
1352                (insert (format "(%-18s . #x%04X)%s"
1353                                name value
1354                                line-breaking))
1355                )
1356               ((and
1357                 (not readable)
1358                 (not (eq name '->subsumptive))
1359                 (not (eq name '->uppercase))
1360                 (not (eq name '->lowercase))
1361                 (not (eq name '->titlecase))
1362                 (not (eq name '->canonical))
1363                 (not (eq name '->Bopomofo))
1364                 (not (eq name '->mistakable))
1365                 (not (eq name '->ideographic-variants))
1366                 (null (get-char-attribute
1367                        char (intern (format "%s*sources" name))))
1368                 (not (string-match "\\*sources$" (symbol-name name)))
1369                 (null (get-char-attribute
1370                        char (intern (format "%s*note" name))))
1371                 (not (string-match "\\*note$" (symbol-name name)))
1372                 (or (eq name '<-identical)
1373                     (eq name '<-uppercase)
1374                     (eq name '<-lowercase)
1375                     (eq name '<-titlecase)
1376                     (eq name '<-canonical)
1377                     (eq name '<-ideographic-variants)
1378                     ;; (eq name '<-synonyms)
1379                     (string-match "^<-synonyms" (symbol-name name))
1380                     (eq name '<-mistakable)
1381                     (when (string-match "^->" (symbol-name name))
1382                       (cond
1383                        ((string-match "^->fullwidth" (symbol-name name))
1384                         (not (and (consp value)
1385                                   (characterp (car value))
1386                                   (encode-char
1387                                    (car value) '=ucs 'defined-only)))
1388                         )
1389                        (t)))
1390                     ))
1391                )
1392               ((or (eq name 'ideographic-structure)
1393                    (eq name 'ideographic-combination)
1394                    ;; (eq name 'ideographic-)
1395                    (eq name '=decomposition)
1396                    (char-feature-base-name= '=decomposition name)
1397                    (char-feature-base-name= '=>decomposition name)
1398                    ;; (string-match "^=>*decomposition\\(@[^*]+\\)?$"
1399                    ;;               (symbol-name name))
1400                    (string-match "^\\(->\\|<-\\)[^*]*$" (symbol-name name))
1401                    (string-match "^\\(->\\|<-\\)[^*]*\\*sources$"
1402                                  (symbol-name name))
1403                    )
1404                (char-db-insert-relation-feature char name value
1405                                                 line-breaking
1406                                                 ccss readable))
1407               ;; ((memq name '(ideograph=
1408               ;;               original-ideograph-of
1409               ;;               ancient-ideograph-of
1410               ;;               vulgar-ideograph-of
1411               ;;               wrong-ideograph-of
1412               ;;               ;; simplified-ideograph-of
1413               ;;               ideographic-variants
1414               ;;               ;; ideographic-different-form-of
1415               ;;               ))
1416               ;;  (insert (format "(%-18s%s " name line-breaking))
1417               ;;  (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1418               ;;        separator nil)
1419               ;;  (while (consp value)
1420               ;;    (setq cell (car value))
1421               ;;    (if (and (consp cell)
1422               ;;             (consp (car cell)))
1423               ;;        (progn
1424               ;;          (if separator
1425               ;;              (insert lbs))
1426               ;;          (char-db-insert-alist cell readable)
1427               ;;          (setq separator lbs))
1428               ;;      (if separator
1429               ;;          (insert separator))
1430               ;;      (insert (prin1-to-string cell))
1431               ;;      (setq separator " "))
1432               ;;    (setq value (cdr value)))
1433               ;;  (insert ")")
1434               ;;  (insert line-breaking))
1435               ((consp value)
1436                (insert (format "(%-18s " name))
1437                (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1438                      separator nil)
1439                (while (consp value)
1440                  (setq cell (car value))
1441                  (if (and (consp cell)
1442                           (consp (car cell))
1443                           (setq ret (condition-case nil
1444                                         (find-char cell)
1445                                       (error nil))))
1446                      (progn
1447                        (setq rest cell
1448                              al nil
1449                              cal nil)
1450                        (while rest
1451                          (setq key (car (car rest)))
1452                          (if (find-charset key)
1453                              (setq cal (cons key cal))
1454                            (setq al (cons key al)))
1455                          (setq rest (cdr rest)))
1456                        (if separator
1457                            (insert lbs))
1458                        (insert-char-attributes ret
1459                                                readable
1460                                                al ; cal
1461                                                nil 'for-sub-node)
1462                        (setq separator lbs))
1463                    (setq ret (prin1-to-string cell))
1464                    (if separator
1465                        (if (< (+ (current-column)
1466                                  (length ret)
1467                                  (length separator))
1468                               76)
1469                            (insert separator)
1470                          (insert lbs)))
1471                    (insert ret)
1472                    (setq separator " "))
1473                  (setq value (cdr value)))
1474                (insert ")")
1475                (insert line-breaking))
1476               (t
1477                (insert (format "(%-18s" name))
1478                (setq ret (prin1-to-string value))
1479                (unless (< (+ (current-column)
1480                              (length ret)
1481                              3)
1482                           76)
1483                  (insert line-breaking))
1484                (insert " . " ret ")" line-breaking)
1485                ;; (insert (format "(%-18s . %S)%s"
1486                ;;                 name value
1487                ;;                 line-breaking))
1488                )
1489               ))
1490       (setq attributes (cdr attributes)))
1491     (insert ")")))
1492
1493 (defun insert-char-data (char &optional readable
1494                               attributes)
1495   (save-restriction
1496     (narrow-to-region (point)(point))
1497     (insert "(define-char
1498   '")
1499     (insert-char-attributes char readable attributes)
1500     (insert ")\n")
1501     (goto-char (point-min))
1502     (while (re-search-forward "[ \t]+$" nil t)
1503       (replace-match ""))
1504     ;; from tabify.
1505     (goto-char (point-min))
1506     (while (re-search-forward "[ \t][ \t][ \t]*" nil t)
1507       (let ((column (current-column))
1508             (indent-tabs-mode t))
1509         (delete-region (match-beginning 0) (point))
1510         (indent-to column)))
1511     (goto-char (point-max))
1512     ;; (tabify (point-min)(point-max))
1513     ))
1514
1515 (defun insert-char-data-with-variant (char &optional printable
1516                                            no-ucs-unified
1517                                            script excluded-script)
1518   (insert-char-data char printable)
1519   (let ((variants (char-variants char))
1520         rest
1521         variant vs ret)
1522     (setq variants (sort variants #'<))
1523     (setq rest variants)
1524     (setq variants (cons char variants))
1525     (while rest
1526       (setq variant (car rest))
1527       (unless (get-char-attribute variant '<-subsumptive)
1528         (if (and (or (null script)
1529                      (null (setq vs (get-char-attribute variant 'script)))
1530                      (memq script vs))
1531                  (or (null excluded-script)
1532                      (null (setq vs (get-char-attribute variant 'script)))
1533                      (not (memq excluded-script vs))))
1534             (unless (and no-ucs-unified (get-char-attribute variant '=ucs))
1535               (insert-char-data variant printable)
1536               (if (setq ret (char-variants variant))
1537                   (while ret
1538                     (or (memq (car ret) variants)
1539                         ;; (get-char-attribute (car ret) '<-subsumptive)
1540                         (setq rest (nconc rest (list (car ret)))))
1541                     (setq ret (cdr ret)))))))
1542       (setq rest (cdr rest)))))
1543
1544 (defun insert-char-range-data (min max &optional script excluded-script)
1545   (let ((code min)
1546         char)
1547     (while (<= code max)
1548       (setq char (decode-char '=ucs code))
1549       (if (encode-char char '=ucs 'defined-only)
1550           (insert-char-data-with-variant char nil 'no-ucs-unified
1551                                          script excluded-script))
1552       (setq code (1+ code)))))
1553
1554 (defun write-char-range-data-to-file (min max file
1555                                           &optional script excluded-script)
1556   (let ((coding-system-for-write char-db-file-coding-system))
1557     (with-temp-buffer
1558       (insert (format ";; -*- coding: %s -*-\n"
1559                       char-db-file-coding-system))
1560       (insert-char-range-data min max script excluded-script)
1561       (write-region (point-min)(point-max) file))))
1562
1563 (defvar what-character-original-window-configuration)
1564
1565 ;;;###autoload
1566 (defun what-char-definition (char)
1567   (interactive (list (char-after)))
1568   (let ((buf (get-buffer-create "*Character Description*"))
1569         (the-buf (current-buffer))
1570         (win-conf (current-window-configuration)))
1571     (pop-to-buffer buf)
1572     (make-local-variable 'what-character-original-window-configuration)
1573     (setq what-character-original-window-configuration win-conf)
1574     (setq buffer-read-only nil)
1575     (erase-buffer)
1576     (condition-case err
1577         (progn
1578           (insert-char-data-with-variant char 'printable)
1579           (unless (char-attribute-alist char)
1580             (insert (format ";; = %c\n"
1581                             (let* ((rest (split-char char))
1582                                    (ccs (pop rest))
1583                                    (code (pop rest)))
1584                               (while rest
1585                                 (setq code (logior (lsh code 8)
1586                                                    (pop rest))))
1587                               (decode-char ccs code)))))
1588           ;; (char-db-update-comment)
1589           (set-buffer-modified-p nil)
1590           (view-mode the-buf (lambda (buf)
1591                                (set-window-configuration
1592                                 what-character-original-window-configuration)
1593                                ))
1594           (goto-char (point-min)))
1595       (error (progn
1596                (set-window-configuration
1597                 what-character-original-window-configuration)
1598                (signal (car err) (cdr err)))))))
1599
1600
1601 ;;; @ end
1602 ;;;
1603
1604 (provide 'char-db-util)
1605
1606 ;;; char-db-util.el ends here