(insert-char-attributes): Ignore `<-unified'.
[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       (dolist (ignored '(composition
861                          ->denotational <-unified ->ucs-unified))
862         (setq attributes (delq ignored attributes))))
863     (setq rest ccs-attributes)
864     (while (and rest
865                 (progn
866                   (setq value (get-char-attribute char (car rest)))
867                   (if value
868                       (if (>= (length (symbol-name (car rest))) 19)
869                           (progn
870                             (setq has-long-ccs-name t)
871                             nil)
872                         t)
873                     t)))
874       (setq rest (cdr rest)))
875     (while attributes
876       (setq name (car attributes))
877       (if (setq value (get-char-attribute char name))
878           (cond ((string-match "^=>ucs@" (symbol-name name))
879                  (insert (format "(%-18s . #x%04X)\t; %c%s"
880                                  name value (decode-char '=ucs value)
881                                  line-breaking))
882                  )
883                 ((eq name 'jisx0208-1978/4X)
884                  (insert (format "(%-18s . #x%04X)%s"
885                                  name value
886                                  line-breaking)))
887                 ((or (eq name 'ideographic-structure)
888                      (eq name 'ideographic-)
889                      (string-match "^\\(->\\|<-\\)" (symbol-name name)))
890                  (insert (format "(%-18s%s " name line-breaking))
891                  (setq lbs (concat "\n" (make-string (current-column) ?\ ))
892                        separator nil)
893                  (while (consp value)
894                    (setq cell (car value))
895                    (if (integerp cell)
896                        (setq cell (decode-char '=ucs cell)))
897                    (cond ((eq name '->unified)
898                           (if separator
899                               (insert lbs))
900                           (let ((char-db-ignored-attributes
901                                  (cons '<-unified
902                                        char-db-ignored-attributes)))
903                             (insert-char-attributes cell readable))
904                           (setq separator lbs))
905                          ((characterp cell)
906                           (setq sources
907                                 (get-char-attribute
908                                  char
909                                  (intern (format "%s*sources" name))))
910                           (setq required-features nil)
911                           (dolist (source sources)
912                             (setq required-features
913                                   (cons
914                                    (if (find-charset
915                                         (setq ret (intern
916                                                    (format "=%s" source))))
917                                        ret
918                                      source)
919                                    required-features)))
920                           (when (string-match "@JP" (symbol-name name))
921                             (setq required-features
922                                   (union required-features
923                                          '(=jis-x0208
924                                            =jis-x0208-1990
925                                            =jis-x0213-1-2000
926                                            =jis-x0213-2-2000
927                                            =jis-x0212
928                                            =jis-x0208-1983
929                                            =jis-x0208-1978))))
930                           (if separator
931                               (insert lbs))
932                           (if readable
933                               (insert (format "%S" cell))
934                             (char-db-insert-char-spec cell readable
935                                                       nil
936                                                       required-features))
937                           (setq separator lbs))
938                          ((consp cell)
939                           (if separator
940                               (insert lbs))
941                           (if (consp (car cell))
942                               (char-db-insert-char-spec cell readable)
943                             (char-db-insert-char-reference cell readable))
944                           (setq separator lbs))
945                          (t
946                           (if separator
947                               (insert separator))
948                           (insert (prin1-to-string cell))
949                           (setq separator " ")))
950                    (setq value (cdr value)))
951                  (insert ")")
952                  (insert line-breaking))
953                 ((memq name '(ideograph=
954                               original-ideograph-of
955                               ancient-ideograph-of
956                               vulgar-ideograph-of
957                               wrong-ideograph-of
958                               ;; simplified-ideograph-of
959                               ideographic-variants
960                               ;; ideographic-different-form-of
961                               ))
962                  (insert (format "(%-18s%s " name line-breaking))
963                  (setq lbs (concat "\n" (make-string (current-column) ?\ ))
964                        separator nil)
965                  (while (consp value)
966                    (setq cell (car value))
967                    (if (and (consp cell)
968                             (consp (car cell)))
969                        (progn
970                          (if separator
971                              (insert lbs))
972                          (char-db-insert-alist cell readable)
973                          (setq separator lbs))
974                      (if separator
975                          (insert separator))
976                      (insert (prin1-to-string cell))
977                      (setq separator " "))
978                    (setq value (cdr value)))
979                  (insert ")")
980                  (insert line-breaking))
981                 ((consp value)
982                  (insert (format "(%-18s " name))
983                  (setq lbs (concat "\n" (make-string (current-column) ?\ ))
984                        separator nil)
985                  (while (consp value)
986                    (setq cell (car value))
987                    (if (and (consp cell)
988                             (consp (car cell))
989                             (setq ret (condition-case nil
990                                           (find-char cell)
991                                         (error nil))))
992                        (progn
993                          (setq rest cell
994                                al nil
995                                cal nil)
996                          (while rest
997                            (setq key (car (car rest)))
998                            (if (find-charset key)
999                                (setq cal (cons key cal))
1000                              (setq al (cons key al)))
1001                            (setq rest (cdr rest)))
1002                          (if separator
1003                              (insert lbs))
1004                          (insert-char-attributes ret
1005                                                  readable
1006                                                  al cal)
1007                          (setq separator lbs))
1008                      (setq ret (prin1-to-string cell))
1009                      (if separator
1010                          (if (< (+ (current-column)
1011                                    (length ret)
1012                                    (length separator))
1013                                 76)
1014                              (insert separator)
1015                            (insert lbs)))
1016                      (insert ret)
1017                      (setq separator " "))
1018                    (setq value (cdr value)))
1019                  (insert ")")
1020                  (insert line-breaking))
1021                 (t
1022                  (insert (format "(%-18s . %S)%s"
1023                                  name value
1024                                  line-breaking)))
1025                 ))
1026       (setq attributes (cdr attributes)))
1027     (while ccs-attributes
1028       (setq name (charset-name (car ccs-attributes)))
1029       (if (and (not (memq name dest-ccss))
1030                (prog1
1031                    (setq value (get-char-attribute char name))
1032                  (setq dest-ccss (cons name dest-ccss))))
1033           (insert
1034            (format
1035             (cond ((memq name '(=daikanwa
1036                                 =daikanwa-rev1 =daikanwa-rev2
1037                                 =gt =gt-k =cbeta))
1038                    (if has-long-ccs-name
1039                        "(%-26s . %05d)\t; %c%s"
1040                      "(%-18s . %05d)\t; %c%s"))
1041                   ((eq name 'mojikyo)
1042                    (if has-long-ccs-name
1043                        "(%-26s . %06d)\t; %c%s"
1044                      "(%-18s . %06d)\t; %c%s"))
1045                   ((>= (charset-dimension name) 2)
1046                    (if has-long-ccs-name
1047                        "(%-26s . #x%04X)\t; %c%s"
1048                      "(%-18s . #x%04X)\t; %c%s"))
1049                   (t
1050                    (if has-long-ccs-name
1051                        "(%-26s . #x%02X)\t; %c%s"
1052                      "(%-18s . #x%02X)\t; %c%s")))
1053             name
1054             (if (= (charset-iso-graphic-plane name) 1)
1055                 (logior value
1056                         (cond ((= (charset-dimension name) 1)
1057                                #x80)
1058                               ((= (charset-dimension name) 2)
1059                                #x8080)
1060                               ((= (charset-dimension name) 3)
1061                                #x808080)
1062                               (t 0)))
1063               value)
1064             (char-db-decode-isolated-char name value)
1065             line-breaking)))
1066       (setq ccs-attributes (cdr ccs-attributes)))
1067     (insert ")")))
1068
1069 (defun insert-char-data (char &optional readable
1070                               attributes ccs-attributes)
1071   (save-restriction
1072     (narrow-to-region (point)(point))
1073     (insert "(define-char
1074   '")
1075     (insert-char-attributes char readable
1076                             attributes ccs-attributes)
1077     (insert ")\n")
1078     (goto-char (point-min))
1079     (while (re-search-forward "[ \t]+$" nil t)
1080       (replace-match ""))
1081     ;; from tabify.
1082     (goto-char (point-min))
1083     (while (re-search-forward "[ \t][ \t][ \t]*" nil t)
1084       (let ((column (current-column))
1085             (indent-tabs-mode t))
1086         (delete-region (match-beginning 0) (point))
1087         (indent-to column)))
1088     (goto-char (point-max))
1089     ;; (tabify (point-min)(point-max))
1090     ))
1091
1092 (defun insert-char-data-with-variant (char &optional printable
1093                                            no-ucs-unified
1094                                            script excluded-script)
1095   (insert-char-data char printable)
1096   (let ((variants (or (char-variants char)
1097                       (let ((ucs (get-char-attribute char '->ucs)))
1098                         (if ucs
1099                             (delete char (char-variants (int-char ucs)))))))
1100         variant vs)
1101     (setq variants (sort variants #'<))
1102     (while variants
1103       (setq variant (car variants))
1104       (if (and (or (null script)
1105                    (null (setq vs (get-char-attribute variant 'script)))
1106                    (memq script vs))
1107                (or (null excluded-script)
1108                    (null (setq vs (get-char-attribute variant 'script)))
1109                    (not (memq excluded-script vs))))
1110           (or (and no-ucs-unified (get-char-attribute variant '=ucs))
1111               (insert-char-data variant printable)))
1112       (setq variants (cdr variants))
1113       )))
1114
1115 (defun insert-char-range-data (min max &optional script excluded-script)
1116   (let ((code min)
1117         char)
1118     (while (<= code max)
1119       (setq char (decode-char '=ucs code))
1120       (if (encode-char char '=ucs 'defined-only)
1121           (insert-char-data-with-variant char nil 'no-ucs-unified
1122                                          script excluded-script))
1123       (setq code (1+ code)))))
1124
1125 (defun write-char-range-data-to-file (min max file
1126                                           &optional script excluded-script)
1127   (let ((coding-system-for-write 'utf-8-mcs))
1128     (with-temp-buffer
1129       (insert ";; -*- coding: utf-8-mcs -*-\n")
1130       (insert-char-range-data min max script excluded-script)
1131       (write-region (point-min)(point-max) file))))
1132
1133 (defvar what-character-original-window-configuration)
1134
1135 ;;;###autoload
1136 (defun what-char-definition (char)
1137   (interactive (list (char-after)))
1138   (let ((buf (get-buffer-create "*Character Description*"))
1139         (the-buf (current-buffer))
1140         (win-conf (current-window-configuration)))
1141     (pop-to-buffer buf)
1142     (make-local-variable 'what-character-original-window-configuration)
1143     (setq what-character-original-window-configuration win-conf)
1144     (setq buffer-read-only nil)
1145     (erase-buffer)
1146     (condition-case err
1147         (progn
1148           (insert-char-data-with-variant char 'printable)
1149           (unless (char-attribute-alist char)
1150             (insert (format ";; = %c\n"
1151                             (let* ((rest (split-char char))
1152                                    (ccs (pop rest))
1153                                    (code (pop rest)))
1154                               (while rest
1155                                 (setq code (logior (lsh code 8)
1156                                                    (pop rest))))
1157                               (decode-char ccs code)))))
1158           ;; (char-db-update-comment)
1159           (set-buffer-modified-p nil)
1160           (view-mode the-buf (lambda (buf)
1161                                (set-window-configuration
1162                                 what-character-original-window-configuration)
1163                                ))
1164           (goto-char (point-min)))
1165       (error (progn
1166                (set-window-configuration
1167                 what-character-original-window-configuration)
1168                (signal (car err) (cdr err)))))))
1169
1170 (provide 'char-db-util)
1171
1172 ;;; char-db-util.el ends here