diff options
| author | Michal Nazarewicz | 2016-06-21 16:46:52 +0200 |
|---|---|---|
| committer | Michal Nazarewicz | 2016-07-04 23:44:06 +0200 |
| commit | 7c6317a0498b6690ea668909ac012cb45e6f809b (patch) | |
| tree | addc7968bfe0759b83b35dc95acb2d35d835f576 | |
| parent | dc294483af221066724f1007a595016b47fb5814 (diff) | |
| download | emacs-7c6317a0498b6690ea668909ac012cb45e6f809b.tar.gz emacs-7c6317a0498b6690ea668909ac012cb45e6f809b.zip | |
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.
| -rw-r--r-- | lisp/simple.el | 7 | ||||
| -rw-r--r-- | test/lisp/simple-tests.el | 3 |
2 files changed, 6 insertions, 4 deletions
diff --git a/lisp/simple.el b/lisp/simple.el index 3fa23ff9477..37f6d5000e9 100644 --- a/lisp/simple.el +++ b/lisp/simple.el | |||
| @@ -607,9 +607,10 @@ buffer if the variable `delete-trailing-lines' is non-nil." | |||
| 607 | (with-syntax-table (make-syntax-table (syntax-table)) | 607 | (with-syntax-table (make-syntax-table (syntax-table)) |
| 608 | ;; Don't delete formfeeds, even if they are considered whitespace. | 608 | ;; Don't delete formfeeds, even if they are considered whitespace. |
| 609 | (modify-syntax-entry ?\f "_") | 609 | (modify-syntax-entry ?\f "_") |
| 610 | (while (re-search-forward "\\s-$" end-marker t) | 610 | ;; Treating \n as non-whitespace makes things easier. |
| 611 | (skip-syntax-backward "-" (line-beginning-position)) | 611 | (modify-syntax-entry ?\n "_") |
| 612 | (delete-region (point) (match-end 0)))) | 612 | (while (re-search-forward "\\s-+$" end-marker t) |
| 613 | (delete-region (match-beginning 0) (match-end 0)))) | ||
| 613 | ;; Delete trailing empty lines. | 614 | ;; Delete trailing empty lines. |
| 614 | (goto-char end-marker) | 615 | (goto-char end-marker) |
| 615 | (when (and (not end) | 616 | (when (and (not end) |
diff --git a/test/lisp/simple-tests.el b/test/lisp/simple-tests.el index 2722544446d..97b6c491629 100644 --- a/test/lisp/simple-tests.el +++ b/test/lisp/simple-tests.el | |||
| @@ -235,7 +235,8 @@ | |||
| 235 | (insert " \f \n \f \f \n\nlast\n") | 235 | (insert " \f \n \f \f \n\nlast\n") |
| 236 | (delete-trailing-whitespace) | 236 | (delete-trailing-whitespace) |
| 237 | (should (string-equal (buffer-string) " \f\n \f \f\n\nlast\n")) | 237 | (should (string-equal (buffer-string) " \f\n \f \f\n\nlast\n")) |
| 238 | (should (equal ?\s (char-syntax ?\f)))))) | 238 | (should (equal ?\s (char-syntax ?\f))) |
| 239 | (should (equal ?\s (char-syntax ?\n)))))) | ||
| 239 | 240 | ||
| 240 | 241 | ||
| 241 | ;;; auto-boundary tests | 242 | ;;; auto-boundary tests |