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