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