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