Initial commit
Inspired by my own configuration and now distributed as a separate package.
This commit is contained in:
commit
9780565b2b
1 changed files with 72 additions and 0 deletions
72
persist-state.el
Normal file
72
persist-state.el
Normal file
|
@ -0,0 +1,72 @@
|
|||
;;; persist-state.el --- Persist variables regularly -*- lexical-binding: t; -*-
|
||||
|
||||
;; Copyright (C) 2023 Bram Schoenmakers
|
||||
|
||||
;; Author: Bram Schoenmakers <me@bramschoenmokers.nl>
|
||||
;; Maintainer: Bram Schoenmakers <me@bramschoenmokers.nl>
|
||||
;; Created: 05 May 2023
|
||||
|
||||
;; Package-Version: 0.1
|
||||
;; Keywords: convenience
|
||||
;; URL: https://apps.bram85.nl/git/bram/emacs-persist-state
|
||||
|
||||
;; This file is not part of GNU Emacs.
|
||||
|
||||
;;; Commentary:
|
||||
|
||||
;;; Code:
|
||||
|
||||
(defgroup persist-state nil
|
||||
"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 the state of the current Emacs function."
|
||||
:type '(repeat function)
|
||||
:group 'persist-state)
|
||||
|
||||
(defvar persist-state--save-state-timer nil
|
||||
"This variable holds the timer object to trigger regular saves.")
|
||||
|
||||
(defcustom persist-state-save-interval
|
||||
(* 15 60)
|
||||
"Persist the Emacs state this amount of seconds."
|
||||
:type '(integer)
|
||||
:group 'persist-state)
|
||||
|
||||
(defcustom persist-state-wait-idle
|
||||
5
|
||||
"When it's time to save, wait for this amount of idle time (in seconds)."
|
||||
:type '(integer)
|
||||
:group 'persist-state)
|
||||
|
||||
(defun persist-state--regularly-run-on-idle (interval idle-seconds f &rest args)
|
||||
"Run function F with ARGS every INTERVAL seconds, plus IDLE-SECONDS."
|
||||
(run-with-timer interval interval
|
||||
(lambda ()
|
||||
(run-with-idle-timer idle-seconds nil
|
||||
(lambda ()
|
||||
(apply f args))))))
|
||||
|
||||
(defun persist-state--save-state ()
|
||||
"Save state but only when the user was active recently."
|
||||
(when (< (float-time (current-idle-time)) persist-state-save-interval)
|
||||
(mapc 'funcall persist-state-saving-functions)))
|
||||
|
||||
;;;###autoload
|
||||
(defun persist-state-enable ()
|
||||
"Start saving the Emacs state at the configured interval."
|
||||
(setq persist-state--save-state-timer
|
||||
(persist-state--regularly-run-on-idle persist-state-save-interval
|
||||
persist-state-wait-idle
|
||||
#'persist-state--save-state)))
|
||||
|
||||
;;;###autoload
|
||||
(defun persist-state-disable ()
|
||||
"Stop saving the Emacs state."
|
||||
(when (timerp persist-state--save-state-timer)
|
||||
(cancel-timer persist-state--save-state-timer)))
|
||||
|
||||
(provide 'persist-state)
|
||||
|
||||
;;; persist-state.el ends here
|
Loading…
Reference in a new issue