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