1
0
Fork 0

Compare commits

...

5 commits

Author SHA1 Message Date
Bram Schoenmakers 33d0a97515 Add back package version 2023-06-17 23:06:51 +02:00
Bram Schoenmakers 4890e721ce Change function names to imply that state may be saved
Because state is only saved when the package is available and active.
2023-06-16 11:57:24 +02:00
Bram Schoenmakers d3980cd8f7 Update README 2023-06-16 09:49:02 +02:00
Bram Schoenmakers 3a839161b9 Declare functions and variables from external packages 2023-06-14 23:01:08 +02:00
Bram Schoenmakers e514140713 Use named functions for supported packages
No anonymous lambdas. Should make it a bit easier to configure the
persist-state-saving-functions variable.
2023-06-13 20:39:57 +02:00
2 changed files with 55 additions and 36 deletions

View file

@ -73,14 +73,16 @@ This table shows which variables can be customized.
#+RESULTS:
| Custom variable | Description |
|--------------------------------+-------------------------------------------------------------------------|
| persist-state-mode | Non-nil if Persist-State mode is enabled. |
| persist-state-mode-hook | Hook run after entering or leaving `persist-state-mode'. |
| persist-state-save-interval | Interval (in seconds) to persist state. |
| persist-state-saving-functions | A list of functions that should be executed as part of saving state. |
| persist-state-wait-idle | When it's time to save, wait for this amount of idle time (in seconds). |
To add custom functions that should be executed, add functions to =persist-state-functions=. E.g. to save bookmarks at regular intervals:
To add your own save functions, simply add those to the =persist-state-functions= variable. For example, if you use the [[https://github.com/nullman/emacs-org-visibility][org-visibility]] package, you could save the heading visibility data on a regular basis as well:
#+begin_src elisp
(add-to-list 'persist-state-functions #'bookmark-save)
(add-to-list 'persist-state-functions #'org-visibility-save-all-buffers)
#+end_src
** COMMENT Attribution :noexport:

View file

@ -5,6 +5,7 @@
;; Author: Bram Schoenmakers <me@bramschoenmakers.nl>
;; Maintainer: Bram Schoenmakers <me@bramschoenmakers.nl>
;; Created: 05 May 2023
;; Package-Version: 0.2
;; Package-Requires: ((emacs "28.2"))
;; Keywords: convenience
;; URL: https://codeberg.org/bram85/emacs-persist-state.git
@ -47,11 +48,6 @@
"Persist the Emacs state at regular intervals."
:group 'convenience)
(defcustom persist-state-saving-functions nil
"A list of functions that should be executed as part of saving state."
:type '(repeat function)
:group 'persist-state)
(defvar persist-state--save-state-timer nil
"This variable holds the timer object to trigger regular saves.")
@ -67,25 +63,53 @@
:type '(integer)
:group 'persist-state)
(declare-function bookmark-save "ext:bookmark")
(defun persist-state--maybe-save-bookmarks ()
"Save bookmarks if the built-in bookmark package is active."
(when (bound-and-true-p bookmark-save-flag)
(bookmark-save)))
(declare-function desktop-save "desktop")
(defvar desktop-path)
(defun persist-state--maybe-save-desktop ()
"Save the desktop if the built-in desktop.el package is active."
(when (bound-and-true-p desktop-save-mode)
(desktop-save (car desktop-path) nil t)))
(declare-function eshell-save-some-history "ext:em-hist")
(defun persist-state--maybe-save-eshell ()
"Save the Eshell history if active."
(when (bound-and-true-p eshell-hist-mode)
(eshell-save-some-history)))
(declare-function prescient--save "ext:prescient")
(defun persist-state--maybe-save-prescient ()
"Save the prescient data if the package is active."
(when (bound-and-true-p prescient-persist-mode)
(prescient--save)))
(declare-function recentf-save-list "ext:recentf")
(defun persist-state--maybe-save-recentf ()
"Save the list of recent files if the built-in recentf package is active."
(when (bound-and-true-p recentf-mode)
(recentf-save-list)))
(declare-function savehist-autosave "ext:savehist")
(defun persist-state--maybe-save-savehist ()
"Save the history variables if the built-in savehist package is active."
(when (bound-and-true-p savehist-mode)
(savehist-autosave)))
(defvar persist-state-supported-packages-alist
`((bookmark . (:function (lambda () (when bookmark-save-flag
(bookmark-save)))))
(desktop . (:function (lambda () (when desktop-save-mode
(desktop-save (car desktop-path) nil t)))))
(em-hist . (:function eshell-save-some-history
`((bookmark . (:function persist-state--maybe-save-bookmarks))
(desktop . (:function persist-state--maybe-save-desktop))
(em-hist . (:function persist-state--maybe-save-eshell
:label "Eshell history"))
(prescient . (:function (lambda () (when prescient-persist-mode
(prescient--save)))
(prescient . (:function persist-state--maybe-save-prescient
:label "Prescient.el"
:url "https://github.com/radian-software/prescient.el"))
(recentf . (:function recentf-save-list))
(savehist . (:function (lambda () (when savehist-mode
(savehist-autosave))))))
(recentf . (:function persist-state--maybe-save-recentf))
(savehist . (:function persist-state--maybe-save-savehist)))
"A list of packages supported by persist-state.
Each package is a cons cell with the package name and a plist with:
@ -93,6 +117,13 @@ Each package is a cons cell with the package name and a plist with:
- :label (optional): a readable package name (for the README);
- :url (optional): URL to the package.")
(defcustom persist-state-saving-functions
(mapcar (lambda (pkg) (plist-get pkg :function))
(map-values persist-state-supported-packages-alist))
"A list of functions that should be executed as part of saving state."
:type '(repeat function)
:group 'persist-state)
(defun persist-state--regularly-run-on-idle (f &rest args)
"Run function F with ARGS at the configured interval (plus some idle time)."
(run-with-timer persist-state-save-interval
@ -107,22 +138,8 @@ Each package is a cons cell with the package name and a plist with:
(when (< (float-time (current-idle-time)) persist-state-save-interval)
(mapc #'funcall persist-state-saving-functions)))
(defun persist-state--enable-packages ()
"Enables all supported packages.
If a package has no arguments (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)
(let ((attrs (cdr package)))
(add-to-list 'persist-state-saving-functions
(plist-get attrs :function)))))
persist-state-supported-packages-alist))
(defun persist-state--enable ()
"Start saving the Emacs state at the configured interval."
(persist-state--enable-packages)
;; only start the timer once
(when (null (timerp persist-state--save-state-timer))