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