aboutsummaryrefslogtreecommitdiff
path: root/stack-filter.el
diff options
context:
space:
mode:
authorArtur Malabarba <bruce.connor.am@gmail.com>2014-11-03 00:45:43 +0000
committerArtur Malabarba <bruce.connor.am@gmail.com>2014-11-03 00:45:43 +0000
commit8329e6ade8d250f5af3502346a9772f3804f0738 (patch)
treede26258bd492cbf4e96ce6f31012cae4549937bb /stack-filter.el
parenta8e752a0454e6ce20162a741dd967ed64380225b (diff)
parent0ecf1dcfb01be97e0d98416dc8e0e2bc1664daeb (diff)
Merge branch 'master' into UI-first-attempt
Diffstat (limited to 'stack-filter.el')
-rw-r--r--stack-filter.el62
1 files changed, 48 insertions, 14 deletions
diff --git a/stack-filter.el b/stack-filter.el
index 1525a4b..4210549 100644
--- a/stack-filter.el
+++ b/stack-filter.el
@@ -32,9 +32,13 @@
;;; Customizations
+(defconst stack-filter-cache-file
+ "filters.el")
+
(defvar stack-filter
'default
- "The current filter. To customize the filter for the next call
+ "The current filter.
+To customize the filter for the next call
to `stack-core-make-request', let-bind this variable to the
output of a call to `stack-core-compile-filter'. Be careful! If
you're going to be using this new filter a lot, create a variable
@@ -42,28 +46,58 @@ for it. Creation requests count against
`stack-core-remaining-api-requests'!")
-;;; Filter compilation
+;;; Compilation
+;;; TODO allow BASE to be a precompiled filter name
(defun stack-filter-compile (&optional include exclude base)
- "Compile a StackExchange filter including fields from INCLUDE,
-excluding those from EXCLUDE, using BASE as a base filter.
+ "Compile INCLUDE and EXCLUDE into a filter derived from BASE.
INCLUDE and EXCLUDE must both be lists; BASE should be a symbol
or string."
(let ((keyword-arguments
- `((include . ,(if include (mapconcat
- #'stack-core-thing-as-string
- include ";")))
- (exclude . ,(if exclude (mapconcat
- #'stack-core-thing-as-string
- exclude ";")))
- (base . ,(if base base)))))
+ `((include . ,(if include (mapconcat
+ #'stack-core-thing-as-string
+ include ";")))
+ (exclude . ,(if exclude (mapconcat
+ #'stack-core-thing-as-string
+ exclude ";")))
+ (base . ,(if base base)))))
(let ((response (stack-core-make-request
- "filter/create"
- keyword-arguments)))
+ "filter/create"
+ keyword-arguments)))
(url-hexify-string
(cdr (assoc 'filter
- (elt response 0)))))))
+ (elt response 0)))))))
+
+
+;;; Storage and Retrieval
+
+(defun stack-filter-get (filter)
+ "Retrieve named FILTER from `stack-filter-cache-file'."
+ (cdr (assoc filter (stack-cache-get stack-filter-cache-file))))
+
+(defun stack-filter-store (name &optional filter)
+ "Store NAME as FILTER in `stack-filter-cache-file'.
+
+NAME should be a symbol and FILTER is a string as compiled by
+`stack-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 (stack-cache-get stack-filter-cache-file))
+ (entry (assoc name dict)))
+ (if entry (setcdr entry filter)
+ (setq dict (cons (cons name filter) dict)))
+
+ (stack-cache-set stack-filter-cache-file dict))))
+
+(defun stack-filter-store-all (name-filter-alist)
+ (mapc #'stack-filter-store name-filter-alist))
(provide 'stack-filter)
;;; stack-filter.el ends here