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