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