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