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