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