(ctree-find-calist): Modify DOC-string.
[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 optional argument ALL is specified, default rules are not ignored
90 even if other rules are matched for ALIST."
91   (if (null rule-tree)
92       (list alist)
93     (let ((type (car rule-tree))
94           (choices (cdr rule-tree))
95           default dest)
96       (while choices
97         (let* ((choice (car choices))
98                (choice-value (car choice)))
99           (if (eq choice-value t)
100               (setq default choice)
101             (let ((ret-alist (calist-field-match alist type (car choice))))
102               (if ret-alist
103                   (if (cdr choice)
104                       (setq dest
105                             (append (ctree-find-calist
106                                      (cdr choice) ret-alist all)
107                                     dest))
108                     (setq dest (cons ret-alist dest))
109                     )))))
110         (setq choices (cdr choices)))
111       (or (and (not all) dest)
112           (if default
113               (let ((ret-alist (calist-field-match alist type t)))
114                 (if ret-alist
115                     (if (cdr default)
116                         (setq dest
117                               (append (ctree-find-calist
118                                        (cdr default) ret-alist all)
119                                       dest))
120                       (setq dest (cons ret-alist dest))
121                       ))))
122           )
123       dest)))
124
125 (defun calist-to-ctree (calist)
126   "Convert condition-alist CALIST to condition-tree."
127   (if calist
128       (let* ((cell (car calist)))
129         (cons (car cell)
130               (list (cons (cdr cell)
131                           (calist-to-ctree (cdr calist))
132                           ))))))
133
134 (defun ctree-add-calist-strictly (ctree calist)
135   "Add condition CALIST to condition-tree CTREE without default clause."
136   (cond ((null calist) ctree)
137         ((null ctree)
138          (calist-to-ctree calist)
139          )
140         (t
141          (let* ((type (car ctree))
142                 (values (cdr ctree))
143                 (ret (assoc type calist)))
144            (if ret
145                (catch 'tag
146                  (while values
147                    (let ((cell (car values)))
148                      (if (equal (car cell)(cdr ret))
149                          (throw 'tag
150                                 (setcdr cell
151                                         (ctree-add-calist-strictly
152                                          (cdr cell)
153                                          (delete ret (copy-alist calist)))
154                                         ))))
155                    (setq values (cdr values)))
156                  (setcdr ctree (cons (cons (cdr ret)
157                                            (calist-to-ctree
158                                             (delete ret (copy-alist calist))))
159                                      (cdr ctree)))
160                  )
161              (catch 'tag
162                (while values
163                  (let ((cell (car values)))
164                    (setcdr cell
165                            (ctree-add-calist-strictly (cdr cell) calist))
166                    )
167                  (setq values (cdr values))))
168              )
169            ctree))))
170
171 (defun ctree-add-calist-with-default (ctree calist)
172   "Add condition CALIST to condition-tree CTREE with default clause."
173   (cond ((null calist) ctree)
174         ((null ctree)
175          (let* ((cell (car calist))
176                 (type (car cell))
177                 (value (cdr cell)))
178            (cons type
179                  (list '(t)
180                        (cons value (calist-to-ctree (cdr calist)))))
181            ))
182         (t
183          (let* ((type (car ctree))
184                 (values (cdr ctree))
185                 (ret (assoc type calist)))
186            (if ret
187                (catch 'tag
188                  (while values
189                    (let ((cell (car values)))
190                      (if (equal (car cell)(cdr ret))
191                          (throw 'tag
192                                 (setcdr cell
193                                         (ctree-add-calist-with-default
194                                          (cdr cell)
195                                          (delete ret (copy-alist calist)))
196                                         ))))
197                    (setq values (cdr values)))
198                  (setcdr ctree (list* '(t)
199                                       (cons (cdr ret)
200                                             (calist-to-ctree
201                                              (delete ret (copy-alist calist))))
202                                       (cdr ctree)))
203                  )
204              (catch 'tag
205                (while values
206                  (let ((cell (car values)))
207                    (setcdr cell
208                            (ctree-add-calist-with-default (cdr cell) calist))
209                    )
210                  (setq values (cdr values)))
211                (let ((elt (cons t (calist-to-ctree calist))))
212                  (or (member elt (cdr ctree))
213                      (setcdr ctree (cons elt (cdr ctree)))
214                      )))
215              )
216            ctree))))
217
218 (defun ctree-set-calist-strictly (ctree-var calist)
219   "Set condition CALIST in CTREE-VAR without default clause."
220   (set ctree-var
221        (ctree-add-calist-strictly (symbol-value ctree-var) calist)))
222
223 (defun ctree-set-calist-with-default (ctree-var calist)
224   "Set condition CALIST to CTREE-VAR with default clause."
225   (set ctree-var
226        (ctree-add-calist-with-default (symbol-value ctree-var) calist)))
227
228
229 ;;; @ end
230 ;;;
231
232 (provide 'calist)
233
234 ;;; calist.el ends here