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