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