unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Proposal: diff-remove-trailing-blanks
@ 2008-04-26 14:59 Óscar Fuentes
  2008-04-26 18:44 ` Dan Nicolaescu
  0 siblings, 1 reply; 18+ messages in thread
From: Óscar Fuentes @ 2008-04-26 14:59 UTC (permalink / raw)
  To: emacs-devel

Some time ago Steffan suggested that this function could be a worth
addition to diff-mode. If this is ok, I need to sign the copyright
papers.

(defun diff-remove-trailing-blanks ()
  "When on a buffer that contains a diff, inspects the
differences and removes trailing whitespace (spaces, tabs) from
the lines modified or introduced by this diff. 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.
  (setq modified-buffers nil)
  (setq white-positions nil)
  (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)
	(when line-offset
	  (set-buffer buf)
	  (save-excursion
	    (goto-char (+ (car pos) (cdr src)))
	    (beginning-of-line)
	    (when (re-search-forward "\\([ \t]+\\)$" (line-end-position) t)
	      (when (not (member buf modified-buffers))
		(push buf modified-buffers))
	      (goto-char (match-end 0))
	      (push (point-marker) white-positions)
	      (goto-char (match-beginning 0))
	      (push (point-marker) white-positions)
	      (push buf white-positions)))))))
  (while white-positions
    (save-excursion
      (set-buffer (pop white-positions))
      (delete-region (pop white-positions) (pop white-positions))))
  (if modified-buffers
      (let ((msg "Deleted new trailing whitespace from:"))
	(dolist (f modified-buffers)
	  (setq msg (concat msg " `" (buffer-name f) "'")))
	(message "%s" msg))
    (message "No fixes needed.")))





^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Proposal: diff-remove-trailing-blanks
  2008-04-26 14:59 Proposal: diff-remove-trailing-blanks Óscar Fuentes
@ 2008-04-26 18:44 ` Dan Nicolaescu
  2008-04-26 19:38   ` Óscar Fuentes
  2008-04-26 19:50   ` Vinicius Jose Latorre
  0 siblings, 2 replies; 18+ messages in thread
From: Dan Nicolaescu @ 2008-04-26 18:44 UTC (permalink / raw)
  To: Óscar Fuentes; +Cc: emacs-devel

Óscar Fuentes <ofv@wanadoo.es> writes:

  > Some time ago Steffan suggested that this function could be a worth
  > addition to diff-mode. If this is ok, I need to sign the copyright
  > papers.

This seems like a good idea.  It would be even nicer if something like
this could be combined with whitespace-mode to clearly show trailing
whitespace.  That way the user knows he has a problem.

(I currently do (setq show-trailing-whitespace t) in a hook for
diff-mode-hook, but it is not good enough, it highlights things that are
not relevant in a diff).




^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Proposal: diff-remove-trailing-blanks
  2008-04-26 18:44 ` Dan Nicolaescu
@ 2008-04-26 19:38   ` Óscar Fuentes
  2008-04-26 19:50   ` Vinicius Jose Latorre
  1 sibling, 0 replies; 18+ messages in thread
From: Óscar Fuentes @ 2008-04-26 19:38 UTC (permalink / raw)
  To: emacs-devel

Dan Nicolaescu <dann@ics.uci.edu> writes:

> This seems like a good idea.  It would be even nicer if something like
> this could be combined with whitespace-mode to clearly show trailing
> whitespace.  That way the user knows he has a problem.
>
> (I currently do (setq show-trailing-whitespace t) in a hook for
> diff-mode-hook, but it is not good enough, it highlights things that are
> not relevant in a diff).

That's what I do too. Maybe it is possible to teach whitespace-mode to
ignore lines not beginning with [-\+!<>].

-- 
Oscar





^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Proposal: diff-remove-trailing-blanks
  2008-04-26 18:44 ` Dan Nicolaescu
  2008-04-26 19:38   ` Óscar Fuentes
@ 2008-04-26 19:50   ` Vinicius Jose Latorre
  2008-04-26 20:08     ` Óscar Fuentes
  1 sibling, 1 reply; 18+ messages in thread
