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

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

[nongnu] elpa/evil-surround 440d391c89 130/175: Fix visible narrowing wh


From: ELPA Syncer
Subject: [nongnu] elpa/evil-surround 440d391c89 130/175: Fix visible narrowing when surrounding within a field (#135)
Date: Mon, 9 Oct 2023 13:01:09 -0400 (EDT)

branch: elpa/evil-surround
commit 440d391c89a7f6d5a7a0c9486b0e8ac4fc7f43aa
Author: Lionel Henry <lionel.hry@gmail.com>
Commit: Filipe Silva <ninrod@users.noreply.github.com>

    Fix visible narrowing when surrounding within a field (#135)
    
    * Fix visible narrowing when surrounding within a field
    
    * Test that buffer is widened before reading char
---
 evil-surround.el           | 25 ++++++++++++++++++++-----
 test/evil-surround-test.el | 27 ++++++++++++++++++++++++++-
 2 files changed, 46 insertions(+), 6 deletions(-)

diff --git a/evil-surround.el b/evil-surround.el
index fbcaee7203..d2e59ddfdc 100644
--- a/evil-surround.el
+++ b/evil-surround.el
@@ -102,6 +102,21 @@ Each item is of the form (OPERATOR . OPERATION)."
       (evil-repeat-record res))
     res))
 
+;; The operator state narrows the buffer to the current field. This
+;; function widens temporarily before reading a character so the
+;; narrowing is not visible to the user.
+(defun evil-surround-read-char ()
+  (if (evil-operator-state-p)
+      (save-restriction (widen) (read-char))
+    (read-char)))
+
+(defun evil-surround-input-char ()
+  (list (evil-surround-read-char)))
+
+(defun evil-surround-input-region-char ()
+  (append (evil-operator-range t)
+          (evil-surround-input-char)))
+
 (defun evil-surround-function ()
   "Read a functionname from the minibuffer and wrap selection in function call"
   (let ((fname (evil-surround-read-from-minibuffer "" "")))
@@ -191,7 +206,7 @@ Alternatively, the text to delete can be represented with
 the overlays OUTER and INNER, where OUTER includes the delimiters
 and INNER excludes them. The intersection (i.e., difference)
 between these overlays is what is deleted."
-  (interactive "c")
+  (interactive (evil-surround-input-char))
   (cond
    ((and outer inner)
     (delete-region (overlay-start outer) (overlay-start inner))
@@ -213,11 +228,11 @@ between these overlays is what is deleted."
   "Change the surrounding delimiters represented by CHAR.
 Alternatively, the text to delete can be represented with the
 overlays OUTER and INNER, which are passed to `evil-surround-delete'."
-  (interactive "c")
+  (interactive (evil-surround-input-char))
   (cond
    ((and outer inner)
     (evil-surround-delete char outer inner)
-    (let ((key (read-char)))
+    (let ((key (evil-surround-read-char)))
       (evil-surround-region (overlay-start outer)
                             (overlay-end outer)
                             nil (if (evil-surround-valid-char-p key) key 
char))))
@@ -315,7 +330,7 @@ Becomes this:
      :thing
    }"
 
-  (interactive "<R>c")
+  (interactive (evil-surround-input-region-char))
   (when (evil-surround-valid-char-p char)
     (let* ((overlay (make-overlay beg end nil nil t))
            (pair (or (and (boundp 'pair) pair) (evil-surround-pair char)))
@@ -371,7 +386,7 @@ Becomes this:
 
 (evil-define-operator evil-Surround-region (beg end type char)
   "Call surround-region, toggling force-new-line"
-  (interactive "<R>c")
+  (interactive (evil-surround-input-region-char))
   (evil-surround-region beg end type char t))
 
 ;;;###autoload
diff --git a/test/evil-surround-test.el b/test/evil-surround-test.el
index 120bac5bb0..df8a03f2bb 100644
--- a/test/evil-surround-test.el
+++ b/test/evil-surround-test.el
@@ -14,6 +14,18 @@
  ?\[ '("[ " . " ]")
  ?\{ '("{ " . " }"))
 
+(defmacro test-widened-buffer (start cmds exp)
+  (declare (indent 0))
+  `(let (widened)
+     (evil-test-buffer
+       ,start
+       (turn-on-evil-surround-mode)
+       (cl-letf (((symbol-function #'widen)
+                  (lambda () (setq widened t))))
+         (execute-kbd-macro ,(car cmds)))
+       ,exp)
+     (should widened)))
+
 (ert-deftest evil-surround-test ()
   (ert-info ("basic surrounding")
     (evil-test-buffer
@@ -169,4 +181,17 @@
       (turn-on-evil-surround-mode)
       ("cs`)")
       "[(]this_is_a_backtick_surrounded_word)"
-      )))
+      ))
+  (ert-info ("buffer is widened before reading char")
+    (test-widened-buffer
+      "`[w]ord`"
+      ("cs`)")
+      "[(]word)")
+    (test-widened-buffer
+      "`[w]ord`"
+      ("ds`")
+      "[w]ord")
+    (test-widened-buffer
+      "[w]ord"
+      ("ysiwb")
+      "[(]word)")))



reply via email to

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