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