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