emacs-diffs
[Top][All Lists]
Advanced

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

[Emacs-diffs] /srv/bzr/emacs/trunk r109502: * vc/diff-mode.el (diff-remo


From: Dan Nicolaescu
Subject: [Emacs-diffs] /srv/bzr/emacs/trunk r109502: * vc/diff-mode.el (diff-remove-trailing-whitespace): New function.
Date: Wed, 08 Aug 2012 00:41:35 -0400
User-agent: Bazaar (2.5.0)

------------------------------------------------------------
revno: 109502
author: Oscar Fuentes  <address@hidden>
committer: Dan Nicolaescu <address@hidden>
branch nick: trunk
timestamp: Wed 2012-08-08 00:41:35 -0400
message:
  * vc/diff-mode.el (diff-remove-trailing-whitespace): New function.
modified:
  lisp/ChangeLog
  lisp/vc/diff-mode.el
=== modified file 'lisp/ChangeLog'
--- a/lisp/ChangeLog    2012-08-08 02:30:08 +0000
+++ b/lisp/ChangeLog    2012-08-08 04:41:35 +0000
@@ -1,3 +1,7 @@
+2012-08-08  Óscar Fuentes  <address@hidden>
+
+       * vc/diff-mode.el (diff-remove-trailing-whitespace): New function.
+
 2012-08-08  Fabián Ezequiel Gallina  <address@hidden>
 
        * progmodes/python.el Fixed defsubst warning.

=== modified file 'lisp/vc/diff-mode.el'
--- a/lisp/vc/diff-mode.el      2012-07-10 11:51:54 +0000
+++ b/lisp/vc/diff-mode.el      2012-08-08 04:41:35 +0000
@@ -2016,6 +2016,47 @@
       ;; When there's no more hunks, diff-hunk-next signals an error.
       (error nil))))
 
+(defun diff-remove-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))
+  (let
+      ;; We assume that the diff header has no trailing whitespace.
+      ((modified-buffers nil)
+       (white-positions nil))
+    (while (re-search-forward "^[+!>].*[ \t]+$" (point-max) t)
+      (save-excursion
+       (cl-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 trailing whitespace fixes needed."))))
+
 ;; provide the package
 (provide 'diff-mode)
 


reply via email to

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