Reputation: 23592
Trimming trailing whitespace works fine on all non-JS files. I've got these lines in my .emacs:
(add-hook 'before-save-hook 'delete-trailing-whitespace)
(add-hook 'before-save-hook 'whitespace-cleanup)
I've also used M-x customize-group <enter> js2-mode
to set:
Js2 Cleanup Whitespace: [Hide Value] [Toggle] on (non-nil)
[State]: SAVED and set.
Non-nil to invoke `delete-trailing-whitespace' before saves.
But it still doesn't trim whitespace on saves! What am I missing?
Edited to add: in response to Drew's suggestions,
I'm not sure how to investigate how js2-mode is intercepting/preventing the before-save-hooks from being triggered.
Upvotes: 2
Views: 408
Reputation: 699
I got it to work by wrapping delete-trailing-whitespace in a lambda instead of quote.
(add-hook 'js2-mode-hook (lambda ()
(add-hook 'before-save-hook (lambda () (delete-trailing-whitespace)))
Upvotes: 1
Reputation: 30701
Test whether delete-trailing-whitespace
works on JS files when you invoke it normally (manually).
Try without whitespace-cleanup
on the same hook -- IOW, simplify to see what the problem is.
Load the source file (simple.el
) that defines delete-trailing-whitespace
. Then M-x debug-on-entry delete-trailing-whitespace
. Then save a JS file and see whether d-t-w
even gets called. If it does, step through the debugger to find out whether or not it deletes whitespace (and the whitespace is perhaps put back afterward), and if not, why not.
If it is never called, then load the file (files.el
) that defines the function (basic-save-buffer
) that invokes buffer-save-hook
. M-x cancel-debug-on-entry RET
to cancel the first, then M-x debug-on-entry basic-save-buffer
. Similarly, see whether the hook is run, if not, why not, if so, why it doesn't DTRT, etc.
Note that basic-save-hook
is not run to completion if one of the hook functions raises an error. See what functions are on the hook, etc. Determine whether an error is preventing TRT.
You get the idea: investigate.
Upvotes: 4