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