1 ;;; calist.el --- Condition functions
3 ;; Copyright (C) 1998 Free Software Foundation, Inc.
4 ;; Copyright (C) 1999 Electrotechnical Laboratory, JAPAN.
5 ;; Licensed to the Free Software Foundation.
7 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
8 ;; Keywords: condition, alist, tree
10 ;; This file is part of APEL (A Portable Emacs Library).
12 ;; This program is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 2, or (at
15 ;; your option) any later version.
17 ;; This program is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 (eval-when-compile (require 'cl))
33 (defvar calist-package-alist nil)
34 (defvar calist-field-match-method-obarray nil)
36 (defun find-calist-package (name)
37 "Return a calist-package by NAME."
38 (cdr (assq name calist-package-alist)))
40 (defun define-calist-field-match-method (field-type function)
41 "Set field-match-method for FIELD-TYPE to FUNCTION."
42 (fset (intern (symbol-name field-type) calist-field-match-method-obarray)
45 (defun use-calist-package (name)
46 "Make the symbols of package NAME accessible in the current package."
47 (mapatoms (lambda (sym)
48 (if (intern-soft (symbol-name sym)
49 calist-field-match-method-obarray)
50 (signal 'conflict-of-calist-symbol
51 (list (format "Conflict of symbol %s")))
53 (define-calist-field-match-method
54 sym (symbol-function sym))
56 (find-calist-package name)))
58 (defun make-calist-package (name &optional use)
59 "Create a new calist-package."
60 (let ((calist-field-match-method-obarray (make-vector 7 0)))
61 (set-alist 'calist-package-alist name
62 calist-field-match-method-obarray)
63 (use-calist-package (or use 'standard))
64 calist-field-match-method-obarray))
66 (defun in-calist-package (name)
67 "Set the current calist-package to a new or existing calist-package."
68 (setq calist-field-match-method-obarray
69 (or (find-calist-package name)
70 (make-calist-package name))))
72 (in-calist-package 'standard)
74 (defun calist-default-field-match-method (calist field-type field-value)
75 (let ((s-field (assoc field-type calist)))
77 (cons (cons field-type field-value) calist)
81 ((equal (cdr s-field) field-value)
84 (define-calist-field-match-method t #'calist-default-field-match-method)
86 (defsubst calist-field-match-method (field-type)
89 (symbol-name field-type) calist-field-match-method-obarray)
90 (intern-soft "t" calist-field-match-method-obarray))))
92 (defsubst calist-field-match (calist field-type field-value)
93 (funcall (calist-field-match-method field-type)
94 calist field-type field-value))
96 (defun ctree-match-calist (rule-tree alist)
97 "Return matched condition-alist if ALIST matches RULE-TREE."
100 (let ((type (car rule-tree))
101 (choices (cdr rule-tree))
105 (let* ((choice (car choices))
106 (choice-value (car choice)))
107 (if (eq choice-value t)
108 (setq default choice)
109 (let ((ret-alist (calist-field-match alist type (car choice))))
113 (ctree-match-calist (cdr choice) ret-alist)
116 (setq choices (cdr choices)))
118 (let ((ret-alist (calist-field-match alist type t)))
121 (ctree-match-calist (cdr default) ret-alist)
125 (defun ctree-match-calist-partially (rule-tree alist)
126 "Return matched condition-alist if ALIST matches RULE-TREE."
129 (let ((type (car rule-tree))
130 (choices (cdr rule-tree))
134 (let* ((choice (car choices))
135 (choice-value (car choice)))
136 (if (eq choice-value t)
137 (setq default choice)
138 (let ((ret-alist (calist-field-match alist type (car choice))))
142 (ctree-match-calist-partially
143 (cdr choice) ret-alist)
146 (setq choices (cdr choices)))
148 (let ((ret-alist (calist-field-match alist type t)))
151 (ctree-match-calist-partially (cdr default) ret-alist)
153 (calist-field-match alist type t))
156 (defun ctree-find-calist (rule-tree alist &optional all)
157 "Return list of condition-alist which matches ALIST in RULE-TREE.
158 If optional argument ALL is specified, default rules are not ignored
159 even if other rules are matched for ALIST."
162 (let ((type (car rule-tree))
163 (choices (cdr rule-tree))
166 (let* ((choice (car choices))
167 (choice-value (car choice)))
168 (if (eq choice-value t)
169 (setq default choice)
170 (let ((ret-alist (calist-field-match alist type (car choice))))
173 (let ((ret (ctree-find-calist
174 (cdr choice) ret-alist all)))
176 (let ((elt (car ret)))
177 (or (member elt dest)
178 (setq dest (cons elt dest))
182 (or (member ret-alist dest)
183 (setq dest (cons ret-alist dest)))
185 (setq choices (cdr choices)))
186 (or (and (not all) dest)
188 (let ((ret-alist (calist-field-match alist type t)))
191 (let ((ret (ctree-find-calist
192 (cdr default) ret-alist all)))
194 (let ((elt (car ret)))
195 (or (member elt dest)
196 (setq dest (cons elt dest))
200 (or (member ret-alist dest)
201 (setq dest (cons ret-alist dest)))
206 (defun calist-to-ctree (calist)
207 "Convert condition-alist CALIST to condition-tree."
209 (let* ((cell (car calist)))
211 (list (cons (cdr cell)
212 (calist-to-ctree (cdr calist))
215 (defun ctree-add-calist-strictly (ctree calist)
216 "Add condition CALIST to condition-tree CTREE without default clause."
217 (cond ((null calist) ctree)
219 (calist-to-ctree calist)
222 (let* ((type (car ctree))
224 (ret (assoc type calist)))
228 (let ((cell (car values)))
229 (if (equal (car cell)(cdr ret))
232 (ctree-add-calist-strictly
234 (delete ret (copy-alist calist)))
236 (setq values (cdr values)))
237 (setcdr ctree (cons (cons (cdr ret)
239 (delete ret (copy-alist calist))))
244 (let ((cell (car values)))
246 (ctree-add-calist-strictly (cdr cell) calist))
248 (setq values (cdr values))))
252 (defun ctree-add-calist-with-default (ctree calist)
253 "Add condition CALIST to condition-tree CTREE with default clause."
254 (cond ((null calist) ctree)
256 (let* ((cell (car calist))
261 (cons value (calist-to-ctree (cdr calist)))))
264 (let* ((type (car ctree))
266 (ret (assoc type calist)))
270 (let ((cell (car values)))
271 (if (equal (car cell)(cdr ret))
274 (ctree-add-calist-with-default
276 (delete ret (copy-alist calist)))
278 (setq values (cdr values)))
279 (if (assq t (cdr ctree))
281 (cons (cons (cdr ret)
283 (delete ret (copy-alist calist))))
289 (delete ret (copy-alist calist))))
294 (let ((cell (car values)))
296 (ctree-add-calist-with-default (cdr cell) calist))
298 (setq values (cdr values)))
299 (let ((cell (assq t (cdr ctree))))
302 (ctree-add-calist-with-default (cdr cell)
304 (let ((elt (cons t (calist-to-ctree calist))))
305 (or (member elt (cdr ctree))
306 (setcdr ctree (cons elt (cdr ctree)))
312 (defun ctree-set-calist-strictly (ctree-var calist)
313 "Set condition CALIST in CTREE-VAR without default clause."
315 (ctree-add-calist-strictly (symbol-value ctree-var) calist)))
317 (defun ctree-set-calist-with-default (ctree-var calist)
318 "Set condition CALIST to CTREE-VAR with default clause."
320 (ctree-add-calist-with-default (symbol-value ctree-var) calist)))
328 ;;; calist.el ends here