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