From 740fbab328d2043c55edede5cd737207cc24c14c Mon Sep 17 00:00:00 2001 From: Gregory Heytings Date: Tue, 6 Apr 2021 19:30:16 +0000 Subject: [PATCH] New user option to scroll isearch matches * lisp/isearch.el (isearch-allow-match-scroll): New user option. (isearch-scroll-up, isearch-scroll-down): New commands to scroll the current search match, that complement the existing isearch-beginning-of-buffer and isearch-end-of-buffer commands. (isearch-pre-command-hook): Handle the new option and commands. * etc/NEWS: Mention the new user option. * doc/emacs/search.texi: Document the new user option. --- doc/emacs/search.texi | 8 ++++++++ etc/NEWS | 8 ++++++++ lisp/isearch.el | 42 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/doc/emacs/search.texi b/doc/emacs/search.texi index f3c42bcea7..2ae7fd24b2 100644 --- a/doc/emacs/search.texi +++ b/doc/emacs/search.texi @@ -587,6 +587,14 @@ or the selected window and frame. The command must not itself attempt an incremental search. This feature is disabled if @code{isearch-allow-scroll} is @code{nil} (which it is by default). +@vindex isearch-allow-match-scroll + Likewise, if you change the variable @code{isearch-allow-match-scroll} +to a non-@code{nil} value, this enables the use of the keyboard scrolling +commands @kbd{M-<}, @kbd{M->}, @kbd{C-v} and @kbd{M-v}, to move +respectively to the first first occurrence of the current search +string in the buffer, the last one, the first one after the current +window, and the last one before the current window. + @item Motion Commands @cindex motion commands, during incremental search When @code{isearch-yank-on-move} is customized to @code{shift}, diff --git a/etc/NEWS b/etc/NEWS index c8400ba8c2..411ea72e7c 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -367,6 +367,14 @@ 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-allow-match-scroll'. +When this option is set, the commands 'beginning-of-buffer', +'end-of-buffer', 'scroll-up-command' and 'scroll-down-command' move +respectively to the first first occurrence of the current search +string in the buffer, the last one, the first one after the current +window, and the last one before the current window. + ** Outline +++ diff --git a/lisp/isearch.el b/lisp/isearch.el index 943e24aa56..ed7b62d2ee 100644 --- a/lisp/isearch.el +++ b/lisp/isearch.el @@ -1942,6 +1942,23 @@ type \\[isearch-repeat-backward] with a numeric argument." (goto-char (point-max)) (isearch-repeat 'backward arg))) +(defun isearch-scroll-up () + "Go to the first occurrence of the current search string after window. +Move point to the end of the window, and search forward." + (interactive) + (setq isearch-just-started t) + (goto-char (window-end)) + (isearch-repeat 'forward)) + +(defun isearch-scroll-down () + "Go to the first occurrence of the current search string before window. +Move point to the beginning of the window, recenter it, and search backward." + (interactive) + (setq isearch-just-started t) + (goto-char (window-start)) + (recenter nil t) + (isearch-repeat 'backward)) + ;;; Toggles for `isearch-regexp-function' and `search-default-mode'. (defmacro isearch-define-mode-toggle (mode key function &optional docstring &rest body) @@ -2840,12 +2857,30 @@ If non-nil, scrolling commands can be used in Isearch mode. However, you cannot scroll far enough that the current match is no longer visible (is off screen). But if the value is `unlimited' that limitation is removed and you can scroll any distance off screen. -If nil, scrolling commands exit Isearch mode." +If nil, scrolling commands exit Isearch mode. +See also the related option `isearch-allow-match-scroll'." :type '(choice (const :tag "Scrolling exits Isearch" nil) (const :tag "Scrolling with current match on screen" t) (const :tag "Scrolling with current match off screen" unlimited)) :group 'isearch) +(put 'beginning-of-buffer 'isearch-match-scroll 'isearch-beginning-of-buffer) +(put 'end-of-buffer 'isearch-match-scroll 'isearch-end-of-buffer) +(put 'scroll-up-command 'isearch-match-scroll 'isearch-scroll-up) +(put 'scroll-down-command 'isearch-match-scroll 'isearch-scroll-down) + +(defcustom isearch-allow-match-scroll nil + "Whether scrolling to another match is allowed during incremental search. +If non-nil, the four scrolling commands `beginning-of-buffer', +`end-of-buffer', `scroll-up-command' and `scroll-down-command' move +respectively to the first first occurrence of the current search string in +the buffer, the last one, the first one after the current window, and the +last one before the current window. +See also the related option `isearch-allow-scroll'." + :type '(choice (const :tag "Off" nil) + (const :tag "On" t)) + :group 'isearch) + (defcustom isearch-allow-prefix t "Whether prefix arguments are allowed during incremental search. If non-nil, entering a prefix argument will not terminate the @@ -2947,6 +2982,11 @@ See more for options in `search-exit-option'." ;; Optionally edit the search string instead of exiting. ((eq search-exit-option 'edit) (setq this-command 'isearch-edit-string)) + ;; Handle match scrolling functions. + ((and isearch-allow-match-scroll + (symbolp this-command) + (get this-command 'isearch-match-scroll)) + (setq this-command (get this-command 'isearch-match-scroll))) ;; Handle a scrolling function or prefix argument. ((or (and isearch-allow-prefix (memq this-command '(universal-argument universal-argument-more -- 2.30.2