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