]> git.eshelyaron.com Git - emacs.git/commitdiff
Allow savehist-additional-variables to truncate lists
authorLars Ingebrigtsen <larsi@gnus.org>
Tue, 15 Feb 2022 10:29:43 +0000 (11:29 +0100)
committerLars Ingebrigtsen <larsi@gnus.org>
Tue, 15 Feb 2022 10:29:43 +0000 (11:29 +0100)
* lisp/savehist.el (savehist-save): Allow truncating values (bug#30943).

etc/NEWS
lisp/savehist.el

index c7aa46395d6df83efc71404ac1862cdcf0f195f8..1b4eaca00b58976c9ed05d1e6747f1c25f99cca2 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -513,6 +513,11 @@ to edit such sequences by allowing point to "enter" the sequence.
 \f
 * 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.
index aab304007b24f3f47b74426352727e77488a1f8e..172acaa4e87db89b15499e52fe65a583743079fa 100644 (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)))