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