From: Vinicius Jose Latorre @ 2008-04-26 19:50 UTC (permalink / raw)
  To: Dan Nicolaescu; +Cc: Óscar Fuentes, emacs-devel

Dan Nicolaescu wrote:
> Óscar Fuentes <ofv@wanadoo.es> writes:
>
>   > Some time ago Steffan suggested that this function could be a worth
>   > addition to diff-mode. If this is ok, I need to sign the copyright
>   > papers.
>
> This seems like a good idea.  It would be even nicer if something like
> this could be combined with whitespace-mode to clearly show trailing
> whitespace.  That way the user knows he has a problem.
>   

Well, whitespace-mode shows trailing blanks.
See whitespace-style variable.

It also reports that a buffer has a trailing blanks, besides other bogus 
whitespaces.
See whitespace-report function.

It also cleans trailing blanks.
See whitespace-cleanup function.

See also whitespace-action variable for some automatic actions that can 
be taken when a buffer is visited, killed or written.

> (I currently do (setq show-trailing-whitespace t) in a hook for
> diff-mode-hook, but it is not good enough, it highlights things that are
> not relevant in a diff)

What relevant things would you like to see (or to be done) in a diff buffer?






^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Proposal: diff-remove-trailing-blanks
  2008-04-26 19:50   ` Vinicius Jose Latorre
@ 2008-04-26 20:08     ` Óscar Fuentes
       [not found]       ` <4813A244.4050908@ig.com.br>
  0 siblings, 1 reply; 18+ messages in thread
From: Óscar Fuentes @ 2008-04-26 20:08 UTC (permalink / raw)
  To: emacs-devel

Vinicius Jose Latorre <viniciusjl@ig.com.br> writes:

[snip]

> Dan Nicolaescu wrote:
>> (I currently do (setq show-trailing-whitespace t) in a hook for
>> diff-mode-hook, but it is not good enough, it highlights things that are
>> not relevant in a diff)
>
> What relevant things would you like to see (or to be done) in a diff buffer?

Here, we are interested on parts of the buffer, we don't care much about
trailing whitespace out of the diff part that shows changed lines. That
is, we are not interested on trailing whitespace on context lines, nor
on those that are artifacts of diff-mode.

-- 
Oscar





^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Proposal: diff-remove-trailing-blanks
       [not found]       ` <4813A244.4050908@ig.com.br>
@ 2008-04-26 22:07         ` Óscar Fuentes
  2008-04-27  2:30           ` Vinicius Jose Latorre
  0 siblings, 1 reply; 18+ messages in thread
From: Óscar Fuentes @ 2008-04-26 22:07 UTC (permalink / raw)
  To: Vinicius Jose Latorre; +Cc: emacs-devel

Vinicius Jose Latorre <viniciusjl@ig.com.br> writes:

>>> What relevant things would you like to see (or to be done) in a diff buffer?
>>>     
>>
>> Here, we are interested on parts of the buffer, we don't care much about
>> trailing whitespace out of the diff part that shows changed lines. That
>> is, we are not interested on trailing whitespace on context lines, nor
>> on those that are artifacts of diff-mode.
>>   
>
> Hummm, I'm confused, the original mail said:
>
>  "When on a buffer that contains a diff, inspects the
> differences and removes trailing whitespace (spaces, tabs) from
> the lines modified or introduced by this diff. 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."
>
>
> So, I thought it was to remove trailing whitespaces from a diff buffer.

The "removing" part is accomplished by my function. We are discussing
the "displaying" part, which shows to the user that there is trailing
whitespace to be removed.

But I'm afraid I confused things out relating `show-trailing-whitespace'
to whitespace-mode'.

> What are the relevant parts of the buffer?

Those that correspond to added or modified lines in the diff.

-- 
Oscar




^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Proposal: diff-remove-trailing-blanks
  2008-04-26 22:07         ` Óscar Fuentes
@ 2008-04-27  2:30           ` Vinicius Jose Latorre
  2008-04-27  2:59             ` Vinicius Jose Latorre
  2008-04-27  4:04             ` Óscar Fuentes
  0 siblings, 2 replies; 18+ messages in thread
