From 5f0f60ebcce61954324c172700fa8ac924b605f7 Mon Sep 17 00:00:00 2001 From: Jim Porter Date: Fri, 8 Jul 2022 18:41:07 -0700 Subject: [PATCH 2/3] Allow Eshell variable aliases to point to other aliases In particular, this resolves an issue where '$+' referenced the real environment variable '$PWD' instead of the Eshell variable alias of the same name. This meant that changing directories in Eshell wouldn't update the value of '$+'. * lisp/eshell/esh-var.el (eshell-get-variable): Allow Eshell variable aliaes to point to other aliases. * test/lisp/eshell/em-dirs-tests.el (em-dirs-test/pwd-var) (em-dirs-test/short-pwd-var): Adapt tests to check this case (bug#56509). --- lisp/eshell/esh-var.el | 41 +++++++++++++++---------------- test/lisp/eshell/em-dirs-tests.el | 10 +++++--- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/lisp/eshell/esh-var.el b/lisp/eshell/esh-var.el index 5092d2c6a5..e1535c1c5d 100644 --- a/lisp/eshell/esh-var.el +++ b/lisp/eshell/esh-var.el @@ -549,27 +549,26 @@ eshell-get-variable "Get the value for the variable NAME. INDICES is a list of index-lists (see `eshell-parse-indices'). If QUOTED is non-nil, this was invoked inside double-quotes." - (let* ((alias (assoc name eshell-variable-aliases-list)) - (var (if alias - (cadr alias) - name))) - (if (and alias (functionp var)) - (funcall var indices) - (eshell-apply-indices - (cond - ((stringp var) - (let ((sym (intern-soft var))) - (if (and sym (boundp sym) - (or eshell-prefer-lisp-variables - (memq sym eshell--local-vars) ; bug#15372 - (not (getenv var)))) - (symbol-value sym) - (getenv var)))) - ((symbolp var) - (symbol-value var)) - (t - (error "Unknown variable `%s'" (eshell-stringify var)))) - indices quoted)))) + (if-let ((alias (assoc name eshell-variable-aliases-list))) + (let ((target (cadr alias))) + (cond + ((functionp target) + (funcall target indices)) + ((symbolp target) + (eshell-apply-indices (symbol-value target) indices quoted)) + (t + (eshell-get-variable target indices quoted)))) + (unless (stringp name) + (error "Unknown variable `%s'" (eshell-stringify name))) + (eshell-apply-indices + (let ((sym (intern-soft name))) + (if (and sym (boundp sym) + (or eshell-prefer-lisp-variables + (memq sym eshell--local-vars) ; bug#15372 + (not (getenv name)))) + (symbol-value sym) + (getenv name))) + indices quoted))) (defun eshell-apply-indices (value indices &optional quoted) "Apply to VALUE all of the given INDICES, returning the sub-result. diff --git a/test/lisp/eshell/em-dirs-tests.el b/test/lisp/eshell/em-dirs-tests.el index eb27acd208..69480051e4 100644 --- a/test/lisp/eshell/em-dirs-tests.el +++ b/test/lisp/eshell/em-dirs-tests.el @@ -36,13 +36,15 @@ (ert-deftest em-dirs-test/pwd-var () "Test using the $PWD variable." - (should (equal (eshell-test-command-result "echo $PWD") - (expand-file-name (eshell/pwd))))) + (let ((default-directory "/some/path")) + (should (equal (eshell-test-command-result "echo $PWD") + (expand-file-name default-directory))))) (ert-deftest em-dirs-test/short-pwd-var () "Test using the $+ (current directory) variable." - (should (equal (eshell-test-command-result "echo $+") - (expand-file-name (eshell/pwd))))) + (let ((default-directory "/some/path")) + (should (equal (eshell-test-command-result "echo $+") + (expand-file-name default-directory))))) (ert-deftest em-dirs-test/oldpwd-var () "Test using the $OLDPWD variable." -- 2.25.1