Subject: Re: bug#47599: [External] : bug#47599: 28.0.50; Feature request improve/update isearch

From: Gregory Heytings <[email-masked]>
Date: Tue, 06 Apr 2021 23:26:30 +0000

>>> While we're improving isearch, there is another minor change that I 
>>> think would improve its behavior, namely to go to the next/previous 
>>> match when changing the search direction, instead of hitting the same 
>>> match before moving to the next/previous one with the next C-s/C-r. 
>>> This change is so small that I'm not sure it's worth creating a 
>>> defcustom for it, but you might have a different opinion.
>> 
>> Please do NOT do this.  There are reasons we want to stay at the same 
>> search hit.
>
> Okay, so I have the answer to my own question: this should become yet 
> another user option.
>

And here is the corresponding patch.

From 8276a0808a978eb124d1a71615a4995e3bc27bd2 Mon Sep 17 00:00:00 2001
From: Gregory Heytings <[email-masked]>
Date: Wed, 6 Apr 2021 23:19:19 +0000
Subject: [PATCH] User option to move to another match when changing direction
 in isearch.

* lisp/isearch.el (isearch-direction-change-changes-match): New
user option.
(isearch-repeat): Use the new user option.

* etc/NEWS: Mention the new user option.

* doc/emacs/search.texi: Document the new user option.
---
 doc/emacs/search.texi |  8 ++++++++
 etc/NEWS              |  6 ++++++
 lisp/isearch.el       | 12 ++++++++++++
 3 files changed, 26 insertions(+)

diff --git a/doc/emacs/search.texi b/doc/emacs/search.texi
index f3c42bcea7..222e5b6c96 100644
--- a/doc/emacs/search.texi
+++ b/doc/emacs/search.texi
@@ -201,6 +201,14 @@ something before the starting point, type @kbd{C-r} to switch to a
 backward search, leaving the search string unchanged.  Similarly,
 @kbd{C-s} in a backward search switches to a forward search.
 
+@cindex search, changing direction
+@vindex isearch-direction-change-changes-match
+  When you change the direction of a search, the first command you
+type will, by default, remain on the same match, and the cursor will
+move to the other end of the match.  To move to another match
+immediately, customize the variable
+@code{isearch-direction-change-changes-match} to @code{t}.
+
 @cindex search, wrapping around
 @cindex search, overwrapped
 @cindex wrapped search
diff --git a/etc/NEWS b/etc/NEWS
index d3a8748ded..73ceb96f1f 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -367,6 +367,12 @@ trying to be non-destructive.
 This command opens a new buffer called "*Memory Report*" and gives a
 summary of where Emacs is using memory currently.
 
++++
+** New user option 'isearch-direction-change-changes-match'.
+When this option is set, direction changes in Isearch move to another
+search match, if there is one, instead of moving point to the other
+end of the current match.
+
 ** Outline
 
 +++
diff --git a/lisp/isearch.el b/lisp/isearch.el
index a828c569aa..bec202516b 100644
--- a/lisp/isearch.el
+++ b/lisp/isearch.el
@@ -185,6 +185,16 @@ When `nil', never wrap, just stop at the last match."
                  (const :tag "Disable wrapping" nil))
   :version "28.1")
 
+(defcustom isearch-direction-change-changes-match nil
+  "Whether a direction change should move to another match.
+When `nil', the default, a direction change moves point to the other
+end of the current search match.
+When `t', a direction change moves to another search match, if there
+is one."
+  :type '(choice (const :tag "Remain on the same match" nil)
+                 (const :tag "Move to another match" t))
+  :version "28.1")
+
 (defvar isearch-mode-hook nil
   "Function(s) to call after starting up an incremental search.")
 
@@ -1847,6 +1857,8 @@ Use `isearch-exit' to quit without signaling."
 	      (funcall isearch-wrap-function)
 	    (goto-char (if isearch-forward (point-min) (point-max))))))
     ;; C-s in reverse or C-r in forward, change direction.
+    (if (and isearch-other-end isearch-direction-change-changes-match)
+        (goto-char isearch-other-end))
     (setq isearch-forward (not isearch-forward)
 	  isearch-success t))
 
-- 
2.30.2