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