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: Deniz Dogan
Subject: Re: Clear trailing whitespace on save, but not at the cursor
Date: Mon, 05 Mar 2012 17:04:57 +0100
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2

On 2012-03-05 12:02, Thien-Thi Nguyen wrote:
() Aaron Meurer<asmeurer@gmail.com>
() Sun, 4 Mar 2012 17:48:21 -0700

    Is there a way to clear whitespace on save, but
    then "put it back" where the cursor is?

Here is one way:

(defadvice delete-trailing-whitespace
   (around except-for-current-line activate)
   "However, leave trailing whitespace before point alone."
   (let ((save (when (looking-back "\\s-+" (line-beginning-position) t)
                 (match-string 0))))
     ad-do-it
     (when save (insert save))))


You could also use a function like the following:

(defun damd-delete-trailing-whitespace-except-current-line ()
  (interactive)
  (let ((back (1- (point-at-bol)))
        (front (1+ (point-at-eol))))
    (delete-trailing-whitespace (point-min) back)
    (delete-trailing-whitespace front (point-max))))

As you can see, I use the optional boundary arguments of `delete-trailing-whitespace' to achieve the affect. It needs some tweaking, as in its current state it doesn't really check anything about the position of point in the current line.

Deniz



reply via email to

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