Merge r21-2-44-utf-2000-m0_18-n0_21-21.
[chise/xemacs-chise.git-] / lisp / utf-2000 / update-cdb.el
1 ;;; update-cdb.el --- Update and/or setup character attribute database
2
3 ;; Copyright (C) 2002,2003,2004 MORIOKA Tomohiko.
4
5 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
6 ;; Keywords: Character, Database, CHISE, Unicode, UCS-4, MULE.
7
8 ;; This file is part of XEmacs CHISE.
9
10 ;; XEmacs CHISE is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
14
15 ;; XEmacs CHISE 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 CHISE; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 (eval-when-compile (require 'cl))
28
29 (defun delete-file-with-children (filename)
30   (if (file-directory-p filename)
31       (let ((files
32              (directory-files filename 'full "^[^.]" 'so-sort)))
33         (if files
34             (dolist (file files)
35               (delete-file-with-children file)))
36         (remove-directory filename))
37     (delete-file filename)))
38
39 (cond
40  ((featurep 'chise)
41   (defvar system-char-database-directory
42     (expand-file-name "chise-db"
43                       (or exec-directory
44                           "../lib-src/")))
45
46   (defun file-name-char-attribute-name (filename)
47     (let ((i 0)
48           (base 0)
49           (len (length filename))
50           chr dest)
51       (while (< i len)
52         (if (eq (setq chr (aref filename i)) ?%)
53             (setq dest (concat dest
54                                (substring filename base i)
55                                (char-to-string
56                                 (int-char
57                                  (string-to-int
58                                   (substring filename (1+ i) (+ i 3)) 16))))
59                   i (+ i 3)
60                   base i)
61           (setq i (1+ i))))
62       (concat dest (substring filename base len))))
63
64   (cond
65    ((or load-ignore-elc-files
66         (not (file-exists-p system-char-database-directory)))
67     (if (file-exists-p system-char-database-directory)
68         (delete-file-with-children system-char-database-directory))
69
70     (load "dumped-chars.el")
71     (dolist (file system-char-db-source-file-list)
72       (pureload file))
73
74     (dolist (attribute (char-attribute-list))
75       (save-char-attribute-table attribute))
76
77     (dolist (ccs (charset-list))
78       (save-charset-mapping-table ccs))
79
80     (with-temp-buffer
81       (insert
82        (format
83         "(setq next-defined-char-id #x%X)\n"
84         next-defined-char-id))
85       (write-region (point-min)(point-max)
86                     "../lisp/utf-2000/cid-conf.el"))
87     )
88    (t
89     (if (>= (function-max-args 'char-attribute-list) 1)
90         (char-attribute-list 'rehash)
91       (mapcar (lambda (file)
92                 (mount-char-attribute-table
93                  (intern (file-name-char-attribute-name file))))
94               (directory-files
95                (expand-file-name "system-char-id" ; "character/feature"
96                                  system-char-database-directory)
97                nil nil t t)))
98     (dolist (ccs (charset-list))
99       (reset-charset-mapping-table ccs))
100     (load "../lisp/utf-2000/cid-conf.el")
101     )))
102  (t
103   (load "dumped-chars.el")
104   (dolist (file system-char-db-source-file-list)
105     (pureload file))
106   (dolist (feature '(ideographic-structure))
107     (map-char-attribute
108      (lambda (c v)
109        (put-char-attribute
110         c feature (char-refs-simplify-char-specs v))
111        nil)
112      feature))
113   ))
114
115 (defun char-ref= (cr1 cr2 &optional tester)
116   (cond ((char-ref-p cr1)
117          (if (char-ref-p cr2)
118              (char-spec= (plist-get cr1 :char)
119                          (plist-get cr2 :char) tester)
120            (char-spec= (plist-get cr1 :char) cr2 tester)))
121         (t
122          (char-spec= cr1
123                      (if (char-ref-p cr2)
124                          (plist-get cr2 :char)
125                        cr2)
126                      tester))))
127
128 (defun char-spec= (cs1 cs2 &optional tester)
129   (unless tester
130     (setq tester #'eq))
131   (if (characterp cs1)
132       (if (characterp cs2)
133           (funcall tester cs1 cs2)
134         (funcall tester cs1 (find-char cs2)))
135     (if (characterp cs2)
136         (funcall tester (find-char cs1) cs2)
137       (funcall tester (find-char cs1) (find-char cs2)))))
138
139 (let (ret)
140   (map-char-attribute
141    (lambda (c dc)
142      (if (consp dc)
143          (setq dc (car dc)))
144      (if (listp dc)
145          (if (setq ret (find-char dc))
146              (setq dc ret)))
147      (when (characterp dc)
148        (setq ret (get-char-attribute dc '->uppercase))
149        (if (if (listp ret)
150                (member* c ret :test #'char-ref=)
151              (char-ref= c ret))
152            (put-case-table-pair c dc (standard-case-table))))
153      nil)
154    '->lowercase))
155
156 (garbage-collect)
157
158 ;;; update-cdb.el ends here