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