(char-db-insert-ccs-feature): Format `===hng-khi' as decimal.
[chise/xemacs-chise.git] / lisp / utf-2000 / char-db-util.el
1 ;;; char-db-util.el --- Character Database utility -*- coding: utf-8-er; -*-
2
3 ;; Copyright (C) 1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,
4 ;;   2008,2009,2010,2011,2012,2013,2014,2015 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                          ===hng-khi))
681             ;; (string-match "^=adobe-" (symbol-name name))
682             )
683         "(%-18s . %05d)\t; %c")
684        ((memq name '(=hanyo-denshi/ks
685                      ==hanyo-denshi/ks ===hanyo-denshi/ks
686                      =>>hanyo-denshi/ks
687                      =koseki
688                      =mj ==mj ===mj =>>mj
689                      =zihai mojikyo))
690         "(%-18s . %06d)\t; %c")
691        ((>= (charset-dimension name) 2)
692         "(%-18s . #x%04X)\t; %c")
693        (t
694         "(%-18s . #x%02X)\t; %c"))
695       name
696       (if (= (charset-iso-graphic-plane name) 1)
697           (logior value
698                   (cond ((= (charset-dimension name) 1)
699                          #x80)
700                         ((= (charset-dimension name) 2)
701                          #x8080)
702                         ((= (charset-dimension name) 3)
703                          #x808080)
704                         (t 0)))
705         value)
706       (char-db-decode-isolated-char name value)))
707     (if (and (= (charset-chars name) 94)
708              (= (charset-dimension name) 2))
709         (insert (format " [%02d-%02d]"
710                         (- (lsh value -8) 32)
711                         (- (logand value 255) 32))))
712     )
713    (t
714     (insert (format "(%-18s . %s)" name value))
715     ))
716   (insert line-breaking))
717
718 (defun char-db-insert-relation-feature (char name value line-breaking
719                                              ccss readable)
720   (insert (format "(%-18s%s " name line-breaking))
721   (let ((lbs (concat "\n" (make-string (current-column) ?\ )))
722         separator cell sources required-features
723         ret)
724     (while (consp value)
725       (setq cell (car value))
726       (if (integerp cell)
727           (setq cell (decode-char '=ucs cell)))
728       (cond
729        ((eq name '->subsumptive)
730         (when (or (not (some (lambda (atr)
731                                (get-char-attribute cell atr))
732                              char-db-ignored-attributes))
733                   (some (lambda (ccs)
734                           (encode-char cell ccs 'defined-only))
735                         ccss))
736           (if separator
737               (insert lbs))
738           (let ((char-db-ignored-attributes
739                  (cons '<-subsumptive
740                        char-db-ignored-attributes)))
741             (insert-char-attributes cell readable nil nil 'for-sub-node))
742           (setq separator lbs))
743         )
744        ((characterp cell)
745         (setq sources
746               (get-char-attribute
747                char (intern (format "%s*sources" name))))
748         (setq required-features nil)
749         (dolist (source sources)
750           (cond
751            ((memq source '(JP
752                            JP/Jouyou
753                            shinjigen shinjigen@1ed shinjigen@rev))
754             (setq required-features
755                   (union required-features
756                          '(=jis-x0208
757                            =jis-x0208@1990
758                            =jis-x0213-1@2000
759                            =jis-x0213-1@2004
760                            =jis-x0213-2
761                            =jis-x0212
762                            =jis-x0208@1983
763                            =jis-x0208@1978
764                            =shinjigen
765                            =shinjigen@1ed
766                            =shinjigen@rev
767                            =shinjigen/+p@rev))))
768            ((eq source 'CN)
769             (setq required-features
770                   (union required-features
771                          '(=gb2312
772                            =gb12345
773                            =iso-ir165)))))
774           (cond
775            ((find-charset (setq ret (intern (format "=%s" source))))
776             (setq required-features
777                   (cons ret required-features)))
778            (t (setq required-features
779                     (cons source required-features)))))
780         (cond ((string-match "@JP" (symbol-name name))
781                (setq required-features
782                      (union required-features
783                             '(=jis-x0208
784                               =jis-x0208@1990
785                               =jis-x0213-1-2000
786                               =jis-x0213-2-2000
787                               =jis-x0212
788                               =jis-x0208@1983
789                               =jis-x0208@1978))))
790               ((string-match "@CN" (symbol-name name))
791                (setq required-features
792                      (union required-features
793                             '(=gb2312
794                               =gb12345
795                               =iso-ir165)))))
796         (if separator
797             (insert lbs))
798         (if readable
799             (insert (format "%S" cell))
800           (char-db-insert-char-spec cell readable
801                                     nil
802                                     required-features))
803         (setq separator lbs))
804        ((consp cell)
805         (if separator
806             (insert lbs))
807         (if (consp (car cell))
808             (char-db-insert-char-spec cell readable)
809           (char-db-insert-char-reference cell readable))
810         (setq separator lbs))
811        (t
812         (if separator
813             (insert separator))
814         (insert (prin1-to-string cell))
815         (setq separator " ")))
816       (setq value (cdr value)))
817     (insert ")")
818     (insert line-breaking)))
819
820 (defun insert-char-attributes (char &optional readable attributes column
821                                     for-sub-node)
822   (unless column
823     (setq column (current-column)))
824   (let (name value ; has-long-ccs-name
825         rest
826         radical strokes
827         (line-breaking
828          (concat "\n" (make-string (1+ column) ?\ )))
829         lbs cell separator ret
830         key al cal
831         dest-ccss ; sources required-features
832         ccss)
833     (let (atr-d)
834       (setq attributes
835             (sort (if attributes
836                       (if (consp attributes)
837                           (progn
838                             (dolist (name attributes)
839                               (unless (memq name char-db-ignored-attributes)
840                                 (if (find-charset name)
841                                     (push name ccss))
842                                 (push name atr-d)))
843                             atr-d))
844                     (dolist (name (char-attribute-list))
845                       (unless (memq name char-db-ignored-attributes)
846                         (if (find-charset name)
847                             (push name ccss))
848                         (push name atr-d)))
849                     atr-d)
850                   #'char-attribute-name<)))
851     (insert "(")
852     (when (memq '<-subsumptive attributes)
853       (when (or readable (not for-sub-node))
854         (when (setq value (get-char-attribute char '<-subsumptive))
855           (char-db-insert-relation-feature char '<-subsumptive value
856                                            line-breaking
857                                            ccss readable)))
858       (setq attributes (delq '<-subsumptive attributes)))
859     (when (and (memq '<-denotational attributes)
860                (setq value (get-char-attribute char '<-denotational)))
861       (char-db-insert-relation-feature char '<-denotational value
862                                        line-breaking
863                                        ccss readable)
864       (setq attributes (delq '<-denotational attributes)))
865     (when (and (memq '<-denotational@component attributes)
866                (setq value (get-char-attribute char '<-denotational@component)))
867       (char-db-insert-relation-feature char '<-denotational@component value
868                                        line-breaking
869                                        ccss readable)
870       (setq attributes (delq '<-denotational@component attributes)))
871     (when (and (memq 'name attributes)
872                (setq value (get-char-attribute char 'name)))
873       (insert (format
874                (if (> (+ (current-column) (length value)) 48)
875                    "(name . %S)%s"
876                  "(name               . %S)%s")
877                value line-breaking))
878       (setq attributes (delq 'name attributes))
879       )
880     (when (and (memq 'name* attributes)
881                (setq value (get-char-attribute char 'name*)))
882       (insert (format
883                (if (> (+ (current-column) (length value)) 48)
884                    "(name* . %S)%s"
885                  "(name*              . %S)%s")
886                value line-breaking))
887       (setq attributes (delq 'name* attributes))
888       )
889     (when (and (memq 'script attributes)
890                (setq value (get-char-attribute char 'script)))
891       (insert (format "(script\t\t%s)%s"
892                       (mapconcat (function prin1-to-string)
893                                  value " ")
894                       line-breaking))
895       (setq attributes (delq 'script attributes))
896       )
897     (dolist (name '(=>ucs =>ucs*))
898       (when (and (memq name attributes)
899                  (setq value (get-char-attribute char name)))
900         (insert (format "(%-18s . #x%04X)\t; %c%s"
901                         name value (decode-char '=ucs value)
902                         line-breaking))
903         (setq attributes (delq name attributes))))
904     (dolist (name '(=>ucs@gb =>ucs@big5))
905       (when (and (memq name attributes)
906                  (setq value (get-char-attribute char name)))
907         (insert (format "(%-18s . #x%04X)\t; %c%s"
908                         name value
909                         (decode-char (intern
910                                       (concat "="
911                                               (substring
912                                                (symbol-name name) 2)))
913                                      value)
914                         line-breaking))
915         (setq attributes (delq name attributes))
916         ))
917     ;; (dolist (name '(=>daikanwa))
918     ;;   (when (and (memq name attributes)
919     ;;              (setq value (get-char-attribute char name)))
920     ;;     (insert
921     ;;      (if (integerp value)
922     ;;          (format "(%-18s . %05d)\t; %c%s"
923     ;;                  name value (decode-char '=daikanwa value)
924     ;;                  line-breaking)
925     ;;        (format "(%-18s %s)\t; %c%s"
926     ;;                name
927     ;;                (mapconcat (function prin1-to-string)
928     ;;                           value " ")
929     ;;                (char-representative-of-daikanwa char)
930     ;;                line-breaking)))
931     ;;     (setq attributes (delq name attributes))))
932     (when (and (memq 'general-category attributes)
933                (setq value (get-char-attribute char 'general-category)))
934       (insert (format
935                "(general-category\t%s) ; %s%s"
936                (mapconcat (lambda (cell)
937                             (format "%S" cell))
938                           value " ")
939                (cond ((rassoc value unidata-normative-category-alist)
940                       "Normative Category")
941                      ((rassoc value unidata-informative-category-alist)
942                       "Informative Category")
943                      (t
944                       "Unknown Category"))
945                line-breaking))
946       (setq attributes (delq 'general-category attributes))
947       )
948     (when (and (memq 'bidi-category attributes)
949                (setq value (get-char-attribute char 'bidi-category)))
950       (insert (format "(bidi-category\t. %S)%s"
951                       value
952                       line-breaking))
953       (setq attributes (delq 'bidi-category attributes))
954       )
955     (unless (or (not (memq 'mirrored attributes))
956                 (eq (setq value (get-char-attribute char 'mirrored 'empty))
957                     'empty))
958       (insert (format "(mirrored\t\t. %S)%s"
959                       value
960                       line-breaking))
961       (setq attributes (delq 'mirrored attributes))
962       )
963     (cond
964      ((and (memq 'decimal-digit-value attributes)
965            (setq value (get-char-attribute char 'decimal-digit-value)))
966       (insert (format "(decimal-digit-value . %S)%s"
967                       value
968                       line-breaking))
969       (setq attributes (delq 'decimal-digit-value attributes))
970       (when (and (memq 'digit-value attributes)
971                  (setq value (get-char-attribute char 'digit-value)))
972         (insert (format "(digit-value\t . %S)%s"
973                         value
974                         line-breaking))
975         (setq attributes (delq 'digit-value attributes))
976         )
977       (when (and (memq 'numeric-value attributes)
978                  (setq value (get-char-attribute char 'numeric-value)))
979         (insert (format "(numeric-value\t . %S)%s"
980                         value
981                         line-breaking))
982         (setq attributes (delq 'numeric-value attributes))
983         )
984       )
985      (t
986       (when (and (memq 'digit-value attributes)
987                  (setq value (get-char-attribute char 'digit-value)))
988         (insert (format "(digit-value\t. %S)%s"
989                         value
990                         line-breaking))
991         (setq attributes (delq 'digit-value attributes))
992         )
993       (when (and (memq 'numeric-value attributes)
994                  (setq value (get-char-attribute char 'numeric-value)))
995         (insert (format "(numeric-value\t. %S)%s"
996                         value
997                         line-breaking))
998         (setq attributes (delq 'numeric-value attributes))
999         )))
1000     (when (and (memq 'iso-10646-comment attributes)
1001                (setq value (get-char-attribute char 'iso-10646-comment)))
1002       (insert (format "(iso-10646-comment\t. %S)%s"
1003                       value
1004                       line-breaking))
1005       (setq attributes (delq 'iso-10646-comment attributes))
1006       )
1007     (when (and (memq 'morohashi-daikanwa attributes)
1008                (setq value (get-char-attribute char 'morohashi-daikanwa)))
1009       (insert (format "(morohashi-daikanwa\t%s)%s"
1010                       (mapconcat (function prin1-to-string) value " ")
1011                       line-breaking))
1012       (setq attributes (delq 'morohashi-daikanwa attributes))
1013       )
1014     (setq radical nil
1015           strokes nil)
1016     (when (and (memq 'ideographic-radical attributes)
1017                (setq value (get-char-attribute char 'ideographic-radical)))
1018       (setq radical value)
1019       (insert (format "(ideographic-radical . %S)\t; %c%s"
1020                       radical
1021                       (ideographic-radical radical)
1022                       line-breaking))
1023       (setq attributes (delq 'ideographic-radical attributes))
1024       )
1025     (when (and (memq 'shuowen-radical attributes)
1026                (setq value (get-char-attribute char 'shuowen-radical)))
1027       (insert (format "(shuowen-radical\t. %S)\t; %c%s"
1028                       value
1029                       (shuowen-radical value)
1030                       line-breaking))
1031       (setq attributes (delq 'shuowen-radical attributes))
1032       )
1033     (let (key)
1034       (dolist (domain
1035                (append
1036                 char-db-feature-domains
1037                 (let (dest domain)
1038                   (dolist (feature (char-attribute-list))
1039                     (setq feature (symbol-name feature))
1040                     (when (string-match
1041                            "\\(radical\\|strokes\\)@\\([^@*]+\\)\\(\\*\\|$\\)"
1042                            feature)
1043                       (setq domain (intern (match-string 2 feature)))
1044                      (unless (memq domain dest)
1045                        (setq dest (cons domain dest)))))
1046                   (sort dest #'string<))))
1047         (setq key (intern (format "%s@%s" 'ideographic-radical domain)))
1048         (when (and (memq key attributes)
1049                    (setq value (get-char-attribute char key)))
1050           (setq radical value)
1051           (insert (format "(%s . %S)\t; %c%s"
1052                           key
1053                           radical
1054                           (ideographic-radical radical)
1055                           line-breaking))
1056           (setq attributes (delq key attributes))
1057           )
1058         (setq key (intern (format "%s@%s" 'ideographic-strokes domain)))
1059         (when (and (memq key attributes)
1060                    (setq value (get-char-attribute char key)))
1061           (setq strokes value)
1062           (insert (format "(%s . %S)%s"
1063                           key
1064                           strokes
1065                           line-breaking))
1066           (setq attributes (delq key attributes))
1067           )
1068         (setq key (intern (format "%s@%s" 'total-strokes domain)))
1069         (when (and (memq key attributes)
1070                    (setq value (get-char-attribute char key)))
1071           (insert (format "(%s       . %S)%s"
1072                           key
1073                           value
1074                           line-breaking))
1075           (setq attributes (delq key attributes))
1076           )
1077         (dolist (feature '(ideographic-radical
1078                            ideographic-strokes
1079                            total-strokes))
1080           (setq key (intern (format "%s@%s*sources" feature domain)))
1081           (when (and (memq key attributes)
1082                      (setq value (get-char-attribute char key)))
1083             (insert (format "(%s%s" key line-breaking))
1084             (dolist (cell value)
1085               (insert (format " %s" cell)))
1086             (insert ")")
1087             (insert line-breaking)
1088             (setq attributes (delq key attributes))
1089             ))
1090         ))
1091     (when (and (memq 'ideographic-strokes attributes)
1092                (setq value (get-char-attribute char 'ideographic-strokes)))
1093       (setq strokes value)
1094       (insert (format "(ideographic-strokes . %S)%s"
1095                       strokes
1096                       line-breaking))
1097       (setq attributes (delq 'ideographic-strokes attributes))
1098       )
1099     (when (and (memq 'kangxi-radical attributes)
1100                (setq value (get-char-attribute char 'kangxi-radical)))
1101       (unless (eq value radical)
1102         (insert (format "(kangxi-radical\t . %S)\t; %c%s"
1103                         value
1104                         (ideographic-radical value)
1105                         line-breaking))
1106         (or radical
1107             (setq radical value)))
1108       (setq attributes (delq 'kangxi-radical attributes))
1109       )
1110     (when (and (memq 'kangxi-strokes attributes)
1111                (setq value (get-char-attribute char 'kangxi-strokes)))
1112       (unless (eq value strokes)
1113         (insert (format "(kangxi-strokes\t . %S)%s"
1114                         value
1115                         line-breaking))
1116         (or strokes
1117             (setq strokes value)))
1118       (setq attributes (delq 'kangxi-strokes attributes))
1119       )
1120     (when (and (memq 'japanese-radical attributes)
1121                (setq value (get-char-attribute char 'japanese-radical)))
1122       (unless (eq value radical)
1123         (insert (format "(japanese-radical\t . %S)\t; %c%s"
1124                         value
1125                         (ideographic-radical value)
1126                         line-breaking))
1127         (or radical
1128             (setq radical value)))
1129       (setq attributes (delq 'japanese-radical attributes))
1130       )
1131     (when (and (memq 'japanese-strokes attributes)
1132                (setq value (get-char-attribute char 'japanese-strokes)))
1133       (unless (eq value strokes)
1134         (insert (format "(japanese-strokes\t . %S)%s"
1135                         value
1136                         line-breaking))
1137         (or strokes
1138             (setq strokes value)))
1139       (setq attributes (delq 'japanese-strokes attributes))
1140       )
1141     (when (and (memq 'cns-radical attributes)
1142                (setq value (get-char-attribute char 'cns-radical)))
1143       (insert (format "(cns-radical\t . %S)\t; %c%s"
1144                       value
1145                       (ideographic-radical value)
1146                       line-breaking))
1147       (setq attributes (delq 'cns-radical attributes))
1148       )
1149     (when (and (memq 'cns-strokes attributes)
1150                (setq value (get-char-attribute char 'cns-strokes)))
1151       (unless (eq value strokes)
1152         (insert (format "(cns-strokes\t . %S)%s"
1153                         value
1154                         line-breaking))
1155         (or strokes
1156             (setq strokes value)))
1157       (setq attributes (delq 'cns-strokes attributes))
1158       )
1159     ;; (when (and (memq 'shinjigen-1-radical attributes)
1160     ;;            (setq value (get-char-attribute char 'shinjigen-1-radical)))
1161     ;;   (unless (eq value radical)
1162     ;;     (insert (format "(shinjigen-1-radical . %S)\t; %c%s"
1163     ;;                     value
1164     ;;                     (ideographic-radical value)
1165     ;;                     line-breaking))
1166     ;;     (or radical
1167     ;;         (setq radical value)))
1168     ;;   (setq attributes (delq 'shinjigen-1-radical attributes))
1169     ;;   )
1170     (when (and (memq 'ideographic- attributes)
1171                (setq value (get-char-attribute char 'ideographic-)))
1172       (insert "(ideographic-       ")
1173       (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1174             separator nil)
1175       (while (consp value)
1176         (setq cell (car value))
1177         (if (integerp cell)
1178             (setq cell (decode-char '=ucs cell)))
1179         (cond ((characterp cell)
1180                (if separator
1181                    (insert lbs))
1182                (if readable
1183                    (insert (format "%S" cell))
1184                  (char-db-insert-char-spec cell readable))
1185                (setq separator lbs))
1186               ((consp cell)
1187                (if separator
1188                    (insert lbs))
1189                (if (consp (car cell))
1190                    (char-db-insert-char-spec cell readable)
1191                  (char-db-insert-char-reference cell readable))
1192                (setq separator lbs))
1193               (t
1194                (if separator
1195                    (insert separator))
1196                (insert (prin1-to-string cell))
1197                (setq separator " ")))
1198         (setq value (cdr value)))
1199       (insert ")")
1200       (insert line-breaking)
1201       (setq attributes (delq 'ideographic- attributes)))
1202     (when (and (memq 'total-strokes attributes)
1203                (setq value (get-char-attribute char 'total-strokes)))
1204       (insert (format "(total-strokes       . %S)%s"
1205                       value
1206                       line-breaking))
1207       (setq attributes (delq 'total-strokes attributes))
1208       )
1209     (when (and (memq '->ideograph attributes)
1210                (setq value (get-char-attribute char '->ideograph)))
1211       (insert (format "(->ideograph\t%s)%s"
1212                       (mapconcat (lambda (code)
1213                                    (cond ((symbolp code)
1214                                           (symbol-name code))
1215                                          ((integerp code)
1216                                           (format "#x%04X" code))
1217                                          (t
1218                                           (format "%s %S"
1219                                                   line-breaking code))))
1220                                  value " ")
1221                       line-breaking))
1222       (setq attributes (delq '->ideograph attributes))
1223       )
1224     ;; (when (and (memq '->decomposition attributes)
1225     ;;            (setq value (get-char-attribute char '->decomposition)))
1226     ;;   (insert (format "(->decomposition\t%s)%s"
1227     ;;                   (mapconcat (lambda (code)
1228     ;;                                (cond ((symbolp code)
1229     ;;                                       (symbol-name code))
1230     ;;                                      ((characterp code)
1231     ;;                                       (if readable
1232     ;;                                           (format "%S" code)
1233     ;;                                         (format "#x%04X"
1234     ;;                                                 (char-int code))
1235     ;;                                         ))
1236     ;;                                      ((integerp code)
1237     ;;                                       (format "#x%04X" code))
1238     ;;                                      (t
1239     ;;                                       (format "%s%S" line-breaking code))))
1240     ;;                              value " ")
1241     ;;                   line-breaking))
1242     ;;   (setq attributes (delq '->decomposition attributes))
1243     ;;   )
1244     (if (equal (get-char-attribute char '->titlecase)
1245                (get-char-attribute char '->uppercase))
1246         (setq attributes (delq '->titlecase attributes)))
1247     (when (and (memq '->mojikyo attributes)
1248                (setq value (get-char-attribute char '->mojikyo)))
1249       (insert (format "(->mojikyo\t\t. %06d)\t; %c%s"
1250                       value (decode-char 'mojikyo value)
1251                       line-breaking))
1252       (setq attributes (delq '->mojikyo attributes))
1253       )
1254     (when (and (memq 'hanyu-dazidian-vol attributes)
1255                (setq value (get-char-attribute char 'hanyu-dazidian-vol)))
1256       (insert (format "(hanyu-dazidian-vol  . %d)%s"
1257                       value line-breaking))
1258       (setq attributes (delq 'hanyu-dazidian-vol attributes))
1259       )
1260     (when (and (memq 'hanyu-dazidian-page attributes)
1261                (setq value (get-char-attribute char 'hanyu-dazidian-page)))
1262       (insert (format "(hanyu-dazidian-page . %d)%s"
1263                       value line-breaking))
1264       (setq attributes (delq 'hanyu-dazidian-page attributes))
1265       )
1266     (when (and (memq 'hanyu-dazidian-char attributes)
1267                (setq value (get-char-attribute char 'hanyu-dazidian-char)))
1268       (insert (format "(hanyu-dazidian-char . %d)%s"
1269                       value line-breaking))
1270       (setq attributes (delq 'hanyu-dazidian-char attributes))
1271       )
1272     (unless readable
1273       (dolist (ignored '(composition
1274                          ->denotational <-subsumptive ->ucs-unified
1275                          ->ideographic-component-forms))
1276         (setq attributes (delq ignored attributes))))
1277     (while attributes
1278       (setq name (car attributes))
1279       (unless (eq (setq value (get-char-attribute char name 'value-is-empty))
1280                   'value-is-empty)
1281         (cond ((setq ret (find-charset name))
1282                (setq name (charset-name ret))
1283                (when (not (memq name dest-ccss))
1284                  (setq dest-ccss (cons name dest-ccss))
1285                  (char-db-insert-ccs-feature name value line-breaking))
1286                )
1287               ((string-match "^=>ucs@" (symbol-name name))
1288                (insert (format "(%-18s . #x%04X)\t; %c%s"
1289                                name value (decode-char '=ucs value)
1290                                line-breaking))
1291                )
1292               ((eq name 'jisx0208-1978/4X)
1293                (insert (format "(%-18s . #x%04X)%s"
1294                                name value
1295                                line-breaking))
1296                )
1297               ((and
1298                 (not readable)
1299                 (not (eq name '->subsumptive))
1300                 (not (eq name '->uppercase))
1301                 (not (eq name '->lowercase))
1302                 (not (eq name '->titlecase))
1303                 (not (eq name '->canonical))
1304                 (not (eq name '->Bopomofo))
1305                 (not (eq name '->mistakable))
1306                 (not (eq name '->ideographic-variants))
1307                 (null (get-char-attribute
1308                        char (intern (format "%s*sources" name))))
1309                 (not (string-match "\\*sources$" (symbol-name name)))
1310                 (null (get-char-attribute
1311                        char (intern (format "%s*note" name))))
1312                 (not (string-match "\\*note$" (symbol-name name)))
1313                 (or (eq name '<-identical)
1314                     (eq name '<-uppercase)
1315                     (eq name '<-lowercase)
1316                     (eq name '<-titlecase)
1317                     (eq name '<-canonical)
1318                     (eq name '<-ideographic-variants)
1319                     ;; (eq name '<-synonyms)
1320                     (string-match "^<-synonyms" (symbol-name name))
1321                     (eq name '<-mistakable)
1322                     (when (string-match "^->" (symbol-name name))
1323                       (cond
1324                        ((string-match "^->fullwidth" (symbol-name name))
1325                         (not (and (consp value)
1326                                   (characterp (car value))
1327                                   (encode-char
1328                                    (car value) '=ucs 'defined-only)))
1329                         )
1330                        (t)))
1331                     ))
1332                )
1333               ((or (eq name 'ideographic-structure)
1334                    (eq name 'ideographic-combination)
1335                    (eq name 'ideographic-)
1336                    (eq name '=decomposition)
1337                    (char-feature-base-name= '=decomposition name)
1338                    (char-feature-base-name= '=>decomposition name)
1339                    ;; (string-match "^=>*decomposition\\(@[^*]+\\)?$"
1340                    ;;               (symbol-name name))
1341                    (string-match "^\\(->\\|<-\\)[^*]*$" (symbol-name name))
1342                    (string-match "^\\(->\\|<-\\)[^*]*\\*sources$"
1343                                  (symbol-name name))
1344                    )
1345                (char-db-insert-relation-feature char name value
1346                                                 line-breaking
1347                                                 ccss readable))
1348               ((memq name '(ideograph=
1349                             original-ideograph-of
1350                             ancient-ideograph-of
1351                             vulgar-ideograph-of
1352                             wrong-ideograph-of
1353                             ;; simplified-ideograph-of
1354                             ideographic-variants
1355                             ;; ideographic-different-form-of
1356                             ))
1357                (insert (format "(%-18s%s " name line-breaking))
1358                (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1359                      separator nil)
1360                (while (consp value)
1361                  (setq cell (car value))
1362                  (if (and (consp cell)
1363                           (consp (car cell)))
1364                      (progn
1365                        (if separator
1366                            (insert lbs))
1367                        (char-db-insert-alist cell readable)
1368                        (setq separator lbs))
1369                    (if separator
1370                        (insert separator))
1371                    (insert (prin1-to-string cell))
1372                    (setq separator " "))
1373                  (setq value (cdr value)))
1374                (insert ")")
1375                (insert line-breaking))
1376               ((consp value)
1377                (insert (format "(%-18s " name))
1378                (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1379                      separator nil)
1380                (while (consp value)
1381                  (setq cell (car value))
1382                  (if (and (consp cell)
1383                           (consp (car cell))
1384                           (setq ret (condition-case nil
1385                                         (find-char cell)
1386                                       (error nil))))
1387                      (progn
1388                        (setq rest cell
1389                              al nil
1390                              cal nil)
1391                        (while rest
1392                          (setq key (car (car rest)))
1393                          (if (find-charset key)
1394                              (setq cal (cons key cal))
1395                            (setq al (cons key al)))
1396                          (setq rest (cdr rest)))
1397                        (if separator
1398                            (insert lbs))
1399                        (insert-char-attributes ret
1400                                                readable
1401                                                al ; cal
1402                                                nil 'for-sub-node)
1403                        (setq separator lbs))
1404                    (setq ret (prin1-to-string cell))
1405                    (if separator
1406                        (if (< (+ (current-column)
1407                                  (length ret)
1408                                  (length separator))
1409                               76)
1410                            (insert separator)
1411                          (insert lbs)))
1412                    (insert ret)
1413                    (setq separator " "))
1414                  (setq value (cdr value)))
1415                (insert ")")
1416                (insert line-breaking))
1417               (t
1418                (insert (format "(%-18s" name))
1419                (setq ret (prin1-to-string value))
1420                (unless (< (+ (current-column)
1421                              (length ret)
1422                              3)
1423                           76)
1424                  (insert line-breaking))
1425                (insert " . " ret ")" line-breaking)
1426                ;; (insert (format "(%-18s . %S)%s"
1427                ;;                 name value
1428                ;;                 line-breaking))
1429                )
1430               ))
1431       (setq attributes (cdr attributes)))
1432     (insert ")")))
1433
1434 (defun insert-char-data (char &optional readable
1435                               attributes)
1436   (save-restriction
1437     (narrow-to-region (point)(point))
1438     (insert "(define-char
1439   '")
1440     (insert-char-attributes char readable attributes)
1441     (insert ")\n")
1442     (goto-char (point-min))
1443     (while (re-search-forward "[ \t]+$" nil t)
1444       (replace-match ""))
1445     ;; from tabify.
1446     (goto-char (point-min))
1447     (while (re-search-forward "[ \t][ \t][ \t]*" nil t)
1448       (let ((column (current-column))
1449             (indent-tabs-mode t))
1450         (delete-region (match-beginning 0) (point))
1451         (indent-to column)))
1452     (goto-char (point-max))
1453     ;; (tabify (point-min)(point-max))
1454     ))
1455
1456 (defun insert-char-data-with-variant (char &optional printable
1457                                            no-ucs-unified
1458                                            script excluded-script)
1459   (insert-char-data char printable)
1460   (let ((variants (char-variants char))
1461         rest
1462         variant vs ret)
1463     (setq variants (sort variants #'<))
1464     (setq rest variants)
1465     (setq variants (cons char variants))
1466     (while rest
1467       (setq variant (car rest))
1468       (unless (get-char-attribute variant '<-subsumptive)
1469         (if (and (or (null script)
1470                      (null (setq vs (get-char-attribute variant 'script)))
1471                      (memq script vs))
1472                  (or (null excluded-script)
1473                      (null (setq vs (get-char-attribute variant 'script)))
1474                      (not (memq excluded-script vs))))
1475             (unless (and no-ucs-unified (get-char-attribute variant '=ucs))
1476               (insert-char-data variant printable)
1477               (if (setq ret (char-variants variant))
1478                   (while ret
1479                     (or (memq (car ret) variants)
1480                         ;; (get-char-attribute (car ret) '<-subsumptive)
1481                         (setq rest (nconc rest (list (car ret)))))
1482                     (setq ret (cdr ret)))))))
1483       (setq rest (cdr rest)))))
1484
1485 (defun insert-char-range-data (min max &optional script excluded-script)
1486   (let ((code min)
1487         char)
1488     (while (<= code max)
1489       (setq char (decode-char '=ucs code))
1490       (if (encode-char char '=ucs 'defined-only)
1491           (insert-char-data-with-variant char nil 'no-ucs-unified
1492                                          script excluded-script))
1493       (setq code (1+ code)))))
1494
1495 (defun write-char-range-data-to-file (min max file
1496                                           &optional script excluded-script)
1497   (let ((coding-system-for-write char-db-file-coding-system))
1498     (with-temp-buffer
1499       (insert (format ";; -*- coding: %s -*-\n"
1500                       char-db-file-coding-system))
1501       (insert-char-range-data min max script excluded-script)
1502       (write-region (point-min)(point-max) file))))
1503
1504 (defvar what-character-original-window-configuration)
1505
1506 ;;;###autoload
1507 (defun what-char-definition (char)
1508   (interactive (list (char-after)))
1509   (let ((buf (get-buffer-create "*Character Description*"))
1510         (the-buf (current-buffer))
1511         (win-conf (current-window-configuration)))
1512     (pop-to-buffer buf)
1513     (make-local-variable 'what-character-original-window-configuration)
1514     (setq what-character-original-window-configuration win-conf)
1515     (setq buffer-read-only nil)
1516     (erase-buffer)
1517     (condition-case err
1518         (progn
1519           (insert-char-data-with-variant char 'printable)
1520           (unless (char-attribute-alist char)
1521             (insert (format ";; = %c\n"
1522                             (let* ((rest (split-char char))
1523                                    (ccs (pop rest))
1524                                    (code (pop rest)))
1525                               (while rest
1526                                 (setq code (logior (lsh code 8)
1527                                                    (pop rest))))
1528                               (decode-char ccs code)))))
1529           ;; (char-db-update-comment)
1530           (set-buffer-modified-p nil)
1531           (view-mode the-buf (lambda (buf)
1532                                (set-window-configuration
1533                                 what-character-original-window-configuration)
1534                                ))
1535           (goto-char (point-min)))
1536       (error (progn
1537                (set-window-configuration
1538                 what-character-original-window-configuration)
1539                (signal (car err) (cdr err)))))))
1540
1541
1542 ;;; @ end
1543 ;;;
1544
1545 (provide 'char-db-util)
1546
1547 ;;; char-db-util.el ends here