(insert-char-data): Fix format of `iso-10646-comment'; swap order of
[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 (defun char-attribute-name< (ka kb)
65   (cond
66    ((find-charset ka)
67     (cond
68      ((find-charset kb)
69       (cond
70        ((= (charset-dimension ka)
71            (charset-dimension kb))
72         (cond ((= (charset-chars ka)(charset-chars kb))
73                (cond
74                 ((>= (charset-final ka) ?@)
75                  (if (>= (charset-final kb) ?@)
76                      (< (charset-final ka)
77                         (charset-final kb))
78                    t))
79                 ((>= (charset-final ka) ?0)
80                  (cond
81                   ((>= (charset-final kb) ?@)
82                    nil)
83                   ((>= (charset-final kb) ?0)
84                    (< (charset-final ka)
85                       (charset-final kb)))
86                   (t)))))
87               ((<= (charset-chars ka)(charset-chars kb)))))
88        (t
89         (< (charset-dimension ka)
90            (charset-dimension kb))
91         )))
92      (t)))
93    ((find-charset kb)
94     t)
95    ((symbolp ka)
96     (cond ((symbolp kb)
97            (string< (symbol-name ka)
98                     (symbol-name kb)))
99           (t)))
100    ((symbolp kb)
101     nil)))
102
103 (defun insert-char-data (char)
104   (let ((data (char-attribute-alist char))
105         cell ret name has-long-ccs-name rest)
106     (when data
107       (save-restriction
108         (narrow-to-region (point)(point))
109         (insert "(define-char
110   '(")
111         (when (setq cell (assq 'name data))
112           (setq cell (cdr cell))
113           (insert (format
114                    (if (> (length cell) 47)
115                        "(name . %S)
116     "
117                      "(name\t\t. %S)
118     ")
119                    cell))
120           (setq data (del-alist 'name data))
121           )
122         (when (setq cell (assq 'name data))
123           (setq cell (cdr cell))
124           (insert (format
125                    (if (> (length cell) 47)
126                        "(name . %S)
127     "
128                      "(name\t\t. %S)
129     ")
130                    cell))
131           (setq data (del-alist 'name data))
132           )
133         (when (setq cell (assq 'ucs data))
134           (setq cell (cdr cell))
135           (insert (format "(ucs\t\t. #x%04X)
136     "
137                           cell))
138           (setq data (del-alist 'ucs data))
139           )
140         (when (setq cell (assq '->ucs data))
141           (setq cell (cdr cell))
142           (insert (format "(->ucs\t\t. #x%04X)
143     "
144                           cell))
145           (setq data (del-alist '->ucs data))
146           )
147         (when (setq cell (assq 'general-category data))
148           (setq ret (cdr cell))
149           (insert (format
150                    "(general-category\t%s) ; %s
151     "
152                    (mapconcat (lambda (cell)
153                                 (format "%S" cell))
154                               ret " ")
155                    (cond ((rassoc (cdr cell)
156                                   unidata-normative-category-alist)
157                           "Normative Category")
158                          ((rassoc (cdr cell)
159                                   unidata-informative-category-alist)
160                           "Informative Category")
161                          (t
162                           "Unknown Category"))))
163           (setq data (del-alist 'general-category data))
164           )
165         (when (setq cell (assq 'bidi-category data))
166           (setq cell (cdr cell))
167           (insert (format "(bidi-category\t. %S)
168     "
169                           cell))
170           (setq data (del-alist 'bidi-category data))
171           )
172         (when (setq cell (assq 'mirrored data))
173           (setq cell (cdr cell))
174           (insert (format "(mirrored\t\t. %S)
175     "
176                           cell))
177           (setq data (del-alist 'mirrored data))
178           )
179         (when (setq cell (assq 'decimal-digit-value data))
180           (setq cell (cdr cell))
181           (insert (format "(decimal-digit-value . %S)
182     "
183                           cell))
184           (setq data (del-alist 'decimal-digit-value data))
185           (when (setq cell (assq 'digit-value data))
186             (setq cell (cdr cell))
187             (insert (format "(digit-value\t . %S)
188     "
189                             cell))
190             (setq data (del-alist 'digit-value data))
191             )
192           (when (setq cell (assq 'numeric-value data))
193             (setq cell (cdr cell))
194             (insert (format "(numeric-value\t . %S)
195     "
196                             cell))
197             (setq data (del-alist 'numeric-value data))
198             )
199           )
200         (when (setq cell (assq 'iso-10646-comment data))
201             (setq cell (cdr cell))
202             (insert (format "(iso-10646-comment\t. %S)
203     "
204                             cell))
205             (setq data (del-alist 'iso-10646-comment data))
206             )
207         (when (setq cell (assq '->decomposition data))
208           (setq cell (cdr cell))
209           (insert (format "(->decomposition\t%s)
210     "
211                           (mapconcat (lambda (code)
212                                        (cond ((symbolp code)
213                                               (symbol-name code))
214                                              ((integerp code)
215                                               (format "#x%04X" code))
216                                              (t
217                                               (format "\n     %S" code))))
218                                      cell " ")))
219           (setq data (del-alist '->decomposition data))
220           )
221         (when (setq cell (assq '->uppercase data))
222           (setq cell (cdr cell))
223           (insert (format "(->uppercase\t%s)
224     "
225                           (mapconcat (lambda (code)
226                                        (cond ((symbolp code)
227                                               (symbol-name code))
228                                              ((integerp code)
229                                               (format "#x%04X" code))
230                                              (t
231                                               (format "\n     %S" code))))
232                                      cell " ")))
233           (setq data (del-alist '->uppercase data))
234           )
235         (when (setq cell (assq '->lowercase data))
236           (setq cell (cdr cell))
237           (insert (format "(->lowercase\t%s)
238     "
239                           (mapconcat (lambda (code)
240                                        (cond ((symbolp code)
241                                               (symbol-name code))
242                                              ((integerp code)
243                                               (format "#x%04X" code))
244                                              (t
245                                               (format "\n     %S" code))))
246                                      cell " ")))
247           (setq data (del-alist '->lowercase data))
248           )
249         (when (setq cell (assq '->titlecase data))
250           (setq cell (cdr cell))
251           (insert (format "(->titlecase\t%s)
252     "
253                           (mapconcat (lambda (code)
254                                        (cond ((symbolp code)
255                                               (symbol-name code))
256                                              ((integerp code)
257                                               (format "#x%04X" code))
258                                              (t
259                                               (format "\n     %S" code))))
260                                      cell " ")))
261           (setq data (del-alist '->titlecase data))
262           )
263         (setq data
264               (sort data
265                     (lambda (a b)
266                       (char-attribute-name< (car a)(car b)))))
267         (setq rest data)
268         (while (and rest
269                     (progn
270                       (setq cell (car rest))
271                       (if (setq ret (find-charset (car cell)))
272                           (if (>= (length (symbol-name (charset-name ret))) 19)
273                               (progn
274                                 (setq has-long-ccs-name t)
275                                 nil)
276                             t)
277                         t)))
278           (setq rest (cdr rest)))
279         (while data
280           (setq cell (car data))
281           (cond ((setq ret (find-charset (car cell)))
282                  (insert (format (if has-long-ccs-name
283                                      "(%-26s %s)
284     "
285                                    "(%-18s %s)
286     "
287                                    )
288                                  (charset-name ret)
289                                  (mapconcat (lambda (b)
290                                               (format "#x%02X" b)
291                                               )
292                                             (cdr cell) " "))))
293                 ((string-match "^->" (symbol-name (car cell)))
294                  (insert
295                   (format "(%-18s %s)
296     "
297                           (car cell)
298                           (mapconcat (lambda (code)
299                                        (cond ((symbolp code)
300                                               (symbol-name code))
301                                              ((integerp code)
302                                               (format "#x%04X" code))
303                                              (t
304                                               (format "\n     %S" code))))
305                                      (cdr cell) " "))))
306                 ((consp (cdr cell))
307                  (insert (format "%S
308     "
309                                  cell)))
310                 (t
311                  (insert (format "(%-18s . %S)
312     "
313                                  (car cell)(cdr cell)))
314                  ))
315           (setq data (cdr data)))
316         (insert "))\n")
317         (goto-char (point-min))
318         (while (re-search-forward "[ \t]+$" nil t)
319           (replace-match ""))
320         (goto-char (point-max))
321         (tabify (point-min)(point-max))
322         ))))
323
324 (defun insert-char-range-data (min max)
325   (let ((code min)
326         char
327         variants)
328     (while (<= code max)
329       (setq char (int-char code))
330       (insert-char-data char)
331       (setq variants (char-variants char))
332       (while variants
333         (insert-char-data (car variants))
334         (setq variants (cdr variants)))
335       (setq code (1+ code))
336       )))
337
338 (defun write-char-range-data-to-file (min max file)
339   (with-temp-buffer
340     (insert-char-range-data min max)
341     (write-region (point-min)(point-max) file)))
342
343 ;;;###autoload
344 (defun char-db-update-comment ()
345   (interactive)
346   (save-excursion
347     (goto-char (point-min))
348     (let (cdef table char)
349       (while (re-search-forward "^[ \t]*\\(([^.()]+)\\)" nil t)
350         (goto-char (match-beginning 1))
351         (setq cdef (read (current-buffer)))
352         (when (find-charset (car cdef))
353           (goto-char (match-end 0))
354           (setq char
355                 (if (or (memq (car cdef) '(ascii latin-viscii-upper
356                                                  latin-viscii-lower))
357                         (= (char-int (charset-final (car cdef))) 0))
358                     (apply (function make-char) cdef)
359                   (if (setq table (charset-mapping-table (car cdef)))
360                       (set-charset-mapping-table (car cdef) nil))
361                   (prog1
362                       (apply (function make-char) cdef)
363                     (if table
364                         (set-charset-mapping-table (car cdef) table)))))
365           (when (not (or (< (char-int char) 32)
366                          (and (<= (char-int char) 128)
367                               (< (char-int char) 160))))
368             (delete-region (point) (point-at-eol))
369             (insert (format "\t; %c" char)))
370           )))))
371
372 ;;;###autoload
373 (defun what-char-definition (char)
374   (interactive (list (char-after)))
375   (let ((buf (get-buffer-create "*Character Description*"))
376         (the-buf (current-buffer))
377         (win-conf (current-window-configuration)))
378     (pop-to-buffer buf)
379     (make-local-variable 'what-character-original-window-configuration)
380     (setq what-character-original-window-configuration win-conf)
381     (setq buffer-read-only nil)
382     (erase-buffer)
383     (condition-case err
384         (progn
385           (insert-char-data char)
386           (set-buffer-modified-p nil)
387           (view-mode the-buf (lambda (buf)
388                                (set-window-configuration
389                                 what-character-original-window-configuration)
390                                ))
391           (goto-char (point-min)))
392       (error (progn
393                (set-window-configuration
394                 what-character-original-window-configuration)
395                (signal (car err) (cdr err)))))))
396
397 (provide 'char-db-util)
398
399 ;;; char-db-util.el ends here