Subject: bug#6157: narrow-to-defun fix when point is on function beginning

From: Lennart Borgman <[email-masked]>
Date: Mon, 10 May 2010 16:51:11 +0200

`beginning-of-defun' goes to previous function when point is on the
first character of a function. This is not currently taken care of in
`narrow-to-defun'. This patch fixes this:

c:\emacs-lp\bld\emacs\emacsw32\lisp\emacs-lisp>bzr diff --old
c:\emacs-lp\bld\emacs\trunk -p trunk/:patched/ lisp.el
=== modified file 'lisp/emacs-lisp/lisp.el'
--- trunk/lisp/emacs-lisp/lisp.el	2010-04-27 17:57:32 +0000
+++ patched/lisp/emacs-lisp/lisp.el	2010-05-10 14:21:59 +0000
@@ -438,7 +438,20 @@
       ;; Try first in this order for the sake of languages with nested
       ;; functions where several can end at the same place as with
       ;; the offside rule, e.g. Python.
-      (beginning-of-defun)
+
+      ;; Finding the start of the function is a bit problematic since
+      ;; `beginning-of-defun' when we are on the first character of
+      ;; the function might go to the previous function.
+      ;;
+      ;; Therefor we first move one character forward and then call
+      ;; `beginning-of-defun'.  However now we must check that we did
+      ;; not move into the next function.
+      (let ((here (point)))
+        (unless (eobp) (forward-char))
+        (beginning-of-defun)
+        (when (< (point) here)
+          (goto-char here)
+          (beginning-of-defun)))
       (setq beg (point))
       (end-of-defun)
       (setq end (point))