forked from wanderlust/apel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calist.el
331 lines (299 loc) · 9.38 KB
/
calist.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
;;; calist.el --- Condition functions
;; Copyright (C) 1998 Free Software Foundation, Inc.
;; Copyright (C) 1999 Electrotechnical Laboratory, JAPAN.
;; Licensed to the Free Software Foundation.
;; Author: MORIOKA Tomohiko <[email protected]>
;; Keywords: condition, alist, tree
;; This file is part of APEL (A Portable Emacs Library).
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2, or (at
;; your option) any later version.
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Code:
(eval-when-compile (require 'cl))
(require 'alist)
(defvar calist-package-alist nil)
(defvar calist-field-match-method-obarray nil)
(defun find-calist-package (name)
"Return a calist-package by NAME."
(cdr (assq name calist-package-alist)))
(defun define-calist-field-match-method (field-type function)
"Set field-match-method for FIELD-TYPE to FUNCTION."
(fset (intern (symbol-name field-type) calist-field-match-method-obarray)
function))
(defun use-calist-package (name)
"Make the symbols of package NAME accessible in the current package."
(mapatoms (lambda (sym)
(if (intern-soft (symbol-name sym)
calist-field-match-method-obarray)
(signal 'conflict-of-calist-symbol
(list (format "Conflict of symbol %s" sym)))
(if (fboundp sym)
(define-calist-field-match-method
sym (symbol-function sym))
)))
(find-calist-package name)))
(defun make-calist-package (name &optional use)
"Create a new calist-package."
(let ((calist-field-match-method-obarray (make-vector 7 0)))
(set-alist 'calist-package-alist name
calist-field-match-method-obarray)
(use-calist-package (or use 'standard))
calist-field-match-method-obarray))
(defun in-calist-package (name)
"Set the current calist-package to a new or existing calist-package."
(setq calist-field-match-method-obarray
(or (find-calist-package name)
(make-calist-package name))))
(in-calist-package 'standard)
(defun calist-default-field-match-method (calist field-type field-value)
(let ((s-field (assoc field-type calist)))
(cond ((null s-field)
(cons (cons field-type field-value) calist)
)
((eq field-value t)
calist)
((equal (cdr s-field) field-value)
calist))))
(define-calist-field-match-method t (function calist-default-field-match-method))
(defsubst calist-field-match-method (field-type)
(symbol-function
(or (intern-soft (if (symbolp field-type)
(symbol-name field-type)
field-type)
calist-field-match-method-obarray)
(intern-soft "t" calist-field-match-method-obarray))))
(defsubst calist-field-match (calist field-type field-value)
(funcall (calist-field-match-method field-type)
calist field-type field-value))
(defun ctree-match-calist (rule-tree alist)
"Return matched condition-alist if ALIST matches RULE-TREE."
(if (null rule-tree)
alist
(let ((type (car rule-tree))
(choices (cdr rule-tree))
default)
(catch 'tag
(while choices
(let* ((choice (car choices))
(choice-value (car choice)))
(if (eq choice-value t)
(setq default choice)
(let ((ret-alist (calist-field-match alist type (car choice))))
(if ret-alist
(throw 'tag
(if (cdr choice)
(ctree-match-calist (cdr choice) ret-alist)
ret-alist))
))))
(setq choices (cdr choices)))
(if default
(let ((ret-alist (calist-field-match alist type t)))
(if ret-alist
(if (cdr default)
(ctree-match-calist (cdr default) ret-alist)
ret-alist))))
))))
(defun ctree-match-calist-partially (rule-tree alist)
"Return matched condition-alist if ALIST matches RULE-TREE."
(if (null rule-tree)
alist
(let ((type (car rule-tree))
(choices (cdr rule-tree))
default)
(catch 'tag
(while choices
(let* ((choice (car choices))
(choice-value (car choice)))
(if (eq choice-value t)
(setq default choice)
(let ((ret-alist (calist-field-match alist type (car choice))))
(if ret-alist
(throw 'tag
(if (cdr choice)
(ctree-match-calist-partially
(cdr choice) ret-alist)
ret-alist))
))))
(setq choices (cdr choices)))
(if default
(let ((ret-alist (calist-field-match alist type t)))
(if ret-alist
(if (cdr default)
(ctree-match-calist-partially (cdr default) ret-alist)
ret-alist)))
(calist-field-match alist type t))
))))
(defun ctree-find-calist (rule-tree alist &optional all)
"Return list of condition-alist which matches ALIST in RULE-TREE.
If optional argument ALL is specified, default rules are not ignored
even if other rules are matched for ALIST."
(if (null rule-tree)
(list alist)
(let ((type (car rule-tree))
(choices (cdr rule-tree))
default dest)
(while choices
(let* ((choice (car choices))
(choice-value (car choice)))
(if (eq choice-value t)
(setq default choice)
(let ((ret-alist (calist-field-match alist type (car choice))))
(if ret-alist
(if (cdr choice)
(let ((ret (ctree-find-calist
(cdr choice) ret-alist all)))
(while ret
(let ((elt (car ret)))
(or (member elt dest)
(setq dest (cons elt dest))
))
(setq ret (cdr ret))
))
(or (member ret-alist dest)
(setq dest (cons ret-alist dest)))
)))))
(setq choices (cdr choices)))
(or (and (not all) dest)
(if default
(let ((ret-alist (calist-field-match alist type t)))
(if ret-alist
(if (cdr default)
(let ((ret (ctree-find-calist
(cdr default) ret-alist all)))
(while ret
(let ((elt (car ret)))
(or (member elt dest)
(setq dest (cons elt dest))
))
(setq ret (cdr ret))
))
(or (member ret-alist dest)
(setq dest (cons ret-alist dest)))
))))
)
dest)))
(defun calist-to-ctree (calist)
"Convert condition-alist CALIST to condition-tree."
(if calist
(let* ((cell (car calist)))
(cons (car cell)
(list (cons (cdr cell)
(calist-to-ctree (cdr calist))
))))))
(defun ctree-add-calist-strictly (ctree calist)
"Add condition CALIST to condition-tree CTREE without default clause."
(cond ((null calist) ctree)
((null ctree)
(calist-to-ctree calist)
)
(t
(let* ((type (car ctree))
(values (cdr ctree))
(ret (assoc type calist)))
(if ret
(catch 'tag
(while values
(let ((cell (car values)))
(if (equal (car cell)(cdr ret))
(throw 'tag
(setcdr cell
(ctree-add-calist-strictly
(cdr cell)
(delete ret (copy-alist calist)))
))))
(setq values (cdr values)))
(setcdr ctree (cons (cons (cdr ret)
(calist-to-ctree
(delete ret (copy-alist calist))))
(cdr ctree)))
)
(catch 'tag
(while values
(let ((cell (car values)))
(setcdr cell
(ctree-add-calist-strictly (cdr cell) calist))
)
(setq values (cdr values))))
)
ctree))))
(defun ctree-add-calist-with-default (ctree calist)
"Add condition CALIST to condition-tree CTREE with default clause."
(cond ((null calist) ctree)
((null ctree)
(let* ((cell (car calist))
(type (car cell))
(value (cdr cell)))
(cons type
(list (list t)
(cons value (calist-to-ctree (cdr calist)))))
))
(t
(let* ((type (car ctree))
(values (cdr ctree))
(ret (assoc type calist)))
(if ret
(catch 'tag
(while values
(let ((cell (car values)))
(if (equal (car cell)(cdr ret))
(throw 'tag
(setcdr cell
(ctree-add-calist-with-default
(cdr cell)
(delete ret (copy-alist calist)))
))))
(setq values (cdr values)))
(if (assq t (cdr ctree))
(setcdr ctree
(cons (cons (cdr ret)
(calist-to-ctree
(delete ret (copy-alist calist))))
(cdr ctree)))
(setcdr ctree
(list* (list t)
(cons (cdr ret)
(calist-to-ctree
(delete ret (copy-alist calist))))
(cdr ctree)))
))
(catch 'tag
(while values
(let ((cell (car values)))
(setcdr cell
(ctree-add-calist-with-default (cdr cell) calist))
)
(setq values (cdr values)))
(let ((cell (assq t (cdr ctree))))
(if cell
(setcdr cell
(ctree-add-calist-with-default (cdr cell)
calist))
(let ((elt (cons t (calist-to-ctree calist))))
(or (member elt (cdr ctree))
(setcdr ctree (cons elt (cdr ctree)))
))
)))
)
ctree))))
(defun ctree-set-calist-strictly (ctree-var calist)
"Set condition CALIST in CTREE-VAR without default clause."
(set ctree-var
(ctree-add-calist-strictly (symbol-value ctree-var) calist)))
(defun ctree-set-calist-with-default (ctree-var calist)
"Set condition CALIST to CTREE-VAR with default clause."
(set ctree-var
(ctree-add-calist-with-default (symbol-value ctree-var) calist)))
;;; @ end
;;;
(require 'product)
(product-provide (provide 'calist) (require 'apel-ver))
;;; calist.el ends here