Sync with r21-2-19-utf-2000-0_13-0.
[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 MORIOKA Tomohiko.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
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 'name data))
137           (setq cell (cdr cell))
138           (insert (format
139                    (if (> (length cell) 47)
140                        "(name . %S)
141     "
142                      "(name\t\t. %S)
143     ")
144                    cell))
145           (setq data (del-alist 'name data))
146           )
147         (when (setq cell (assq 'ucs data))
148           (setq cell (cdr cell))
149           (insert (format "(ucs\t\t. #x%04X)
150     "
151                           cell))
152           (setq data (del-alist 'ucs data))
153           )
154         (when (setq cell (assq '->ucs data))
155           (setq cell (cdr cell))
156           (insert (format "(->ucs\t\t. #x%04X)
157     "
158                           cell))
159           (setq data (del-alist '->ucs data))
160           )
161         (when (setq cell (assq 'general-category data))
162           (setq ret (cdr cell))
163           (insert (format
164                    "(general-category\t%s) ; %s
165     "
166                    (mapconcat (lambda (cell)
167                                 (format "%S" cell))
168                               ret " ")
169                    (cond ((rassoc (cdr cell)
170                                   unidata-normative-category-alist)
171                           "Normative Category")
172                          ((rassoc (cdr cell)
173                                   unidata-informative-category-alist)
174                           "Informative Category")
175                          (t
176                           "Unknown Category"))))
177           (setq data (del-alist 'general-category data))
178           )
179         (when (setq cell (assq 'bidi-category data))
180           (setq cell (cdr cell))
181           (insert (format "(bidi-category\t. %S)
182     "
183                           cell))
184           (setq data (del-alist 'bidi-category data))
185           )
186         (when (setq cell (assq 'mirrored data))
187           (setq cell (cdr cell))
188           (insert (format "(mirrored\t\t. %S)
189     "
190                           cell))
191           (setq data (del-alist 'mirrored data))
192           )
193         (cond
194          ((setq cell (assq 'decimal-digit-value data))
195           (setq cell (cdr cell))
196           (insert (format "(decimal-digit-value . %S)
197     "
198                           cell))
199           (setq data (del-alist 'decimal-digit-value data))
200           (when (setq cell (assq 'digit-value data))
201             (setq cell (cdr cell))
202             (insert (format "(digit-value\t . %S)
203     "
204                             cell))
205             (setq data (del-alist 'digit-value data))
206             )
207           (when (setq cell (assq 'numeric-value data))
208             (setq cell (cdr cell))
209             (insert (format "(numeric-value\t . %S)
210     "
211                             cell))
212             (setq data (del-alist 'numeric-value data))
213             )
214           )
215          (t
216           (when (setq cell (assq 'digit-value data))
217             (setq cell (cdr cell))
218             (insert (format "(digit-value\t. %S)
219     "
220                             cell))
221             (setq data (del-alist 'digit-value data))
222             )
223           (when (setq cell (assq 'numeric-value data))
224             (setq cell (cdr cell))
225             (insert (format "(numeric-value\t. %S)
226     "
227                             cell))
228             (setq data (del-alist 'numeric-value data))
229             )))
230         (when (setq cell (assq 'iso-10646-comment data))
231           (setq cell (cdr cell))
232           (insert (format "(iso-10646-comment\t. %S)
233     "
234                           cell))
235           (setq data (del-alist 'iso-10646-comment data))
236           )
237         (when (setq cell (assq 'morohashi-daikanwa data))
238           (setq cell (cdr cell))
239           (insert (format "(morohashi-daikanwa\t%s)
240     "
241                           (mapconcat (function prin1-to-string) cell " ")))
242           (setq data (del-alist 'morohashi-daikanwa data))
243           )
244         (setq radical nil)
245         (when (setq cell (assq 'ideographic-radical data))
246           (setq radical (cdr cell))
247           (insert (format "(ideographic-radical . %S)\t; %c
248     "
249                           radical
250                           (aref ideographic-radicals radical)))
251           (setq data (del-alist 'ideographic-radical data))
252           )
253         (when (setq cell (assq 'kangxi-radical data))
254           (setq cell (cdr cell))
255           (unless (eq cell radical)
256             (insert (format "(kangxi-radical\t . %S)\t; %c
257     "
258                             cell
259                             (aref ideographic-radicals cell)))
260             (setq radical cell))
261           (setq data (del-alist 'kangxi-radical data))
262           )
263         (when (setq cell (assq 'japanese-radical data))
264           (setq cell (cdr cell))
265           (unless (eq cell radical)
266             (insert (format "(japanese-radical . %S)\t; %c
267     "
268                             cell
269                             (aref ideographic-radicals cell)))
270             (setq radical cell))
271           (setq data (del-alist 'japanese-radical data))
272           )
273         (when (setq cell (assq 'cns-radical data))
274           (setq cell (cdr cell))
275           (insert (format "(cns-radical\t . %S)\t; %c
276     "
277                           cell
278                           (aref ideographic-radicals cell)))
279           (setq data (del-alist 'cns-radical data))
280           )
281         (setq strokes nil)
282         (cond
283          ((setq cell (assq 'ideographic-strokes data))
284           (setq strokes (cdr cell))
285           (insert (format "(ideographic-strokes . %S)
286     "
287                           strokes))
288           (setq data (del-alist 'ideographic-strokes data))
289           (when (setq cell (assq 'kangxi-strokes data))
290             (setq cell (cdr cell))
291             (unless (eq cell strokes)
292               (insert (format "(kangxi-strokes\t . %S)
293     "
294                               cell))
295               (setq strokes cell))
296             (setq data (del-alist 'kangxi-strokes data))
297             )
298           (when (setq cell (assq 'japanese-strokes data))
299             (setq cell (cdr cell))
300             (unless (eq cell strokes)
301               (insert (format "(japanese-strokes\t . %S)
302     "
303                               cell))
304               (setq strokes cell))
305             (setq data (del-alist 'japanese-strokes data))
306             )
307           (when (setq cell (assq 'total-strokes data))
308             (setq cell (cdr cell))
309             (insert (format "(total-strokes\t . %S)
310     "
311                             cell))
312             (setq data (del-alist 'total-strokes data))
313             )
314           )
315          ((setq cell (assq 'total-strokes data))
316           (setq cell (cdr cell))
317           (insert (format "(total-strokes\t. %S)
318     "
319                           cell))
320           (setq data (del-alist 'total-strokes data))
321           ))
322         (when (setq cell (assq '->decomposition data))
323           (setq cell (cdr cell))
324           (insert (format "(->decomposition\t%s)
325     "
326                           (mapconcat (lambda (code)
327                                        (cond ((symbolp code)
328                                               (symbol-name code))
329                                              ((integerp code)
330                                               (format "#x%04X" code))
331                                              (t
332                                               (format "\n     %S" code))))
333                                      cell " ")))
334           (setq data (del-alist '->decomposition data))
335           )
336         (when (setq cell (assq '->uppercase data))
337           (setq cell (cdr cell))
338           (insert (format "(->uppercase\t%s)
339     "
340                           (mapconcat (lambda (code)
341                                        (cond ((symbolp code)
342                                               (symbol-name code))
343                                              ((integerp code)
344                                               (format "#x%04X" code))
345                                              (t
346                                               (format "\n     %S" code))))
347                                      cell " ")))
348           (setq data (del-alist '->uppercase data))
349           )
350         (when (setq cell (assq '->lowercase data))
351           (setq cell (cdr cell))
352           (insert (format "(->lowercase\t%s)
353     "
354                           (mapconcat (lambda (code)
355                                        (cond ((symbolp code)
356                                               (symbol-name code))
357                                              ((integerp code)
358                                               (format "#x%04X" code))
359                                              (t
360                                               (format "\n     %S" code))))
361                                      cell " ")))
362           (setq data (del-alist '->lowercase data))
363           )
364         (when (setq cell (assq '->titlecase data))
365           (setq cell (cdr cell))
366           (insert (format "(->titlecase\t%s)
367     "
368                           (mapconcat (lambda (code)
369                                        (cond ((symbolp code)
370                                               (symbol-name code))
371                                              ((integerp code)
372                                               (format "#x%04X" code))
373                                              (t
374                                               (format "\n     %S" code))))
375                                      cell " ")))
376           (setq data (del-alist '->titlecase data))
377           )
378         (setq data
379               (sort data
380                     (lambda (a b)
381                       (char-attribute-name< (car a)(car b)))))
382         (setq rest data)
383         (while (and rest
384                     (progn
385                       (setq cell (car rest))
386                       (if (setq ret (find-charset (car cell)))
387                           (if (>= (length (symbol-name (charset-name ret))) 19)
388                               (progn
389                                 (setq has-long-ccs-name t)
390                                 nil)
391                             t)
392                         t)))
393           (setq rest (cdr rest)))
394         (while data
395           (setq cell (car data))
396           (cond ((setq ret (find-charset (car cell)))
397                  (insert
398                   (format
399                    (if has-long-ccs-name
400                        "(%-26s %s)
401     "
402                      "(%-18s %s)
403     "
404                      )
405                    (charset-name ret)
406                    (mapconcat
407                     (lambda (b)
408                       (format "#x%02X"
409                               (if (= (charset-iso-graphic-plane ret) 1)
410                                   (logior b 128)
411                                 b)))
412                     (cdr cell) " "))))
413                 ((string-match "^->" (symbol-name (car cell)))
414                  (insert
415                   (format "(%-18s %s)
416     "
417                           (car cell)
418                           (mapconcat (lambda (code)
419                                        (cond ((symbolp code)
420                                               (symbol-name code))
421                                              ((integerp code)
422                                               (format "#x%04X" code))
423                                              (t
424                                               (format "\n     %S" code))))
425                                      (cdr cell) " "))))
426                 ((consp (cdr cell))
427                  (insert (format "(%-18s %s)
428     "
429                                  (car cell)
430                                  (mapconcat (function prin1-to-string)
431                                             (cdr cell) " "))))
432                 (t
433                  (insert (format "(%-18s . %S)
434     "
435                                  (car cell)(cdr cell)))
436                  ))
437           (setq data (cdr data)))
438         (insert "))\n")
439         (goto-char (point-min))
440         (while (re-search-forward "[ \t]+$" nil t)
441           (replace-match ""))
442         (goto-char (point-max))
443         (tabify (point-min)(point-max))
444         ))))
445
446 ;;;###autoload
447 (defun char-db-update-comment ()
448   (interactive)
449   (save-excursion
450     (goto-char (point-min))
451     (let (cdef table char)
452       (while (re-search-forward "^[ \t]*\\(([^.()]+)\\)" nil t)
453         (goto-char (match-beginning 1))
454         (setq cdef (read (current-buffer)))
455         (when (find-charset (car cdef))
456           (goto-char (match-end 0))
457           (setq char
458                 (if (and
459                      (not (eq (car cdef) 'ideograph-daikanwa))
460                      (or (memq (car cdef) '(ascii latin-viscii-upper
461                                                   latin-viscii-lower
462                                                   arabic-iso8859-6
463                                                   japanese-jisx0213-1
464                                                   japanese-jisx0213-2))
465                          (= (char-int (charset-iso-final-char (car cdef)))
466                             0)))
467                     (apply (function make-char) cdef)
468                   (if (setq table (charset-mapping-table (car cdef)))
469                       (set-charset-mapping-table (car cdef) nil))
470                   (prog1
471                       (apply (function make-char) cdef)
472                     (if table
473                         (set-charset-mapping-table (car cdef) table)))))
474           (when (not (or (< (char-int char) 32)
475                          (and (<= 128 (char-int char))
476                               (< (char-int char) 160))))
477             (delete-region (point) (point-at-eol))
478             (insert (format "\t; %c" char)))
479           )))))
480
481 (defun insert-char-data-with-variant (char)
482   (insert-char-data char)
483   (let ((variants (or (char-variants char)
484                       (let ((ucs (get-char-attribute char '->ucs)))
485                         (if ucs
486                             (delete char (char-variants (int-char ucs))))))))
487     (while variants
488       (insert-char-data (car variants))
489       (setq variants (cdr variants))
490       )))
491
492 (defun insert-char-range-data (min max)
493   (let ((code min)
494         char)
495     (while (<= code max)
496       (setq char (int-char code))
497       (insert-char-data-with-variant char)
498       (setq code (1+ code))
499       )))
500
501 (defun write-char-range-data-to-file (min max file)
502   (with-temp-buffer
503     (insert-char-range-data min max)
504     (write-region (point-min)(point-max) file)))
505
506 (defvar what-character-original-window-configuration)
507
508 ;;;###autoload
509 (defun what-char-definition (char)
510   (interactive (list (char-after)))
511   (let ((buf (get-buffer-create "*Character Description*"))
512         (the-buf (current-buffer))
513         (win-conf (current-window-configuration)))
514     (pop-to-buffer buf)
515     (make-local-variable 'what-character-original-window-configuration)
516     (setq what-character-original-window-configuration win-conf)
517     (setq buffer-read-only nil)
518     (erase-buffer)
519     (condition-case err
520         (progn
521           (insert-char-data-with-variant char)
522           (char-db-update-comment)
523           (set-buffer-modified-p nil)
524           (view-mode the-buf (lambda (buf)
525                                (set-window-configuration
526                                 what-character-original-window-configuration)
527                                ))
528           (goto-char (point-min)))
529       (error (progn
530                (set-window-configuration
531                 what-character-original-window-configuration)
532                (signal (car err) (cdr err)))))))
533
534 (provide 'char-db-util)
535
536 ;;; char-db-util.el ends here