(U-0002A94C): New character.
[chise/xemacs-chise.git.1] / lisp / utf-2000 / char-db-util.el
1 ;;; char-db-util.el --- Character Database utility -*- coding: utf-8-er; -*-
2
3 ;; Copyright (C) 1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,
4 ;;   2008,2009,2010,2011,2012,2013,2014,2015,2016,2017 MORIOKA Tomohiko.
5
6 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
7 ;; Keywords: CHISE, Character Database, ISO/IEC 10646, UCS, Unicode, MULE.
8
9 ;; This file is part of XEmacs CHISE.
10
11 ;; XEmacs CHISE is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; XEmacs CHISE is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs CHISE; see the file COPYING.  If not, write to
23 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Code:
27
28 (require 'chise-subr)
29 (require 'ideograph-subr)
30
31 (defconst unidata-normative-category-alist
32   '(("Lu" letter        uppercase)
33     ("Ll" letter        lowercase)
34     ("Lt" letter        titlecase)
35     ("Mn" mark          non-spacing)
36     ("Mc" mark          spacing-combining)
37     ("Me" mark          enclosing)
38     ("Nd" number        decimal-digit)
39     ("Nl" number        letter)
40     ("No" number        other)
41     ("Zs" separator     space)
42     ("Zl" separator     line)
43     ("Zp" separator     paragraph)
44     ("Cc" other         control)
45     ("Cf" other         format)
46     ("Cs" other         surrogate)
47     ("Co" other         private-use)
48     ("Cn" other         not-assigned)))
49
50 (defconst unidata-informative-category-alist
51   '(("Lm" letter        modifier)
52     ("Lo" letter        other)
53     ("Pc" punctuation   connector)
54     ("Pd" punctuation   dash)
55     ("Ps" punctuation   open)
56     ("Pe" punctuation   close)
57     ("Pi" punctuation   initial-quote)
58     ("Pf" punctuation   final-quote)
59     ("Po" punctuation   other)
60     ("Sm" symbol        math)
61     ("Sc" symbol        currency)
62     ("Sk" symbol        modifier)
63     ("So" symbol        other)
64     ))
65
66 (defconst shuowen-radicals
67   [?一 ?上 ?示 ?三 ?王 ?玉 ?玨 ?气 ?士 ?丨 ; 010
68    ?屮 ?艸 ?蓐 ?茻 ?小 ?八 ?釆 ?半 ?牛 ?犛 ; 020
69    ?告 ?口 ?凵 ?吅 ?哭 ?走 ?止 ?癶 ?步 ?此 ; 030
70    ?正 ?是 ?辵 ?彳 ?廴 ?㢟 ?行 ?齒 ?牙 ?足 ; 040
71    ?疋 ?品 ?龠 ?冊 ?㗊 ?舌 ?干 ?谷 ?只 ?㕯 ; 050
72    ?句 ?丩 ?古 ?十 ?卅 ?言 ?誩 ?音 ?䇂 ?丵 ; 060
73    ?菐 ?𠬞 ?𠬜 ?共 ?異 ?舁 ?𦥑 ?䢅 ?爨 ?革 ; 070
74    ?鬲 ?䰜 ?爪 ?𠃨 ?鬥 ?又 ?𠂇 ?㕜 ?支 ?𦘒 ; 080
75    ?聿 ?畫 ?隶 ?臤 ?臣 ?殳 ?殺 ?𠘧 ?寸 ?皮 ; 090
76    ?㼱 ?攴 ?敎 ?卜 ?用 ?爻 ?㸚 ?𥄎 ?目 ?䀠 ; 100
77    ?眉 ?盾 ?自 ?白 ?鼻 ?皕 ?習 ?羽 ?隹 ?奞 ; 110
78    ?萑 ?𦫳 ?苜 ?羊 ?羴 ?瞿 ?雔 ?雥 ?鳥 ?烏 ; 120
79    ?𠦒 ?冓 ?幺 ?𢆶 ?叀 ?玄 ?予 ?放 ?𠬪 ?𣦼 ; 130
80    ?歺 ?死 ?冎 ?骨 ?肉 ?筋 ?刀 ?刃 ?㓞 ?丰 ; 140
81    ?耒 ?𧢲 ?竹 ?箕 ?丌 ?左 ?工 ?㠭 ?巫 ?甘 ; 150
82    ?曰 ?乃 ?丂 ?可 ?兮 ?号 ?亏 ?旨 ?喜 ?壴 ; 160
83 ;  ?旨 ?曰 ?乃 ?丂 ?可 ?兮 ?号 ?亏 ?喜 ?壴 ; 160
84    ?鼓 ?豈 ?豆 ?豊 ?豐 ?䖒 ?虍 ?虎 ?虤 ?皿 ; 170
85    ?𠙴 ?去 ?血 ?丶 ?丹 ?青 ?井 ?皀 ?鬯 ?食 ; 180
86    ?亼 ?會 ?倉 ?入 ?缶 ?矢 ?高 ?冂 ?𩫏 ?京 ; 190
87    ?亯 ?𣆪 ?畗 ?㐭 ?嗇 ?來 ?麥 ?夊 ?舛 ?䑞 ; 200
88    ?韋 ?弟 ?夂 ?久 ?桀 ?木 ?東 ?林 ?才 ?叒 ; 210
89    ?之 ?帀 ?出 ?𣎵 ?生 ?乇 ?𠂹 ?𠌶 ?華 ?𥝌 ; 220
90    ?稽 ?巢 ?桼 ?束 ?㯻 ?囗 ?員 ?貝 ?邑 ?𨛜 ; 230
91    ?日 ?旦 ?倝 ?㫃 ?冥 ?晶 ?月 ?有 ?明 ?囧 ; 240
92    ?夕 ?多 ?毌 ?𢎘 ?𣐺 ?卣 ?齊 ?朿 ?片 ?鼎 ; 250
93    ?克 ?彔 ?禾 ?秝 ?黍 ?香 ?米 ?毇 ?臼 ?凶 ; 260
94    ?𣎳 ?林 ?麻 ?尗 ?耑 ?韭 ?瓜 ?瓠 ?宀 ?宮 ; 270
95    ?呂 ?穴 ?㝱 ?𤕫 ?冖 ?𠔼 ?冃 ?㒳 ?网 ?襾 ; 280
96    ?巾 ?巿 ?帛 ?白 ?㡀 ?黹 ?人 ?𠤎 ?匕 ?从 ; 290
97    ?比 ?北 ?丘 ?㐺 ?𡈼 ?重 ?臥 ?身 ?㐆 ?衣 ; 300
98    ?裘 ?老 ?毛 ?毳 ?尸 ?尺 ?尾 ?履 ?舟 ?方 ; 310
99    ?儿 ?兄 ?兂 ?皃 ?𠑹 ?先 ?秃 ?見 ?覞 ?欠 ; 320
100    ?㱃 ?㳄 ?旡 ?頁 ?𦣻 ?面 ?丏 ?首 ?𥄉 ?須 ; 330
101    ?彡 ?彣 ?文 ?髟 ?后 ?司 ?卮 ?卩 ?印 ?色 ; 340
102    ?𠨍 ?辟 ?勹 ?包 ?茍 ?鬼 ?甶 ?厶 ?嵬 ?山 ; 350
103    ?屾 ?屵 ?广 ?厂 ?丸 ?危 ?石 ?長 ?勿 ?冄 ; 360
104    ?而 ?豕 ?㣇 ?彑 ?豚 ?豸 ?𤉡 ?易 ?象 ?馬 ; 370
105    ?𢊁 ?鹿 ?麤 ?㲋 ?兔 ?萈 ?犬 ?㹜 ?鼠 ?能 ; 380
106    ?熊 ?火 ?炎 ?黑 ?囪 ?焱 ?炙 ?赤 ?大 ?亦 ; 390
107    ])
108
109 (defun shuowen-radical (number)
110   (aref shuowen-radicals (1- number)))
111
112 (defvar char-db-file-coding-system 'utf-8-mcs-er)
113
114 (defvar char-db-ignored-attributes '(ideographic-products))
115
116 (defvar char-db-coded-charset-priority-list
117   '(ascii
118     control-1
119     latin-iso8859-1
120     latin-iso8859-2
121     latin-iso8859-3
122     latin-iso8859-4
123     latin-iso8859-9
124     latin-jisx0201
125     cyrillic-iso8859-5
126     greek-iso8859-7
127     thai-tis620
128     ;; =mj
129     ;; =adobe-japan1-0
130     ;; =adobe-japan1-1
131     ;; =adobe-japan1-2
132     ;; =adobe-japan1-3
133     ;; =adobe-japan1-4
134     ;; =adobe-japan1-5
135     ;; =adobe-japan1-6
136     =jis-x0208
137     =jis-x0208@1978
138     =jis-x0208@1983
139     japanese-jisx0212
140     chinese-gb2312
141     =jis-x0208@1990
142     chinese-cns11643-1
143     chinese-cns11643-2
144     chinese-cns11643-3
145     chinese-cns11643-4
146     chinese-cns11643-5
147     chinese-cns11643-6
148     chinese-cns11643-7
149     =jis-x0213-1
150     =jis-x0213-1@2000
151     =jis-x0213-1@2004
152     =jis-x0213-2
153     korean-ksc5601
154     chinese-isoir165
155     katakana-jisx0201
156     hebrew-iso8859-8
157     chinese-gb12345
158     latin-viscii
159     ethiopic-ucs
160     =big5-cdp
161     =hanyo-denshi/ja
162     =hanyo-denshi/jb
163     =hanyo-denshi/jc
164     =hanyo-denshi/jd
165     =hanyo-denshi/ft
166     =hanyo-denshi/ia
167     =hanyo-denshi/ib
168     =hanyo-denshi/hg
169     =hanyo-denshi/jt
170     =hanyo-denshi/ks
171     =hanyo-denshi/tk
172     ;; ==mj
173     ;; ==adobe-japan1-0
174     ;; ==adobe-japan1-1
175     ;; ==adobe-japan1-2
176     ;; ==adobe-japan1-3
177     ;; ==adobe-japan1-4
178     ;; ==adobe-japan1-5
179     ;; ==adobe-japan1-6
180     ==jis-x0208
181     ==jis-x0213-1
182     ==jis-x0213-2
183     ==jis-x0212
184     ==hanyo-denshi/ja
185     ==hanyo-denshi/jb
186     ==hanyo-denshi/jc
187     ==hanyo-denshi/ft
188     ==hanyo-denshi/ia
189     ==hanyo-denshi/ib
190     ==hanyo-denshi/hg
191     ==hanyo-denshi/jt
192     ==hanyo-denshi/ks
193     ==hanyo-denshi/tk
194     =gt
195     =gt-k
196     =daikanwa
197     =daikanwa@rev2
198     =daikanwa@rev1
199     =daikanwa/+p
200     ==daikanwa
201     =cbeta
202     ideograph-hanziku-1
203     ideograph-hanziku-2
204     ideograph-hanziku-3
205     ideograph-hanziku-4
206     ideograph-hanziku-5
207     ideograph-hanziku-6
208     ideograph-hanziku-7
209     ideograph-hanziku-8
210     ideograph-hanziku-9
211     ideograph-hanziku-10
212     ideograph-hanziku-11
213     ideograph-hanziku-12
214     ;; =>>>adobe-japan1-0
215     ;; =>>>adobe-japan1-1
216     ;; =>>>adobe-japan1-2
217     ;; =>>>adobe-japan1-3
218     ;; =>>>adobe-japan1-4
219     ;; =>>>adobe-japan1-5
220     ;; =>>>adobe-japan1-6
221     ;; =>>>jis-x0208
222     ;; =>>>jis-x0213-1
223     ;; =>>>jis-x0213-2
224     ;; =>>>hanyo-denshi/ja
225     ;; =>>>hanyo-denshi/jb
226     ;; =>>>hanyo-denshi/jc
227     ;; =>>>hanyo-denshi/ft
228     ;; =>>>hanyo-denshi/ib
229     ;; =>>>hanyo-denshi/hg
230     ;; =>>>hanyo-denshi/jt
231     ;; =>>>hanyo-denshi/ks
232     ;; =>>>gt
233     =>>adobe-japan1-0
234     =>>adobe-japan1-1
235     =>>adobe-japan1-2
236     =>>adobe-japan1-3
237     =>>adobe-japan1-4
238     =>>adobe-japan1-5
239     =>>adobe-japan1-6
240     =>>jis-x0208
241     =>>jis-x0213-1
242     =>>jis-x0213-1@2000
243     =>>jis-x0213-1@2004
244     =>>jis-x0213-2
245     =>>jis-x0208@1978
246     =>>hanyo-denshi/ft
247     =>>hanyo-denshi/jt
248     =>>hanyo-denshi/ks
249     =>>gt
250     =>>daikanwa
251     =>>cbeta
252     =+>jis-x0208
253     =+>jis-x0213-1
254     =+>jis-x0213-2
255     =+>adobe-japan1-0
256     =+>adobe-japan1-1
257     =+>adobe-japan1-2
258     =+>adobe-japan1-3
259     =+>adobe-japan1-4
260     =+>adobe-japan1-5
261     =+>adobe-japan1-6
262     =+>jis-x0208@1978
263     =>jis-x0208
264     =>jis-x0208@1997
265     =>jis-x0213-1
266     =>jis-x0213-1@2000
267     =>jis-x0213-1@2004
268     =>jis-x0213-2
269     ==>ucs@bucs
270     =>iwds-1
271     =>ucs@hanyo-denshi
272     =>ucs@iso
273     =>ucs@unicode
274     =>ucs@jis
275     =>ucs@cns
276     =>ucs@ks
277     =+>ucs@iso
278     =+>ucs@unicode
279     =+>ucs@jis
280     =+>ucs@jis/1990
281     =+>ucs@cns
282     =+>ucs@ks
283     =>>ucs@iso
284     =>>ucs@unicode
285     =>>ucs@jis
286     =>>ucs@cns
287     =>>>ucs@iso
288     =>>>ucs@unicode
289     ==ucs@iso
290     ==ucs@unicode
291     ;; ==ucs@cns
292     ==gb2312
293     ==ks-x1001
294     ==cns11643-1
295     ==cns11643-2
296     ==cns11643-3
297     ==cns11643-4
298     ==cns11643-5
299     ==cns11643-6
300     ==cns11643-7
301     ==gt
302     ==jis-x0208@1990
303     ;; ==jis-x0208@1983
304     ==jis-x0208@1978
305     ==gt-k
306     =ucs@iso
307     =ucs@unicode
308     =ucs@cns
309     ==big5-cdp
310     ==cbeta
311     =>>big5-cdp
312     =>>gt-k
313     =+>gt
314     =>gt
315     =>mj
316     =>big5-cdp
317     =>daikanwa
318     =>daikanwa/ho
319     =>cns11643-5
320     =>cns11643-7
321     =big5
322     =big5-eten
323     =>gt-k
324     =zinbun-oracle
325     =>zinbun-oracle
326     =ruimoku-v6
327     =>>ruimoku-v6
328     ==ruimoku-v6
329     =jef-china3
330     =>cbeta
331     =shinjigen
332     =ucs-var-001
333     =ucs-var-002
334     =ucs-var-003
335     =ucs-var-004
336     =ucs-var-005
337     =ucs-var-006
338     =ucs-var-008
339     =ucs-var-010
340     =ucs-itaiji-001
341     =ucs-itaiji-002
342     =ucs-itaiji-003
343     =ucs-itaiji-004
344     =ucs-itaiji-005
345     =ucs-itaiji-006
346     =ucs-itaiji-008
347     =ucs-itaiji-009
348     =>ucs-itaiji-001
349     =>ucs-itaiji-002
350     =>ucs-itaiji-005
351     =>ucs-itaiji-006
352     =big5-cdp-var-001
353     =big5-cdp-var-002
354     =big5-cdp-var-003
355     =big5-cdp-var-004
356     =big5-cdp-var-005
357     =big5-cdp-itaiji-001
358     =big5-cdp-itaiji-002
359     =big5-cdp-itaiji-003
360     =>big5-cdp-itaiji-001
361     =>ucs@iwds-1
362     =>ucs@component
363     ==ucs@gb
364     =ucs@JP/hanazono))
365
366
367 ;;; @ char-db formatters
368 ;;;
369
370 (defun char-db-make-char-spec (char)
371   (let (ret char-spec)
372     (cond ((characterp char)
373            (cond ((and (setq ret (encode-char char '=ucs 'defined-only))
374                        (not (and (<= #xE000 ret)(<= ret #xF8FF))))
375                   (setq char-spec (list (cons '=ucs ret)))
376                   (cond ((setq ret (get-char-attribute char 'name))
377                          (setq char-spec (cons (cons 'name ret) char-spec))
378                          )
379                         ((setq ret (get-char-attribute char 'name*))
380                          (setq char-spec (cons (cons 'name* ret) char-spec))
381                          ))
382                   )
383                  ((encode-char char '=mj 'defined-only)
384                   (setq char-spec nil)
385                   (dolist (ccs (charset-list))
386                     (if (and (or (eq ccs '=mj)
387                                  ;; (eq (charset-property ccs 'iso-ir) 177)
388                                  (string-match "=ucs@" (symbol-name ccs))
389                                  )
390                              (setq ccs (charset-name ccs))
391                              (null (assq ccs char-spec))
392                              (setq ret (encode-char char ccs 'defined-only)))
393                         (setq char-spec (cons (cons ccs ret) char-spec))))
394                   )
395                  ((encode-char char '==mj 'defined-only)
396                   (setq char-spec nil)
397                   (dolist (ccs (charset-list))
398                     (if (and (or (eq ccs '==mj)
399                                  ;; (eq (charset-property ccs 'iso-ir) 177)
400                                  (string-match "=ucs@" (symbol-name ccs))
401                                  )
402                              (setq ccs (charset-name ccs))
403                              (null (assq ccs char-spec))
404                              (setq ret (encode-char char ccs 'defined-only)))
405                         (setq char-spec (cons (cons ccs ret) char-spec))))
406                   )
407                  ((encode-char char '=adobe-japan1 'defined-only)
408                   (setq char-spec nil)
409                   (dolist (ccs (charset-list))
410                     (if (and (or (memq ccs
411                                        '(=adobe-japan1-0
412                                          =adobe-japan1-1
413                                          =adobe-japan1-2
414                                          =adobe-japan1-3
415                                          =adobe-japan1-4
416                                          =adobe-japan1-5
417                                          =adobe-japan1-6
418                                          ))
419                                  ;; (eq (charset-property ccs 'iso-ir) 177)
420                                  (string-match "=ucs@" (symbol-name ccs))
421                                  )
422                              (setq ccs (charset-name ccs))
423                              (null (assq ccs char-spec))
424                              (setq ret (encode-char char ccs 'defined-only)))
425                         (setq char-spec (cons (cons ccs ret) char-spec))))
426                   )
427                  ((encode-char char '==adobe-japan1 'defined-only)
428                   (setq char-spec nil)
429                   (dolist (ccs (charset-list))
430                     (if (and (or (memq ccs
431                                        '(==adobe-japan1-0
432                                          ==adobe-japan1-1
433                                          ==adobe-japan1-2
434                                          ==adobe-japan1-3
435                                          ==adobe-japan1-4
436                                          ==adobe-japan1-5
437                                          ==adobe-japan1-6
438                                          ))
439                                  ;; (eq (charset-property ccs 'iso-ir) 177)
440                                  (string-match "=ucs@" (symbol-name ccs))
441                                  )
442                              (setq ccs (charset-name ccs))
443                              (null (assq ccs char-spec))
444                              (setq ret (encode-char char ccs 'defined-only)))
445                         (setq char-spec (cons (cons ccs ret) char-spec))))
446                   )
447                  ((setq ret
448                         (catch 'tag
449                           (let ((rest char-db-coded-charset-priority-list)
450                                 ccs)
451                             (while rest
452                               (setq ccs (charset-name
453                                          (find-charset (car rest))))
454                               (if (setq ret
455                                         (encode-char char ccs
456                                                      'defined-only))
457                                   (throw 'tag (cons ccs ret)))
458                               (setq rest (cdr rest))))))
459                   (setq char-spec (list ret))
460                   (dolist (ccs (delq (car ret) (charset-list)))
461                     (if (and (or (charset-iso-final-char ccs)
462                                  (memq ccs
463                                        '(=daikanwa
464                                          =daikanwa@rev2
465                                          ;; =gt-k
466                                          =jis-x0208@1997
467                                          ))
468                                  (eq (charset-property ccs 'iso-ir) 177)
469                                  ;; (string-match "=ucs@" (symbol-name ccs))
470                                  )
471                              (setq ccs (charset-name ccs))
472                              (null (assq ccs char-spec))
473                              (setq ret (encode-char char ccs 'defined-only)))
474                         (setq char-spec (cons (cons ccs ret) char-spec))))
475                   (if (null char-spec)
476                       (setq char-spec (split-char char)))
477                   (cond ((setq ret (get-char-attribute char 'name))
478                          (setq char-spec (cons (cons 'name ret) char-spec))
479                          )
480                         ((setq ret (get-char-attribute char 'name*))
481                          (setq char-spec (cons (cons 'name* ret) char-spec))
482                          ))
483                   )
484                  ((setq ret (get-char-attribute
485                              char 'ideographic-combination))
486                   (setq char-spec
487                         (cons (cons 'ideographic-combination ret)
488                               char-spec))
489                   ))
490            char-spec)
491           ((consp char)
492            char))))
493     
494 (defun char-db-insert-char-spec (char &optional readable column
495                                       required-features)
496   (unless column
497     (setq column (current-column)))
498   (let (char-spec temp-char)
499     (setq char-spec (char-db-make-char-spec char))
500     (unless (or (characterp char) ; char
501                 (condition-case nil
502                     (setq char (find-char char-spec))
503                   (error nil)))
504       ;; define temporary character
505       ;;   Current implementation is dirty.
506       (setq temp-char (define-char (cons '(ideograph-daikanwa . 0)
507                                          char-spec)))
508       (remove-char-attribute temp-char 'ideograph-daikanwa)
509       (setq char temp-char))
510     (insert-char-attributes char
511                             readable
512                             (union (mapcar #'car char-spec)
513                                    required-features)
514                             nil 'for-sub-node)
515     (when temp-char
516       ;; undefine temporary character
517       ;;   Current implementation is dirty.
518       (setq char-spec (char-attribute-alist temp-char))
519       (while char-spec
520         (remove-char-attribute temp-char (car (car char-spec)))
521         (setq char-spec (cdr char-spec))))))
522
523 (defun char-db-insert-alist (alist &optional readable column)
524   (unless column
525     (setq column (current-column)))
526   (let ((line-breaking
527          (concat "\n" (make-string (1+ column) ?\ )))
528         name value
529         ret al ; cal
530         key
531         lbs cell rest separator)
532     (insert "(")
533     (while alist
534       (setq name (car (car alist))
535             value (cdr (car alist)))
536       (cond ((eq name 'char)
537              (insert "(char . ")
538              (if (setq ret (condition-case nil
539                                (find-char value)
540                              (error nil)))
541                  (progn
542                    (setq al nil
543                          ;; cal nil
544                          )
545                    (while value
546                      (setq key (car (car value)))
547                      ;; (if (find-charset key)
548                      ;;     (setq cal (cons key cal))
549                      (setq al (cons key al))
550                      ;; )
551                      (setq value (cdr value)))
552                    (insert-char-attributes ret
553                                            readable
554                                            (or al 'none) ; cal
555                                            nil 'for-sub-node))
556                (insert (prin1-to-string value)))
557              (insert ")")
558              (insert line-breaking))
559             ((consp value)
560              (insert (format "(%-18s " name))
561              (setq lbs (concat "\n" (make-string (current-column) ?\ )))
562              (while (consp value)
563                (setq cell (car value))
564                (if (and (consp cell)
565                         (consp (car cell))
566                         (setq ret (condition-case nil
567                                       (find-char cell)
568                                     (error nil)))
569                         )
570                    (progn
571                      (setq rest cell
572                            al nil
573                            ;; cal nil
574                            )
575                      (while rest
576                        (setq key (car (car rest)))
577                        ;; (if (find-charset key)
578                        ;;     (setq cal (cons key cal))
579                        (setq al (cons key al))
580                        ;; )
581                        (setq rest (cdr rest)))
582                      (if separator
583                          (insert lbs))
584                      (insert-char-attributes ret
585                                              readable
586                                              al ; cal
587                                              nil 'for-sub-node)
588                      (setq separator lbs))
589                  (if separator
590                      (insert separator))
591                  (insert (prin1-to-string cell))
592                  (setq separator " "))
593                (setq value (cdr value)))
594              (insert ")")
595              (insert line-breaking))
596             (t
597              (insert (format "(%-18s . %S)%s"
598                              name value
599                              line-breaking))))
600       (setq alist (cdr alist))))
601   (insert ")"))
602
603 (defun char-db-insert-char-reference (plist &optional readable column)
604   (unless column
605     (setq column (current-column)))
606   (let ((line-breaking
607          (concat "\n" (make-string (1+ column) ?\ )))
608         (separator "")
609         name value)
610     (insert "(")
611     (while plist
612       (setq name (pop plist))
613       (setq value (pop plist))
614       (cond ((eq name :char)
615              (insert separator)
616              (insert ":char\t")
617              (cond ((numberp value)
618                     (setq value (decode-char '=ucs value)))
619                    ;; ((consp value)
620                    ;;  (setq value (or (find-char value)
621                    ;;                  value)))
622                    )
623              (char-db-insert-char-spec value readable)
624              (insert line-breaking)
625              (setq separator ""))
626             ((eq name :radical)
627              (insert (format "%s%s\t%d ; %c%s"
628                              separator
629                              name value
630                              (ideographic-radical value)
631                              line-breaking))
632              (setq separator ""))
633             (t
634              (insert (format "%s%s\t%S" separator name value))
635              (setq separator line-breaking)))
636       ))
637   (insert ")"))
638
639 (defun char-db-decode-isolated-char (ccs code-point)
640   (let (ret)
641     (setq ret
642           (cond ((eq ccs 'arabic-iso8859-6)
643                  (decode-char ccs code-point))
644                 ;; ((eq ccs '=gt)
645                 ;;  (decode-builtin-char '==gt code-point))
646                 ((and (memq ccs '(=gt-pj-1
647                                   =gt-pj-2
648                                   =gt-pj-3
649                                   =gt-pj-4
650                                   =gt-pj-5
651                                   =gt-pj-6
652                                   =gt-pj-7
653                                   =gt-pj-8
654                                   =gt-pj-9
655                                   =gt-pj-10
656                                   =gt-pj-11))
657                       (setq ret (decode-char ccs code-point))
658                       (setq ret (encode-char ret '=gt 'defined-only)))
659                  (decode-builtin-char '=gt ret))
660                 (t
661                  (decode-builtin-char ccs code-point))))
662     (cond ((null ret)
663            (or (decode-char ccs code-point)
664                (define-char (list (cons ccs code-point)))))
665           ((and (<= 0 (char-int ret))
666                 (<= (char-int ret) #x1F))
667            (decode-char '=ucs (+ #x2400 (char-int ret))))
668           ((= (char-int ret) #x7F)
669            ?\u2421)
670           (t ret))))
671
672 (defvar char-db-convert-obsolete-format t)
673
674 (defun char-db-insert-ccs-feature (name value line-breaking)
675   (cond
676    ((integerp value)
677     (insert
678      (format
679       (cond
680        ((memq name '(=>iwds-1
681                      ==shinjigen
682                      =shinjigen
683                      =shinjigen@1ed ==shinjigen@1ed
684                      =shinjigen@rev ==shinjigen@rev
685                      =shinjigen/+p@rev ==shinjigen/+p@rev
686                      ===daikanwa/ho ==daikanwa/ho
687                      =daikanwa/ho =>>daikanwa/ho =>daikanwa/ho))
688         "(%-18s .  %04d)\t; %c")
689        ((eq name '=shinjigen@1ed/24pr)
690         "(%-18s . %04d)\t; %c")
691        ((or
692          (memq name
693                '(===daikanwa
694                  ==daikanwa =daikanwa =>>daikanwa =>daikanwa
695                  =daikanwa@rev1 =daikanwa@rev2
696                  =daikanwa/+p ==daikanwa/+p ===daikanwa/+p
697                  =>>daikanwa/+p
698                  =daikanwa/+2p =>>daikanwa/+2p
699                  =gt ==gt ===gt
700                  =>>gt =+>gt =>gt
701                  =gt-k ==gt-k ===gt-k
702                  =>>gt-k =>gt-k
703                  =adobe-japan1-0 ==adobe-japan1-0 ===adobe-japan1-0
704                  =adobe-japan1-1 ==adobe-japan1-1 ===adobe-japan1-1
705                  =adobe-japan1-2 ==adobe-japan1-2 ===adobe-japan1-2
706                  =adobe-japan1-3 ==adobe-japan1-3 ===adobe-japan1-3
707                  =adobe-japan1-4 ==adobe-japan1-4 ===adobe-japan1-4
708                  =adobe-japan1-5 ==adobe-japan1-5 ===adobe-japan1-5
709                  =adobe-japan1-6 ==adobe-japan1-6 ===adobe-japan1-6
710                  =>>adobe-japan1-0 =+>adobe-japan1-0
711                  =>>adobe-japan1-1 =+>adobe-japan1-1
712                  =>>adobe-japan1-2 =+>adobe-japan1-2
713                  =>>adobe-japan1-3 =+>adobe-japan1-3
714                  =>>adobe-japan1-4 =+>adobe-japan1-4
715                  =>>adobe-japan1-5 =+>adobe-japan1-5
716                  =>>adobe-japan1-6 =+>adobe-japan1-6
717                  =>cbeta =cbeta =>>cbeta ==cbeta ===cbeta
718                  =zinbun-oracle =>zinbun-oracle
719                  ===hng-jou ===hng-keg ===hng-dng ===hng-mam
720                  ===hng-drt ===hng-kgk ===hng-myz ===hng-kda
721                  ===hng-khi ===hng-khm ===hng-hok ===hng-kyd ===hng-sok
722                  ===hng-yhk ===hng-kak ===hng-kar ===hng-kae
723                  ===hng-sys ===hng-tsu ===hng-tzj
724                  ===hng-hos ===hng-nak ===hng-jhk
725                  ===hng-hod ===hng-gok ===hng-ink ===hng-nto
726                  ===hng-nkm ===hng-k24 ===hng-nkk
727                  ===hng-kcc ===hng-kcj ===hng-kbk ===hng-sik
728                  ===hng-skk ===hng-kyu ===hng-ksk ===hng-wan
729                  ===hng-okd ===hng-wad ===hng-kmi
730                  ===hng-zkd ===hng-doh ===hng-jyu
731                  ===hng-tzs ===hng-kss ===hng-kyo
732                  ===hng-smk))
733          ;; (string-match "^=adobe-" (symbol-name name))
734          )
735         "(%-18s . %05d)\t; %c")
736        ((memq name '(=hanyo-denshi/ks
737                      ==hanyo-denshi/ks ===hanyo-denshi/ks
738                      =>>hanyo-denshi/ks
739                      =koseki ==koseki
740                      =mj ==mj ===mj =>>mj =>mj
741                      =zihai mojikyo))
742         "(%-18s . %06d)\t; %c")
743        ((memq name '(=hanyo-denshi/tk ==hanyo-denshi/tk))
744         "(%-18s . %08d)\t; %c")
745        ((>= (charset-dimension name) 2)
746         "(%-18s . #x%04X)\t; %c")
747        (t
748         "(%-18s . #x%02X)\t; %c"))
749       name
750       (if (= (charset-iso-graphic-plane name) 1)
751           (logior value
752                   (cond ((= (charset-dimension name) 1)
753                          #x80)
754                         ((= (charset-dimension name) 2)
755                          #x8080)
756                         ((= (charset-dimension name) 3)
757                          #x808080)
758                         (t 0)))
759         value)
760       (char-db-decode-isolated-char name value)))
761     (if (and (= (charset-chars name) 94)
762              (= (charset-dimension name) 2))
763         (insert (format " [%02d-%02d]"
764                         (- (lsh value -8) 32)
765                         (- (logand value 255) 32))))
766     )
767    (t
768     (insert (format "(%-18s . %s)" name value))
769     ))
770   (insert line-breaking))
771
772 (defun char-db-insert-relation-feature (char name value line-breaking
773                                              ccss readable)
774   (insert (format "(%-18s%s " name line-breaking))
775   (let ((lbs (concat "\n" (make-string (current-column) ?\ )))
776         separator cell sources required-features
777         ret)
778     (while (consp value)
779       (setq cell (car value))
780       (if (integerp cell)
781           (setq cell (decode-char '=ucs cell)))
782       (cond
783        ((eq name '->subsumptive)
784         (when (or (not (some (lambda (atr)
785                                (get-char-attribute cell atr))
786                              char-db-ignored-attributes))
787                   (some (lambda (ccs)
788                           (encode-char cell ccs 'defined-only))
789                         ccss))
790           (if separator
791               (insert lbs))
792           (let ((char-db-ignored-attributes
793                  (cons '<-subsumptive
794                        char-db-ignored-attributes)))
795             (insert-char-attributes cell readable nil nil 'for-sub-node))
796           (setq separator lbs))
797         )
798        ((characterp cell)
799         (setq sources
800               (get-char-attribute
801                char (intern (format "%s*sources" name))))
802         (setq required-features nil)
803         (dolist (source sources)
804           (cond
805            ((memq source '(JP
806                            JP/Jouyou
807                            shinjigen shinjigen@1ed shinjigen@rev))
808             (setq required-features
809                   (union required-features
810                          '(=jis-x0208
811                            =jis-x0208@1990
812                            =jis-x0213-1@2000
813                            =jis-x0213-1@2004
814                            =jis-x0213-2
815                            =jis-x0212
816                            =jis-x0208@1983
817                            =jis-x0208@1978
818                            =shinjigen
819                            =shinjigen@1ed
820                            =shinjigen@rev
821                            =shinjigen/+p@rev))))
822            ((eq source 'CN)
823             (setq required-features
824                   (union required-features
825                          '(=gb2312
826                            =gb12345
827                            =iso-ir165)))))
828           (cond
829            ((find-charset (setq ret (intern (format "=%s" source))))
830             (setq required-features
831                   (cons ret required-features)))
832            (t (setq required-features
833                     (cons source required-features)))))
834         (cond ((string-match "@JP" (symbol-name name))
835                (setq required-features
836                      (union required-features
837                             '(=jis-x0208
838                               =jis-x0208@1990
839                               =jis-x0213-1-2000
840                               =jis-x0213-2-2000
841                               =jis-x0212
842                               =jis-x0208@1983
843                               =jis-x0208@1978))))
844               ((string-match "@CN" (symbol-name name))
845                (setq required-features
846                      (union required-features
847                             '(=gb2312
848                               =gb12345
849                               =iso-ir165)))))
850         (if separator
851             (insert lbs))
852         (if readable
853             (insert (format "%S" cell))
854           (char-db-insert-char-spec cell readable
855                                     nil
856                                     required-features))
857         (setq separator lbs))
858        ((consp cell)
859         (if separator
860             (insert lbs))
861         (if (consp (car cell))
862             (char-db-insert-char-spec cell readable)
863           (char-db-insert-char-reference cell readable))
864         (setq separator lbs))
865        (t
866         (if separator
867             (insert separator))
868         (insert (prin1-to-string cell))
869         (setq separator " ")))
870       (setq value (cdr value)))
871     (insert ")")
872     (insert line-breaking)))
873
874 (defun insert-char-attributes (char &optional readable attributes column
875                                     for-sub-node)
876   (unless column
877     (setq column (current-column)))
878   (let (name value ; has-long-ccs-name
879         rest
880         radical strokes
881         (line-breaking
882          (concat "\n" (make-string (1+ column) ?\ )))
883         lbs cell separator ret
884         key al cal
885         dest-ccss ; sources required-features
886         ccss)
887     (let (atr-d)
888       (setq attributes
889             (sort (if attributes
890                       (if (consp attributes)
891                           (progn
892                             (dolist (name attributes)
893                               (unless (memq name char-db-ignored-attributes)
894                                 (if (find-charset name)
895                                     (push name ccss))
896                                 (push name atr-d)))
897                             atr-d))
898                     (dolist (name (char-attribute-list))
899                       (unless (memq name char-db-ignored-attributes)
900                         (if (find-charset name)
901                             (push name ccss))
902                         (push name atr-d)))
903                     atr-d)
904                   #'char-attribute-name<)))
905     (insert "(")
906     (when (memq '<-subsumptive attributes)
907       (when (or readable (not for-sub-node))
908         (when (setq value (get-char-attribute char '<-subsumptive))
909           (char-db-insert-relation-feature char '<-subsumptive value
910                                            line-breaking
911                                            ccss readable)))
912       (setq attributes (delq '<-subsumptive attributes)))
913     (when (and (memq '<-denotational attributes)
914                (setq value (get-char-attribute char '<-denotational)))
915       (char-db-insert-relation-feature char '<-denotational value
916                                        line-breaking
917                                        ccss readable)
918       (setq attributes (delq '<-denotational attributes)))
919     (when (and (memq '<-denotational@component attributes)
920                (setq value (get-char-attribute char '<-denotational@component)))
921       (char-db-insert-relation-feature char '<-denotational@component value
922                                        line-breaking
923                                        ccss readable)
924       (setq attributes (delq '<-denotational@component attributes)))
925     (when (and (memq 'name attributes)
926                (setq value (get-char-attribute char 'name)))
927       (insert (format
928                (if (> (+ (current-column) (length value)) 48)
929                    "(name . %S)%s"
930                  "(name               . %S)%s")
931                value line-breaking))
932       (setq attributes (delq 'name attributes))
933       )
934     (when (and (memq 'name* attributes)
935                (setq value (get-char-attribute char 'name*)))
936       (insert (format
937                (if (> (+ (current-column) (length value)) 48)
938                    "(name* . %S)%s"
939                  "(name*              . %S)%s")
940                value line-breaking))
941       (setq attributes (delq 'name* attributes))
942       )
943     (when (and (memq 'script attributes)
944                (setq value (get-char-attribute char 'script)))
945       (insert (format "(script\t\t%s)%s"
946                       (mapconcat (function prin1-to-string)
947                                  value " ")
948                       line-breaking))
949       (setq attributes (delq 'script attributes))
950       )
951     (dolist (name '(=>ucs =>ucs*))
952       (when (and (memq name attributes)
953                  (setq value (get-char-attribute char name)))
954         (insert (format "(%-18s . #x%04X)\t; %c%s"
955                         name value (decode-char '=ucs value)
956                         line-breaking))
957         (setq attributes (delq name attributes))))
958     (dolist (name '(=>ucs@gb =>ucs@big5))
959       (when (and (memq name attributes)
960                  (setq value (get-char-attribute char name)))
961         (insert (format "(%-18s . #x%04X)\t; %c%s"
962                         name value
963                         (decode-char (intern
964                                       (concat "="
965                                               (substring
966                                                (symbol-name name) 2)))
967                                      value)
968                         line-breaking))
969         (setq attributes (delq name attributes))
970         ))
971     ;; (dolist (name '(=>daikanwa))
972     ;;   (when (and (memq name attributes)
973     ;;              (setq value (get-char-attribute char name)))
974     ;;     (insert
975     ;;      (if (integerp value)
976     ;;          (format "(%-18s . %05d)\t; %c%s"
977     ;;                  name value (decode-char '=daikanwa value)
978     ;;                  line-breaking)
979     ;;        (format "(%-18s %s)\t; %c%s"
980     ;;                name
981     ;;                (mapconcat (function prin1-to-string)
982     ;;                           value " ")
983     ;;                (char-representative-of-daikanwa char)
984     ;;                line-breaking)))
985     ;;     (setq attributes (delq name attributes))))
986     (when (and (memq 'general-category attributes)
987                (setq value (get-char-attribute char 'general-category)))
988       (insert (format
989                "(general-category\t%s) ; %s%s"
990                (mapconcat (lambda (cell)
991                             (format "%S" cell))
992                           value " ")
993                (cond ((rassoc value unidata-normative-category-alist)
994                       "Normative Category")
995                      ((rassoc value unidata-informative-category-alist)
996                       "Informative Category")
997                      (t
998                       "Unknown Category"))
999                line-breaking))
1000       (setq attributes (delq 'general-category attributes))
1001       )
1002     (when (and (memq 'bidi-category attributes)
1003                (setq value (get-char-attribute char 'bidi-category)))
1004       (insert (format "(bidi-category\t. %S)%s"
1005                       value
1006                       line-breaking))
1007       (setq attributes (delq 'bidi-category attributes))
1008       )
1009     (unless (or (not (memq 'mirrored attributes))
1010                 (eq (setq value (get-char-attribute char 'mirrored 'empty))
1011                     'empty))
1012       (insert (format "(mirrored\t\t. %S)%s"
1013                       value
1014                       line-breaking))
1015       (setq attributes (delq 'mirrored attributes))
1016       )
1017     (cond
1018      ((and (memq 'decimal-digit-value attributes)
1019            (setq value (get-char-attribute char 'decimal-digit-value)))
1020       (insert (format "(decimal-digit-value . %S)%s"
1021                       value
1022                       line-breaking))
1023       (setq attributes (delq 'decimal-digit-value attributes))
1024       (when (and (memq 'digit-value attributes)
1025                  (setq value (get-char-attribute char 'digit-value)))
1026         (insert (format "(digit-value\t . %S)%s"
1027                         value
1028                         line-breaking))
1029         (setq attributes (delq 'digit-value attributes))
1030         )
1031       (when (and (memq 'numeric-value attributes)
1032                  (setq value (get-char-attribute char 'numeric-value)))
1033         (insert (format "(numeric-value\t . %S)%s"
1034                         value
1035                         line-breaking))
1036         (setq attributes (delq 'numeric-value attributes))
1037         )
1038       )
1039      (t
1040       (when (and (memq 'digit-value attributes)
1041                  (setq value (get-char-attribute char 'digit-value)))
1042         (insert (format "(digit-value\t. %S)%s"
1043                         value
1044                         line-breaking))
1045         (setq attributes (delq 'digit-value attributes))
1046         )
1047       (when (and (memq 'numeric-value attributes)
1048                  (setq value (get-char-attribute char 'numeric-value)))
1049         (insert (format "(numeric-value\t. %S)%s"
1050                         value
1051                         line-breaking))
1052         (setq attributes (delq 'numeric-value attributes))
1053         )))
1054     (when (and (memq 'iso-10646-comment attributes)
1055                (setq value (get-char-attribute char 'iso-10646-comment)))
1056       (insert (format "(iso-10646-comment\t. %S)%s"
1057                       value
1058                       line-breaking))
1059       (setq attributes (delq 'iso-10646-comment attributes))
1060       )
1061     (when (and (memq 'morohashi-daikanwa attributes)
1062                (setq value (get-char-attribute char 'morohashi-daikanwa)))
1063       (insert (format "(morohashi-daikanwa\t%s)%s"
1064                       (mapconcat (function prin1-to-string) value " ")
1065                       line-breaking))
1066       (setq attributes (delq 'morohashi-daikanwa attributes))
1067       )
1068     (setq radical nil
1069           strokes nil)
1070     (when (and (memq 'ideographic-radical attributes)
1071                (setq value (get-char-attribute char 'ideographic-radical)))
1072       (setq radical value)
1073       (insert (format "(ideographic-radical . %S)\t; %c%s"
1074                       radical
1075                       (ideographic-radical radical)
1076                       line-breaking))
1077       (setq attributes (delq 'ideographic-radical attributes))
1078       )
1079     (when (and (memq 'shuowen-radical attributes)
1080                (setq value (get-char-attribute char 'shuowen-radical)))
1081       (insert (format "(shuowen-radical\t. %S)\t; %c%s"
1082                       value
1083                       (shuowen-radical value)
1084                       line-breaking))
1085       (setq attributes (delq 'shuowen-radical attributes))
1086       )
1087     (let (key)
1088       (dolist (domain
1089                (append
1090                 char-db-feature-domains
1091                 (let (dest domain)
1092                   (dolist (feature (char-attribute-list))
1093                     (setq feature (symbol-name feature))
1094                     (when (string-match
1095                            "\\(radical\\|strokes\\)@\\([^@*]+\\)\\(\\*\\|$\\)"
1096                            feature)
1097                       (setq domain (intern (match-string 2 feature)))
1098                      (unless (memq domain dest)
1099                        (setq dest (cons domain dest)))))
1100                   (sort dest #'string<))))
1101         (setq key (intern (format "%s@%s" 'ideographic-radical domain)))
1102         (when (and (memq key attributes)
1103                    (setq value (get-char-attribute char key)))
1104           (setq radical value)
1105           (insert (format "(%s . %S)\t; %c%s"
1106                           key
1107                           radical
1108                           (ideographic-radical radical)
1109                           line-breaking))
1110           (setq attributes (delq key attributes))
1111           )
1112         (setq key (intern (format "%s@%s" 'ideographic-strokes domain)))
1113         (when (and (memq key attributes)
1114                    (setq value (get-char-attribute char key)))
1115           (setq strokes value)
1116           (insert (format "(%s . %S)%s"
1117                           key
1118                           strokes
1119                           line-breaking))
1120           (setq attributes (delq key attributes))
1121           )
1122         (setq key (intern (format "%s@%s" 'total-strokes domain)))
1123         (when (and (memq key attributes)
1124                    (setq value (get-char-attribute char key)))
1125           (insert (format "(%s       . %S)%s"
1126                           key
1127                           value
1128                           line-breaking))
1129           (setq attributes (delq key attributes))
1130           )
1131         (dolist (feature '(ideographic-radical
1132                            ideographic-strokes
1133                            total-strokes))
1134           (setq key (intern (format "%s@%s*sources" feature domain)))
1135           (when (and (memq key attributes)
1136                      (setq value (get-char-attribute char key)))
1137             (insert (format "(%s%s" key line-breaking))
1138             (dolist (cell value)
1139               (insert (format " %s" cell)))
1140             (insert ")")
1141             (insert line-breaking)
1142             (setq attributes (delq key attributes))
1143             ))
1144         ))
1145     (when (and (memq 'ideographic-strokes attributes)
1146                (setq value (get-char-attribute char 'ideographic-strokes)))
1147       (setq strokes value)
1148       (insert (format "(ideographic-strokes . %S)%s"
1149                       strokes
1150                       line-breaking))
1151       (setq attributes (delq 'ideographic-strokes attributes))
1152       )
1153     (when (and (memq 'kangxi-radical attributes)
1154                (setq value (get-char-attribute char 'kangxi-radical)))
1155       (unless (eq value radical)
1156         (insert (format "(kangxi-radical\t . %S)\t; %c%s"
1157                         value
1158                         (ideographic-radical value)
1159                         line-breaking))
1160         (or radical
1161             (setq radical value)))
1162       (setq attributes (delq 'kangxi-radical attributes))
1163       )
1164     (when (and (memq 'kangxi-strokes attributes)
1165                (setq value (get-char-attribute char 'kangxi-strokes)))
1166       (unless (eq value strokes)
1167         (insert (format "(kangxi-strokes\t . %S)%s"
1168                         value
1169                         line-breaking))
1170         (or strokes
1171             (setq strokes value)))
1172       (setq attributes (delq 'kangxi-strokes attributes))
1173       )
1174     ;; (when (and (memq 'japanese-radical attributes)
1175     ;;            (setq value (get-char-attribute char 'japanese-radical)))
1176     ;;   (unless (eq value radical)
1177     ;;     (insert (format "(japanese-radical\t . %S)\t; %c%s"
1178     ;;                     value
1179     ;;                     (ideographic-radical value)
1180     ;;                     line-breaking))
1181     ;;     (or radical
1182     ;;         (setq radical value)))
1183     ;;   (setq attributes (delq 'japanese-radical attributes))
1184     ;;   )
1185     (when (and (memq 'japanese-strokes attributes)
1186                (setq value (get-char-attribute char 'japanese-strokes)))
1187       (unless (eq value strokes)
1188         (insert (format "(japanese-strokes\t . %S)%s"
1189                         value
1190                         line-breaking))
1191         (or strokes
1192             (setq strokes value)))
1193       (setq attributes (delq 'japanese-strokes attributes))
1194       )
1195     (when (and (memq 'cns-radical attributes)
1196                (setq value (get-char-attribute char 'cns-radical)))
1197       (insert (format "(cns-radical\t . %S)\t; %c%s"
1198                       value
1199                       (ideographic-radical value)
1200                       line-breaking))
1201       (setq attributes (delq 'cns-radical attributes))
1202       )
1203     (when (and (memq 'cns-strokes attributes)
1204                (setq value (get-char-attribute char 'cns-strokes)))
1205       (unless (eq value strokes)
1206         (insert (format "(cns-strokes\t . %S)%s"
1207                         value
1208                         line-breaking))
1209         (or strokes
1210             (setq strokes value)))
1211       (setq attributes (delq 'cns-strokes attributes))
1212       )
1213     ;; (when (and (memq 'shinjigen-1-radical attributes)
1214     ;;            (setq value (get-char-attribute char 'shinjigen-1-radical)))
1215     ;;   (unless (eq value radical)
1216     ;;     (insert (format "(shinjigen-1-radical . %S)\t; %c%s"
1217     ;;                     value
1218     ;;                     (ideographic-radical value)
1219     ;;                     line-breaking))
1220     ;;     (or radical
1221     ;;         (setq radical value)))
1222     ;;   (setq attributes (delq 'shinjigen-1-radical attributes))
1223     ;;   )
1224     ;; (when (and (memq 'ideographic- attributes)
1225     ;;            (setq value (get-char-attribute char 'ideographic-)))
1226     ;;   (insert "(ideographic-       ")
1227     ;;   (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1228     ;;         separator nil)
1229     ;;   (while (consp value)
1230     ;;     (setq cell (car value))
1231     ;;     (if (integerp cell)
1232     ;;         (setq cell (decode-char '=ucs cell)))
1233     ;;     (cond ((characterp cell)
1234     ;;            (if separator
1235     ;;                (insert lbs))
1236     ;;            (if readable
1237     ;;                (insert (format "%S" cell))
1238     ;;              (char-db-insert-char-spec cell readable))
1239     ;;            (setq separator lbs))
1240     ;;           ((consp cell)
1241     ;;            (if separator
1242     ;;                (insert lbs))
1243     ;;            (if (consp (car cell))
1244     ;;                (char-db-insert-char-spec cell readable)
1245     ;;              (char-db-insert-char-reference cell readable))
1246     ;;            (setq separator lbs))
1247     ;;           (t
1248     ;;            (if separator
1249     ;;                (insert separator))
1250     ;;            (insert (prin1-to-string cell))
1251     ;;            (setq separator " ")))
1252     ;;     (setq value (cdr value)))
1253     ;;   (insert ")")
1254     ;;   (insert line-breaking)
1255     ;;   (setq attributes (delq 'ideographic- attributes)))
1256     (when (and (memq 'total-strokes attributes)
1257                (setq value (get-char-attribute char 'total-strokes)))
1258       (insert (format "(total-strokes       . %S)%s"
1259                       value
1260                       line-breaking))
1261       (setq attributes (delq 'total-strokes attributes))
1262       )
1263     ;; (when (and (memq '->ideograph attributes)
1264     ;;            (setq value (get-char-attribute char '->ideograph)))
1265     ;;   (insert (format "(->ideograph\t%s)%s"
1266     ;;                   (mapconcat (lambda (code)
1267     ;;                                (cond ((symbolp code)
1268     ;;                                       (symbol-name code))
1269     ;;                                      ((integerp code)
1270     ;;                                       (format "#x%04X" code))
1271     ;;                                      (t
1272     ;;                                       (format "%s %S"
1273     ;;                                               line-breaking code))))
1274     ;;                              value " ")
1275     ;;                   line-breaking))
1276     ;;   (setq attributes (delq '->ideograph attributes))
1277     ;;   )
1278     ;; (when (and (memq '->decomposition attributes)
1279     ;;            (setq value (get-char-attribute char '->decomposition)))
1280     ;;   (insert (format "(->decomposition\t%s)%s"
1281     ;;                   (mapconcat (lambda (code)
1282     ;;                                (cond ((symbolp code)
1283     ;;                                       (symbol-name code))
1284     ;;                                      ((characterp code)
1285     ;;                                       (if readable
1286     ;;                                           (format "%S" code)
1287     ;;                                         (format "#x%04X"
1288     ;;                                                 (char-int code))
1289     ;;                                         ))
1290     ;;                                      ((integerp code)
1291     ;;                                       (format "#x%04X" code))
1292     ;;                                      (t
1293     ;;                                       (format "%s%S" line-breaking code))))
1294     ;;                              value " ")
1295     ;;                   line-breaking))
1296     ;;   (setq attributes (delq '->decomposition attributes))
1297     ;;   )
1298     (if (equal (get-char-attribute char '->titlecase)
1299                (get-char-attribute char '->uppercase))
1300         (setq attributes (delq '->titlecase attributes)))
1301     (when (and (memq '->mojikyo attributes)
1302                (setq value (get-char-attribute char '->mojikyo)))
1303       (insert (format "(->mojikyo\t\t. %06d)\t; %c%s"
1304                       value (decode-char 'mojikyo value)
1305                       line-breaking))
1306       (setq attributes (delq '->mojikyo attributes))
1307       )
1308     ;; (when (and (memq 'hanyu-dazidian-vol attributes)
1309     ;;            (setq value (get-char-attribute char 'hanyu-dazidian-vol)))
1310     ;;   (insert (format "(hanyu-dazidian-vol  . %d)%s"
1311     ;;                   value line-breaking))
1312     ;;   (setq attributes (delq 'hanyu-dazidian-vol attributes))
1313     ;;   )
1314     ;; (when (and (memq 'hanyu-dazidian-page attributes)
1315     ;;            (setq value (get-char-attribute char 'hanyu-dazidian-page)))
1316     ;;   (insert (format "(hanyu-dazidian-page . %d)%s"
1317     ;;                   value line-breaking))
1318     ;;   (setq attributes (delq 'hanyu-dazidian-page attributes))
1319     ;;   )
1320     ;; (when (and (memq 'hanyu-dazidian-char attributes)
1321     ;;            (setq value (get-char-attribute char 'hanyu-dazidian-char)))
1322     ;;   (insert (format "(hanyu-dazidian-char . %d)%s"
1323     ;;                   value line-breaking))
1324     ;;   (setq attributes (delq 'hanyu-dazidian-char attributes))
1325     ;;   )
1326     (unless readable
1327       (dolist (ignored '(composition
1328                          ->denotational <-subsumptive ->ucs-unified
1329                          ->ideographic-component-forms))
1330         (setq attributes (delq ignored attributes))))
1331     (while attributes
1332       (setq name (car attributes))
1333       (unless (eq (setq value (get-char-attribute char name 'value-is-empty))
1334                   'value-is-empty)
1335         (cond ((setq ret (find-charset name))
1336                (setq name (charset-name ret))
1337                (when (not (memq name dest-ccss))
1338                  (setq dest-ccss (cons name dest-ccss))
1339                  (char-db-insert-ccs-feature name value line-breaking))
1340                )
1341               ((string-match "^=>ucs@" (symbol-name name))
1342                (insert (format "(%-18s . #x%04X)\t; %c%s"
1343                                name value (decode-char '=ucs value)
1344                                line-breaking))
1345                )
1346               ((eq name 'jisx0208-1978/4X)
1347                (insert (format "(%-18s . #x%04X)%s"
1348                                name value
1349                                line-breaking))
1350                )
1351               ((and
1352                 (not readable)
1353                 (not (eq name '->subsumptive))
1354                 (not (eq name '->uppercase))
1355                 (not (eq name '->lowercase))
1356                 (not (eq name '->titlecase))
1357                 (not (eq name '->canonical))
1358                 (not (eq name '->Bopomofo))
1359                 (not (eq name '->mistakable))
1360                 (not (eq name '->ideographic-variants))
1361                 (null (get-char-attribute
1362                        char (intern (format "%s*sources" name))))
1363                 (not (string-match "\\*sources$" (symbol-name name)))
1364                 (null (get-char-attribute
1365                        char (intern (format "%s*note" name))))
1366                 (not (string-match "\\*note$" (symbol-name name)))
1367                 (or (eq name '<-identical)
1368                     (eq name '<-uppercase)
1369                     (eq name '<-lowercase)
1370                     (eq name '<-titlecase)
1371                     (eq name '<-canonical)
1372                     (eq name '<-ideographic-variants)
1373                     ;; (eq name '<-synonyms)
1374                     (string-match "^<-synonyms" (symbol-name name))
1375                     (eq name '<-mistakable)
1376                     (when (string-match "^->" (symbol-name name))
1377                       (cond
1378                        ((string-match "^->fullwidth" (symbol-name name))
1379                         (not (and (consp value)
1380                                   (characterp (car value))
1381                                   (encode-char
1382                                    (car value) '=ucs 'defined-only)))
1383                         )
1384                        (t)))
1385                     ))
1386                )
1387               ((or (eq name 'ideographic-structure)
1388                    (eq name 'ideographic-combination)
1389                    ;; (eq name 'ideographic-)
1390                    (eq name '=decomposition)
1391                    (char-feature-base-name= '=decomposition name)
1392                    (char-feature-base-name= '=>decomposition name)
1393                    ;; (string-match "^=>*decomposition\\(@[^*]+\\)?$"
1394                    ;;               (symbol-name name))
1395                    (string-match "^\\(->\\|<-\\)[^*]*$" (symbol-name name))
1396                    (string-match "^\\(->\\|<-\\)[^*]*\\*sources$"
1397                                  (symbol-name name))
1398                    )
1399                (char-db-insert-relation-feature char name value
1400                                                 line-breaking
1401                                                 ccss readable))
1402               ;; ((memq name '(ideograph=
1403               ;;               original-ideograph-of
1404               ;;               ancient-ideograph-of
1405               ;;               vulgar-ideograph-of
1406               ;;               wrong-ideograph-of
1407               ;;               ;; simplified-ideograph-of
1408               ;;               ideographic-variants
1409               ;;               ;; ideographic-different-form-of
1410               ;;               ))
1411               ;;  (insert (format "(%-18s%s " name line-breaking))
1412               ;;  (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1413               ;;        separator nil)
1414               ;;  (while (consp value)
1415               ;;    (setq cell (car value))
1416               ;;    (if (and (consp cell)
1417               ;;             (consp (car cell)))
1418               ;;        (progn
1419               ;;          (if separator
1420               ;;              (insert lbs))
1421               ;;          (char-db-insert-alist cell readable)
1422               ;;          (setq separator lbs))
1423               ;;      (if separator
1424               ;;          (insert separator))
1425               ;;      (insert (prin1-to-string cell))
1426               ;;      (setq separator " "))
1427               ;;    (setq value (cdr value)))
1428               ;;  (insert ")")
1429               ;;  (insert line-breaking))
1430               ((consp value)
1431                (insert (format "(%-18s " name))
1432                (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1433                      separator nil)
1434                (while (consp value)
1435                  (setq cell (car value))
1436                  (if (and (consp cell)
1437                           (consp (car cell))
1438                           (setq ret (condition-case nil
1439                                         (find-char cell)
1440                                       (error nil))))
1441                      (progn
1442                        (setq rest cell
1443                              al nil
1444                              cal nil)
1445                        (while rest
1446                          (setq key (car (car rest)))
1447                          (if (find-charset key)
1448                              (setq cal (cons key cal))
1449                            (setq al (cons key al)))
1450                          (setq rest (cdr rest)))
1451                        (if separator
1452                            (insert lbs))
1453                        (insert-char-attributes ret
1454                                                readable
1455                                                al ; cal
1456                                                nil 'for-sub-node)
1457                        (setq separator lbs))
1458                    (setq ret (prin1-to-string cell))
1459                    (if separator
1460                        (if (< (+ (current-column)
1461                                  (length ret)
1462                                  (length separator))
1463                               76)
1464                            (insert separator)
1465                          (insert lbs)))
1466                    (insert ret)
1467                    (setq separator " "))
1468                  (setq value (cdr value)))
1469                (insert ")")
1470                (insert line-breaking))
1471               (t
1472                (insert (format "(%-18s" name))
1473                (setq ret (prin1-to-string value))
1474                (unless (< (+ (current-column)
1475                              (length ret)
1476                              3)
1477                           76)
1478                  (insert line-breaking))
1479                (insert " . " ret ")" line-breaking)
1480                ;; (insert (format "(%-18s . %S)%s"
1481                ;;                 name value
1482                ;;                 line-breaking))
1483                )
1484               ))
1485       (setq attributes (cdr attributes)))
1486     (insert ")")))
1487
1488 (defun insert-char-data (char &optional readable
1489                               attributes)
1490   (save-restriction
1491     (narrow-to-region (point)(point))
1492     (insert "(define-char
1493   '")
1494     (insert-char-attributes char readable attributes)
1495     (insert ")\n")
1496     (goto-char (point-min))
1497     (while (re-search-forward "[ \t]+$" nil t)
1498       (replace-match ""))
1499     ;; from tabify.
1500     (goto-char (point-min))
1501     (while (re-search-forward "[ \t][ \t][ \t]*" nil t)
1502       (let ((column (current-column))
1503             (indent-tabs-mode t))
1504         (delete-region (match-beginning 0) (point))
1505         (indent-to column)))
1506     (goto-char (point-max))
1507     ;; (tabify (point-min)(point-max))
1508     ))
1509
1510 (defun insert-char-data-with-variant (char &optional printable
1511                                            no-ucs-unified
1512                                            script excluded-script)
1513   (insert-char-data char printable)
1514   (let ((variants (char-variants char))
1515         rest
1516         variant vs ret)
1517     (setq variants (sort variants #'<))
1518     (setq rest variants)
1519     (setq variants (cons char variants))
1520     (while rest
1521       (setq variant (car rest))
1522       (unless (get-char-attribute variant '<-subsumptive)
1523         (if (and (or (null script)
1524                      (null (setq vs (get-char-attribute variant 'script)))
1525                      (memq script vs))
1526                  (or (null excluded-script)
1527                      (null (setq vs (get-char-attribute variant 'script)))
1528                      (not (memq excluded-script vs))))
1529             (unless (and no-ucs-unified (get-char-attribute variant '=ucs))
1530               (insert-char-data variant printable)
1531               (if (setq ret (char-variants variant))
1532                   (while ret
1533                     (or (memq (car ret) variants)
1534                         ;; (get-char-attribute (car ret) '<-subsumptive)
1535                         (setq rest (nconc rest (list (car ret)))))
1536                     (setq ret (cdr ret)))))))
1537       (setq rest (cdr rest)))))
1538
1539 (defun insert-char-range-data (min max &optional script excluded-script)
1540   (let ((code min)
1541         char)
1542     (while (<= code max)
1543       (setq char (decode-char '=ucs code))
1544       (if (encode-char char '=ucs 'defined-only)
1545           (insert-char-data-with-variant char nil 'no-ucs-unified
1546                                          script excluded-script))
1547       (setq code (1+ code)))))
1548
1549 (defun write-char-range-data-to-file (min max file
1550                                           &optional script excluded-script)
1551   (let ((coding-system-for-write char-db-file-coding-system))
1552     (with-temp-buffer
1553       (insert (format ";; -*- coding: %s -*-\n"
1554                       char-db-file-coding-system))
1555       (insert-char-range-data min max script excluded-script)
1556       (write-region (point-min)(point-max) file))))
1557
1558 (defvar what-character-original-window-configuration)
1559
1560 ;;;###autoload
1561 (defun what-char-definition (char)
1562   (interactive (list (char-after)))
1563   (let ((buf (get-buffer-create "*Character Description*"))
1564         (the-buf (current-buffer))
1565         (win-conf (current-window-configuration)))
1566     (pop-to-buffer buf)
1567     (make-local-variable 'what-character-original-window-configuration)
1568     (setq what-character-original-window-configuration win-conf)
1569     (setq buffer-read-only nil)
1570     (erase-buffer)
1571     (condition-case err
1572         (progn
1573           (insert-char-data-with-variant char 'printable)
1574           (unless (char-attribute-alist char)
1575             (insert (format ";; = %c\n"
1576                             (let* ((rest (split-char char))
1577                                    (ccs (pop rest))
1578                                    (code (pop rest)))
1579                               (while rest
1580                                 (setq code (logior (lsh code 8)
1581                                                    (pop rest))))
1582                               (decode-char ccs code)))))
1583           ;; (char-db-update-comment)
1584           (set-buffer-modified-p nil)
1585           (view-mode the-buf (lambda (buf)
1586                                (set-window-configuration
1587                                 what-character-original-window-configuration)
1588                                ))
1589           (goto-char (point-min)))
1590       (error (progn
1591                (set-window-configuration
1592                 what-character-original-window-configuration)
1593                (signal (car err) (cdr err)))))))
1594
1595
1596 ;;; @ end
1597 ;;;
1598
1599 (provide 'char-db-util)
1600
1601 ;;; char-db-util.el ends here