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