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