Sync up with r21-4-15-chise-0_22-release.
[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 data-directory
44                           "../etc/")))
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       (and (fboundp 'save-charset-properties)
79            (save-charset-properties ccs))
80       (save-charset-mapping-table ccs))
81
82     (with-temp-buffer
83       (insert
84        (format
85         "(setq next-defined-char-id #x%X)\n"
86         next-defined-char-id))
87       (write-region (point-min)(point-max)
88                     "../lisp/utf-2000/cid-conf.el"))
89     )
90    (t
91     (if (>= (function-max-args 'char-attribute-list) 1)
92         (char-attribute-list 'rehash)
93       (mapcar (lambda (file)
94                 (mount-char-attribute-table
95                  (intern (file-name-char-attribute-name file))))
96               (directory-files
97                (expand-file-name "system-char-id" ; "character/feature"
98                                  system-char-database-directory)
99                nil nil t t)))
100     (dolist (ccs (charset-list))
101       (reset-charset-mapping-table ccs))
102     (load "../lisp/utf-2000/cid-conf.el")
103     )))
104  (t
105   (load "dumped-chars.el")
106   (dolist (file system-char-db-source-file-list)
107     (pureload file))
108   (dolist (feature '(ideographic-structure))
109     (map-char-attribute
110      (lambda (c v)
111        (put-char-attribute
112         c feature (char-refs-simplify-char-specs v))
113        nil)
114      feature))
115   ))
116
117 (defun char-ref= (cr1 cr2 &optional tester)
118   (cond ((char-ref-p cr1)
119          (if (char-ref-p cr2)
120              (char-spec= (plist-get cr1 :char)
121                          (plist-get cr2 :char) tester)
122            (char-spec= (plist-get cr1 :char) cr2 tester)))
123         (t
124          (char-spec= cr1
125                      (if (char-ref-p cr2)
126                          (plist-get cr2 :char)
127                        cr2)
128                      tester))))
129
130 (defun char-spec= (cs1 cs2 &optional tester)
131   (unless tester
132     (setq tester #'eq))
133   (if (characterp cs1)
134       (if (characterp cs2)
135           (funcall tester cs1 cs2)
136         (funcall tester cs1 (find-char cs2)))
137     (if (characterp cs2)
138         (funcall tester (find-char cs1) cs2)
139       (funcall tester (find-char cs1) (find-char cs2)))))
140
141 (let (ret)
142   (map-char-attribute
143    (lambda (c dc)
144      (if (consp dc)
145          (setq dc (car dc)))
146      (if (listp dc)
147          (if (setq ret (find-char dc))
148              (setq dc ret)))
149      (when (characterp dc)
150        (setq ret (get-char-attribute dc '->uppercase))
151        (if (if (listp ret)
152                (member* c ret :test #'char-ref=)
153              (char-ref= c ret))
154            (put-case-table-pair c dc (standard-case-table))))
155      nil)
156    '->lowercase))
157
158 (garbage-collect)
159
160 ;;; update-cdb.el ends here