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.

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'."
  (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))
    (catch 'done
      (dotimes (_ 2)

        (when (treesit-beginning-of-thing treesit-defun-type-regexp arg)
          (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))))))))