From: Jim Porter Date: Tue, 13 Sep 2022 23:14:00 +0000 (-0700) Subject: Allow using a symbol as an index into an alist in Eshell X-Git-Tag: emacs-29.0.90~1856^2~497 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=b8e9239b47391c6628d94a4e2e91320c5366d27b;p=emacs.git Allow using a symbol as an index into an alist in Eshell * lisp/eshell/esh-var.el (eshell-index-value): If INDEX is a symbol, use 'assoc' for indexing. * test/lisp/eshell/esh-var-tests.el (esh-var-test/interp-var-assoc) (esh-var-test/quoted-interp-var-assoc): Add checks for indexing via symbol (bug#57787). --- diff --git a/lisp/eshell/esh-var.el b/lisp/eshell/esh-var.el index a9df172e88e..36e59cd5a41 100644 --- a/lisp/eshell/esh-var.el +++ b/lisp/eshell/esh-var.el @@ -646,23 +646,24 @@ For example, to retrieve the second element of a user's record in "Reference VALUE using the given INDEX." (when (and (stringp index) (get-text-property 0 'number index)) (setq index (string-to-number index))) - (if (stringp index) - (cdr (assoc index value)) - (cond - ((ring-p value) - (if (> index (ring-length value)) - (error "Index exceeds length of ring") - (ring-ref value index))) - ((listp value) - (if (> index (length value)) - (error "Index exceeds length of list") - (nth index value))) - ((vectorp value) - (if (> index (length value)) - (error "Index exceeds length of vector") - (aref value index))) - (t - (error "Invalid data type for indexing"))))) + (if (integerp index) + (cond + ((ring-p value) + (if (> index (ring-length value)) + (error "Index exceeds length of ring") + (ring-ref value index))) + ((listp value) + (if (> index (length value)) + (error "Index exceeds length of list") + (nth index value))) + ((vectorp value) + (if (> index (length value)) + (error "Index exceeds length of vector") + (aref value index))) + (t + (error "Invalid data type for indexing"))) + ;; INDEX is some non-integer value, so treat VALUE as an alist. + (cdr (assoc index value)))) ;;;_* Variable name completion diff --git a/test/lisp/eshell/esh-var-tests.el b/test/lisp/eshell/esh-var-tests.el index bebc57d3592..cb5b1766bb5 100644 --- a/test/lisp/eshell/esh-var-tests.el +++ b/test/lisp/eshell/esh-var-tests.el @@ -105,9 +105,11 @@ (ert-deftest esh-var-test/interp-var-assoc () "Interpolate alist variable with index" - (let ((eshell-test-value '(("foo" . 1)))) + (let ((eshell-test-value '(("foo" . 1) (bar . 2)))) (eshell-command-result-equal "echo $eshell-test-value[foo]" - 1))) + 1) + (eshell-command-result-equal "echo $eshell-test-value[#'bar]" + 2))) (ert-deftest esh-var-test/interp-var-length-list () "Interpolate length of list variable" @@ -257,9 +259,11 @@ inside double-quotes" (ert-deftest esh-var-test/quoted-interp-var-assoc () "Interpolate alist variable with index inside double-quotes" - (let ((eshell-test-value '(("foo" . 1)))) + (let ((eshell-test-value '(("foo" . 1) (bar . 2)))) (eshell-command-result-equal "echo \"$eshell-test-value[foo]\"" - "1"))) + "1") + (eshell-command-result-equal "echo \"$eshell-test-value[#'bar]\"" + "2"))) (ert-deftest esh-var-test/quoted-interp-var-length-list () "Interpolate length of list variable inside double-quotes"