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