From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: =?iso-8859-1?Q?=D3scar_Fuentes?= Newsgroups: gmane.emacs.devel Subject: Re: New function: diff-delete-new-trailing-whitespace Date: Fri, 25 Jul 2008 05:46:16 +0200 Message-ID: <8wvqzkvr.fsf@telefonica.net> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1216957615 10908 80.91.229.12 (25 Jul 2008 03:46:55 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 25 Jul 2008 03:46:55 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Fri Jul 25 05:47:45 2008 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1KMEHN-0007Dd-V7 for ged-emacs-devel@m.gmane.org; Fri, 25 Jul 2008 05:47:42 +0200 Original-Received: from localhost ([127.0.0.1]:47508 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KMEGU-0003rK-Bm for ged-emacs-devel@m.gmane.org; Thu, 24 Jul 2008 23:46:46 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KMEGP-0003qc-OP for emacs-devel@gnu.org; Thu, 24 Jul 2008 23:46:41 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KMEGN-0003pX-Ax for emacs-devel@gnu.org; Thu, 24 Jul 2008 23:46:40 -0400 Original-Received: from [199.232.76.173] (port=59859 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KMEGN-0003pU-47 for emacs-devel@gnu.org; Thu, 24 Jul 2008 23:46:39 -0400 Original-Received: from main.gmane.org ([80.91.229.2]:50724 helo=ciao.gmane.org) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1KMEGM-0002Xp-Kx for emacs-devel@gnu.org; Thu, 24 Jul 2008 23:46:38 -0400 Original-Received: from list by ciao.gmane.org with local (Exim 4.43) id 1KMEGG-0003BA-KM for emacs-devel@gnu.org; Fri, 25 Jul 2008 03:46:32 +0000 Original-Received: from 230.red-83-61-145.dynamicip.rima-tde.net ([83.61.145.230]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 25 Jul 2008 03:46:32 +0000 Original-Received: from ofv by 230.red-83-61-145.dynamicip.rima-tde.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 25 Jul 2008 03:46:32 +0000 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 61 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: 230.red-83-61-145.dynamicip.rima-tde.net User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (windows-nt) Cancel-Lock: sha1:gTTb+eVeZRw+ovnTAWQ8rEJu6ic= X-detected-kernel: by monty-python.gnu.org: Linux 2.6, seldom 2.4 (older, 4) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:101447 Archived-At: Óscar Fuentes writes: Removed some `set-buffer's as suggested by Stefan. 2008-07-25 Óscar Fuentes * 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)