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