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