From 273102a287700bb58ef76d9e40515c234cdd5604 Mon Sep 17 00:00:00 2001 From: Bram Schoenmakers Date: Sat, 13 May 2023 21:51:02 +0200 Subject: [PATCH] Preconditions and arguments should be used within :function Simplify the definition of functions. Also add some preconditions to some functions, to enable saving only when the corresponding saving mode is enabled. --- README.org | 4 +++- persist-state.el | 33 ++++++++++++++++++++------------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/README.org b/README.org index 53b046f..cf3a3ed 100644 --- a/README.org +++ b/README.org @@ -9,7 +9,9 @@ At moments like these, it's convenient to have a reasonably up to date state of This package allows you to execute a function at a regular interval. To minimize any disturbance while you're working, a few seconds of idleness will trigger the execution of all save functions. -Supported packages: +* Supported packages + +persist-state supports a number of packages out of the box: #+begin_src elisp :exports results :results list (mapcar (lambda (package) diff --git a/persist-state.el b/persist-state.el index 6e0da42..2f8b4f0 100644 --- a/persist-state.el +++ b/persist-state.el @@ -41,20 +41,30 @@ :group 'persist-state) (defvar persist-state-supported-packages-alist - `((bookmark . (:function bookmark-save)) - (desktop . (:function desktop-save :args (desktop-path))) - (em-hist . (:function eshell-save-some-history :label "Eshell history")) - (prescient . (_ :function prescient--save - :label "Prescient.el" - :url "https://github.com/radian-software/prescient.el")) + `((bookmark . (:function (lambda () (when bookmark-save-flag + (bookmark-save))))) + + (desktop . (:function (lambda () (when desktop-save-mode + (desktop-save desktop-path))))) + + (em-hist . (:function eshell-save-some-history + :label "Eshell history")) + + (prescient . (:function (lambda () (when prescient-persist-mode + (prescient--save))) + :label "Prescient.el" + :url "https://github.com/radian-software/prescient.el")) + (recentf . (:function recentf-save-list)) - (savehist . (:function savehist-autosave))) + + (savehist . (:function (lambda () (when savehist-mode + savehist-autosave))))) "A list of packages supported by persist-state. Each package is a cons cell with the package name and a plist with: - :function (mandatory): function to call to save state; -- :args (optional): arguments to be passed to the function; -- :label (optional): a readable package name (for the README).") +- :label (optional): a readable package name (for the README); +- :url (optional): URL to the package.") (defun persist-state--regularly-run-on-idle (interval idle-seconds f &rest args) "Run function F with ARGS every INTERVAL seconds, plus IDLE-SECONDS." @@ -79,10 +89,7 @@ is added as-is, otherwise it's wrapped in a lambda performing an (with-eval-after-load (car package) (let ((attrs (cdr package))) (add-to-list 'persist-state-saving-functions - (if (plist-member attrs :args) - (lambda () (apply (plist-get attrs :function) - (plist-get attrs :args))) - (plist-get attrs :function)))))) + (plist-get attrs :function))))) persist-state-supported-packages-alist)) ;;;###autoload