I have the following before save hook defined which deletes all trailing whitespace and converts tabs to spaces (except in makefile mode since makefiles need tabs to work). This works great, however, it creates a little bit of an annoying side effect where if my cursor has some space before it, removing the space, moves the cursor back. I would like to tweak this a little by removing all trailing space, except if the cursor is at the end of a line.
(defun my-before-save-hook () (progn (if (not (string-match ".*makefile.*" (message "%s" major-mode))) ; Makefiles require tabs (untabify (point-min) (point-max))) ; Convert tabs to spaces (delete-trailing-whitespace))) ; Remote trailing spaces (add-hook 'before-save-hook 'my-before-save-hook)