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