emacs-devel
[Top][All Lists]
Advanced

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

Re: Proposal: diff-remove-trailing-blanks


From: Vinicius Jose Latorre
Subject: Re: Proposal: diff-remove-trailing-blanks
Date: Sat, 26 Apr 2008 23:59:37 -0300
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.13) Gecko/20080313 SeaMonkey/1.1.9


Hummm, I'm confused, the original mail said:

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


So, I thought it was to remove trailing whitespaces from a diff buffer.

The "removing" part is accomplished by my function. We are discussing
the "displaying" part, which shows to the user that there is trailing
whitespace to be removed.

But I'm afraid I confused things out relating `show-trailing-whitespace'
to whitespace-mode'.

Well, both parts, the "removing" part and the "displaying" part, related with trailing whitespaces can be done by whitespace-mode.

Does the following function do the job?

(defun diff-remove-trailing-blanks ()
 "When on a buffer that contains a diff, inspects the
differences and removes trailing whitespace (spaces, tabs).
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.
 (let (modified-buffers)
   (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)
         (unless (member buf modified-buffers)
           (when line-offset
             (set-buffer buf)
             (when (re-search-forward "\\([ \t]+\\)$" (point-max) t)
               (push buf modified-buffers)
               (let ((whitespace-style '(trailing)))
(whitespace-cleanup)))))))) ; cleanup trailing blanks in buf
   (if modified-buffers
       (let ((bufs (mapconcat #(lambda (buf)
                                 (format "`%s'" (buffer-name buf)))
                              modified-buffers
                              " "))
             (whitespace-style '(trailing)))
(whitespace-mode) ; display trailing blanks in diff buffer
         (message "Deleted trailing whitespace from: %s" bufs))
     (message "No fixes needed."))))


A better implementation of function above:

(defun diff-remove-trailing-blanks ()
 "When on a buffer that contains a diff, inspects the
differences and removes trailing whitespace (spaces, tabs).
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 ((whitespace-style '(trailing))
         modified-buffers)
     (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 (and line-offset
                      (not (member buf modified-buffers)))
             (set-buffer buf)
             (when (re-search-forward "\\([ \t]+\\)$" (point-max) t)
               (push buf modified-buffers)
               (whitespace-cleanup)))))) ; cleanup trailing blanks in buf
     (if modified-buffers
         (progn
           (whitespace-mode) ; display trailing blanks in diff buffer
           (message "Deleted trailing whitespace from: %s"
                    (mapconcat
                     #'(lambda (buf)
                         (format "`%s'" (buffer-name buf)))
                     modified-buffers
                     " ")))
       (message "No fixes needed.")))))





reply via email to

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