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