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