changeset 55727:babf5161afd4

(mark-defun, narrow-to-defun): If moving back then fwd gets a defun that ends before point, try again moving fwd then back.
author Richard M. Stallman <rms@gnu.org>
date Sat, 22 May 2004 07:41:55 +0000
parents 9dbff3d047a0
children 9b4d490eb47f
files lisp/emacs-lisp/lisp.el
diffstat 1 files changed, 46 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/lisp/emacs-lisp/lisp.el	Sat May 22 07:40:41 2004 +0000
+++ b/lisp/emacs-lisp/lisp.el	Sat May 22 07:41:55 2004 +0000
@@ -281,15 +281,31 @@
 	    (end-of-defun)
 	    (point))))
 	(t
-	 ;; Do it 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.
-	 (push-mark (point))
-	 (beginning-of-defun)
-	 (push-mark (point) nil t)
-	 (end-of-defun)
-	 (exchange-point-and-mark)
-	 (re-search-backward "^\n" (- (point) 1) t))))
+	 (let ((opoint (point))
+	       beg end)
+	   (push-mark opoint)
+	   ;; 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)
+	   (setq beg (point))
+	   (end-of-defun)
+	   (setq end (point))
+	   (while (looking-at "^\n")
+	     (forward-line 1))
+	   (if (> (point) opoint)
+	       (progn
+		 ;; We got the right defun.
+		 (push-mark beg nil t)
+		 (goto-char end)
+		 (exchange-point-and-mark))
+	     ;; beginning-of-defun moved back one defun
+	     ;; so we got the wrong one.
+	     (goto-char opoint)
+	     (end-of-defun)
+	     (push-mark (point) nil t)
+	     (beginning-of-defun))
+	   (re-search-backward "^\n" (- (point) 1) t)))))
 
 (defun narrow-to-defun (&optional arg)
   "Make text outside current defun invisible.
@@ -298,13 +314,28 @@
   (interactive)
   (save-excursion
     (widen)
-    ;; Do it 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)
-    (let ((beg (point)))
+    (let ((opoint (point))
+	  beg end)
+      ;; 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)
+      (setq beg (point))
       (end-of-defun)
-      (narrow-to-region beg (point)))))
+      (setq end (point))
+      (while (looking-at "^\n")
+	(forward-line 1))
+      (unless (> (point) opoint)
+	;; beginning-of-defun moved back one defun
+	;; so we got the wrong one.
+	(goto-char opoint)
+	(end-of-defun)
+	(setq end (point))
+	(beginning-of-defun)
+	(setq beg (point)))
+      (goto-char end)
+      (re-search-backward "^\n" (- (point) 1) t)
+      (narrow-to-region beg end))))
 
 (defun insert-pair (arg &optional open close)
   "Enclose following ARG sexps in a pair of OPEN and CLOSE characters.