is non-nil.
If variable `beginning-of-defun-function' is non-nil, its value
-is called as a function to find the defun's beginning."
+is called as a function to find the defun's beginning.
+
+Return non-nil if this function successfully found the beginning
+of a defun, nil if it failed to find one."
(interactive "^p") ; change this to "P", maybe, if we ever come to pass ARG
; to beginning-of-defun-function.
(unless arg (setq arg 1))
(push-mark))
(if (or (null arg) (= arg 0)) (setq arg 1))
(let ((pos (point))
+ (success nil)
(beg (progn (when end-of-defun-moves-to-eol
(end-of-line 1))
(beginning-of-defun-raw 1) (point)))
(setq arg (1- arg))
;; We started from after the end of the previous function.
(goto-char pos))
+ ;; At this point, point either didn't move (because we started
+ ;; in between two defun's), or is at the end of a defun
+ ;; (because we started in the middle of a defun).
(unless (zerop arg)
- (beginning-of-defun-raw (- arg))
- (funcall end-of-defun-function)))
+ (when (setq success (beginning-of-defun-raw (- arg)))
+ (funcall end-of-defun-function))))
((< arg 0)
;; Moving backward.
(if (< (point) pos)
;; We started from inside a function.
(goto-char beg))
(unless (zerop arg)
- (beginning-of-defun-raw (- arg))
- (setq beg (point))
- (funcall end-of-defun-function))))
+ (when (setq success (beginning-of-defun-raw (- arg)))
+ (setq beg (point))
+ (funcall end-of-defun-function)))))
(funcall skip)
- (while (and (< arg 0) (>= (point) pos))
+ (while (and (< arg 0) (>= (point) pos) success)
;; We intended to move backward, but this ended up not doing so:
;; Try harder!
(goto-char beg)
- (beginning-of-defun-raw (- arg))
- (if (>= (point) beg)
+ (setq success (beginning-of-defun-raw (- arg)))
+ ;; If we successfully moved pass point, or there is no further
+ ;; defun beginnings anymore, stop.
+ (if (or (>= (point) beg) (not success))
(setq arg 0)
(setq beg (point))
(funcall end-of-defun-function)