[Top][All Lists]

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

dntw.el --- Deletes trailing whitespace from changed lines.

From: Óscar Fuentes
Subject: dntw.el --- Deletes trailing whitespace from changed lines.
Date: Wed, 20 Feb 2008 06:07:22 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (windows-nt)

Version 0.2.

Fixed support for appended lines.

Fixed support for multiple changes on the diff.

Avoids repeating buffer names on the final message.

;;; dntw.el --- Deletes trailing whitespace from changed lines.

;; Copyright (C) 2008 by Óscar Fuentes

;; Author: Óscar Fuentes, <address@hidden>

;; dntw.el is free software; you can redistribute it
;; and/or modify it under the terms of the GNU General Public License
;; as published by the Free Software Foundation; either version 3, or
;; (at your option) any later version.

;; dntw.el is distributed in the hope that it will
;; be useful, but WITHOUT ANY WARRANTY; without even the implied
;; See the GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:

;; dntw.el helps to avoid introducing changes containing trailing
;; whitespace.

;; Do a diff on the file(s) you want to fix, invoke
;; `delete-new-trailing-whitespace' on the diff buffer and trailing
;; whitespace will disappear from the lines that are marked as
;; modified or added. This is specially useful while reviewing the
;; changes you want to commit to your Version Control System.

;; `delete-new-trailing-whitespace' tells you the names of the buffers
;; that were modified. It does not save the respective files.

;; Usage:
;; Save this file somewhere on your load-path and put the following in
;; your .emacs:
;; (require 'dntw)

;; It is a good idea to show trailing whitespace on the diff, so you
;; know when to invoke this funcion. The code below implements this
;; and assigns the hotkey C-c C-k to `delete-new-trailing-whitespace'.

;; (defun dntf-diff-mode-hook ()
;;   (local-set-key [?\C-c?\C-k] 'delete-new-trailing-whitespace)
;;   (setq show-trailing-whitespace t))

;; (add-hook 'diff-mode-hook 'dntf-diff-mode-hook t)

;; Please contact me if you have ideas or improvements for
;; dntw.el

;;; Version 0.2

;;; Code:

(defun 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."
  ;; TODO: Check that we are in diff-mode or one of its derived modes.
  (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)
      (destructuring-bind (buf line-offset pos src dst &optional switched)
          (diff-find-source-location nil t)
        (when line-offset
          (set-buffer buf)
            (goto-char (+ (car pos) (cdr src)))
            (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
      (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 'dntw)

reply via email to

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