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