(shuowen-radicals): Add ?𠦒, ?冓, ?幺, ?𢆶, ?叀, ?玄, ?予, ?放, ?𠬪,
[chise/xemacs-chise.git.1] / lisp / utf-2000 / char-db-util.el
1 ;;; char-db-util.el --- Character Database utility -*- coding: utf-8-er; -*-
2
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;;   2007 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 'alist)
29
30 (defconst unidata-normative-category-alist
31   '(("Lu" letter        uppercase)
32     ("Ll" letter        lowercase)
33     ("Lt" letter        titlecase)
34     ("Mn" mark          non-spacing)
35     ("Mc" mark          spacing-combining)
36     ("Me" mark          enclosing)
37     ("Nd" number        decimal-digit)
38     ("Nl" number        letter)
39     ("No" number        other)
40     ("Zs" separator     space)
41     ("Zl" separator     line)
42     ("Zp" separator     paragraph)
43     ("Cc" other         control)
44     ("Cf" other         format)
45     ("Cs" other         surrogate)
46     ("Co" other         private-use)
47     ("Cn" other         not-assigned)))
48
49 (defconst unidata-informative-category-alist
50   '(("Lm" letter        modifier)
51     ("Lo" letter        other)
52     ("Pc" punctuation   connector)
53     ("Pd" punctuation   dash)
54     ("Ps" punctuation   open)
55     ("Pe" punctuation   close)
56     ("Pi" punctuation   initial-quote)
57     ("Pf" punctuation   final-quote)
58     ("Po" punctuation   other)
59     ("Sm" symbol        math)
60     ("Sc" symbol        currency)
61     ("Sk" symbol        modifier)
62     ("So" symbol        other)
63     ))
64
65 (defconst ideographic-radicals
66   (let ((v (make-vector 215 nil))
67         (i 1))
68     (while (< i 215)
69       (aset v i (decode-char '=ucs (+ #x2EFF i)))
70       (setq i (1+ i)))
71     v))
72
73 (defun ideographic-radical (number)
74   (aref ideographic-radicals number))
75
76 (defconst shuowen-radicals
77   [?一 ?上 ?示 ?三 ?王 ?玉 ?玨 ?气 ?士 ?丨
78    ?屮 ?艸 ?蓐 ?茻 ?小 ?八 ?釆 ?半 ?牛 ?犛
79    ?告 ?口 ?凵 ?吅 ?哭 ?走 ?止 ?癶 ?步 ?此
80    ?正 ?是 ?辵 ?彳 ?廴 ?𢓊 ?行 ?齒 ?牙 ?足
81    ?疋 ?品 ?龠 ?冊 ?㗊 ?舌 ?干 ?谷 ?只 ?㕯
82    ?句 ?丩 ?古 ?十 ?卅 ?言 ?誩 ?音 ?䇂 ?丵
83    ?菐 ?𠬞 ?廾 ?共 ?異 ?舁 ?𦥑 ?䢅 ?爨 ?革
84    ?鬲 ?䰜 ?爪 ?𠃨 ?鬥 ?又 ?𠂇 ?㕜 ?支 ?𦘒
85    ?聿 ?畫 ?隶 ?堅 ?臣 ?殳 ?殺 ?𠘧 ?寸 ?皮
86    ?㼱 ?攴 ?敎 ?卜 ?用 ?爻 ?㸚 ?𥄎 ?目 ?䀠
87    ?眉 ?盾 ?自 ?白 ?鼻 ?皕 ?習 ?羽 ?隹 ?奞
88    ?萑 ?𦫳 ?苜 ?羊 ?羴 ?瞿 ?雔 ?雥 ?鳥 ?𠦒
89    ?冓 ?幺 ?𢆶 ?叀 ?玄 ?予 ?放 ?𠬪 ?𣦼 ?歺
90    ?死 ?冎 ?骨 ?肉 ?筋 ?刀 ?刃 ?㓞 ?丰 ?耒
91    ?𧢲])
92
93 (defun shuowen-radical (number)
94   (aref shuowen-radicals (1- number)))
95
96 (defvar char-db-file-coding-system 'utf-8-mcs-er)
97
98 (defvar char-db-feature-domains
99   '(ucs ucs/compat daikanwa cns gt jis jis/alt jis/a jis/b
100         jis-x0212 jis-x0213 cdp shinjigen misc unknown))
101
102 (defvar char-db-ignored-attributes '(ideographic-products))
103
104 (defun char-attribute-name< (ka kb)
105   (cond
106    ((eq '->denotational kb)
107     t)
108    ((eq '->subsumptive kb)
109     (not (eq '->denotational ka)))
110    ((eq '->denotational ka)
111     nil)
112    ((eq '->subsumptive ka)
113     nil)
114    ((and (symbolp ka)
115          (string-match "^->" (symbol-name ka)))
116     (cond ((and (symbolp kb)
117                 (string-match "^->" (symbol-name kb)))
118            (string< (symbol-name ka)
119                     (symbol-name kb))
120            ))
121     )
122    ((and (symbolp kb)
123          (string-match "^->" (symbol-name kb)))
124     t)
125    ((and (symbolp ka)
126          (string-match "^<-" (symbol-name ka)))
127     (cond ((symbolp kb)
128            (cond ((string-match "^<-" (symbol-name kb))
129                   (string< (symbol-name ka)
130                            (symbol-name kb))
131                   )
132                  ;; ((string-match "^->" (symbol-name kb))
133                  ;;  t)
134                  )))
135     )
136    ((and (symbolp kb)
137          (string-match "^<-" (symbol-name kb)))
138     t
139     ;; (not (string-match "^->" (symbol-name ka)))
140     )
141    ((find-charset ka)
142     (if (find-charset kb)
143         (if (<= (charset-id ka) 1)
144             (if (<= (charset-id kb) 1)
145                 (cond
146                  ((= (charset-dimension ka)
147                      (charset-dimension kb))
148                   (> (charset-id ka)(charset-id kb)))
149                  (t
150                   (> (charset-dimension ka)
151                      (charset-dimension kb))
152                   ))
153               t)
154           (if (<= (charset-id kb) 1)
155               nil
156             (< (charset-id ka)(charset-id kb))))
157       nil))
158    ((find-charset kb)
159     t)
160    ((symbolp ka)
161     (cond ((symbolp kb)
162            (string< (symbol-name ka)
163                     (symbol-name kb)))
164           (t)))
165    ((symbolp kb)
166     nil)))
167
168 (defvar char-db-coded-charset-priority-list
169   '(ascii
170     control-1
171     latin-iso8859-1
172     latin-iso8859-2
173     latin-iso8859-3
174     latin-iso8859-4
175     latin-iso8859-9
176     latin-jisx0201
177     cyrillic-iso8859-5
178     greek-iso8859-7
179     thai-tis620
180     =jis-x0208
181     =jis-x0208@1978
182     =jis-x0208@1983
183     japanese-jisx0212
184     chinese-gb2312
185     =jis-x0208@1990
186     chinese-cns11643-1
187     chinese-cns11643-2
188     chinese-cns11643-3
189     chinese-cns11643-4
190     chinese-cns11643-5
191     chinese-cns11643-6
192     chinese-cns11643-7
193     =jis-x0213-1-2000
194     =jis-x0213-2-2000
195     korean-ksc5601
196     chinese-isoir165
197     katakana-jisx0201
198     hebrew-iso8859-8
199     chinese-gb12345
200     latin-viscii
201     ethiopic-ucs
202     =big5-cdp
203     =gt
204     ideograph-daikanwa-2
205     ideograph-daikanwa
206     =cbeta
207     ideograph-hanziku-1
208     ideograph-hanziku-2
209     ideograph-hanziku-3
210     ideograph-hanziku-4
211     ideograph-hanziku-5
212     ideograph-hanziku-6
213     ideograph-hanziku-7
214     ideograph-hanziku-8
215     ideograph-hanziku-9
216     ideograph-hanziku-10
217     ideograph-hanziku-11
218     ideograph-hanziku-12
219     =gt-k
220     =ucs@iso
221     =ucs@unicode
222     =big5
223     =big5-eten
224     =jis-x0208@1997
225     =zinbun-oracle
226     =ruimoku-v6
227     =jef-china3))
228
229 (defun char-db-make-char-spec (char)
230   (let (ret char-spec)
231     (cond ((characterp char)
232            (cond ((and (setq ret (encode-char char '=ucs 'defined-only))
233                        (not (and (<= #xE000 ret)(<= ret #xF8FF))))
234                   (setq char-spec (list (cons '=ucs ret)))
235                   (cond ((setq ret (get-char-attribute char 'name))
236                          (setq char-spec (cons (cons 'name ret) char-spec))
237                          )
238                         ((setq ret (get-char-attribute char 'name*))
239                          (setq char-spec (cons (cons 'name* ret) char-spec))
240                          ))
241                   )
242                  ((setq ret
243                         (catch 'tag
244                           (let ((rest char-db-coded-charset-priority-list)
245                                 ccs)
246                             (while rest
247                               (setq ccs (charset-name
248                                          (find-charset (car rest))))
249                               (if (setq ret
250                                         (encode-char char ccs
251                                                      'defined-only))
252                                   (throw 'tag (cons ccs ret)))
253                               (setq rest (cdr rest))))))
254                   (setq char-spec (list ret))
255                   (dolist (ccs (delq (car ret) (charset-list)))
256                     (if (and (or (charset-iso-final-char ccs)
257                                  (memq ccs
258                                        '(=daikanwa
259                                          =daikanwa@rev2
260                                          ;; =gt-k
261                                          =jis-x0208@1997
262                                          )))
263                              (setq ccs (charset-name ccs))
264                              (null (assq ccs char-spec))
265                              (setq ret (encode-char char ccs 'defined-only)))
266                         (setq char-spec (cons (cons ccs ret) char-spec))))
267                   (if (null char-spec)
268                       (setq char-spec (split-char char)))
269                   (cond ((setq ret (get-char-attribute char 'name))
270                          (setq char-spec (cons (cons 'name ret) char-spec))
271                          )
272                         ((setq ret (get-char-attribute char 'name*))
273                          (setq char-spec (cons (cons 'name* ret) char-spec))
274                          ))
275                   ))
276            char-spec)
277           ((consp char)
278            char))))
279     
280 (defun char-db-insert-char-spec (char &optional readable column
281                                       required-features)
282   (unless column
283     (setq column (current-column)))
284   (let (char-spec temp-char)
285     (setq char-spec (char-db-make-char-spec char))
286     (unless (or (characterp char) ; char
287                 (condition-case nil
288                     (setq char (find-char char-spec))
289                   (error nil)))
290       ;; define temporary character
291       ;;   Current implementation is dirty.
292       (setq temp-char (define-char (cons '(ideograph-daikanwa . 0)
293                                          char-spec)))
294       (remove-char-attribute temp-char 'ideograph-daikanwa)
295       (setq char temp-char))
296     (insert-char-attributes char
297                             readable
298                             (union (mapcar #'car char-spec)
299                                    required-features))
300     (when temp-char
301       ;; undefine temporary character
302       ;;   Current implementation is dirty.
303       (setq char-spec (char-attribute-alist temp-char))
304       (while char-spec
305         (remove-char-attribute temp-char (car (car char-spec)))
306         (setq char-spec (cdr char-spec))))))
307
308 (defun char-db-insert-alist (alist &optional readable column)
309   (unless column
310     (setq column (current-column)))
311   (let ((line-breaking
312          (concat "\n" (make-string (1+ column) ?\ )))
313         name value
314         ret al ; cal
315         key
316         lbs cell rest separator)
317     (insert "(")
318     (while alist
319       (setq name (car (car alist))
320             value (cdr (car alist)))
321       (cond ((eq name 'char)
322              (insert "(char . ")
323              (if (setq ret (condition-case nil
324                                (find-char value)
325                              (error nil)))
326                  (progn
327                    (setq al nil
328                          ;; cal nil
329                          )
330                    (while value
331                      (setq key (car (car value)))
332                      ;; (if (find-charset key)
333                      ;;     (setq cal (cons key cal))
334                      (setq al (cons key al))
335                      ;; )
336                      (setq value (cdr value)))
337                    (insert-char-attributes ret
338                                            readable
339                                            (or al 'none) ; cal
340                                            ))
341                (insert (prin1-to-string value)))
342              (insert ")")
343              (insert line-breaking))
344             ((consp value)
345              (insert (format "(%-18s " name))
346              (setq lbs (concat "\n" (make-string (current-column) ?\ )))
347              (while (consp value)
348                (setq cell (car value))
349                (if (and (consp cell)
350                         (consp (car cell))
351                         (setq ret (condition-case nil
352                                       (find-char cell)
353                                     (error nil)))
354                         )
355                    (progn
356                      (setq rest cell
357                            al nil
358                            ;; cal nil
359                            )
360                      (while rest
361                        (setq key (car (car rest)))
362                        ;; (if (find-charset key)
363                        ;;     (setq cal (cons key cal))
364                        (setq al (cons key al))
365                        ;; )
366                        (setq rest (cdr rest)))
367                      (if separator
368                          (insert lbs))
369                      (insert-char-attributes ret
370                                              readable
371                                              al ; cal
372                                              )
373                      (setq separator lbs))
374                  (if separator
375                      (insert separator))
376                  (insert (prin1-to-string cell))
377                  (setq separator " "))
378                (setq value (cdr value)))
379              (insert ")")
380              (insert line-breaking))
381             (t
382              (insert (format "(%-18s . %S)%s"
383                              name value
384                              line-breaking))))
385       (setq alist (cdr alist))))
386   (insert ")"))
387
388 (defun char-db-insert-char-reference (plist &optional readable column)
389   (unless column
390     (setq column (current-column)))
391   (let ((line-breaking
392          (concat "\n" (make-string (1+ column) ?\ )))
393         (separator "")
394         name value)
395     (insert "(")
396     (while plist
397       (setq name (pop plist))
398       (setq value (pop plist))
399       (cond ((eq name :char)
400              (insert separator)
401              (insert ":char\t")
402              (cond ((numberp value)
403                     (setq value (decode-char '=ucs value)))
404                    ;; ((consp value)
405                    ;;  (setq value (or (find-char value)
406                    ;;                  value)))
407                    )
408              (char-db-insert-char-spec value readable)
409              (insert line-breaking)
410              (setq separator ""))
411             ((eq name :radical)
412              (insert (format "%s%s\t%d ; %c%s"
413                              separator
414                              name value
415                              (ideographic-radical value)
416                              line-breaking))
417              (setq separator ""))
418             (t
419              (insert (format "%s%s\t%S" separator name value))
420              (setq separator line-breaking)))
421       ))
422   (insert ")"))
423
424 (defun char-db-decode-isolated-char (ccs code-point)
425   (let (ret)
426     (setq ret
427           (cond ((eq ccs 'arabic-iso8859-6)
428                  (decode-char ccs code-point))
429                 ((and (memq ccs '(=gt-pj-1
430                                   =gt-pj-2
431                                   =gt-pj-3
432                                   =gt-pj-4
433                                   =gt-pj-5
434                                   =gt-pj-6
435                                   =gt-pj-7
436                                   =gt-pj-8
437                                   =gt-pj-9
438                                   =gt-pj-10
439                                   =gt-pj-11))
440                       (setq ret (decode-char ccs code-point))
441                       (setq ret (encode-char ret '=gt 'defined-only)))
442                  (decode-builtin-char '=gt ret))
443                 (t
444                  (decode-builtin-char ccs code-point))))
445     (cond ((and (<= 0 (char-int ret))
446                 (<= (char-int ret) #x1F))
447            (decode-char '=ucs (+ #x2400 (char-int ret))))
448           ((= (char-int ret) #x7F)
449            ?\u2421)
450           (t ret))))
451
452 (defvar char-db-convert-obsolete-format t)
453
454 (defun char-db-insert-ccs-feature (name value line-breaking)
455   (insert
456    (format
457     (cond ((or (memq name '(=daikanwa
458                             =daikanwa@rev1 =daikanwa@rev2
459                             =gt =gt-k =cbeta =zinbun-oracle))
460                (string-match "^=adobe-" (symbol-name name)))
461            "(%-18s . %05d)\t; %c")
462           ((eq name 'mojikyo)
463            "(%-18s . %06d)\t; %c")
464           ((>= (charset-dimension name) 2)
465            "(%-18s . #x%04X)\t; %c")
466           (t
467            "(%-18s . #x%02X)\t; %c"))
468     name
469     (if (= (charset-iso-graphic-plane name) 1)
470         (logior value
471                 (cond ((= (charset-dimension name) 1)
472                        #x80)
473                       ((= (charset-dimension name) 2)
474                        #x8080)
475                       ((= (charset-dimension name) 3)
476                        #x808080)
477                       (t 0)))
478       value)
479     (char-db-decode-isolated-char name value)))
480   (if (and (= (charset-chars name) 94)
481            (= (charset-dimension name) 2))
482       (insert (format " [%02d-%02d]"
483                       (- (lsh value -8) 32)
484                       (- (logand value 255) 32))))
485   (insert line-breaking))
486
487 (defun char-db-insert-relation-feature (char name value line-breaking
488                                              ccss readable)
489   (insert (format "(%-18s%s " name line-breaking))
490   (let ((lbs (concat "\n" (make-string (current-column) ?\ )))
491         separator cell sources required-features
492         ret)
493     (while (consp value)
494       (setq cell (car value))
495       (if (integerp cell)
496           (setq cell (decode-char '=ucs cell)))
497       (cond
498        ((eq name '->subsumptive)
499         (when (or (not (some (lambda (atr)
500                                (get-char-attribute cell atr))
501                              char-db-ignored-attributes))
502                   (some (lambda (ccs)
503                           (encode-char cell ccs 'defined-only))
504                         ccss))
505           (if separator
506               (insert lbs))
507           (let ((char-db-ignored-attributes
508                  (cons '<-subsumptive
509                        char-db-ignored-attributes)))
510             (insert-char-attributes cell readable))
511           (setq separator lbs))
512         )
513        ((characterp cell)
514         (setq sources
515               (get-char-attribute
516                char (intern (format "%s*sources" name))))
517         (setq required-features nil)
518         (dolist (source sources)
519           (cond
520            ((memq source '(JP JP/Jouyou shinjigen-1))
521             (setq required-features
522                   (union required-features
523                          '(=jis-x0208
524                            =jis-x0208@1990
525                            =jis-x0213-1-2000
526                            =jis-x0213-2-2000
527                            =jis-x0212
528                            =jis-x0208@1983
529                            =jis-x0208@1978))))
530            ((eq source 'CN)
531             (setq required-features
532                   (union required-features
533                          '(=gb2312
534                            =gb12345
535                            =iso-ir165)))))
536           (cond
537            ((find-charset (setq ret (intern (format "=%s" source))))
538             (setq required-features
539                   (cons ret required-features)))
540            (t (setq required-features
541                     (cons source required-features)))))
542         (cond ((string-match "@JP" (symbol-name name))
543                (setq required-features
544                      (union required-features
545                             '(=jis-x0208
546                               =jis-x0208@1990
547                               =jis-x0213-1-2000
548                               =jis-x0213-2-2000
549                               =jis-x0212
550                               =jis-x0208@1983
551                               =jis-x0208@1978))))
552               ((string-match "@CN" (symbol-name name))
553                (setq required-features
554                      (union required-features
555                             '(=gb2312
556                               =gb12345
557                               =iso-ir165)))))
558         (if separator
559             (insert lbs))
560         (if readable
561             (insert (format "%S" cell))
562           (char-db-insert-char-spec cell readable
563                                     nil
564                                     required-features))
565         (setq separator lbs))
566        ((consp cell)
567         (if separator
568             (insert lbs))
569         (if (consp (car cell))
570             (char-db-insert-char-spec cell readable)
571           (char-db-insert-char-reference cell readable))
572         (setq separator lbs))
573        (t
574         (if separator
575             (insert separator))
576         (insert (prin1-to-string cell))
577         (setq separator " ")))
578       (setq value (cdr value)))
579     (insert ")")
580     (insert line-breaking)))
581
582 (defun insert-char-attributes (char &optional readable attributes column)
583   (unless column
584     (setq column (current-column)))
585   (let (name value ; has-long-ccs-name
586         rest
587         radical strokes
588         (line-breaking
589          (concat "\n" (make-string (1+ column) ?\ )))
590         lbs cell separator ret
591         key al cal
592         dest-ccss ; sources required-features
593         ccss)
594     (let (atr-d)
595       (setq attributes
596             (sort (if attributes
597                       (if (consp attributes)
598                           (progn
599                             (dolist (name attributes)
600                               (unless (memq name char-db-ignored-attributes)
601                                 (if (find-charset name)
602                                     (push name ccss))
603                                 (push name atr-d)))
604                             atr-d))
605                     (dolist (name (char-attribute-list))
606                       (unless (memq name char-db-ignored-attributes)
607                         (if (find-charset name)
608                             (push name ccss))
609                         (push name atr-d)))
610                     atr-d)
611                   #'char-attribute-name<)))
612     (insert "(")
613     (when (memq '<-subsumptive attributes)
614       (when readable
615         (when (setq value (get-char-attribute char '<-subsumptive))
616           (char-db-insert-relation-feature char '<-subsumptive value
617                                            line-breaking
618                                            ccss readable)))
619       (setq attributes (delq '<-subsumptive attributes)))
620     (when (and (memq '<-denotational attributes)
621                (setq value (get-char-attribute char '<-denotational)))
622       (char-db-insert-relation-feature char '<-denotational value
623                                        line-breaking
624                                        ccss readable)
625       (setq attributes (delq '<-denotational attributes)))
626     (when (and (memq 'name attributes)
627                (setq value (get-char-attribute char 'name)))
628       (insert (format
629                (if (> (+ (current-column) (length value)) 48)
630                    "(name . %S)%s"
631                  "(name               . %S)%s")
632                value line-breaking))
633       (setq attributes (delq 'name attributes))
634       )
635     (when (and (memq 'name* attributes)
636                (setq value (get-char-attribute char 'name*)))
637       (insert (format
638                (if (> (+ (current-column) (length value)) 48)
639                    "(name* . %S)%s"
640                  "(name*              . %S)%s")
641                value line-breaking))
642       (setq attributes (delq 'name* attributes))
643       )
644     (when (and (memq 'script attributes)
645                (setq value (get-char-attribute char 'script)))
646       (insert (format "(script\t\t%s)%s"
647                       (mapconcat (function prin1-to-string)
648                                  value " ")
649                       line-breaking))
650       (setq attributes (delq 'script attributes))
651       )
652     (dolist (name '(=>ucs =>ucs*))
653       (when (and (memq name attributes)
654                  (setq value (get-char-attribute char name)))
655         (insert (format "(%-18s . #x%04X)\t; %c%s"
656                         name value (decode-char '=ucs value)
657                         line-breaking))
658         (setq attributes (delq name attributes))))
659     (dolist (name '(=>ucs@gb =>ucs@cns =>ucs@jis =>ucs@ks =>ucs@big5))
660       (when (and (memq name attributes)
661                  (setq value (get-char-attribute char name)))
662         (insert (format "(%-18s . #x%04X)\t; %c%s"
663                         name value
664                         (decode-char (intern
665                                       (concat "="
666                                               (substring
667                                                (symbol-name name) 2)))
668                                      value)
669                         line-breaking))
670         (setq attributes (delq name attributes))
671         ))
672     (dolist (name '(=>daikanwa))
673       (when (and (memq name attributes)
674                  (setq value (get-char-attribute char name)))
675         (insert
676          (if (integerp value)
677              (format "(%-18s . %05d)\t; %c%s"
678                      name value (decode-char '=daikanwa value)
679                      line-breaking)
680            (format "(%-18s %s)\t; %c%s"
681                    name
682                    (mapconcat (function prin1-to-string)
683                               value " ")
684                    (char-representative-of-daikanwa char)
685                    line-breaking)))
686         (setq attributes (delq name attributes))))
687     (when (and (memq 'general-category attributes)
688                (setq value (get-char-attribute char 'general-category)))
689       (insert (format
690                "(general-category\t%s) ; %s%s"
691                (mapconcat (lambda (cell)
692                             (format "%S" cell))
693                           value " ")
694                (cond ((rassoc value unidata-normative-category-alist)
695                       "Normative Category")
696                      ((rassoc value unidata-informative-category-alist)
697                       "Informative Category")
698                      (t
699                       "Unknown Category"))
700                line-breaking))
701       (setq attributes (delq 'general-category attributes))
702       )
703     (when (and (memq 'bidi-category attributes)
704                (setq value (get-char-attribute char 'bidi-category)))
705       (insert (format "(bidi-category\t. %S)%s"
706                       value
707                       line-breaking))
708       (setq attributes (delq 'bidi-category attributes))
709       )
710     (unless (or (not (memq 'mirrored attributes))
711                 (eq (setq value (get-char-attribute char 'mirrored 'empty))
712                     'empty))
713       (insert (format "(mirrored\t\t. %S)%s"
714                       value
715                       line-breaking))
716       (setq attributes (delq 'mirrored attributes))
717       )
718     (cond
719      ((and (memq 'decimal-digit-value attributes)
720            (setq value (get-char-attribute char 'decimal-digit-value)))
721       (insert (format "(decimal-digit-value . %S)%s"
722                       value
723                       line-breaking))
724       (setq attributes (delq 'decimal-digit-value attributes))
725       (when (and (memq 'digit-value attributes)
726                  (setq value (get-char-attribute char 'digit-value)))
727         (insert (format "(digit-value\t . %S)%s"
728                         value
729                         line-breaking))
730         (setq attributes (delq 'digit-value attributes))
731         )
732       (when (and (memq 'numeric-value attributes)
733                  (setq value (get-char-attribute char 'numeric-value)))
734         (insert (format "(numeric-value\t . %S)%s"
735                         value
736                         line-breaking))
737         (setq attributes (delq 'numeric-value attributes))
738         )
739       )
740      (t
741       (when (and (memq 'digit-value attributes)
742                  (setq value (get-char-attribute char 'digit-value)))
743         (insert (format "(digit-value\t. %S)%s"
744                         value
745                         line-breaking))
746         (setq attributes (delq 'digit-value attributes))
747         )
748       (when (and (memq 'numeric-value attributes)
749                  (setq value (get-char-attribute char 'numeric-value)))
750         (insert (format "(numeric-value\t. %S)%s"
751                         value
752                         line-breaking))
753         (setq attributes (delq 'numeric-value attributes))
754         )))
755     (when (and (memq 'iso-10646-comment attributes)
756                (setq value (get-char-attribute char 'iso-10646-comment)))
757       (insert (format "(iso-10646-comment\t. %S)%s"
758                       value
759                       line-breaking))
760       (setq attributes (delq 'iso-10646-comment attributes))
761       )
762     (when (and (memq 'morohashi-daikanwa attributes)
763                (setq value (get-char-attribute char 'morohashi-daikanwa)))
764       (insert (format "(morohashi-daikanwa\t%s)%s"
765                       (mapconcat (function prin1-to-string) value " ")
766                       line-breaking))
767       (setq attributes (delq 'morohashi-daikanwa attributes))
768       )
769     (setq radical nil
770           strokes nil)
771     (when (and (memq 'ideographic-radical attributes)
772                (setq value (get-char-attribute char 'ideographic-radical)))
773       (setq radical value)
774       (insert (format "(ideographic-radical . %S)\t; %c%s"
775                       radical
776                       (ideographic-radical radical)
777                       line-breaking))
778       (setq attributes (delq 'ideographic-radical attributes))
779       )
780     (when (and (memq 'shuowen-radical attributes)
781                (setq value (get-char-attribute char 'shuowen-radical)))
782       (insert (format "(shuowen-radical\t. %S)\t; %c%s"
783                       value
784                       (shuowen-radical value)
785                       line-breaking))
786       (setq attributes (delq 'shuowen-radical attributes))
787       )
788     (let (key)
789       (dolist (domain
790                (append
791                 char-db-feature-domains
792                 (let (dest domain)
793                   (dolist (feature (char-attribute-list))
794                     (setq feature (symbol-name feature))
795                     (when (string-match
796                            "\\(radical\\|strokes\\)@\\([^@*]+\\)\\(\\*\\|$\\)"
797                            feature)
798                       (setq domain (intern (match-string 2 feature)))
799                      (unless (memq domain dest)
800                        (setq dest (cons domain dest)))))
801                   (sort dest #'string<))))
802         (setq key (intern (format "%s@%s" 'ideographic-radical domain)))
803         (when (and (memq key attributes)
804                    (setq value (get-char-attribute char key)))
805           (setq radical value)
806           (insert (format "(%s . %S)\t; %c%s"
807                           key
808                           radical
809                           (ideographic-radical radical)
810                           line-breaking))
811           (setq attributes (delq key attributes))
812           )
813         (setq key (intern (format "%s@%s" 'ideographic-strokes domain)))
814         (when (and (memq key attributes)
815                    (setq value (get-char-attribute char key)))
816           (setq strokes value)
817           (insert (format "(%s . %S)%s"
818                           key
819                           strokes
820                           line-breaking))
821           (setq attributes (delq key attributes))
822           )
823         (setq key (intern (format "%s@%s" 'total-strokes domain)))
824         (when (and (memq key attributes)
825                    (setq value (get-char-attribute char key)))
826           (insert (format "(%s       . %S)%s"
827                           key
828                           value
829                           line-breaking))
830           (setq attributes (delq key attributes))
831           )
832         (dolist (feature '(ideographic-radical
833                            ideographic-strokes
834                            total-strokes))
835           (setq key (intern (format "%s@%s*sources" feature domain)))
836           (when (and (memq key attributes)
837                      (setq value (get-char-attribute char key)))
838             (insert (format "(%s%s" key line-breaking))
839             (dolist (cell value)
840               (insert (format " %s" cell)))
841             (insert ")")
842             (insert line-breaking)
843             (setq attributes (delq key attributes))
844             ))
845         ))
846     (when (and (memq 'ideographic-strokes attributes)
847                (setq value (get-char-attribute char 'ideographic-strokes)))
848       (setq strokes value)
849       (insert (format "(ideographic-strokes . %S)%s"
850                       strokes
851                       line-breaking))
852       (setq attributes (delq 'ideographic-strokes attributes))
853       )
854     (when (and (memq 'kangxi-radical attributes)
855                (setq value (get-char-attribute char 'kangxi-radical)))
856       (unless (eq value radical)
857         (insert (format "(kangxi-radical\t . %S)\t; %c%s"
858                         value
859                         (ideographic-radical value)
860                         line-breaking))
861         (or radical
862             (setq radical value)))
863       (setq attributes (delq 'kangxi-radical attributes))
864       )
865     (when (and (memq 'kangxi-strokes attributes)
866                (setq value (get-char-attribute char 'kangxi-strokes)))
867       (unless (eq value strokes)
868         (insert (format "(kangxi-strokes\t . %S)%s"
869                         value
870                         line-breaking))
871         (or strokes
872             (setq strokes value)))
873       (setq attributes (delq 'kangxi-strokes attributes))
874       )
875     (when (and (memq 'japanese-radical attributes)
876                (setq value (get-char-attribute char 'japanese-radical)))
877       (unless (eq value radical)
878         (insert (format "(japanese-radical\t . %S)\t; %c%s"
879                         value
880                         (ideographic-radical value)
881                         line-breaking))
882         (or radical
883             (setq radical value)))
884       (setq attributes (delq 'japanese-radical attributes))
885       )
886     (when (and (memq 'japanese-strokes attributes)
887                (setq value (get-char-attribute char 'japanese-strokes)))
888       (unless (eq value strokes)
889         (insert (format "(japanese-strokes\t . %S)%s"
890                         value
891                         line-breaking))
892         (or strokes
893             (setq strokes value)))
894       (setq attributes (delq 'japanese-strokes attributes))
895       )
896     (when (and (memq 'cns-radical attributes)
897                (setq value (get-char-attribute char 'cns-radical)))
898       (insert (format "(cns-radical\t . %S)\t; %c%s"
899                       value
900                       (ideographic-radical value)
901                       line-breaking))
902       (setq attributes (delq 'cns-radical attributes))
903       )
904     (when (and (memq 'cns-strokes attributes)
905                (setq value (get-char-attribute char 'cns-strokes)))
906       (unless (eq value strokes)
907         (insert (format "(cns-strokes\t . %S)%s"
908                         value
909                         line-breaking))
910         (or strokes
911             (setq strokes value)))
912       (setq attributes (delq 'cns-strokes attributes))
913       )
914     (when (and (memq 'shinjigen-1-radical attributes)
915                (setq value (get-char-attribute char 'shinjigen-1-radical)))
916       (unless (eq value radical)
917         (insert (format "(shinjigen-1-radical . %S)\t; %c%s"
918                         value
919                         (ideographic-radical value)
920                         line-breaking))
921         (or radical
922             (setq radical value)))
923       (setq attributes (delq 'shinjigen-1-radical attributes))
924       )
925     (when (and (memq 'ideographic- attributes)
926                (setq value (get-char-attribute char 'ideographic-)))
927       (insert "(ideographic-       ")
928       (setq lbs (concat "\n" (make-string (current-column) ?\ ))
929             separator nil)
930       (while (consp value)
931         (setq cell (car value))
932         (if (integerp cell)
933             (setq cell (decode-char '=ucs cell)))
934         (cond ((characterp cell)
935                (if separator
936                    (insert lbs))
937                (if readable
938                    (insert (format "%S" cell))
939                  (char-db-insert-char-spec cell readable))
940                (setq separator lbs))
941               ((consp cell)
942                (if separator
943                    (insert lbs))
944                (if (consp (car cell))
945                    (char-db-insert-char-spec cell readable)
946                  (char-db-insert-char-reference cell readable))
947                (setq separator lbs))
948               (t
949                (if separator
950                    (insert separator))
951                (insert (prin1-to-string cell))
952                (setq separator " ")))
953         (setq value (cdr value)))
954       (insert ")")
955       (insert line-breaking)
956       (setq attributes (delq 'ideographic- attributes)))
957     (when (and (memq 'total-strokes attributes)
958                (setq value (get-char-attribute char 'total-strokes)))
959       (insert (format "(total-strokes       . %S)%s"
960                       value
961                       line-breaking))
962       (setq attributes (delq 'total-strokes attributes))
963       )
964     (when (and (memq '->ideograph attributes)
965                (setq value (get-char-attribute char '->ideograph)))
966       (insert (format "(->ideograph\t%s)%s"
967                       (mapconcat (lambda (code)
968                                    (cond ((symbolp code)
969                                           (symbol-name code))
970                                          ((integerp code)
971                                           (format "#x%04X" code))
972                                          (t
973                                           (format "%s %S"
974                                                   line-breaking code))))
975                                  value " ")
976                       line-breaking))
977       (setq attributes (delq '->ideograph attributes))
978       )
979     ;; (when (and (memq '->decomposition attributes)
980     ;;            (setq value (get-char-attribute char '->decomposition)))
981     ;;   (insert (format "(->decomposition\t%s)%s"
982     ;;                   (mapconcat (lambda (code)
983     ;;                                (cond ((symbolp code)
984     ;;                                       (symbol-name code))
985     ;;                                      ((characterp code)
986     ;;                                       (if readable
987     ;;                                           (format "%S" code)
988     ;;                                         (format "#x%04X"
989     ;;                                                 (char-int code))
990     ;;                                         ))
991     ;;                                      ((integerp code)
992     ;;                                       (format "#x%04X" code))
993     ;;                                      (t
994     ;;                                       (format "%s%S" line-breaking code))))
995     ;;                              value " ")
996     ;;                   line-breaking))
997     ;;   (setq attributes (delq '->decomposition attributes))
998     ;;   )
999     (if (equal (get-char-attribute char '->titlecase)
1000                (get-char-attribute char '->uppercase))
1001         (setq attributes (delq '->titlecase attributes)))
1002     (when (and (memq '->mojikyo attributes)
1003                (setq value (get-char-attribute char '->mojikyo)))
1004       (insert (format "(->mojikyo\t\t. %06d)\t; %c%s"
1005                       value (decode-char 'mojikyo value)
1006                       line-breaking))
1007       (setq attributes (delq '->mojikyo attributes))
1008       )
1009     (when (and (memq 'hanyu-dazidian-vol attributes)
1010                (setq value (get-char-attribute char 'hanyu-dazidian-vol)))
1011       (insert (format "(hanyu-dazidian-vol  . %d)%s"
1012                       value line-breaking))
1013       (setq attributes (delq 'hanyu-dazidian-vol attributes))
1014       )
1015     (when (and (memq 'hanyu-dazidian-page attributes)
1016                (setq value (get-char-attribute char 'hanyu-dazidian-page)))
1017       (insert (format "(hanyu-dazidian-page . %d)%s"
1018                       value line-breaking))
1019       (setq attributes (delq 'hanyu-dazidian-page attributes))
1020       )
1021     (when (and (memq 'hanyu-dazidian-char attributes)
1022                (setq value (get-char-attribute char 'hanyu-dazidian-char)))
1023       (insert (format "(hanyu-dazidian-char . %d)%s"
1024                       value line-breaking))
1025       (setq attributes (delq 'hanyu-dazidian-char attributes))
1026       )
1027     (unless readable
1028       (dolist (ignored '(composition
1029                          ->denotational <-subsumptive ->ucs-unified
1030                          ->ideographic-component-forms))
1031         (setq attributes (delq ignored attributes))))
1032     (while attributes
1033       (setq name (car attributes))
1034       (if (setq value (get-char-attribute char name))
1035           (cond ((setq ret (find-charset name))
1036                  (setq name (charset-name ret))
1037                  (if (and (not (memq name dest-ccss))
1038                           (prog1
1039                               (setq value (get-char-attribute char name))
1040                             (setq dest-ccss (cons name dest-ccss))))
1041                      (char-db-insert-ccs-feature name value line-breaking))
1042                  )
1043                 ((string-match "^=>ucs@" (symbol-name name))
1044                  (insert (format "(%-18s . #x%04X)\t; %c%s"
1045                                  name value (decode-char '=ucs value)
1046                                  line-breaking))
1047                  )
1048                 ((eq name 'jisx0208-1978/4X)
1049                  (insert (format "(%-18s . #x%04X)%s"
1050                                  name value
1051                                  line-breaking))
1052                  )
1053                 ((and
1054                   (not readable)
1055                   (not (eq name '->subsumptive))
1056                   (not (eq name '->uppercase))
1057                   (not (eq name '->lowercase))
1058                   (not (eq name '->titlecase))
1059                   (not (eq name '->canonical))
1060                   (not (eq name '->Bopomofo))
1061                   (not (eq name '->mistakable))
1062                   (not (eq name '->ideographic-variants))
1063                   (null (get-char-attribute
1064                          char (intern (format "%s*sources" name))))
1065                   (not (string-match "\\*sources$" (symbol-name name)))
1066                   (null (get-char-attribute
1067                          char (intern (format "%s*note" name))))
1068                   (not (string-match "\\*note$" (symbol-name name)))
1069                   (or (eq name '<-identical)
1070                       (eq name '<-uppercase)
1071                       (eq name '<-lowercase)
1072                       (eq name '<-titlecase)
1073                       (eq name '<-canonical)
1074                       (eq name '<-ideographic-variants)
1075                       ;; (eq name '<-synonyms)
1076                       (string-match "^<-synonyms" (symbol-name name))
1077                       (eq name '<-mistakable)
1078                       (when (string-match "^->" (symbol-name name))
1079                         (cond
1080                          ((string-match "^->fullwidth" (symbol-name name))
1081                           (not (and (consp value)
1082                                     (characterp (car value))
1083                                     (encode-char
1084                                      (car value) '=ucs 'defined-only)))
1085                           )
1086                          (t)))
1087                       ))
1088                  )
1089                 ((or (eq name 'ideographic-structure)
1090                      (eq name 'ideographic-combination)
1091                      (eq name 'ideographic-)
1092                      (eq name '=decomposition)
1093                      (string-match "^=>decomposition" (symbol-name name))
1094                      (string-match "^\\(->\\|<-\\)[^*]*$" (symbol-name name))
1095                      (string-match "^\\(->\\|<-\\)[^*]*\\*sources$"
1096                                    (symbol-name name))
1097                      )
1098                  (char-db-insert-relation-feature char name value
1099                                                   line-breaking
1100                                                   ccss readable))
1101                 ((memq name '(ideograph=
1102                               original-ideograph-of
1103                               ancient-ideograph-of
1104                               vulgar-ideograph-of
1105                               wrong-ideograph-of
1106                               ;; simplified-ideograph-of
1107                               ideographic-variants
1108                               ;; ideographic-different-form-of
1109                               ))
1110                  (insert (format "(%-18s%s " name line-breaking))
1111                  (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1112                        separator nil)
1113                  (while (consp value)
1114                    (setq cell (car value))
1115                    (if (and (consp cell)
1116                             (consp (car cell)))
1117                        (progn
1118                          (if separator
1119                              (insert lbs))
1120                          (char-db-insert-alist cell readable)
1121                          (setq separator lbs))
1122                      (if separator
1123                          (insert separator))
1124                      (insert (prin1-to-string cell))
1125                      (setq separator " "))
1126                    (setq value (cdr value)))
1127                  (insert ")")
1128                  (insert line-breaking))
1129                 ((consp value)
1130                  (insert (format "(%-18s " name))
1131                  (setq lbs (concat "\n" (make-string (current-column) ?\ ))
1132                        separator nil)
1133                  (while (consp value)
1134                    (setq cell (car value))
1135                    (if (and (consp cell)
1136                             (consp (car cell))
1137                             (setq ret (condition-case nil
1138                                           (find-char cell)
1139                                         (error nil))))
1140                        (progn
1141                          (setq rest cell
1142                                al nil
1143                                cal nil)
1144                          (while rest
1145                            (setq key (car (car rest)))
1146                            (if (find-charset key)
1147                                (setq cal (cons key cal))
1148                              (setq al (cons key al)))
1149                            (setq rest (cdr rest)))
1150                          (if separator
1151                              (insert lbs))
1152                          (insert-char-attributes ret
1153                                                  readable
1154                                                  al cal)
1155                          (setq separator lbs))
1156                      (setq ret (prin1-to-string cell))
1157                      (if separator
1158                          (if (< (+ (current-column)
1159                                    (length ret)
1160                                    (length separator))
1161                                 76)
1162                              (insert separator)
1163                            (insert lbs)))
1164                      (insert ret)
1165                      (setq separator " "))
1166                    (setq value (cdr value)))
1167                  (insert ")")
1168                  (insert line-breaking))
1169                 (t
1170                  (insert (format "(%-18s" name))
1171                  (setq ret (prin1-to-string value))
1172                  (unless (< (+ (current-column)
1173                                (length ret)
1174                                3)
1175                             76)
1176                    (insert line-breaking))
1177                  (insert " . " ret ")" line-breaking)
1178                  ;; (insert (format "(%-18s . %S)%s"
1179                  ;;                 name value
1180                  ;;                 line-breaking))
1181                  )
1182                 ))
1183       (setq attributes (cdr attributes)))
1184     (insert ")")))
1185
1186 (defun insert-char-data (char &optional readable
1187                               attributes)
1188   (save-restriction
1189     (narrow-to-region (point)(point))
1190     (insert "(define-char
1191   '")
1192     (insert-char-attributes char readable attributes)
1193     (insert ")\n")
1194     (goto-char (point-min))
1195     (while (re-search-forward "[ \t]+$" nil t)
1196       (replace-match ""))
1197     ;; from tabify.
1198     (goto-char (point-min))
1199     (while (re-search-forward "[ \t][ \t][ \t]*" nil t)
1200       (let ((column (current-column))
1201             (indent-tabs-mode t))
1202         (delete-region (match-beginning 0) (point))
1203         (indent-to column)))
1204     (goto-char (point-max))
1205     ;; (tabify (point-min)(point-max))
1206     ))
1207
1208 (defun insert-char-data-with-variant (char &optional printable
1209                                            no-ucs-unified
1210                                            script excluded-script)
1211   (insert-char-data char printable)
1212   (let ((variants (char-variants char))
1213         rest
1214         variant vs ret)
1215     (setq variants (sort variants #'<))
1216     (setq rest variants)
1217     (setq variants (cons char variants))
1218     (while rest
1219       (setq variant (car rest))
1220       (unless (get-char-attribute variant '<-subsumptive)
1221         (if (and (or (null script)
1222                      (null (setq vs (get-char-attribute variant 'script)))
1223                      (memq script vs))
1224                  (or (null excluded-script)
1225                      (null (setq vs (get-char-attribute variant 'script)))
1226                      (not (memq excluded-script vs))))
1227             (unless (and no-ucs-unified (get-char-attribute variant '=ucs))
1228               (insert-char-data variant printable)
1229               (if (setq ret (char-variants variant))
1230                   (while ret
1231                     (or (memq (car ret) variants)
1232                         ;; (get-char-attribute (car ret) '<-subsumptive)
1233                         (setq rest (nconc rest (list (car ret)))))
1234                     (setq ret (cdr ret)))))))
1235       (setq rest (cdr rest)))))
1236
1237 (defun insert-char-range-data (min max &optional script excluded-script)
1238   (let ((code min)
1239         char)
1240     (while (<= code max)
1241       (setq char (decode-char '=ucs code))
1242       (if (encode-char char '=ucs 'defined-only)
1243           (insert-char-data-with-variant char nil 'no-ucs-unified
1244                                          script excluded-script))
1245       (setq code (1+ code)))))
1246
1247 (defun write-char-range-data-to-file (min max file
1248                                           &optional script excluded-script)
1249   (let ((coding-system-for-write char-db-file-coding-system))
1250     (with-temp-buffer
1251       (insert (format ";; -*- coding: %s -*-\n"
1252                       char-db-file-coding-system))
1253       (insert-char-range-data min max script excluded-script)
1254       (write-region (point-min)(point-max) file))))
1255
1256 (defvar what-character-original-window-configuration)
1257
1258 ;;;###autoload
1259 (defun what-char-definition (char)
1260   (interactive (list (char-after)))
1261   (let ((buf (get-buffer-create "*Character Description*"))
1262         (the-buf (current-buffer))
1263         (win-conf (current-window-configuration)))
1264     (pop-to-buffer buf)
1265     (make-local-variable 'what-character-original-window-configuration)
1266     (setq what-character-original-window-configuration win-conf)
1267     (setq buffer-read-only nil)
1268     (erase-buffer)
1269     (condition-case err
1270         (progn
1271           (insert-char-data-with-variant char 'printable)
1272           (unless (char-attribute-alist char)
1273             (insert (format ";; = %c\n"
1274                             (let* ((rest (split-char char))
1275                                    (ccs (pop rest))
1276                                    (code (pop rest)))
1277                               (while rest
1278                                 (setq code (logior (lsh code 8)
1279                                                    (pop rest))))
1280                               (decode-char ccs code)))))
1281           ;; (char-db-update-comment)
1282           (set-buffer-modified-p nil)
1283           (view-mode the-buf (lambda (buf)
1284                                (set-window-configuration
1285                                 what-character-original-window-configuration)
1286                                ))
1287           (goto-char (point-min)))
1288       (error (progn
1289                (set-window-configuration
1290                 what-character-original-window-configuration)
1291                (signal (car err) (cdr err)))))))
1292
1293 (provide 'char-db-util)
1294
1295 ;;; char-db-util.el ends here