unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: "Óscar Fuentes" <ofv@wanadoo.es>
To: emacs-devel@gnu.org
Subject: Re: show whitespace problems by default in diff-mode
Date: Fri, 17 Jul 2009 02:03:38 +0200	[thread overview]
Message-ID: <871vog89b9.fsf@telefonica.net> (raw)
In-Reply-To: 200907162214.n6GMEo5o025706@godzilla.ics.uci.edu

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

> diff-mode is set up to show trailing whitespace problems, but it's not
> doing it by default, to enable this one has to do:
>
> (add-hook 'diff-mode-hook 'whitespace-mode)
>
> Vinicius also posted a patch to edit files to clean the trailing
> whitespace problems based on the diff.
>
> Why not turn on whitespace-mode by default in diff-mode?  It makes
> spotting whitespace problems very easy, and will make it also easy to
> fix.

Time ago I posted a patch for removing new trailing whitespace from
files based on their diffs. Vinicius suggested several improvements, so
maybe this code is the one that you mention. Aparently the maintainers
forgot about it and never was incorporated to Emacs. Just in case, here
is the version I'm using now:

(defun diff-delete-new-trailing-whitespace ()
  "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 "^[+!>]" (point-max) t)
    (save-excursion
      (destructuring-bind (buf line-offset pos src dst &optional switched)
	  (diff-find-source-location t t)
	(when line-offset
	  (with-current-buffer buf
	    (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
    (with-current-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.")))

It has a bug: if the last chunk of the diff does not end with a newline
(because the original file lacks it) `diff-find-source-location' returns
`line-offset' as nil and hence no whitespace is cleaned. No idea why.

-- 
Óscar





  parent reply	other threads:[~2009-07-17  0:03 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-16 22:14 show whitespace problems by default in diff-mode Dan Nicolaescu
2009-07-16 22:33 ` Drew Adams
2009-07-16 22:53   ` Dan Nicolaescu
2009-07-16 23:06     ` Drew Adams
2009-07-16 23:19       ` Dan Nicolaescu
2009-07-16 23:38 ` Miles Bader
2009-07-17  1:35   ` Dan Nicolaescu
2009-07-17  1:45     ` Miles Bader
2009-07-17  2:29       ` Óscar Fuentes
2009-07-17  3:24         ` Miles Bader
2009-07-17  3:36           ` Dan Nicolaescu
2009-07-17  4:15             ` Óscar Fuentes
2009-07-17  4:26             ` Miles Bader
2009-07-17  6:39               ` Dan Nicolaescu
2009-07-17  7:49                 ` Miles Bader
2009-07-17  0:03 ` Óscar Fuentes [this message]
2009-07-17  1:37   ` Dan Nicolaescu
2009-07-17  3:14 ` Stefan Monnier

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=871vog89b9.fsf@telefonica.net \
    --to=ofv@wanadoo.es \
    --cc=emacs-devel@gnu.org \
    /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).