Function: python-nav--beginning-of-defun

python-nav--beginning-of-defun is a byte-compiled function defined in python.el.gz.

Signature

(python-nav--beginning-of-defun &optional ARG)

Documentation

Internal implementation of python-nav-beginning-of-defun.

With positive ARG search backwards, else search forwards.

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/python.el.gz
(defun python-nav--beginning-of-defun (&optional arg)
  "Internal implementation of `python-nav-beginning-of-defun'.
With positive ARG search backwards, else search forwards."
  (when (or (null arg) (= arg 0)) (setq arg 1))
  (let* ((re-search-fn (if (> arg 0)
                           #'re-search-backward
                         #'re-search-forward))
         (line-beg-pos (line-beginning-position))
         (line-content-start (+ line-beg-pos (current-indentation)))
         (pos (point-marker))
         (min-indentation (if (python-info-current-line-empty-p)
                              most-positive-fixnum
                            (current-indentation)))
         (body-indentation
          (and (> arg 0)
               (or (and (python-info-looking-at-beginning-of-defun nil t)
                        (+ (save-excursion
                             (python-nav-beginning-of-statement)
                             (current-indentation))
                           python-indent-offset))
                   (save-excursion
                     (while
                         (and
                          (python-nav-backward-block)
                          (or (not (python-info-looking-at-beginning-of-defun))
                              (>= (current-indentation) min-indentation))
                          (setq min-indentation
                                (min min-indentation (current-indentation)))))
                     (or (and (python-info-looking-at-beginning-of-defun)
                              (+ (current-indentation) python-indent-offset))
                         0)))))
         (found
          (progn
            (when (and (python-info-looking-at-beginning-of-defun nil t)
                       (or (< arg 0)
                           ;; If looking at beginning of defun, and if
                           ;; pos is > line-content-start, ensure a
                           ;; backward re search match this defun by
                           ;; going to end of line before calling
                           ;; re-search-fn bug#40563
                           (and (> arg 0)
                                (or (python-info-continuation-line-p)
                                    (> pos line-content-start)))))
              (python-nav-end-of-statement))

            (while (and (funcall re-search-fn
                                 python-nav-beginning-of-defun-regexp nil t)
                        (or (python-syntax-context-type)
                            ;; Handle nested defuns when moving
                            ;; backwards by checking indentation.
                            (and (> arg 0)
                                 (not (= (current-indentation) 0))
                                 (>= (current-indentation) body-indentation)))))
            (and (python-info-looking-at-beginning-of-defun nil t)
                 (or (not (= (line-number-at-pos pos)
                             (line-number-at-pos)))
                     (and (>= (point) line-beg-pos)
                          (<= (point) line-content-start)
                          (> pos line-content-start)))))))
    (if found
        (progn
          (when (< arg 0)
            (python-nav-beginning-of-statement))
          (beginning-of-line 1)
          t)
      (and (goto-char pos) nil))))