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