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