Allow savehist-additional-variables to truncate lists

* lisp/savehist.el (savehist-save): Allow truncating values (bug#30943).
This commit is contained in:
Lars Ingebrigtsen 2022-02-15 11:29:43 +01:00
parent f84f686ec9
commit 8a2f73f12a
2 changed files with 32 additions and 13 deletions

View file

@ -513,6 +513,11 @@ to edit such sequences by allowing point to "enter" the sequence.
* Changes in Specialized Modes and Packages in Emacs 29.1
---
** 'savehist-additional-variables' can now specify variable lengths.
Lists that are longer than the specified length will be truncated
before saving.
** Minibuffer and Completions
*** The "*Completions*" buffer can now be automatically selected.

View file

@ -60,14 +60,19 @@ If you want to save only specific histories, use `savehist-save-hook'
to modify the value of `savehist-minibuffer-history-variables'."
:type 'boolean)
(defcustom savehist-additional-variables ()
(defcustom savehist-additional-variables nil
"List of additional variables to save.
Each element is a symbol whose value will be persisted across Emacs
sessions that use Savehist. The contents of variables should be
printable with the Lisp printer. You don't need to add minibuffer
history variables to this list, all minibuffer histories will be
saved automatically as long as `savehist-save-minibuffer-history' is
non-nil.
Each element is a variable that will be persisted across Emacs
sessions that use Savehist.
An element may be variable name (a symbol) or a cons cell of the form
\(VAR . MAX-SIZE), which means to truncate VAR's value to at most
MAX-SIZE elements (if the value is a list) before saving the value.
The contents of variables should be printable with the Lisp
printer. You don't need to add minibuffer history variables to
this list, all minibuffer histories will be saved automatically
as long as `savehist-save-minibuffer-history' is non-nil.
User options should be saved with the Customize interface. This
list is useful for saving automatically updated variables that are not
@ -278,12 +283,21 @@ If AUTO-SAVE is non-nil, compare the saved contents to the one last saved,
(delete-region (point) (1+ (point)))))
(insert "))\n"))))))
;; Save the additional variables.
(dolist (symbol savehist-additional-variables)
(when (boundp symbol)
(let ((value (symbol-value symbol)))
(when (savehist-printable value)
(prin1 `(setq ,symbol ',value) (current-buffer))
(insert ?\n))))))
(dolist (elem savehist-additional-variables)
(let ((symbol (if (consp elem)
(car elem)
elem)))
(when (boundp symbol)
(let ((value (symbol-value symbol)))
(when (savehist-printable value)
;; When we have a max-size, chop off the last elements.
(when (and (consp elem)
(listp value)
(length> value (cdr elem)))
(setq value (copy-sequence value))
(setcdr (nthcdr (cdr elem) value) nil))
(prin1 `(setq ,symbol ',value) (current-buffer))
(insert ?\n)))))))
;; If autosaving, avoid writing if nothing has changed since the
;; last write.
(let ((checksum (md5 (current-buffer) nil nil savehist-coding-system)))