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