2

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,

  1. Running delete-trailing whitespace manually on the file does work.
  2. Still doesn't work without whitespace cleanup, which isn't surprising because...
  3. delete-trailing-whitespace doesn't seem to be called on saves.
  4. Neither does basic-save-buffer.

I'm not sure how to investigate how js2-mode is intercepting/preventing the before-save-hooks from being triggered.

kris
  • 23,024
  • 10
  • 70
  • 79
  • Not a direct answer, but you might like to try using either [ws-trim](ftp://ftp.lysator.liu.se/pub/emacs/ws-trim.el) or [ws-butler](https://github.com/lewang/ws-butler) instead. These libraries can smartly tidy up only the lines you actually edit, which prevents you from making unnecessary changes to other parts of the file. (If you use version control, I would consider this mandatory, to avoid committing irrelevant changes.) – phils Dec 06 '13 at 02:20
  • Version of Emacs? Version of js2-mode? (from GNU ELPA? you haven't specified). I can't replicate the issue on Emacs 24.3 using the 'latest' js2-mode from http://elpa.gnu.org/packages/js2-mode.html – phils Dec 06 '13 at 03:46
  • I upgraaded to Emacs 24.2 and the latest js2-mode, still happening. I think this isn't a suitable StackOverflow question because of the back-and-forth needed, so I'll take it to a mailing list. – kris Dec 10 '13 at 03:42

2 Answers2

4
  1. Test whether delete-trailing-whitespace works on JS files when you invoke it normally (manually).

  2. Try without whitespace-cleanup on the same hook -- IOW, simplify to see what the problem is.

  3. 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.

  4. 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.

  5. 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.

Drew
  • 29,895
  • 7
  • 74
  • 104
  • Thanks for the suggestions, I tried them and posted an update in the question above. – kris Dec 06 '13 at 02:49
1

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)))
marneborn
  • 699
  • 3
  • 9