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