[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: |
Tue, 12 Feb 2008 00:01:21 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (windows-nt) |
;;; 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
;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
;; 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
;;; 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 left unsaved. If
a file referenced on the diff has no buffer and needs to be
fixed, a buffer visiting that file is created."
(interactive)
;; 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)
(while (re-search-forward "^[+!>].*[ \t]+$" (point-max) t)
(save-excursion
(destructuring-bind (buf line-offset pos src dst &optional switched)
(diff-find-source-location)
(set-buffer buf)
(save-excursion
(goto-char (+ (car pos) (cdr src)))
(beginning-of-line)
(when (re-search-forward "\\([ \t]+\\)$" (line-end-position) t)
(delete-region (match-beginning 0) (match-end 0))
(message "%s" (buffer-name buf))
(push buf modified-buffers))))))
(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)
- dntw.el --- Deletes trailing whitespace from changed lines.,
Óscar Fuentes <=