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