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