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