1
0
Fork 0

Made package support more extensible and maintainable

This commit is contained in:
Bram Schoenmakers 2023-05-12 19:05:37 +02:00
parent a567abcf7c
commit 58f654c35d
2 changed files with 40 additions and 11 deletions

View file

@ -5,9 +5,25 @@
I don't leave Emacs often, but sometimes Emacs leaves me.
At moments like these, it's convenient to have a reasonably up to date state of recent files, bookmarks, history variables, etc. saved.
At moments like these, it's convenient to have a reasonably up to date state of recent files, bookmarks, history variables, etc. available when you restart Emacs again.
This package contains allows you to execute a function at a regular interval, and then waits for a few seconds of idleness to actually execute the function.
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:
#+begin_src elisp :exports results :results list
(mapcar (lambda (package)
(or (plist-get (cdr package) :label)
(car package)))
persist-state-supported-packages-alist)
#+end_src
#+RESULTS:
- bookmark
- desktop
- Eshell history
- recentf
- savehist
* Installation and usage
@ -29,6 +45,8 @@ Then, add functions to =persist-state-functions= which should be executed, e.g.
* Configuration
This table shows which variables can be customized.
#+begin_src emacs-lisp :exports results :results table :colnames '("Custom variable" "Description")
(let ((rows))
(mapatoms

View file

@ -41,15 +41,17 @@
:group 'persist-state)
(defvar persist-state-supported-packages
'((bookmark . bookmark-save)
(desktop . (lambda () (desktop-save (car desktop-path))))
(em-hist . eshell-save-some-history)
(recentf . recentf-save-list)
(savehist . savehist-autosave))
`((bookmark . (:function bookmark-save))
(desktop . (:function desktop-save :args (desktop-path)))
(em-hist . (:function eshell-save-some-history :label "Eshell history"))
(recentf . (:function recentf-save-list))
(savehist . (:function savehist-autosave)))
"A list of packages supported by persist-state.
Each package is a cons cell with the package name and the
function name that is responsible for saving 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).")
(defun persist-state--regularly-run-on-idle (interval idle-seconds f &rest args)
"Run function F with ARGS every INTERVAL seconds, plus IDLE-SECONDS."
@ -65,10 +67,19 @@ function name that is responsible for saving state.")
(mapc 'funcall persist-state-saving-functions)))
(defun persist-state--enable-packages ()
"Enables all supported packages."
"Enables all supported packages.
If a package has no argumunts (no `:args' attribute, the function
is added as-is, otherwise it's wrapped in a lambda performing an
`apply' call.)"
(mapc (lambda (package)
(with-eval-after-load (car package)
(add-to-list 'persist-state-saving-functions (cdr 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))))))
persist-state-supported-packages))
;;;###autoload