emacs-devel
[Top][All Lists]
Advanced

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

Re: [PATCH] delete-trailing-whitespace on active region


From: Stefan Monnier
Subject: Re: [PATCH] delete-trailing-whitespace on active region
Date: Thu, 10 Feb 2011 14:05:20 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

> Attached is a reworked patch. Is it better now?

Yes, please install after fixing the details below.

> -- 
> Deniz Dogan

> === modified file 'lisp/simple.el'
> --- lisp/simple.el    2011-02-01 21:22:21 +0000
> +++ lisp/simple.el    2011-02-10 15:30:53 +0000
> @@ -614,22 +614,26 @@
>      (if (looking-at "^[ \t]*\n\\'")
>       (delete-region (point) (point-max)))))
 
> -(defun delete-trailing-whitespace ()
> +(defun delete-trailing-whitespace (&optional start end)
>    "Delete all the trailing whitespace across the current buffer.
>  All whitespace after the last non-whitespace character in a line is deleted.
>  This respects narrowing, created by \\[narrow-to-region] and friends.
> -A formfeed is not considered whitespace by this function."
> -  (interactive "*")
> +A formfeed is not considered whitespace by this function.
> +If the region is active, only delete whitespace within the region."
> +  (interactive (if (use-region-p)
> +                   (list (region-beginning) (region-end))
> +                 (list (point-min) (point-max))))
> +  (barf-if-buffer-read-only)

Nitpick: the barf-if-buffer-read-only should be in the interactive form.
You can keep passing nil rather than point-min/max when the region is
not used (see below).

>    (save-match-data
>      (save-excursion
> -      (goto-char (point-min))
> -      (while (re-search-forward "\\s-$" nil t)
> -     (skip-syntax-backward "-" (save-excursion (forward-line 0) (point)))
> -     ;; Don't delete formfeeds, even if they are considered whitespace.
> -     (save-match-data
> -       (if (looking-at ".*\f")
> -           (goto-char (match-end 0))))
> -     (delete-region (point) (match-end 0))))))
> +      (goto-char start)

But here you shouldn't assume that start/end won't be nil, since the
function might be called without arguments from Elisp code.


        Stefan



reply via email to

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