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 ()
(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.

1 Answer 1


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)

(add-hook 'c-beginning-of-defun-after-hook #'resposition-window-for-c-beginning-of-defun)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.