The snippet you have here will work for any text file. There isn't anything Java specific about it besides the fact that it is named java-mode-untabify
. The only thing you should need to do is duplicate that add-hook
call and change the hook name to espresso-mode-hook
, or whatever the correct hook is for espresso-mode. Although I might suggest you change the name of the function to something that reflects the fact that it is not Java specific. Something like cleanup-whitespace-in-buffer
, or whatever you like.
(defun cleanup-whitespace-in-buffer () ; <- more appropriate name
(save-excursion
(goto-char (point-min))
(while (re-search-forward "[ \t]+$" nil t)
(delete-region (match-beginning 0) (match-end 0)))
(goto-char (point-min))
(if (search-forward "\t" nil t)
(untabify (1- (point)) (point-max))))
nil)
(add-hook 'espresso-mode-hook ; <- you can actually add this to any hook
'(lambda ()
(make-local-variable 'write-contents-hooks)
(add-hook 'write-contents-hooks 'cleanup-whitespace-in-buffer)))
CAVEAT:
I just modified the snippet you posted. I'm not on a machine with Emacs right now, so I haven't tested any of this.
The cleanup-whitespace-in-buffer
function simply deletes any trailing whitespace from every line in the file, then converts non-trailing tabs into spaces. The call to add-hook
creates an anonymous function (a lambda) that will be called whenever the hook is run. This anonymous function arranges for cleanup-whitespace-in-buffer
to be run in a buffer before it is saved.
I really like Alex Ott's answer a lot better since it is much more flexible. It sets up a function that is run whenever ANY buffer is saved. It checks to see if the buffer being saved has a mode that is in the list of specified modes and, if so, untabifies the buffer before saving. It doesn't remove trailing white space, but you could easily change the (untabify (point-min) (point-max))
call into (cleanup-whitespace-in-buffer)
if you want that behavior. This allows you to easily add other modes that will have their files preprocessed before saving. It even works if, for some reason, your mode doesn't have a hook associated with it. His method is better than what I suggested. I simply posted this answer in the interests of clarifying how your snippet works since you said you feel a little week in Emacs Lisp.