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