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