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