(insert-char-data): If an element of `->decomposition' property is a
[chise/xemacs-chise.git-] / lisp / utf-2000 / char-db-util.el
1 ;;; char-db-util.el --- Character Database utility
2
3 ;; Copyright (C) 1998,1999,2000 MORIOKA Tomohiko.
4
5 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
6 ;; Keywords: UTF-2000, ISO/IEC 10646, Unicode, UCS-4, MULE.
7
8 ;; This file is part of UTF-2000.
9
10 ;; UTF-2000 is free software; you can redistribute it and/or modify it
11 ;; under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; UTF-2000 is distributed in the hope that it will be useful, but
16 ;; 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; see the file COPYING.  If not, write to the Free
22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 ;; 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 (int-char (+ #x2EFF i)))
69       (setq i (1+ i)))
70     (if (< (charset-iso-final-char (car (split-char (aref v 34)))) ?0)
71         (aset v 34 (make-char 'chinese-gb2312 #x62 #x3A)))
72     v))
73
74 (defun char-attribute-name< (ka kb)
75   (cond
76    ((find-charset ka)
77     (cond
78      ((find-charset kb)
79       (cond
80        ((= (charset-dimension ka)
81            (charset-dimension kb))
82         (cond ((= (charset-chars ka)(charset-chars kb))
83                (cond
84                 ((>= (charset-iso-final-char ka) ?@)
85                  (if (>= (charset-iso-final-char kb) ?@)
86                      (< (charset-iso-final-char ka)
87                         (charset-iso-final-char kb))
88                    t))
89                 ((>= (charset-iso-final-char ka) ?0)
90                  (cond
91                   ((>= (charset-iso-final-char kb) ?@)
92                    nil)
93                   ((>= (charset-iso-final-char kb) ?0)
94                    (< (charset-iso-final-char ka)
95                       (charset-iso-final-char kb)))
96                   (t)))))
97               ((<= (charset-chars ka)(charset-chars kb)))))
98        (t
99         (< (charset-dimension ka)
100            (charset-dimension kb))
101         )))
102      ((symbolp kb)
103       nil)
104      (t
105       t)))
106    ((find-charset kb)
107     t)
108    ((symbolp ka)
109     (cond ((symbolp kb)
110            (string< (symbol-name ka)
111                     (symbol-name kb)))
112           (t)))
113    ((symbolp kb)
114     nil)))
115
116 (defun insert-char-data (char)
117   (let ((data (char-attribute-alist char))
118         cell ret has-long-ccs-name rest
119         radical strokes)
120     (when data
121       (save-restriction
122         (narrow-to-region (point)(point))
123         (insert "(define-char
124   '(")
125         (when (setq cell (assq 'name data))
126           (setq cell (cdr cell))
127           (insert (format
128                    (if (> (length cell) 47)
129                        "(name . %S)
130     "
131                      "(name\t\t. %S)
132     ")
133                    cell))
134           (setq data (del-alist 'name data))
135           )
136         (when (setq cell (assq 'script data))
137           (insert (format "(script\t\t%s)
138     "
139                           (mapconcat (function prin1-to-string)
140                                      (cdr cell) " ")))
141           (setq data (del-alist 'script data))
142           )
143         (when (setq cell (assq 'ucs data))
144           (setq cell (cdr cell))
145           (insert (format "(ucs\t\t. #x%04X)
146     "
147                           cell))
148           (setq data (del-alist 'ucs data))
149           )
150         (when (setq cell (assq '->ucs data))
151           (setq cell (cdr cell))
152           (insert (format "(->ucs\t\t. #x%04X)\t; %c
153     "
154                           cell (decode-char 'ucs cell)))
155           (setq data (del-alist '->ucs data))
156           )
157         (when (setq cell (assq 'general-category data))
158           (setq ret (cdr cell))
159           (insert (format
160                    "(general-category\t%s) ; %s
161     "
162                    (mapconcat (lambda (cell)
163                                 (format "%S" cell))
164                               ret " ")
165                    (cond ((rassoc (cdr cell)
166                                   unidata-normative-category-alist)
167                           "Normative Category")
168                          ((rassoc (cdr cell)
169                                   unidata-informative-category-alist)
170                           "Informative Category")
171                          (t
172                           "Unknown Category"))))
173           (setq data (del-alist 'general-category data))
174           )
175         (when (setq cell (assq 'bidi-category data))
176           (setq cell (cdr cell))
177           (insert (format "(bidi-category\t. %S)
178     "
179                           cell))
180           (setq data (del-alist 'bidi-category data))
181           )
182         (when (setq cell (assq 'mirrored data))
183           (setq cell (cdr cell))
184           (insert (format "(mirrored\t\t. %S)
185     "
186                           cell))
187           (setq data (del-alist 'mirrored data))
188           )
189         (cond
190          ((setq cell (assq 'decimal-digit-value data))
191           (setq cell (cdr cell))
192           (insert (format "(decimal-digit-value . %S)
193     "
194                           cell))
195           (setq data (del-alist 'decimal-digit-value data))
196           (when (setq cell (assq 'digit-value data))
197             (setq cell (cdr cell))
198             (insert (format "(digit-value\t . %S)
199     "
200                             cell))
201             (setq data (del-alist 'digit-value data))
202             )
203           (when (setq cell (assq 'numeric-value data))
204             (setq cell (cdr cell))
205             (insert (format "(numeric-value\t . %S)
206     "
207                             cell))
208             (setq data (del-alist 'numeric-value data))
209             )
210           )
211          (t
212           (when (setq cell (assq 'digit-value data))
213             (setq cell (cdr cell))
214             (insert (format "(digit-value\t. %S)
215     "
216                             cell))
217             (setq data (del-alist 'digit-value data))
218             )
219           (when (setq cell (assq 'numeric-value data))
220             (setq cell (cdr cell))
221             (insert (format "(numeric-value\t. %S)
222     "
223                             cell))
224             (setq data (del-alist 'numeric-value data))
225             )))
226         (when (setq cell (assq 'iso-10646-comment data))
227           (setq cell (cdr cell))
228           (insert (format "(iso-10646-comment\t. %S)
229     "
230                           cell))
231           (setq data (del-alist 'iso-10646-comment data))
232           )
233         (when (setq cell (assq 'morohashi-daikanwa data))
234           (setq cell (cdr cell))
235           (insert (format "(morohashi-daikanwa\t%s)
236     "
237                           (mapconcat (function prin1-to-string) cell " ")))
238           (setq data (del-alist 'morohashi-daikanwa data))
239           )
240         (setq radical nil
241               strokes nil)
242         (when (setq cell (assq 'ideographic-radical data))
243           (setq radical (cdr cell))
244           (insert (format "(ideographic-radical . %S)\t; %c
245     "
246                           radical
247                           (aref ideographic-radicals radical)))
248           (setq data (del-alist 'ideographic-radical data))
249           )
250         (when (setq cell (assq 'ideographic-strokes data))
251           (setq strokes (cdr cell))
252           (insert (format "(ideographic-strokes . %S)
253     "
254                           strokes))
255           (setq data (del-alist 'ideographic-strokes data))
256           )
257         (when (setq cell (assq 'kangxi-radical data))
258           (setq cell (cdr cell))
259           (unless (eq cell radical)
260             (insert (format "(kangxi-radical\t . %S)\t; %c
261     "
262                             cell
263                             (aref ideographic-radicals cell)))
264             (setq radical cell))
265           (setq data (del-alist 'kangxi-radical data))
266           )
267         (when (setq cell (assq 'kangxi-strokes data))
268           (setq cell (cdr cell))
269           (unless (eq cell strokes)
270             (insert (format "(kangxi-strokes\t . %S)
271     "
272                             cell))
273             (setq strokes cell))
274           (setq data (del-alist 'kangxi-strokes data))
275           )
276         (when (setq cell (assq 'japanese-radical data))
277           (setq cell (cdr cell))
278           (unless (eq cell radical)
279             (insert (format "(japanese-radical\t . %S)\t; %c
280     "
281                             cell
282                             (aref ideographic-radicals cell)))
283             (setq radical cell))
284           (setq data (del-alist 'japanese-radical data))
285           )
286         (when (setq cell (assq 'japanese-strokes data))
287           (setq cell (cdr cell))
288           (unless (eq cell strokes)
289             (insert (format "(japanese-strokes\t . %S)
290     "
291                             cell))
292             (setq strokes cell))
293           (setq data (del-alist 'japanese-strokes data))
294           )
295         (when (setq cell (assq 'cns-radical data))
296           (setq cell (cdr cell))
297           (insert (format "(cns-radical\t . %S)\t; %c
298     "
299                           cell
300                           (aref ideographic-radicals cell)))
301           (setq data (del-alist 'cns-radical data))
302           )
303         (when (setq cell (assq 'cns-strokes data))
304           (setq cell (cdr cell))
305           (unless (eq cell strokes)
306             (insert (format "(cns-strokes\t . %S)
307     "
308                             cell))
309             (setq strokes cell))
310           (setq data (del-alist 'cns-strokes data))
311           )
312         (when (setq cell (assq 'total-strokes data))
313           (setq cell (cdr cell))
314           (insert (format "(total-strokes\t . %S)
315     "
316                           cell))
317           (setq data (del-alist 'total-strokes data))
318           )
319         (when (setq cell (assq '->ideograph data))
320           (setq cell (cdr cell))
321           (insert (format "(->ideograph\t%s)
322     "
323                           (mapconcat (lambda (code)
324                                        (cond ((symbolp code)
325                                               (symbol-name code))
326                                              ((integerp code)
327                                               (format "#x%04X" code))
328                                              (t
329                                               (format "\n     %S" code))))
330                                      cell " ")))
331           (setq data (del-alist '->ideograph data))
332           )
333         (when (setq cell (assq '->decomposition data))
334           (setq cell (cdr cell))
335           (insert (format "(->decomposition\t%s)
336     "
337                           (mapconcat (lambda (code)
338                                        (cond ((symbolp code)
339                                               (symbol-name code))
340                                              ((characterp code)
341                                               (format "%S" code))
342                                              ((integerp code)
343                                               (format "#x%04X" code))
344                                              (t
345                                               (format "\n     %S" code))))
346                                      cell " ")))
347           (setq data (del-alist '->decomposition data))
348           )
349         (when (setq cell (assq '->uppercase data))
350           (setq cell (cdr cell))
351           (insert (format "(->uppercase\t%s)
352     "
353                           (mapconcat (lambda (code)
354                                        (cond ((symbolp code)
355                                               (symbol-name code))
356                                              ((integerp code)
357                                               (format "#x%04X" code))
358                                              (t
359                                               (format "\n     %S" code))))
360                                      cell " ")))
361           (setq data (del-alist '->uppercase data))
362           )
363         (when (setq cell (assq '->lowercase data))
364           (setq cell (cdr cell))
365           (insert (format "(->lowercase\t%s)
366     "
367                           (mapconcat (lambda (code)
368                                        (cond ((symbolp code)
369                                               (symbol-name code))
370                                              ((integerp code)
371                                               (format "#x%04X" code))
372                                              (t
373                                               (format "\n     %S" code))))
374                                      cell " ")))
375           (setq data (del-alist '->lowercase data))
376           )
377         (when (setq cell (assq '->titlecase data))
378           (setq cell (cdr cell))
379           (insert (format "(->titlecase\t%s)
380     "
381                           (mapconcat (lambda (code)
382                                        (cond ((symbolp code)
383                                               (symbol-name code))
384                                              ((integerp code)
385                                               (format "#x%04X" code))
386                                              (t
387                                               (format "\n     %S" code))))
388                                      cell " ")))
389           (setq data (del-alist '->titlecase data))
390           )
391         (setq data
392               (sort data
393                     (lambda (a b)
394                       (char-attribute-name< (car a)(car b)))))
395         (setq rest data)
396         (while (and rest
397                     (progn
398                       (setq cell (car rest))
399                       (if (setq ret (find-charset (car cell)))
400                           (if (>= (length (symbol-name (charset-name ret))) 19)
401                               (progn
402                                 (setq has-long-ccs-name t)
403                                 nil)
404                             t)
405                         t)))
406           (setq rest (cdr rest)))
407         (while data
408           (setq cell (car data))
409           (cond ((setq ret (find-charset (car cell)))
410                  (or (string-match "^mojikyo-pj-"
411                                    (symbol-name (charset-name ret)))
412                      (insert
413                       (format
414                        (if has-long-ccs-name
415                            (if (memq ret
416                                      (list (find-charset 'ideograph-daikanwa)
417                                            (find-charset 'mojikyo)))
418                                "(%-26s . %05d)\t; %c
419     "
420                              "(%-26s . #x%X)\t; %c
421     "
422                              )
423                          (if (memq ret
424                                    (list (find-charset 'ideograph-daikanwa)
425                                          (find-charset 'mojikyo)))
426                              "(%-18s . %05d)\t; %c
427     "
428                            "(%-18s . #x%X)\t; %c
429     "
430                            ))
431                        (charset-name ret)
432                        (if (= (charset-iso-graphic-plane ret) 1)
433                            (logior (cdr cell)
434                                    (cond ((= (charset-dimension ret) 1)
435                                           #x80)
436                                          ((= (charset-dimension ret) 2)
437                                           #x8080)
438                                          ((= (charset-dimension ret) 3)
439                                           #x808080)
440                                          (t 0)))
441                          (cdr cell))
442                        (decode-builtin-char ret (cdr cell))))))
443                 ((string-match "^->" (symbol-name (car cell)))
444                  (insert
445                   (format "(%-18s %s)
446     "
447                           (car cell)
448                           (mapconcat (lambda (code)
449                                        (cond ((symbolp code)
450                                               (symbol-name code))
451                                              ((integerp code)
452                                               (format "#x%04X" code))
453                                              (t
454                                               (format "\n     %S" code))))
455                                      (cdr cell) " "))))
456                 ((consp (cdr cell))
457                  (insert (format "(%-18s %s)
458     "
459                                  (car cell)
460                                  (mapconcat (function prin1-to-string)
461                                             (cdr cell) " "))))
462                 ((eq (car cell) 'jisx0208-1978/4X)
463                  (insert (format "(%-18s . #x%04X)
464     "
465                                  (car cell)(cdr cell))))
466                 (t
467                  (insert (format "(%-18s . %S)
468     "
469                                  (car cell)(cdr cell)))
470                  ))
471           (setq data (cdr data)))
472         (insert "))\n")
473         (goto-char (point-min))
474         (while (re-search-forward "[ \t]+$" nil t)
475           (replace-match ""))
476         (goto-char (point-max))
477         (tabify (point-min)(point-max))
478         ))))
479
480 (defun decode-builtin-char (charset code-point)
481   (setq charset (get-charset charset))
482   (if (and (not (memq (charset-name charset)
483                       '(ideograph-daikanwa mojikyo)))
484            (or (memq (charset-name charset)
485                      '(ascii latin-viscii-upper
486                              latin-viscii-lower
487                              arabic-iso8859-6
488                              japanese-jisx0213-1
489                              japanese-jisx0213-2))
490                (= (char-int (charset-iso-final-char charset)) 0)))
491       (decode-char charset code-point)
492     (let ((table (charset-mapping-table charset)))
493       (if table
494           (prog2
495               (set-charset-mapping-table charset nil)
496               (decode-char charset code-point)
497             (set-charset-mapping-table charset table))
498         (decode-char charset code-point)))))
499
500 ;;;###autoload
501 (defun char-db-update-comment ()
502   (interactive)
503   (save-excursion
504     (goto-char (point-min))
505     (let (cdef table char)
506       (while (re-search-forward "^[ \t]*\\(([^.()]+)\\)" nil t)
507         (goto-char (match-beginning 1))
508         (setq cdef (read (current-buffer)))
509         (when (find-charset (car cdef))
510           (goto-char (match-end 0))
511           (setq char
512                 (if (and
513                      (not (eq (car cdef) 'ideograph-daikanwa))
514                      (or (memq (car cdef) '(ascii latin-viscii-upper
515                                                   latin-viscii-lower
516                                                   arabic-iso8859-6
517                                                   japanese-jisx0213-1
518                                                   japanese-jisx0213-2))
519                          (= (char-int (charset-iso-final-char (car cdef)))
520                             0)))
521                     (apply (function make-char) cdef)
522                   (if (setq table (charset-mapping-table (car cdef)))
523                       (set-charset-mapping-table (car cdef) nil))
524                   (prog1
525                       (apply (function make-char) cdef)
526                     (if table
527                         (set-charset-mapping-table (car cdef) table)))))
528           (when (not (or (< (char-int char) 32)
529                          (and (<= 128 (char-int char))
530                               (< (char-int char) 160))))
531             (delete-region (point) (point-at-eol))
532             (insert (format "\t; %c" char)))
533           )))))
534
535 (defun insert-char-data-with-variant (char &optional script)
536   (insert-char-data char)
537   (let ((variants (or (char-variants char)
538                       (let ((ucs (get-char-attribute char '->ucs)))
539                         (if ucs
540                             (delete char (char-variants (int-char ucs)))))))
541         variant vs)
542     (while variants
543       (setq variant (car variants))
544       (if (or (null script)
545               (null (setq vs (get-char-attribute variant 'script)))
546               (memq script vs))
547           (insert-char-data variant))
548       (setq variants (cdr variants))
549       )))
550
551 (defun insert-char-range-data (min max &optional script)
552   (let ((code min)
553         char)
554     (while (<= code max)
555       (setq char (int-char code))
556       (insert-char-data-with-variant char script)
557       (setq code (1+ code))
558       )))
559
560 (defun write-char-range-data-to-file (min max file &optional script)
561   (let ((coding-system-for-write 'utf-8))
562     (with-temp-buffer
563       (insert-char-range-data min max script)
564       (write-region (point-min)(point-max) file))))
565
566 (defvar what-character-original-window-configuration)
567
568 ;;;###autoload
569 (defun what-char-definition (char)
570   (interactive (list (char-after)))
571   (let ((buf (get-buffer-create "*Character Description*"))
572         (the-buf (current-buffer))
573         (win-conf (current-window-configuration)))
574     (pop-to-buffer buf)
575     (make-local-variable 'what-character-original-window-configuration)
576     (setq what-character-original-window-configuration win-conf)
577     (setq buffer-read-only nil)
578     (erase-buffer)
579     (condition-case err
580         (progn
581           (insert-char-data-with-variant char)
582           ;; (char-db-update-comment)
583           (set-buffer-modified-p nil)
584           (view-mode the-buf (lambda (buf)
585                                (set-window-configuration
586                                 what-character-original-window-configuration)
587                                ))
588           (goto-char (point-min)))
589       (error (progn
590                (set-window-configuration
591                 what-character-original-window-configuration)
592                (signal (car err) (cdr err)))))))
593
594 (provide 'char-db-util)
595
596 ;;; char-db-util.el ends here