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