emacs-devel
[Top][All Lists]
Advanced

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

Re: Proposal: diff-remove-trailing-blanks


From: Óscar Fuentes
Subject: Re: Proposal: diff-remove-trailing-blanks
Date: Sun, 27 Apr 2008 06:04:08 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (windows-nt)

Vinicius Jose Latorre <address@hidden> writes:

> Well, both parts, the "removing" part and the "displaying" part,
> related with trailing whitespaces can be done by whitespace-mode.
>
> Does the following function do the job?
>
> (defun diff-remove-trailing-blanks ()
>  "When on a buffer that contains a diff, inspects the
> differences and removes trailing whitespace (spaces, tabs).
> Shows a message with the name of the altered buffers, which are
> unsaved.  If a file referenced on the diff has no buffer and
> needs to be fixed, a buffer visiting that file is created."
>  (interactive)
>  (goto-char (point-min))
>  ;; We assume that the diff header has no trailing whitespace.
>  (let (modified-buffers)
>    (while (re-search-forward "^[+!>].*[ \t]+$" (point-max) t)
>      (save-excursion
>    (destructuring-bind (buf line-offset pos src dst &optional switched)
>            (diff-find-source-location t t)
>          (unless (member buf modified-buffers)
>            (when line-offset
>              (set-buffer buf)
>              (when (re-search-forward "\\([ \t]+\\)$" (point-max) t)
>                (push buf modified-buffers)
>                (let ((whitespace-style '(trailing)))
>                  (whitespace-cleanup)))))))) ; cleanup trailing blanks in buf
>    (if modified-buffers
>        (let ((bufs (mapconcat #(lambda (buf)
>                                  (format "`%s'" (buffer-name buf)))
>                               modified-buffers
>                               " "))
>              (whitespace-style '(trailing)))
>          (whitespace-mode)           ; display trailing blanks in diff
> buffer
>          (message "Deleted trailing whitespace from: %s" bufs))
>      (message "No fixes needed."))))
>

Your function removes trailing whitespace from the buffer, while my
function removes it only from the lines that were added or modified.

The motivation of my proposed function is this: when working with source
code that may contain trailing whitespace, maybe you don't want to
create "noise" with changes that just fixes whitespace, but you wish to
avoid making things worse introducing new trailing whitespace. My
function does this: before committing your changes, do a diff of the
modified files, apply the function on the diff buffer, and all trailing
whitespace you introduced is gone, leaving the rest of the code intact.

>>> What are the relevant parts of the buffer?
>>
>> Those that correspond to added or modified lines in the diff.
>
> So, should lines beginning with +, - and ! have a face to display the
> change?

The topic Dan Nicolaescu introduced was how to make evident that the
changes introduce new trailing whitespace. For this, we both use
`show-trailing-whitespace', but this has the inconvenience that shows
trailing whitespace not only for the changed lines, but for the rest of
the diff too, which is a bit annoying.

As it is safe to apply `diff-remove-trailing-blanks' to a diff that does
not introduce new trailing whitespace, what I do is this: before
I commit my changes, I do a diff comprising all the involved source
files (which is easy to do with PCL-CVS or psvn, maybe with vc-dired
too), apply `diff-remove-trailing-blanks' to the diff buffer, do a C-x s
if it fixed whitespace on some buffer, and commit.

Hope this clarifies the confusion I caused with my reference to
whitespace-mode.

-- 
Oscar





reply via email to

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