From: Reuben Thomas <rrt@sc3d.org>
To: Noam Postavsky <npostavs@users.sourceforge.net>
Cc: Mark Karpov <markkarpov@openmailbox.org>, 25157@debbugs.gnu.org
Subject: bug#25157: 26.0.50; whitespace-cleanup does not remove single trailing empty line anymore
Date: Wed, 21 Dec 2016 00:51:04 +0000 [thread overview]
Message-ID: <CAOnWdog3MLi7XAtkNZr0mTEHfNBLrMd0KzhEEwEZTdfvfg-TSw@mail.gmail.com> (raw)
In-Reply-To: <CAOnWdoj=icjFZPDSYN494E1U59iKq_AD4O2J5pe5is9mZ7=UuA@mail.gmail.com>
[-- Attachment #1.1: Type: text/plain, Size: 265 bytes --]
I attach a patch for consideration, along the lines I previously proposed.
It seems to work: at least, it deletes a single trailing extra newline,
while still leaving a newline at the end of the buffer, and doesn't delete
a single newline at the end of the buffer.
[-- Attachment #1.2: Type: text/html, Size: 381 bytes --]
[-- Attachment #2: 0007-Fix-whitespace-cleanup-to-remove-single-trailing-new.patch --]
[-- Type: text/x-patch, Size: 2323 bytes --]
From fb396e7a732f377747b013f7e589133acb12258f Mon Sep 17 00:00:00 2001
From: Reuben Thomas <rrt@sc3d.org>
Date: Wed, 21 Dec 2016 00:40:11 +0000
Subject: [PATCH 7/7] Fix whitespace-cleanup to remove single trailing newline
(Bug#25157)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* lisp/whitespace.el (whitespace-empty-at-eob-regexp): Simplify and
correct the regexp, which I broke when trying to fix Bug#24745.
(whitespace-cleanup): Remove a FIXME: the reason for save-match-data
is the call of re-search-forward. Detect if we delete at least one
newline at the end of the buffer, and if so, add one back. This is
necessary because there’s no way to construct the the eob-regexp so
that it always leaves a single newline at the end of the buffer if at
least one newline was present after the last non-whitespace character.
---
lisp/whitespace.el | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/lisp/whitespace.el b/lisp/whitespace.el
index 29d60c9..009511f 100644
--- a/lisp/whitespace.el
+++ b/lisp/whitespace.el
@@ -729,7 +729,7 @@ whitespace-empty-at-bob-regexp
:group 'whitespace)
-(defcustom whitespace-empty-at-eob-regexp "^\\([ \t\n]*\\(\n\\{2,\\}\\|[ \t]+\\)\\)\\'"
+(defcustom whitespace-empty-at-eob-regexp "\\([ \t]+\\|\\([ \t\n]*\n[ \t\n]+\\)\\)\\'"
"Specify regexp for empty lines at end of buffer.
Used when `whitespace-style' includes `empty'."
@@ -1398,7 +1398,7 @@ whitespace-cleanup
;; whole buffer
(t
(save-excursion
- (save-match-data ;FIXME: Why?
+ (save-match-data
;; PROBLEM 1: empty lines at bob
;; PROBLEM 2: empty lines at eob
;; ACTION: remove all empty lines at bob and/or eob
@@ -1408,7 +1408,10 @@ whitespace-cleanup
(when (looking-at whitespace-empty-at-bob-regexp)
(delete-region (match-beginning 1) (match-end 1)))
(when (re-search-forward
- whitespace-empty-at-eob-regexp nil t)
+ whitespace-empty-at-eob-regexp nil t)
+ ;; If we deleted a trailing newline, reinsert it
+ (if (string-match-p "\n" (match-string 0))
+ (insert ?\n))
(delete-region (match-beginning 1) (match-end 1)))))))
;; PROBLEM 3: `tab-width' or more SPACEs at bol
;; PROBLEM 4: SPACEs before TAB
--
2.7.4
next prev parent reply other threads:[~2016-12-21 0:51 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-12-10 14:41 bug#25157: 26.0.50; whitespace-cleanup does not remove single trailing empty line anymore Mark Karpov
2016-12-20 4:36 ` npostavs
2016-12-20 18:37 ` Reuben Thomas
2016-12-21 0:51 ` Reuben Thomas [this message]
2016-12-21 4:07 ` npostavs
2016-12-21 11:07 ` Reuben Thomas
2016-12-22 2:18 ` npostavs
2016-12-22 12:53 ` Reuben Thomas
2016-12-23 0:03 ` npostavs
2016-12-23 10:44 ` Reuben Thomas
2016-12-24 15:34 ` npostavs
2016-12-26 22:37 ` Reuben Thomas
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=CAOnWdog3MLi7XAtkNZr0mTEHfNBLrMd0KzhEEwEZTdfvfg-TSw@mail.gmail.com \
--to=rrt@sc3d.org \
--cc=25157@debbugs.gnu.org \
--cc=markkarpov@openmailbox.org \
--cc=npostavs@users.sourceforge.net \
/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.