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