emacs-elpa-diffs
[Top][All Lists]
Advanced

[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)))
 



reply via email to

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