unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Vinicius Jose Latorre <viniciusjl@ig.com.br>
To: "Óscar Fuentes" <ofv@wanadoo.es>
Cc: emacs-devel@gnu.org
Subject: Re: Proposal: diff-remove-trailing-blanks
Date: Sun, 27 Apr 2008 14:30:04 -0300	[thread overview]
Message-ID: <4814B81C.8030407@ig.com.br> (raw)
In-Reply-To: <mynf8h2d.fsf@telefonica.net>


>>      (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)





  reply	other threads:[~2008-04-27 17:30 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4814B81C.8030407@ig.com.br \
    --to=viniciusjl@ig.com.br \
    --cc=emacs-devel@gnu.org \
    --cc=ofv@wanadoo.es \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).