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