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