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