0

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 ()
  (interactive)
  (save-some-buffers 'no-prompt)
  (recompile))

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

Drew
  • 75,699
  • 9
  • 109
  • 225

2 Answers2

0

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.

Drew
  • 75,699
  • 9
  • 109
  • 225
  • 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`. – Fran Burstall Jun 27 '22 at 22:11
0

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

(setq compilation-ask-about-save nil)

may help.

Drew
  • 75,699
  • 9
  • 109
  • 225
Fran Burstall
  • 3,665
  • 10
  • 18