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