]> git.eshelyaron.com Git - emacs.git/commitdiff
Erase existing duplicates in eshell-history-ring
authorRobin Joy <emacs@robinjoy.net>
Fri, 24 May 2024 12:26:39 +0000 (14:26 +0200)
committerEshel Yaron <me@eshelyaron.com>
Sun, 26 May 2024 05:56:56 +0000 (07:56 +0200)
Erase all existing duplicates instead of just the last duplicate entry
when 'eshell-hist-ignoredups' is set to 'erase'.  Multiple duplicates
can exist in case 'eshell-hist-ignoredups' was set to something else
than 'erase' in the past or if the history file contains duplicates
(bug#71107).

* lisp/eshell/em-hist.el (eshell-add-input-to-history): Remove all
duplicates from history ring.

* test/lisp/eshell/em-hist-tests.el
(em-hist-test/add-to-history/erase-existing-dups): New test.

(cherry picked from commit 984fb346fdf0d5ec9eaea6126aad0bea8823b8a3)

lisp/eshell/em-hist.el
test/lisp/eshell/em-hist-tests.el

index 21029eae1bcc00306a0a024614fa9262f5499016..b171a2850ff5d8c1d3c409329b45d098716532f3 100644 (file)
@@ -398,11 +398,9 @@ input."
              (pcase eshell-hist-ignoredups
                ('nil t)                 ; Always add to history
                ('erase                  ; Add, removing any old occurrences
-                (when-let ((old-index (ring-member eshell-history-ring input)))
-                  ;; Remove the old occurrence of this input so we can
-                  ;; add it to the end.  FIXME: Should we try to
-                  ;; remove multiple old occurrences, e.g. if the user
-                  ;; recently changed to using `erase'?
+                (while-let ((old-index (ring-member eshell-history-ring input)))
+                  ;; Remove the old occurrences of this input so we can
+                  ;; add it to the end.
                   (ring-remove eshell-history-ring old-index))
                 t)
                (_                       ; Add if not already the latest entry
index a4e1e01b124999d6578238fe244f355998683936..40e6f90478d3f8a7943f50a937dbc88bc8ec31bd 100644 (file)
@@ -163,6 +163,23 @@ elements against that; if t (the default), check against EXPECTED."
      (should (equal (ring-elements eshell-history-ring)
                     '("echo hi" "echo bye"))))))
 
+(ert-deftest em-hist-test/add-to-history/erase-existing-dups ()
+  "Test adding to history, erasing any old dups after switching to 'erase."
+  (let ((eshell-hist-ignoredups nil))
+    (with-temp-eshell
+     (eshell-insert-command "echo hi")
+     (eshell-insert-command "echo bye")
+     (eshell-insert-command "echo bye")
+     (eshell-insert-command "echo hi")
+     (eshell-insert-command "echo bye")
+     (setq eshell-hist-ignoredups 'erase)
+     (eshell-insert-command "echo hi")
+     (should (equal (ring-elements eshell-history-ring)
+                    '("echo hi" "echo bye" "echo bye" "echo bye")))
+     (eshell-insert-command "echo bye")
+     (should (equal (ring-elements eshell-history-ring)
+                    '("echo bye" "echo hi"))))))
+
 (provide 'em-hist-test)
 
 ;;; em-hist-tests.el ends here