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