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