From 4dc73269561237d04280b0a212eee603f1e73c9f Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Fri, 23 Nov 2018 00:02:56 +0200 Subject: [PATCH] Add Isearch commands for going to absolute occurrence of matches (bug#29321) * lisp/isearch.el (isearch-mode-map): Bind 'M-s M-<' to 'isearch-beginning-of-buffer' and 'isearch-end-of-buffer' to 'M-s M->'. (isearch-beginning-of-buffer, isearch-end-of-buffer): New commands. --- etc/NEWS | 8 ++++++++ lisp/isearch.el | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index dc08e1caf25..f413bbea069 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -647,6 +647,14 @@ A negative argument repeats the search in the opposite direction. This makes possible also to use a prefix argument for 'M-s .' ('isearch-forward-symbol-at-point') to find the next Nth symbol. +*** To go to the first/last occurrence of the current search string +is possible now with new commands 'isearch-beginning-of-buffer' and +'isearch-end-of-buffer' bound to 'M-s M-<' and 'M-s M->' in Isearch. +With a numeric argument, they go to the Nth absolute occurrence +counting from the beginning/end of the buffer. This complements +'C-s'/'C-r' that searches for the next Nth relative occurrence +with a numeric argument. + *** 'isearch-lazy-count' shows the current match number and total number of matches in the Isearch prompt. Customizable variables 'lazy-count-prefix-format' and 'lazy-count-suffix-format' define the diff --git a/lisp/isearch.el b/lisp/isearch.el index b05805ccd6d..5099fb39f65 100644 --- a/lisp/isearch.el +++ b/lisp/isearch.el @@ -544,6 +544,9 @@ This is like `describe-bindings', but displays only Isearch keys." (define-key map "\C-y" 'isearch-yank-kill) (define-key map "\M-s\C-e" 'isearch-yank-line) + (define-key map "\M-s\M-<" 'isearch-beginning-of-buffer) + (define-key map "\M-s\M->" 'isearch-end-of-buffer) + (define-key map (char-to-string help-char) isearch-help-map) (define-key map [help] isearch-help-map) (define-key map [f1] isearch-help-map) @@ -1622,8 +1625,12 @@ Use `isearch-exit' to quit without signaling." (defun isearch-repeat-forward (&optional arg) "Repeat incremental search forwards. -With a prefix argument, repeat the search ARG times. -A negative argument searches backwards." +With a numeric argument, repeat the search ARG times. +A negative argument searches backwards. +\\ +This command finds the next relative occurrence of the current +search string. To find the absolute occurrence from the beginning +of the buffer, type \\[isearch-beginning-of-buffer] with a numeric argument." (interactive "P") (if arg (let ((count (prefix-numeric-value arg))) @@ -1639,8 +1646,12 @@ A negative argument searches backwards." (defun isearch-repeat-backward (&optional arg) "Repeat incremental search backwards. -With a prefix argument, repeat the search ARG times. -A negative argument searches forwards." +With a numeric argument, repeat the search ARG times. +A negative argument searches forwards. +\\ +This command finds the next relative occurrence of the current +search string. To find the absolute occurrence from the end +of the buffer, type \\[isearch-end-of-buffer] with a numeric argument." (interactive "P") (if arg (let ((count (prefix-numeric-value arg))) @@ -1654,6 +1665,36 @@ A negative argument searches forwards." (isearch-repeat 'backward count)))) (isearch-repeat 'backward))) +(defun isearch-beginning-of-buffer (&optional arg) + "Go to the first occurrence of the current search string. +Move point to the beginning of the buffer and search forwards from the top. +\\ +With a numeric argument, go to the ARGth absolute occurrence counting from +the beginning of the buffer. To find the next relative occurrence forwards, +type \\[isearch-repeat-forward] with a numeric argument." + (interactive "p") + (if (and arg (< arg 0)) + (isearch-end-of-buffer (abs arg)) + ;; For the case when the match is at bobp, + ;; don't forward char in isearch-repeat + (setq isearch-just-started t) + (goto-char (point-min)) + (isearch-repeat 'forward arg))) + +(defun isearch-end-of-buffer (&optional arg) + "Go to the last occurrence of the current search string. +Move point to the end of the buffer and search backwards from the bottom. +\\ +With a numeric argument, go to the ARGth absolute occurrence counting from +the end of the buffer. To find the next relative occurrence backwards, +type \\[isearch-repeat-backward] with a numeric argument." + (interactive "p") + (if (and arg (< arg 0)) + (isearch-beginning-of-buffer (abs arg)) + (setq isearch-just-started t) + (goto-char (point-max)) + (isearch-repeat 'backward arg))) + ;;; Toggles for `isearch-regexp-function' and `search-default-mode'. (defmacro isearch-define-mode-toggle (mode key function &optional docstring &rest body) -- 2.39.5