Skip to content

Commit f7e6084

Browse files
LaurenceWarnebbatsov
authored andcommitted
Add new custom variable 'projectile-cmd-hist-ignoredups'
Add new custom variable 'projectile-cmd-hist-ignoredups', which can be used to tweak how duplicates are dealt with in projectile's command history. The custom variable is identical in behaviour to 'eshell-hist-ignoredups'. Specifically, the existing default behavior is maintained with the value of t, which means consecutive duplicates are ignored. A value of 'erase means only the last duplicate is kept, whilst a value of nil means all duplicates are kept.
1 parent 55e9026 commit f7e6084

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* [#1870](https://github.com/bbatsov/projectile/pull/1870): Add package command for CMake projects.
99
* [#1875](https://github.com/bbatsov/projectile/pull/1875): Add support for Sapling VCS.
1010
* [#1876](https://github.com/bbatsov/projectile/pull/1876): Add support for Jujutsu VCS.
11+
* [#1877](https://github.com/bbatsov/projectile/pull/1877): Add custom variable `projectile-cmd-hist-ignoredups`.
1112

1213
## 2.8.0 (2023-10-13)
1314

doc/modules/ROOT/pages/projects.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,3 +838,9 @@ external command or an Emacs Lisp function:
838838
In addition caching of commands can be disabled by setting the variable
839839
`projectile-project-enable-cmd-caching` is to `nil`. This is useful for
840840
preset-based CMake projects.
841+
842+
By default, Projectile will not add consecutive duplicate commands to its
843+
command history. To alter this behaviour you can use `projectile-cmd-hist-ignoredups`.
844+
The default value of `t` means consecutive duplicates are ignore, a value
845+
of `nil` means nothing is ignored, and a value of `'erase'` means only
846+
the last duplicate is kept in the command history.

projectile.el

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,17 @@ If the value is nil, there is no limit to the opend buffers count."
874874
:type 'integer
875875
:package-version '(projectile . "2.2.0"))
876876

877+
(defcustom projectile-cmd-hist-ignoredups t
878+
"Controls when inputs are added to projectile's command history.
879+
880+
A value of t means consecutive duplicates are ignored.
881+
A value of `erase' means only the last duplicate is kept.
882+
A value of nil means nothing is ignored."
883+
:type '(choice (const :tag "Don't ignore anything" nil)
884+
(const :tag "Ignore consecutive duplicates" t)
885+
(const :tag "Only keep last duplicate" erase))
886+
:package-version '(projectile . "2.9.0"))
887+
877888
(defvar projectile-project-test-suffix nil
878889
"Use this variable to override the current project's test-suffix property.
879890
It takes precedence over the test-suffix for the project type when set.
@@ -5227,8 +5238,17 @@ The command actually run is returned."
52275238
(when command-map
52285239
(puthash default-directory command command-map)
52295240
(let ((hist (projectile--get-command-history project-root)))
5230-
(unless (string= (car-safe (ring-elements hist)) command)
5231-
(ring-insert hist command))))
5241+
(cond
5242+
((eq projectile-cmd-hist-ignoredups t)
5243+
(unless (string= (car-safe (ring-elements hist)) command)
5244+
(ring-insert hist command)))
5245+
((eq projectile-cmd-hist-ignoredups 'erase)
5246+
(let ((idx (ring-member hist command)))
5247+
(while idx
5248+
(ring-remove hist idx)
5249+
(setq idx (ring-member hist command))))
5250+
(ring-insert hist command))
5251+
(t (ring-insert hist command)))))
52325252
(when save-buffers
52335253
(save-some-buffers (not compilation-ask-about-save)
52345254
(lambda ()

test/projectile-test.el

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,25 +2138,43 @@ projectile-process-current-project-buffers-current to have similar behaviour"
21382138
(expect 'async-shell-command :to-have-been-called-with "cmd" nil nil))))
21392139

21402140
(describe "projectile--run-project-cmd"
2141-
(it "command history is not duplicated"
2141+
2142+
(before-each
21422143
(spy-on 'projectile-run-compilation)
21432144
(spy-on 'projectile-maybe-read-command :and-call-fake
21442145
(lambda (arg default-cmd prompt) default-cmd))
21452146
;; Stops projectile--run-project-cmd from creating a new directory for
21462147
;; the compilation dir
2147-
(spy-on 'file-directory-p :and-return-value t)
2148+
(spy-on 'file-directory-p :and-return-value t))
2149+
2150+
(it "projectile-cmd-hist-ignoredups set to t"
2151+
21482152
(let ((command-map (make-hash-table :test 'equal))
2153+
(projectile-cmd-hist-ignoredups t)
21492154
;; history is based on the project root, so we set it to a random
21502155
;; path to ensure there are no existing commands in history
21512156
(projectile-project-root "/a/random/path"))
21522157
(projectile--run-project-cmd "foo" command-map)
21532158
(projectile--run-project-cmd "foo" command-map)
2159+
(projectile--run-project-cmd "bar" command-map)
2160+
(projectile--run-project-cmd "foo" command-map)
2161+
(expect 'projectile-run-compilation :to-have-been-called-times 4)
2162+
(expect (ring-elements
2163+
(projectile--get-command-history projectile-project-root))
2164+
:to-equal '("foo" "bar" "foo"))))
2165+
2166+
(it "projectile-cmd-hist-ignoredups set to erase"
2167+
(let ((command-map (make-hash-table :test 'equal))
2168+
(projectile-cmd-hist-ignoredups 'erase)
2169+
(projectile-project-root "/a/random/path"))
21542170
(projectile--run-project-cmd "foo" command-map)
21552171
(projectile--run-project-cmd "bar" command-map)
2172+
(projectile--run-project-cmd "foo" command-map)
2173+
(projectile--run-project-cmd "foo" command-map)
21562174
(expect 'projectile-run-compilation :to-have-been-called-times 4)
21572175
(expect (ring-elements
21582176
(projectile--get-command-history projectile-project-root))
2159-
:to-equal '("bar" "foo")))))
2177+
:to-equal '("foo" "bar")))))
21602178

21612179
(describe "projectile-test-prefix"
21622180
:var ((mock-projectile-project-types

0 commit comments

Comments
 (0)