diff options
| author | Henrik Lissner <henrik@lissner.net> | 2015-06-04 17:54:21 -0400 |
|---|---|---|
| committer | Henrik Lissner <henrik@lissner.net> | 2015-06-04 17:54:21 -0400 |
| commit | a3034c284d962c28ae5a47e94037aaa8bc3e9d03 (patch) | |
| tree | 9b0ea3b0f743e1deeb2bdb0095d5e516e403f8df | |
| parent | 5e6bcb38e9b36506a8a0554f47cd00947e84ac0d (diff) | |
Fix surrounding with invalid chars, like ESC (Fix #51)
| -rwxr-xr-x | evil-surround.el | 74 |
1 files changed, 40 insertions, 34 deletions
diff --git a/evil-surround.el b/evil-surround.el index faa7d96..169de60 100755 --- a/evil-surround.el +++ b/evil-surround.el @@ -95,6 +95,10 @@ Each item is of the form (OPERATOR . OPERATION)." (cons (format "<%s%s>" (or tag "") (or rest "")) (format "</%s>" (or tag ""))))) +(defun evil-surround-valid-char-p (char) + "Returns whether CHAR is a valid surround char or not." + (not (memq char '(?\C-\[ ?\C-?)))) + (defun evil-surround-pair (char) "Return the evil-surround pair of char. This is a cons cell (LEFT . RIGHT), both strings." @@ -191,9 +195,10 @@ overlays OUTER and INNER, which are passed to `evil-surround-delete'." (cond ((and outer inner) (evil-surround-delete char outer inner) - (evil-surround-region (overlay-start outer) - (overlay-end outer) - nil (read-char))) + (let ((key (read-char))) + (evil-surround-region (overlay-start outer) + (overlay-end outer) + nil (if (evil-surround-valid-char-p key) key char)))) (t (let* ((outer (evil-surround-outer-overlay char)) (inner (evil-surround-inner-overlay char))) @@ -259,37 +264,38 @@ Becomes this: }" (interactive "<R>c") - (let* ((overlay (make-overlay beg end nil nil t)) - (pair (evil-surround-pair char)) - (open (car pair)) - (close (cdr pair))) - (unwind-protect - (progn - (goto-char (overlay-start overlay)) - - (cond ((eq type 'line) - (insert open) - (indent-according-to-mode) - (newline-and-indent) - (goto-char (overlay-end overlay)) - (insert close) - (indent-according-to-mode) - (newline)) - - (force-new-line - (insert open) - (indent-according-to-mode) - (newline-and-indent) - (goto-char (overlay-end overlay)) - (newline-and-indent) - (insert close)) - - (t - (insert open) - (goto-char (overlay-end overlay)) - (insert close))) - (goto-char (overlay-start overlay))) - (delete-overlay overlay)))) + (when (evil-surround-valid-char-p char) + (let* ((overlay (make-overlay beg end nil nil t)) + (pair (evil-surround-pair char)) + (open (car pair)) + (close (cdr pair))) + (unwind-protect + (progn + (goto-char (overlay-start overlay)) + + (cond ((eq type 'line) + (insert open) + (indent-according-to-mode) + (newline-and-indent) + (goto-char (overlay-end overlay)) + (insert close) + (indent-according-to-mode) + (newline)) + + (force-new-line + (insert open) + (indent-according-to-mode) + (newline-and-indent) + (goto-char (overlay-end overlay)) + (newline-and-indent) + (insert close)) + + (t + (insert open) + (goto-char (overlay-end overlay)) + (insert close))) + (goto-char (overlay-start overlay))) + (delete-overlay overlay))))) (evil-define-operator evil-Surround-region (beg end type char) "Call surround-region, toggling force-new-line" |
