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