[Top][All Lists]

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

Re: show whitespace problems by default in diff-mode

From: Óscar Fuentes
Subject: Re: show whitespace problems by default in diff-mode
Date: Fri, 17 Jul 2009 02:03:38 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (gnu/linux)

Dan Nicolaescu <address@hidden> 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."
  (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)
      (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)))
            (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.


reply via email to

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