(char-db-insert-relation-feature): Accept character as argument
[chise/xemacs-chise.git-] / lisp / utf-2000 / char-db-util.el
1 ;;; char-db-util.el --- Character Database utility -*- coding: utf-8-er; -*-
2
3 ;; Copyright (C) 1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,
4 ;;   2008,2009,2010,2011,2012,2013,2014,2015,2016 MORIOKA Tomohiko.
5
6 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
7 ;; Keywords: CHISE, Character Database, ISO/IEC 10646, UCS, Unicode, MULE.
8
9 ;; This file is part of XEmacs CHISE.
10
11 ;; XEmacs CHISE is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; XEmacs CHISE is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs CHISE; see the file COPYING.  If not, write to
23 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Code:
27
28 (require 'chise-subr)
29 (require 'ideograph-subr)
30
31 (defconst unidata-normative-category-alist
32   '(("Lu" letter        uppercase)
33     ("Ll" letter        lowercase)
34     ("Lt" letter        titlecase)
35     ("Mn" mark          non-spacing)
36     ("Mc" mark          spacing-combining)
37     ("Me" mark          enclosing)
38     ("Nd" number        decimal-digit)
39     ("Nl" number        letter)
40     ("No" number        other)
41     ("Zs" separator     space)
42     ("Zl" separator     line)
43     ("Zp" separator     paragraph)
44     ("Cc" other         control)
45     ("Cf" other         format)
46     ("Cs" other         surrogate)
47     ("Co" other         private-use)
48     ("Cn" other         not-assigned)))
49
50 (defconst unidata-informative-category-alist
51   '(("Lm" letter        modifier)
52     ("Lo" letter        other)
53     ("Pc" punctuation   connector)
54     ("Pd" punctuation   dash)
55     ("Ps" punctuation   open)
56     ("Pe" punctuation   close)
57     ("Pi" punctuation   initial-quote)
58     ("Pf" punctuation   final-quote)
59     ("Po" punctuation   other)
60     ("Sm" symbol        math)
61     ("Sc" symbol        currency)
62     ("Sk" symbol        modifier)
63     ("So" symbol        other)
64     ))
65
66 (defconst shuowen-radicals
67   [?一 ?上 ?示 ?三 ?王 ?玉 ?玨 ?气 ?士 ?丨 ; 010
68    ?屮 ?艸 ?蓐 ?茻 ?小 ?八 ?釆 ?半 ?牛 ?犛 ; 020
69    ?告 ?口 ?凵 ?吅 ?哭 ?走 ?止 ?癶 ?步 ?此 ; 030
70    ?正 ?是 ?辵 ?彳 ?廴 ?㢟 ?行 ?齒 ?牙 ?足 ; 040
71    ?疋 ?品 ?龠 ?冊 ?㗊 ?舌 ?干 ?谷 ?只 ?㕯 ; 050
72    ?句 ?丩 ?古 ?十 ?卅 ?言 ?誩 ?音 ?䇂 ?丵 ; 060
73    ?菐 ?𠬞 ?𠬜 ?共 ?異 ?舁 ?𦥑 ?䢅 ?爨 ?革 ; 070
74    ?鬲 ?䰜 ?爪 ?𠃨 ?鬥 ?又 ?𠂇 ?㕜 ?支 ?𦘒 ; 080
75    ?聿 ?畫 ?隶 ?臤 ?臣 ?殳 ?殺 ?𠘧 ?寸 ?皮 ; 090
76    ?㼱 ?攴 ?敎 ?卜 ?用 ?爻 ?㸚 ?𥄎 ?目 ?䀠 ; 100
77    ?眉 ?盾 ?自 ?白 ?鼻 ?皕 ?習 ?羽 ?隹 ?奞 ; 110
78    ?萑 ?𦫳 ?苜 ?羊 ?羴 ?瞿 ?雔 ?雥 ?鳥 ?烏 ; 120
79    ?𠦒 ?冓 ?幺 ?𢆶 ?叀 ?玄 ?予 ?放 ?𠬪 ?𣦼 ; 130
80    ?歺 ?死 ?冎 ?骨 ?肉 ?筋 ?刀 ?刃 ?㓞 ?丰 ; 140
81    ?耒 ?𧢲 ?竹 ?箕 ?丌 ?左 ?工 ?㠭 ?巫 ?甘 ; 150
82    ?曰 ?乃 ?丂 ?可 ?兮 ?号 ?亏 ?旨 ?喜 ?壴 ; 160
83 ;  ?旨 ?曰 ?乃 ?丂 ?可 ?兮 ?号 ?亏 ?喜 ?壴 ; 160
84    ?鼓 ?豈 ?豆 ?豊 ?豐 ?䖒 ?虍 ?虎 ?虤 ?皿 ; 170
85    ?𠙴 ?去 ?血 ?丶 ?丹 ?青 ?井 ?皀 ?鬯 ?食 ; 180
86    ?亼 ?會 ?倉 ?入 ?缶 ?矢 ?高 ?冂 ?𩫏 ?京 ; 190
87    ?亯 ?𣆪 ?畗 ?㐭 ?嗇 ?來 ?麥 ?夊 ?舛 ?䑞 ; 200
88    ?韋 ?弟 ?夂 ?久 ?桀 ?木 ?東 ?林 ?才 ?叒 ; 210
89    ?之 ?帀 ?出 ?𣎵 ?生 ?乇 ?𠂹 ?𠌶 ?華 ?𥝌 ; 220
90    ?稽 ?巢 ?桼 ?束 ?㯻 ?囗 ?員 ?貝 ?邑 ?𨛜 ; 230
91    ?日 ?旦 ?倝 ?㫃 ?冥 ?晶 ?月 ?有 ?明 ?囧 ; 240
92    ?夕 ?多 ?毌 ?𢎘 ?𣐺 ?卣 ?齊 ?朿 ?片 ?鼎 ; 250
93    ?克 ?彔 ?禾 ?秝 ?黍 ?香 ?米 ?毇 ?臼 ?凶 ; 260
94    ?𣎳 ?林 ?麻 ?尗 ?耑 ?韭 ?瓜 ?瓠 ?宀 ?宮 ; 270
95    ?呂 ?穴 ?㝱 ?𤕫 ?冖 ?𠔼 ?冃 ?㒳 ?网 ?襾 ; 280
96    ?巾 ?巿 ?帛 ?白 ?㡀 ?黹 ?人 ?𠤎 ?匕 ?从 ; 290
97    ?比 ?北 ?丘 ?㐺 ?𡈼 ?重 ?臥 ?身 ?㐆 ?衣 ; 300
98    ?裘 ?老 ?毛 ?毳 ?尸 ?尺 ?尾 ?履 ?舟 ?方 ; 310
99    ?儿 ?兄 ?兂 ?皃 ?𠑹 ?先 ?秃 ?見 ?覞 ?欠 ; 320
100    ?㱃 ?㳄 ?旡 ?頁 ?𦣻 ?面 ?丏 ?首 ?𥄉 ?須 ; 330
101    ?彡 ?彣 ?文 ?髟 ?后 ?司 ?卮 ?卩 ?印 ?色 ; 340
102    ?𠨍 ?辟 ?勹 ?包 ?茍 ?鬼 ?甶 ?厶 ?嵬 ?山 ; 350
103    ?屾 ?屵 ?广 ?厂 ?丸 ?危 ?石 ?長 ?勿 ?冄 ; 360
104    ?而 ?豕 ?㣇 ?彑 ?豚 ?豸 ?𤉡 ?易 ?象 ?馬 ; 370
105    ?𢊁 ?鹿 ?麤 ?㲋 ?兔 ?萈 ?犬 ?㹜 ?鼠 ?能 ; 380
106    ?熊 ?火 ?炎 ?黑 ?囪 ?焱 ?炙 ?赤 ?大 ?亦 ; 390
107    ])
108
109 (defun shuowen-radical (number)
110   (aref shuowen-radicals (1- number)))
111
112 (defvar char-db-file-coding-system 'utf-8-mcs-er)
113
114 (defvar char-db-ignored-attributes '(ideographic-products))
115
116 (defvar char-db-coded-charset-priority-list
117   '(ascii
118     control-1
119     latin-iso8859-1
120     latin-iso8859-2
121     latin-iso8859-3
122     latin-iso8859-4
123     latin-iso8859-9
124     latin-jisx0201
125     cyrillic-iso8859-5
126     greek-iso8859-7
127     thai-tis620
128     ;; =mj
129     ;; =adobe-japan1-0
130     ;; =adobe-japan1-1
131     ;; =adobe-japan1-2
132     ;; =adobe-japan1-3
133     ;; =adobe-japan1-4
134     ;; =adobe-japan1-5
135     ;; =adobe-japan1-6
136     =jis-x0208
137     =jis-x0208@1978
138     =jis-x0208@1983
139     japanese-jisx0212
140     chinese-gb2312
141     =jis-x0208@1990
142     chinese-cns11643-1
143     chinese-cns11643-2
144     chinese-cns11643-3
145     chinese-cns11643-4
146     chinese-cns11643-5
147     chinese-cns11643-6
148     chinese-cns11643-7
149     =jis-x0213-1
150     =jis-x0213-1@2000
151     =jis-x0213-1@2004
152     =jis-x0213-2
153     korean-ksc5601
154     chinese-isoir165
155     katakana-jisx0201
156     hebrew-iso8859-8
157     chinese-gb12345
158     latin-viscii
159     ethiopic-ucs
160     =big5-cdp
161     =hanyo-denshi/ja
162     =hanyo-denshi/jb
163     =hanyo-denshi/jc
164     =hanyo-denshi/jd
165     =hanyo-denshi/ft
166     =hanyo-denshi/ia
167     =hanyo-denshi/ib
168     =hanyo-denshi/hg
169     =hanyo-denshi/jt
170     =hanyo-denshi/ks
171     =hanyo-denshi/tk
172     ;; ==mj
173     ;; ==adobe-japan1-0
174     ;; ==adobe-japan1-1
175     ;; ==adobe-japan1-2
176     ;; ==adobe-japan1-3
177     ;; ==adobe-japan1-4
178     ;; ==adobe-japan1-5
179     ;; ==adobe-japan1-6
180     ==jis-x0208
181     ==jis-x0213-1
182     ==jis-x0213-2
183     ==jis-x0212
184     ==hanyo-denshi/ja
185     ==hanyo-denshi/jb
186     ==hanyo-denshi/jc
187     ==hanyo-denshi/ft
188     ==hanyo-denshi/ib
189     ==hanyo-denshi/hg
190     ==hanyo-denshi/jt
191     ==hanyo-denshi/ks
192     =gt
193     =gt-k
194     =daikanwa
195     =daikanwa@rev2
196     =daikanwa@rev1
197     =daikanwa/+p
198     ==daikanwa
199     =cbeta
200     ideograph-hanziku-1
201     ideograph-hanziku-2
202     ideograph-hanziku-3
203     ideograph-hanziku-4
204     ideograph-hanziku-5
205     ideograph-hanziku-6
206     ideograph-hanziku-7
207     ideograph-hanziku-8
208     ideograph-hanziku-9
209     ideograph-hanziku-10
210     ideograph-hanziku-11
211     ideograph-hanziku-12
212     ;; =>>>adobe-japan1-0
213     ;; =>>>adobe-japan1-1
214     ;; =>>>adobe-japan1-2
215     ;; =>>>adobe-japan1-3
216     ;; =>>>adobe-japan1-4
217     ;; =>>>adobe-japan1-5
218     ;; =>>>adobe-japan1-6
219     ;; =>>>jis-x0208
220     ;; =>>>jis-x0213-1
221     ;; =>>>jis-x0213-2
222     ;; =>>>hanyo-denshi/ja
223     ;; =>>>hanyo-denshi/jb
224     ;; =>>>hanyo-denshi/jc
225     ;; =>>>hanyo-denshi/ft
226     ;; =>>>hanyo-denshi/ib
227     ;; =>>>hanyo-denshi/hg
228     ;; =>>>hanyo-denshi/jt
229     ;; =>>>hanyo-denshi/ks
230     ;; =>>>gt
231     =>>adobe-japan1-0
232     =>>adobe-japan1-1
233     =>>adobe-japan1-2
234     =>>adobe-japan1-3
235     =>>adobe-japan1-4
236     =>>adobe-japan1-5
237     =>>adobe-japan1-6
238     =>>jis-x0208
239     =>>jis-x0213-1
240     =>>jis-x0213-1@2000
241     =>>jis-x0213-1@2004
242     =>>jis-x0213-2
243     =>>jis-x0208@1978
244     =>>hanyo-denshi/ft
245     =>>hanyo-denshi/jt
246     =>>hanyo-denshi/ks
247     =>>gt
248     =>>daikanwa
249     =>>cbeta
250     =+>jis-x0208
251     =+>jis-x0213-1
252     =+>jis-x0213-2
253     =+>adobe-japan1-0
254     =+>adobe-japan1-1
255     =+>adobe-japan1-2
256     =+>adobe-japan1-3
257     =+>adobe-japan1-4
258     =+>adobe-japan1-5
259     =+>adobe-japan1-6
260     =+>jis-x0208@1978
261     =>jis-x0208
262     =>jis-x0208@1997
263     =>jis-x0213-1
264     =>jis-x0213-1@2000
265     =>jis-x0213-1@2004
266     =>jis-x0213-2
267     ==>ucs@bucs
268     =>iwds-1
269     =>ucs@hanyo-denshi
270     =>ucs@iso
271     =>ucs@unicode
272     =>ucs@jis
273     =>ucs@cns
274     =>ucs@ks
275     =+>ucs@iso
276     =+>ucs@unicode
277     =+>ucs@jis
278     =+>ucs@jis/1990
279     =+>ucs@cns
280     =+>ucs@ks
281     =>>ucs@iso
282     =>>ucs@unicode
283     =>>ucs@jis
284     =>>ucs@cns
285     =>>>ucs@iso
286     =>>>ucs@unicode
287     ==ucs@iso
288     ==ucs@unicode
289     ;; ==ucs@cns
290     ==gb2312
291     ==ks-x1001
292     ==cns11643-1
293     ==cns11643-2
294     ==cns11643-3
295     ==cns11643-4
296     ==cns11643-5
297     ==cns11643-6
298     ==cns11643-7
299     ==gt
300     ==jis-x0208@1990
301     ;; ==jis-x0208@1983
302     ==jis-x0208@1978
303     ==gt-k
304     =ucs@iso
305     =ucs@unicode
306     =ucs@cns
307     ==big5-cdp
308     ==cbeta
309     =>>big5-cdp
310     =>>gt-k
311     =+>gt
312     =>gt
313     =>mj
314     =>big5-cdp
315     =>daikanwa
316     =>daikanwa/ho
317     =>cns11643-5
318     =>cns11643-7
319     =big5
320     =big5-eten
321     =>gt-k
322     =zinbun-oracle
323     =>zinbun-oracle
324     =ruimoku-v6
325     =>>ruimoku-v6
326     ==ruimoku-v6
327     =jef-china3
328     =>cbeta
329     =shinjigen
330     =ucs-var-001
331     =ucs-var-002
332     =ucs-var-003
333     =ucs-var-004
334     =ucs-var-006
335     =ucs-var-008
336     =ucs-var-010
337     =ucs-itaiji-001
338     =ucs-itaiji-002
339     =ucs-itaiji-003
340     =ucs-itaiji-004
341     =ucs-itaiji-005
342     =ucs-itaiji-006
343     =ucs-itaiji-008
344     =ucs-itaiji-009
345     =big5-cdp-var-003
346     =big5-cdp-var-004
347     =big5-cdp-var-005
348     =>ucs@iwds-1
349     ==ucs@gb
350     =ucs@JP/hanazono))
351
352
353 ;;; @ char-db formatters
354 ;;;
355
356 (defun char-db-make-char-spec (char)
357   (let (ret char-spec)
358     (cond ((characterp char)
359            (cond ((and (setq ret (encode-char char '=ucs 'defined-only))
360                        (not (and (<= #xE000 ret)(<= ret #xF8FF))))
361                   (setq char-spec (list (cons '=ucs ret)))
362                   (cond ((setq ret (get-char-attribute char 'name))
363                          (setq char-spec (cons (cons 'name ret) char-spec))
364                          )
365                         ((setq ret (get-char-attribute char 'name*))
366                          (setq char-spec (cons (cons 'name* ret) char-spec))
367                          ))
368                   )
369                  ((encode-char char '=mj 'defined-only)
370                   (setq char-spec nil)
371                   (dolist (ccs (charset-list))
372                     (if (and (or (eq ccs '=mj)
373                                  ;; (eq (charset-property ccs 'iso-ir) 177)
374                                  (string-match "=ucs@" (symbol-name ccs))
375                                  )
376                              (setq ccs (charset-name ccs))
377                              (null (assq ccs char-spec))
378                              (setq ret (encode-char char ccs 'defined-only)))
379                         (setq char-spec (cons (cons ccs ret) char-spec))))
380                   )
381                  ((encode-char char '==mj 'defined-only)
382                   (setq char-spec nil)
383                   (dolist (ccs (charset-list))
384                     (if (and (or (eq ccs '==mj)
385                                  ;; (eq (charset-property ccs 'iso-ir) 177)
386                                  (string-match "=ucs@" (symbol-name ccs))
387                                  )
388                              (setq ccs (charset-name ccs))
389                              (null (assq ccs char-spec))
390                              (setq ret (encode-char char ccs 'defined-only)))
391                         (setq char-spec (cons (cons ccs ret) char-spec))))
392                   )
393                  ((encode-char char '=adobe-japan1 'defined-only)
394                   (setq char-spec nil)
395                   (dolist (ccs (charset-list))
396                     (if (and (or (memq ccs
397                                        '(=adobe-japan1-0
398                                          =adobe-japan1-1
399                                          =adobe-japan1-2
400                                          =adobe-japan1-3
401                                          =adobe-japan1-4
402                                          =adobe-japan1-5
403                                          =adobe-japan1-6
404                                          ))
405                                  ;; (eq (charset-property ccs 'iso-ir) 177)
406                                  (string-match "=ucs@" (symbol-name ccs))
407                                  )
408                              (setq ccs (charset-name ccs))
409                              (null (assq ccs char-spec))
410                              (setq ret (encode-char char ccs 'defined-only)))
411                         (setq char-spec (cons (cons ccs ret) char-spec))))
412                   )
413                  ((encode-char char '==adobe-japan1 'defined-only)
414                   (setq char-spec nil)
415                   (dolist (ccs (charset-list))
416                     (if (and (or (memq ccs
417                                        '(==adobe-japan1-0
418                                          ==adobe-japan1-1
419                                          ==adobe-japan1-2
420                                          ==adobe-japan1-3
421                                          ==adobe-japan1-4
422                                          ==adobe-japan1-5
423                                          ==adobe-japan1-6
424                                          ))
425                                  ;; (eq (charset-property ccs 'iso-ir) 177)
426                                  (string-match "=ucs@" (symbol-name ccs))
427                                  )
428                              (setq ccs (charset-name ccs))
429                              (null (assq ccs char-spec))
430                              (setq ret (encode-char char ccs 'defined-only)))
431                         (setq char-spec (cons (cons ccs ret) char-spec))))
432                   )
433                  ((setq ret
434                         (catch 'tag
435                           (let ((rest char-db-coded-charset-priority-list)
436                                 ccs)
437                             (while rest
438                               (setq ccs (charset-name
439                                          (find-charset (car rest))))
440                               (if (setq ret
441                                         (encode-char char ccs
442                                                      'defined-only))
443                                   (throw 'tag (cons ccs ret)))
444                               (setq rest (cdr rest))))))
445                   (setq char-spec (list ret))
446                   (dolist (ccs (delq (car ret) (charset-list)))
447                     (if (and (or (charset-iso-final-char ccs)
448                                  (memq ccs
449                                        '(=daikanwa
450                                          =daikanwa@rev2
451                                          ;; =gt-k
452                                          =jis-x0208@1997
453                                          ))
454                                  (eq (charset-property ccs 'iso-ir) 177)
455                                  ;; (string-match "=ucs@" (symbol-name ccs))
456                                  )
457                              (setq ccs (charset-name ccs))
458                              (null (assq ccs char-spec))
459                              (setq ret (encode-char char ccs 'defined-only)))
460                         (setq char-spec (cons (cons ccs ret) char-spec))))
461                   (if (null char-spec)
462                       (setq char-spec (split-char char)))
463                   (cond ((setq ret (get-char-attribute char 'name))
464                          (setq char-spec (cons (cons 'name ret) char-spec))
465                          )
466                         ((setq ret (get-char-attribute char 'name*))
467                          (setq char-spec (cons (cons 'name* ret) char-spec))
468                          ))
469                   )
470                  ((setq ret (get-char-attribute
471                              char 'ideographic-combination))
472                   (setq char-spec
473                         (cons (cons 'ideographic-combination ret)
474                               char-spec))
475                   ))
476            char-spec)
477           ((consp char)
478            char))))
479     
480 (defun char-db-insert-char-spec (char &optional readable column
481                                       required-features)
482   (unless column
483     (setq column (current-column)))
484   (let (char-spec temp-char)
485     (setq char-spec (char-db-make-char-spec char))
486     (unless (or (characterp char) ; char
487                 (condition-case nil
488                     (setq char (find-char char-spec))
489                   (error nil)))
490       ;; define temporary character
491       ;;   Current implementation is dirty.
492       (setq temp-char (define-char (cons '(ideograph-daikanwa . 0)
493                                          char-spec)))
494       (remove-char-attribute temp-char 'ideograph-daikanwa)
495       (setq char temp-char))
496     (insert-char-attributes char
497                             readable
498                             (union (mapcar #'car char-spec)
499                                    required-features)
500                             nil 'for-sub-node)
501     (when temp-char
502       ;; undefine temporary character
503       ;;   Current implementation is dirty.
504       (setq char-spec (char-attribute-alist temp-char))
505       (while char-spec
506         (remove-char-attribute temp-char (car (car char-spec)))
507         (setq char-spec (cdr char-spec))))))
508
509 (defun char-db-insert-alist (alist &optional readable column)
510   (unless column
511     (setq column (current-column)))
512   (let ((line-breaking
513          (concat "\n" (make-string (1+ column) ?\ )))
514         name value
515         ret al ; cal
516         key
517         lbs cell rest separator)
518     (insert "(")
519     (while alist
520       (setq name (car (car alist))
521             value (cdr (car alist)))
522       (cond ((eq name 'char)
523              (insert "(char . ")
524              (if (setq ret (condition-case nil
525                                (find-char value)
526                              (error nil)))
527                  (progn
528                    (setq al nil
529                          ;; cal nil
530                          )
531                    (while value
532                      (setq key (car (car value)))
533                      ;; (if (find-charset key)
534                      ;;     (setq cal (cons key cal))
535                      (setq al (cons key al))
536                      ;; )
537                      (setq value (cdr value)))
538                    (insert-char-attributes ret
539                                            readable
540                                            (or al 'none) ; cal
541                                            nil 'for-sub-node))
542                (insert (prin1-to-string value)))
543              (insert ")")
544              (insert line-breaking))
545             ((consp value)
546              (insert (format "(%-18s " name))
547              (setq lbs (concat "\n" (make-string (current-column) ?\ )))
548              (while (consp value)
549                (setq cell (car value))
550                (if (and (consp cell)
551                         (consp (car cell))
552                         (setq ret (condition-case nil
553                                       (find-char cell)
554                                     (error nil)))
555                         )
556                    (progn
557                      (setq rest cell
558                            al nil
559                            ;; cal nil
560                            )
561                      (while rest
562                        (setq key (car (car rest)))
563                        ;; (if (find-charset key)
564                        ;;     (setq cal (cons key cal))
565                        (setq al (cons key al))
566                        ;; )
567                        (setq rest (cdr rest)))
568                      (if separator
569                          (insert lbs))
570                      (insert-char-attributes ret
571                                              readable
572                                              al ; cal
573                                              nil 'for-sub-node)
574                      (setq separator lbs))
575                  (if separator
576                      (insert separator))
577                  (insert (prin1-to-string cell))
578                  (setq separator " "))
579                (setq value (cdr value)))
580              (insert ")")
581              (insert line-breaking))
582             (t
583              (insert (format "(%-18s . %S)%s"
584                              name value
585                              line-breaking))))
586       (setq alist (cdr alist))))
587   (insert ")"))
588
589 (defun char-db-insert-char-reference (plist &optional readable column)
590   (unless column
591     (setq column (current-column)))
592   (let ((line-breaking
593          (concat "\n" (make-string (1+ column) ?\ )))
594         (separator "")
595         name value)
596     (insert "(")
597     (while plist
598       (setq name (pop plist))
599       (setq value (pop plist))
600       (cond ((eq name :char)
601              (insert separator)
602              (insert ":char\t")
603              (cond ((numberp value)
604                     (setq value (decode-char '=ucs value)))
605                    ;; ((consp value)
606                    ;;  (setq value (or (find-char value)
607                    ;;                  value)))
608                    )
609              (char-db-insert-char-spec value readable)
610              (insert line-breaking)
611              (setq separator ""))
612             ((eq name :radical)
613              (insert (format "%s%s\t%d ; %c%s"
614                              separator
615                              name value
616                              (ideographic-radical value)
617                              line-breaking))
618              (setq separator ""))
619             (t
620              (insert (format "%s%s\t%S" separator name value))
621              (setq separator line-breaking)))
622       ))
623   (insert ")"))
624
625 (defun char-db-decode-isolated-char (ccs code-point)
626   (let (ret)
627     (setq ret
628           (cond ((eq ccs 'arabic-iso8859-6)
629                  (decode-char ccs code-point))
630                 ;; ((eq ccs '=gt)
631                 ;;  (decode-builtin-char '==gt code-point))
632                 ((and (memq ccs '(=gt-pj-1
633                                   =gt-pj-2
634                                   =gt-pj-3
635                                   =gt-pj-4
636                                   =gt-pj-5
637                                   =gt-pj-6
638                                   =gt-pj-7
639                                   =gt-pj-8
640                                   =gt-pj-9
641                                   =gt-pj-10
642                                   =gt-pj-11))
643                       (setq ret (decode-char ccs code-point))
644                       (setq ret (encode-char ret '=gt 'defined-only)))
645                  (decode-builtin-char '=gt ret))
646                 (t
647                  (decode-builtin-char ccs code-point))))
648     (cond ((null ret)
649            (or (decode-char ccs code-point)
650                (define-char (list (cons ccs code-point)))))
651           ((and (<= 0 (char-int ret))
652                 (<= (char-int ret) #x1F))
653            (decode-char '=ucs (+ #x2400 (char-int ret))))
654           ((= (char-int ret) #x7F)
655            ?\u2421)
656           (t ret))))
657
658 (defvar char-db-convert-obsolete-format t)
659
660 (defun char-db-insert-ccs-feature (name value line-breaking)
661   (cond
662    ((integerp value)
663     (insert
664      (format
665       (cond
666        ((memq name '(=>iwds-1
667                      ==shinjigen
668                      =shinjigen
669                      =shinjigen@1ed ==shinjigen@1ed
670                      =shinjigen@rev ==shinjigen@rev
671                      =shinjigen/+p@rev ==shinjigen/+p@rev
672                      ===daikanwa/ho ==daikanwa/ho
673                      =daikanwa/ho =>>daikanwa/ho =>daikanwa/ho))
674         "(%-18s .  %04d)\t; %c")
675        ((eq name '=shinjigen@1ed/24pr)
676         "(%-18s . %04d)\t; %c")
677        ((or
678          (memq name
679                '(===daikanwa
680                  ==daikanwa =daikanwa =>>daikanwa =>daikanwa
681                  =daikanwa@rev1 =daikanwa@rev2
682                  =daikanwa/+p ==daikanwa/+p ===daikanwa/+p
683                  =>>daikanwa/+p
684                  =daikanwa/+2p =>>daikanwa/+2p
685                  =gt ==gt ===gt
686                  =>>gt =+>gt =>gt
687                  =gt-k ==gt-k ===gt-k
688                  =>>gt-k =>gt-k
689                  =adobe-japan1-0 ==adobe-japan1-0 ===adobe-japan1-0
690                  =adobe-japan1-1 ==adobe-japan1-1 ===adobe-japan1-1
691                  =adobe-japan1-2 ==adobe-japan1-2 ===adobe-japan1-2
692                  =adobe-japan1-3 ==adobe-japan1-3 ===adobe-japan1-3
693                  =adobe-japan1-4 ==adobe-japan1-4 ===adobe-japan1-4
694                  =adobe-japan1-5 ==adobe-japan1-5 ===adobe-japan1-5
695                  =adobe-japan1-6 ==adobe-japan1-6 ===adobe-japan1-6
696                  =>>adobe-japan1-0 =+>adobe-japan1-0
697                  =>>adobe-japan1-1 =+>adobe-japan1-1
698                  =>>adobe-japan1-2 =+>adobe-japan1-2
699                  =>>adobe-japan1-3 =+>adobe-japan1-3
700                  =>>adobe-japan1-4 =+>adobe-japan1-4
701                  =>>adobe-japan1-5 =+>adobe-japan1-5
702                  =>>adobe-japan1-6 =+>adobe-japan1-6
703                  =>cbeta =cbeta =>>cbeta ==cbeta ===cbeta
704                  =zinbun-oracle =>zinbun-oracle
705                  ===hng-jou ===hng-keg ===hng-dng ===hng-mam
706                  ===hng-drt ===hng-kgk ===hng-myz ===hng-kda
707                  ===hng-khi ===hng-khm ===hng-hok ===hng-kyd ===hng-sok
708                  ===hng-yhk ===hng-kak ===hng-kar ===hng-kae
709                  ===hng-sys ===hng-tsu ===hng-tzj
710                  ===hng-hos ===hng-nak ===hng-jhk
711                  ===hng-hod ===hng-gok ===hng-ink ===hng-nto
712                  ===hng-nkm ===hng-k24 ===hng-nkk
713                  ===hng-kcc ===hng-kcj ===hng-kbk ===hng-sik
714                  ===hng-skk ===hng-kyu ===hng-ksk ===hng-wan
715                  ===hng-okd ===hng-wad ===hng-kmi
716                  ===hng-zkd ===hng-doh ===hng-jyu
717                  ===hng-tzs ===hng-kss ===hng-kyo
718                  ===hng-smk))
719          ;; (string-match "^=adobe-" (symbol-name name))
720          )
721         "(%-18s . %05d)\t; %c")
722        ((memq name '(=hanyo-denshi/ks
723                      ==hanyo-denshi/ks ===hanyo-denshi/ks
724                      =>>hanyo-denshi/ks
725                      =koseki ==koseki
726                      =mj ==mj ===mj =>>mj =>mj
727                      =zihai mojikyo))
728         "(%-18s . %06d)\t; %c")
729        ((memq name '(=hanyo-denshi/tk ==hanyo-denshi/tk))
730         "(%-18s . %08d)\t; %c")
731        ((>= (charset-dimension name) 2)
732         "(%-18s . #x%04X)\t; %c")
733        (t
734         "(%-18s . #x%02X)\t; %c"))
735       name
736       (if (= (charset-iso-graphic-plane name) 1)
737           (logior value
738                   (cond ((= (charset-dimension name) 1)
739                          #x80)
740                         ((= (charset-dimension name) 2)
741                          #x8080)
742                         ((= (charset-dimension name) 3)
743                          #x808080)
744                         (t 0)))
745         value)
746       (char-db-decode-isolated-char name value)))
747     (if (and (= (charset-chars name) 94)
748              (= (charset-dimension name) 2))
749         (insert (format " [%02d-%02d]"
750                         (- (lsh value -8) 32)
751                         (- (logand value 255) 32))))
752     )
753    (t
754     (insert (format "(%-18s . %s)" name value))
755     ))
756   (insert line-breaking))
757
758 (defun char-db-insert-relation-feature (char name value line-breaking
759                                              ccss readable)
760   (insert (format "(%-18s%s " name line-breaking))
761   (let ((lbs (concat "\n" (make-string (current-column) ?\ )))
762         separator cell sources required-features
763         ret)
764     (if (characterp value)
765         (setq value (list value)))
766     (while (consp value)
767       (setq cell (car value))
768       (if (integerp cell)
769           (setq cell (decode-char '=ucs cell)))
770       (cond
771        ((eq name '->subsumptive)
772         (when (or (not (some (lambda (atr)
773                                (get-char-attribute cell atr))
774                              char-db-ignored-attributes))
775                   (some (lambda (ccs)
776                           (encode-char cell ccs 'defined-only))
777                         ccss))
778           (if separator
779               (insert lbs))
780           (let ((char-db-ignored-attributes
781                  (cons '<-subsumptive
782                        char-db-ignored-attributes)))
783             (insert-char-attributes cell readable nil nil 'for-sub-node))
784           (setq separator lbs))
785         )
786        ((characterp cell)
787         (setq sources
788               (get-char-attribute
789                char (intern (format "%s*sources" name))))
790         (setq required-features nil)
791         (dolist (source sources)
792           (cond
793            ((memq source '(JP
794                            JP/Jouyou
795                            shinjigen shinjigen@1ed shinjigen@rev))
796             (setq required-features
797                   (union required-features
798                          '(=jis-x0208
799                            =jis-x0208@1990
800                            =jis-x0213-1@2000
801                            =jis-x0213-1@2004
802                            =jis-x0213-2
803                            =jis-x0212
804                            =jis-x0208@1983
805                            =jis-x0208@1978
806                            =shinjigen
807                            =shinjigen@1ed
808                            =shinjigen@rev
809                            =shinjigen/+p@rev))))
810            ((eq source 'CN)
811             (setq required-features
812                   (union required-features
813                          '(=gb2312
814                            =gb12345
815                            =iso-ir165)))))
816           (cond
817            ((find-charset (setq ret (intern (format "=%s" source))))
818             (setq required-features
819                   (cons ret required-features)))
820            (t (setq required-features
821                     (cons source required-features)))))
822         (cond ((string-match "@JP" (symbol-name name))
823                (setq required-features
824                      (union required-features
825                             '(=jis-x0208
826                               =jis-x0208@1990
827                               =jis-x0213-1-2000
828                               =jis-x0213-2-2000
829                               =jis-x0212
830                               =jis-x0208@1983
831                               =jis-x0208@1978))))
832               ((string-match "@CN" (symbol-name name))
833                (setq required-features
834                      (union required-features
835                             '(=gb2312
836                               =gb12345
837                               =iso-ir165)))))
838         (if separator
839             (insert lbs))
840         (if readable
841             (insert (format "%S" cell))
842           (char-db-insert-char-spec cell readable
843                                     nil
844                                     required-features))
845         (setq separator lbs))
846        ((consp cell)
847         (if separator
848             (insert lbs))
849         (if (consp (car cell))
850             (char-db-insert-char-spec cell readable)
851           (char-db-insert-char-reference cell readable))
852         (setq separator lbs))
853        (t
854         (if separator
855             (insert separator))
856         (insert (prin1-to-string cell))
857         (setq separator " ")))
858       (setq value (cdr value)))
859     (insert ")")
860     (insert line-breaking)))
861
862 (defun insert-char-attributes (char &optional readable attributes column
863                                     for-sub-node)
864   (unless column
865     (setq column (current-column)))
866   (let (name value ; has-long-ccs-name
867         rest
868         radical strokes
869         (line-breaking
870          (concat "\n" (make-string (1+ column) ?\ )))
871         lbs cell separator ret
872         key al cal
873         dest-ccss ; sources required-features
874         ccss)
875     (let (atr-d)
876       (setq attributes
877             (sort (if attributes
878                       (if (consp attributes)
879                           (progn
880                             (dolist (name attributes)
881                               (unless (memq name char-db-ignored-attributes)
882                                 (if (find-charset name)
883                                     (push name ccss))
884                                 (push name atr-d)))
885                             atr-d))
886                     (dolist (name (char-attribute-list))
887                       (unless (memq name char-db-ignored-attributes)
888                         (if (find-charset name)
889                             (push name ccss))
890                         (push name atr-d)))
891                     atr-d)
892                   #'char-attribute-name<)))
893     (insert "(")
894     (when (memq '<-subsumptive attributes)
895       (when (or readable (not for-sub-node))
896         (when (setq value (get-char-attribute char '<-subsumptive))
897           (char-db-insert-relation-feature char '<-subsumptive value
898                                            line-breaking
899                                            ccss readable)))
900       (setq attributes (delq '<-subsumptive attributes)))
901     (when (and (memq '<-denotational attributes)
902                (setq value (get-char-attribute char '<-denotational)))
903       (char-db-insert-relation-feature char '<-denotational value
904                                        line-breaking
905                                        ccss readable)
906       (setq attributes (delq '<-denotational attributes)))
907     (when (and (memq '<-denotational@component attributes)
908                (setq value (get-char-attribute char '<-denotational@component)))
909       (char-db-insert-relation-feature char '<-denotational@component value
910                                        line-breaking
911                                        ccss readable)
912       (setq attributes (delq '<-denotational@component attributes)))
913     (when (and (memq 'name attributes)
914                (setq value (get-char-attribute char 'name)))
915       (insert (format
916                (if (> (+ (current-column) (length value)) 48)
917                    "(name . %S)%s"
918                  "(name               . %S)%s")
919                value line-breaking))
920       (setq attributes (delq 'name attributes))
921       )
922     (when (and (memq 'name* attributes)
923                (setq value (get-char-attribute char 'name*)))
924       (insert (format
925                (if (> (+ (current-column) (length value)) 48)
926                    "(name* . %S)%s"
927                  "(name*              . %S)%s")
928                value line-breaking))
929       (setq attributes (delq 'name* attributes))
930       )
931     (when (and (memq 'script attributes)
932                (setq value (get-char-attribute char 'script)))
933       (insert (format "(script\t\t%s)%s"
934                       (mapconcat (function prin1-to-string)
935                                  value " ")
936                       line-breaking))
937       (setq attributes (delq 'script attributes))
938       )
939     (dolist (name '(=>ucs =>ucs*))
940       (when (and (memq name attributes)
941                  (setq value (get-char-attribute char name)))
942         (insert (format "(%-18s . #x%04X)\t; %c%s"
943                         name value (decode-char '=ucs value)
944                         line-breaking))
945         (setq attributes (delq name attributes))))
946     (dolist (name '(=>ucs@gb =>ucs@big5))
947       (when (and (memq name attributes)
948                  (setq value (get-char-attribute char name)))
949         (insert (format "(%-18s . #x%04X)\t; %c%s"
950                         name value
951                         (decode-char (intern
952                                       (concat "="
953                                               (substring
954                                                (symbol-name name) 2)))
955                                      value)
956                         line-breaking))
957         (setq attributes (delq name attributes))
958         ))
959     ;; (dolist (name '(=>daikanwa))
960     ;;   (when (and (memq name attributes)
961     ;;              (setq value (get-char-attribute char name)))
962     ;;     (insert
963     ;;      (if (integerp value)
964     ;;          (format "(%-18s . %05d)\t; %c%s"
965     ;;                  name value (decode-char '=daikanwa value)
966     ;;                  line-breaking)
967     ;;        (format "(%-18s %s)\t; %c%s"
968     ;;                name
969     ;;                (mapconcat (function prin1-to-string)
970     ;;                           value " ")
971     ;;                (char-representative-of-daikanwa char)
972     ;;                line-breaking)))
973     ;;     (setq attributes (delq name attributes))))
974     (when (and (memq 'general-category attributes)
975                (setq value (get-char-attribute char 'general-category)))
976       (insert (format
977                "(general-category\t%s) ; %s%s"
978                (mapconcat (lambda (cell)
979                             (format "%S" cell))
980                           value " ")
981                (cond ((rassoc value unidata-normative-category-alist)
982                       "Normative Category")
983                      ((rassoc value unidata-informative-category-alist)
984                       "Informative Category")
985                      (t
986                       "Unknown Category"))
987                line-breaking))
988       (setq attributes (delq 'general-category attributes))
989       )
990     (when (and (memq 'bidi-category attributes)
991                (setq value (get-char-attribute char 'bidi-category)))
992       (insert (format "(bidi-category\t. %S)%s"
993                       value
994                       line-breaking))
995       (setq attributes (delq 'bidi-category attributes))
996       )
997     (unless (or (not (memq 'mirrored attributes))
998                 (eq (setq value (get-char-attribute char 'mirrored 'empty))
999                     'empty))
1000       (insert (format "(mirrored\t\t. %S)%s"
1001                       value
1002                       line-breaking))
1003       (setq attributes (delq 'mirrored attributes))
1004       )
1005     (cond
1006      ((and (memq 'decimal-digit-value attributes)
1007            (setq value (get-char-attribute char 'decimal-digit-value)))
1008       (insert (format "(decimal-digit-value . %S)%s"
1009                       value
1010                       line-breaking))
1011       (setq attributes (delq 'decimal-digit-value attributes))
1012       (when (and (memq 'digit-value attributes)
1013                  (setq value (get-char-attribute char 'digit-value)))
1014         (insert (format "(digit-value\t . %S)%s"
1015                         value
1016                         line-breaking))
1017         (setq attributes (delq 'digit-value attributes))
1018         )
1019       (when (and (memq 'numeric-value attributes)
1020                  (setq value (get-char-attribute char 'numeric-value)))
1021         (insert (format "(numeric-value\t . %S)%s"
1022                         value
1023                         line-breaking))
1024         (setq attributes (delq 'numeric-value attributes))
1025         )
1026       )
1027      (t
1028       (when (and (memq 'digit-value attributes)
1029                  (setq value (get-char-attribute char 'digit-value)))
1030         (insert (format "(digit-value\t. %S)%s"
1031                         value
1032                         line-breaking))
1033         (setq attributes (delq 'digit-value attributes))
1034         )
1035       (when (and (memq 'numeric-value attributes)
1036                  (setq value (get-char-attribute char 'numeric-value)))
1037         (insert (format "(numeric-value\t. %S)%s"
1038                         value
1039                         line-breaking))
1040         (setq attributes (delq 'numeric-value attributes))
1041         )))
1042     (when (and (memq 'iso-10646-comment attributes)
1043                (setq value (get-char-attribute char 'iso-10646-comment)))
1044       (insert (format "(iso-10646-comment\t. %S)%s"
1045                       value
1046                       line-breaking))
1047       (setq attributes (delq 'iso-10646-comment attributes))
1048       )
1049     (when (and (memq 'morohashi-daikanwa attributes)
1050                (setq value (get-char-attribute char 'morohashi-daikanwa)))
1051       (insert (format "(morohashi-daikanwa\t%s)%s"
1052                       (mapconcat (function prin1-to-string) value " ")
1053                       line-breaking))
1054       (setq attributes (delq 'morohashi-daikanwa attributes))
1055       )
1056     (setq radical nil
1057           strokes nil)
1058     (when (and (memq 'ideographic-radical attributes)
1059                (setq value (get-char-attribute char 'ideographic-radical)))
1060       (setq radical value)
1061       (insert (format "(ideographic-radical . %S)\t; %c%s"
1062                       radical
1063                       (ideographic-radical radical)
1064                       line-breaking))
1065       (setq attributes (delq 'ideographic-radical attributes))
1066       )
1067     (when (and (memq 'shuowen-radical attributes)
1068                (setq value (get-char-attribute char 'shuowen-radical)))
1069       (insert (format "(shuowen-radical\t. %S)\t; %c%s"
1070                       value
1071                       (shuowen-radical value)
1072                       line-breaking))
1073       (setq attributes (delq 'shuowen-radical attributes))
1074       )
1075     (let (key)
1076       (dolist (domain
1077                (append
1078                 char-db-feature-domains
1079                 (let (dest domain)
1080                   (dolist (feature (char-attribute-list))
1081                     (setq feature (symbol-name feature))
1082                     (when (string-match
1083                            "\\(radical\\|strokes\\)@\\([^@*]+\\)\\(\\*\\|$\\)"
1084                            feature)
1085                       (setq domain (intern (match-string 2 feature)))
1086                      (unless (memq domain dest)
1087                        (setq dest (cons domain dest)))))
1088                   (sort dest #'string<))))
1089         (setq key (intern (format "%s@%s" 'ideographic-radical domain)))
1090         (when (and (memq key attributes)
1091                    (setq value (get-char-attribute char key)))
1092           (setq radical value)
1093           (insert (format "(%s . %S)\t; %c%s"
1094                           key
1095                           radical
1096                           (ideographic-radical radical)
1097                           line-breaking))
1098           (setq attributes (delq key attributes))
1099           )
1100         (setq key (intern (format "%s@%s" 'ideographic-strokes domain)))
1101         (when (and (memq key attributes)
1102                    (setq value (get-char-attribute char key)))
1103           (setq strokes value)
1104           (insert (format "(%s . %S)%s"
1105                           key
1106                           strokes
1107                           line-breaking))
1108           (setq attributes (delq key attributes))
1109           )
1110         (setq key (intern (format "%s@%s" 'total-strokes domain)))
1111         (when (and (memq key attributes)
1112                    (setq value (get-char-attribute char key)))
1113           (insert (format "(%s       . %S)%s"
1114                           key
1115                           value
1116                           line-breaking))
1117           (setq attributes (delq key attributes))
1118           )
1119         (dolist (feature '(ideographic-radical
1120                            ideographic-strokes
1121                            total-strokes))
1122           (setq key (intern (format "%s@%s*sources" feature domain)))
1123           (when (and (memq key attributes)
1124                      (setq value (get-char-attribute char key)))
1125             (insert (format "(%s%s" key line-breaking))
1126             (dolist (cell value)
1127               (insert (format " %s" cell)))
1128             (insert ")")
1129             (insert line-breaking)
1130             (setq attributes (delq key attributes))
1131             ))
1132         ))
1133     (when (and (memq 'ideographic-strokes attributes)
1134                (setq value (get-char-attribute char 'ideographic-strokes)))
1135       (setq strokes value)
1136       (insert (format "(ideographic-strokes . %S)%s"
1137                       strokes
1138                       line-breaking))
1139       (setq attributes (delq 'ideographic-strokes attributes))
1140       )
1141     (when (and (memq 'kangxi-radical attributes)
1142                (setq value (get-char-attribute char 'kangxi-radical)))
1143       (unless (eq value radical)
1144         (insert (format "(kangxi-radical\t . %S)\t; %c%s"
1145                         value
1146                         (ideographic-radical value)
1147                         line-breaking))
1148         (or radical
1149             (setq radical value)))
1150       (setq attributes (delq 'kangxi-radical attributes))
1151       )
1152     (when (and (memq 'kangxi-strokes attributes)
1153                (setq value (get-char-attribute char 'kangxi-strokes)))
1154       (unless (eq value strokes)
1155         (insert (format "(kangxi-strokes\t . %S)%s"
1156                         value
1157                         line-breaking))
1158         (or strokes
1159             (setq strokes value)))
1160       (setq attributes (delq 'kangxi-strokes attributes))
1161       )
1162     ;; (when (and (memq 'japanese-radical attributes)
1163     ;;            (setq value (get-char-attribute char 'japanese-radical)))
1164     ;;   (unless (eq value radical)
1165     ;;     (insert (format "(japanese-radical\t . %S)\t; %c%s"
1166     ;;                     value
1167     ;;                     (ideographic-radical value)
1168     ;;                     line-breaking))
1169     ;;     (or radical
1170     ;;         (setq radical value)))
1171     ;;   (setq attributes (delq 'japanese-radical attributes))
1172     ;;   )
1173     (when (and (memq 'japanese-strokes attributes)
1174                (setq value (get-char-attribute char 'japanese-strokes)))
1175       (unless (eq value strokes)
1176         (insert (format "(japanese-strokes\t . %S)%s"
1177                         value
1178                         line-breaking))
1179         (or strokes
1180             (setq strokes value)))
1181       (setq attributes (delq 'japanese-strokes attributes))
1182       )
1183     (when (and (memq 'cns-radical attributes)
1184                (setq value (get-char-attribute char 'cns-radical)))
1185       (insert (format "(cns-radical\t . %S)\t; %c%s"
1186                       value
1187                       (ideographic-radical value)
1188                       line-breaking))
1189       (setq attributes (delq 'cns-radical attributes))
1190       )
1191     (when (and (memq 'cns-strokes attributes)
1192                (setq value (get-char-attribute char 'cns-strokes)))
1193       (unless (eq value strokes)
1194         (insert (format "(cns-strokes\t . %S)%s"
1195                         value
1196                         line-breaking))
1197         (or strokes
1198             (setq strokes value)))
1199       (setq attributes (delq 'cns-strokes attributes))
1200       )
1201     ;; (when (and (memq 'shinjigen-1-radical attributes)
1202     ;;            (setq value (get-char-attribute char 'shinjigen-1-radical)))
1203     ;;   (unless (eq value radical)
1204     ;;     (insert (format "(shinjigen-1-radical . %S)\t; %c%s"
1205     ;;                     value
1206     ;;                     (ideographic-radical value)
1207     ;;                     line-breaking))
1208     ;;     (or radical
1209     ;;         (setq radical value)))
1210     ;;   (setq attributes (delq 'shinjigen-1-radical attributes))
1211     ;;   )
1212     ;; (when (and (memq 'ideographic- attributes)
1213     ;;            (setq value (get-char-attribute char 'ideographic-)))
1214     ;;   (insert "(ideographic-       ")
1215     ;;   (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1216     ;;         separator nil)
1217     ;;   (while (consp value)
1218     ;;     (setq cell (car value))
1219     ;;     (if (integerp cell)
1220     ;;         (setq cell (decode-char '=ucs cell)))
1221     ;;     (cond ((characterp cell)
1222     ;;            (if separator
1223     ;;                (insert lbs))
1224     ;;            (if readable
1225     ;;                (insert (format "%S" cell))
1226     ;;              (char-db-insert-char-spec cell readable))
1227     ;;            (setq separator lbs))
1228     ;;           ((consp cell)
1229     ;;            (if separator
1230     ;;                (insert lbs))
1231     ;;            (if (consp (car cell))
1232     ;;                (char-db-insert-char-spec cell readable)
1233     ;;              (char-db-insert-char-reference cell readable))
1234     ;;            (setq separator lbs))
1235     ;;           (t
1236     ;;            (if separator
1237     ;;                (insert separator))
1238     ;;            (insert (prin1-to-string cell))
1239     ;;            (setq separator " ")))
1240     ;;     (setq value (cdr value)))
1241     ;;   (insert ")")
1242     ;;   (insert line-breaking)
1243     ;;   (setq attributes (delq 'ideographic- attributes)))
1244     (when (and (memq 'total-strokes attributes)
1245                (setq value (get-char-attribute char 'total-strokes)))
1246       (insert (format "(total-strokes       . %S)%s"
1247                       value
1248                       line-breaking))
1249       (setq attributes (delq 'total-strokes attributes))
1250       )
1251     ;; (when (and (memq '->ideograph attributes)
1252     ;;            (setq value (get-char-attribute char '->ideograph)))
1253     ;;   (insert (format "(->ideograph\t%s)%s"
1254     ;;                   (mapconcat (lambda (code)
1255     ;;                                (cond ((symbolp code)
1256     ;;                                       (symbol-name code))
1257     ;;                                      ((integerp code)
1258     ;;                                       (format "#x%04X" code))
1259     ;;                                      (t
1260     ;;                                       (format "%s %S"
1261     ;;                                               line-breaking code))))
1262     ;;                              value " ")
1263     ;;                   line-breaking))
1264     ;;   (setq attributes (delq '->ideograph attributes))
1265     ;;   )
1266     ;; (when (and (memq '->decomposition attributes)
1267     ;;            (setq value (get-char-attribute char '->decomposition)))
1268     ;;   (insert (format "(->decomposition\t%s)%s"
1269     ;;                   (mapconcat (lambda (code)
1270     ;;                                (cond ((symbolp code)
1271     ;;                                       (symbol-name code))
1272     ;;                                      ((characterp code)
1273     ;;                                       (if readable
1274     ;;                                           (format "%S" code)
1275     ;;                                         (format "#x%04X"
1276     ;;                                                 (char-int code))
1277     ;;                                         ))
1278     ;;                                      ((integerp code)
1279     ;;                                       (format "#x%04X" code))
1280     ;;                                      (t
1281     ;;                                       (format "%s%S" line-breaking code))))
1282     ;;                              value " ")
1283     ;;                   line-breaking))
1284     ;;   (setq attributes (delq '->decomposition attributes))
1285     ;;   )
1286     (if (equal (get-char-attribute char '->titlecase)
1287                (get-char-attribute char '->uppercase))
1288         (setq attributes (delq '->titlecase attributes)))
1289     (when (and (memq '->mojikyo attributes)
1290                (setq value (get-char-attribute char '->mojikyo)))
1291       (insert (format "(->mojikyo\t\t. %06d)\t; %c%s"
1292                       value (decode-char 'mojikyo value)
1293                       line-breaking))
1294       (setq attributes (delq '->mojikyo attributes))
1295       )
1296     ;; (when (and (memq 'hanyu-dazidian-vol attributes)
1297     ;;            (setq value (get-char-attribute char 'hanyu-dazidian-vol)))
1298     ;;   (insert (format "(hanyu-dazidian-vol  . %d)%s"
1299     ;;                   value line-breaking))
1300     ;;   (setq attributes (delq 'hanyu-dazidian-vol attributes))
1301     ;;   )
1302     ;; (when (and (memq 'hanyu-dazidian-page attributes)
1303     ;;            (setq value (get-char-attribute char 'hanyu-dazidian-page)))
1304     ;;   (insert (format "(hanyu-dazidian-page . %d)%s"
1305     ;;                   value line-breaking))
1306     ;;   (setq attributes (delq 'hanyu-dazidian-page attributes))
1307     ;;   )
1308     ;; (when (and (memq 'hanyu-dazidian-char attributes)
1309     ;;            (setq value (get-char-attribute char 'hanyu-dazidian-char)))
1310     ;;   (insert (format "(hanyu-dazidian-char . %d)%s"
1311     ;;                   value line-breaking))
1312     ;;   (setq attributes (delq 'hanyu-dazidian-char attributes))
1313     ;;   )
1314     (unless readable
1315       (dolist (ignored '(composition
1316                          ->denotational <-subsumptive ->ucs-unified
1317                          ->ideographic-component-forms))
1318         (setq attributes (delq ignored attributes))))
1319     (while attributes
1320       (setq name (car attributes))
1321       (unless (eq (setq value (get-char-attribute char name 'value-is-empty))
1322                   'value-is-empty)
1323         (cond ((setq ret (find-charset name))
1324                (setq name (charset-name ret))
1325                (when (not (memq name dest-ccss))
1326                  (setq dest-ccss (cons name dest-ccss))
1327                  (char-db-insert-ccs-feature name value line-breaking))
1328                )
1329               ((string-match "^=>ucs@" (symbol-name name))
1330                (insert (format "(%-18s . #x%04X)\t; %c%s"
1331                                name value (decode-char '=ucs value)
1332                                line-breaking))
1333                )
1334               ((eq name 'jisx0208-1978/4X)
1335                (insert (format "(%-18s . #x%04X)%s"
1336                                name value
1337                                line-breaking))
1338                )
1339               ((and
1340                 (not readable)
1341                 (not (eq name '->subsumptive))
1342                 (not (eq name '->uppercase))
1343                 (not (eq name '->lowercase))
1344                 (not (eq name '->titlecase))
1345                 (not (eq name '->canonical))
1346                 (not (eq name '->Bopomofo))
1347                 (not (eq name '->mistakable))
1348                 (not (eq name '->ideographic-variants))
1349                 (null (get-char-attribute
1350                        char (intern (format "%s*sources" name))))
1351                 (not (string-match "\\*sources$" (symbol-name name)))
1352                 (null (get-char-attribute
1353                        char (intern (format "%s*note" name))))
1354                 (not (string-match "\\*note$" (symbol-name name)))
1355                 (or (eq name '<-identical)
1356                     (eq name '<-uppercase)
1357                     (eq name '<-lowercase)
1358                     (eq name '<-titlecase)
1359                     (eq name '<-canonical)
1360                     (eq name '<-ideographic-variants)
1361                     ;; (eq name '<-synonyms)
1362                     (string-match "^<-synonyms" (symbol-name name))
1363                     (eq name '<-mistakable)
1364                     (when (string-match "^->" (symbol-name name))
1365                       (cond
1366                        ((string-match "^->fullwidth" (symbol-name name))
1367                         (not (and (consp value)
1368                                   (characterp (car value))
1369                                   (encode-char
1370                                    (car value) '=ucs 'defined-only)))
1371                         )
1372                        (t)))
1373                     ))
1374                )
1375               ((or (eq name 'ideographic-structure)
1376                    (eq name 'ideographic-combination)
1377                    ;; (eq name 'ideographic-)
1378                    (eq name '=decomposition)
1379                    (char-feature-base-name= '=decomposition name)
1380                    (char-feature-base-name= '=>decomposition name)
1381                    ;; (string-match "^=>*decomposition\\(@[^*]+\\)?$"
1382                    ;;               (symbol-name name))
1383                    (string-match "^\\(->\\|<-\\)[^*]*$" (symbol-name name))
1384                    (string-match "^\\(->\\|<-\\)[^*]*\\*sources$"
1385                                  (symbol-name name))
1386                    )
1387                (char-db-insert-relation-feature char name value
1388                                                 line-breaking
1389                                                 ccss readable))
1390               ;; ((memq name '(ideograph=
1391               ;;               original-ideograph-of
1392               ;;               ancient-ideograph-of
1393               ;;               vulgar-ideograph-of
1394               ;;               wrong-ideograph-of
1395               ;;               ;; simplified-ideograph-of
1396               ;;               ideographic-variants
1397               ;;               ;; ideographic-different-form-of
1398               ;;               ))
1399               ;;  (insert (format "(%-18s%s " name line-breaking))
1400               ;;  (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1401               ;;        separator nil)
1402               ;;  (while (consp value)
1403               ;;    (setq cell (car value))
1404               ;;    (if (and (consp cell)
1405               ;;             (consp (car cell)))
1406               ;;        (progn
1407               ;;          (if separator
1408               ;;              (insert lbs))
1409               ;;          (char-db-insert-alist cell readable)
1410               ;;          (setq separator lbs))
1411               ;;      (if separator
1412               ;;          (insert separator))
1413               ;;      (insert (prin1-to-string cell))
1414               ;;      (setq separator " "))
1415               ;;    (setq value (cdr value)))
1416               ;;  (insert ")")
1417               ;;  (insert line-breaking))
1418               ((consp value)
1419                (insert (format "(%-18s " name))
1420                (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1421                      separator nil)
1422                (while (consp value)
1423                  (setq cell (car value))
1424                  (if (and (consp cell)
1425                           (consp (car cell))
1426                           (setq ret (condition-case nil
1427                                         (find-char cell)
1428                                       (error nil))))
1429                      (progn
1430                        (setq rest cell
1431                              al nil
1432                              cal nil)
1433                        (while rest
1434                          (setq key (car (car rest)))
1435                          (if (find-charset key)
1436                              (setq cal (cons key cal))
1437                            (setq al (cons key al)))
1438                          (setq rest (cdr rest)))
1439                        (if separator
1440                            (insert lbs))
1441                        (insert-char-attributes ret
1442                                                readable
1443                                                al ; cal
1444                                                nil 'for-sub-node)
1445                        (setq separator lbs))
1446                    (setq ret (prin1-to-string cell))
1447                    (if separator
1448                        (if (< (+ (current-column)
1449                                  (length ret)
1450                                  (length separator))
1451                               76)
1452                            (insert separator)
1453                          (insert lbs)))
1454                    (insert ret)
1455                    (setq separator " "))
1456                  (setq value (cdr value)))
1457                (insert ")")
1458                (insert line-breaking))
1459               (t
1460                (insert (format "(%-18s" name))
1461                (setq ret (prin1-to-string value))
1462                (unless (< (+ (current-column)
1463                              (length ret)
1464                              3)
1465                           76)
1466                  (insert line-breaking))
1467                (insert " . " ret ")" line-breaking)
1468                ;; (insert (format "(%-18s . %S)%s"
1469                ;;                 name value
1470                ;;                 line-breaking))
1471                )
1472               ))
1473       (setq attributes (cdr attributes)))
1474     (insert ")")))
1475
1476 (defun insert-char-data (char &optional readable
1477                               attributes)
1478   (save-restriction
1479     (narrow-to-region (point)(point))
1480     (insert "(define-char
1481   '")
1482     (insert-char-attributes char readable attributes)
1483     (insert ")\n")
1484     (goto-char (point-min))
1485     (while (re-search-forward "[ \t]+$" nil t)
1486       (replace-match ""))
1487     ;; from tabify.
1488     (goto-char (point-min))
1489     (while (re-search-forward "[ \t][ \t][ \t]*" nil t)
1490       (let ((column (current-column))
1491             (indent-tabs-mode t))
1492         (delete-region (match-beginning 0) (point))
1493         (indent-to column)))
1494     (goto-char (point-max))
1495     ;; (tabify (point-min)(point-max))
1496     ))
1497
1498 (defun insert-char-data-with-variant (char &optional printable
1499                                            no-ucs-unified
1500                                            script excluded-script)
1501   (insert-char-data char printable)
1502   (let ((variants (char-variants char))
1503         rest
1504         variant vs ret)
1505     (setq variants (sort variants #'<))
1506     (setq rest variants)
1507     (setq variants (cons char variants))
1508     (while rest
1509       (setq variant (car rest))
1510       (unless (get-char-attribute variant '<-subsumptive)
1511         (if (and (or (null script)
1512                      (null (setq vs (get-char-attribute variant 'script)))
1513                      (memq script vs))
1514                  (or (null excluded-script)
1515                      (null (setq vs (get-char-attribute variant 'script)))
1516                      (not (memq excluded-script vs))))
1517             (unless (and no-ucs-unified (get-char-attribute variant '=ucs))
1518               (insert-char-data variant printable)
1519               (if (setq ret (char-variants variant))
1520                   (while ret
1521                     (or (memq (car ret) variants)
1522                         ;; (get-char-attribute (car ret) '<-subsumptive)
1523                         (setq rest (nconc rest (list (car ret)))))
1524                     (setq ret (cdr ret)))))))
1525       (setq rest (cdr rest)))))
1526
1527 (defun insert-char-range-data (min max &optional script excluded-script)
1528   (let ((code min)
1529         char)
1530     (while (<= code max)
1531       (setq char (decode-char '=ucs code))
1532       (if (encode-char char '=ucs 'defined-only)
1533           (insert-char-data-with-variant char nil 'no-ucs-unified
1534                                          script excluded-script))
1535       (setq code (1+ code)))))
1536
1537 (defun write-char-range-data-to-file (min max file
1538                                           &optional script excluded-script)
1539   (let ((coding-system-for-write char-db-file-coding-system))
1540     (with-temp-buffer
1541       (insert (format ";; -*- coding: %s -*-\n"
1542                       char-db-file-coding-system))
1543       (insert-char-range-data min max script excluded-script)
1544       (write-region (point-min)(point-max) file))))
1545
1546 (defvar what-character-original-window-configuration)
1547
1548 ;;;###autoload
1549 (defun what-char-definition (char)
1550   (interactive (list (char-after)))
1551   (let ((buf (get-buffer-create "*Character Description*"))
1552         (the-buf (current-buffer))
1553         (win-conf (current-window-configuration)))
1554     (pop-to-buffer buf)
1555     (make-local-variable 'what-character-original-window-configuration)
1556     (setq what-character-original-window-configuration win-conf)
1557     (setq buffer-read-only nil)
1558     (erase-buffer)
1559     (condition-case err
1560         (progn
1561           (insert-char-data-with-variant char 'printable)
1562           (unless (char-attribute-alist char)
1563             (insert (format ";; = %c\n"
1564                             (let* ((rest (split-char char))
1565                                    (ccs (pop rest))
1566                                    (code (pop rest)))
1567                               (while rest
1568                                 (setq code (logior (lsh code 8)
1569                                                    (pop rest))))
1570                               (decode-char ccs code)))))
1571           ;; (char-db-update-comment)
1572           (set-buffer-modified-p nil)
1573           (view-mode the-buf (lambda (buf)
1574                                (set-window-configuration
1575                                 what-character-original-window-configuration)
1576                                ))
1577           (goto-char (point-min)))
1578       (error (progn
1579                (set-window-configuration
1580                 what-character-original-window-configuration)
1581                (signal (car err) (cdr err)))))))
1582
1583
1584 ;;; @ end
1585 ;;;
1586
1587 (provide 'char-db-util)
1588
1589 ;;; char-db-util.el ends here