diff options
| author | Lionel Henry <lionel.hry@gmail.com> | 2018-10-20 14:48:22 +0200 |
|---|---|---|
| committer | Filipe Silva <ninrod@users.noreply.github.com> | 2018-10-20 09:48:22 -0300 |
| commit | 440d391c89a7f6d5a7a0c9486b0e8ac4fc7f43aa (patch) | |
| tree | df20d9dee73e280cb376f64ffc528e5f8399250b | |
| parent | a92151def9db892b4eaef687d1a7fbe5b97cc760 (diff) | |
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
| -rw-r--r-- | evil-surround.el | 25 | ||||
| -rw-r--r-- | test/evil-surround-test.el | 27 |
2 files changed, 46 insertions, 6 deletions
diff --git a/evil-surround.el b/evil-surround.el index fbcaee7..d2e59dd 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 120bac5..df8a03f 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)"))) |
