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