Alex Rivera | Logout

emacs: is before-save-hook a local variable?

Asked 2009-12-19T02:54:50.080
13

how would I figure this out?

I added delete-trailing-whitespace to the before-save-hook in my c-mode-common-hook, but it looks like delete-trailing-whitespace is getting called for every file, not just buffers using c-mode and derivatives.

Can I make the before-save-hook buffer local?

Edit
Report

1 Answer

0

Use write-contents-function instead:

write-contents-functions is a variable defined in `files.el'.
Its value is nil

  Automatically becomes buffer-local when set in any fashion.

Documentation:
List of functions to be called before writing out a buffer to a file.
If one of them returns non-nil, the file is considered already written
and the rest are not called and neither are the functions in
`write-file-functions'.

This variable is meant to be used for hooks that pertain to the
buffer's contents, not to the particular visited file; thus,
`set-visited-file-name' does not clear this variable; but changing the
major mode does clear it.

For hooks that _do_ pertain to the particular visited file, use
`write-file-functions'.  Both this variable and
`write-file-functions' relate to how a buffer is saved to file.
To perform various checks or updates before the buffer is saved,
use `before-save-hook'.

You should create a wrapper to call delete-trailing-whitespace as you want to ensure that you return nil from the wrapper, so that further processing (and eventual saving) takes place.

answered 2009-12-19T15:17:52.457

Your Answer