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