Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <code>java-mode-untabify</code>. The only thing you should need to do is duplicate that <code>add-hook</code> call and change the hook name to <code>espresso-mode-hook</code>, 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 <code>cleanup-whitespace-in-buffer</code>, or whatever you like.</p> <pre><code>(defun cleanup-whitespace-in-buffer () ; &lt;- 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 ; &lt;- you can actually add this to any hook (lambda () (add-hook 'write-contents-hooks 'cleanup-whitespace-in-buffer nil 'local))) </code></pre> <h3>CAVEAT:</h3> <p>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.</p> <p>The <code>cleanup-whitespace-in-buffer</code> function simply deletes any trailing whitespace from every line in the file, then converts non-trailing tabs into spaces. The call to <code>add-hook</code> creates an anonymous function (a lambda) that will be called whenever the hook is run. This anonymous function arranges for <code>cleanup-whitespace-in-buffer</code> to be run in a buffer before it is saved.</p> <p>I really like <a href="https://stackoverflow.com/questions/3313210/converting-this-untabify-on-save-hook-for-emacs-to-work-with-espresso-mode-or-al/3316038#3316038">Alex Ott's answer</a> 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 <code>(untabify (point-min) (point-max))</code> call into <code>(cleanup-whitespace-in-buffer)</code> 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload