diff options
| -rw-r--r-- | tests/test-wttrin-saved-locations.el | 24 | ||||
| -rw-r--r-- | wttrin.el | 14 |
2 files changed, 37 insertions, 1 deletions
diff --git a/tests/test-wttrin-saved-locations.el b/tests/test-wttrin-saved-locations.el index c2f44b8..267e5cc 100644 --- a/tests/test-wttrin-saved-locations.el +++ b/tests/test-wttrin-saved-locations.el @@ -373,5 +373,29 @@ so it persists as a named entry rather than only as the favorite string." (should (null wttrin--location-history))) (testutil-wttrin-teardown))) +;;; history stays in sync with the directory + +(ert-deftest test-wttrin-saved-locations-normal-save-drops-query-from-history () + "Normal: saving a location drops its query from history, so the place lives in +the directory only and does not also appear as a separate history candidate." + (let ((wttrin-saved-locations nil) + (wttrin--location-history '("New Orleans" "Paris"))) + (cl-letf (((symbol-function 'message) (lambda (&rest _) nil))) + (wttrin-save-location "Home" "New Orleans")) + (should-not (member "New Orleans" wttrin--location-history)) + (should (member "Paris" wttrin--location-history)))) + +(ert-deftest test-wttrin-saved-locations-normal-remove-forgets-history () + "Normal: removing a saved location drops both its name and its query from +history, so a removed place does not resurface as a history candidate." + (let ((wttrin-saved-locations '(("Home" . "New Orleans"))) + (wttrin--location-history '("Home" "New Orleans" "Paris"))) + (cl-letf (((symbol-function 'yes-or-no-p) (lambda (&rest _) t)) + ((symbol-function 'message) (lambda (&rest _) nil))) + (wttrin-remove-location "Home")) + (should-not (member "Home" wttrin--location-history)) + (should-not (member "New Orleans" wttrin--location-history)) + (should (member "Paris" wttrin--location-history)))) + (provide 'test-wttrin-saved-locations) ;;; test-wttrin-saved-locations.el ends here @@ -618,6 +618,15 @@ promoted to most-recent, and the list is trimmed to (butlast wttrin--location-history (- (length wttrin--location-history) max))))))) +(defun wttrin--drop-from-location-history (&rest locations) + "Remove each non-nil string in LOCATIONS from `wttrin--location-history'. +Keeps the directory and the history disjoint: a place lives in one or the +other, never both." + (dolist (location locations) + (when (and location (stringp location)) + (setq wttrin--location-history + (delete location wttrin--location-history))))) + (defun wttrin--saved-locations () "Return `wttrin-saved-locations' as a clean list of (NAME . QUERY) pairs. Skips malformed entries — non-cons, a non-string name or query, or an empty @@ -808,6 +817,7 @@ address, else the query. Saving an existing name updates its query." (message "Cancelled") (let ((existing (assoc (string-trim name) (wttrin--saved-locations))) (saved (wttrin--put-saved-location name query))) + (wttrin--drop-from-location-history saved query) (message (if existing "Updated %s" "Saved %s") saved)))) (defun wttrin-rename-location (old new) @@ -847,7 +857,9 @@ When NAME is the favorite, it is left as a literal query with a warning." ((not (assoc name (wttrin--saved-locations))) (user-error "No saved location named %s" name)) ((yes-or-no-p (format "Remove saved location \"%s\"? " name)) - (wttrin--remove-saved-location name) + (let ((query (cdr (assoc name (wttrin--saved-locations))))) + (wttrin--remove-saved-location name) + (wttrin--drop-from-location-history name query)) (if (equal wttrin-favorite-location name) (progn (when (bound-and-true-p wttrin-mode-line-mode) |
