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