tags:

views:

326

answers:

3

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?

+1  A: 

Never wanted to do this before, but this should work:

(set (make-local-variable 'before-save-hook) '((lambda() (rg-msg "foobie"))))

In general C-h v will prompt for a variable name and display a description telling you whether the var is buffer-local.

before-save-hook is a variable defined in `files.el'. Its value is nil

This variable is potentially risky when used as a file local variable.

Documentation: Normal hook that is run before a buffer is saved to its file.

You can customize this variable.

vs.

next-error-function is a variable defined in `simple.el'. Its value is nil

Automatically becomes buffer-local when set in any fashion. This variable is potentially risky when used as a file local variable.

Documentation: Function to use to find the next error in the current buffer. The function is called with 2 parameters:

[...]

rgiar
+4  A: 

Add it to local-write-file-hooks instead:

(add-hook 'c-mode-common-hook
      (lambda()
        (add-hook 'local-write-file-hooks
              '(lambda()
                 (save-excursion
                   (delete-trailing-whitespace))))))

As the Emacs Lisp Reference Manual explains:

This works just like write-file-hooks, but it is intended to be made buffer-local in particular buffers, and used for hooks that pertain to the file name or the way the buffer contents were obtained.

The variable is marked as a permanent local, so that changing the major mode does not alter a buffer-local value. This is convenient for packages that read "file" contents in special ways, and set up hooks to save the data in a corresponding way.

This works properly for me in Emacs 23.1 (i.e., it deletes all trailing whitespace from C files but preserves trailing whitespace in all other file types).

Emerick Rogul
A: 

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.

Joe Casadonte