diff options
| -rw-r--r-- | README.org | 2 | ||||
| -rw-r--r-- | tests/test-wttrin-location-history.el | 27 | ||||
| -rw-r--r-- | wttrin.el | 10 |
3 files changed, 37 insertions, 2 deletions
@@ -139,7 +139,7 @@ History is capped at =wttrin-location-history-max= entries (default 20); the old (setq wttrin-location-history-max 20) #+end_src -To persist the history across Emacs restarts, enable the built-in =savehist-mode= (Wttrin registers its history variable automatically). Without it, history lasts for the session only. +To persist the history across Emacs restarts, enable the built-in =savehist-mode=. Wttrin keeps its history variable registered automatically, even if you set =savehist-additional-variables= yourself, so there is nothing else to configure. Without =savehist-mode=, history lasts for the session only. #+begin_src emacs-lisp (savehist-mode 1) diff --git a/tests/test-wttrin-location-history.el b/tests/test-wttrin-location-history.el index d03430d..d23bdcd 100644 --- a/tests/test-wttrin-location-history.el +++ b/tests/test-wttrin-location-history.el @@ -203,5 +203,32 @@ (require 'savehist) (should (memq 'wttrin--location-history savehist-additional-variables))) +(ert-deftest test-wttrin-location-history-normal-savehist-register-adds-var () + "wttrin--savehist-register adds the history variable to the save list." + (require 'savehist) + (let ((savehist-additional-variables '(kill-ring))) + (wttrin--savehist-register) + (should (memq 'wttrin--location-history savehist-additional-variables)))) + +(ert-deftest test-wttrin-location-history-boundary-savehist-register-idempotent () + "Registering when already present does not duplicate the entry." + (require 'savehist) + (let ((savehist-additional-variables '(wttrin--location-history))) + (wttrin--savehist-register) + (should (equal '(wttrin--location-history) savehist-additional-variables)))) + +(ert-deftest test-wttrin-location-history-integration-savehist-register-on-save-hook () + "The registration runs on `savehist-save-hook' so it survives a clobber." + (require 'savehist) + (should (memq 'wttrin--savehist-register savehist-save-hook))) + +(ert-deftest test-wttrin-location-history-integration-savehist-survives-clobber () + "A user setq that drops the variable is repaired before the next save." + (require 'savehist) + (let ((savehist-additional-variables '(kill-ring search-ring))) + ;; simulate the save path: savehist-save runs this hook first + (run-hooks 'savehist-save-hook) + (should (memq 'wttrin--location-history savehist-additional-variables)))) + (provide 'test-wttrin-location-history) ;;; test-wttrin-location-history.el ends here @@ -509,9 +509,17 @@ Persisted across sessions via `savehist-mode'.") ;; Declared so the byte-compiler doesn't warn; savehist defines it for real. (defvar savehist-additional-variables) -(with-eval-after-load 'savehist +(defun wttrin--savehist-register () + "Ensure `wttrin--location-history' is persisted by savehist. +Run both at load and on `savehist-save-hook', so the registration survives a +user `setq' of `savehist-additional-variables' (a common config pattern) that +would otherwise drop the entry before it could be saved." (add-to-list 'savehist-additional-variables 'wttrin--location-history)) +(with-eval-after-load 'savehist + (wttrin--savehist-register) + (add-hook 'savehist-save-hook #'wttrin--savehist-register)) + (defun wttrin--add-to-location-history (location) "Record LOCATION as a recent successful search. No-op when LOCATION is nil, empty, or already a default location. An existing |
