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