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