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