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