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