Subject: [PATCH] Support not jumping to bol in beginning-of-defun

From: Spencer Baugh <[email-masked]>
Date: Tue, 11 Jul 2023 14:15:35 -0400

Tags: patch

As mentioned in the commit, this default behavior by
beginning-of-defun is 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 users and major modes make this decision
on a case-by-case basis.

In GNU Emacs 29.0.92 (build 5, x86_64-pc-linux-gnu, X toolkit, cairo
 version 1.15.12, Xaw scroll bars) of 2023-07-10 built on
 igm-qws-u22796a
Repository revision: dd15432ffacbeff0291381c0109f5b1245060b1d
Repository branch: emacs-29
Windowing system distributor 'The X.Org Foundation', version 11.0.12011000
System Description: CentOS Linux 7 (Core)

Configured using:
 'configure --config-cache --with-x-toolkit=lucid
 --with-gif=ifavailable'

>From 12c4f80e046c3c6dd6996e13f332d0eb41d1f1dd Mon Sep 17 00:00:00 2001
From: Spencer Baugh <[email-masked]>
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 is 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 users and major modes make this decision
on a case-by-case basis.

* lisp/emacs-lisp/lisp.el (beginning-of-defun-go-beginning-of-line):
Add variable.
(beginning-of-defun): Check variable.
---
 lisp/emacs-lisp/lisp.el | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/lisp/emacs-lisp/lisp.el b/lisp/emacs-lisp/lisp.el
index b91d56cfb4f..70c296b2c31 100644
--- a/lisp/emacs-lisp/lisp.el
+++ b/lisp/emacs-lisp/lisp.el
@@ -335,6 +335,18 @@ kill-backward-up-list
           (insert current-sexp))
       (user-error "Not at a sexp"))))
 
+(defvar beginning-of-defun-go-beginning-of-line t
+  "If non-nil, `beginning-of-defun' runs `beginning-of-line' at the end.
+
+By default, `beginning-of-defun' jumps to the beginning of the
+line with `beginning-of-line' after finding the start of the
+defun.
+
+For languages where defuns may be indented inside nested
+structures like classes or modules, this behavior may be
+undesirable.  Major modes for such languages can set this
+variable to nil to avoid it.")
+
 (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
@@ -376,7 +388,9 @@ 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-beginning-of-line
+                (beginning-of-line))
+              t)))
 
 (defun beginning-of-defun-raw (&optional arg)
   "Move point to the character that starts a defun.
-- 
2.39.3