emacs-diffs
[Top][All Lists]
Advanced

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

[Emacs-diffs] masater 1f8b940 2/3: Simplify ‘delete-trailing-whitespace


From: Michal Nazarewicz
Subject: [Emacs-diffs] masater 1f8b940 2/3: Simplify ‘delete-trailing-whitespace’ by not treating \n as whitespace
Date: Mon, 4 Jul 2016 12:11:59 +0000 (UTC)

branch: masater
commit 1f8b9408085adc087f94c34db732573f6c76f62f
Author: Michal Nazarewicz <address@hidden>
Commit: Michal Nazarewicz <address@hidden>

    Simplify ‘delete-trailing-whitespace’ by not treating \n as whitespace
    
    * 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 3fa23ff..37f6d50 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -607,9 +607,10 @@ buffer if the variable `delete-trailing-lines' is non-nil."
         (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 @@
       (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



reply via email to

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