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

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

[nongnu] elpa/ws-butler ee0fc31020 52/64: Merge branch 'xificurC-master'


From: Stefan Kangas
Subject: [nongnu] elpa/ws-butler ee0fc31020 52/64: Merge branch 'xificurC-master'
Date: Fri, 31 Dec 2021 11:09:33 -0500 (EST)

branch: elpa/ws-butler
commit ee0fc3102011062ffce1d50d8a41f4a0f4110c86
Merge: 123a517a14 93409c1ca7
Author: Le Wang <l26wang@gmail.com>
Commit: Le Wang <l26wang@gmail.com>

    Merge branch 'xificurC-master'
---
 README.md                | 10 ++++++++++
 tests/ws-butler-tests.el | 17 +++++++++++++++++
 ws-butler.el             | 24 ++++++++++++++++++++++--
 3 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md
index 0982d20400..612e6e0c1c 100644
--- a/README.md
+++ b/README.md
@@ -22,6 +22,16 @@ 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)))))
+
+
 ## Installation
 
 ### Debian 9 or later or Ubuntu 16.10 or later
diff --git a/tests/ws-butler-tests.el b/tests/ws-butler-tests.el
index 43bcde435f..f49c1d614c 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))
@@ -13,6 +14,7 @@
        (when (get-buffer test-buffer-name)
          (kill-buffer test-buffer-name))
        (switch-to-buffer (get-buffer-create test-buffer-name))
+       (ws-butler-mode t)
        ,@body)))
 
 (defmacro ws-butler-test-with-common-setup (&rest body)
@@ -28,3 +30,18 @@
     (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
+    (setq-local ws-butler-trim-predicate (lambda (_beg _end) nil))
+    (insert "a b c. \n")
+    (ws-butler-before-save)
+    (should (string-equal (buffer-string) "a b c. \n"))))
+
+(ert-deftest ws-butler-test-trim-predicate-nil ()
+  "Tests `ws-butler-trim-predicate' is nil."
+  (ws-butler-test-with-common-setup
+    (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 b008c8e15b..fa51b08415 100644
--- a/ws-butler.el
+++ b/ws-butler.el
@@ -56,6 +56,13 @@
 (eval-when-compile
   (require 'cl))
 
+(eval-and-compile
+  (unless (fboundp 'setq-local)
+    (defmacro setq-local (var val)
+      "Set variable VAR to value VAL in current buffer."
+      ;; Can't use backquote here, it's too early in the bootstrap.
+      (list 'set (list 'make-local-variable (list 'quote var)) val))))
+
 (defgroup ws-butler nil
   "Unobtrusively whitespace deletion like a butler."
   :group 'convenience)
@@ -79,6 +86,16 @@ This should be a list of trailing whitespace significant 
major-modes."
   :type '(repeat (symbol :tag "Major mode"))
   :group 'ws-butler)
 
+(defcustom ws-butler-trim-predicate
+  (lambda (_beg _end) t)
+  "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)
 
@@ -133,7 +150,8 @@ Also see `require-final-newline'."
        (goto-char (point-max))
        (skip-chars-backward " \t\n\v")
        (let ((printable-point-max (point)))
-         (when (>= last-modified-pos printable-point-max)
+         (when (and (funcall ws-butler-trim-predicate printable-point-max 
(point-max))
+                  (>= last-modified-pos printable-point-max))
            (ws-butler-trim-eob-lines))))))
   ;; clean return code for hooks
   nil)
@@ -196,6 +214,7 @@ in place."
 
 Setting `ws-butler-keep-whitespace-before-point' will also
 ensure point doesn't jump due to white space trimming."
+
   ;; save data to restore later
   (when ws-butler-keep-whitespace-before-point
     (ws-butler-with-save
@@ -213,7 +232,8 @@ 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)
+       (when (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]