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