1 ;;; calist.el --- Condition functions
3 ;; Copyright (C) 1998 MORIOKA Tomohiko.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: condition, alist, tree
8 ;; This file is part of APEL (A Portable Emacs Library).
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.
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.
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.
27 (eval-when-compile (require 'cl))
29 (defvar calist-field-match-method-obarray [nil])
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)
36 (defun calist-default-field-match-method (calist field-type field-value)
37 (let ((s-field (assoc field-type calist)))
39 (cons (cons field-type field-value) calist)
43 ((equal (cdr s-field) field-value)
46 (defsubst calist-field-match-method (field-type)
50 (symbol-name field-type) calist-field-match-method-obarray))
51 (error (symbol-function 'calist-default-field-match-method))
54 (defsubst calist-field-match (calist field-type field-value)
55 (funcall (calist-field-match-method field-type)
56 calist field-type field-value))
58 (defun ctree-match-calist (rule-tree alist)
59 "Return matched condition-alist if ALIST matches RULE-TREE."
62 (let ((type (car rule-tree))
63 (choices (cdr rule-tree))
67 (let* ((choice (car choices))
68 (choice-value (car choice)))
69 (if (eq choice-value t)
71 (let ((ret-alist (calist-field-match alist type (car choice))))
75 (ctree-match-calist (cdr choice) ret-alist)
78 (setq choices (cdr choices)))
80 (let ((ret-alist (calist-field-match alist type t)))
83 (ctree-match-calist (cdr default) ret-alist)
87 (defun ctree-match-calist-partially (rule-tree alist)
88 "Return matched condition-alist if ALIST matches RULE-TREE."
91 (let ((type (car rule-tree))
92 (choices (cdr rule-tree))
96 (let* ((choice (car choices))
97 (choice-value (car choice)))
98 (if (eq choice-value t)
100 (let ((ret-alist (calist-field-match alist type (car choice))))
104 (ctree-match-calist-partially
105 (cdr choice) ret-alist)
108 (setq choices (cdr choices)))
110 (let ((ret-alist (calist-field-match alist type t)))
113 (ctree-match-calist-partially (cdr default) ret-alist)
115 (calist-field-match alist type t))
118 (defun ctree-find-calist (rule-tree alist &optional all)
119 "Return list of condition-alist which matches ALIST in RULE-TREE.
120 If optional argument ALL is specified, default rules are not ignored
121 even if other rules are matched for ALIST."
124 (let ((type (car rule-tree))
125 (choices (cdr rule-tree))
128 (let* ((choice (car choices))
129 (choice-value (car choice)))
130 (if (eq choice-value t)
131 (setq default choice)
132 (let ((ret-alist (calist-field-match alist type (car choice))))
135 (let ((ret (ctree-find-calist
136 (cdr choice) ret-alist all)))
138 (let ((elt (car ret)))
139 (or (member elt dest)
140 (setq dest (cons elt dest))
144 (or (member ret-alist dest)
145 (setq dest (cons ret-alist dest)))
147 (setq choices (cdr choices)))
148 (or (and (not all) dest)
150 (let ((ret-alist (calist-field-match alist type t)))
153 (let ((ret (ctree-find-calist
154 (cdr default) ret-alist all)))
156 (let ((elt (car ret)))
157 (or (member elt dest)
158 (setq dest (cons elt dest))
162 (or (member ret-alist dest)
163 (setq dest (cons ret-alist dest)))
168 (defun calist-to-ctree (calist)
169 "Convert condition-alist CALIST to condition-tree."
171 (let* ((cell (car calist)))
173 (list (cons (cdr cell)
174 (calist-to-ctree (cdr calist))
177 (defun ctree-add-calist-strictly (ctree calist)
178 "Add condition CALIST to condition-tree CTREE without default clause."
179 (cond ((null calist) ctree)
181 (calist-to-ctree calist)
184 (let* ((type (car ctree))
186 (ret (assoc type calist)))
190 (let ((cell (car values)))
191 (if (equal (car cell)(cdr ret))
194 (ctree-add-calist-strictly
196 (delete ret (copy-alist calist)))
198 (setq values (cdr values)))
199 (setcdr ctree (cons (cons (cdr ret)
201 (delete ret (copy-alist calist))))
206 (let ((cell (car values)))
208 (ctree-add-calist-strictly (cdr cell) calist))
210 (setq values (cdr values))))
214 (defun ctree-add-calist-with-default (ctree calist)
215 "Add condition CALIST to condition-tree CTREE with default clause."
216 (cond ((null calist) ctree)
218 (let* ((cell (car calist))
223 (cons value (calist-to-ctree (cdr calist)))))
226 (let* ((type (car ctree))
228 (ret (assoc type calist)))
232 (let ((cell (car values)))
233 (if (equal (car cell)(cdr ret))
236 (ctree-add-calist-with-default
238 (delete ret (copy-alist calist)))
240 (setq values (cdr values)))
241 (if (assq t (cdr ctree))
243 (cons (cons (cdr ret)
245 (delete ret (copy-alist calist))))
251 (delete ret (copy-alist calist))))
256 (let ((cell (car values)))
258 (ctree-add-calist-with-default (cdr cell) calist))
260 (setq values (cdr values)))
261 (let ((cell (assq t (cdr ctree))))
264 (ctree-add-calist-with-default (cdr cell)
266 (let ((elt (cons t (calist-to-ctree calist))))
267 (or (member elt (cdr ctree))
268 (setcdr ctree (cons elt (cdr ctree)))
274 (defun ctree-set-calist-strictly (ctree-var calist)
275 "Set condition CALIST in CTREE-VAR without default clause."
277 (ctree-add-calist-strictly (symbol-value ctree-var) calist)))
279 (defun ctree-set-calist-with-default (ctree-var calist)
280 "Set condition CALIST to CTREE-VAR with default clause."
282 (ctree-add-calist-with-default (symbol-value ctree-var) calist)))
290 ;;; calist.el ends here