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