emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Proposal: add a binding for `imenu' under M-g


From: Stefan Monnier
Subject: Re: Proposal: add a binding for `imenu' under M-g
Date: Thu, 21 Apr 2022 09:14:17 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux)

John Yates [2022-04-20 11:53:24] wrote:
> On Wed, Apr 20, 2022 at 8:11 AM Stefan Monnier <monnier@iro.umontreal.ca> 
> wrote:
>> I personally use `C-s` to go to
>> a particular line number (with a hack that adds line number NN as
>> a match when searching for "NN")
> Would you be willing to share that snippet?

I think the patch below is the relevant part.


        Stefan


diff --git a/lisp/isearch.el b/lisp/isearch.el
index be0227b6e75..17bf2b69828 100644
--- a/lisp/isearch.el
+++ b/lisp/isearch.el
@@ -2778,6 +2828,8 @@ isearch-search-and-update
            ;; Unsuccessful regexp search may become successful by
            ;; addition of characters which make isearch-string valid
            isearch-regexp
+           ;; Unsuccessful line search may become successful by adding chars.
+           (string-match "\\`[0-9]+\\'" isearch-string)
            ;; If the string was found but was completely invisible,
            ;; it might now be partly visible, so try again.
            (prog1 isearch-hidden (setq isearch-hidden nil)))
@@ -3522,7 +3572,7 @@ isearch-lazy-count-format
 
 ;; Searching
 
-(defvar isearch-search-fun-function 'isearch-search-fun-default
+(defvar isearch-search-fun-function #'isearch-search-fun-default
   "Non-default value overrides the behavior of `isearch-search-fun-default'.
 This variable's value should be a function, which will be called
 with no arguments, and should return a function that takes three
@@ -3536,7 +3586,7 @@ isearch-search-fun-function
 (defun isearch-search-fun ()
   "Return the function to use for the search.
 Can be changed via `isearch-search-fun-function' for special needs."
-  (funcall (or isearch-search-fun-function 'isearch-search-fun-default)))
+  (funcall (or isearch-search-fun-function #'isearch-search-fun-default)))
 
 (defun isearch--lax-regexp-function-p ()
   "Non-nil if next regexp-function call should be lax."
@@ -3634,8 +3684,34 @@ isearch-search
            (retry t))
        (setq isearch-error nil)
        (while retry
-         (setq isearch-success
-               (isearch-search-string isearch-string nil t))
+         (let ((start (point))
+               (res
+                 (isearch-search-string isearch-string nil t)))
+           ;; Look for a possible line-number.
+           (when (and (save-match-data
+                        (string-match "\\`[0-9]+\\'" isearch-string))
+                      (not isearch-regexp))
+             ;; Search from the barrier when entering the line-number and
+             ;; not finding any other match.
+             (unless (or res (equal isearch-string (isearch--state-string (car 
isearch-cmds))))
+               (setq start isearch-barrier))
+             (let ((dest (save-excursion
+                            (goto-char (point-min))
+                            (forward-line
+                             (1- (string-to-number isearch-string)))
+                           (point))))
+               ;; Match the line-number if relevant.
+               (when (and (funcall (if isearch-forward '> '<) dest start)
+                          (or (funcall (if isearch-forward '< '>)
+                                       dest (point))
+                              (null res)))
+                 (goto-char dest)
+                 (set-match-data (list (point)
+                                       (if isearch-forward
+                                           (progn (end-of-line) (point))
+                                         (line-end-position))))
+                 (setq res (point)))))
+           (setq isearch-success res))
          ;; Clear RETRY unless the search predicate says
          ;; to skip this search hit.
          (if (or (not isearch-success)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]