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