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