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