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