(char-db-coded-charset-priority-list): Add `=+>ucs-var-003'.
[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,
5 ;;   2020 MORIOKA Tomohiko.
6
7 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
8 ;; Keywords: CHISE, Character Database, ISO/IEC 10646, UCS, Unicode, MULE.
9
10 ;; This file is part of XEmacs CHISE.
11
12 ;; XEmacs CHISE is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 2, or (at
15 ;; your option) any later version.
16
17 ;; XEmacs CHISE is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 ;; General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with XEmacs CHISE; see the file COPYING.  If not, write to
24 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Code:
28
29 (require 'chise-subr)
30 (require 'ideograph-subr)
31
32 (defconst unidata-normative-category-alist
33   '(("Lu" letter        uppercase)
34     ("Ll" letter        lowercase)
35     ("Lt" letter        titlecase)
36     ("Mn" mark          non-spacing)
37     ("Mc" mark          spacing-combining)
38     ("Me" mark          enclosing)
39     ("Nd" number        decimal-digit)
40     ("Nl" number        letter)
41     ("No" number        other)
42     ("Zs" separator     space)
43     ("Zl" separator     line)
44     ("Zp" separator     paragraph)
45     ("Cc" other         control)
46     ("Cf" other         format)
47     ("Cs" other         surrogate)
48     ("Co" other         private-use)
49     ("Cn" other         not-assigned)))
50
51 (defconst unidata-informative-category-alist
52   '(("Lm" letter        modifier)
53     ("Lo" letter        other)
54     ("Pc" punctuation   connector)
55     ("Pd" punctuation   dash)
56     ("Ps" punctuation   open)
57     ("Pe" punctuation   close)
58     ("Pi" punctuation   initial-quote)
59     ("Pf" punctuation   final-quote)
60     ("Po" punctuation   other)
61     ("Sm" symbol        math)
62     ("Sc" symbol        currency)
63     ("Sk" symbol        modifier)
64     ("So" symbol        other)
65     ))
66
67 (defconst shuowen-radicals
68   [?一 ?上 ?示 ?三 ?王 ?玉 ?玨 ?气 ?士 ?丨 ; 010
69    ?屮 ?艸 ?蓐 ?茻 ?小 ?八 ?釆 ?半 ?牛 ?犛 ; 020
70    ?告 ?口 ?凵 ?吅 ?哭 ?走 ?止 ?癶 ?步 ?此 ; 030
71    ?正 ?是 ?辵 ?彳 ?廴 ?㢟 ?行 ?齒 ?牙 ?足 ; 040
72    ?疋 ?品 ?龠 ?冊 ?㗊 ?舌 ?干 ?𧮫 ?只 ?㕯 ; 050
73    ?句 ?丩 ?古 ?十 ?卅 ?言 ?誩 ?音 ?䇂 ?丵 ; 060
74    ?菐 ?廾 ?𠬜 ?共 ?異 ?舁 ?𦥑 ?䢅 ?爨 ?革 ; 070
75    ?鬲 ?䰜 ?爪 ?𠃨 ?鬥 ?又 ?𠂇 ?史 ?支 ?𦘒 ; 080
76    ?聿 ?畫 ?隶 ?臤 ?臣 ?殳 ?殺 ?𠘧 ?寸 ?皮 ; 090
77    ?㼱 ?攴 ?敎 ?卜 ?用 ?爻 ?㸚 ?𥄎 ?目 ?䀠 ; 100
78    ?眉 ?盾 ?自 ?白 ?鼻 ?皕 ?習 ?羽 ?隹 ?奞 ; 110
79    ?雈 ?𦫳 ?𥄕 ?羊 ?羴 ?瞿 ?雔 ?雥 ?鳥 ?烏 ; 120
80    ?𠦒 ?冓 ?幺 ?𢆶 ?叀 ?玄 ?予 ?放 ?𠬪 ?𣦼 ; 130
81    ?歺 ?死 ?冎 ?骨 ?肉 ?筋 ?刀 ?刃 ?㓞 ?丯 ; 140
82    ?耒 ?角 ?竹 ?箕 ?丌 ?左 ?工 ?㠭 ?巫 ?甘 ; 150
83    ?曰 ?乃 ?丂 ?可 ?兮 ?号 ?亏 ?旨 ?喜 ?壴 ; 160
84 ;  ?旨 ?曰 ?乃 ?丂 ?可 ?兮 ?号 ?亏 ?喜 ?壴 ; 160
85    ?鼓 ?豈 ?豆 ?豊 ?豐 ?䖒 ?虍 ?虎 ?虤 ?皿 ; 170
86    ?𠙴 ?去 ?血 ?丶 ?丹 ?青 ?井 ?皀 ?鬯 ?食 ; 180
87    ?亼 ?會 ?倉 ?入 ?缶 ?矢 ?高 ?冂 ?𩫏 ?京 ; 190
88    ?亯 ?𣆪 ?畗 ?㐭 ?嗇 ?來 ?麥 ?夊 ?舛 ?䑞 ; 200
89    ?韋 ?弟 ?夂 ?久 ?桀 ?木 ?東 ?林 ?才 ?叒 ; 210
90    ?之 ?帀 ?出 ?𣎵 ?生 ?乇 ?𠂹 ?𠌶 ?華 ?𥝌 ; 220
91    ?稽 ?巢 ?桼 ?束 ?㯻 ?囗 ?員 ?貝 ?邑 ?𨛜 ; 230
92    ?日 ?旦 ?倝 ?㫃 ?冥 ?晶 ?月 ?有 ?明 ?囧 ; 240
93    ?夕 ?多 ?毌 ?𢎘 ?𣐺 ?𠧪 ?齊 ?朿 ?片 ?鼎 ; 250
94    ?克 ?彔 ?禾 ?秝 ?黍 ?香 ?米 ?毇 ?臼 ?凶 ; 260
95    ?𣎳 ?𣏟 ?麻 ?尗 ?耑 ?韭 ?瓜 ?瓠 ?宀 ?宮 ; 270
96    ?呂 ?穴 ?㝱 ?疒 ?冖 ?𠔼 ?冃 ?㒳 ?网 ?襾 ; 280
97    ?巾 ?巿 ?帛 ?白 ?㡀 ?黹 ?人 ?𠤎 ?匕 ?从 ; 290
98    ?比 ?北 ?丘 ?㐺 ?𡈼 ?重 ?臥 ?身 ?㐆 ?衣 ; 300
99    ?裘 ?老 ?毛 ?毳 ?尸 ?尺 ?尾 ?履 ?舟 ?方 ; 310
100    ?儿 ?兄 ?兂 ?皃 ?𠑹 ?先 ?秃 ?見 ?覞 ?欠 ; 320
101    ?㱃 ?㳄 ?旡 ?頁 ?𦣻 ?面 ?丏 ?首 ?𥄉 ?須 ; 330
102    ?彡 ?彣 ?文 ?髟 ?后 ?司 ?卮 ?卩 ?印 ?色 ; 340
103    ?𠨍 ?辟 ?勹 ?包 ?茍 ?鬼 ?甶 ?厶 ?嵬 ?山 ; 350
104    ?屾 ?屵 ?广 ?厂 ?丸 ?危 ?石 ?長 ?勿 ?冄 ; 360
105    ?而 ?豕 ?㣇 ?彑 ?豚 ?豸 ?𤉡 ?易 ?象 ?馬 ; 370
106    ?𢊁 ?鹿 ?麤 ?㲋 ?兔 ?萈 ?犬 ?㹜 ?鼠 ?能 ; 380
107    ?熊 ?火 ?炎 ?黑 ?囪 ?焱 ?炙 ?赤 ?大 ?亦 ; 390
108    ?夨 ?夭 ?交 ?尣 ?壺 ?壹 ?㚔 ?奢 ?亢 ?夲 ; 400
109    ])
110
111 (defun shuowen-radical (number)
112   (aref shuowen-radicals (1- number)))
113
114 (defvar char-db-file-coding-system 'utf-8-mcs-er)
115
116 (defvar char-db-ignored-attributes '(ideographic-products))
117
118 (defvar char-db-coded-charset-priority-list
119   '(ascii
120     control-1
121     latin-iso8859-1
122     latin-iso8859-2
123     latin-iso8859-3
124     latin-iso8859-4
125     latin-iso8859-9
126     latin-jisx0201
127     cyrillic-iso8859-5
128     greek-iso8859-7
129     thai-tis620
130     ;; =mj
131     ;; =adobe-japan1-0
132     ;; =adobe-japan1-1
133     ;; =adobe-japan1-2
134     ;; =adobe-japan1-3
135     ;; =adobe-japan1-4
136     ;; =adobe-japan1-5
137     ;; =adobe-japan1-6
138     =jis-x0208
139     =jis-x0208@1978
140     =jis-x0208@1983
141     japanese-jisx0212
142     chinese-gb2312
143     =jis-x0208@1990
144     chinese-cns11643-1
145     chinese-cns11643-2
146     chinese-cns11643-3
147     chinese-cns11643-4
148     chinese-cns11643-5
149     chinese-cns11643-6
150     chinese-cns11643-7
151     =jis-x0213-1
152     =jis-x0213-1@2000
153     =jis-x0213-1@2004
154     =jis-x0213-2
155     korean-ksc5601
156     chinese-isoir165
157     katakana-jisx0201
158     hebrew-iso8859-8
159     chinese-gb12345
160     latin-viscii
161     ethiopic-ucs
162     =big5-cdp
163     =hanyo-denshi/ja
164     =hanyo-denshi/jb
165     =hanyo-denshi/jc
166     =hanyo-denshi/jd
167     =hanyo-denshi/ft
168     =hanyo-denshi/ia
169     =hanyo-denshi/ib
170     =hanyo-denshi/hg
171     =hanyo-denshi/jt
172     =hanyo-denshi/ks
173     =hanyo-denshi/tk
174     ;; ==mj
175     ;; ==adobe-japan1-0
176     ;; ==adobe-japan1-1
177     ;; ==adobe-japan1-2
178     ;; ==adobe-japan1-3
179     ;; ==adobe-japan1-4
180     ;; ==adobe-japan1-5
181     ;; ==adobe-japan1-6
182     ==jis-x0208
183     ==jis-x0213-1
184     ==jis-x0213-2
185     ==jis-x0212
186     ==hanyo-denshi/ja
187     ==hanyo-denshi/jb
188     ==hanyo-denshi/jc
189     ==hanyo-denshi/ft
190     ==hanyo-denshi/ia
191     ==hanyo-denshi/ib
192     ==hanyo-denshi/hg
193     ==hanyo-denshi/jt
194     ==hanyo-denshi/ks
195     ==hanyo-denshi/tk
196     =gt
197     =gt-k
198     =daikanwa
199     =daijiten
200     =daikanwa@rev2
201     =daikanwa@rev1
202     =daikanwa/+p
203     ==daikanwa
204     ==daijiten
205     =cbeta
206     ideograph-hanziku-1
207     ideograph-hanziku-2
208     ideograph-hanziku-3
209     ideograph-hanziku-4
210     ideograph-hanziku-5
211     ideograph-hanziku-6
212     ideograph-hanziku-7
213     ideograph-hanziku-8
214     ideograph-hanziku-9
215     ideograph-hanziku-10
216     ideograph-hanziku-11
217     ideograph-hanziku-12
218     ;; =>>>adobe-japan1-0
219     ;; =>>>adobe-japan1-1
220     ;; =>>>adobe-japan1-2
221     ;; =>>>adobe-japan1-3
222     ;; =>>>adobe-japan1-4
223     ;; =>>>adobe-japan1-5
224     ;; =>>>adobe-japan1-6
225     ;; =>>>jis-x0208
226     ;; =>>>jis-x0213-1
227     ;; =>>>jis-x0213-2
228     ;; =>>>hanyo-denshi/ja
229     ;; =>>>hanyo-denshi/jb
230     ;; =>>>hanyo-denshi/jc
231     ;; =>>>hanyo-denshi/ft
232     ;; =>>>hanyo-denshi/ib
233     ;; =>>>hanyo-denshi/hg
234     ;; =>>>hanyo-denshi/jt
235     ;; =>>>hanyo-denshi/ks
236     ;; =>>>gt
237     =>>adobe-japan1-0
238     =>>adobe-japan1-1
239     =>>adobe-japan1-2
240     =>>adobe-japan1-3
241     =>>adobe-japan1-4
242     =>>adobe-japan1-5
243     =>>adobe-japan1-6
244     =>>jis-x0208
245     =>>jis-x0213-1
246     =>>jis-x0213-1@2000
247     =>>jis-x0213-1@2004
248     =>>jis-x0213-2
249     =>>jis-x0208@1978
250     =>>hanyo-denshi/ft
251     =>>hanyo-denshi/jt
252     =>>hanyo-denshi/ks
253     =>>gt
254     =>>daikanwa
255     =>>cbeta
256     =+>jis-x0208
257     =+>jis-x0213-1
258     =+>jis-x0213-2
259     =+>adobe-japan1-0
260     =+>adobe-japan1-1
261     =+>adobe-japan1-2
262     =+>adobe-japan1-3
263     =+>adobe-japan1-4
264     =+>adobe-japan1-5
265     =+>adobe-japan1-6
266     =+>jis-x0208@1978
267     =>jis-x0208
268     =>jis-x0208@1997
269     =>jis-x0213-1
270     =>jis-x0213-1@2000
271     =>jis-x0213-1@2004
272     =>jis-x0213-2
273     ==>ucs@bucs
274     ==>daijiten
275     ;; =>ucs@hanyo-denshi
276     =>ucs@iso
277     =>ucs@unicode
278     =>ucs@jis
279     =>ucs@cns
280     =>ucs@ks
281     =+>ucs@iso
282     =+>ucs@unicode
283     =+>ucs@jis
284     =+>ucs@jis/1990
285     =+>ucs@cns
286     =+>ucs@ks
287     =>>ucs@iso
288     =>>ucs@unicode
289     =>>ucs@jis
290     =>>ucs@cns
291     =>>>ucs@iso
292     =>>>ucs@unicode
293     ==ucs@iso
294     ==ucs@unicode
295     ;; ==ucs@cns
296     ==gb2312
297     ==ks-x1001
298     ==cns11643-1
299     ==cns11643-2
300     ==cns11643-3
301     ==cns11643-4
302     ==cns11643-5
303     ==cns11643-6
304     ==cns11643-7
305     ==gt
306     ==jis-x0208@1990
307     ;; ==jis-x0208@1983
308     ==jis-x0208@1978
309     ==gt-k
310     =ucs@iso
311     =ucs@unicode
312     =ucs@cns
313     ==big5-cdp
314     ==cbeta
315     =>>big5-cdp
316     =>>gt-k
317     =+>gt
318     =+>big5-cdp
319     =>big5-cdp
320     =>big5-cdp@iwds-1
321     =>mj
322     =>iwds-1
323     =big5
324     =big5-eten
325     =zinbun-oracle
326     =>zinbun-oracle
327     =ruimoku-v6
328     =>>ruimoku-v6
329     ==ruimoku-v6
330     =jef-china3
331     =shinjigen
332     =>cns11643-5
333     =>cns11643-7
334     =>gt
335     =>gt-k
336     =>daikanwa
337     =>daikanwa/ho
338     =>cbeta
339     =ucs-var-001
340     =ucs-var-002
341     =ucs-var-003
342     =ucs-var-004
343     =ucs-var-005
344     =ucs-var-006
345     =ucs-var-007
346     =ucs-var-008
347     =ucs-var-010
348     =ucs-itaiji-001
349     =ucs-itaiji-002
350     =ucs-itaiji-003
351     =ucs-itaiji-004
352     =ucs-itaiji-005
353     =ucs-itaiji-006
354     =ucs-itaiji-007
355     =ucs-itaiji-008
356     =ucs-itaiji-009
357     =ucs-itaiji-010
358     =ucs-itaiji-011
359     =ucs-itaiji-012
360     =+>ucs-var-003
361     =>ucs-itaiji-001
362     =>ucs-itaiji-002
363     =>ucs-itaiji-003
364     =>ucs-itaiji-004
365     =>ucs-itaiji-005
366     =>ucs-itaiji-006
367     =>ucs-itaiji-007
368     =>ucs-itaiji-008
369     =>ucs-itaiji-009
370     =>ucs-itaiji-010
371     =>ucs-itaiji-011
372     =big5-cdp-var-001
373     =big5-cdp-var-002
374     =big5-cdp-var-003
375     =big5-cdp-var-004
376     =big5-cdp-var-005
377     =big5-cdp-var-007
378     =big5-cdp-var-010
379     =big5-cdp-itaiji-001
380     =big5-cdp-itaiji-002
381     =big5-cdp-itaiji-003
382     =>big5-cdp-itaiji-001
383     =>big5-cdp-itaiji-003
384     =>big5-cdp-var-001
385     =>ucs@iwds-1
386     =>ucs@cognate
387     =>ucs@component
388     =>ucs@iwds-1/normalized
389     =>ucs-itaiji-001@iwds-1
390     =>ucs-itaiji-002@iwds-1
391     =>big5-cdp@component
392     =>big5-cdp@cognate
393     ==ucs@gb
394     =ucs@gb
395     ==ucs-var-002
396     =ucs@JP/hanazono))
397
398
399 ;;; @ char-db formatters
400 ;;;
401
402 (defun char-db-make-char-spec (char)
403   (let (ret char-spec)
404     (cond ((characterp char)
405            (cond ((and (setq ret (encode-char char '=ucs 'defined-only))
406                        (not (and (<= #xE000 ret)(<= ret #xF8FF))))
407                   (setq char-spec (list (cons '=ucs ret)))
408                   (cond ((setq ret (get-char-attribute char 'name))
409                          (setq char-spec (cons (cons 'name ret) char-spec))
410                          )
411                         ((setq ret (get-char-attribute char 'name*))
412                          (setq char-spec (cons (cons 'name* ret) char-spec))
413                          ))
414                   )
415                  ((encode-char char '=mj 'defined-only)
416                   (setq char-spec nil)
417                   (dolist (ccs (charset-list))
418                     (if (and (or (eq ccs '=mj)
419                                  ;; (eq (charset-property ccs 'iso-ir) 177)
420                                  (string-match "=ucs@" (symbol-name ccs))
421                                  )
422                              (setq ccs (charset-name ccs))
423                              (null (assq ccs char-spec))
424                              (setq ret (encode-char char ccs 'defined-only)))
425                         (setq char-spec (cons (cons ccs ret) char-spec))))
426                   )
427                  ((encode-char char '==mj 'defined-only)
428                   (setq char-spec nil)
429                   (dolist (ccs (charset-list))
430                     (if (and (or (eq ccs '==mj)
431                                  ;; (eq (charset-property ccs 'iso-ir) 177)
432                                  (string-match "=ucs@" (symbol-name ccs))
433                                  )
434                              (setq ccs (charset-name ccs))
435                              (null (assq ccs char-spec))
436                              (setq ret (encode-char char ccs 'defined-only)))
437                         (setq char-spec (cons (cons ccs ret) char-spec))))
438                   )
439                  ((encode-char char '=adobe-japan1 'defined-only)
440                   (setq char-spec nil)
441                   (dolist (ccs (charset-list))
442                     (if (and (or (memq ccs
443                                        '(=adobe-japan1-0
444                                          =adobe-japan1-1
445                                          =adobe-japan1-2
446                                          =adobe-japan1-3
447                                          =adobe-japan1-4
448                                          =adobe-japan1-5
449                                          =adobe-japan1-6
450                                          ))
451                                  ;; (eq (charset-property ccs 'iso-ir) 177)
452                                  (string-match "=ucs@" (symbol-name ccs))
453                                  )
454                              (setq ccs (charset-name ccs))
455                              (null (assq ccs char-spec))
456                              (setq ret (encode-char char ccs 'defined-only)))
457                         (setq char-spec (cons (cons ccs ret) char-spec))))
458                   )
459                  ((encode-char char '==adobe-japan1 'defined-only)
460                   (setq char-spec nil)
461                   (dolist (ccs (charset-list))
462                     (if (and (or (memq ccs
463                                        '(==adobe-japan1-0
464                                          ==adobe-japan1-1
465                                          ==adobe-japan1-2
466                                          ==adobe-japan1-3
467                                          ==adobe-japan1-4
468                                          ==adobe-japan1-5
469                                          ==adobe-japan1-6
470                                          ))
471                                  ;; (eq (charset-property ccs 'iso-ir) 177)
472                                  (string-match "=ucs@" (symbol-name ccs))
473                                  )
474                              (setq ccs (charset-name ccs))
475                              (null (assq ccs char-spec))
476                              (setq ret (encode-char char ccs 'defined-only)))
477                         (setq char-spec (cons (cons ccs ret) char-spec))))
478                   )
479                  ((setq ret
480                         (catch 'tag
481                           (let ((rest char-db-coded-charset-priority-list)
482                                 ccs)
483                             (while rest
484                               (setq ccs (charset-name
485                                          (find-charset (car rest))))
486                               (if (setq ret
487                                         (encode-char char ccs
488                                                      'defined-only))
489                                   (throw 'tag (cons ccs ret)))
490                               (setq rest (cdr rest))))))
491                   (setq char-spec (list ret))
492                   (dolist (ccs (delq (car ret) (charset-list)))
493                     (if (and (or (charset-iso-final-char ccs)
494                                  (memq ccs
495                                        '(=daikanwa
496                                          =daikanwa@rev2
497                                          ;; =gt-k
498                                          =jis-x0208@1997
499                                          ))
500                                  (and (eq (charset-property ccs 'iso-ir) 177)
501                                       (not (eq (charset-name ccs) '=ucs@big5)))
502                                  ;; (string-match "=ucs@" (symbol-name ccs))
503                                  )
504                              (setq ccs (charset-name ccs))
505                              (null (assq ccs char-spec))
506                              (setq ret (encode-char char ccs 'defined-only)))
507                         (setq char-spec (cons (cons ccs ret) char-spec))))
508                   (if (null char-spec)
509                       (setq char-spec (split-char char)))
510                   (cond ((setq ret (get-char-attribute char 'name))
511                          (setq char-spec (cons (cons 'name ret) char-spec))
512                          )
513                         ((setq ret (get-char-attribute char 'name*))
514                          (setq char-spec (cons (cons 'name* ret) char-spec))
515                          ))
516                   )
517                  ((setq ret (get-char-attribute
518                              char 'ideographic-combination))
519                   (setq char-spec
520                         (cons (cons 'ideographic-combination ret)
521                               char-spec))
522                   ))
523            char-spec)
524           ((consp char)
525            char))))
526     
527 (defun char-db-insert-char-spec (char &optional readable column
528                                       required-features)
529   (unless column
530     (setq column (current-column)))
531   (let (char-spec temp-char)
532     (setq char-spec (char-db-make-char-spec char))
533     (unless (or (characterp char) ; char
534                 (condition-case nil
535                     (setq char (find-char char-spec))
536                   (error nil)))
537       ;; define temporary character
538       ;;   Current implementation is dirty.
539       (setq temp-char (define-char (cons '(ideograph-daikanwa . 0)
540                                          char-spec)))
541       (remove-char-attribute temp-char 'ideograph-daikanwa)
542       (setq char temp-char))
543     (insert-char-attributes char
544                             readable
545                             (union (mapcar #'car char-spec)
546                                    required-features)
547                             nil 'for-sub-node)
548     (when temp-char
549       ;; undefine temporary character
550       ;;   Current implementation is dirty.
551       (setq char-spec (char-attribute-alist temp-char))
552       (while char-spec
553         (remove-char-attribute temp-char (car (car char-spec)))
554         (setq char-spec (cdr char-spec))))))
555
556 (defun char-db-insert-alist (alist &optional readable column)
557   (unless column
558     (setq column (current-column)))
559   (let ((line-breaking
560          (concat "\n" (make-string (1+ column) ?\ )))
561         name value
562         ret al ; cal
563         key
564         lbs cell rest separator)
565     (insert "(")
566     (while alist
567       (setq name (car (car alist))
568             value (cdr (car alist)))
569       (cond ((eq name 'char)
570              (insert "(char . ")
571              (if (setq ret (condition-case nil
572                                (find-char value)
573                              (error nil)))
574                  (progn
575                    (setq al nil
576                          ;; cal nil
577                          )
578                    (while value
579                      (setq key (car (car value)))
580                      ;; (if (find-charset key)
581                      ;;     (setq cal (cons key cal))
582                      (setq al (cons key al))
583                      ;; )
584                      (setq value (cdr value)))
585                    (insert-char-attributes ret
586                                            readable
587                                            (or al 'none) ; cal
588                                            nil 'for-sub-node))
589                (insert (prin1-to-string value)))
590              (insert ")")
591              (insert line-breaking))
592             ((consp value)
593              (insert (format "(%-18s " name))
594              (setq lbs (concat "\n" (make-string (current-column) ?\ )))
595              (while (consp value)
596                (setq cell (car value))
597                (if (and (consp cell)
598                         (consp (car cell))
599                         (setq ret (condition-case nil
600                                       (find-char cell)
601                                     (error nil)))
602                         )
603                    (progn
604                      (setq rest cell
605                            al nil
606                            ;; cal nil
607                            )
608                      (while rest
609                        (setq key (car (car rest)))
610                        ;; (if (find-charset key)
611                        ;;     (setq cal (cons key cal))
612                        (setq al (cons key al))
613                        ;; )
614                        (setq rest (cdr rest)))
615                      (if separator
616                          (insert lbs))
617                      (insert-char-attributes ret
618                                              readable
619                                              al ; cal
620                                              nil 'for-sub-node)
621                      (setq separator lbs))
622                  (if separator
623                      (insert separator))
624                  (insert (prin1-to-string cell))
625                  (setq separator " "))
626                (setq value (cdr value)))
627              (insert ")")
628              (insert line-breaking))
629             (t
630              (insert (format "(%-18s . %S)%s"
631                              name value
632                              line-breaking))))
633       (setq alist (cdr alist))))
634   (insert ")"))
635
636 (defun char-db-insert-char-reference (plist &optional readable column)
637   (unless column
638     (setq column (current-column)))
639   (let ((line-breaking
640          (concat "\n" (make-string (1+ column) ?\ )))
641         (separator "")
642         name value)
643     (insert "(")
644     (while plist
645       (setq name (pop plist))
646       (setq value (pop plist))
647       (cond ((eq name :char)
648              (insert separator)
649              (insert ":char\t")
650              (cond ((numberp value)
651                     (setq value (decode-char '=ucs value)))
652                    ;; ((consp value)
653                    ;;  (setq value (or (find-char value)
654                    ;;                  value)))
655                    )
656              (char-db-insert-char-spec value readable)
657              (insert line-breaking)
658              (setq separator ""))
659             ((eq name :radical)
660              (insert (format "%s%s\t%d ; %c%s"
661                              separator
662                              name value
663                              (ideographic-radical value)
664                              line-breaking))
665              (setq separator ""))
666             (t
667              (insert (format "%s%s\t%S" separator name value))
668              (setq separator line-breaking)))
669       ))
670   (insert ")"))
671
672 (defun char-db-decode-isolated-char (ccs code-point)
673   (let (ret)
674     (setq ret
675           (cond ((eq ccs 'arabic-iso8859-6)
676                  (decode-char ccs code-point))
677                 ;; ((eq ccs '=gt)
678                 ;;  (decode-builtin-char '==gt code-point))
679                 ((and (memq ccs '(=gt-pj-1
680                                   =gt-pj-2
681                                   =gt-pj-3
682                                   =gt-pj-4
683                                   =gt-pj-5
684                                   =gt-pj-6
685                                   =gt-pj-7
686                                   =gt-pj-8
687                                   =gt-pj-9
688                                   =gt-pj-10
689                                   =gt-pj-11))
690                       (setq ret (decode-char ccs code-point))
691                       (setq ret (encode-char ret '=gt 'defined-only)))
692                  (decode-builtin-char '=gt ret))
693                 (t
694                  (decode-builtin-char ccs code-point))))
695     (cond ((null ret)
696            (or (decode-char ccs code-point)
697                (define-char (list (cons ccs code-point)))))
698           ((and (<= 0 (char-int ret))
699                 (<= (char-int ret) #x1F))
700            (decode-char '=ucs (+ #x2400 (char-int ret))))
701           ((= (char-int ret) #x7F)
702            ?\u2421)
703           (t ret))))
704
705 (defvar char-db-convert-obsolete-format t)
706
707 (defun char-db-insert-ccs-feature (name value line-breaking)
708   (cond
709    ((integerp value)
710     (insert
711      (format
712       (cond
713        ((memq name '(=>iwds-1
714                      ==shinjigen
715                      =shinjigen
716                      =shinjigen@1ed ==shinjigen@1ed
717                      =shinjigen@rev ==shinjigen@rev
718                      =shinjigen/+p@rev ==shinjigen/+p@rev
719                      ===daikanwa/ho ==daikanwa/ho
720                      =daikanwa/ho =>>daikanwa/ho =>daikanwa/ho))
721         "(%-18s .  %04d)\t; %c")
722        ((eq name '=shinjigen@1ed/24pr)
723         "(%-18s . %04d)\t; %c")
724        ((or
725          (memq name
726                '(===daikanwa
727                  ==daikanwa =daikanwa =>>daikanwa =>daikanwa
728                  =daikanwa@rev1 =daikanwa@rev2
729                  =daikanwa/+p ==daikanwa/+p ===daikanwa/+p
730                  =>>daikanwa/+p
731                  =daikanwa/+2p =>>daikanwa/+2p
732                  =gt ==gt ===gt
733                  =>>gt =+>gt =>gt
734                  =gt-k ==gt-k ===gt-k
735                  =>>gt-k =>gt-k
736                  =adobe-japan1-0 ==adobe-japan1-0 ===adobe-japan1-0
737                  =adobe-japan1-1 ==adobe-japan1-1 ===adobe-japan1-1
738                  =adobe-japan1-2 ==adobe-japan1-2 ===adobe-japan1-2
739                  =adobe-japan1-3 ==adobe-japan1-3 ===adobe-japan1-3
740                  =adobe-japan1-4 ==adobe-japan1-4 ===adobe-japan1-4
741                  =adobe-japan1-5 ==adobe-japan1-5 ===adobe-japan1-5
742                  =adobe-japan1-6 ==adobe-japan1-6 ===adobe-japan1-6
743                  =>>adobe-japan1-0 =+>adobe-japan1-0
744                  =>>adobe-japan1-1 =+>adobe-japan1-1
745                  =>>adobe-japan1-2 =+>adobe-japan1-2
746                  =>>adobe-japan1-3 =+>adobe-japan1-3
747                  =>>adobe-japan1-4 =+>adobe-japan1-4
748                  =>>adobe-japan1-5 =+>adobe-japan1-5
749                  =>>adobe-japan1-6 =+>adobe-japan1-6
750                  =>cbeta =cbeta =>>cbeta ==cbeta ===cbeta
751                  =zinbun-oracle =>zinbun-oracle
752                  =daijiten ==daijiten ===daijiten ==>daijiten
753                  ===hng-jou ===hng-keg ===hng-dng ===hng-mam
754                  ===hng-drt ===hng-kgk ===hng-myz ===hng-kda
755                  ===hng-khi ===hng-khm ===hng-fhs ===hng-hok
756                  ===hng-kyd ===hng-sok
757                  ===hng-yhk ===hng-kak ===hng-kar ===hng-kae
758                  ===hng-sys ===hng-tsu ===hng-tzj
759                  ===hng-hos ===hng-kkh ===hng-nak ===hng-jhk
760                  ===hng-hod ===hng-gok ===hng-ink ===hng-nto
761                  ===hng-nkm ===hng-k24 ===hng-ini ===hng-nkk
762                  ===hng-kcc ===hng-kcj ===hng-kbk ===hng-sik
763                  ===hng-skk ===hng-kyu ===hng-ksk ===hng-wan
764                  ===hng-okd ===hng-wad ===hng-kmi
765                  ===hng-zkd ===hng-doh ===hng-jyu ===hng-tzs
766                  ===hng-sai ===hng-kad ===hng-kss
767                  ===hng-kyo ===hng-ykk ===hng-saa
768                  ===hng-sab ===hng-wks ===hng-wke ===hng-smk
769                  ===hng-sgs ===hng-sts
770                  =shuowen-jiguge ===shuowen-jiguge4 ===shuowen-jiguge5))
771          ;; (string-match "^=adobe-" (symbol-name name))
772          )
773         "(%-18s . %05d)\t; %c")
774        ((memq name '(=hanyo-denshi/ks
775                      ==hanyo-denshi/ks ===hanyo-denshi/ks
776                      =>>hanyo-denshi/ks
777                      =koseki ==koseki
778                      =mj ==mj ===mj =>>mj =>mj
779                      =zihai mojikyo))
780         "(%-18s . %06d)\t; %c")
781        ((memq name '(=hanyo-denshi/tk ==hanyo-denshi/tk))
782         "(%-18s . %08d)\t; %c")
783        ((>= (charset-dimension name) 2)
784         "(%-18s . #x%04X)\t; %c")
785        (t
786         "(%-18s . #x%02X)\t; %c"))
787       name
788       (if (= (charset-iso-graphic-plane name) 1)
789           (logior value
790                   (cond ((= (charset-dimension name) 1)
791                          #x80)
792                         ((= (charset-dimension name) 2)
793                          #x8080)
794                         ((= (charset-dimension name) 3)
795                          #x808080)
796                         (t 0)))
797         value)
798       (char-db-decode-isolated-char name value)))
799     (if (and (= (charset-chars name) 94)
800              (= (charset-dimension name) 2))
801         (insert (format " [%02d-%02d]"
802                         (- (lsh value -8) 32)
803                         (- (logand value 255) 32))))
804     )
805    (t
806     (insert (format "(%-18s . %s)" name value))
807     ))
808   (insert line-breaking))
809
810 (defun char-db-insert-relation-feature (char name value line-breaking
811                                              ccss readable)
812   (insert (format "(%-18s%s " name line-breaking))
813   (let ((lbs (concat "\n" (make-string (current-column) ?\ )))
814         separator cell sources required-features
815         ret)
816     (while (consp value)
817       (setq cell (car value))
818       (if (integerp cell)
819           (setq cell (decode-char '=ucs cell)))
820       (cond
821        ((eq name '->subsumptive)
822         (when (or (not (some (lambda (atr)
823                                (get-char-attribute cell atr))
824                              char-db-ignored-attributes))
825                   (some (lambda (ccs)
826                           (encode-char cell ccs 'defined-only))
827                         ccss))
828           (if separator
829               (insert lbs))
830           (let ((char-db-ignored-attributes
831                  (cons '<-subsumptive
832                        char-db-ignored-attributes)))
833             (insert-char-attributes cell readable nil nil 'for-sub-node))
834           (setq separator lbs))
835         )
836        ((characterp cell)
837         (setq sources
838               (get-char-attribute
839                char (intern (format "%s*sources" name))))
840         (setq required-features nil)
841         (dolist (source sources)
842           (cond
843            ((memq source '(JP
844                            JP/Jouyou
845                            shinjigen shinjigen@1ed shinjigen@rev))
846             (setq required-features
847                   (union required-features
848                          '(=jis-x0208
849                            =jis-x0208@1990
850                            =jis-x0213-1@2000
851                            =jis-x0213-1@2004
852                            =jis-x0213-2
853                            =jis-x0212
854                            =jis-x0208@1983
855                            =jis-x0208@1978
856                            =shinjigen
857                            =shinjigen@1ed
858                            =shinjigen@rev
859                            =shinjigen/+p@rev))))
860            ((eq source 'CN)
861             (setq required-features
862                   (union required-features
863                          '(=gb2312
864                            =gb12345
865                            =iso-ir165)))))
866           (cond
867            ((find-charset (setq ret (intern (format "=%s" source))))
868             (setq required-features
869                   (cons ret required-features)))
870            (t (setq required-features
871                     (cons source required-features)))))
872         (cond ((string-match "@JP" (symbol-name name))
873                (setq required-features
874                      (union required-features
875                             '(=jis-x0208
876                               =jis-x0208@1990
877                               =jis-x0213-1-2000
878                               =jis-x0213-2-2000
879                               =jis-x0212
880                               =jis-x0208@1983
881                               =jis-x0208@1978))))
882               ((string-match "@CN" (symbol-name name))
883                (setq required-features
884                      (union required-features
885                             '(=gb2312
886                               =gb12345
887                               =iso-ir165)))))
888         (if separator
889             (insert lbs))
890         (if readable
891             (insert (format "%S" cell))
892           (char-db-insert-char-spec cell readable
893                                     nil
894                                     required-features))
895         (setq separator lbs))
896        ((consp cell)
897         (if separator
898             (insert lbs))
899         (if (consp (car cell))
900             (char-db-insert-char-spec cell readable)
901           (char-db-insert-char-reference cell readable))
902         (setq separator lbs))
903        (t
904         (if separator
905             (insert separator))
906         (insert (prin1-to-string cell))
907         (setq separator " ")))
908       (setq value (cdr value)))
909     (insert ")")
910     (insert line-breaking)))
911
912 (defun insert-char-attributes (char &optional readable attributes column
913                                     for-sub-node)
914   (unless column
915     (setq column (current-column)))
916   (let (name value ; has-long-ccs-name
917         rest
918         radical strokes
919         (line-breaking
920          (concat "\n" (make-string (1+ column) ?\ )))
921         lbs cell separator ret
922         key al cal
923         dest-ccss ; sources required-features
924         ccss)
925     (let (atr-d)
926       (setq attributes
927             (sort (if attributes
928                       (if (consp attributes)
929                           (progn
930                             (dolist (name attributes)
931                               (unless (memq name char-db-ignored-attributes)
932                                 (if (find-charset name)
933                                     (push name ccss))
934                                 (push name atr-d)))
935                             atr-d))
936                     (dolist (name (char-attribute-list))
937                       (unless (memq name char-db-ignored-attributes)
938                         (if (find-charset name)
939                             (push name ccss))
940                         (push name atr-d)))
941                     atr-d)
942                   #'char-attribute-name<)))
943     (insert "(")
944     (when (memq '<-subsumptive attributes)
945       (when (or readable (not for-sub-node))
946         (when (setq value (get-char-attribute char '<-subsumptive))
947           (char-db-insert-relation-feature char '<-subsumptive value
948                                            line-breaking
949                                            ccss readable)))
950       (setq attributes (delq '<-subsumptive attributes)))
951     (when (and (memq '<-denotational attributes)
952                (setq value (get-char-attribute char '<-denotational)))
953       (char-db-insert-relation-feature char '<-denotational value
954                                        line-breaking
955                                        ccss readable)
956       (setq attributes (delq '<-denotational attributes)))
957     (when (and (memq '<-denotational@component attributes)
958                (setq value (get-char-attribute char '<-denotational@component)))
959       (char-db-insert-relation-feature char '<-denotational@component value
960                                        line-breaking
961                                        ccss readable)
962       (setq attributes (delq '<-denotational@component attributes)))
963     (when (and (memq '<-denotational@usage attributes)
964                (setq value (get-char-attribute char '<-denotational@usage)))
965       (char-db-insert-relation-feature char '<-denotational@usage value
966                                        line-breaking
967                                        ccss readable)
968       (setq attributes (delq '<-denotational@usage attributes)))
969     (when (and (memq 'name attributes)
970                (setq value (get-char-attribute char 'name)))
971       (insert (format
972                (if (> (+ (current-column) (length value)) 48)
973                    "(name . %S)%s"
974                  "(name               . %S)%s")
975                value line-breaking))
976       (setq attributes (delq 'name attributes))
977       )
978     (when (and (memq 'name* attributes)
979                (setq value (get-char-attribute char 'name*)))
980       (insert (format
981                (if (> (+ (current-column) (length value)) 48)
982                    "(name* . %S)%s"
983                  "(name*              . %S)%s")
984                value line-breaking))
985       (setq attributes (delq 'name* attributes))
986       )
987     (when (and (memq 'script attributes)
988                (setq value (get-char-attribute char 'script)))
989       (insert (format "(script\t\t%s)%s"
990                       (mapconcat (function prin1-to-string)
991                                  value " ")
992                       line-breaking))
993       (setq attributes (delq 'script attributes))
994       )
995     (dolist (name '(=>ucs =>ucs*))
996       (when (and (memq name attributes)
997                  (setq value (get-char-attribute char name)))
998         (insert (format "(%-18s . #x%04X)\t; %c%s"
999                         name value (decode-char '=ucs value)
1000                         line-breaking))
1001         (setq attributes (delq name attributes))))
1002     (dolist (name '(=>ucs@gb =>ucs@big5))
1003       (when (and (memq name attributes)
1004                  (setq value (get-char-attribute char name)))
1005         (insert (format "(%-18s . #x%04X)\t; %c%s"
1006                         name value
1007                         (decode-char (intern
1008                                       (concat "="
1009                                               (substring
1010                                                (symbol-name name) 2)))
1011                                      value)
1012                         line-breaking))
1013         (setq attributes (delq name attributes))
1014         ))
1015     ;; (dolist (name '(=>daikanwa))
1016     ;;   (when (and (memq name attributes)
1017     ;;              (setq value (get-char-attribute char name)))
1018     ;;     (insert
1019     ;;      (if (integerp value)
1020     ;;          (format "(%-18s . %05d)\t; %c%s"
1021     ;;                  name value (decode-char '=daikanwa value)
1022     ;;                  line-breaking)
1023     ;;        (format "(%-18s %s)\t; %c%s"
1024     ;;                name
1025     ;;                (mapconcat (function prin1-to-string)
1026     ;;                           value " ")
1027     ;;                (char-representative-of-daikanwa char)
1028     ;;                line-breaking)))
1029     ;;     (setq attributes (delq name attributes))))
1030     (when (and (memq 'general-category attributes)
1031                (setq value (get-char-attribute char 'general-category)))
1032       (insert (format
1033                "(general-category\t%s) ; %s%s"
1034                (mapconcat (lambda (cell)
1035                             (format "%S" cell))
1036                           value " ")
1037                (cond ((rassoc value unidata-normative-category-alist)
1038                       "Normative Category")
1039                      ((rassoc value unidata-informative-category-alist)
1040                       "Informative Category")
1041                      (t
1042                       "Unknown Category"))
1043                line-breaking))
1044       (setq attributes (delq 'general-category attributes))
1045       )
1046     (when (and (memq 'bidi-category attributes)
1047                (setq value (get-char-attribute char 'bidi-category)))
1048       (insert (format "(bidi-category\t. %S)%s"
1049                       value
1050                       line-breaking))
1051       (setq attributes (delq 'bidi-category attributes))
1052       )
1053     (unless (or (not (memq 'mirrored attributes))
1054                 (eq (setq value (get-char-attribute char 'mirrored 'empty))
1055                     'empty))
1056       (insert (format "(mirrored\t\t. %S)%s"
1057                       value
1058                       line-breaking))
1059       (setq attributes (delq 'mirrored attributes))
1060       )
1061     (cond
1062      ((and (memq 'decimal-digit-value attributes)
1063            (setq value (get-char-attribute char 'decimal-digit-value)))
1064       (insert (format "(decimal-digit-value . %S)%s"
1065                       value
1066                       line-breaking))
1067       (setq attributes (delq 'decimal-digit-value attributes))
1068       (when (and (memq 'digit-value attributes)
1069                  (setq value (get-char-attribute char 'digit-value)))
1070         (insert (format "(digit-value\t . %S)%s"
1071                         value
1072                         line-breaking))
1073         (setq attributes (delq 'digit-value attributes))
1074         )
1075       (when (and (memq 'numeric-value attributes)
1076                  (setq value (get-char-attribute char 'numeric-value)))
1077         (insert (format "(numeric-value\t . %S)%s"
1078                         value
1079                         line-breaking))
1080         (setq attributes (delq 'numeric-value attributes))
1081         )
1082       )
1083      (t
1084       (when (and (memq 'digit-value attributes)
1085                  (setq value (get-char-attribute char 'digit-value)))
1086         (insert (format "(digit-value\t. %S)%s"
1087                         value
1088                         line-breaking))
1089         (setq attributes (delq 'digit-value attributes))
1090         )
1091       (when (and (memq 'numeric-value attributes)
1092                  (setq value (get-char-attribute char 'numeric-value)))
1093         (insert (format "(numeric-value\t. %S)%s"
1094                         value
1095                         line-breaking))
1096         (setq attributes (delq 'numeric-value attributes))
1097         )))
1098     (when (and (memq 'iso-10646-comment attributes)
1099                (setq value (get-char-attribute char 'iso-10646-comment)))
1100       (insert (format "(iso-10646-comment\t. %S)%s"
1101                       value
1102                       line-breaking))
1103       (setq attributes (delq 'iso-10646-comment attributes))
1104       )
1105     (when (and (memq 'morohashi-daikanwa attributes)
1106                (setq value (get-char-attribute char 'morohashi-daikanwa)))
1107       (insert (format "(morohashi-daikanwa\t%s)%s"
1108                       (mapconcat (function prin1-to-string) value " ")
1109                       line-breaking))
1110       (setq attributes (delq 'morohashi-daikanwa attributes))
1111       )
1112     (setq radical nil
1113           strokes nil)
1114     (when (and (memq 'ideographic-radical attributes)
1115                (setq value (get-char-attribute char 'ideographic-radical)))
1116       (setq radical value)
1117       (insert (format "(ideographic-radical . %S)\t; %c%s"
1118                       radical
1119                       (ideographic-radical radical)
1120                       line-breaking))
1121       (setq attributes (delq 'ideographic-radical attributes))
1122       )
1123     (when (and (memq 'shuowen-radical attributes)
1124                (setq value (get-char-attribute char 'shuowen-radical)))
1125       (insert (format "(shuowen-radical\t. %S)\t; %c%s"
1126                       value
1127                       (shuowen-radical value)
1128                       line-breaking))
1129       (setq attributes (delq 'shuowen-radical attributes))
1130       )
1131     (let (key)
1132       (dolist (domain
1133                (append
1134                 char-db-feature-domains
1135                 (let (dest domain)
1136                   (dolist (feature (char-attribute-list))
1137                     (setq feature (symbol-name feature))
1138                     (when (string-match
1139                            "\\(radical\\|strokes\\)@\\([^@*]+\\)\\(\\*\\|$\\)"
1140                            feature)
1141                       (setq domain (intern (match-string 2 feature)))
1142                      (unless (memq domain dest)
1143                        (setq dest (cons domain dest)))))
1144                   (sort dest #'string<))))
1145         (setq key (intern (format "%s@%s" 'ideographic-radical domain)))
1146         (when (and (memq key attributes)
1147                    (setq value (get-char-attribute char key)))
1148           (setq radical value)
1149           (insert (format "(%s . %S)\t; %c%s"
1150                           key
1151                           radical
1152                           (ideographic-radical radical)
1153                           line-breaking))
1154           (setq attributes (delq key attributes))
1155           )
1156         (setq key (intern (format "%s@%s" 'ideographic-strokes domain)))
1157         (when (and (memq key attributes)
1158                    (setq value (get-char-attribute char key)))
1159           (setq strokes value)
1160           (insert (format "(%s . %S)%s"
1161                           key
1162                           strokes
1163                           line-breaking))
1164           (setq attributes (delq key attributes))
1165           )
1166         (setq key (intern (format "%s@%s" 'total-strokes domain)))
1167         (when (and (memq key attributes)
1168                    (setq value (get-char-attribute char key)))
1169           (insert (format "(%s       . %S)%s"
1170                           key
1171                           value
1172                           line-breaking))
1173           (setq attributes (delq key attributes))
1174           )
1175         (dolist (feature '(ideographic-radical
1176                            ideographic-strokes
1177                            total-strokes))
1178           (setq key (intern (format "%s@%s*sources" feature domain)))
1179           (when (and (memq key attributes)
1180                      (setq value (get-char-attribute char key)))
1181             (insert (format "(%s%s" key line-breaking))
1182             (dolist (cell value)
1183               (insert (format " %s" cell)))
1184             (insert ")")
1185             (insert line-breaking)
1186             (setq attributes (delq key attributes))
1187             ))
1188         ))
1189     (when (and (memq 'ideographic-strokes attributes)
1190                (setq value (get-char-attribute char 'ideographic-strokes)))
1191       (setq strokes value)
1192       (insert (format "(ideographic-strokes . %S)%s"
1193                       strokes
1194                       line-breaking))
1195       (setq attributes (delq 'ideographic-strokes attributes))
1196       )
1197     (when (and (memq 'kangxi-radical attributes)
1198                (setq value (get-char-attribute char 'kangxi-radical)))
1199       (unless (eq value radical)
1200         (insert (format "(kangxi-radical\t . %S)\t; %c%s"
1201                         value
1202                         (ideographic-radical value)
1203                         line-breaking))
1204         (or radical
1205             (setq radical value)))
1206       (setq attributes (delq 'kangxi-radical attributes))
1207       )
1208     (when (and (memq 'kangxi-strokes attributes)
1209                (setq value (get-char-attribute char 'kangxi-strokes)))
1210       (unless (eq value strokes)
1211         (insert (format "(kangxi-strokes\t . %S)%s"
1212                         value
1213                         line-breaking))
1214         (or strokes
1215             (setq strokes value)))
1216       (setq attributes (delq 'kangxi-strokes attributes))
1217       )
1218     ;; (when (and (memq 'japanese-radical attributes)
1219     ;;            (setq value (get-char-attribute char 'japanese-radical)))
1220     ;;   (unless (eq value radical)
1221     ;;     (insert (format "(japanese-radical\t . %S)\t; %c%s"
1222     ;;                     value
1223     ;;                     (ideographic-radical value)
1224     ;;                     line-breaking))
1225     ;;     (or radical
1226     ;;         (setq radical value)))
1227     ;;   (setq attributes (delq 'japanese-radical attributes))
1228     ;;   )
1229     (when (and (memq 'japanese-strokes attributes)
1230                (setq value (get-char-attribute char 'japanese-strokes)))
1231       (unless (eq value strokes)
1232         (insert (format "(japanese-strokes\t . %S)%s"
1233                         value
1234                         line-breaking))
1235         (or strokes
1236             (setq strokes value)))
1237       (setq attributes (delq 'japanese-strokes attributes))
1238       )
1239     (when (and (memq 'cns-radical attributes)
1240                (setq value (get-char-attribute char 'cns-radical)))
1241       (insert (format "(cns-radical\t . %S)\t; %c%s"
1242                       value
1243                       (ideographic-radical value)
1244                       line-breaking))
1245       (setq attributes (delq 'cns-radical attributes))
1246       )
1247     (when (and (memq 'cns-strokes attributes)
1248                (setq value (get-char-attribute char 'cns-strokes)))
1249       (unless (eq value strokes)
1250         (insert (format "(cns-strokes\t . %S)%s"
1251                         value
1252                         line-breaking))
1253         (or strokes
1254             (setq strokes value)))
1255       (setq attributes (delq 'cns-strokes attributes))
1256       )
1257     ;; (when (and (memq 'ideographic- attributes)
1258     ;;            (setq value (get-char-attribute char 'ideographic-)))
1259     ;;   (insert "(ideographic-       ")
1260     ;;   (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1261     ;;         separator nil)
1262     ;;   (while (consp value)
1263     ;;     (setq cell (car value))
1264     ;;     (if (integerp cell)
1265     ;;         (setq cell (decode-char '=ucs cell)))
1266     ;;     (cond ((characterp cell)
1267     ;;            (if separator
1268     ;;                (insert lbs))
1269     ;;            (if readable
1270     ;;                (insert (format "%S" cell))
1271     ;;              (char-db-insert-char-spec cell readable))
1272     ;;            (setq separator lbs))
1273     ;;           ((consp cell)
1274     ;;            (if separator
1275     ;;                (insert lbs))
1276     ;;            (if (consp (car cell))
1277     ;;                (char-db-insert-char-spec cell readable)
1278     ;;              (char-db-insert-char-reference cell readable))
1279     ;;            (setq separator lbs))
1280     ;;           (t
1281     ;;            (if separator
1282     ;;                (insert separator))
1283     ;;            (insert (prin1-to-string cell))
1284     ;;            (setq separator " ")))
1285     ;;     (setq value (cdr value)))
1286     ;;   (insert ")")
1287     ;;   (insert line-breaking)
1288     ;;   (setq attributes (delq 'ideographic- attributes)))
1289     (when (and (memq 'total-strokes attributes)
1290                (setq value (get-char-attribute char 'total-strokes)))
1291       (insert (format "(total-strokes       . %S)%s"
1292                       value
1293                       line-breaking))
1294       (setq attributes (delq 'total-strokes attributes))
1295       )
1296     ;; (when (and (memq '->ideograph attributes)
1297     ;;            (setq value (get-char-attribute char '->ideograph)))
1298     ;;   (insert (format "(->ideograph\t%s)%s"
1299     ;;                   (mapconcat (lambda (code)
1300     ;;                                (cond ((symbolp code)
1301     ;;                                       (symbol-name code))
1302     ;;                                      ((integerp code)
1303     ;;                                       (format "#x%04X" code))
1304     ;;                                      (t
1305     ;;                                       (format "%s %S"
1306     ;;                                               line-breaking code))))
1307     ;;                              value " ")
1308     ;;                   line-breaking))
1309     ;;   (setq attributes (delq '->ideograph attributes))
1310     ;;   )
1311     ;; (when (and (memq '->decomposition attributes)
1312     ;;            (setq value (get-char-attribute char '->decomposition)))
1313     ;;   (insert (format "(->decomposition\t%s)%s"
1314     ;;                   (mapconcat (lambda (code)
1315     ;;                                (cond ((symbolp code)
1316     ;;                                       (symbol-name code))
1317     ;;                                      ((characterp code)
1318     ;;                                       (if readable
1319     ;;                                           (format "%S" code)
1320     ;;                                         (format "#x%04X"
1321     ;;                                                 (char-int code))
1322     ;;                                         ))
1323     ;;                                      ((integerp code)
1324     ;;                                       (format "#x%04X" code))
1325     ;;                                      (t
1326     ;;                                       (format "%s%S" line-breaking code))))
1327     ;;                              value " ")
1328     ;;                   line-breaking))
1329     ;;   (setq attributes (delq '->decomposition attributes))
1330     ;;   )
1331     (if (equal (get-char-attribute char '->titlecase)
1332                (get-char-attribute char '->uppercase))
1333         (setq attributes (delq '->titlecase attributes)))
1334     (when (and (memq '->mojikyo attributes)
1335                (setq value (get-char-attribute char '->mojikyo)))
1336       (insert (format "(->mojikyo\t\t. %06d)\t; %c%s"
1337                       value (decode-char 'mojikyo value)
1338                       line-breaking))
1339       (setq attributes (delq '->mojikyo attributes))
1340       )
1341     (unless readable
1342       (dolist (ignored '(composition
1343                          ->denotational <-subsumptive ->ucs-unified
1344                          ;; ->ideographic-component-forms
1345                          ))
1346         (setq attributes (delq ignored attributes))))
1347     (while attributes
1348       (setq name (car attributes))
1349       (unless (eq (setq value (get-char-attribute char name 'value-is-empty))
1350                   'value-is-empty)
1351         (cond ((setq ret (find-charset name))
1352                (setq name (charset-name ret))
1353                (when (not (memq name dest-ccss))
1354                  (setq dest-ccss (cons name dest-ccss))
1355                  (char-db-insert-ccs-feature name value line-breaking))
1356                )
1357               ((string-match "^=>ucs@" (symbol-name name))
1358                (insert (format "(%-18s . #x%04X)\t; %c%s"
1359                                name value (decode-char '=ucs value)
1360                                line-breaking))
1361                )
1362               ((eq name 'jisx0208-1978/4X)
1363                (insert (format "(%-18s . #x%04X)%s"
1364                                name value
1365                                line-breaking))
1366                )
1367               ((and
1368                 (not readable)
1369                 (not (eq name '->subsumptive))
1370                 (not (eq name '->uppercase))
1371                 (not (eq name '->lowercase))
1372                 (not (eq name '->titlecase))
1373                 (not (eq name '->canonical))
1374                 (not (eq name '->Bopomofo))
1375                 (not (eq name '->mistakable))
1376                 (not (eq name '->ideographic-variants))
1377                 (null (get-char-attribute
1378                        char (intern (format "%s*sources" name))))
1379                 (not (string-match "\\*sources$" (symbol-name name)))
1380                 (null (get-char-attribute
1381                        char (intern (format "%s*note" name))))
1382                 (not (string-match "\\*note$" (symbol-name name)))
1383                 (or (eq name '<-identical)
1384                     (eq name '<-uppercase)
1385                     (eq name '<-lowercase)
1386                     (eq name '<-titlecase)
1387                     (eq name '<-canonical)
1388                     (eq name '<-ideographic-variants)
1389                     ;; (eq name '<-synonyms)
1390                     (string-match "^<-synonyms" (symbol-name name))
1391                     (eq name '<-mistakable)
1392                     (when (string-match "^->" (symbol-name name))
1393                       (cond
1394                        ((string-match "^->fullwidth" (symbol-name name))
1395                         (not (and (consp value)
1396                                   (characterp (car value))
1397                                   (encode-char
1398                                    (car value) '=ucs 'defined-only)))
1399                         )
1400                        (t)))
1401                     ))
1402                )
1403               ((or (eq name 'ideographic-structure)
1404                    (eq name 'ideographic-structure@apparent)
1405                    (eq name 'ideographic-combination)
1406                    ;; (eq name 'ideographic-)
1407                    (eq name '=decomposition)
1408                    (char-feature-base-name= '=decomposition name)
1409                    (char-feature-base-name= '=>decomposition name)
1410                    ;; (string-match "^=>*decomposition\\(@[^*]+\\)?$"
1411                    ;;               (symbol-name name))
1412                    (string-match "^\\(->\\|<-\\)[^*]*$" (symbol-name name))
1413                    (string-match "^\\(->\\|<-\\)[^*]*\\*sources$"
1414                                  (symbol-name name))
1415                    )
1416                (char-db-insert-relation-feature char name value
1417                                                 line-breaking
1418                                                 ccss readable))
1419               ;; ((memq name '(ideograph=
1420               ;;               original-ideograph-of
1421               ;;               ancient-ideograph-of
1422               ;;               vulgar-ideograph-of
1423               ;;               wrong-ideograph-of
1424               ;;               ;; simplified-ideograph-of
1425               ;;               ideographic-variants
1426               ;;               ;; ideographic-different-form-of
1427               ;;               ))
1428               ;;  (insert (format "(%-18s%s " name line-breaking))
1429               ;;  (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1430               ;;        separator nil)
1431               ;;  (while (consp value)
1432               ;;    (setq cell (car value))
1433               ;;    (if (and (consp cell)
1434               ;;             (consp (car cell)))
1435               ;;        (progn
1436               ;;          (if separator
1437               ;;              (insert lbs))
1438               ;;          (char-db-insert-alist cell readable)
1439               ;;          (setq separator lbs))
1440               ;;      (if separator
1441               ;;          (insert separator))
1442               ;;      (insert (prin1-to-string cell))
1443               ;;      (setq separator " "))
1444               ;;    (setq value (cdr value)))
1445               ;;  (insert ")")
1446               ;;  (insert line-breaking))
1447               ((consp value)
1448                (insert (format "(%-18s " name))
1449                (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1450                      separator nil)
1451                (while (consp value)
1452                  (setq cell (car value))
1453                  (if (and (consp cell)
1454                           (consp (car cell))
1455                           (setq ret (condition-case nil
1456                                         (find-char cell)
1457                                       (error nil))))
1458                      (progn
1459                        (setq rest cell
1460                              al nil
1461                              cal nil)
1462                        (while rest
1463                          (setq key (car (car rest)))
1464                          (if (find-charset key)
1465                              (setq cal (cons key cal))
1466                            (setq al (cons key al)))
1467                          (setq rest (cdr rest)))
1468                        (if separator
1469                            (insert lbs))
1470                        (insert-char-attributes ret
1471                                                readable
1472                                                al ; cal
1473                                                nil 'for-sub-node)
1474                        (setq separator lbs))
1475                    (setq ret (prin1-to-string cell))
1476                    (if separator
1477                        (if (< (+ (current-column)
1478                                  (length ret)
1479                                  (length separator))
1480                               76)
1481                            (insert separator)
1482                          (insert lbs)))
1483                    (insert ret)
1484                    (setq separator " "))
1485                  (setq value (cdr value)))
1486                (insert ")")
1487                (insert line-breaking))
1488               (t
1489                (insert (format "(%-18s" name))
1490                (setq ret (prin1-to-string value))
1491                (unless (< (+ (current-column)
1492                              (length ret)
1493                              3)
1494                           76)
1495                  (insert line-breaking))
1496                (insert " . " ret ")" line-breaking)
1497                ;; (insert (format "(%-18s . %S)%s"
1498                ;;                 name value
1499                ;;                 line-breaking))
1500                )
1501               ))
1502       (setq attributes (cdr attributes)))
1503     (insert ")")))
1504
1505 (defun insert-char-data (char &optional readable
1506                               attributes)
1507   (save-restriction
1508     (narrow-to-region (point)(point))
1509     (insert "(define-char
1510   '")
1511     (insert-char-attributes char readable attributes)
1512     (insert ")\n")
1513     (goto-char (point-min))
1514     (while (re-search-forward "[ \t]+$" nil t)
1515       (replace-match ""))
1516     ;; from tabify.
1517     (goto-char (point-min))
1518     (while (re-search-forward "[ \t][ \t][ \t]*" nil t)
1519       (let ((column (current-column))
1520             (indent-tabs-mode t))
1521         (delete-region (match-beginning 0) (point))
1522         (indent-to column)))
1523     (goto-char (point-max))
1524     ;; (tabify (point-min)(point-max))
1525     ))
1526
1527 (defun insert-char-data-with-variant (char &optional printable
1528                                            no-ucs-unified
1529                                            script excluded-script)
1530   (insert-char-data char printable)
1531   (let ((variants (char-variants char))
1532         rest
1533         variant vs ret)
1534     (setq variants (sort variants #'<))
1535     (setq rest variants)
1536     (setq variants (cons char variants))
1537     (while rest
1538       (setq variant (car rest))
1539       (unless (get-char-attribute variant '<-subsumptive)
1540         (if (and (or (null script)
1541                      (null (setq vs (get-char-attribute variant 'script)))
1542                      (memq script vs))
1543                  (or (null excluded-script)
1544                      (null (setq vs (get-char-attribute variant 'script)))
1545                      (not (memq excluded-script vs))))
1546             (unless (and no-ucs-unified (get-char-attribute variant '=ucs))
1547               (insert-char-data variant printable)
1548               (if (setq ret (char-variants variant))
1549                   (while ret
1550                     (or (memq (car ret) variants)
1551                         ;; (get-char-attribute (car ret) '<-subsumptive)
1552                         (setq rest (nconc rest (list (car ret)))))
1553                     (setq ret (cdr ret)))))))
1554       (setq rest (cdr rest)))))
1555
1556 (defun insert-char-range-data (min max &optional script excluded-script)
1557   (let ((code min)
1558         char)
1559     (while (<= code max)
1560       (setq char (decode-char '=ucs code))
1561       (if (encode-char char '=ucs 'defined-only)
1562           (insert-char-data-with-variant char nil 'no-ucs-unified
1563                                          script excluded-script))
1564       (setq code (1+ code)))))
1565
1566 (defun write-char-range-data-to-file (min max file
1567                                           &optional script excluded-script)
1568   (let ((coding-system-for-write char-db-file-coding-system))
1569     (with-temp-buffer
1570       (insert (format ";; -*- coding: %s -*-\n"
1571                       char-db-file-coding-system))
1572       (insert-char-range-data min max script excluded-script)
1573       (write-region (point-min)(point-max) file))))
1574
1575 (defvar what-character-original-window-configuration)
1576
1577 ;;;###autoload
1578 (defun what-char-definition (char)
1579   (interactive (list (char-after)))
1580   (let ((buf (get-buffer-create "*Character Description*"))
1581         (the-buf (current-buffer))
1582         (win-conf (current-window-configuration)))
1583     (pop-to-buffer buf)
1584     (make-local-variable 'what-character-original-window-configuration)
1585     (setq what-character-original-window-configuration win-conf)
1586     (setq buffer-read-only nil)
1587     (erase-buffer)
1588     (condition-case err
1589         (progn
1590           (insert-char-data-with-variant char 'printable)
1591           (unless (char-attribute-alist char)
1592             (insert (format ";; = %c\n"
1593                             (let* ((rest (split-char char))
1594                                    (ccs (pop rest))
1595                                    (code (pop rest)))
1596                               (while rest
1597                                 (setq code (logior (lsh code 8)
1598                                                    (pop rest))))
1599                               (decode-char ccs code)))))
1600           ;; (char-db-update-comment)
1601           (set-buffer-modified-p nil)
1602           (view-mode the-buf (lambda (buf)
1603                                (set-window-configuration
1604                                 what-character-original-window-configuration)
1605                                ))
1606           (goto-char (point-min)))
1607       (error (progn
1608                (set-window-configuration
1609                 what-character-original-window-configuration)
1610                (signal (car err) (cdr err)))))))
1611
1612
1613 ;;; @ end
1614 ;;;
1615
1616 (provide 'char-db-util)
1617
1618 ;;; char-db-util.el ends here