aboutsummaryrefslogtreecommitdiff
path: root/sx-filter.el
diff options
context:
space:
mode:
authorSean Allred <code@seanallred.com>2014-11-08 14:12:42 -0500
committerSean Allred <code@seanallred.com>2014-11-08 14:12:42 -0500
commit7ed29c4dc940a871562aaa802ac53ddee4c66a27 (patch)
tree4656459efc90a16ad70a5f857630066a85805e11 /sx-filter.el
parentb6043f63e5b3437633a53bcc191214bb8a7f936b (diff)
Re-work filtering and caching
* sx-auth.el - Use new symbolic cache access * sx-cache.el - Implement symbolic cache access * sx-filter.el - Use symbolic cache access - Compile and save filters on-demand (more work to be done to this end) * sx-question.el - Symbolic filters * sx-request.el - Protection against infinitely recursing when compiling a filter This will be re-worked into requests (a front-end function) and 'raw' requests (a back-end function). The front-end will add convenience to the back-end. * test/tests.el Remove outdated tests
Diffstat (limited to 'sx-filter.el')
-rw-r--r--sx-filter.el54
1 files changed, 17 insertions, 37 deletions
diff --git a/sx-filter.el b/sx-filter.el
index 7178259..0ba8186 100644
--- a/sx-filter.el
+++ b/sx-filter.el
@@ -32,17 +32,9 @@
;;; Customizations
-(defconst sx-filter-cache-file
- "filters.el")
-
-(defvar sx-filter
- 'default
- "The current filter.
-To customize the filter for the next call to `sx-request-make',
-let-bind this variable to the output of a call to
-`sx-filter-compile'. Be careful! If you're going to be using
-this new filter a lot, create a variable for it. Creation
-requests count against `sx-request-remaining-api-requests'!")
+(defvar sx--filter-alist
+ (sx-cache-get 'filter)
+ "")
;;; Compilation
@@ -67,32 +59,20 @@ or string."
;;; Storage and Retrieval
-(defun sx-filter-get (filter)
- "Retrieve named FILTER from `sx-filter-cache-file'."
- (cdr (assoc filter (sx-cache-get sx-filter-cache-file))))
-
-(defun sx-filter-store (name &optional filter)
- "Store NAME as FILTER in `sx-filter-cache-file'.
-
-NAME should be a symbol and FILTER is a string as compiled by
-`sx-filter-compile'.
-
-If NAME is a cons cell, (car NAME) is taken to be the actual NAME
-and (cdr NAME) is taken to be the actual FILTER. In this case,
-the second argument is simply ignored."
- (let ((name (if (consp name) (car name) name))
- (filter (if (consp name) (cdr name) filter)))
- (unless (symbolp name)
- (error "Name must be a symbol: %S" name))
- (let* ((dict (sx-cache-get sx-filter-cache-file))
- (entry (assoc name dict)))
- (if entry (setcdr entry filter)
- (setq dict (cons (cons name filter) dict)))
-
- (sx-cache-set sx-filter-cache-file dict))))
-
-(defun sx-filter-store-all (name-filter-alist)
- (mapc #'sx-filter-store name-filter-alist))
+(defun sx-filter-get-var (filter-variable)
+ "Return the string representation of FILTER-VARIABLE."
+ (apply #'sx-filter-get filter-variable))
+
+(defun sx-filter-get (&optional include exclude base)
+ "Return the string representation of the given filter."
+ ;; Maybe we alreay have this filter
+ (or (cdr (assoc (list include exclude base) sx--filter-alist))
+ ;; If we don't, build it, save it, and return it.
+ (let ((filter (sx-filter-compile include exclude base)))
+ (when filter
+ (push (cons (list include exclude base) filter) sx--filter-alist)
+ (sx-cache-set 'filter sx--filter-alist)
+ filter))))
(provide 'sx-filter)
;;; sx-filter.el ends here