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