help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Clear trailing whitespace on save, but not at the cursor


From: Le Wang
Subject: Re: Clear trailing whitespace on save, but not at the cursor
Date: Thu, 22 Mar 2012 23:45:16 +0800

On Thu, Mar 22, 2012 at 8:13 AM, Aaron Meurer <asmeurer@gmail.com> wrote:
Sorry, I'm still *very* new to emacs lisp (lisp in general, actually).
 Does this mean that it's possible to modify the above defadvice
function you gave above so that it actually clears it before the save,
but then puts it back?  The function works just fine in not clearing
at the cursor, but as noted, this is not quite what I want, because I
do *not* want to save trailing whitespace to file at all (I would
rather have my current annoyance).

The defadvice solution is not ideal.  You're changing the fundamental behaviour of a function that could be called by other functions.  It's better to make your own command, this should fit all your requirements:

(defun my-save-buffer-dtws (arg)
  "save buffer delete trailing white space, preserve white space before point if point is past text"
  (interactive "p")
  (let ((save (when (and (looking-at "\\s-*$")
                         (looking-back "\\s-+" (line-beginning-position) t))
                (match-string 0))))
    (delete-trailing-whitespace)
    (save-buffer arg)
    (when save
      (insert save)
      (set-buffer-modified-p nil))))

(global-set-key [remap save-buffer] 'my-save-buffer-dtws)


 
Aaron Meurer


--
Le

reply via email to

[Prev in Thread] Current Thread [Next in Thread]