From 8a2f73f12aaec40d9ef56faba00d4a7545b0cf0d Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Tue, 15 Feb 2022 11:29:43 +0100 Subject: [PATCH] Allow savehist-additional-variables to truncate lists * lisp/savehist.el (savehist-save): Allow truncating values (bug#30943). --- etc/NEWS | 5 +++++ lisp/savehist.el | 40 +++++++++++++++++++++++++++------------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index c7aa46395d6..1b4eaca00b5 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -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. diff --git a/lisp/savehist.el b/lisp/savehist.el index aab304007b2..172acaa4e87 100644 --- a/lisp/savehist.el +++ b/lisp/savehist.el @@ -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))) -- 2.39.5