summaryrefslogtreecommitdiff
path: root/lisp/eshell/esh-util.el
diff options
context:
space:
mode:
authorJim Porter <jporterbugs@gmail.com>2022-02-28 17:38:39 -0800
committerLars Ingebrigtsen <larsi@gnus.org>2022-05-03 18:22:57 +0200
commitf7a82699d6e854c2920f7c090fb8df7a3e012a4d (patch)
tree49c602db57bad60c4a796d7e1198fb7373670aec /lisp/eshell/esh-util.el
parent316c082d58601119372a0ae6745cba96f3404c86 (diff)
downloademacs-f7a82699d6e854c2920f7c090fb8df7a3e012a4d.tar.gz
emacs-f7a82699d6e854c2920f7c090fb8df7a3e012a4d.tar.bz2
emacs-f7a82699d6e854c2920f7c090fb8df7a3e012a4d.zip
Eshell variable expansion should always return strings inside quotes
This is closer in behavior to regular shells, and gives Eshell users greater flexibility in how variables are expanded. * lisp/eshell/esh-util.el (eshell-convert): Add TO-STRING argument. * lisp/eshell/esh-var.el (eshell-parse-variable-ref): Add MODIFIER-P argument and adjust how 'eshell-convert' and 'eshell-apply-indices' are called. (eshell-get-variable, eshell-apply-indices): Add QUOTED argument. * test/lisp/eshell/esh-var-tests.el (eshell-test-value): New defvar. (esh-var-test/interp-convert-var-number) (esh-var-test/interp-convert-var-split-indices) (esh-var-test/interp-convert-quoted-var-number) (esh-var-test/interp-convert-quoted-var-split-indices) (esh-var-test/interp-convert-cmd-string-newline) (esh-var-test/interp-convert-cmd-multiline) (esh-var-test/interp-convert-cmd-number) (esh-var-test/interp-convert-cmd-split-indices) (esh-var-test/quoted-interp-convert-var-number) (esh-var-test/quoted-interp-convert-var-split-indices) (esh-var-test/quoted-interp-convert-quoted-var-number) (esh-var-test/quoted-interp-convert-quoted-var-split-indices) (esh-var-test/quoted-interp-convert-cmd-string-newline) (esh-var-test/quoted-interp-convert-cmd-multiline) (esh-var-test/quoted-interp-convert-cmd-number) (esh-var-test/quoted-interp-convert-cmd-split-indices): New tests. * doc/misc/eshell.texi (Arguments): Expand this section, and document the new behavior. (Dollars Expansion): Provide more detail about '$(lisp)' and '${command}' forms. * etc/NEWS (Eshell): Announce this change (bug#55236).
Diffstat (limited to 'lisp/eshell/esh-util.el')
-rw-r--r--lisp/eshell/esh-util.el46
1 files changed, 30 insertions, 16 deletions
diff --git a/lisp/eshell/esh-util.el b/lisp/eshell/esh-util.el
index 3da712c719a..6c130974e95 100644
--- a/lisp/eshell/esh-util.el
+++ b/lisp/eshell/esh-util.el
@@ -198,23 +198,37 @@ doubling it up."
(when (= depth 0)
(if reverse-p (point) (1- (point)))))))
-(defun eshell-convert (string)
- "Convert STRING into a more native looking Lisp object."
- (if (not (stringp string))
- string
- (let ((len (length string)))
- (if (= len 0)
- string
- (if (eq (aref string (1- len)) ?\n)
+(defun eshell-convert (string &optional to-string)
+ "Convert STRING into a more-native Lisp object.
+If TO-STRING is non-nil, always return a single string with
+trailing newlines removed. Otherwise, this behaves as follows:
+
+* Return non-strings as-is.
+
+* Split multiline strings by line.
+
+* If `eshell-convert-numeric-aguments' is non-nil, convert
+ numeric strings to numbers."
+ (cond
+ ((not (stringp string))
+ (if to-string
+ (eshell-stringify string)
+ string))
+ (to-string (string-trim-right string "\n+"))
+ (t (let ((len (length string)))
+ (if (= len 0)
+ string
+ (when (eq (aref string (1- len)) ?\n)
(setq string (substring string 0 (1- len))))
- (if (string-search "\n" string)
- (split-string string "\n")
- (if (and eshell-convert-numeric-arguments
- (string-match
- (concat "\\`\\s-*" eshell-number-regexp
- "\\s-*\\'") string))
- (string-to-number string)
- string))))))
+ (cond
+ ((string-search "\n" string)
+ (split-string string "\n"))
+ ((and eshell-convert-numeric-arguments
+ (string-match
+ (concat "\\`\\s-*" eshell-number-regexp "\\s-*\\'")
+ string))
+ (string-to-number string))
+ (t string)))))))
(defvar-local eshell-path-env (getenv "PATH")
"Content of $PATH.