(ctree-find-calist): Add optional argument 'all.
[elisp/apel.git] / calist.el
1 ;;; calist.el --- Condition functions
2
3 ;; Copyright (C) 1998 MORIOKA Tomohiko.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: condition, alist, tree
7
8 ;; This file is part of APEL (A Portable Emacs Library).
9
10 ;; This program 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 ;; This program 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 GNU Emacs; see the file COPYING.  If not, write to the
22 ;; 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 (defvar calist-field-match-method-obarray [nil])
30
31 (defun define-calist-field-match-method (field-type function)
32   "Set field-match-method for FIELD-TYPE to FUNCTION."
33   (fset (intern (symbol-name field-type) calist-field-match-method-obarray)
34         function))
35
36 (defun calist-default-field-match-method (calist field-type field-value)
37   (let ((s-field (assoc field-type calist)))
38     (cond ((null s-field)
39            (cons (cons field-type field-value) calist)
40            )
41           ((eq field-value t)
42            calist)
43           ((equal (cdr s-field) field-value)
44            calist))))
45
46 (defsubst calist-field-match-method (field-type)
47   (condition-case nil
48       (symbol-function
49        (intern-soft
50         (symbol-name field-type) calist-field-match-method-obarray))
51     (error (symbol-function 'calist-default-field-match-method))
52     ))
53
54 (defsubst calist-field-match (calist field-type field-value)
55   (funcall (calist-field-match-method field-type)
56            calist field-type field-value))
57
58 (defun ctree-match-calist (rule-tree alist)
59   "Return matched condition-alist if ALIST matches RULE-TREE."
60   (if (null rule-tree)
61       alist
62     (let ((type (car rule-tree))
63           (choices (cdr rule-tree))
64           default)
65       (catch 'tag
66         (while choices
67           (let* ((choice (car choices))
68                  (choice-value (car choice)))
69             (if (eq choice-value t)
70                 (setq default choice)
71               (let ((ret-alist (calist-field-match alist type (car choice))))
72                 (if ret-alist
73                     (throw 'tag
74                            (if (cdr choice)
75                                (ctree-match-calist (cdr choice) ret-alist)
76                              ret-alist))
77                   ))))
78           (setq choices (cdr choices)))
79         (if default
80             (let ((ret-alist (calist-field-match alist type t)))
81               (if ret-alist
82                   (if (cdr default)
83                       (ctree-match-calist (cdr default) ret-alist)
84                     ret-alist))))
85         ))))
86
87 (defun ctree-find-calist (rule-tree alist &optional all)
88   "Return list of condition-alist which matches ALIST in RULE-TREE."
89   (if (null rule-tree)
90       (list alist)
91     (let ((type (car rule-tree))
92           (choices (cdr rule-tree))
93           default dest)
94       (while choices
95         (let* ((choice (car choices))
96                (choice-value (car choice)))
97           (if (eq choice-value t)
98               (setq default choice)
99             (let ((ret-alist (calist-field-match alist type (car choice))))
100               (if ret-alist
101                   (if (cdr choice)
102                       (setq dest
103                             (append (ctree-find-calist
104                                      (cdr choice) ret-alist all)
105                                     dest))
106                     (setq dest (cons ret-alist dest))
107                     )))))
108         (setq choices (cdr choices)))
109       (or (and (not all) dest)
110           (if default
111               (let ((ret-alist (calist-field-match alist type t)))
112                 (if ret-alist
113                     (if (cdr default)
114                         (setq dest
115                               (append (ctree-find-calist
116                                        (cdr default) ret-alist all)
117                                       dest))
118                       (setq dest (cons ret-alist dest))
119                       ))))
120           )
121       dest)))
122
123 (defun calist-to-ctree (calist)
124   "Convert condition-alist CALIST to condition-tree."
125   (if calist
126       (let* ((cell (car calist)))
127         (cons (car cell)
128               (list (cons (cdr cell)
129                           (calist-to-ctree (cdr calist))
130                           ))))))
131
132 (defun ctree-add-calist-strictly (ctree calist)
133   "Add condition CALIST to condition-tree CTREE without default clause."
134   (cond ((null calist) ctree)
135         ((null ctree)
136          (calist-to-ctree calist)
137          )
138         (t
139          (let* ((type (car ctree))
140                 (values (cdr ctree))
141                 (ret (assoc type calist)))
142            (if ret
143                (catch 'tag
144                  (while values
145                    (let ((cell (car values)))
146                      (if (equal (car cell)(cdr ret))
147                          (throw 'tag
148                                 (setcdr cell
149                                         (ctree-add-calist-strictly
150                                          (cdr cell)
151                                          (delete ret (copy-alist calist)))
152                                         ))))
153                    (setq values (cdr values)))
154                  (setcdr ctree (cons (cons (cdr ret)
155                                            (calist-to-ctree
156                                             (delete ret (copy-alist calist))))
157                                      (cdr ctree)))
158                  )
159              (catch 'tag
160                (while values
161                  (let ((cell (car values)))
162                    (setcdr cell
163                            (ctree-add-calist-strictly (cdr cell) calist))
164                    )
165                  (setq values (cdr values))))
166              )
167            ctree))))
168
169 (defun ctree-add-calist-with-default (ctree calist)
170   "Add condition CALIST to condition-tree CTREE with default clause."
171   (cond ((null calist) ctree)
172         ((null ctree)
173          (let* ((cell (car calist))
174                 (type (car cell))
175                 (value (cdr cell)))
176            (cons type
177                  (list '(t)
178                        (cons value (calist-to-ctree (cdr calist)))))
179            ))
180         (t
181          (let* ((type (car ctree))
182                 (values (cdr ctree))
183                 (ret (assoc type calist)))
184            (if ret
185                (catch 'tag
186                  (while values
187                    (let ((cell (car values)))
188                      (if (equal (car cell)(cdr ret))
189                          (throw 'tag
190                                 (setcdr cell
191                                         (ctree-add-calist-with-default
192                                          (cdr cell)
193                                          (delete ret (copy-alist calist)))
194                                         ))))
195                    (setq values (cdr values)))
196                  (setcdr ctree (list* '(t)
197                                       (cons (cdr ret)
198                                             (calist-to-ctree
199                                              (delete ret (copy-alist calist))))
200                                       (cdr ctree)))
201                  )
202              (catch 'tag
203                (while values
204                  (let ((cell (car values)))
205                    (setcdr cell
206                            (ctree-add-calist-with-default (cdr cell) calist))
207                    )
208                  (setq values (cdr values)))
209                (let ((elt (cons t (calist-to-ctree calist))))
210                  (or (member elt (cdr ctree))
211                      (setcdr ctree (cons elt (cdr ctree)))
212                      )))
213              )
214            ctree))))
215
216 (defun ctree-set-calist-strictly (ctree-var calist)
217   "Set condition CALIST in CTREE-VAR without default clause."
218   (set ctree-var
219        (ctree-add-calist-strictly (symbol-value ctree-var) calist)))
220
221 (defun ctree-set-calist-with-default (ctree-var calist)
222   "Set condition CALIST to CTREE-VAR with default clause."
223   (set ctree-var
224        (ctree-add-calist-with-default (symbol-value ctree-var) calist)))
225
226
227 ;;; @ end
228 ;;;
229
230 (provide 'calist)
231
232 ;;; calist.el ends here