diff options
| author | Lars Ingebrigtsen | 2022-02-15 11:29:43 +0100 |
|---|---|---|
| committer | Lars Ingebrigtsen | 2022-02-15 11:29:43 +0100 |
| commit | 8a2f73f12aaec40d9ef56faba00d4a7545b0cf0d (patch) | |
| tree | b39524fd872a75ac717c534e897e900d49de3728 | |
| parent | f84f686ec9ce18fcb766a278970f4a81c8fe027c (diff) | |
| download | emacs-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/NEWS | 5 | ||||
| -rw-r--r-- | lisp/savehist.el | 40 |
2 files changed, 32 insertions, 13 deletions
| @@ -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. | ||
| 518 | Lists that are longer than the specified length will be truncated | ||
| 519 | before 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' | |||
| 60 | to modify the value of `savehist-minibuffer-history-variables'." | 60 | to 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. |
| 65 | Each element is a symbol whose value will be persisted across Emacs | 65 | Each element is a variable that will be persisted across Emacs |
| 66 | sessions that use Savehist. The contents of variables should be | 66 | sessions that use Savehist. |
| 67 | printable with the Lisp printer. You don't need to add minibuffer | 67 | |
| 68 | history variables to this list, all minibuffer histories will be | 68 | An element may be variable name (a symbol) or a cons cell of the form |
| 69 | saved automatically as long as `savehist-save-minibuffer-history' is | 69 | \(VAR . MAX-SIZE), which means to truncate VAR's value to at most |
| 70 | non-nil. | 70 | MAX-SIZE elements (if the value is a list) before saving the value. |
| 71 | |||
| 72 | The contents of variables should be printable with the Lisp | ||
| 73 | printer. You don't need to add minibuffer history variables to | ||
| 74 | this list, all minibuffer histories will be saved automatically | ||
| 75 | as long as `savehist-save-minibuffer-history' is non-nil. | ||
| 71 | 76 | ||
| 72 | User options should be saved with the Customize interface. This | 77 | User options should be saved with the Customize interface. This |
| 73 | list is useful for saving automatically updated variables that are not | 78 | 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, | |||
| 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))) |