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