From e91752e2d2b6c631fd774823a10868c5d7e40d8a Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sun, 2 Nov 2014 10:25:21 -0500 Subject: Add cache directory as a customizable variable --- stack-core.el | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'stack-core.el') diff --git a/stack-core.el b/stack-core.el index 9a535cd..3391d14 100644 --- a/stack-core.el +++ b/stack-core.el @@ -44,6 +44,10 @@ ;;; Constants and Customizable Options +(defcustom stack-cache-directory + (expand-file-name ".stackmode" user-emacs-directory) + "Directory containined cached files and precompiled filters.") + (defconst stack-core-api-version "2.2" "The current version of the API.") -- cgit v1.2.3 From d273e792fbc1cc7238e156e24099d25ed0b3c53e Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sun, 2 Nov 2014 11:13:41 -0500 Subject: Add function to retrieve cache files --- stack-core.el | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'stack-core.el') diff --git a/stack-core.el b/stack-core.el index 3391d14..fde06a8 100644 --- a/stack-core.el +++ b/stack-core.el @@ -245,5 +245,14 @@ entire response as a complex alist." cons-cell)))) data)))) +(defun stack-cache-get-file (filename) + "Return a buffer for FILENAME from `stack-cache-directory'." + (let ((find-file-hook nil) + (file (expand-file-name filename stack-cache-directory))) + (unless (file-exists-p stack-cache-directory) + (mkdir stack-cache-directory)) + (unless (file-exists-p file) + (find-file-noselect file)))) + (provide 'stack-core) ;;; stack-core.el ends here -- cgit v1.2.3 From 434b71bb59ceea9c6cf93fcf3b6450b31426a260 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sun, 2 Nov 2014 11:49:49 -0500 Subject: Convenience function for retrieving cache files This should be sorted somewhere... Maybe stack-cache.el needs to be its own file. --- stack-core.el | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'stack-core.el') diff --git a/stack-core.el b/stack-core.el index fde06a8..0d87f48 100644 --- a/stack-core.el +++ b/stack-core.el @@ -245,10 +245,14 @@ entire response as a complex alist." cons-cell)))) data)))) +(defun stack-cache-get-file-name (filename) + "Expands FILENAME in the context of `stack-cache-directory'." + (expand-file-name filename stack-cache-directory)) + (defun stack-cache-get-file (filename) "Return a buffer for FILENAME from `stack-cache-directory'." (let ((find-file-hook nil) - (file (expand-file-name filename stack-cache-directory))) + (file (stack-cache-get-file-name filename))) (unless (file-exists-p stack-cache-directory) (mkdir stack-cache-directory)) (unless (file-exists-p file) -- cgit v1.2.3 From a4adbdf59250807d5c1872df7f034029d6f44b08 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sun, 2 Nov 2014 11:50:42 -0500 Subject: Fix return when file already exists --- stack-core.el | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'stack-core.el') diff --git a/stack-core.el b/stack-core.el index 0d87f48..f9b9669 100644 --- a/stack-core.el +++ b/stack-core.el @@ -255,8 +255,7 @@ entire response as a complex alist." (file (stack-cache-get-file-name filename))) (unless (file-exists-p stack-cache-directory) (mkdir stack-cache-directory)) - (unless (file-exists-p file) - (find-file-noselect file)))) + (find-file-noselect file))) (provide 'stack-core) ;;; stack-core.el ends here -- cgit v1.2.3 From f349fe5d764730a0a339045a59a46d53b9a33100 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sun, 2 Nov 2014 13:11:15 -0500 Subject: Implement and use `stack-cache-get' and `-set' --- stack-core.el | 30 +++++++++++++++++++++++------- stack-filter.el | 24 +++++++++--------------- 2 files changed, 32 insertions(+), 22 deletions(-) (limited to 'stack-core.el') diff --git a/stack-core.el b/stack-core.el index f9b9669..c2aff15 100644 --- a/stack-core.el +++ b/stack-core.el @@ -249,13 +249,29 @@ entire response as a complex alist." "Expands FILENAME in the context of `stack-cache-directory'." (expand-file-name filename stack-cache-directory)) -(defun stack-cache-get-file (filename) - "Return a buffer for FILENAME from `stack-cache-directory'." - (let ((find-file-hook nil) - (file (stack-cache-get-file-name filename))) - (unless (file-exists-p stack-cache-directory) - (mkdir stack-cache-directory)) - (find-file-noselect file))) +(defun stack-cache-get (cache) + "Return the data within CACHE. + +As with `stack-cache-set', CACHE is a file name within the +context of `stack-cache-directory'." + (unless (file-exists-p stack-cache-directory) + (mkdir stack-cache-directory)) + (let ((file (stack-cache-get-file-name cache))) + (when (file-exists-p file) + (with-temp-buffer + (insert-file-contents (stack-cache-get-file-name cache)) + (read (buffer-string)))))) + +(defun stack-cache-set (cache data) + "Set the content of CACHE to DATA. + +As with `stack-cache-get', CACHE is a file name within the +context of `stack-cache-directory'." + (unless (file-exists-p stack-cache-directory) + (mkdir stack-cache-directory)) + (write-region (prin1-to-string data) nil + (stack-cache-get-file-name cache)) + data) (provide 'stack-core) ;;; stack-core.el ends here diff --git a/stack-filter.el b/stack-filter.el index e5fb54d..91c4875 100644 --- a/stack-filter.el +++ b/stack-filter.el @@ -74,9 +74,7 @@ or string." (defun stack-filter-get (filter) "Retrieve named FILTER from `stack-filter-cache-file'." - (with-current-buffer (stack-cache-get-file stack-filter-cache-file) - (or (cdr (ignore-errors (assoc filter (read (buffer-string))))) - nil))) + (cdr (assoc filter (stack-cache-get stack-filter-cache-file)))) (defun stack-filter-store (name filter) "Store NAME as FILTER in `stack-filter-cache-file'. @@ -85,17 +83,12 @@ NAME should be a symbol and FILTER is a string as compiled by `stack-filter-compile'." (unless (symbolp name) (error "Name must be a symbol: %S" name)) - (with-current-buffer (stack-cache-get-file stack-filter-cache-file) - (let* ((dict (or (ignore-errors - (read (buffer-string))) - nil)) - (entry (assoc name dict))) - (if entry (setf (cdr entry) filter) - (setq dict (cons (cons name filter) dict))) - (erase-buffer) - (insert (prin1-to-string dict)) - (save-buffer) - dict))) + (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))) ;;; TODO tail recursion should be made more efficient for the ;;; byte-compiler @@ -104,7 +97,8 @@ NAME should be a symbol and FILTER is a string as compiled by (stack-filter-store (caar name-filter-alist) (cdar name-filter-alist)) - (stack-filter-store-all (cdr name-filter-alist)))) + (stack-filter-store-all + (cdr name-filter-alist)))) (provide 'stack-filter) ;;; stack-filter.el ends here -- cgit v1.2.3