unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* New function: diff-delete-new-trailing-whitespace
@ 2008-07-25  3:00 Óscar Fuentes
  2008-07-25  3:46 ` Óscar Fuentes
  0 siblings, 1 reply; 2+ messages in thread
From: Óscar Fuentes @ 2008-07-25  3:00 UTC (permalink / raw
  To: emacs-devel


This was discussed past April. Vinicius Jose Latorre suggested good
stylistic improvements for this function, but those are not contemplated
here.


2008-07-25  Óscar Fuentes  <ofv@wanadoo.es>

	* diff-mode.el (diff-delete-new-trailing-whitespace):
          New function.


Index: diff-mode.el
===================================================================
RCS file: /sources/emacs/emacs/lisp/diff-mode.el,v
retrieving revision 1.147
diff -u -u -r1.147 diff-mode.el
--- diff-mode.el	22 Jul 2008 20:55:30 -0000	1.147
+++ diff-mode.el	25 Jul 2008 02:46:45 -0000
@@ -1881,6 +1881,46 @@
         ;; When there's no more hunks, diff-hunk-next signals an error.
 	(error nil)))))
 
+(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
+	  (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.")))
+
 ;; provide the package
 (provide 'diff-mode)




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: New function: diff-delete-new-trailing-whitespace
  2008-07-25  3:00 New function: diff-delete-new-trailing-whitespace Óscar Fuentes
@ 2008-07-25  3:46 ` Óscar Fuentes
  0 siblings, 0 replies; 2+ messages in thread
From: Óscar Fuentes @ 2008-07-25  3:46 UTC (permalink / raw
  To: emacs-devel

Óscar Fuentes <ofv@wanadoo.es> writes:

Removed some `set-buffer's as suggested by Stefan.

2008-07-25  Óscar Fuentes  <ofv@wanadoo.es>

	* diff-mode.el (diff-delete-new-trailing-whitespace):
          New function.


Index: diff-mode.el
===================================================================
RCS file: /sources/emacs/emacs/lisp/diff-mode.el,v
retrieving revision 1.147
diff -u -u -r1.147 diff-mode.el
--- diff-mode.el	22 Jul 2008 20:55:30 -0000	1.147
+++ diff-mode.el	25 Jul 2008 03:43:00 -0000
@@ -1881,6 +1881,44 @@
         ;; When there's no more hunks, diff-hunk-next signals an error.
 	(error nil)))))
 
+(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.")))
+
 ;; provide the package
 (provide 'diff-mode)





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2008-07-25  3:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-07-25  3:00 New function: diff-delete-new-trailing-whitespace Óscar Fuentes
2008-07-25  3:46 ` Óscar Fuentes

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