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