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