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