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