From: Jim Porter Date: Wed, 27 Apr 2022 04:51:23 +0000 (-0700) Subject: Handle escaped characters in Eshell argument predicates/modifiers X-Git-Tag: emacs-29.0.90~1931^2~150 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=bb40507fed7b211bb0ef5b5e3dcc609876f6ad8d;p=emacs.git Handle escaped characters in Eshell argument predicates/modifiers * lisp/eshell/em-pred.el (eshell-get-delimited-modifier-argument): Unescape escaped characters. * test/lisp/eshell/em-pred-tests.el (em-pred-test/predicate-escaping): New test (bug#55204). --- diff --git a/lisp/eshell/em-pred.el b/lisp/eshell/em-pred.el index 594563554d2..d73976d3464 100644 --- a/lisp/eshell/em-pred.el +++ b/lisp/eshell/em-pred.el @@ -416,7 +416,9 @@ before the closing delimiter. This allows modifiers like (close (cdr (assoc open eshell-pred-delimiter-pairs))) (end (eshell-find-delimiter open close nil nil t))) (prog1 - (buffer-substring-no-properties (1+ (point)) end) + (replace-regexp-in-string + (rx-to-string `(seq "\\" (group (or "\\" ,open ,close)))) "\\1" + (buffer-substring-no-properties (1+ (point)) end)) (goto-char (if (and chained-p (eq open close)) end (1+ end)))))) diff --git a/test/lisp/eshell/em-pred-tests.el b/test/lisp/eshell/em-pred-tests.el index 4d2af392923..3b50543d69a 100644 --- a/test/lisp/eshell/em-pred-tests.el +++ b/test/lisp/eshell/em-pred-tests.el @@ -533,4 +533,16 @@ PREDICATE is the predicate used to query that attribute." (format ":j%c-%c" (car delims) (cdr delims))) "foo-bar-baz")))) +(ert-deftest em-pred-test/predicate-escaping () + "Test string escaping in predicate and modifier parameters." + ;; Escaping the delimiter should remove the backslash. + (should (equal (eshell-eval-predicate '("foo" "bar" "baz") ":j'\\''") + "foo'bar'baz")) + ;; Escaping a backlash should remove the first backslash. + (should (equal (eshell-eval-predicate '("foo" "bar" "baz") ":j'\\\\'") + "foo\\bar\\baz")) + ;; Escaping a different character should keep the backslash. + (should (equal (eshell-eval-predicate '("foo" "bar" "baz") ":j'\\\"'") + "foo\\\"bar\\\"baz"))) + ;; em-pred-tests.el ends here