From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Vinicius Jose Latorre Newsgroups: gmane.emacs.devel Subject: Re: Proposal: diff-remove-trailing-blanks Date: Sun, 27 Apr 2008 14:30:04 -0300 Message-ID: <4814B81C.8030407@ig.com.br> References: <200804261845.m3QIixvX026773@sallyv1.ics.uci.edu> <4813879E.8030700@ig.com.br> <4813A244.4050908@ig.com.br> <8wz09so0.fsf@telefonica.net> <4813E542.10802@ig.com.br> <3ap89c5j.fsf@telefonica.net> <48146DF3.3090706@ig.com.br> <48147BC0.6080406@ig.com.br> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1209317461 20443 80.91.229.12 (27 Apr 2008 17:31:01 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 27 Apr 2008 17:31:01 +0000 (UTC) Cc: emacs-devel@gnu.org To: =?ISO-8859-1?Q?=D3scar_Fuentes?= Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sun Apr 27 19:31:37 2008 connect(): Connection refused 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 1JqAit-0004dS-Gh for ged-emacs-devel@m.gmane.org; Sun, 27 Apr 2008 19:31:35 +0200 Original-Received: from localhost ([127.0.0.1]:58239 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1JqAiC-0005AL-T6 for ged-emacs-devel@m.gmane.org; Sun, 27 Apr 2008 13:30:52 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1JqAi8-00059a-7Q for emacs-devel@gnu.org; Sun, 27 Apr 2008 13:30:48 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1JqAi3-00055G-7s for emacs-devel@gnu.org; Sun, 27 Apr 2008 13:30:47 -0400 Original-Received: from [199.232.76.173] (port=40283 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1JqAi2-00055D-Vp for emacs-devel@gnu.org; Sun, 27 Apr 2008 13:30:43 -0400 Original-Received: from qb-out-0506.google.com ([72.14.204.233]) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1JqAi1-0001Kg-Nm for emacs-devel@gnu.org; Sun, 27 Apr 2008 13:30:41 -0400 Original-Received: by qb-out-0506.google.com with SMTP id e6so1853765qbe.2 for ; Sun, 27 Apr 2008 10:30:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding:sender; bh=ISTikg27yq5DFiIWki85YlqkOtAf74dGGYhv/5woy1k=; b=Q54Zt2gwLQ4VudN0flW3lIBJgRzO9nnH3Qwsxl8ObLrL8LfdIWbasyGz84jjGDXk/GSXKDqq6H1n7613s1kxuiMvHupDtasC4zXHP2vXj+pTMagdOvOaOfbOD6OpDZv3Qgz4Gn4oOxrSdUhqrzN0/CEGp3r9pNunwZaazTnFfGY= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding:sender; b=mGnrpDPdAvbJKnCtSZpgSEvAxwcvYUeiweocoQcrZBvGT6a9Aew4ifG6p2t5rxUuEREj8SnaxvtCeRA5GfoePxFDUEdDaI0zFoX3OzqULnOuRfZe/2Z8TzodyaPJjtZ3Tm0RpXpEuO2LWqmf62zKt0/8e0d1JwkG8gco4EYZnQY= Original-Received: by 10.150.218.11 with SMTP id q11mr2835428ybg.82.1209317440272; Sun, 27 Apr 2008 10:30:40 -0700 (PDT) Original-Received: from ?192.168.0.100? ( [201.82.227.22]) by mx.google.com with ESMTPS id 9sm9691417yws.6.2008.04.27.10.30.36 (version=TLSv1/SSLv3 cipher=RC4-MD5); Sun, 27 Apr 2008 10:30:38 -0700 (PDT) User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.13) Gecko/20080313 SeaMonkey/1.1.9 In-Reply-To: X-detected-kernel: by monty-python.gnu.org: Linux 2.6 (newer, 2) 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:96009 Archived-At: >> (while (re-search-forward "^[+!>] .*?[ \t]+$" (point-max) t) > > In unified diffs, there is no white space after `+'. In other diff > types, its seems that there is: > > +foo (changed or added line, unified diff) >> foo (changed or added line, normal diff) > ! foo (changed line, context diff) > + foo (added line, context diff) > > The solution is to test the format type and adapt the regexp for it: > > (diff-beginning-of-hunk 'try-harder) > (setq (diff-hunk-style)) > (case style > (unified (setq re ...)) ;; (+) > (context (setq re ...)) ;; (+! ) > (t (setq re ...))) ;; (> ) > > [snip] > >> (if modified-buffers >> (let ((whitespace-style '(trailing)) >> (whitespace-trailing-regexp "^[+!>] .*?\\([\t ]+\\)$")) > > Likewise. > >> (whitespace-mode 1) ; display trailing blanks in diff buffer > > Seeing that the diff introduces trailing whitespace is most interesting > before removing it too. Actually, you want to highlight trailing > withespace on the diff for knowing that you should invoke > `diff-remove-trailing-blanks'. (A nice thing would be to automatically > test the diff and put something on the buffer's modeline indicating that > the diff introduces trailing whitespace. `diff-mode-hook' is handy for > this). > > [snip] Ok, so, maybe the functions below fix all of this. (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) (save-excursion ;; We assume that the diff header has no trailing whitespace. (let (modified-buffers white-positions) (goto-char (point-min)) (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 (save-excursion (set-buffer buf) (goto-char (+ (car pos) (cdr src))) (beginning-of-line) (when (re-search-forward "\\([ \t]+\\)$" (line-end-position) t) (unless (member buf modified-buffers) (push buf modified-buffers)) (push buf white-positions) (push (match-beginning 0) white-positions) (push (match-end 0) white-positions))))))) (setq white-positions (nreverse white-positions)) (while white-positions (save-excursion (set-buffer (pop white-positions)) (delete-region (pop white-positions) (pop white-positions)))) (if modified-buffers (message "Deleted trailing whitespace from: %s" (mapconcat #'(lambda (buf) (format "`%s'" (buffer-name buf))) modified-buffers " ")) (message "No fixes needed."))))) (defun diff-show-trailing-blanks () "Show trailing blanks in modified lines for diff-mode." (interactive) (let ((whitespace-style '(trailing)) (whitespace-trailing-regexp "^[+!>].*?\\([\t ]+\\)$")) (whitespace-mode 1))) ; display trailing blanks in diff buffer (add-hook 'diff-mode-hook 'diff-show-trailing-blanks)