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