update.
[chise/xemacs-chise.git.1] / lisp / utf-2000 / char-db-util.el
1 ;;; char-db-util.el --- Character Database utility -*- coding: utf-8-er; -*-
2
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;;   2007, 2008, 2009, 2010, 2011, 2012 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    ?鼓 ?豈 ?豆 ?豊 ?豐 ?䖒 ?虍 ?虎 ?虤 ?皿 ; 170
84    ?𠙴 ?去 ?血 ?丶 ?丹 ?青 ?井 ?皀 ?鬯 ?食 ; 180
85    ?亼 ?會 ?倉 ?入 ?缶 ?矢 ?高 ?冂 ?𩫏 ?京 ; 190
86    ?亯 ?𣆪 ?畗 ?㐭 ?嗇 ?來 ?麥 ?夊 ?舛 ?䑞 ; 200
87    ?韋 ?弟 ?夂 ?久 ?桀 ?木 ?東 ?林 ?才 ?叒 ; 210
88    ?之 ?帀 ?出 ?𣎵 ?生 ?乇 ?𠂹 ?𠌶 ?華 ?𥝌 ; 220
89    ?稽 ?巢 ?桼 ?束 ?㯻 ?囗 ?員 ?貝 ?邑 ?𨛜 ; 230
90    ?日 ?旦 ?倝 ?㫃 ?冥 ?晶 ?月 ?有 ?明 ?囧 ; 240
91    ?夕 ?多 ?毌 ?𢎘 ?𣐺 ?卣 ?齊 ?朿 ?片 ?鼎 ; 250
92    ?克 ?彔 ?禾 ?秝 ?黍 ?香 ?米 ?毇 ?臼 ?凶 ; 260
93    ?𣎳 ?林 ?麻 ?尗 ?耑 ?韭 ?瓜 ?瓠 ?宀 ?宮 ; 270
94    ?呂 ?穴 ?㝱 ?𤕫 ?冖 ?𠔼 ?冃 ?㒳 ?网 ?襾 ; 280
95    ?巾 ?巿 ?帛 ?白 ?㡀 ?黹 ?人 ?𠤎 ?匕 ?从 ; 290
96    ])
97
98 (defun shuowen-radical (number)
99   (aref shuowen-radicals (1- number)))
100
101 (defvar char-db-file-coding-system 'utf-8-mcs-er)
102
103 (defvar char-db-ignored-attributes '(ideographic-products))
104
105 (defvar char-db-coded-charset-priority-list
106   '(ascii
107     control-1
108     latin-iso8859-1
109     latin-iso8859-2
110     latin-iso8859-3
111     latin-iso8859-4
112     latin-iso8859-9
113     latin-jisx0201
114     cyrillic-iso8859-5
115     greek-iso8859-7
116     thai-tis620
117     =adobe-japan1-0
118     =adobe-japan1-1
119     =adobe-japan1-2
120     =adobe-japan1-3
121     =adobe-japan1-4
122     =adobe-japan1-5
123     =adobe-japan1-6
124     =jis-x0208
125     =jis-x0208@1978
126     =jis-x0208@1983
127     japanese-jisx0212
128     chinese-gb2312
129     =jis-x0208@1990
130     chinese-cns11643-1
131     chinese-cns11643-2
132     chinese-cns11643-3
133     chinese-cns11643-4
134     chinese-cns11643-5
135     chinese-cns11643-6
136     chinese-cns11643-7
137     =jis-x0213-1
138     =jis-x0213-1@2000
139     =jis-x0213-1@2004
140     =jis-x0213-2
141     korean-ksc5601
142     chinese-isoir165
143     katakana-jisx0201
144     hebrew-iso8859-8
145     chinese-gb12345
146     latin-viscii
147     ethiopic-ucs
148     =big5-cdp
149     =hanyo-denshi/ja
150     =hanyo-denshi/jb
151     =hanyo-denshi/jc
152     =hanyo-denshi/jd
153     =hanyo-denshi/ft
154     =hanyo-denshi/ia
155     =hanyo-denshi/ib
156     =hanyo-denshi/hg
157     =hanyo-denshi/jt
158     =hanyo-denshi/ks
159     =gt
160     =gt-k
161     =daikanwa
162     =daikanwa@rev2
163     =daikanwa@rev1
164     =cbeta
165     ideograph-hanziku-1
166     ideograph-hanziku-2
167     ideograph-hanziku-3
168     ideograph-hanziku-4
169     ideograph-hanziku-5
170     ideograph-hanziku-6
171     ideograph-hanziku-7
172     ideograph-hanziku-8
173     ideograph-hanziku-9
174     ideograph-hanziku-10
175     ideograph-hanziku-11
176     ideograph-hanziku-12
177     =>>>adobe-japan1-0
178     =>>>adobe-japan1-1
179     =>>>adobe-japan1-2
180     =>>>adobe-japan1-3
181     =>>>adobe-japan1-4
182     =>>>adobe-japan1-5
183     =>>>adobe-japan1-6
184     =>>>jis-x0208
185     =>>>jis-x0213-1
186     =>>>jis-x0213-2
187     =>>>gt
188     =>>adobe-japan1-0
189     =>>adobe-japan1-1
190     =>>adobe-japan1-2
191     =>>adobe-japan1-3
192     =>>adobe-japan1-4
193     =>>adobe-japan1-5
194     =>>adobe-japan1-6
195     =>>jis-x0208
196     =>>jis-x0213-1
197     =>>jis-x0213-1@2000
198     =>>jis-x0213-1@2004
199     =>>jis-x0213-2
200     =>>jis-x0208@1978
201     =>>hanyo-denshi/ft
202     =>>hanyo-denshi/jt
203     =>>hanyo-denshi/ks
204     =>>gt
205     =>>daikanwa
206     =+>jis-x0208
207     =+>jis-x0213-1
208     =+>jis-x0213-2
209     =+>jis-x0208@1978
210     =+>adobe-japan1
211     =>jis-x0208
212     =>jis-x0208@1997
213     =>jis-x0213-1
214     =>jis-x0213-1@2000
215     =>jis-x0213-1@2004
216     =>jis-x0213-2
217     ==>ucs@bucs
218     =>ucs@iso
219     =>ucs@unicode
220     =>ucs@jis
221     =>ucs@cns
222     =>ucs@ks
223     =+>ucs@iso
224     =+>ucs@unicode
225     =+>ucs@jis
226     =+>ucs@jis/1990
227     =+>ucs@cns
228     =+>ucs@ks
229     =>>ucs@iso
230     =>>ucs@unicode
231     =>>ucs@jis
232     =>>ucs@cns
233     =>>>ucs@iso
234     =>>>ucs@unicode
235     =ucs@iso
236     =ucs@unicode
237     =>>big5-cdp
238     =>>gt-k
239     =+>gt
240     =>gt
241     =>big5-cdp
242     =>daikanwa
243     =>daikanwa/ho
244     =>cns11643-7
245     =big5
246     =big5-eten
247     =>gt-k
248     =zinbun-oracle
249     =>zinbun-oracle
250     =ruimoku-v6
251     =>>ruimoku-v6
252     =jef-china3
253     =shinjigen))
254
255
256 ;;; @ char-db formatters
257 ;;;
258
259 (defun char-db-make-char-spec (char)
260   (let (ret char-spec)
261     (cond ((characterp char)
262            (cond ((and (setq ret (encode-char char '=ucs 'defined-only))
263                        (not (and (<= #xE000 ret)(<= ret #xF8FF))))
264                   (setq char-spec (list (cons '=ucs ret)))
265                   (cond ((setq ret (get-char-attribute char 'name))
266                          (setq char-spec (cons (cons 'name ret) char-spec))
267                          )
268                         ((setq ret (get-char-attribute char 'name*))
269                          (setq char-spec (cons (cons 'name* ret) char-spec))
270                          ))
271                   )
272                  ((encode-char char '=adobe-japan1 'defined-only)
273                   (setq char-spec nil)
274                   (dolist (ccs (charset-list))
275                     (if (and (or (memq ccs
276                                        '(=adobe-japan1-0
277                                          =adobe-japan1-1
278                                          =adobe-japan1-2
279                                          =adobe-japan1-3
280                                          =adobe-japan1-4
281                                          =adobe-japan1-5
282                                          =adobe-japan1-6
283                                          ))
284                                  ;; (eq (charset-property ccs 'iso-ir) 177)
285                                  (string-match "=ucs@" (symbol-name ccs))
286                                  )
287                              (setq ccs (charset-name ccs))
288                              (null (assq ccs char-spec))
289                              (setq ret (encode-char char ccs 'defined-only)))
290                         (setq char-spec (cons (cons ccs ret) char-spec))))
291                   )
292                  ((setq ret
293                         (catch 'tag
294                           (let ((rest char-db-coded-charset-priority-list)
295                                 ccs)
296                             (while rest
297                               (setq ccs (charset-name
298                                          (find-charset (car rest))))
299                               (if (setq ret
300                                         (encode-char char ccs
301                                                      'defined-only))
302                                   (throw 'tag (cons ccs ret)))
303                               (setq rest (cdr rest))))))
304                   (setq char-spec (list ret))
305                   (dolist (ccs (delq (car ret) (charset-list)))
306                     (if (and (or (charset-iso-final-char ccs)
307                                  (memq ccs
308                                        '(=daikanwa
309                                          =daikanwa@rev2
310                                          ;; =gt-k
311                                          =jis-x0208@1997
312                                          ))
313                                  (eq (charset-property ccs 'iso-ir) 177)
314                                  ;; (string-match "=ucs@" (symbol-name ccs))
315                                  )
316                              (setq ccs (charset-name ccs))
317                              (null (assq ccs char-spec))
318                              (setq ret (encode-char char ccs 'defined-only)))
319                         (setq char-spec (cons (cons ccs ret) char-spec))))
320                   (if (null char-spec)
321                       (setq char-spec (split-char char)))
322                   (cond ((setq ret (get-char-attribute char 'name))
323                          (setq char-spec (cons (cons 'name ret) char-spec))
324                          )
325                         ((setq ret (get-char-attribute char 'name*))
326                          (setq char-spec (cons (cons 'name* ret) char-spec))
327                          ))
328                   )
329                  ((setq ret (get-char-attribute
330                              char 'ideographic-combination))
331                   (setq char-spec
332                         (cons (cons 'ideographic-combination ret)
333                               char-spec))
334                   ))
335            char-spec)
336           ((consp char)
337            char))))
338     
339 (defun char-db-insert-char-spec (char &optional readable column
340                                       required-features)
341   (unless column
342     (setq column (current-column)))
343   (let (char-spec temp-char)
344     (setq char-spec (char-db-make-char-spec char))
345     (unless (or (characterp char) ; char
346                 (condition-case nil
347                     (setq char (find-char char-spec))
348                   (error nil)))
349       ;; define temporary character
350       ;;   Current implementation is dirty.
351       (setq temp-char (define-char (cons '(ideograph-daikanwa . 0)
352                                          char-spec)))
353       (remove-char-attribute temp-char 'ideograph-daikanwa)
354       (setq char temp-char))
355     (insert-char-attributes char
356                             readable
357                             (union (mapcar #'car char-spec)
358                                    required-features)
359                             nil 'for-sub-node)
360     (when temp-char
361       ;; undefine temporary character
362       ;;   Current implementation is dirty.
363       (setq char-spec (char-attribute-alist temp-char))
364       (while char-spec
365         (remove-char-attribute temp-char (car (car char-spec)))
366         (setq char-spec (cdr char-spec))))))
367
368 (defun char-db-insert-alist (alist &optional readable column)
369   (unless column
370     (setq column (current-column)))
371   (let ((line-breaking
372          (concat "\n" (make-string (1+ column) ?\ )))
373         name value
374         ret al ; cal
375         key
376         lbs cell rest separator)
377     (insert "(")
378     (while alist
379       (setq name (car (car alist))
380             value (cdr (car alist)))
381       (cond ((eq name 'char)
382              (insert "(char . ")
383              (if (setq ret (condition-case nil
384                                (find-char value)
385                              (error nil)))
386                  (progn
387                    (setq al nil
388                          ;; cal nil
389                          )
390                    (while value
391                      (setq key (car (car value)))
392                      ;; (if (find-charset key)
393                      ;;     (setq cal (cons key cal))
394                      (setq al (cons key al))
395                      ;; )
396                      (setq value (cdr value)))
397                    (insert-char-attributes ret
398                                            readable
399                                            (or al 'none) ; cal
400                                            nil 'for-sub-node))
401                (insert (prin1-to-string value)))
402              (insert ")")
403              (insert line-breaking))
404             ((consp value)
405              (insert (format "(%-18s " name))
406              (setq lbs (concat "\n" (make-string (current-column) ?\ )))
407              (while (consp value)
408                (setq cell (car value))
409                (if (and (consp cell)
410                         (consp (car cell))
411                         (setq ret (condition-case nil
412                                       (find-char cell)
413                                     (error nil)))
414                         )
415                    (progn
416                      (setq rest cell
417                            al nil
418                            ;; cal nil
419                            )
420                      (while rest
421                        (setq key (car (car rest)))
422                        ;; (if (find-charset key)
423                        ;;     (setq cal (cons key cal))
424                        (setq al (cons key al))
425                        ;; )
426                        (setq rest (cdr rest)))
427                      (if separator
428                          (insert lbs))
429                      (insert-char-attributes ret
430                                              readable
431                                              al ; cal
432                                              nil 'for-sub-node)
433                      (setq separator lbs))
434                  (if separator
435                      (insert separator))
436                  (insert (prin1-to-string cell))
437                  (setq separator " "))
438                (setq value (cdr value)))
439              (insert ")")
440              (insert line-breaking))
441             (t
442              (insert (format "(%-18s . %S)%s"
443                              name value
444                              line-breaking))))
445       (setq alist (cdr alist))))
446   (insert ")"))
447
448 (defun char-db-insert-char-reference (plist &optional readable column)
449   (unless column
450     (setq column (current-column)))
451   (let ((line-breaking
452          (concat "\n" (make-string (1+ column) ?\ )))
453         (separator "")
454         name value)
455     (insert "(")
456     (while plist
457       (setq name (pop plist))
458       (setq value (pop plist))
459       (cond ((eq name :char)
460              (insert separator)
461              (insert ":char\t")
462              (cond ((numberp value)
463                     (setq value (decode-char '=ucs value)))
464                    ;; ((consp value)
465                    ;;  (setq value (or (find-char value)
466                    ;;                  value)))
467                    )
468              (char-db-insert-char-spec value readable)
469              (insert line-breaking)
470              (setq separator ""))
471             ((eq name :radical)
472              (insert (format "%s%s\t%d ; %c%s"
473                              separator
474                              name value
475                              (ideographic-radical value)
476                              line-breaking))
477              (setq separator ""))
478             (t
479              (insert (format "%s%s\t%S" separator name value))
480              (setq separator line-breaking)))
481       ))
482   (insert ")"))
483
484 (defun char-db-decode-isolated-char (ccs code-point)
485   (let (ret)
486     (setq ret
487           (cond ((eq ccs 'arabic-iso8859-6)
488                  (decode-char ccs code-point))
489                 ((and (memq ccs '(=gt-pj-1
490                                   =gt-pj-2
491                                   =gt-pj-3
492                                   =gt-pj-4
493                                   =gt-pj-5
494                                   =gt-pj-6
495                                   =gt-pj-7
496                                   =gt-pj-8
497                                   =gt-pj-9
498                                   =gt-pj-10
499                                   =gt-pj-11))
500                       (setq ret (decode-char ccs code-point))
501                       (setq ret (encode-char ret '=gt 'defined-only)))
502                  (decode-builtin-char '=gt ret))
503                 (t
504                  (decode-builtin-char ccs code-point))))
505     (cond ((and (<= 0 (char-int ret))
506                 (<= (char-int ret) #x1F))
507            (decode-char '=ucs (+ #x2400 (char-int ret))))
508           ((= (char-int ret) #x7F)
509            ?\u2421)
510           (t ret))))
511
512 (defvar char-db-convert-obsolete-format t)
513
514 (defun char-db-insert-ccs-feature (name value line-breaking)
515   (cond
516    ((integerp value)
517     (insert
518      (format
519       (cond
520        ((memq name '(=shinjigen
521                      =shinjigen@1ed
522                      =shinjigen@rev =shinjigen/+p@rev
523                      =daikanwa/ho =>>daikanwa/ho =>daikanwa/ho))
524         "(%-18s .  %04d)\t; %c")
525        ((eq name '=shinjigen@1ed/24pr)
526         "(%-18s . %04d)\t; %c")
527        ((or (memq name '(=daikanwa =>>daikanwa =>daikanwa
528                          =daikanwa@rev1 =daikanwa@rev2
529                          =daikanwa/+p =>>daikanwa/+p
530                          =daikanwa/+2p =>>daikanwa/+2p
531                          =gt =>>>gt =>>gt =+>gt =>gt
532                          =gt-k =>>gt-k =>gt-k
533                          =adobe-japan1-0 =>>>adobe-japan1-0 =>>adobe-japan1-0
534                          =adobe-japan1-1 =>>>adobe-japan1-1 =>>adobe-japan1-1
535                          =adobe-japan1-2 =>>>adobe-japan1-2 =>>adobe-japan1-2
536                          =adobe-japan1-3 =>>>adobe-japan1-3 =>>adobe-japan1-3
537                          =adobe-japan1-4 =>>>adobe-japan1-4 =>>adobe-japan1-4
538                          =adobe-japan1-5 =>>>adobe-japan1-5 =>>adobe-japan1-5
539                          =adobe-japan1-6 =>>>adobe-japan1-6 =>>adobe-japan1-6
540                          =+>adobe-japan1
541                          =cbeta =>>cbeta
542                          =zinbun-oracle =>zinbun-oracle))
543             ;; (string-match "^=adobe-" (symbol-name name))
544             )
545         "(%-18s . %05d)\t; %c")
546        ((memq name '(=hanyo-denshi/ks =>>>hanyo-denshi/ks =>>hanyo-denshi/ks mojikyo))
547         "(%-18s . %06d)\t; %c")
548        ((>= (charset-dimension name) 2)
549         "(%-18s . #x%04X)\t; %c")
550        (t
551         "(%-18s . #x%02X)\t; %c"))
552       name
553       (if (= (charset-iso-graphic-plane name) 1)
554           (logior value
555                   (cond ((= (charset-dimension name) 1)
556                          #x80)
557                         ((= (charset-dimension name) 2)
558                          #x8080)
559                         ((= (charset-dimension name) 3)
560                          #x808080)
561                         (t 0)))
562         value)
563       (char-db-decode-isolated-char name value)))
564     (if (and (= (charset-chars name) 94)
565              (= (charset-dimension name) 2))
566         (insert (format " [%02d-%02d]"
567                         (- (lsh value -8) 32)
568                         (- (logand value 255) 32))))
569     )
570    (t
571     (insert (format "(%-18s . %s)" name value))
572     ))
573   (insert line-breaking))
574
575 (defun char-db-insert-relation-feature (char name value line-breaking
576                                              ccss readable)
577   (insert (format "(%-18s%s " name line-breaking))
578   (let ((lbs (concat "\n" (make-string (current-column) ?\ )))
579         separator cell sources required-features
580         ret)
581     (while (consp value)
582       (setq cell (car value))
583       (if (integerp cell)
584           (setq cell (decode-char '=ucs cell)))
585       (cond
586        ((eq name '->subsumptive)
587         (when (or (not (some (lambda (atr)
588                                (get-char-attribute cell atr))
589                              char-db-ignored-attributes))
590                   (some (lambda (ccs)
591                           (encode-char cell ccs 'defined-only))
592                         ccss))
593           (if separator
594               (insert lbs))
595           (let ((char-db-ignored-attributes
596                  (cons '<-subsumptive
597                        char-db-ignored-attributes)))
598             (insert-char-attributes cell readable nil nil 'for-sub-node))
599           (setq separator lbs))
600         )
601        ((characterp cell)
602         (setq sources
603               (get-char-attribute
604                char (intern (format "%s*sources" name))))
605         (setq required-features nil)
606         (dolist (source sources)
607           (cond
608            ((memq source '(JP
609                            JP/Jouyou
610                            shinjigen shinjigen@1ed shinjigen@rev))
611             (setq required-features
612                   (union required-features
613                          '(=jis-x0208
614                            =jis-x0208@1990
615                            =jis-x0213-1@2000
616                            =jis-x0213-1@2004
617                            =jis-x0213-2
618                            =jis-x0212
619                            =jis-x0208@1983
620                            =jis-x0208@1978
621                            =shinjigen))))
622            ((eq source 'CN)
623             (setq required-features
624                   (union required-features
625                          '(=gb2312
626                            =gb12345
627                            =iso-ir165)))))
628           (cond
629            ((find-charset (setq ret (intern (format "=%s" source))))
630             (setq required-features
631                   (cons ret required-features)))
632            (t (setq required-features
633                     (cons source required-features)))))
634         (cond ((string-match "@JP" (symbol-name name))
635                (setq required-features
636                      (union required-features
637                             '(=jis-x0208
638                               =jis-x0208@1990
639                               =jis-x0213-1-2000
640                               =jis-x0213-2-2000
641                               =jis-x0212
642                               =jis-x0208@1983
643                               =jis-x0208@1978))))
644               ((string-match "@CN" (symbol-name name))
645                (setq required-features
646                      (union required-features
647                             '(=gb2312
648                               =gb12345
649                               =iso-ir165)))))
650         (if separator
651             (insert lbs))
652         (if readable
653             (insert (format "%S" cell))
654           (char-db-insert-char-spec cell readable
655                                     nil
656                                     required-features))
657         (setq separator lbs))
658        ((consp cell)
659         (if separator
660             (insert lbs))
661         (if (consp (car cell))
662             (char-db-insert-char-spec cell readable)
663           (char-db-insert-char-reference cell readable))
664         (setq separator lbs))
665        (t
666         (if separator
667             (insert separator))
668         (insert (prin1-to-string cell))
669         (setq separator " ")))
670       (setq value (cdr value)))
671     (insert ")")
672     (insert line-breaking)))
673
674 (defun insert-char-attributes (char &optional readable attributes column
675                                     for-sub-node)
676   (unless column
677     (setq column (current-column)))
678   (let (name value ; has-long-ccs-name
679         rest
680         radical strokes
681         (line-breaking
682          (concat "\n" (make-string (1+ column) ?\ )))
683         lbs cell separator ret
684         key al cal
685         dest-ccss ; sources required-features
686         ccss)
687     (let (atr-d)
688       (setq attributes
689             (sort (if attributes
690                       (if (consp attributes)
691                           (progn
692                             (dolist (name attributes)
693                               (unless (memq name char-db-ignored-attributes)
694                                 (if (find-charset name)
695                                     (push name ccss))
696                                 (push name atr-d)))
697                             atr-d))
698                     (dolist (name (char-attribute-list))
699                       (unless (memq name char-db-ignored-attributes)
700                         (if (find-charset name)
701                             (push name ccss))
702                         (push name atr-d)))
703                     atr-d)
704                   #'char-attribute-name<)))
705     (insert "(")
706     (when (memq '<-subsumptive attributes)
707       (when (or readable (not for-sub-node))
708         (when (setq value (get-char-attribute char '<-subsumptive))
709           (char-db-insert-relation-feature char '<-subsumptive value
710                                            line-breaking
711                                            ccss readable)))
712       (setq attributes (delq '<-subsumptive attributes)))
713     (when (and (memq '<-denotational attributes)
714                (setq value (get-char-attribute char '<-denotational)))
715       (char-db-insert-relation-feature char '<-denotational value
716                                        line-breaking
717                                        ccss readable)
718       (setq attributes (delq '<-denotational attributes)))
719     (when (and (memq 'name attributes)
720                (setq value (get-char-attribute char 'name)))
721       (insert (format
722                (if (> (+ (current-column) (length value)) 48)
723                    "(name . %S)%s"
724                  "(name               . %S)%s")
725                value line-breaking))
726       (setq attributes (delq 'name attributes))
727       )
728     (when (and (memq 'name* attributes)
729                (setq value (get-char-attribute char 'name*)))
730       (insert (format
731                (if (> (+ (current-column) (length value)) 48)
732                    "(name* . %S)%s"
733                  "(name*              . %S)%s")
734                value line-breaking))
735       (setq attributes (delq 'name* attributes))
736       )
737     (when (and (memq 'script attributes)
738                (setq value (get-char-attribute char 'script)))
739       (insert (format "(script\t\t%s)%s"
740                       (mapconcat (function prin1-to-string)
741                                  value " ")
742                       line-breaking))
743       (setq attributes (delq 'script attributes))
744       )
745     (dolist (name '(=>ucs =>ucs*))
746       (when (and (memq name attributes)
747                  (setq value (get-char-attribute char name)))
748         (insert (format "(%-18s . #x%04X)\t; %c%s"
749                         name value (decode-char '=ucs value)
750                         line-breaking))
751         (setq attributes (delq name attributes))))
752     (dolist (name '(=>ucs@gb =>ucs@big5))
753       (when (and (memq name attributes)
754                  (setq value (get-char-attribute char name)))
755         (insert (format "(%-18s . #x%04X)\t; %c%s"
756                         name value
757                         (decode-char (intern
758                                       (concat "="
759                                               (substring
760                                                (symbol-name name) 2)))
761                                      value)
762                         line-breaking))
763         (setq attributes (delq name attributes))
764         ))
765     ;; (dolist (name '(=>daikanwa))
766     ;;   (when (and (memq name attributes)
767     ;;              (setq value (get-char-attribute char name)))
768     ;;     (insert
769     ;;      (if (integerp value)
770     ;;          (format "(%-18s . %05d)\t; %c%s"
771     ;;                  name value (decode-char '=daikanwa value)
772     ;;                  line-breaking)
773     ;;        (format "(%-18s %s)\t; %c%s"
774     ;;                name
775     ;;                (mapconcat (function prin1-to-string)
776     ;;                           value " ")
777     ;;                (char-representative-of-daikanwa char)
778     ;;                line-breaking)))
779     ;;     (setq attributes (delq name attributes))))
780     (when (and (memq 'general-category attributes)
781                (setq value (get-char-attribute char 'general-category)))
782       (insert (format
783                "(general-category\t%s) ; %s%s"
784                (mapconcat (lambda (cell)
785                             (format "%S" cell))
786                           value " ")
787                (cond ((rassoc value unidata-normative-category-alist)
788                       "Normative Category")
789                      ((rassoc value unidata-informative-category-alist)
790                       "Informative Category")
791                      (t
792                       "Unknown Category"))
793                line-breaking))
794       (setq attributes (delq 'general-category attributes))
795       )
796     (when (and (memq 'bidi-category attributes)
797                (setq value (get-char-attribute char 'bidi-category)))
798       (insert (format "(bidi-category\t. %S)%s"
799                       value
800                       line-breaking))
801       (setq attributes (delq 'bidi-category attributes))
802       )
803     (unless (or (not (memq 'mirrored attributes))
804                 (eq (setq value (get-char-attribute char 'mirrored 'empty))
805                     'empty))
806       (insert (format "(mirrored\t\t. %S)%s"
807                       value
808                       line-breaking))
809       (setq attributes (delq 'mirrored attributes))
810       )
811     (cond
812      ((and (memq 'decimal-digit-value attributes)
813            (setq value (get-char-attribute char 'decimal-digit-value)))
814       (insert (format "(decimal-digit-value . %S)%s"
815                       value
816                       line-breaking))
817       (setq attributes (delq 'decimal-digit-value attributes))
818       (when (and (memq 'digit-value attributes)
819                  (setq value (get-char-attribute char 'digit-value)))
820         (insert (format "(digit-value\t . %S)%s"
821                         value
822                         line-breaking))
823         (setq attributes (delq 'digit-value attributes))
824         )
825       (when (and (memq 'numeric-value attributes)
826                  (setq value (get-char-attribute char 'numeric-value)))
827         (insert (format "(numeric-value\t . %S)%s"
828                         value
829                         line-breaking))
830         (setq attributes (delq 'numeric-value attributes))
831         )
832       )
833      (t
834       (when (and (memq 'digit-value attributes)
835                  (setq value (get-char-attribute char 'digit-value)))
836         (insert (format "(digit-value\t. %S)%s"
837                         value
838                         line-breaking))
839         (setq attributes (delq 'digit-value attributes))
840         )
841       (when (and (memq 'numeric-value attributes)
842                  (setq value (get-char-attribute char 'numeric-value)))
843         (insert (format "(numeric-value\t. %S)%s"
844                         value
845                         line-breaking))
846         (setq attributes (delq 'numeric-value attributes))
847         )))
848     (when (and (memq 'iso-10646-comment attributes)
849                (setq value (get-char-attribute char 'iso-10646-comment)))
850       (insert (format "(iso-10646-comment\t. %S)%s"
851                       value
852                       line-breaking))
853       (setq attributes (delq 'iso-10646-comment attributes))
854       )
855     (when (and (memq 'morohashi-daikanwa attributes)
856                (setq value (get-char-attribute char 'morohashi-daikanwa)))
857       (insert (format "(morohashi-daikanwa\t%s)%s"
858                       (mapconcat (function prin1-to-string) value " ")
859                       line-breaking))
860       (setq attributes (delq 'morohashi-daikanwa attributes))
861       )
862     (setq radical nil
863           strokes nil)
864     (when (and (memq 'ideographic-radical attributes)
865                (setq value (get-char-attribute char 'ideographic-radical)))
866       (setq radical value)
867       (insert (format "(ideographic-radical . %S)\t; %c%s"
868                       radical
869                       (ideographic-radical radical)
870                       line-breaking))
871       (setq attributes (delq 'ideographic-radical attributes))
872       )
873     (when (and (memq 'shuowen-radical attributes)
874                (setq value (get-char-attribute char 'shuowen-radical)))
875       (insert (format "(shuowen-radical\t. %S)\t; %c%s"
876                       value
877                       (shuowen-radical value)
878                       line-breaking))
879       (setq attributes (delq 'shuowen-radical attributes))
880       )
881     (let (key)
882       (dolist (domain
883                (append
884                 char-db-feature-domains
885                 (let (dest domain)
886                   (dolist (feature (char-attribute-list))
887                     (setq feature (symbol-name feature))
888                     (when (string-match
889                            "\\(radical\\|strokes\\)@\\([^@*]+\\)\\(\\*\\|$\\)"
890                            feature)
891                       (setq domain (intern (match-string 2 feature)))
892                      (unless (memq domain dest)
893                        (setq dest (cons domain dest)))))
894                   (sort dest #'string<))))
895         (setq key (intern (format "%s@%s" 'ideographic-radical domain)))
896         (when (and (memq key attributes)
897                    (setq value (get-char-attribute char key)))
898           (setq radical value)
899           (insert (format "(%s . %S)\t; %c%s"
900                           key
901                           radical
902                           (ideographic-radical radical)
903                           line-breaking))
904           (setq attributes (delq key attributes))
905           )
906         (setq key (intern (format "%s@%s" 'ideographic-strokes domain)))
907         (when (and (memq key attributes)
908                    (setq value (get-char-attribute char key)))
909           (setq strokes value)
910           (insert (format "(%s . %S)%s"
911                           key
912                           strokes
913                           line-breaking))
914           (setq attributes (delq key attributes))
915           )
916         (setq key (intern (format "%s@%s" 'total-strokes domain)))
917         (when (and (memq key attributes)
918                    (setq value (get-char-attribute char key)))
919           (insert (format "(%s       . %S)%s"
920                           key
921                           value
922                           line-breaking))
923           (setq attributes (delq key attributes))
924           )
925         (dolist (feature '(ideographic-radical
926                            ideographic-strokes
927                            total-strokes))
928           (setq key (intern (format "%s@%s*sources" feature domain)))
929           (when (and (memq key attributes)
930                      (setq value (get-char-attribute char key)))
931             (insert (format "(%s%s" key line-breaking))
932             (dolist (cell value)
933               (insert (format " %s" cell)))
934             (insert ")")
935             (insert line-breaking)
936             (setq attributes (delq key attributes))
937             ))
938         ))
939     (when (and (memq 'ideographic-strokes attributes)
940                (setq value (get-char-attribute char 'ideographic-strokes)))
941       (setq strokes value)
942       (insert (format "(ideographic-strokes . %S)%s"
943                       strokes
944                       line-breaking))
945       (setq attributes (delq 'ideographic-strokes attributes))
946       )
947     (when (and (memq 'kangxi-radical attributes)
948                (setq value (get-char-attribute char 'kangxi-radical)))
949       (unless (eq value radical)
950         (insert (format "(kangxi-radical\t . %S)\t; %c%s"
951                         value
952                         (ideographic-radical value)
953                         line-breaking))
954         (or radical
955             (setq radical value)))
956       (setq attributes (delq 'kangxi-radical attributes))
957       )
958     (when (and (memq 'kangxi-strokes attributes)
959                (setq value (get-char-attribute char 'kangxi-strokes)))
960       (unless (eq value strokes)
961         (insert (format "(kangxi-strokes\t . %S)%s"
962                         value
963                         line-breaking))
964         (or strokes
965             (setq strokes value)))
966       (setq attributes (delq 'kangxi-strokes attributes))
967       )
968     (when (and (memq 'japanese-radical attributes)
969                (setq value (get-char-attribute char 'japanese-radical)))
970       (unless (eq value radical)
971         (insert (format "(japanese-radical\t . %S)\t; %c%s"
972                         value
973                         (ideographic-radical value)
974                         line-breaking))
975         (or radical
976             (setq radical value)))
977       (setq attributes (delq 'japanese-radical attributes))
978       )
979     (when (and (memq 'japanese-strokes attributes)
980                (setq value (get-char-attribute char 'japanese-strokes)))
981       (unless (eq value strokes)
982         (insert (format "(japanese-strokes\t . %S)%s"
983                         value
984                         line-breaking))
985         (or strokes
986             (setq strokes value)))
987       (setq attributes (delq 'japanese-strokes attributes))
988       )
989     (when (and (memq 'cns-radical attributes)
990                (setq value (get-char-attribute char 'cns-radical)))
991       (insert (format "(cns-radical\t . %S)\t; %c%s"
992                       value
993                       (ideographic-radical value)
994                       line-breaking))
995       (setq attributes (delq 'cns-radical attributes))
996       )
997     (when (and (memq 'cns-strokes attributes)
998                (setq value (get-char-attribute char 'cns-strokes)))
999       (unless (eq value strokes)
1000         (insert (format "(cns-strokes\t . %S)%s"
1001                         value
1002                         line-breaking))
1003         (or strokes
1004             (setq strokes value)))
1005       (setq attributes (delq 'cns-strokes attributes))
1006       )
1007     (when (and (memq 'shinjigen-1-radical attributes)
1008                (setq value (get-char-attribute char 'shinjigen-1-radical)))
1009       (unless (eq value radical)
1010         (insert (format "(shinjigen-1-radical . %S)\t; %c%s"
1011                         value
1012                         (ideographic-radical value)
1013                         line-breaking))
1014         (or radical
1015             (setq radical value)))
1016       (setq attributes (delq 'shinjigen-1-radical attributes))
1017       )
1018     (when (and (memq 'ideographic- attributes)
1019                (setq value (get-char-attribute char 'ideographic-)))
1020       (insert "(ideographic-       ")
1021       (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1022             separator nil)
1023       (while (consp value)
1024         (setq cell (car value))
1025         (if (integerp cell)
1026             (setq cell (decode-char '=ucs cell)))
1027         (cond ((characterp cell)
1028                (if separator
1029                    (insert lbs))
1030                (if readable
1031                    (insert (format "%S" cell))
1032                  (char-db-insert-char-spec cell readable))
1033                (setq separator lbs))
1034               ((consp cell)
1035                (if separator
1036                    (insert lbs))
1037                (if (consp (car cell))
1038                    (char-db-insert-char-spec cell readable)
1039                  (char-db-insert-char-reference cell readable))
1040                (setq separator lbs))
1041               (t
1042                (if separator
1043                    (insert separator))
1044                (insert (prin1-to-string cell))
1045                (setq separator " ")))
1046         (setq value (cdr value)))
1047       (insert ")")
1048       (insert line-breaking)
1049       (setq attributes (delq 'ideographic- attributes)))
1050     (when (and (memq 'total-strokes attributes)
1051                (setq value (get-char-attribute char 'total-strokes)))
1052       (insert (format "(total-strokes       . %S)%s"
1053                       value
1054                       line-breaking))
1055       (setq attributes (delq 'total-strokes attributes))
1056       )
1057     (when (and (memq '->ideograph attributes)
1058                (setq value (get-char-attribute char '->ideograph)))
1059       (insert (format "(->ideograph\t%s)%s"
1060                       (mapconcat (lambda (code)
1061                                    (cond ((symbolp code)
1062                                           (symbol-name code))
1063                                          ((integerp code)
1064                                           (format "#x%04X" code))
1065                                          (t
1066                                           (format "%s %S"
1067                                                   line-breaking code))))
1068                                  value " ")
1069                       line-breaking))
1070       (setq attributes (delq '->ideograph attributes))
1071       )
1072     ;; (when (and (memq '->decomposition attributes)
1073     ;;            (setq value (get-char-attribute char '->decomposition)))
1074     ;;   (insert (format "(->decomposition\t%s)%s"
1075     ;;                   (mapconcat (lambda (code)
1076     ;;                                (cond ((symbolp code)
1077     ;;                                       (symbol-name code))
1078     ;;                                      ((characterp code)
1079     ;;                                       (if readable
1080     ;;                                           (format "%S" code)
1081     ;;                                         (format "#x%04X"
1082     ;;                                                 (char-int code))
1083     ;;                                         ))
1084     ;;                                      ((integerp code)
1085     ;;                                       (format "#x%04X" code))
1086     ;;                                      (t
1087     ;;                                       (format "%s%S" line-breaking code))))
1088     ;;                              value " ")
1089     ;;                   line-breaking))
1090     ;;   (setq attributes (delq '->decomposition attributes))
1091     ;;   )
1092     (if (equal (get-char-attribute char '->titlecase)
1093                (get-char-attribute char '->uppercase))
1094         (setq attributes (delq '->titlecase attributes)))
1095     (when (and (memq '->mojikyo attributes)
1096                (setq value (get-char-attribute char '->mojikyo)))
1097       (insert (format "(->mojikyo\t\t. %06d)\t; %c%s"
1098                       value (decode-char 'mojikyo value)
1099                       line-breaking))
1100       (setq attributes (delq '->mojikyo attributes))
1101       )
1102     (when (and (memq 'hanyu-dazidian-vol attributes)
1103                (setq value (get-char-attribute char 'hanyu-dazidian-vol)))
1104       (insert (format "(hanyu-dazidian-vol  . %d)%s"
1105                       value line-breaking))
1106       (setq attributes (delq 'hanyu-dazidian-vol attributes))
1107       )
1108     (when (and (memq 'hanyu-dazidian-page attributes)
1109                (setq value (get-char-attribute char 'hanyu-dazidian-page)))
1110       (insert (format "(hanyu-dazidian-page . %d)%s"
1111                       value line-breaking))
1112       (setq attributes (delq 'hanyu-dazidian-page attributes))
1113       )
1114     (when (and (memq 'hanyu-dazidian-char attributes)
1115                (setq value (get-char-attribute char 'hanyu-dazidian-char)))
1116       (insert (format "(hanyu-dazidian-char . %d)%s"
1117                       value line-breaking))
1118       (setq attributes (delq 'hanyu-dazidian-char attributes))
1119       )
1120     (unless readable
1121       (dolist (ignored '(composition
1122                          ->denotational <-subsumptive ->ucs-unified
1123                          ->ideographic-component-forms))
1124         (setq attributes (delq ignored attributes))))
1125     (while attributes
1126       (setq name (car attributes))
1127       (unless (eq (setq value (get-char-attribute char name 'value-is-empty))
1128                   'value-is-empty)
1129         (cond ((setq ret (find-charset name))
1130                (setq name (charset-name ret))
1131                (when (not (memq name dest-ccss))
1132                  (setq dest-ccss (cons name dest-ccss))
1133                  (char-db-insert-ccs-feature name value line-breaking))
1134                )
1135               ((string-match "^=>ucs@" (symbol-name name))
1136                (insert (format "(%-18s . #x%04X)\t; %c%s"
1137                                name value (decode-char '=ucs value)
1138                                line-breaking))
1139                )
1140               ((eq name 'jisx0208-1978/4X)
1141                (insert (format "(%-18s . #x%04X)%s"
1142                                name value
1143                                line-breaking))
1144                )
1145               ((and
1146                 (not readable)
1147                 (not (eq name '->subsumptive))
1148                 (not (eq name '->uppercase))
1149                 (not (eq name '->lowercase))
1150                 (not (eq name '->titlecase))
1151                 (not (eq name '->canonical))
1152                 (not (eq name '->Bopomofo))
1153                 (not (eq name '->mistakable))
1154                 (not (eq name '->ideographic-variants))
1155                 (null (get-char-attribute
1156                        char (intern (format "%s*sources" name))))
1157                 (not (string-match "\\*sources$" (symbol-name name)))
1158                 (null (get-char-attribute
1159                        char (intern (format "%s*note" name))))
1160                 (not (string-match "\\*note$" (symbol-name name)))
1161                 (or (eq name '<-identical)
1162                     (eq name '<-uppercase)
1163                     (eq name '<-lowercase)
1164                     (eq name '<-titlecase)
1165                     (eq name '<-canonical)
1166                     (eq name '<-ideographic-variants)
1167                     ;; (eq name '<-synonyms)
1168                     (string-match "^<-synonyms" (symbol-name name))
1169                     (eq name '<-mistakable)
1170                     (when (string-match "^->" (symbol-name name))
1171                       (cond
1172                        ((string-match "^->fullwidth" (symbol-name name))
1173                         (not (and (consp value)
1174                                   (characterp (car value))
1175                                   (encode-char
1176                                    (car value) '=ucs 'defined-only)))
1177                         )
1178                        (t)))
1179                     ))
1180                )
1181               ((or (eq name 'ideographic-structure)
1182                    (eq name 'ideographic-combination)
1183                    (eq name 'ideographic-)
1184                    (eq name '=decomposition)
1185                    (char-feature-base-name= '=decomposition name)
1186                    (char-feature-base-name= '=>decomposition name)
1187                    ;; (string-match "^=>*decomposition\\(@[^*]+\\)?$"
1188                    ;;               (symbol-name name))
1189                    (string-match "^\\(->\\|<-\\)[^*]*$" (symbol-name name))
1190                    (string-match "^\\(->\\|<-\\)[^*]*\\*sources$"
1191                                  (symbol-name name))
1192                    )
1193                (char-db-insert-relation-feature char name value
1194                                                 line-breaking
1195                                                 ccss readable))
1196               ((memq name '(ideograph=
1197                             original-ideograph-of
1198                             ancient-ideograph-of
1199                             vulgar-ideograph-of
1200                             wrong-ideograph-of
1201                             ;; simplified-ideograph-of
1202                             ideographic-variants
1203                             ;; ideographic-different-form-of
1204                             ))
1205                (insert (format "(%-18s%s " name line-breaking))
1206                (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1207                      separator nil)
1208                (while (consp value)
1209                  (setq cell (car value))
1210                  (if (and (consp cell)
1211                           (consp (car cell)))
1212                      (progn
1213                        (if separator
1214                            (insert lbs))
1215                        (char-db-insert-alist cell readable)
1216                        (setq separator lbs))
1217                    (if separator
1218                        (insert separator))
1219                    (insert (prin1-to-string cell))
1220                    (setq separator " "))
1221                  (setq value (cdr value)))
1222                (insert ")")
1223                (insert line-breaking))
1224               ((consp value)
1225                (insert (format "(%-18s " name))
1226                (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1227                      separator nil)
1228                (while (consp value)
1229                  (setq cell (car value))
1230                  (if (and (consp cell)
1231                           (consp (car cell))
1232                           (setq ret (condition-case nil
1233                                         (find-char cell)
1234                                       (error nil))))
1235                      (progn
1236                        (setq rest cell
1237                              al nil
1238                              cal nil)
1239                        (while rest
1240                          (setq key (car (car rest)))
1241                          (if (find-charset key)
1242                              (setq cal (cons key cal))
1243                            (setq al (cons key al)))
1244                          (setq rest (cdr rest)))
1245                        (if separator
1246                            (insert lbs))
1247                        (insert-char-attributes ret
1248                                                readable
1249                                                al ; cal
1250                                                nil 'for-sub-node)
1251                        (setq separator lbs))
1252                    (setq ret (prin1-to-string cell))
1253                    (if separator
1254                        (if (< (+ (current-column)
1255                                  (length ret)
1256                                  (length separator))
1257                               76)
1258                            (insert separator)
1259                          (insert lbs)))
1260                    (insert ret)
1261                    (setq separator " "))
1262                  (setq value (cdr value)))
1263                (insert ")")
1264                (insert line-breaking))
1265               (t
1266                (insert (format "(%-18s" name))
1267                (setq ret (prin1-to-string value))
1268                (unless (< (+ (current-column)
1269                              (length ret)
1270                              3)
1271                           76)
1272                  (insert line-breaking))
1273                (insert " . " ret ")" line-breaking)
1274                ;; (insert (format "(%-18s . %S)%s"
1275                ;;                 name value
1276                ;;                 line-breaking))
1277                )
1278               ))
1279       (setq attributes (cdr attributes)))
1280     (insert ")")))
1281
1282 (defun insert-char-data (char &optional readable
1283                               attributes)
1284   (save-restriction
1285     (narrow-to-region (point)(point))
1286     (insert "(define-char
1287   '")
1288     (insert-char-attributes char readable attributes)
1289     (insert ")\n")
1290     (goto-char (point-min))
1291     (while (re-search-forward "[ \t]+$" nil t)
1292       (replace-match ""))
1293     ;; from tabify.
1294     (goto-char (point-min))
1295     (while (re-search-forward "[ \t][ \t][ \t]*" nil t)
1296       (let ((column (current-column))
1297             (indent-tabs-mode t))
1298         (delete-region (match-beginning 0) (point))
1299         (indent-to column)))
1300     (goto-char (point-max))
1301     ;; (tabify (point-min)(point-max))
1302     ))
1303
1304 (defun insert-char-data-with-variant (char &optional printable
1305                                            no-ucs-unified
1306                                            script excluded-script)
1307   (insert-char-data char printable)
1308   (let ((variants (char-variants char))
1309         rest
1310         variant vs ret)
1311     (setq variants (sort variants #'<))
1312     (setq rest variants)
1313     (setq variants (cons char variants))
1314     (while rest
1315       (setq variant (car rest))
1316       (unless (get-char-attribute variant '<-subsumptive)
1317         (if (and (or (null script)
1318                      (null (setq vs (get-char-attribute variant 'script)))
1319                      (memq script vs))
1320                  (or (null excluded-script)
1321                      (null (setq vs (get-char-attribute variant 'script)))
1322                      (not (memq excluded-script vs))))
1323             (unless (and no-ucs-unified (get-char-attribute variant '=ucs))
1324               (insert-char-data variant printable)
1325               (if (setq ret (char-variants variant))
1326                   (while ret
1327                     (or (memq (car ret) variants)
1328                         ;; (get-char-attribute (car ret) '<-subsumptive)
1329                         (setq rest (nconc rest (list (car ret)))))
1330                     (setq ret (cdr ret)))))))
1331       (setq rest (cdr rest)))))
1332
1333 (defun insert-char-range-data (min max &optional script excluded-script)
1334   (let ((code min)
1335         char)
1336     (while (<= code max)
1337       (setq char (decode-char '=ucs code))
1338       (if (encode-char char '=ucs 'defined-only)
1339           (insert-char-data-with-variant char nil 'no-ucs-unified
1340                                          script excluded-script))
1341       (setq code (1+ code)))))
1342
1343 (defun write-char-range-data-to-file (min max file
1344                                           &optional script excluded-script)
1345   (let ((coding-system-for-write char-db-file-coding-system))
1346     (with-temp-buffer
1347       (insert (format ";; -*- coding: %s -*-\n"
1348                       char-db-file-coding-system))
1349       (insert-char-range-data min max script excluded-script)
1350       (write-region (point-min)(point-max) file))))
1351
1352 (defvar what-character-original-window-configuration)
1353
1354 ;;;###autoload
1355 (defun what-char-definition (char)
1356   (interactive (list (char-after)))
1357   (let ((buf (get-buffer-create "*Character Description*"))
1358         (the-buf (current-buffer))
1359         (win-conf (current-window-configuration)))
1360     (pop-to-buffer buf)
1361     (make-local-variable 'what-character-original-window-configuration)
1362     (setq what-character-original-window-configuration win-conf)
1363     (setq buffer-read-only nil)
1364     (erase-buffer)
1365     (condition-case err
1366         (progn
1367           (insert-char-data-with-variant char 'printable)
1368           (unless (char-attribute-alist char)
1369             (insert (format ";; = %c\n"
1370                             (let* ((rest (split-char char))
1371                                    (ccs (pop rest))
1372                                    (code (pop rest)))
1373                               (while rest
1374                                 (setq code (logior (lsh code 8)
1375                                                    (pop rest))))
1376                               (decode-char ccs code)))))
1377           ;; (char-db-update-comment)
1378           (set-buffer-modified-p nil)
1379           (view-mode the-buf (lambda (buf)
1380                                (set-window-configuration
1381                                 what-character-original-window-configuration)
1382                                ))
1383           (goto-char (point-min)))
1384       (error (progn
1385                (set-window-configuration
1386                 what-character-original-window-configuration)
1387                (signal (car err) (cdr err)))))))
1388
1389
1390 ;;; @ end
1391 ;;;
1392
1393 (provide 'char-db-util)
1394
1395 ;;; char-db-util.el ends here