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