[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/ws-butler 7d57ac7cf6 42/64: Added `ws-butler-trim-predicat
From: |
Stefan Kangas |
Subject: |
[nongnu] elpa/ws-butler 7d57ac7cf6 42/64: Added `ws-butler-trim-predicate' custom variable |
Date: |
Fri, 31 Dec 2021 11:09:31 -0500 (EST) |
branch: elpa/ws-butler
commit 7d57ac7cf63d3ef0094e1be29cc7d6ebe012016e
Author: xificurC <xificurC@gmail.com>
Commit: xificurC <xificurC@gmail.com>
Added `ws-butler-trim-predicate' custom variable
The variable should hold a function with 2 arguments for region
beginning and end and should return true for regions that should be
trimmed. Can be useful to e.g. disable trimming for multiline strings.
When not defined all regions are trimmed.
---
README.md | 9 +++++++++
tests/ws-butler-tests.el | 14 ++++++++++++++
ws-butler.el | 14 +++++++++++++-
3 files changed, 36 insertions(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 7b6057d8c7..4c0789059e 100644
--- a/README.md
+++ b/README.md
@@ -22,6 +22,15 @@ By default, ws-butler preserves "virtual spaces" in front of
point if necessary.
This can be disabled with `ws-butler-keep-whitespace-before-point`.
+#### Trimming only specific lines.
+
+There might be lines you don't want to get trimmed, e.g. spaces in multiline
strings. The behavior can be customized through `ws-butler-trim-predicate`.
This variable should hold a function that expects 2 arguments (region beginning
and end) and should return true only for regions that one wants to get trimmed.
As an example
+
+ (setq ws-butler-trim-predicate
+ (lambda (beg end)
+ (not (eq 'font-lock-string-face
+ (get-text-property end 'face)))))
+
## History
1. I started by trimming all spaces at EOL in source code in a
diff --git a/tests/ws-butler-tests.el b/tests/ws-butler-tests.el
index 43bcde435f..5784a4aeb7 100644
--- a/tests/ws-butler-tests.el
+++ b/tests/ws-butler-tests.el
@@ -5,6 +5,7 @@
;; for "every" function
(require 'cl)
+(load-file "ws-butler.el")
(defmacro ws-butler-test-with-test-buffer (&rest body)
(declare (indent 0) (debug t))
@@ -28,3 +29,16 @@
(execute-kbd-macro (read-kbd-macro "M-DEL"))
(should (every #'identity (list 1 2 3)))
(should (string-equal (buffer-string) "a b "))))
+
+(ert-deftest ws-butler-test-trim-predicate ()
+ "Tests `ws-butler-trim-predicate'."
+ (ws-butler-test-with-common-setup
+ (let ((ws-butler-trim-predicate (lambda (_beg _end) false)))
+ (insert "a b c. \n")
+ (ws-butler-before-save)
+ (should (string-equal (buffer-string) "a b c. \n")))
+ (let (ws-butler-trim-predicate)
+ (erase-buffer)
+ (insert "a b c. \n")
+ (ws-butler-before-save)
+ (should (string-equal (buffer-string) "a b c.\n")))))
diff --git a/ws-butler.el b/ws-butler.el
index 93f52524f3..4ef08be6fd 100644
--- a/ws-butler.el
+++ b/ws-butler.el
@@ -71,6 +71,16 @@ i.e. only the \"virtual\" space is preserved in the buffer."
:type 'boolean
:group 'ws-butler)
+(defcustom ws-butler-trim-predicate
+ nil
+ "Return true for regions that should be trimmed.
+
+Expects 2 arguments - beginning and end of a region.
+Should return a truthy value for regions that should
+have their trailing whitespace trimmed.
+When not defined all regions are trimmed."
+ :type 'function
+ :group 'ws-butler)
(defvar ws-butler-saved)
@@ -200,7 +210,9 @@ ensure point doesn't jump due to white space trimming."
;; always expand to end of line anyway, this should be OK.
end (progn (goto-char (1- end))
(point-at-eol))))
- (ws-butler-clean-region beg end)
+ (unless (and (functionp ws-butler-trim-predicate)
+ (not (funcall ws-butler-trim-predicate beg end)))
+ (ws-butler-clean-region beg end))
(setq last-end end)))
(ws-butler-maybe-trim-eob-lines last-end)))
- [nongnu] elpa/ws-butler ea94b9895a 57/64: Add subsection on basic config in README.md, (continued)
- [nongnu] elpa/ws-butler ea94b9895a 57/64: Add subsection on basic config in README.md, Stefan Kangas, 2021/12/31
- [nongnu] elpa/ws-butler 2d546097bb 62/64: Use cl-lib, Stefan Kangas, 2021/12/31
- [nongnu] elpa/ws-butler 80dabd5d15 56/64: README tweak, Stefan Kangas, 2021/12/31
- [nongnu] elpa/ws-butler 84b3666af3 04/64: fix revert-hook typo, Stefan Kangas, 2021/12/31
- [nongnu] elpa/ws-butler 24cd7721f8 21/64: widen before doing work, Stefan Kangas, 2021/12/31
- [nongnu] elpa/ws-butler 246e5655ab 23/64: Fix never removing blank lines from EOF., Stefan Kangas, 2021/12/31
- [nongnu] elpa/ws-butler de5d1569bf 26/64: Clear buffer-modified-p only when necessary., Stefan Kangas, 2021/12/31
- [nongnu] elpa/ws-butler d265cef1ef 12/64: rename function, Stefan Kangas, 2021/12/31
- [nongnu] elpa/ws-butler d62694365c 34/64: Merge pull request #11 from chris-martin/readme-cleanup, Stefan Kangas, 2021/12/31
- [nongnu] elpa/ws-butler b59e36b245 53/64: bump version to 0.5, Stefan Kangas, 2021/12/31
- [nongnu] elpa/ws-butler 7d57ac7cf6 42/64: Added `ws-butler-trim-predicate' custom variable,
Stefan Kangas <=
- [nongnu] elpa/ws-butler 6f46d5885a 63/64: Revert "Inhibit modification hooks in ws-butler-after-save", Stefan Kangas, 2021/12/31
- [nongnu] elpa/ws-butler b9bcf50496 60/64: Inhibit modification hooks in ws-butler-after-save, Stefan Kangas, 2021/12/31
- [nongnu] elpa/ws-butler af050ffb6c 09/64: fix typo in commentary #1, Stefan Kangas, 2021/12/31
- [nongnu] elpa/ws-butler a5c01e3221 06/64: remove debug message, Stefan Kangas, 2021/12/31
- [nongnu] elpa/ws-butler 45a93f2bc2 18/64: add Travis-CI tests skeleton, Stefan Kangas, 2021/12/31
- [nongnu] elpa/ws-butler 0c941aedeb 31/64: Declare type of keep-whitespace-before-point, Stefan Kangas, 2021/12/31
- [nongnu] elpa/ws-butler 7d30203cf9 19/64: forgot to trim blanks in README (oh the irony!), Stefan Kangas, 2021/12/31
- [nongnu] elpa/ws-butler fe08e8770b 08/64: update lighter, Stefan Kangas, 2021/12/31
- [nongnu] elpa/ws-butler 8ca373047a 24/64: Fix point moving if after blanks., Stefan Kangas, 2021/12/31
- [nongnu] elpa/ws-butler 4154816145 27/64: Clean up end marker., Stefan Kangas, 2021/12/31