unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Michal Nazarewicz <mina86@mina86.com>
To: 23843@debbugs.gnu.org
Subject: bug#23843: [PATCH 2/3] Simplify ‘delete-trailing-whitespace’ by not treating \n as whitespace
Date: Sat, 25 Jun 2016 00:14:44 +0200	[thread overview]
Message-ID: <1466806485-15732-2-git-send-email-mina86@mina86.com> (raw)
In-Reply-To: <1466806485-15732-1-git-send-email-mina86@mina86.com>

* lisp/simple.el (delete-trailing-whitespace): Set newline’s character
syntax to non-whitespace so that ‘\s-’ regular expression does not match
it.

This simplifies the loop slightly since a simple ‘\s-+$’ can be used and
as a consequence ‘line-beginning-position’ function does not need to be
called any longer.

Furthermore, when newline has whitespace syntax, ‘\s-$’ regular
expression ends up matching empty lins since ‘\s-’ matches newline
characetr of proceeding line.  This leads to needless loop iterations.

Since previous change to ‘delete-trailing-whitespace’ already introduced
‘with-syntax-table’, take advantage of it and also overwrite newline’s
character syntax.
---
 lisp/simple.el            | 7 ++++---
 test/lisp/simple-tests.el | 3 ++-
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/lisp/simple.el b/lisp/simple.el
index ffedbfa..fbeef9d 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -607,9 +607,10 @@ delete-trailing-whitespace
         (with-syntax-table (make-syntax-table (syntax-table))
           ;; Don't delete formfeeds, even if they are considered whitespace.
           (modify-syntax-entry ?\f "_")
-          (while (re-search-forward "\\s-$" end-marker t)
-            (skip-syntax-backward "-" (line-beginning-position))
-            (delete-region (point) (match-end 0))))
+          ;; Treating \n as non-whitespace makes things easier.
+          (modify-syntax-entry ?\n "_")
+          (while (re-search-forward "\\s-+$" end-marker t)
+            (delete-region (match-beginning 0) (match-end 0))))
         ;; Delete trailing empty lines.
         (goto-char end-marker)
         (when (and (not end)
diff --git a/test/lisp/simple-tests.el b/test/lisp/simple-tests.el
index 2722544..97b6c49 100644
--- a/test/lisp/simple-tests.el
+++ b/test/lisp/simple-tests.el
@@ -235,7 +235,8 @@ simple-test--transpositions
       (insert " \f \n \f \f \n\nlast\n")
       (delete-trailing-whitespace)
       (should (string-equal (buffer-string) " \f\n \f \f\n\nlast\n"))
-      (should (equal ?\s (char-syntax ?\f))))))
+      (should (equal ?\s (char-syntax ?\f)))
+      (should (equal ?\s (char-syntax ?\n))))))
 
 
 ;;; auto-boundary tests
-- 
2.8.0.rc3.226.g39d4020






  reply	other threads:[~2016-06-24 22:14 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-24 22:09 bug#23843: [PATCH 0/3] Small `delete-trailing-whitespace' fixes and improvements Michal Nazarewicz
2016-06-24 22:14 ` bug#23843: [PATCH 1/3] Make ‘delete-trailing-whitespace’ delete spaces after form feed Michal Nazarewicz
2016-06-24 22:14   ` Michal Nazarewicz [this message]
2016-06-24 22:14   ` bug#23843: [PATCH 3/3] Don’t create unnecessary marker in ‘delete-trailing-whitespace’ Michal Nazarewicz
2016-07-04 12:12 ` bug#23843: Small `delete-trailing-whitespace' fixes and improvements Michal Nazarewicz
2016-07-04 16:29   ` Glenn Morris
2016-07-04 22:06     ` Michal Nazarewicz

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

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1466806485-15732-2-git-send-email-mina86@mina86.com \
    --to=mina86@mina86.com \
    --cc=23843@debbugs.gnu.org \
    /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 public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).