master 10fc4559e1d: Fix end-of-defun with argument < -1

Filipp Gunbin <[email protected]> Fri, 17 Jul 2026 11:12:43 -0400 (EDT)
Newsgroups gmane.emacs.diffs
Message-ID <[email protected]>
branch: master
commit 10fc4559e1d906d8b3e641fad84c4758318b1492
Author: Filipp Gunbin <[email protected]>
Commit: Filipp Gunbin <[email protected]>

    Fix end-of-defun with argument < -1
    
    When end-of-defun is invoked with argument < -1 after the end of
    defun, and there is some text between the end of defun and
    point, the result is off by 1 (Bug#81305).
    * lisp/emacs-lisp/lisp.el (end-of-defun): Always move point to
    beg.
    * test/lisp/emacs-lisp/lisp-tests.el (end-of-defun-twice): Add
    test case.
---
 lisp/emacs-lisp/lisp.el            | 11 +++++------
 test/lisp/emacs-lisp/lisp-tests.el |  6 +++++-
 2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/lisp/emacs-lisp/lisp.el b/lisp/emacs-lisp/lisp.el
index 41c740912f7..1db2f5693b6 100644
--- a/lisp/emacs-lisp/lisp.el
+++ b/lisp/emacs-lisp/lisp.el
@@ -634,13 +634,12 @@ report errors as appropriate for this kind of usage."
             (funcall end-of-defun-function))))
        ((< arg 0)
         ;; Moving backward.
-        (if (< (point) pos)
-            ;; We already moved backward because we started from between
-            ;; two functions.
-            (setq arg (1+ arg))
-          ;; We started from inside a function.
-          (goto-char beg))
+        (when (< (point) pos)
+          ;; We already moved backward because we started from between
+          ;; two functions.
+          (setq arg (1+ arg)))
         (unless (zerop arg)
+          (goto-char beg)
           (when (setq success (beginning-of-defun-raw (- arg)))
             (setq beg (point))
             (funcall end-of-defun-function)))))
diff --git a/test/lisp/emacs-lisp/lisp-tests.el b/test/lisp/emacs-lisp/lisp-tests.el
index 560fd436015..eb4b12b1ffb 100644
--- a/test/lisp/emacs-lisp/lisp-tests.el
+++ b/test/lisp/emacs-lisp/lisp-tests.el
@@ -403,10 +403,14 @@ of two."
   \"docstring\"
   body)
 
-;; end
+=!p3=;; end
 "
     (goto-char p1)
     (end-of-defun 2)
+    (should (= (point) p2))
+    ;; Negative arg
+    (goto-char p3)
+    (end-of-defun -2)
     (should (= (point) p2))))
 
 ;;; mark-defun