From: Deniz Dogan <deniz.a.m.dogan@gmail.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: emacs-devel@gnu.org
Subject: Re: [PATCH] delete-trailing-whitespace on active region
Date: Fri, 11 Feb 2011 07:33:49 +0100 [thread overview]
Message-ID: <AANLkTikjZ=t0FQgweWeQq47hnn+zT19icWZkv5gibdCe@mail.gmail.com> (raw)
In-Reply-To: <jwvoc6js4me.fsf-monnier+emacs@gnu.org>
[-- Attachment #1: Type: text/plain, Size: 836 bytes --]
2011/2/11 Stefan Monnier <monnier@iro.umontreal.ca>:
>>> + (while (re-search-forward "\\s-$" end 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))))))
>
>> You can't use the value of `end' to limit the search like that, since
>> the deletion changes the size of the region. (Use narrowing or a
>> marker.)
>
> Good point, thanks. ...or do it from the end.
>
This patch is teaching me a lot...
Attached is a last attempt, if it's not good then I give up and hope
that someone else will take care of it.
Thanks,
Deniz Dogan
[-- Attachment #2: dtw3.patch --]
[-- Type: application/octet-stream, Size: 2037 bytes --]
=== modified file 'lisp/simple.el'
--- lisp/simple.el 2011-02-01 21:22:21 +0000
+++ lisp/simple.el 2011-02-11 06:31:41 +0000
@@ -614,22 +614,30 @@
(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 (progn
+ (barf-if-buffer-read-only)
+ (if (use-region-p)
+ (list (region-beginning) (region-end))
+ (list nil nil))))
(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))))))
+ (let ((end-marker (set-marker (make-marker) (or end (point-max))))
+ (start (or start (point-min))))
+ (goto-char start)
+ (while (re-search-forward "\\s-$" end-marker 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)))
+ (set-marker end-marker nil)))))
(defun newline-and-indent ()
"Insert a newline, then indent according to major mode.
next prev parent reply other threads:[~2011-02-11 6:33 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-02-09 18:28 [PATCH] delete-trailing-whitespace on active region Deniz Dogan
2011-02-10 2:35 ` Stefan Monnier
2011-02-10 15:31 ` Deniz Dogan
2011-02-10 19:05 ` Stefan Monnier
2011-02-10 21:31 ` Johan Bockgård
2011-02-11 2:10 ` Stefan Monnier
2011-02-11 6:33 ` Deniz Dogan [this message]
2011-02-11 14:30 ` Stefan Monnier
2011-02-11 18:27 ` Deniz Dogan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='AANLkTikjZ=t0FQgweWeQq47hnn+zT19icWZkv5gibdCe@mail.gmail.com' \
--to=deniz.a.m.dogan@gmail.com \
--cc=emacs-devel@gnu.org \
--cc=monnier@iro.umontreal.ca \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.