From be03d6e994303c3f32d676194f6f31e89917013e Mon Sep 17 00:00:00 2001 From: Spencer Baugh Date: Tue, 11 Jul 2023 14:14:34 -0400 Subject: [PATCH] Support not jumping to bol in beginning-of-defun As mentioned in the commit, this default behavior by beginning-of-defun may be undesirable in some languages and major modes. I'm thinking of OCaml in particular here, but it's also arguably unwanted in Python and C++ as well, where defs may be indented inside class definitions. Let's let the user make this decision. * lisp/emacs-lisp/lisp.el (beginning-of-defun-go-bol): Add defcustom. (beginning-of-defun): Check beginning-of-defun-go-bol. --- lisp/emacs-lisp/lisp.el | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lisp/emacs-lisp/lisp.el b/lisp/emacs-lisp/lisp.el index b91d56cfb4f..e5b024b19a6 100644 --- a/lisp/emacs-lisp/lisp.el +++ b/lisp/emacs-lisp/lisp.el @@ -335,6 +335,17 @@ kill-backward-up-list (insert current-sexp)) (user-error "Not at a sexp")))) +(defcustom beginning-of-defun-go-bol t + "If non-nil, `beginning-of-defun' moves to beginning of line. + +By default, `beginning-of-defun' point moves to the beginning of +the line where a defun starts. For languages where defuns may be +indented inside nested structures like classes or modules, this +behavior may be undesirable." + :type '(choice (const :tag "Don't go to BOL in beginning-of-defun" nil) + (const :tag "Go to BOL in beginning-of-defun" t)) + :group 'lisp) + (defvar beginning-of-defun-function nil "If non-nil, function for `beginning-of-defun-raw' to call. This is used to find the beginning of the defun instead of using the @@ -367,16 +378,17 @@ beginning-of-defun value is called as a function, with argument ARG, to find the defun's beginning. -Regardless of the values of `defun-prompt-regexp' and -`beginning-of-defun-function', point always moves to the -beginning of the line whenever the search is successful." +If `beginning-of-defun-go-bol' is non-nil, point moves to the +beginning of the line if the search is successful." (interactive "^p") (or (not (eq this-command 'beginning-of-defun)) (eq last-command 'beginning-of-defun) (and transient-mark-mode mark-active) (push-mark)) (and (beginning-of-defun-raw arg) - (progn (beginning-of-line) t))) + (progn (when beginning-of-defun-go-bol + (beginning-of-line)) + t))) (defun beginning-of-defun-raw (&optional arg) "Move point to the character that starts a defun. -- 2.39.3