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