From e455df61ca058b5bf5597c2d4b44086e8aec77e0 Mon Sep 17 00:00:00 2001 From: Ellis Kenyo Date: Fri, 18 Aug 2023 20:10:04 +0100 Subject: [PATCH 1/3] fix: handle escaped parentheses Currently, if an escaped paren is encountered in Clojure code; it's interpreted as a delimiter and breaks deletion --- evil-cleverparens-util.el | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/evil-cleverparens-util.el b/evil-cleverparens-util.el index c6c71c4..29db3ea 100644 --- a/evil-cleverparens-util.el +++ b/evil-cleverparens-util.el @@ -47,6 +47,15 @@ (when pos (goto-char pos)) (looking-at-p evil-cp--ws-regexp))) +(defun evil-cp--looking-at-escaped-p (&optional pos) + (save-excursion + (when pos (goto-char pos)) + (or + (looking-at-p "\\\\") + (progn + (goto-char (- (point) 1)) + (looking-at-p "\\\\"))))) + (defun evil-cp--pair-for (pair pairs) (cond ((not pairs) @@ -72,6 +81,7 @@ question. Ignores parentheses inside strings." (save-excursion (when pos (goto-char pos)) (and (sp--looking-at-p (evil-cp--get-opening-regexp)) + (not (evil-cp--looking-at-escaped-p)) (not (evil-cp--inside-string-p))))) (defun evil-cp--looking-at-opening-anywhere-p (&optional pos) @@ -81,6 +91,7 @@ question. Includes parentheses inside strings." (save-excursion (when pos (goto-char pos)) (and (sp--looking-at-p (evil-cp--get-opening-regexp)) + (not (evil-cp--looking-at-escaped-p)) (not (evil-cp--looking-at-string-closing-p))))) (defun evil-cp--looking-at-closing-p (&optional pos) @@ -90,6 +101,7 @@ question. Ignores parentheses inside strings." (save-excursion (when pos (goto-char pos)) (and (sp--looking-at-p (evil-cp--get-closing-regexp)) + (not (evil-cp--looking-at-escaped-p)) (not (evil-cp--inside-string-p))))) (defun evil-cp--looking-at-paren-p (&optional pos) From 3a365da809be59f09ed9dbc4aa8b9c74ba1a1a07 Mon Sep 17 00:00:00 2001 From: Tom Dalziel Date: Fri, 18 Aug 2023 23:19:30 +0100 Subject: [PATCH 2/3] Bypass sp-get-enclosing-sexp for deletion Use evil-cp--matching-paren-pos's value instead. --- evil-cleverparens.el | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/evil-cleverparens.el b/evil-cleverparens.el index b7d415f..9154c09 100644 --- a/evil-cleverparens.el +++ b/evil-cleverparens.el @@ -597,13 +597,12 @@ delimiters in the region defined by BEG and END." (delete-char diff) (setq chars-left (- chars-left diff)))) ((evil-cp--looking-at-any-opening-p) - (let ((other-end (evil-cp--matching-paren-pos))) + (let ((p (point)) + (other-end (evil-cp--matching-paren-pos))) ;; matching paren is in the range of the command - (if (<= (point) other-end end) - (let ((char-count - (evil-cp--guard-point - (sp-get (sp-get-enclosing-sexp) - (- :end :beg))))) + (if (<= p other-end end) + ;; 1+ makes the char-count inclusive + (let ((char-count (1+ (- other-end p)))) (delete-char char-count) (setq chars-left (- chars-left char-count))) (forward-char) From 07104ec362755518dc3dff5b19df6a39378b5bae Mon Sep 17 00:00:00 2001 From: Tom Dalziel Date: Sat, 19 Aug 2023 00:45:56 +0100 Subject: [PATCH 3/3] Fix deletion for escaped parens --- evil-cleverparens-tests.el | 24 ++++++++++++++++++++++-- evil-cleverparens-util.el | 12 ++++++------ evil-cleverparens.el | 3 +++ 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/evil-cleverparens-tests.el b/evil-cleverparens-tests.el index 890c311..269c152 100644 --- a/evil-cleverparens-tests.el +++ b/evil-cleverparens-tests.el @@ -283,8 +283,21 @@ golf foxtrot deltahotel india")) (evil-cp-test-buffer "(alpha (bravo[ ]charlie))" ("d^") - "(( charlie))"))) - + "(( charlie))")) + (ert-info ("Can delete line containing escaped parens") + (evil-test-buffer ;; As we need to enable evil-cp later anyway + "(alpha\n(b[r]avo ?\\))\ncharlie)" + (emacs-lisp-mode) + (evil-cleverparens-mode t) + ("dd") + "(alpha\ncharlie)")) + (ert-info ("Can delete unbalanced line containing escaped parens") + (evil-test-buffer ;; As we need to enable evil-cp later anyway + "(alpha\n(b[r]avo ?\\)\ncharlie)\ndelta)" + (emacs-lisp-mode) + (evil-cleverparens-mode t) + ("dd") + "(alpha\n (charlie)\n delta)"))) ;; (alpha[ ]bravo) charlie ;; charlie (bravo[ ]alpha) @@ -668,6 +681,13 @@ golf foxtrot deltahotel india")) "alpha bravo (charlie [d]elta echo) foxtrot golf" (">") "alpha bravo (charlie [d]elta echo foxtrot) golf")) + (ert-info ("Can slurp escaped parens") + (evil-test-buffer ;; As we need to enable evil-cp later anyway + "(alpha[)] ?\\) bravo" + (emacs-lisp-mode) + (evil-cleverparens-mode t) + (">") + "(alpha ?\\)) bravo")) (ert-info ("Can barf backwards when on opening delimiter") (evil-cp-test-buffer "alpha bravo [(]charlie delta echo) foxtrot golf" diff --git a/evil-cleverparens-util.el b/evil-cleverparens-util.el index 29db3ea..fa83465 100644 --- a/evil-cleverparens-util.el +++ b/evil-cleverparens-util.el @@ -47,14 +47,14 @@ (when pos (goto-char pos)) (looking-at-p evil-cp--ws-regexp))) -(defun evil-cp--looking-at-escaped-p (&optional pos) +(defun evil-cp--looking-at-escape-p (&optional pos) (save-excursion (when pos (goto-char pos)) - (or - (looking-at-p "\\\\") - (progn - (goto-char (- (point) 1)) - (looking-at-p "\\\\"))))) + (looking-at-p (regexp-quote (or sp-escape-char "\\"))))) + +(defun evil-cp--looking-at-escaped-p (&optional pos) + (or (evil-cp--looking-at-escape-p pos) + (evil-cp--looking-at-escape-p (1- (or pos (point)))))) (defun evil-cp--pair-for (pair pairs) (cond diff --git a/evil-cleverparens.el b/evil-cleverparens.el index 9154c09..7179a63 100644 --- a/evil-cleverparens.el +++ b/evil-cleverparens.el @@ -607,6 +607,9 @@ delimiters in the region defined by BEG and END." (setq chars-left (- chars-left char-count))) (forward-char) (cl-decf chars-left)))) + ((and (evil-cp--looking-at-escape-p) (< 1 chars-left)) + (delete-char 2) + (cl-decf chars-left 2)) ((and (evil-cp--looking-at-any-closing-p) (= chars-left 1)) (cl-decf chars-left)) ((evil-cp--looking-at-any-closing-p)