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