emacs-devel
[Top][All Lists]
Advanced

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

Proposal: diff-remove-trailing-blanks


From: Óscar Fuentes
Subject: Proposal: diff-remove-trailing-blanks
Date: Sat, 26 Apr 2008 16:59:34 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (windows-nt)

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





reply via email to

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