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