From: Vinicius Jose Latorre @ 2008-04-27  2:30 UTC (permalink / raw)
  To: Óscar Fuentes; +Cc: emacs-devel


>>>> What relevant things would you like to see (or to be done) in a diff buffer?
>>>>     
>>> Here, we are interested on parts of the buffer, we don't care much about
>>> trailing whitespace out of the diff part that shows changed lines. That
>>> is, we are not interested on trailing whitespace on context lines, nor
>>> on those that are artifacts of diff-mode.
>>>   
>> Hummm, I'm confused, the original mail said:
>>
>>  "When on a buffer that contains a diff, inspects the
>> differences and removes trailing whitespace (spaces, tabs) from
>> the lines modified or introduced by this diff. 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."
>>
>>
>> So, I thought it was to remove trailing whitespaces from a diff buffer.
>
> The "removing" part is accomplished by my function. We are discussing
> the "displaying" part, which shows to the user that there is trailing
> whitespace to be removed.
>
> But I'm afraid I confused things out relating `show-trailing-whitespace'
> to whitespace-mode'.

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."))))


>> 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?






^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Proposal: diff-remove-trailing-blanks
  2008-04-27  2:30           ` Vinicius Jose Latorre
@ 2008-04-27  2:59             ` Vinicius Jose Latorre
  2008-04-27  4:04             ` Óscar Fuentes
  1 sibling, 0 replies; 18+ messages in thread
From: Vinicius Jose Latorre @ 2008-04-27  2:59 UTC (permalink / raw)
  To: Óscar Fuentes; +Cc: emacs-devel


>>> Hummm, I'm confused, the original mail said:
>>>
>>>  "When on a buffer that contains a diff, inspects the
>>> differences and removes trailing whitespace (spaces, tabs) from
>>> the lines modified or introduced by this diff. 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."
>>>
>>>
>>> So, I thought it was to remove trailing whitespaces from a diff buffer.
>>
>> The "removing" part is accomplished by my function. We are discussing
>> the "displaying" part, which shows to the user that there is trailing
>> whitespace to be removed.
>>
>> But I'm afraid I confused things out relating `show-trailing-whitespace'
>> to whitespace-mode'.
>
> 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."))))


A better implementation of function above:

(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)
  (save-excursion
    ;; We assume that the diff header has no trailing whitespace.
    (let ((whitespace-style '(trailing))
          modified-buffers)
      (goto-char (point-min))
      (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)
            (when (and line-offset
                       (not (member buf modified-buffers)))
              (set-buffer buf)
              (when (re-search-forward "\\([ \t]+\\)$" (point-max) t)
                (push buf modified-buffers)
                (whitespace-cleanup)))))) ; cleanup trailing blanks in buf
      (if modified-buffers
          (progn
            (whitespace-mode) ; display trailing blanks in diff buffer
            (message "Deleted trailing whitespace from: %s"
                     (mapconcat
                      #'(lambda (buf)
                          (format "`%s'" (buffer-name buf)))
                      modified-buffers
                      " ")))
        (message "No fixes needed.")))))





^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Proposal: diff-remove-trailing-blanks
  2008-04-27  2:30           ` Vinicius Jose Latorre
  2008-04-27  2:59             ` Vinicius Jose Latorre
@ 2008-04-27  4:04             ` Óscar Fuentes
       [not found]               ` <48146DF3.3090706@ig.com.br>
  1 sibling, 1 reply; 18+ messages in thread
From: Óscar Fuentes @ 2008-04-27  4:04 UTC (permalink / raw)
  To: emacs-devel

Vinicius Jose Latorre <viniciusjl@ig.com.br> 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





^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Proposal: diff-remove-trailing-blanks
       [not found]                 ` <48147BC0.6080406@ig.com.br>
@ 2008-04-27 15:15                   ` Óscar Fuentes
  2008-04-27 17:30                     ` Vinicius Jose Latorre
  0 siblings, 1 reply; 18+ messages in thread
From: Óscar Fuentes @ 2008-04-27 15:15 UTC (permalink / raw)
  To: Vinicius Jose Latorre; +Cc: emacs-devel

Vinicius Jose Latorre <viniciusjl@ig.com.br> writes:

>      (while (re-search-forward "^[+!>] .*?[ \t]+$" (point-max) t)

In unified diffs, there is no white space after `+'. In other diff
types, its seems that there is:

