4

I always want to run reposition-window after c-beginning-of-defun.


I've tried this:

(advice-add 'c-beginning-of-defun :after #'reposition-window)

it results in this at execution:

Variable binding depth exceeds max-specpdl-size

I've considered hooks, but I don't know how to find a hook for c-beginning-of-defun. I don't think hooks are the way to go if it's not for modes.


I've tried redefining c-beginning-of-defun, but then I need to call to the previous version, and I get infinite recursion.


I've tried implementing my own function:

(defun my-beginning-of-defun ()
  (interactive)
  (beginning-of-defun)
  (reposition-window))
(global-set-key (kbd "C-M-a") 'my-beginning-of-defun) ;;; This doesn't bite!

but this doesn't work in C, because its major mode overrides C-M-a with the C version of the function. I could make my own version of the C function as well, and bind C-M-a in my own c-mode-hook, but... It seems like doing work that advises are designed to do.

Drew
  • 75,699
  • 9
  • 109
  • 225
Gauthier
  • 499
  • 2
  • 13

1 Answers1

3

reposition-window calls beginning-of-defun to compute the window boundaries.
So you get an infinite recursion when calling reposition-window from c-beginning-of-defun.

You can add the following guard for avoiding that recursion:

(defvar c-beginning-of-defun-after-hook nil
  "Functions to be run after `c-beginning-of-defun'.")

(advice-add 'c-beginning-of-defun :after (lambda (&rest _args) (run-hooks 'c-beginning-of-defun-after-hook)))

(defun resposition-window-for-c-beginning-of-defun (&rest args)
  "Deactivate `c-beginning-of-defun-after-hook' and call `resposition-window'."
  (let (c-beginning-of-defun-after-hook)
    (reposition-window)))

(add-hook 'c-beginning-of-defun-after-hook #'resposition-window-for-c-beginning-of-defun)
Tobias
  • 32,569
  • 1
  • 34
  • 75