Merge branch 'master' into single-inheritance.
[chise/xemacs-chise.git-] / lisp / utf-2000 / char-db-util.el
1 ;;; char-db-util.el --- Character Database utility -*- coding: utf-8-er; -*-
2
3 ;; Copyright (C) 1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,
4 ;;   2008,2009,2010,2011,2012,2013,2014,2015,2016,2017 MORIOKA Tomohiko.
5
6 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
7 ;; Keywords: CHISE, Character Database, ISO/IEC 10646, UCS, Unicode, MULE.
8
9 ;; This file is part of XEmacs CHISE.
10
11 ;; XEmacs CHISE is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; XEmacs CHISE is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs CHISE; see the file COPYING.  If not, write to
23 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Code:
27
28 (require 'chise-subr)
29 (require 'ideograph-subr)
30
31 (defconst unidata-normative-category-alist
32   '(("Lu" letter        uppercase)
33     ("Ll" letter        lowercase)
34     ("Lt" letter        titlecase)
35     ("Mn" mark          non-spacing)
36     ("Mc" mark          spacing-combining)
37     ("Me" mark          enclosing)
38     ("Nd" number        decimal-digit)
39     ("Nl" number        letter)
40     ("No" number        other)
41     ("Zs" separator     space)
42     ("Zl" separator     line)
43     ("Zp" separator     paragraph)
44     ("Cc" other         control)
45     ("Cf" other         format)
46     ("Cs" other         surrogate)
47     ("Co" other         private-use)
48     ("Cn" other         not-assigned)))
49
50 (defconst unidata-informative-category-alist
51   '(("Lm" letter        modifier)
52     ("Lo" letter        other)
53     ("Pc" punctuation   connector)
54     ("Pd" punctuation   dash)
55     ("Ps" punctuation   open)
56     ("Pe" punctuation   close)
57     ("Pi" punctuation   initial-quote)
58     ("Pf" punctuation   final-quote)
59     ("Po" punctuation   other)
60     ("Sm" symbol        math)
61     ("Sc" symbol        currency)
62     ("Sk" symbol        modifier)
63     ("So" symbol        other)
64     ))
65
66 (defconst shuowen-radicals
67   [?一 ?上 ?示 ?三 ?王 ?玉 ?玨 ?气 ?士 ?丨 ; 010
68    ?屮 ?艸 ?蓐 ?茻 ?小 ?八 ?釆 ?半 ?牛 ?犛 ; 020
69    ?告 ?口 ?凵 ?吅 ?哭 ?走 ?止 ?癶 ?步 ?此 ; 030
70    ?正 ?是 ?辵 ?彳 ?廴 ?㢟 ?行 ?齒 ?牙 ?足 ; 040
71    ?疋 ?品 ?龠 ?冊 ?㗊 ?舌 ?干 ?谷 ?只 ?㕯 ; 050
72    ?句 ?丩 ?古 ?十 ?卅 ?言 ?誩 ?音 ?䇂 ?丵 ; 060
73    ?菐 ?𠬞 ?𠬜 ?共 ?異 ?舁 ?𦥑 ?䢅 ?爨 ?革 ; 070
74    ?鬲 ?䰜 ?爪 ?𠃨 ?鬥 ?又 ?𠂇 ?㕜 ?支 ?𦘒 ; 080
75    ?聿 ?畫 ?隶 ?臤 ?臣 ?殳 ?殺 ?𠘧 ?寸 ?皮 ; 090
76    ?㼱 ?攴 ?敎 ?卜 ?用 ?爻 ?㸚 ?𥄎 ?目 ?䀠 ; 100
77    ?眉 ?盾 ?自 ?白 ?鼻 ?皕 ?習 ?羽 ?隹 ?奞 ; 110
78    ?萑 ?𦫳 ?苜 ?羊 ?羴 ?瞿 ?雔 ?雥 ?鳥 ?烏 ; 120
79    ?𠦒 ?冓 ?幺 ?𢆶 ?叀 ?玄 ?予 ?放 ?𠬪 ?𣦼 ; 130
80    ?歺 ?死 ?冎 ?骨 ?肉 ?筋 ?刀 ?刃 ?㓞 ?丰 ; 140
81    ?耒 ?𧢲 ?竹 ?箕 ?丌 ?左 ?工 ?㠭 ?巫 ?甘 ; 150
82    ?曰 ?乃 ?丂 ?可 ?兮 ?号 ?亏 ?旨 ?喜 ?壴 ; 160
83 ;  ?旨 ?曰 ?乃 ?丂 ?可 ?兮 ?号 ?亏 ?喜 ?壴 ; 160
84    ?鼓 ?豈 ?豆 ?豊 ?豐 ?䖒 ?虍 ?虎 ?虤 ?皿 ; 170
85    ?𠙴 ?去 ?血 ?丶 ?丹 ?青 ?井 ?皀 ?鬯 ?食 ; 180
86    ?亼 ?會 ?倉 ?入 ?缶 ?矢 ?高 ?冂 ?𩫏 ?京 ; 190
87    ?亯 ?𣆪 ?畗 ?㐭 ?嗇 ?來 ?麥 ?夊 ?舛 ?䑞 ; 200
88    ?韋 ?弟 ?夂 ?久 ?桀 ?木 ?東 ?林 ?才 ?叒 ; 210
89    ?之 ?帀 ?出 ?𣎵 ?生 ?乇 ?𠂹 ?𠌶 ?華 ?𥝌 ; 220
90    ?稽 ?巢 ?桼 ?束 ?㯻 ?囗 ?員 ?貝 ?邑 ?𨛜 ; 230
91    ?日 ?旦 ?倝 ?㫃 ?冥 ?晶 ?月 ?有 ?明 ?囧 ; 240
92    ?夕 ?多 ?毌 ?𢎘 ?𣐺 ?卣 ?齊 ?朿 ?片 ?鼎 ; 250
93    ?克 ?彔 ?禾 ?秝 ?黍 ?香 ?米 ?毇 ?臼 ?凶 ; 260
94    ?𣎳 ?林 ?麻 ?尗 ?耑 ?韭 ?瓜 ?瓠 ?宀 ?宮 ; 270
95    ?呂 ?穴 ?㝱 ?𤕫 ?冖 ?𠔼 ?冃 ?㒳 ?网 ?襾 ; 280
96    ?巾 ?巿 ?帛 ?白 ?㡀 ?黹 ?人 ?𠤎 ?匕 ?从 ; 290
97    ?比 ?北 ?丘 ?㐺 ?𡈼 ?重 ?臥 ?身 ?㐆 ?衣 ; 300
98    ?裘 ?老 ?毛 ?毳 ?尸 ?尺 ?尾 ?履 ?舟 ?方 ; 310
99    ?儿 ?兄 ?兂 ?皃 ?𠑹 ?先 ?秃 ?見 ?覞 ?欠 ; 320
100    ?㱃 ?㳄 ?旡 ?頁 ?𦣻 ?面 ?丏 ?首 ?𥄉 ?須 ; 330
101    ?彡 ?彣 ?文 ?髟 ?后 ?司 ?卮 ?卩 ?印 ?色 ; 340
102    ?𠨍 ?辟 ?勹 ?包 ?茍 ?鬼 ?甶 ?厶 ?嵬 ?山 ; 350
103    ?屾 ?屵 ?广 ?厂 ?丸 ?危 ?石 ?長 ?勿 ?冄 ; 360
104    ?而 ?豕 ?㣇 ?彑 ?豚 ?豸 ?𤉡 ?易 ?象 ?馬 ; 370
105    ?𢊁 ?鹿 ?麤 ?㲋 ?兔 ?萈 ?犬 ?㹜 ?鼠 ?能 ; 380
106    ?熊 ?火 ?炎 ?黑 ?囪 ?焱 ?炙 ?赤 ?大 ?亦 ; 390
107    ])
108
109 (defun shuowen-radical (number)
110   (aref shuowen-radicals (1- number)))
111
112 (defvar char-db-file-coding-system 'utf-8-mcs-er)
113
114 (defvar char-db-ignored-attributes '(ideographic-products))
115
116 (defvar char-db-coded-charset-priority-list
117   '(ascii
118     control-1
119     latin-iso8859-1
120     latin-iso8859-2
121     latin-iso8859-3
122     latin-iso8859-4
123     latin-iso8859-9
124     latin-jisx0201
125     cyrillic-iso8859-5
126     greek-iso8859-7
127     thai-tis620
128     ;; =mj
129     ;; =adobe-japan1-0
130     ;; =adobe-japan1-1
131     ;; =adobe-japan1-2
132     ;; =adobe-japan1-3
133     ;; =adobe-japan1-4
134     ;; =adobe-japan1-5
135     ;; =adobe-japan1-6
136     =jis-x0208
137     =jis-x0208@1978
138     =jis-x0208@1983
139     japanese-jisx0212
140     chinese-gb2312
141     =jis-x0208@1990
142     chinese-cns11643-1
143     chinese-cns11643-2
144     chinese-cns11643-3
145     chinese-cns11643-4
146     chinese-cns11643-5
147     chinese-cns11643-6
148     chinese-cns11643-7
149     =jis-x0213-1
150     =jis-x0213-1@2000
151     =jis-x0213-1@2004
152     =jis-x0213-2
153     korean-ksc5601
154     chinese-isoir165
155     katakana-jisx0201
156     hebrew-iso8859-8
157     chinese-gb12345
158     latin-viscii
159     ethiopic-ucs
160     =big5-cdp
161     =hanyo-denshi/ja
162     =hanyo-denshi/jb
163     =hanyo-denshi/jc
164     =hanyo-denshi/jd
165     =hanyo-denshi/ft
166     =hanyo-denshi/ia
167     =hanyo-denshi/ib
168     =hanyo-denshi/hg
169     =hanyo-denshi/jt
170     =hanyo-denshi/ks
171     =hanyo-denshi/tk
172     ;; ==mj
173     ;; ==adobe-japan1-0
174     ;; ==adobe-japan1-1
175     ;; ==adobe-japan1-2
176     ;; ==adobe-japan1-3
177     ;; ==adobe-japan1-4
178     ;; ==adobe-japan1-5
179     ;; ==adobe-japan1-6
180     ==jis-x0208
181     ==jis-x0213-1
182     ==jis-x0213-2
183     ==jis-x0212
184     ==hanyo-denshi/ja
185     ==hanyo-denshi/jb
186     ==hanyo-denshi/jc
187     ==hanyo-denshi/ft
188     ==hanyo-denshi/ia
189     ==hanyo-denshi/ib
190     ==hanyo-denshi/hg
191     ==hanyo-denshi/jt
192     ==hanyo-denshi/ks
193     ==hanyo-denshi/tk
194     =gt
195     =gt-k
196     =daikanwa
197     =daikanwa@rev2
198     =daikanwa@rev1
199     =daikanwa/+p
200     ==daikanwa
201     =cbeta
202     ideograph-hanziku-1
203     ideograph-hanziku-2
204     ideograph-hanziku-3
205     ideograph-hanziku-4
206     ideograph-hanziku-5
207     ideograph-hanziku-6
208     ideograph-hanziku-7
209     ideograph-hanziku-8
210     ideograph-hanziku-9
211     ideograph-hanziku-10
212     ideograph-hanziku-11
213     ideograph-hanziku-12
214     ;; =>>>adobe-japan1-0
215     ;; =>>>adobe-japan1-1
216     ;; =>>>adobe-japan1-2
217     ;; =>>>adobe-japan1-3
218     ;; =>>>adobe-japan1-4
219     ;; =>>>adobe-japan1-5
220     ;; =>>>adobe-japan1-6
221     ;; =>>>jis-x0208
222     ;; =>>>jis-x0213-1
223     ;; =>>>jis-x0213-2
224     ;; =>>>hanyo-denshi/ja
225     ;; =>>>hanyo-denshi/jb
226     ;; =>>>hanyo-denshi/jc
227     ;; =>>>hanyo-denshi/ft
228     ;; =>>>hanyo-denshi/ib
229     ;; =>>>hanyo-denshi/hg
230     ;; =>>>hanyo-denshi/jt
231     ;; =>>>hanyo-denshi/ks
232     ;; =>>>gt
233     =>>adobe-japan1-0
234     =>>adobe-japan1-1
235     =>>adobe-japan1-2
236     =>>adobe-japan1-3
237     =>>adobe-japan1-4
238     =>>adobe-japan1-5
239     =>>adobe-japan1-6
240     =>>jis-x0208
241     =>>jis-x0213-1
242     =>>jis-x0213-1@2000
243     =>>jis-x0213-1@2004
244     =>>jis-x0213-2
245     =>>jis-x0208@1978
246     =>>hanyo-denshi/ft
247     =>>hanyo-denshi/jt
248     =>>hanyo-denshi/ks
249     =>>gt
250     =>>daikanwa
251     =>>cbeta
252     =+>jis-x0208
253     =+>jis-x0213-1
254     =+>jis-x0213-2
255     =+>adobe-japan1-0
256     =+>adobe-japan1-1
257     =+>adobe-japan1-2
258     =+>adobe-japan1-3
259     =+>adobe-japan1-4
260     =+>adobe-japan1-5
261     =+>adobe-japan1-6
262     =+>jis-x0208@1978
263     =>jis-x0208
264     =>jis-x0208@1997
265     =>jis-x0213-1
266     =>jis-x0213-1@2000
267     =>jis-x0213-1@2004
268     =>jis-x0213-2
269     ==>ucs@bucs
270     =>iwds-1
271     =>ucs@hanyo-denshi
272     =>ucs@iso
273     =>ucs@unicode
274     =>ucs@jis
275     =>ucs@cns
276     =>ucs@ks
277     =+>ucs@iso
278     =+>ucs@unicode
279     =+>ucs@jis
280     =+>ucs@jis/1990
281     =+>ucs@cns
282     =+>ucs@ks
283     =>>ucs@iso
284     =>>ucs@unicode
285     =>>ucs@jis
286     =>>ucs@cns
287     =>>>ucs@iso
288     =>>>ucs@unicode
289     ==ucs@iso
290     ==ucs@unicode
291     ;; ==ucs@cns
292     ==gb2312
293     ==ks-x1001
294     ==cns11643-1
295     ==cns11643-2
296     ==cns11643-3
297     ==cns11643-4
298     ==cns11643-5
299     ==cns11643-6
300     ==cns11643-7
301     ==gt
302     ==jis-x0208@1990
303     ;; ==jis-x0208@1983
304     ==jis-x0208@1978
305     ==gt-k
306     =ucs@iso
307     =ucs@unicode
308     =ucs@cns
309     ==big5-cdp
310     ==cbeta
311     =>>big5-cdp
312     =>>gt-k
313     =+>gt
314     =>gt
315     =>mj
316     =>big5-cdp
317     =>daikanwa
318     =>daikanwa/ho
319     =>cns11643-5
320     =>cns11643-7
321     =big5
322     =big5-eten
323     =>gt-k
324     =zinbun-oracle
325     =>zinbun-oracle
326     =ruimoku-v6
327     =>>ruimoku-v6
328     ==ruimoku-v6
329     =jef-china3
330     =>cbeta
331     =shinjigen
332     =ucs-var-001
333     =ucs-var-002
334     =ucs-var-003
335     =ucs-var-004
336     =ucs-var-005
337     =ucs-var-006
338     =ucs-var-008
339     =ucs-var-010
340     =ucs-itaiji-001
341     =ucs-itaiji-002
342     =ucs-itaiji-003
343     =ucs-itaiji-004
344     =ucs-itaiji-005
345     =ucs-itaiji-006
346     =ucs-itaiji-008
347     =ucs-itaiji-009
348     =>ucs-itaiji-001
349     =>ucs-itaiji-002
350     =>ucs-itaiji-005
351     =>ucs-itaiji-006
352     =big5-cdp-var-001
353     =big5-cdp-var-002
354     =big5-cdp-var-003
355     =big5-cdp-var-004
356     =big5-cdp-var-005
357     =big5-cdp-itaiji-001
358     =big5-cdp-itaiji-002
359     =big5-cdp-itaiji-003
360     =>big5-cdp-itaiji-001
361     =>ucs@iwds-1
362     =>ucs@component
363     ==ucs@gb
364     =ucs@JP/hanazono))
365
366
367 ;;; @ char-db formatters
368 ;;;
369
370 (defun char-db-make-char-spec (char)
371   (let (ret char-spec)
372     (cond ((characterp char)
373            (cond ((and (setq ret (encode-char char '=ucs 'defined-only))
374                        (not (and (<= #xE000 ret)(<= ret #xF8FF))))
375                   (setq char-spec (list (cons '=ucs ret)))
376                   (cond ((setq ret (get-char-attribute char 'name))
377                          (setq char-spec (cons (cons 'name ret) char-spec))
378                          )
379                         ((setq ret (get-char-attribute char 'name*))
380                          (setq char-spec (cons (cons 'name* ret) char-spec))
381                          ))
382                   )
383                  ((encode-char char '=mj 'defined-only)
384                   (setq char-spec nil)
385                   (dolist (ccs (charset-list))
386                     (if (and (or (eq ccs '=mj)
387                                  ;; (eq (charset-property ccs 'iso-ir) 177)
388                                  (string-match "=ucs@" (symbol-name ccs))
389                                  )
390                              (setq ccs (charset-name ccs))
391                              (null (assq ccs char-spec))
392                              (setq ret (encode-char char ccs 'defined-only)))
393                         (setq char-spec (cons (cons ccs ret) char-spec))))
394                   )
395                  ((encode-char char '==mj 'defined-only)
396                   (setq char-spec nil)
397                   (dolist (ccs (charset-list))
398                     (if (and (or (eq ccs '==mj)
399                                  ;; (eq (charset-property ccs 'iso-ir) 177)
400                                  (string-match "=ucs@" (symbol-name ccs))
401                                  )
402                              (setq ccs (charset-name ccs))
403                              (null (assq ccs char-spec))
404                              (setq ret (encode-char char ccs 'defined-only)))
405                         (setq char-spec (cons (cons ccs ret) char-spec))))
406                   )
407                  ((encode-char char '=adobe-japan1 'defined-only)
408                   (setq char-spec nil)
409                   (dolist (ccs (charset-list))
410                     (if (and (or (memq ccs
411                                        '(=adobe-japan1-0
412                                          =adobe-japan1-1
413                                          =adobe-japan1-2
414                                          =adobe-japan1-3
415                                          =adobe-japan1-4
416                                          =adobe-japan1-5
417                                          =adobe-japan1-6
418                                          ))
419                                  ;; (eq (charset-property ccs 'iso-ir) 177)
420                                  (string-match "=ucs@" (symbol-name ccs))
421                                  )
422                              (setq ccs (charset-name ccs))
423                              (null (assq ccs char-spec))
424                              (setq ret (encode-char char ccs 'defined-only)))
425                         (setq char-spec (cons (cons ccs ret) char-spec))))
426                   )
427                  ((encode-char char '==adobe-japan1 'defined-only)
428                   (setq char-spec nil)
429                   (dolist (ccs (charset-list))
430                     (if (and (or (memq ccs
431                                        '(==adobe-japan1-0
432                                          ==adobe-japan1-1
433                                          ==adobe-japan1-2
434                                          ==adobe-japan1-3
435                                          ==adobe-japan1-4
436                                          ==adobe-japan1-5
437                                          ==adobe-japan1-6
438                                          ))
439                                  ;; (eq (charset-property ccs 'iso-ir) 177)
440                                  (string-match "=ucs@" (symbol-name ccs))
441                                  )
442                              (setq ccs (charset-name ccs))
443                              (null (assq ccs char-spec))
444                              (setq ret (encode-char char ccs 'defined-only)))
445                         (setq char-spec (cons (cons ccs ret) char-spec))))
446                   )
447                  ((setq ret
448                         (catch 'tag
449                           (let ((rest char-db-coded-charset-priority-list)
450                                 ccs)
451                             (while rest
452                               (setq ccs (charset-name
453                                          (find-charset (car rest))))
454                               (if (setq ret
455                                         (encode-char char ccs
456                                                      'defined-only))
457                                   (throw 'tag (cons ccs ret)))
458                               (setq rest (cdr rest))))))
459                   (setq char-spec (list ret))
460                   (dolist (ccs (delq (car ret) (charset-list)))
461                     (if (and (or (charset-iso-final-char ccs)
462                                  (memq ccs
463                                        '(=daikanwa
464                                          =daikanwa@rev2
465                                          ;; =gt-k
466                                          =jis-x0208@1997
467                                          ))
468                                  (eq (charset-property ccs 'iso-ir) 177)
469                                  ;; (string-match "=ucs@" (symbol-name ccs))
470                                  )
471                              (setq ccs (charset-name ccs))
472                              (null (assq ccs char-spec))
473                              (setq ret (encode-char char ccs 'defined-only)))
474                         (setq char-spec (cons (cons ccs ret) char-spec))))
475                   (if (null char-spec)
476                       (setq char-spec (split-char char)))
477                   (cond ((setq ret (get-char-attribute char 'name))
478                          (setq char-spec (cons (cons 'name ret) char-spec))
479                          )
480                         ((setq ret (get-char-attribute char 'name*))
481                          (setq char-spec (cons (cons 'name* ret) char-spec))
482                          ))
483                   )
484                  ((setq ret (get-char-attribute
485                              char 'ideographic-combination))
486                   (setq char-spec
487                         (cons (cons 'ideographic-combination ret)
488                               char-spec))
489                   ))
490            char-spec)
491           ((consp char)
492            char))))
493     
494 (defun char-db-insert-char-spec (char &optional readable column
495                                       required-features)
496   (unless column
497     (setq column (current-column)))
498   (let (char-spec temp-char)
499     (setq char-spec (char-db-make-char-spec char))
500     (unless (or (characterp char) ; char
501                 (condition-case nil
502                     (setq char (find-char char-spec))
503                   (error nil)))
504       ;; define temporary character
505       ;;   Current implementation is dirty.
506       (setq temp-char (define-char (cons '(ideograph-daikanwa . 0)
507                                          char-spec)))
508       (remove-char-attribute temp-char 'ideograph-daikanwa)
509       (setq char temp-char))
510     (insert-char-attributes char
511                             readable
512                             (union (mapcar #'car char-spec)
513                                    required-features)
514                             nil 'for-sub-node)
515     (when temp-char
516       ;; undefine temporary character
517       ;;   Current implementation is dirty.
518       (setq char-spec (char-attribute-alist temp-char))
519       (while char-spec
520         (remove-char-attribute temp-char (car (car char-spec)))
521         (setq char-spec (cdr char-spec))))))
522
523 (defun char-db-insert-alist (alist &optional readable column)
524   (unless column
525     (setq column (current-column)))
526   (let ((line-breaking
527          (concat "\n" (make-string (1+ column) ?\ )))
528         name value
529         ret al ; cal
530         key
531         lbs cell rest separator)
532     (insert "(")
533     (while alist
534       (setq name (car (car alist))
535             value (cdr (car alist)))
536       (cond ((eq name 'char)
537              (insert "(char . ")
538              (if (setq ret (condition-case nil
539                                (find-char value)
540                              (error nil)))
541                  (progn
542                    (setq al nil
543                          ;; cal nil
544                          )
545                    (while value
546                      (setq key (car (car value)))
547                      ;; (if (find-charset key)
548                      ;;     (setq cal (cons key cal))
549                      (setq al (cons key al))
550                      ;; )
551                      (setq value (cdr value)))
552                    (insert-char-attributes ret
553                                            readable
554                                            (or al 'none) ; cal
555                                            nil 'for-sub-node))
556                (insert (prin1-to-string value)))
557              (insert ")")
558              (insert line-breaking))
559             ((consp value)
560              (insert (format "(%-18s " name))
561              (setq lbs (concat "\n" (make-string (current-column) ?\ )))
562              (while (consp value)
563                (setq cell (car value))
564                (if (and (consp cell)
565                         (consp (car cell))
566                         (setq ret (condition-case nil
567                                       (find-char cell)
568                                     (error nil)))
569                         )
570                    (progn
571                      (setq rest cell
572                            al nil
573                            ;; cal nil
574                            )
575                      (while rest
576                        (setq key (car (car rest)))
577                        ;; (if (find-charset key)
578                        ;;     (setq cal (cons key cal))
579                        (setq al (cons key al))
580                        ;; )
581                        (setq rest (cdr rest)))
582                      (if separator
583                          (insert lbs))
584                      (insert-char-attributes ret
585                                              readable
586                                              al ; cal
587                                              nil 'for-sub-node)
588                      (setq separator lbs))
589                  (if separator
590                      (insert separator))
591                  (insert (prin1-to-string cell))
592                  (setq separator " "))
593                (setq value (cdr value)))
594              (insert ")")
595              (insert line-breaking))
596             (t
597              (insert (format "(%-18s . %S)%s"
598                              name value
599                              line-breaking))))
600       (setq alist (cdr alist))))
601   (insert ")"))
602
603 (defun char-db-insert-char-reference (plist &optional readable column)
604   (unless column
605     (setq column (current-column)))
606   (let ((line-breaking
607          (concat "\n" (make-string (1+ column) ?\ )))
608         (separator "")
609         name value)
610     (insert "(")
611     (while plist
612       (setq name (pop plist))
613       (setq value (pop plist))
614       (cond ((eq name :char)
615              (insert separator)
616              (insert ":char\t")
617              (cond ((numberp value)
618                     (setq value (decode-char '=ucs value)))
619                    ;; ((consp value)
620                    ;;  (setq value (or (find-char value)
621                    ;;                  value)))
622                    )
623              (char-db-insert-char-spec value readable)
624              (insert line-breaking)
625              (setq separator ""))
626             ((eq name :radical)
627              (insert (format "%s%s\t%d ; %c%s"
628                              separator
629                              name value
630                              (ideographic-radical value)
631                              line-breaking))
632              (setq separator ""))
633             (t
634              (insert (format "%s%s\t%S" separator name value))
635              (setq separator line-breaking)))
636       ))
637   (insert ")"))
638
639 (defun char-db-decode-isolated-char (ccs code-point)
640   (let (ret)
641     (setq ret
642           (cond ((eq ccs 'arabic-iso8859-6)
643                  (decode-char ccs code-point))
644                 ;; ((eq ccs '=gt)
645                 ;;  (decode-builtin-char '==gt code-point))
646                 ((and (memq ccs '(=gt-pj-1
647                                   =gt-pj-2
648                                   =gt-pj-3
649                                   =gt-pj-4
650                                   =gt-pj-5
651                                   =gt-pj-6
652                                   =gt-pj-7
653                                   =gt-pj-8
654                                   =gt-pj-9
655                                   =gt-pj-10
656                                   =gt-pj-11))
657                       (setq ret (decode-char ccs code-point))
658                       (setq ret (encode-char ret '=gt 'defined-only)))
659                  (decode-builtin-char '=gt ret))
660                 (t
661                  (decode-builtin-char ccs code-point))))
662     (cond ((null ret)
663            (or (decode-char ccs code-point)
664                (define-char (list (cons ccs code-point)))))
665           ((and (<= 0 (char-int ret))
666                 (<= (char-int ret) #x1F))
667            (decode-char '=ucs (+ #x2400 (char-int ret))))
668           ((= (char-int ret) #x7F)
669            ?\u2421)
670           (t ret))))
671
672 (defvar char-db-convert-obsolete-format t)
673
674 (defun char-db-insert-ccs-feature (name value line-breaking)
675   (cond
676    ((integerp value)
677     (insert
678      (format
679       (cond
680        ((memq name '(=>iwds-1
681                      ==shinjigen
682                      =shinjigen
683                      =shinjigen@1ed ==shinjigen@1ed
684                      =shinjigen@rev ==shinjigen@rev
685                      =shinjigen/+p@rev ==shinjigen/+p@rev
686                      ===daikanwa/ho ==daikanwa/ho
687                      =daikanwa/ho =>>daikanwa/ho =>daikanwa/ho))
688         "(%-18s .  %04d)\t; %c")
689        ((eq name '=shinjigen@1ed/24pr)
690         "(%-18s . %04d)\t; %c")
691        ((or
692          (memq name
693                '(===daikanwa
694                  ==daikanwa =daikanwa =>>daikanwa =>daikanwa
695                  =daikanwa@rev1 =daikanwa@rev2
696                  =daikanwa/+p ==daikanwa/+p ===daikanwa/+p
697                  =>>daikanwa/+p
698                  =daikanwa/+2p =>>daikanwa/+2p
699                  =gt ==gt ===gt
700                  =>>gt =+>gt =>gt
701                  =gt-k ==gt-k ===gt-k
702                  =>>gt-k =>gt-k
703                  =adobe-japan1-0 ==adobe-japan1-0 ===adobe-japan1-0
704                  =adobe-japan1-1 ==adobe-japan1-1 ===adobe-japan1-1
705                  =adobe-japan1-2 ==adobe-japan1-2 ===adobe-japan1-2
706                  =adobe-japan1-3 ==adobe-japan1-3 ===adobe-japan1-3
707                  =adobe-japan1-4 ==adobe-japan1-4 ===adobe-japan1-4
708                  =adobe-japan1-5 ==adobe-japan1-5 ===adobe-japan1-5
709                  =adobe-japan1-6 ==adobe-japan1-6 ===adobe-japan1-6
710                  =>>adobe-japan1-0 =+>adobe-japan1-0
711                  =>>adobe-japan1-1 =+>adobe-japan1-1
712                  =>>adobe-japan1-2 =+>adobe-japan1-2
713                  =>>adobe-japan1-3 =+>adobe-japan1-3
714                  =>>adobe-japan1-4 =+>adobe-japan1-4
715                  =>>adobe-japan1-5 =+>adobe-japan1-5
716                  =>>adobe-japan1-6 =+>adobe-japan1-6
717                  =>cbeta =cbeta =>>cbeta ==cbeta ===cbeta
718                  =zinbun-oracle =>zinbun-oracle
719                  ===hng-jou ===hng-keg ===hng-dng ===hng-mam
720                  ===hng-drt ===hng-kgk ===hng-myz ===hng-kda
721                  ===hng-khi ===hng-khm ===hng-hok ===hng-kyd ===hng-sok
722                  ===hng-yhk ===hng-kak ===hng-kar ===hng-kae
723                  ===hng-sys ===hng-tsu ===hng-tzj
724                  ===hng-hos ===hng-nak ===hng-jhk
725                  ===hng-hod ===hng-gok ===hng-ink ===hng-nto
726                  ===hng-nkm ===hng-k24 ===hng-nkk
727                  ===hng-kcc ===hng-kcj ===hng-kbk ===hng-sik
728                  ===hng-skk ===hng-kyu ===hng-ksk ===hng-wan
729                  ===hng-okd ===hng-wad ===hng-kmi
730                  ===hng-zkd ===hng-doh ===hng-jyu
731                  ===hng-tzs ===hng-kss ===hng-kyo
732                  ===hng-smk))
733          ;; (string-match "^=adobe-" (symbol-name name))
734          )
735         "(%-18s . %05d)\t; %c")
736        ((memq name '(=hanyo-denshi/ks
737                      ==hanyo-denshi/ks ===hanyo-denshi/ks
738                      =>>hanyo-denshi/ks
739                      =koseki ==koseki
740                      =mj ==mj ===mj =>>mj =>mj
741                      =zihai mojikyo))
742         "(%-18s . %06d)\t; %c")
743        ((memq name '(=hanyo-denshi/tk ==hanyo-denshi/tk))
744         "(%-18s . %08d)\t; %c")
745        ((>= (charset-dimension name) 2)
746         "(%-18s . #x%04X)\t; %c")
747        (t
748         "(%-18s . #x%02X)\t; %c"))
749       name
750       (if (= (charset-iso-graphic-plane name) 1)
751           (logior value
752                   (cond ((= (charset-dimension name) 1)
753                          #x80)
754                         ((= (charset-dimension name) 2)
755                          #x8080)
756                         ((= (charset-dimension name) 3)
757                          #x808080)
758                         (t 0)))
759         value)
760       (char-db-decode-isolated-char name value)))
761     (if (and (= (charset-chars name) 94)
762              (= (charset-dimension name) 2))
763         (insert (format " [%02d-%02d]"
764                         (- (lsh value -8) 32)
765                         (- (logand value 255) 32))))
766     )
767    (t
768     (insert (format "(%-18s . %s)" name value))
769     ))
770   (insert line-breaking))
771
772 (defun char-db-insert-relation-feature (char name value line-breaking
773                                              ccss readable)
774   (insert (format "(%-18s%s " name line-breaking))
775   (let ((lbs (concat "\n" (make-string (current-column) ?\ )))
776         separator cell sources required-features
777         ret)
778     (if (characterp value)
779         (setq value (list value)))
780     (while (consp value)
781       (setq cell (car value))
782       (if (integerp cell)
783           (setq cell (decode-char '=ucs cell)))
784       (cond
785        ((eq name '->subsumptive)
786         (when (or (not (some (lambda (atr)
787                                (get-char-attribute cell atr))
788                              char-db-ignored-attributes))
789                   (some (lambda (ccs)
790                           (encode-char cell ccs 'defined-only))
791                         ccss))
792           (if separator
793               (insert lbs))
794           (let ((char-db-ignored-attributes
795                  (cons '<-subsumptive
796                        char-db-ignored-attributes)))
797             (insert-char-attributes cell readable nil nil 'for-sub-node))
798           (setq separator lbs))
799         )
800        ((characterp cell)
801         (setq sources
802               (get-char-attribute
803                char (intern (format "%s*sources" name))))
804         (setq required-features nil)
805         (dolist (source sources)
806           (cond
807            ((memq source '(JP
808                            JP/Jouyou
809                            shinjigen shinjigen@1ed shinjigen@rev))
810             (setq required-features
811                   (union required-features
812                          '(=jis-x0208
813                            =jis-x0208@1990
814                            =jis-x0213-1@2000
815                            =jis-x0213-1@2004
816                            =jis-x0213-2
817                            =jis-x0212
818                            =jis-x0208@1983
819                            =jis-x0208@1978
820                            =shinjigen
821                            =shinjigen@1ed
822                            =shinjigen@rev
823                            =shinjigen/+p@rev))))
824            ((eq source 'CN)
825             (setq required-features
826                   (union required-features
827                          '(=gb2312
828                            =gb12345
829                            =iso-ir165)))))
830           (cond
831            ((find-charset (setq ret (intern (format "=%s" source))))
832             (setq required-features
833                   (cons ret required-features)))
834            (t (setq required-features
835                     (cons source required-features)))))
836         (cond ((string-match "@JP" (symbol-name name))
837                (setq required-features
838                      (union required-features
839                             '(=jis-x0208
840                               =jis-x0208@1990
841                               =jis-x0213-1-2000
842                               =jis-x0213-2-2000
843                               =jis-x0212
844                               =jis-x0208@1983
845                               =jis-x0208@1978))))
846               ((string-match "@CN" (symbol-name name))
847                (setq required-features
848                      (union required-features
849                             '(=gb2312
850                               =gb12345
851                               =iso-ir165)))))
852         (if separator
853             (insert lbs))
854         (if readable
855             (insert (format "%S" cell))
856           (char-db-insert-char-spec cell readable
857                                     nil
858                                     required-features))
859         (setq separator lbs))
860        ((consp cell)
861         (if separator
862             (insert lbs))
863         (if (consp (car cell))
864             (char-db-insert-char-spec cell readable)
865           (char-db-insert-char-reference cell readable))
866         (setq separator lbs))
867        (t
868         (if separator
869             (insert separator))
870         (insert (prin1-to-string cell))
871         (setq separator " ")))
872       (setq value (cdr value)))
873     (insert ")")
874     (insert line-breaking)))
875
876 (defun insert-char-attributes (char &optional readable attributes column
877                                     for-sub-node)
878   (unless column
879     (setq column (current-column)))
880   (let (name value ; has-long-ccs-name
881         rest
882         radical strokes
883         (line-breaking
884          (concat "\n" (make-string (1+ column) ?\ )))
885         lbs cell separator ret
886         key al cal
887         dest-ccss ; sources required-features
888         ccss)
889     (let (atr-d)
890       (setq attributes
891             (sort (if attributes
892                       (if (consp attributes)
893                           (progn
894                             (dolist (name attributes)
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                     (dolist (name (char-attribute-list))
901                       (unless (memq name char-db-ignored-attributes)
902                         (if (find-charset name)
903                             (push name ccss))
904                         (push name atr-d)))
905                     atr-d)
906                   #'char-attribute-name<)))
907     (insert "(")
908     (when (memq '<-subsumptive attributes)
909       (when (or readable (not for-sub-node))
910         (when (setq value (get-char-attribute char '<-subsumptive))
911           (char-db-insert-relation-feature char '<-subsumptive value
912                                            line-breaking
913                                            ccss readable)))
914       (setq attributes (delq '<-subsumptive attributes)))
915     (when (and (memq '<-denotational attributes)
916                (setq value (get-char-attribute char '<-denotational)))
917       (char-db-insert-relation-feature char '<-denotational value
918                                        line-breaking
919                                        ccss readable)
920       (setq attributes (delq '<-denotational attributes)))
921     (when (and (memq '<-denotational@component attributes)
922                (setq value (get-char-attribute char '<-denotational@component)))
923       (char-db-insert-relation-feature char '<-denotational@component value
924                                        line-breaking
925                                        ccss readable)
926       (setq attributes (delq '<-denotational@component attributes)))
927     (when (and (memq 'name attributes)
928                (setq value (get-char-attribute char 'name)))
929       (insert (format
930                (if (> (+ (current-column) (length value)) 48)
931                    "(name . %S)%s"
932                  "(name               . %S)%s")
933                value line-breaking))
934       (setq attributes (delq 'name attributes))
935       )
936     (when (and (memq 'name* attributes)
937                (setq value (get-char-attribute char 'name*)))
938       (insert (format
939                (if (> (+ (current-column) (length value)) 48)
940                    "(name* . %S)%s"
941                  "(name*              . %S)%s")
942                value line-breaking))
943       (setq attributes (delq 'name* attributes))
944       )
945     (when (and (memq 'script attributes)
946                (setq value (get-char-attribute char 'script)))
947       (insert (format "(script\t\t%s)%s"
948                       (mapconcat (function prin1-to-string)
949                                  value " ")
950                       line-breaking))
951       (setq attributes (delq 'script attributes))
952       )
953     (dolist (name '(=>ucs =>ucs*))
954       (when (and (memq name attributes)
955                  (setq value (get-char-attribute char name)))
956         (insert (format "(%-18s . #x%04X)\t; %c%s"
957                         name value (decode-char '=ucs value)
958                         line-breaking))
959         (setq attributes (delq name attributes))))
960     (dolist (name '(=>ucs@gb =>ucs@big5))
961       (when (and (memq name attributes)
962                  (setq value (get-char-attribute char name)))
963         (insert (format "(%-18s . #x%04X)\t; %c%s"
964                         name value
965                         (decode-char (intern
966                                       (concat "="
967                                               (substring
968                                                (symbol-name name) 2)))
969                                      value)
970                         line-breaking))
971         (setq attributes (delq name attributes))
972         ))
973     ;; (dolist (name '(=>daikanwa))
974     ;;   (when (and (memq name attributes)
975     ;;              (setq value (get-char-attribute char name)))
976     ;;     (insert
977     ;;      (if (integerp value)
978     ;;          (format "(%-18s . %05d)\t; %c%s"
979     ;;                  name value (decode-char '=daikanwa value)
980     ;;                  line-breaking)
981     ;;        (format "(%-18s %s)\t; %c%s"
982     ;;                name
983     ;;                (mapconcat (function prin1-to-string)
984     ;;                           value " ")
985     ;;                (char-representative-of-daikanwa char)
986     ;;                line-breaking)))
987     ;;     (setq attributes (delq name attributes))))
988     (when (and (memq 'general-category attributes)
989                (setq value (get-char-attribute char 'general-category)))
990       (insert (format
991                "(general-category\t%s) ; %s%s"
992                (mapconcat (lambda (cell)
993                             (format "%S" cell))
994                           value " ")
995                (cond ((rassoc value unidata-normative-category-alist)
996                       "Normative Category")
997                      ((rassoc value unidata-informative-category-alist)
998                       "Informative Category")
999                      (t
1000                       "Unknown Category"))
1001                line-breaking))
1002       (setq attributes (delq 'general-category attributes))
1003       )
1004     (when (and (memq 'bidi-category attributes)
1005                (setq value (get-char-attribute char 'bidi-category)))
1006       (insert (format "(bidi-category\t. %S)%s"
1007                       value
1008                       line-breaking))
1009       (setq attributes (delq 'bidi-category attributes))
1010       )
1011     (unless (or (not (memq 'mirrored attributes))
1012                 (eq (setq value (get-char-attribute char 'mirrored 'empty))
1013                     'empty))
1014       (insert (format "(mirrored\t\t. %S)%s"
1015                       value
1016                       line-breaking))
1017       (setq attributes (delq 'mirrored attributes))
1018       )
1019     (cond
1020      ((and (memq 'decimal-digit-value attributes)
1021            (setq value (get-char-attribute char 'decimal-digit-value)))
1022       (insert (format "(decimal-digit-value . %S)%s"
1023                       value
1024                       line-breaking))
1025       (setq attributes (delq 'decimal-digit-value attributes))
1026       (when (and (memq 'digit-value attributes)
1027                  (setq value (get-char-attribute char 'digit-value)))
1028         (insert (format "(digit-value\t . %S)%s"
1029                         value
1030                         line-breaking))
1031         (setq attributes (delq 'digit-value attributes))
1032         )
1033       (when (and (memq 'numeric-value attributes)
1034                  (setq value (get-char-attribute char 'numeric-value)))
1035         (insert (format "(numeric-value\t . %S)%s"
1036                         value
1037                         line-breaking))
1038         (setq attributes (delq 'numeric-value attributes))
1039         )
1040       )
1041      (t
1042       (when (and (memq 'digit-value attributes)
1043                  (setq value (get-char-attribute char 'digit-value)))
1044         (insert (format "(digit-value\t. %S)%s"
1045                         value
1046                         line-breaking))
1047         (setq attributes (delq 'digit-value attributes))
1048         )
1049       (when (and (memq 'numeric-value attributes)
1050                  (setq value (get-char-attribute char 'numeric-value)))
1051         (insert (format "(numeric-value\t. %S)%s"
1052                         value
1053                         line-breaking))
1054         (setq attributes (delq 'numeric-value attributes))
1055         )))
1056     (when (and (memq 'iso-10646-comment attributes)
1057                (setq value (get-char-attribute char 'iso-10646-comment)))
1058       (insert (format "(iso-10646-comment\t. %S)%s"
1059                       value
1060                       line-breaking))
1061       (setq attributes (delq 'iso-10646-comment attributes))
1062       )
1063     (when (and (memq 'morohashi-daikanwa attributes)
1064                (setq value (get-char-attribute char 'morohashi-daikanwa)))
1065       (insert (format "(morohashi-daikanwa\t%s)%s"
1066                       (mapconcat (function prin1-to-string) value " ")
1067                       line-breaking))
1068       (setq attributes (delq 'morohashi-daikanwa attributes))
1069       )
1070     (setq radical nil
1071           strokes nil)
1072     (when (and (memq 'ideographic-radical attributes)
1073                (setq value (get-char-attribute char 'ideographic-radical)))
1074       (setq radical value)
1075       (insert (format "(ideographic-radical . %S)\t; %c%s"
1076                       radical
1077                       (ideographic-radical radical)
1078                       line-breaking))
1079       (setq attributes (delq 'ideographic-radical attributes))
1080       )
1081     (when (and (memq 'shuowen-radical attributes)
1082                (setq value (get-char-attribute char 'shuowen-radical)))
1083       (insert (format "(shuowen-radical\t. %S)\t; %c%s"
1084                       value
1085                       (shuowen-radical value)
1086                       line-breaking))
1087       (setq attributes (delq 'shuowen-radical attributes))
1088       )
1089     (let (key)
1090       (dolist (domain
1091                (append
1092                 char-db-feature-domains
1093                 (let (dest domain)
1094                   (dolist (feature (char-attribute-list))
1095                     (setq feature (symbol-name feature))
1096                     (when (string-match
1097                            "\\(radical\\|strokes\\)@\\([^@*]+\\)\\(\\*\\|$\\)"
1098                            feature)
1099                       (setq domain (intern (match-string 2 feature)))
1100                      (unless (memq domain dest)
1101                        (setq dest (cons domain dest)))))
1102                   (sort dest #'string<))))
1103         (setq key (intern (format "%s@%s" 'ideographic-radical domain)))
1104         (when (and (memq key attributes)
1105                    (setq value (get-char-attribute char key)))
1106           (setq radical value)
1107           (insert (format "(%s . %S)\t; %c%s"
1108                           key
1109                           radical
1110                           (ideographic-radical radical)
1111                           line-breaking))
1112           (setq attributes (delq key attributes))
1113           )
1114         (setq key (intern (format "%s@%s" 'ideographic-strokes domain)))
1115         (when (and (memq key attributes)
1116                    (setq value (get-char-attribute char key)))
1117           (setq strokes value)
1118           (insert (format "(%s . %S)%s"
1119                           key
1120                           strokes
1121                           line-breaking))
1122           (setq attributes (delq key attributes))
1123           )
1124         (setq key (intern (format "%s@%s" 'total-strokes domain)))
1125         (when (and (memq key attributes)
1126                    (setq value (get-char-attribute char key)))
1127           (insert (format "(%s       . %S)%s"
1128                           key
1129                           value
1130                           line-breaking))
1131           (setq attributes (delq key attributes))
1132           )
1133         (dolist (feature '(ideographic-radical
1134                            ideographic-strokes
1135                            total-strokes))
1136           (setq key (intern (format "%s@%s*sources" feature domain)))
1137           (when (and (memq key attributes)
1138                      (setq value (get-char-attribute char key)))
1139             (insert (format "(%s%s" key line-breaking))
1140             (dolist (cell value)
1141               (insert (format " %s" cell)))
1142             (insert ")")
1143             (insert line-breaking)
1144             (setq attributes (delq key attributes))
1145             ))
1146         ))
1147     (when (and (memq 'ideographic-strokes attributes)
1148                (setq value (get-char-attribute char 'ideographic-strokes)))
1149       (setq strokes value)
1150       (insert (format "(ideographic-strokes . %S)%s"
1151                       strokes
1152                       line-breaking))
1153       (setq attributes (delq 'ideographic-strokes attributes))
1154       )
1155     (when (and (memq 'kangxi-radical attributes)
1156                (setq value (get-char-attribute char 'kangxi-radical)))
1157       (unless (eq value radical)
1158         (insert (format "(kangxi-radical\t . %S)\t; %c%s"
1159                         value
1160                         (ideographic-radical value)
1161                         line-breaking))
1162         (or radical
1163             (setq radical value)))
1164       (setq attributes (delq 'kangxi-radical attributes))
1165       )
1166     (when (and (memq 'kangxi-strokes attributes)
1167                (setq value (get-char-attribute char 'kangxi-strokes)))
1168       (unless (eq value strokes)
1169         (insert (format "(kangxi-strokes\t . %S)%s"
1170                         value
1171                         line-breaking))
1172         (or strokes
1173             (setq strokes value)))
1174       (setq attributes (delq 'kangxi-strokes attributes))
1175       )
1176     ;; (when (and (memq 'japanese-radical attributes)
1177     ;;            (setq value (get-char-attribute char 'japanese-radical)))
1178     ;;   (unless (eq value radical)
1179     ;;     (insert (format "(japanese-radical\t . %S)\t; %c%s"
1180     ;;                     value
1181     ;;                     (ideographic-radical value)
1182     ;;                     line-breaking))
1183     ;;     (or radical
1184     ;;         (setq radical value)))
1185     ;;   (setq attributes (delq 'japanese-radical attributes))
1186     ;;   )
1187     (when (and (memq 'japanese-strokes attributes)
1188                (setq value (get-char-attribute char 'japanese-strokes)))
1189       (unless (eq value strokes)
1190         (insert (format "(japanese-strokes\t . %S)%s"
1191                         value
1192                         line-breaking))
1193         (or strokes
1194             (setq strokes value)))
1195       (setq attributes (delq 'japanese-strokes attributes))
1196       )
1197     (when (and (memq 'cns-radical attributes)
1198                (setq value (get-char-attribute char 'cns-radical)))
1199       (insert (format "(cns-radical\t . %S)\t; %c%s"
1200                       value
1201                       (ideographic-radical value)
1202                       line-breaking))
1203       (setq attributes (delq 'cns-radical attributes))
1204       )
1205     (when (and (memq 'cns-strokes attributes)
1206                (setq value (get-char-attribute char 'cns-strokes)))
1207       (unless (eq value strokes)
1208         (insert (format "(cns-strokes\t . %S)%s"
1209                         value
1210                         line-breaking))
1211         (or strokes
1212             (setq strokes value)))
1213       (setq attributes (delq 'cns-strokes attributes))
1214       )
1215     ;; (when (and (memq 'shinjigen-1-radical attributes)
1216     ;;            (setq value (get-char-attribute char 'shinjigen-1-radical)))
1217     ;;   (unless (eq value radical)
1218     ;;     (insert (format "(shinjigen-1-radical . %S)\t; %c%s"
1219     ;;                     value
1220     ;;                     (ideographic-radical value)
1221     ;;                     line-breaking))
1222     ;;     (or radical
1223     ;;         (setq radical value)))
1224     ;;   (setq attributes (delq 'shinjigen-1-radical attributes))
1225     ;;   )
1226     ;; (when (and (memq 'ideographic- attributes)
1227     ;;            (setq value (get-char-attribute char 'ideographic-)))
1228     ;;   (insert "(ideographic-       ")
1229     ;;   (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1230     ;;         separator nil)
1231     ;;   (while (consp value)
1232     ;;     (setq cell (car value))
1233     ;;     (if (integerp cell)
1234     ;;         (setq cell (decode-char '=ucs cell)))
1235     ;;     (cond ((characterp cell)
1236     ;;            (if separator
1237     ;;                (insert lbs))
1238     ;;            (if readable
1239     ;;                (insert (format "%S" cell))
1240     ;;              (char-db-insert-char-spec cell readable))
1241     ;;            (setq separator lbs))
1242     ;;           ((consp cell)
1243     ;;            (if separator
1244     ;;                (insert lbs))
1245     ;;            (if (consp (car cell))
1246     ;;                (char-db-insert-char-spec cell readable)
1247     ;;              (char-db-insert-char-reference cell readable))
1248     ;;            (setq separator lbs))
1249     ;;           (t
1250     ;;            (if separator
1251     ;;                (insert separator))
1252     ;;            (insert (prin1-to-string cell))
1253     ;;            (setq separator " ")))
1254     ;;     (setq value (cdr value)))
1255     ;;   (insert ")")
1256     ;;   (insert line-breaking)
1257     ;;   (setq attributes (delq 'ideographic- attributes)))
1258     (when (and (memq 'total-strokes attributes)
1259                (setq value (get-char-attribute char 'total-strokes)))
1260       (insert (format "(total-strokes       . %S)%s"
1261                       value
1262                       line-breaking))
1263       (setq attributes (delq 'total-strokes attributes))
1264       )
1265     ;; (when (and (memq '->ideograph attributes)
1266     ;;            (setq value (get-char-attribute char '->ideograph)))
1267     ;;   (insert (format "(->ideograph\t%s)%s"
1268     ;;                   (mapconcat (lambda (code)
1269     ;;                                (cond ((symbolp code)
1270     ;;                                       (symbol-name code))
1271     ;;                                      ((integerp code)
1272     ;;                                       (format "#x%04X" code))
1273     ;;                                      (t
1274     ;;                                       (format "%s %S"
1275     ;;                                               line-breaking code))))
1276     ;;                              value " ")
1277     ;;                   line-breaking))
1278     ;;   (setq attributes (delq '->ideograph attributes))
1279     ;;   )
1280     ;; (when (and (memq '->decomposition attributes)
1281     ;;            (setq value (get-char-attribute char '->decomposition)))
1282     ;;   (insert (format "(->decomposition\t%s)%s"
1283     ;;                   (mapconcat (lambda (code)
1284     ;;                                (cond ((symbolp code)
1285     ;;                                       (symbol-name code))
1286     ;;                                      ((characterp code)
1287     ;;                                       (if readable
1288     ;;                                           (format "%S" code)
1289     ;;                                         (format "#x%04X"
1290     ;;                                                 (char-int code))
1291     ;;                                         ))
1292     ;;                                      ((integerp code)
1293     ;;                                       (format "#x%04X" code))
1294     ;;                                      (t
1295     ;;                                       (format "%s%S" line-breaking code))))
1296     ;;                              value " ")
1297     ;;                   line-breaking))
1298     ;;   (setq attributes (delq '->decomposition attributes))
1299     ;;   )
1300     (if (equal (get-char-attribute char '->titlecase)
1301                (get-char-attribute char '->uppercase))
1302         (setq attributes (delq '->titlecase attributes)))
1303     (when (and (memq '->mojikyo attributes)
1304                (setq value (get-char-attribute char '->mojikyo)))
1305       (insert (format "(->mojikyo\t\t. %06d)\t; %c%s"
1306                       value (decode-char 'mojikyo value)
1307                       line-breaking))
1308       (setq attributes (delq '->mojikyo attributes))
1309       )
1310     ;; (when (and (memq 'hanyu-dazidian-vol attributes)
1311     ;;            (setq value (get-char-attribute char 'hanyu-dazidian-vol)))
1312     ;;   (insert (format "(hanyu-dazidian-vol  . %d)%s"
1313     ;;                   value line-breaking))
1314     ;;   (setq attributes (delq 'hanyu-dazidian-vol attributes))
1315     ;;   )
1316     ;; (when (and (memq 'hanyu-dazidian-page attributes)
1317     ;;            (setq value (get-char-attribute char 'hanyu-dazidian-page)))
1318     ;;   (insert (format "(hanyu-dazidian-page . %d)%s"
1319     ;;                   value line-breaking))
1320     ;;   (setq attributes (delq 'hanyu-dazidian-page attributes))
1321     ;;   )
1322     ;; (when (and (memq 'hanyu-dazidian-char attributes)
1323     ;;            (setq value (get-char-attribute char 'hanyu-dazidian-char)))
1324     ;;   (insert (format "(hanyu-dazidian-char . %d)%s"
1325     ;;                   value line-breaking))
1326     ;;   (setq attributes (delq 'hanyu-dazidian-char attributes))
1327     ;;   )
1328     (unless readable
1329       (dolist (ignored '(composition
1330                          ->denotational <-subsumptive ->ucs-unified
1331                          ->ideographic-component-forms))
1332         (setq attributes (delq ignored attributes))))
1333     (while attributes
1334       (setq name (car attributes))
1335       (unless (eq (setq value (get-char-attribute char name 'value-is-empty))
1336                   'value-is-empty)
1337         (cond ((setq ret (find-charset name))
1338                (setq name (charset-name ret))
1339                (when (not (memq name dest-ccss))
1340                  (setq dest-ccss (cons name dest-ccss))
1341                  (char-db-insert-ccs-feature name value line-breaking))
1342                )
1343               ((string-match "^=>ucs@" (symbol-name name))
1344                (insert (format "(%-18s . #x%04X)\t; %c%s"
1345                                name value (decode-char '=ucs value)
1346                                line-breaking))
1347                )
1348               ((eq name 'jisx0208-1978/4X)
1349                (insert (format "(%-18s . #x%04X)%s"
1350                                name value
1351                                line-breaking))
1352                )
1353               ((and
1354                 (not readable)
1355                 (not (eq name '->subsumptive))
1356                 (not (eq name '->uppercase))
1357                 (not (eq name '->lowercase))
1358                 (not (eq name '->titlecase))
1359                 (not (eq name '->canonical))
1360                 (not (eq name '->Bopomofo))
1361                 (not (eq name '->mistakable))
1362                 (not (eq name '->ideographic-variants))
1363                 (null (get-char-attribute
1364                        char (intern (format "%s*sources" name))))
1365                 (not (string-match "\\*sources$" (symbol-name name)))
1366                 (null (get-char-attribute
1367                        char (intern (format "%s*note" name))))
1368                 (not (string-match "\\*note$" (symbol-name name)))
1369                 (or (eq name '<-identical)
1370                     (eq name '<-uppercase)
1371                     (eq name '<-lowercase)
1372                     (eq name '<-titlecase)
1373                     (eq name '<-canonical)
1374                     (eq name '<-ideographic-variants)
1375                     ;; (eq name '<-synonyms)
1376                     (string-match "^<-synonyms" (symbol-name name))
1377                     (eq name '<-mistakable)
1378                     (when (string-match "^->" (symbol-name name))
1379                       (cond
1380                        ((string-match "^->fullwidth" (symbol-name name))
1381                         (not (and (consp value)
1382                                   (characterp (car value))
1383                                   (encode-char
1384                                    (car value) '=ucs 'defined-only)))
1385                         )
1386                        (t)))
1387                     ))
1388                )
1389               ((or (eq name 'ideographic-structure)
1390                    (eq name 'ideographic-combination)
1391                    ;; (eq name 'ideographic-)
1392                    (eq name '=decomposition)
1393                    (char-feature-base-name= '=decomposition name)
1394                    (char-feature-base-name= '=>decomposition name)
1395                    ;; (string-match "^=>*decomposition\\(@[^*]+\\)?$"
1396                    ;;               (symbol-name name))
1397                    (string-match "^\\(->\\|<-\\)[^*]*$" (symbol-name name))
1398                    (string-match "^\\(->\\|<-\\)[^*]*\\*sources$"
1399                                  (symbol-name name))
1400                    )
1401                (char-db-insert-relation-feature char name value
1402                                                 line-breaking
1403                                                 ccss readable))
1404               ;; ((memq name '(ideograph=
1405               ;;               original-ideograph-of
1406               ;;               ancient-ideograph-of
1407               ;;               vulgar-ideograph-of
1408               ;;               wrong-ideograph-of
1409               ;;               ;; simplified-ideograph-of
1410               ;;               ideographic-variants
1411               ;;               ;; ideographic-different-form-of
1412               ;;               ))
1413               ;;  (insert (format "(%-18s%s " name line-breaking))
1414               ;;  (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1415               ;;        separator nil)
1416               ;;  (while (consp value)
1417               ;;    (setq cell (car value))
1418               ;;    (if (and (consp cell)
1419               ;;             (consp (car cell)))
1420               ;;        (progn
1421               ;;          (if separator
1422               ;;              (insert lbs))
1423               ;;          (char-db-insert-alist cell readable)
1424               ;;          (setq separator lbs))
1425               ;;      (if separator
1426               ;;          (insert separator))
1427               ;;      (insert (prin1-to-string cell))
1428               ;;      (setq separator " "))
1429               ;;    (setq value (cdr value)))
1430               ;;  (insert ")")
1431               ;;  (insert line-breaking))
1432               ((consp value)
1433                (insert (format "(%-18s " name))
1434                (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1435                      separator nil)
1436                (while (consp value)
1437                  (setq cell (car value))
1438                  (if (and (consp cell)
1439                           (consp (car cell))
1440                           (setq ret (condition-case nil
1441                                         (find-char cell)
1442                                       (error nil))))
1443                      (progn
1444                        (setq rest cell
1445                              al nil
1446                              cal nil)
1447                        (while rest
1448                          (setq key (car (car rest)))
1449                          (if (find-charset key)
1450                              (setq cal (cons key cal))
1451                            (setq al (cons key al)))
1452                          (setq rest (cdr rest)))
1453                        (if separator
1454                            (insert lbs))
1455                        (insert-char-attributes ret
1456                                                readable
1457                                                al ; cal
1458                                                nil 'for-sub-node)
1459                        (setq separator lbs))
1460                    (setq ret (prin1-to-string cell))
1461                    (if separator
1462                        (if (< (+ (current-column)
1463                                  (length ret)
1464                                  (length separator))
1465                               76)
1466                            (insert separator)
1467                          (insert lbs)))
1468                    (insert ret)
1469                    (setq separator " "))
1470                  (setq value (cdr value)))
1471                (insert ")")
1472                (insert line-breaking))
1473               (t
1474                (insert (format "(%-18s" name))
1475                (setq ret (prin1-to-string value))
1476                (unless (< (+ (current-column)
1477                              (length ret)
1478                              3)
1479                           76)
1480                  (insert line-breaking))
1481                (insert " . " ret ")" line-breaking)
1482                ;; (insert (format "(%-18s . %S)%s"
1483                ;;                 name value
1484                ;;                 line-breaking))
1485                )
1486               ))
1487       (setq attributes (cdr attributes)))
1488     (insert ")")))
1489
1490 (defun insert-char-data (char &optional readable
1491                               attributes)
1492   (save-restriction
1493     (narrow-to-region (point)(point))
1494     (insert "(define-char
1495   '")
1496     (insert-char-attributes char readable attributes)
1497     (insert ")\n")
1498     (goto-char (point-min))
1499     (while (re-search-forward "[ \t]+$" nil t)
1500       (replace-match ""))
1501     ;; from tabify.
1502     (goto-char (point-min))
1503     (while (re-search-forward "[ \t][ \t][ \t]*" nil t)
1504       (let ((column (current-column))
1505             (indent-tabs-mode t))
1506         (delete-region (match-beginning 0) (point))
1507         (indent-to column)))
1508     (goto-char (point-max))
1509     ;; (tabify (point-min)(point-max))
1510     ))
1511
1512 (defun insert-char-data-with-variant (char &optional printable
1513                                            no-ucs-unified
1514                                            script excluded-script)
1515   (insert-char-data char printable)
1516   (let ((variants (char-variants char))
1517         rest
1518         variant vs ret)
1519     (setq variants (sort variants #'<))
1520     (setq rest variants)
1521     (setq variants (cons char variants))
1522     (while rest
1523       (setq variant (car rest))
1524       (unless (get-char-attribute variant '<-subsumptive)
1525         (if (and (or (null script)
1526                      (null (setq vs (get-char-attribute variant 'script)))
1527                      (memq script vs))
1528                  (or (null excluded-script)
1529                      (null (setq vs (get-char-attribute variant 'script)))
1530                      (not (memq excluded-script vs))))
1531             (unless (and no-ucs-unified (get-char-attribute variant '=ucs))
1532               (insert-char-data variant printable)
1533               (if (setq ret (char-variants variant))
1534                   (while ret
1535                     (or (memq (car ret) variants)
1536                         ;; (get-char-attribute (car ret) '<-subsumptive)
1537                         (setq rest (nconc rest (list (car ret)))))
1538                     (setq ret (cdr ret)))))))
1539       (setq rest (cdr rest)))))
1540
1541 (defun insert-char-range-data (min max &optional script excluded-script)
1542   (let ((code min)
1543         char)
1544     (while (<= code max)
1545       (setq char (decode-char '=ucs code))
1546       (if (encode-char char '=ucs 'defined-only)
1547           (insert-char-data-with-variant char nil 'no-ucs-unified
1548                                          script excluded-script))
1549       (setq code (1+ code)))))
1550
1551 (defun write-char-range-data-to-file (min max file
1552                                           &optional script excluded-script)
1553   (let ((coding-system-for-write char-db-file-coding-system))
1554     (with-temp-buffer
1555       (insert (format ";; -*- coding: %s -*-\n"
1556                       char-db-file-coding-system))
1557       (insert-char-range-data min max script excluded-script)
1558       (write-region (point-min)(point-max) file))))
1559
1560 (defvar what-character-original-window-configuration)
1561
1562 ;;;###autoload
1563 (defun what-char-definition (char)
1564   (interactive (list (char-after)))
1565   (let ((buf (get-buffer-create "*Character Description*"))
1566         (the-buf (current-buffer))
1567         (win-conf (current-window-configuration)))
1568     (pop-to-buffer buf)
1569     (make-local-variable 'what-character-original-window-configuration)
1570     (setq what-character-original-window-configuration win-conf)
1571     (setq buffer-read-only nil)
1572     (erase-buffer)
1573     (condition-case err
1574         (progn
1575           (insert-char-data-with-variant char 'printable)
1576           (unless (char-attribute-alist char)
1577             (insert (format ";; = %c\n"
1578                             (let* ((rest (split-char char))
1579                                    (ccs (pop rest))
1580                                    (code (pop rest)))
1581                               (while rest
1582                                 (setq code (logior (lsh code 8)
1583                                                    (pop rest))))
1584                               (decode-char ccs code)))))
1585           ;; (char-db-update-comment)
1586           (set-buffer-modified-p nil)
1587           (view-mode the-buf (lambda (buf)
1588                                (set-window-configuration
1589                                 what-character-original-window-configuration)
1590                                ))
1591           (goto-char (point-min)))
1592       (error (progn
1593                (set-window-configuration
1594                 what-character-original-window-configuration)
1595                (signal (car err) (cdr err)))))))
1596
1597
1598 ;;; @ end
1599 ;;;
1600
1601 (provide 'char-db-util)
1602
1603 ;;; char-db-util.el ends here