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