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