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