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