]> code.delx.au - gnu-emacs/commitdiff
Simplify ‘delete-trailing-whitespace’ by not treating \n as whitespace
authorMichal Nazarewicz <mina86@mina86.com>
Tue, 21 Jun 2016 14:46:52 +0000 (16:46 +0200)
committerMichal Nazarewicz <mina86@mina86.com>
Mon, 4 Jul 2016 21:44:06 +0000 (23:44 +0200)
* 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
test/lisp/simple-tests.el

index 3fa23ff947712d9bc4e0cd1f4d9d640bae5cdc03..37f6d5000e9cf4ee109b63317bf57ab8d526e327 100644 (file)
@@ -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)
index 2722544446d1cfb755e7f6d2c2219d3fab732ed4..97b6c491629564ba57b9a7c7a35c34887315d256 100644 (file)
       (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