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