aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Ingebrigtsen2022-02-15 11:29:43 +0100
committerLars Ingebrigtsen2022-02-15 11:29:43 +0100
commit8a2f73f12aaec40d9ef56faba00d4a7545b0cf0d (patch)
treeb39524fd872a75ac717c534e897e900d49de3728
parentf84f686ec9ce18fcb766a278970f4a81c8fe027c (diff)
downloademacs-8a2f73f12aaec40d9ef56faba00d4a7545b0cf0d.tar.gz
emacs-8a2f73f12aaec40d9ef56faba00d4a7545b0cf0d.zip
Allow savehist-additional-variables to truncate lists
* lisp/savehist.el (savehist-save): Allow truncating values (bug#30943).
-rw-r--r--etc/NEWS5
-rw-r--r--lisp/savehist.el40
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.
513 513
514* Changes in Specialized Modes and Packages in Emacs 29.1 514* Changes in Specialized Modes and Packages in Emacs 29.1
515 515
516---
517** 'savehist-additional-variables' can now specify variable lengths.
518Lists that are longer than the specified length will be truncated
519before saving.
520
516** Minibuffer and Completions 521** Minibuffer and Completions
517 522
518*** The "*Completions*" buffer can now be automatically selected. 523*** 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'
60to modify the value of `savehist-minibuffer-history-variables'." 60to modify the value of `savehist-minibuffer-history-variables'."
61 :type 'boolean) 61 :type 'boolean)
62 62
63(defcustom savehist-additional-variables () 63(defcustom savehist-additional-variables nil
64 "List of additional variables to save. 64 "List of additional variables to save.
65Each element is a symbol whose value will be persisted across Emacs 65Each element is a variable that will be persisted across Emacs
66sessions that use Savehist. The contents of variables should be 66sessions that use Savehist.
67printable with the Lisp printer. You don't need to add minibuffer 67
68history variables to this list, all minibuffer histories will be 68An element may be variable name (a symbol) or a cons cell of the form
69saved automatically as long as `savehist-save-minibuffer-history' is 69\(VAR . MAX-SIZE), which means to truncate VAR's value to at most
70non-nil. 70MAX-SIZE elements (if the value is a list) before saving the value.
71
72The contents of variables should be printable with the Lisp
73printer. You don't need to add minibuffer history variables to
74this list, all minibuffer histories will be saved automatically
75as long as `savehist-save-minibuffer-history' is non-nil.
71 76
72User options should be saved with the Customize interface. This 77User options should be saved with the Customize interface. This
73list is useful for saving automatically updated variables that are not 78list 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,
278 (delete-region (point) (1+ (point))))) 283 (delete-region (point) (1+ (point)))))
279 (insert "))\n")))))) 284 (insert "))\n"))))))
280 ;; Save the additional variables. 285 ;; Save the additional variables.
281 (dolist (symbol savehist-additional-variables) 286 (dolist (elem savehist-additional-variables)
282 (when (boundp symbol) 287 (let ((symbol (if (consp elem)
283 (let ((value (symbol-value symbol))) 288 (car elem)
284 (when (savehist-printable value) 289 elem)))
285 (prin1 `(setq ,symbol ',value) (current-buffer)) 290 (when (boundp symbol)
286 (insert ?\n)))))) 291 (let ((value (symbol-value symbol)))
292 (when (savehist-printable value)
293 ;; When we have a max-size, chop off the last elements.
294 (when (and (consp elem)
295 (listp value)
296 (length> value (cdr elem)))
297 (setq value (copy-sequence value))
298 (setcdr (nthcdr (cdr elem) value) nil))
299 (prin1 `(setq ,symbol ',value) (current-buffer))
300 (insert ?\n)))))))
287 ;; If autosaving, avoid writing if nothing has changed since the 301 ;; If autosaving, avoid writing if nothing has changed since the
288 ;; last write. 302 ;; last write.
289 (let ((checksum (md5 (current-buffer) nil nil savehist-coding-system))) 303 (let ((checksum (md5 (current-buffer) nil nil savehist-coding-system)))