Function: treesit-beginning-of-defun

treesit-beginning-of-defun is an interactive and byte-compiled function defined in treesit.el.gz.

Signature

(treesit-beginning-of-defun &optional ARG)

Documentation

Move backward to the beginning of a defun.

With argument ARG, do it that many times. Negative ARG means move forward to the ARGth following beginning of defun.

If search is successful, return t, otherwise return nil.

This is a tree-sitter equivalent of beginning-of-defun. Behavior of this function depends on treesit-defun-type-regexp and treesit-defun-skipper. If treesit-defun-type-regexp is not set, Emacs also looks for definition of defun in treesit-thing-settings.

Whether this goes to the innermost nested defun or a top-level one is determined by the value of treesit-defun-tactic.

View in manual

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/treesit.el.gz
(defun treesit-beginning-of-defun (&optional arg)
  "Move backward to the beginning of a defun.

With argument ARG, do it that many times.  Negative ARG means
move forward to the ARGth following beginning of defun.

If search is successful, return t, otherwise return nil.

This is a tree-sitter equivalent of `beginning-of-defun'.
Behavior of this function depends on `treesit-defun-type-regexp'
and `treesit-defun-skipper'.  If `treesit-defun-type-regexp' is
not set, Emacs also looks for definition of defun in
`treesit-thing-settings'.

Whether this goes to the innermost nested defun or a top-level
one is determined by the value of `treesit-defun-tactic'."
  (interactive "^p")
  (or (not (eq this-command 'treesit-beginning-of-defun))
      (eq last-command 'treesit-beginning-of-defun)
      (and transient-mark-mode mark-active)
      (push-mark))
  (let ((orig-point (point))
        (success nil)
        (pred (or treesit-defun-type-regexp 'defun)))
    (unless arg (setq arg 1))
    (catch 'done
      (dotimes (_ 2)

        (when (treesit-beginning-of-thing pred arg treesit-defun-tactic)
          (when treesit-defun-skipper
            (funcall treesit-defun-skipper)
            (setq success t)))

        ;; If we end up at the same point, it means we went to the
        ;; next beg-of-defun, but defun skipper moved point back to
        ;; where we started, in this case we just move one step
        ;; further.
        (if (or (eq arg 0) (not (eq orig-point (point))))
            (throw 'done success)
          (setq arg (if (> arg 0) (1+ arg) (1- arg))))))))