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