diff options
author | Lars Ingebrigtsen <larsi@gnus.org> | 2022-02-15 11:29:43 +0100 |
---|---|---|
committer | Lars Ingebrigtsen <larsi@gnus.org> | 2022-02-15 11:29:43 +0100 |
commit | 8a2f73f12aaec40d9ef56faba00d4a7545b0cf0d (patch) | |
tree | b39524fd872a75ac717c534e897e900d49de3728 /lisp/savehist.el | |
parent | f84f686ec9ce18fcb766a278970f4a81c8fe027c (diff) | |
download | emacs-8a2f73f12aaec40d9ef56faba00d4a7545b0cf0d.tar.gz |
Allow savehist-additional-variables to truncate lists
* lisp/savehist.el (savehist-save): Allow truncating values (bug#30943).
Diffstat (limited to 'lisp/savehist.el')
-rw-r--r-- | lisp/savehist.el | 40 |
1 files changed, 27 insertions, 13 deletions
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))) |