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: Fri, 09 May 2008 21:45:44 -0300
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.13) Gecko/20080313 SeaMonkey/1.1.9


Well, can I install diff-kill-trailing-whitespace
and diff-show-trailing-whitespace into diff-mode?
I haven't had time to look at the code, but assuming it's clean enough,
I don't see any problem with it,
Ok, shouldn't Óscar Fuentes <address@hidden> sign papers before
code installation?

I think so.  But I haven't kept track of this thread enough to know
which part of the code is his and which is yours.

Attached there are the following files:

   original.el --- contains the original proposal from Óscar Fuentes

   modified.el --- contains the modified proposal by me

(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)
  (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 "^[+!>].*[ \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
          (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.")))
(defun diff-kill-trailing-blanks ()
  "Inspect the current diff and remove trailing whitespace (spaces, tabs).
That is, it removes trailing whitespaces 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

reply via email to

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