How can I prepend a function call in Elisp?

E.g. I have a key binding (C-c C-c C-t) that runs some tests (command rustic-cargo-test) and it always asks me if I want to save - I'd like to run (save-some-buffers 'no-prompt) each time before this, so it doesn't ask.

With the standard recompile function I created my own function and rebound the key, e.g.:

(defun save-and-recompile ()
  (save-some-buffers 'no-prompt)

(global-set-key (kbd "C-c C-k") 'save-and-recompile)

But this feels a bit hacky, and I would want to do it for other rustic-cargo-* functions. Is there a better way than writing a function/macro to automate the above? (Probably putting it in the rustic-hook and not making it global though!)

2 Answers 2


Do you always want recompile to do what you describe? If so, you can advise that function, giving it :before advice:

 (advice-add 'recompile :before (lambda () (save-some-buffers 'no-prompt)))

See the Elisp manual, node Advising Functions.

If you don't want recompile to always do that then I think defining your own command, as you did, is a good approach.

But I don't really understand the relation between your rustic-cargo-* commands/functions and recompile. It sounds like you might really want to advise some of those rustic-cargo-* functions.

  • The rustic-cargo-* functions all end with a call to rustic-compilation but before they do that they call rustic-compilation-process-live which, in turn, calls rustic-save-some-buffers. Commented Jun 27, 2022 at 22:11

It looks like rustic-save-some-buffers looks at compilation-ask-about-save so

(setq compilation-ask-about-save nil)

may help.

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.