+foo (changed or added line, unified diff)
> foo (changed or added line, normal diff)
! foo (changed line, context diff)
+ foo (added line, context diff)

The solution is to test the format type and adapt the regexp for it:

(diff-beginning-of-hunk 'try-harder)
(setq (diff-hunk-style))
(case style
    (unified (setq re ...)) ;; (+)
    (context (setq re ...)) ;; (+! )
    (t (setq re ...))) ;; (> )

[snip]

>      (if modified-buffers
>          (let ((whitespace-style '(trailing))
>                (whitespace-trailing-regexp "^[+!>] .*?\\([\t ]+\\)$"))

Likewise.

>            (whitespace-mode 1)        ; display trailing blanks in diff buffer

Seeing that the diff introduces trailing whitespace is most interesting
before removing it too. Actually, you want to highlight trailing
withespace on the diff for knowing that you should invoke
`diff-remove-trailing-blanks'. (A nice thing would be to automatically
test the diff and put something on the buffer's modeline indicating that
the diff introduces trailing whitespace. `diff-mode-hook' is handy for
this).

[snip]

-- 
Oscar




^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Proposal: diff-remove-trailing-blanks
  2008-04-27 15:15                   ` Óscar Fuentes
@ 2008-04-27 17:30                     ` Vinicius Jose Latorre
  2008-04-27 20:05                       ` Óscar Fuentes
  0 siblings, 1 reply; 18+ messages in thread
From: Vinicius Jose Latorre @ 2008-04-27 17:30 UTC (permalink / raw)
  To: Óscar Fuentes; +Cc: emacs-devel


>>      (while (re-search-forward "^[+!>] .*?[ \t]+$" (point-max) t)
>
> In unified diffs, there is no white space after `+'. In other diff
> types, its seems that there is:
>
> +foo (changed or added line, unified diff)
>> foo (changed or added line, normal diff)
> ! foo (changed line, context diff)
> + foo (added line, context diff)
>
> The solution is to test the format type and adapt the regexp for it:
>
> (diff-beginning-of-hunk 'try-harder)
> (setq (diff-hunk-style))
> (case style
>     (unified (setq re ...)) ;; (+)
>     (context (setq re ...)) ;; (+! )
>     (t (setq re ...))) ;; (> )
>
> [snip]
>
>>      (if modified-buffers
>>          (let ((whitespace-style '(trailing))
>>                (whitespace-trailing-regexp "^[+!>] .*?\\([\t ]+\\)$"))
>
> Likewise.
>
>>            (whitespace-mode 1)        ; display trailing blanks in diff buffer
>
> Seeing that the diff introduces trailing whitespace is most interesting
> before removing it too. Actually, you want to highlight trailing
> withespace on the diff for knowing that you should invoke
> `diff-remove-trailing-blanks'. (A nice thing would be to automatically
> test the diff and put something on the buffer's modeline indicating that
> the diff introduces trailing whitespace. `diff-mode-hook' is handy for
> this).
>
> [snip]

Ok, so, maybe the functions below fix all of this.

(defun diff-remove-trailing-blanks ()
  "When on a buffer that contains a diff, inspects the
differences and removes trailing whitespace (spaces, tabs) from
the lines modified or introduced by this diff. 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)
  (save-excursion
    ;; We assume that the diff header has no trailing whitespace.
    (let (modified-buffers white-positions)
      (goto-char (point-min))
      (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)
            (when line-offset
              (save-excursion
                (set-buffer buf)
                (goto-char (+ (car pos) (cdr src)))
                (beginning-of-line)
                (when (re-search-forward "\\([ \t]+\\)$" 
(line-end-position) t)
                  (unless (member buf modified-buffers)
                    (push buf modified-buffers))
                  (push buf white-positions)
                  (push (match-beginning 0) white-positions)
                  (push (match-end 0) white-positions)))))))
      (setq white-positions (nreverse white-positions))
      (while white-positions
        (save-excursion
          (set-buffer (pop white-positions))
          (delete-region (pop white-positions) (pop white-positions))))
      (if modified-buffers
          (message "Deleted trailing whitespace from: %s"
                   (mapconcat
                    #'(lambda (buf)
                        (format "`%s'" (buffer-name buf)))
                    modified-buffers
                    " "))
        (message "No fixes needed.")))))

(defun diff-show-trailing-blanks ()
  "Show trailing blanks in modified lines for diff-mode."
  (interactive)
  (let ((whitespace-style '(trailing))
        (whitespace-trailing-regexp "^[+!>].*?\\([\t ]+\\)$"))
    (whitespace-mode 1)))     ; display trailing blanks in diff buffer

(add-hook 'diff-mode-hook 'diff-show-trailing-blanks)





^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Proposal: diff-remove-trailing-blanks
  2008-04-27 17:30                     ` Vinicius Jose Latorre
@ 2008-04-27 20:05                       ` Óscar Fuentes
  2008-05-01  0:34                         ` Vinicius Jose Latorre
  0 siblings, 1 reply; 18+ messages in thread
From: Óscar Fuentes @ 2008-04-27 20:05 UTC (permalink / raw)
  To: emacs-devel

Vinicius Jose Latorre <viniciusjl@ig.com.br> writes:

>>>      (while (re-search-forward "^[+!>] .*?[ \t]+$" (point-max) t)
>>
>> In unified diffs, there is no white space after `+'. In other diff
>> types, its seems that there is:
>>
>> +foo (changed or added line, unified diff)
>>> foo (changed or added line, normal diff)
>> ! foo (changed line, context diff)
>> + foo (added line, context diff)
>>
>> The solution is to test the format type and adapt the regexp for it:
>>
>> (diff-beginning-of-hunk 'try-harder)
>> (setq (diff-hunk-style))
>> (case style
>>     (unified (setq re ...)) ;; (+)
>>     (context (setq re ...)) ;; (+! )
>>     (t (setq re ...))) ;; (> )
>>
>
> Ok, so, maybe the functions below fix all of this.
[snip]
>      (while (re-search-forward "^[+!>].*?[ \t]+$" (point-max) t)

As every change indicator is followed by a white space in non-unified
format, this regexp matches every change on normal and context
formats. This is not bad, as the trailing whitespace condition is tested
again below for the source file, which is what matters. But then, you
could use a simpler regexp above ("^[+!>]", for instance), for behaving
the same with diff formats.

[snip]

> (defun diff-show-trailing-blanks ()
>  "Show trailing blanks in modified lines for diff-mode."
>  (interactive)
>  (let ((whitespace-style '(trailing))
>        (whitespace-trailing-regexp "^[+!>].*?\\([\t ]+\\)$"))
>    (whitespace-mode 1)))     ; display trailing blanks in diff buffer

My CVS Emacs is a few weeks old, son I can't test this, but it looks
good to me :-)

> (add-hook 'diff-mode-hook 'diff-show-trailing-blanks)

Of course this is left for the .emacs file of each own.

However, I suggest adding a key sequence to diff-mode that executes
diff-remove-trailing-blanks. I use C-c C-k. And perhaps a more adequate
name is diff-kill-trailing-whitespace.

Do you intend to install it on CVS soon?

-- 
Oscar





^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Proposal: diff-remove-trailing-blanks
  2008-04-27 20:05                       ` Óscar Fuentes
@ 2008-05-01  0:34                         ` Vinicius Jose Latorre
  2008-05-01  1:04                           ` Óscar Fuentes
  2008-05-02 14:22                           ` Stefan Monnier
  0 siblings, 2 replies; 18+ messages in thread
From: Vinicius Jose Latorre @ 2008-05-01  0:34 UTC (permalink / raw)
  To: Óscar Fuentes; +Cc: ema >> "GNU Emacs (devel)"


>>>>      (while (re-search-forward "^[+!>] .*?[ \t]+$" (point-max) t)
>>>>         
>>> In unified diffs, there is no white space after `+'. In other diff
>>> types, its seems that there is:
>>>
>>> +foo (changed or added line, unified diff)
>>>       
>>>> foo (changed or added line, normal diff)
>>>>         
>>> ! foo (changed line, context diff)
>>> + foo (added line, context diff)
>>>
>>> The solution is to test the format type and adapt the regexp for it:
>>>
>>> (diff-beginning-of-hunk 'try-harder)
>>> (setq (diff-hunk-style))
>>> (case style
>>>     (unified (setq re ...)) ;; (+)
>>>     (context (setq re ...)) ;; (+! )
>>>     (t (setq re ...))) ;; (> )
>>>
>>>       
>> Ok, so, maybe the functions below fix all of this.
>>     
> [snip]
>   
>>      (while (re-search-forward "^[+!>].*?[ \t]+$" (point-max) t)
>>     
>
> As every change indicator is followed by a white space in non-unified
> format, this regexp matches every change on normal and context
> formats. This is not bad, as the trailing whitespace condition is tested
> again below for the source file, which is what matters. But then, you
> could use a simpler regexp above ("^[+!>]", for instance), for behaving
> the same with diff formats.
>   

Ok, but the search in diff buffer is only for modified lines with 
trailing blanks,
so there is less finds/replacements in the source file.


>> (defun diff-show-trailing-blanks ()
>>  "Show trailing blanks in modified lines for diff-mode."
>>  (interactive)
>>  (let ((whitespace-style '(trailing))
>>        (whitespace-trailing-regexp "^[+!>].*?\\([\t ]+\\)$"))
>>    (whitespace-mode 1)))     ; display trailing blanks in diff buffer
>>     
>
> My CVS Emacs is a few weeks old, son I can't test this, but it looks
> good to me :-)
>   

Ok.


>> (add-hook 'diff-mode-hook 'diff-show-trailing-blanks)
>>     
>
> Of course this is left for the .emacs file of each own.
>   

Of course, this line is there only as an usage example.


> However, I suggest adding a key sequence to diff-mode that executes
> diff-remove-trailing-blanks. I use C-c C-k. And perhaps a more adequate
> name is diff-kill-trailing-whitespace.
>
> Do you intend to install it on CVS soon?
>   


Well, can I install diff-kill-trailing-whitespace
and diff-show-trailing-whitespace into diff-mode?





^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Proposal: diff-remove-trailing-blanks
  2008-05-01  0:34                         ` Vinicius Jose Latorre
@ 2008-05-01  1:04                           ` Óscar Fuentes
  2008-05-02 14:22                           ` Stefan Monnier
  1 sibling, 0 replies; 18+ messages in thread
From: Óscar Fuentes @ 2008-05-01  1:04 UTC (permalink / raw)
  To: emacs-devel

Vinicius Jose Latorre <viniciusjl@ig.com.br> writes:

>>>      (while (re-search-forward "^[+!>].*?[ \t]+$" (point-max) t)
>>>     
>>
>> As every change indicator is followed by a white space in non-unified
>> format, this regexp matches every change on normal and context
>> formats. This is not bad, as the trailing whitespace condition is tested
>> again below for the source file, which is what matters. But then, you
>> could use a simpler regexp above ("^[+!>]", for instance), for behaving
>> the same with diff formats.
>>   
>
> Ok, but the search in diff buffer is only for modified lines with
> trailing blanks,
> so there is less finds/replacements in the source file.

Precisely, my point is that non-unified format has a trailing blank for
empty new lines, even when the new line has no trailing blank. You can
check that yourself adding an empty new line to a file, diff it in
`context' or `normal' format and you can check that the diff has a blank
space after the `>' or `+'.

[snip]

>> Do you intend to install it on CVS soon?
>>   
>
> Well, can I install diff-kill-trailing-whitespace
> and diff-show-trailing-whitespace into diff-mode?

The reason I would prefer diff-mode.el for those features, is that you
can also install a key sequence for it (I suggested C-c C-k). If you do
not receive authorization from an Emacs maintainer for adding the
functions to diff-mode, just rename them to whitespace-kill-from-diff or
something and install them in whitespace.el.

-- 
Oscar





^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Proposal: diff-remove-trailing-blanks
  2008-05-01  0:34                         ` Vinicius Jose Latorre
  2008-05-01  1:04                           ` Óscar Fuentes
@ 2008-05-02 14:22                           ` Stefan Monnier
  2008-05-07  0:25                             ` Vinicius Jose Latorre
  1 sibling, 1 reply; 18+ messages in thread
From: Stefan Monnier @ 2008-05-02 14:22 UTC (permalink / raw)
  To: Vinicius Jose Latorre
  Cc: Óscar Fuentes, ema >> "GNU Emacs (devel)"

> Well, can I install diff-kill-trailing-whitespace
> and diff-show-trailing-whitespace into diff-mode?

I haven't had time to look at the code, but assuming it's clean enough,
I don't see any problem with it,


        Stefan




^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Proposal: diff-remove-trailing-blanks
  2008-05-02 14:22                           ` Stefan Monnier
@ 2008-05-07  0:25                             ` Vinicius Jose Latorre
  2008-05-07  2:04                               ` Stefan Monnier
  0 siblings, 1 reply; 18+ messages in thread
From: Vinicius Jose Latorre @ 2008-05-07  0:25 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Óscar Fuentes, GNU Emacs (devel)


>> Well, can I install diff-kill-trailing-whitespace
>> and diff-show-trailing-whitespace into diff-mode?
>>     
>
> I haven't had time to look at the code, but assuming it's clean enough,
> I don't see any problem with it,
>   

Ok, shouldn't Óscar Fuentes <ofv@wanadoo.es> sign papers before code 
installation?





^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Proposal: diff-remove-trailing-blanks
  2008-05-07  0:25                             ` Vinicius Jose Latorre
@ 2008-05-07  2:04                               ` Stefan Monnier
  2008-05-10  0:45                                 ` Vinicius Jose Latorre
  0 siblings, 1 reply; 18+ messages in thread
From: Stefan Monnier @ 2008-05-07  2:04 UTC (permalink / raw)
  To: Vinicius Jose Latorre; +Cc: Óscar Fuentes, GNU Emacs (devel)

>>> Well, can I install diff-kill-trailing-whitespace
>>> and diff-show-trailing-whitespace into diff-mode?
>> I haven't had time to look at the code, but assuming it's clean enough,
>> I don't see any problem with it,
> Ok, shouldn't Óscar Fuentes <ofv@wanadoo.es> sign papers before
> code installation?

I think so.  But I haven't kept track of this thread enough to know
which part of the code is his and which is yours.


        Stefan




^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Proposal: diff-remove-trailing-blanks
  2008-05-07  2:04                               ` Stefan Monnier
@ 2008-05-10  0:45                                 ` Vinicius Jose Latorre
  0 siblings, 0 replies; 18+ messages in thread
From: Vinicius Jose Latorre @ 2008-05-10  0:45 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Óscar Fuentes, GNU Emacs (devel)

[-- Attachment #1: Type: text/plain, Size: 646 bytes --]


>>>> Well, can I install diff-kill-trailing-whitespace
>>>> and diff-show-trailing-whitespace into diff-mode?
>>>>         
>>> I haven't had time to look at the code, but assuming it's clean enough,
>>> I don't see any problem with it,
>>>       
>> Ok, shouldn't Óscar Fuentes <ofv@wanadoo.es> sign papers before
>> code installation?
>>     
>
> I think so.  But I haven't kept track of this thread enough to know
> which part of the code is his and which is yours.
>   

Attached there are the following files:

    original.el --- contains the original proposal from Óscar Fuentes

    modified.el --- contains the modified proposal by me


[-- Attachment #2: original.el --]
[-- Type: text/plain, Size: 1595 bytes --]

(defun diff-remove-trailing-blanks ()
  "When on a buffer that contains a diff, inspects the
differences and removes trailing whitespace (spaces, tabs) from
the lines modified or introduced by this diff. 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.
  (setq modified-buffers nil)
  (setq white-positions nil)
  (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)
	(when line-offset
	  (set-buffer buf)
	  (save-excursion
	    (goto-char (+ (car pos) (cdr src)))
	    (beginning-of-line)
	    (when (re-search-forward "\\([ \t]+\\)$" (line-end-position) t)
	      (when (not (member buf modified-buffers))
		(push buf modified-buffers))
	      (goto-char (match-end 0))
	      (push (point-marker) white-positions)
	      (goto-char (match-beginning 0))
	      (push (point-marker) white-positions)
	      (push buf white-positions)))))))
  (while white-positions
    (save-excursion
      (set-buffer (pop white-positions))
      (delete-region (pop white-positions) (pop white-positions))))
  (if modified-buffers
      (let ((msg "Deleted new trailing whitespace from:"))
	(dolist (f modified-buffers)
	  (setq msg (concat msg " `" (buffer-name f) "'")))
	(message "%s" msg))
    (message "No fixes needed.")))

[-- Attachment #3: modified.el --]
[-- Type: text/plain, Size: 1857 bytes --]

(defun diff-kill-trailing-blanks ()
  "Inspect the current diff and remove trailing whitespace (spaces, tabs).
That is, it removes trailing whitespaces from  the lines modified or introduced
by this diff.

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)
  (save-excursion
    ;; We assume that the diff header has no trailing whitespace.
    (let (modified-buffers white-positions)
      (goto-char (point-min))
      (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)
	    (when line-offset
	      (save-excursion
		(set-buffer buf)
		(goto-char (+ (car pos) (cdr src)))
		(beginning-of-line)
		(when (re-search-forward "\\([ \t]+\\)$" (line-end-position) t)
		  (unless (member buf modified-buffers)
		    (push buf modified-buffers))
		  (push buf white-positions)
		  (push (match-beginning 0) white-positions)
		  (push (match-end 0) white-positions)))))))
      (setq white-positions (nreverse white-positions))
      (while white-positions
	(save-excursion
	  (set-buffer (pop white-positions))
	  (delete-region (pop white-positions) (pop white-positions))))
      (if modified-buffers
	  (message "Deleted trailing whitespace from: %s"
		   (mapconcat
		    #'(lambda (buf)
			(format "`%s'" (buffer-name buf)))
		    modified-buffers
		    " "))
	(message "No fixes needed.")))))

(defun diff-show-trailing-blanks ()
  "Show trailing blanks in modified lines for diff-mode."
  (interactive)
  (let ((whitespace-style '(trailing))
	(whitespace-trailing-regexp "^[+!>].*?\\([\t ]+\\)$"))
    (whitespace-mode 1)))     ; display trailing blanks in diff buffer

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2008-05-10  0:45 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-04-26 14:59 Proposal: diff-remove-trailing-blanks Óscar Fuentes
2008-04-26 18:44 ` Dan Nicolaescu
2008-04-26 19:38   ` Óscar Fuentes
2008-04-26 19:50   ` Vinicius Jose Latorre
2008-04-26 20:08     ` Óscar Fuentes
     [not found]       ` <4813A244.4050908@ig.com.br>
2008-04-26 22:07         ` Óscar Fuentes
2008-04-27  2:30           ` Vinicius Jose Latorre
2008-04-27  2:59             ` Vinicius Jose Latorre
2008-04-27  4:04             ` Óscar Fuentes
     [not found]               ` <48146DF3.3090706@ig.com.br>
     [not found]                 ` <48147BC0.6080406@ig.com.br>
2008-04-27 15:15                   ` Óscar Fuentes
2008-04-27 17:30                     ` Vinicius Jose Latorre
2008-04-27 20:05                       ` Óscar Fuentes
2008-05-01  0:34                         ` Vinicius Jose Latorre
2008-05-01  1:04                           ` Óscar Fuentes
2008-05-02 14:22                           ` Stefan Monnier
2008-05-07  0:25                             ` Vinicius Jose Latorre
2008-05-07  2:04                               ` Stefan Monnier
2008-05-10  0:45                                 ` Vinicius Jose Latorre

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).