tags:

views:

128

answers:

4

Hello

GNU 23.1.1

By clicking the F5 button I can compile my project. However, I want to extend this so that any unsaved work would be saved and then compiled.

Normally I just do C-x-s to save then click F5. But can I add a line that will save without having to ask me do I want to save then it will compile, all done automatically?

; Compile program using <F5>
; Save all unsaved files here, then compile
(global-set-key [f5] 'compile)

Hope you understand me?

Many thanks for any advice,

+2  A: 

You can use (save-some-buffers 1) to save all buffers containing changes.

Wrap it up with a function together with compile like so:

(defun save-all-and-compile ()
  (save-some-buffers 1)
  (compile compile-command))

Then set F5 to run this function instead of plain compile.

Use (save-some-buffers) (no argument) if you prefer Emacs to ask you about each buffer to be saved. Also, you might want to make the compilation-command customisable, as it is with compile... I'll leave that to you though.

Michał Marczyk
I would recommend that you use a key (or a combination) in your main typing area rather than something out of the way like F5 to do this though.
Noufal Ibrahim
+8  A: 

On every Emacs I use (from the 19.34 to 23), compile do ask for saving unsaved buffer before proceeding. I'm very surprise it is not the case for you.

So just M-x compile (f5 for you) will ask to save unsaved buffer.

you can customize compilation-ask-about-save to nil if you want compile to unconditionally save all buffer, or let it to it's default non nil value if you want to be asked.

Rémi
Yes, you are right. It does ask you to save. However, I would prefer not to be prompted but to save anything on each compile. Normally I always have more than 1 buffer open and I would just like to save anything without being prompted for each compile.
robUK
then customize compilation-ask-about-save. It's just for this (save all buffer with no asking prior to compilation)?
Rémi
+1  A: 

I would have tried to have used a advice before, but Remi solution sounds better now

Notthinking
+1  A: 

Put (setq compilation-ask-about-save nil) in your .emacs to automatically save (without asking) before you compile or recompile.

Matt Curtis