diff options
| author | Jim Porter | 2022-09-13 16:14:00 -0700 |
|---|---|---|
| committer | Jim Porter | 2022-09-14 17:27:21 -0700 |
| commit | b8e9239b47391c6628d94a4e2e91320c5366d27b (patch) | |
| tree | 9c36827e8763f35d18984ee41c00dd02715729c0 | |
| parent | 30ca49c8f64b73f991d94b10afcfc0e2d592fe6a (diff) | |
| download | emacs-b8e9239b47391c6628d94a4e2e91320c5366d27b.tar.gz emacs-b8e9239b47391c6628d94a4e2e91320c5366d27b.zip | |
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).
| -rw-r--r-- | lisp/eshell/esh-var.el | 35 | ||||
| -rw-r--r-- | test/lisp/eshell/esh-var-tests.el | 12 |
2 files changed, 26 insertions, 21 deletions
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 | |||
| 646 | "Reference VALUE using the given INDEX." | 646 | "Reference VALUE using the given INDEX." |
| 647 | (when (and (stringp index) (get-text-property 0 'number index)) | 647 | (when (and (stringp index) (get-text-property 0 'number index)) |
| 648 | (setq index (string-to-number index))) | 648 | (setq index (string-to-number index))) |
| 649 | (if (stringp index) | 649 | (if (integerp index) |
| 650 | (cdr (assoc index value)) | 650 | (cond |
| 651 | (cond | 651 | ((ring-p value) |
| 652 | ((ring-p value) | 652 | (if (> index (ring-length value)) |
| 653 | (if (> index (ring-length value)) | 653 | (error "Index exceeds length of ring") |
| 654 | (error "Index exceeds length of ring") | 654 | (ring-ref value index))) |
| 655 | (ring-ref value index))) | 655 | ((listp value) |
| 656 | ((listp value) | 656 | (if (> index (length value)) |
| 657 | (if (> index (length value)) | 657 | (error "Index exceeds length of list") |
| 658 | (error "Index exceeds length of list") | 658 | (nth index value))) |
| 659 | (nth index value))) | 659 | ((vectorp value) |
| 660 | ((vectorp value) | 660 | (if (> index (length value)) |
| 661 | (if (> index (length value)) | 661 | (error "Index exceeds length of vector") |
| 662 | (error "Index exceeds length of vector") | 662 | (aref value index))) |
| 663 | (aref value index))) | 663 | (t |
| 664 | (t | 664 | (error "Invalid data type for indexing"))) |
| 665 | (error "Invalid data type for indexing"))))) | 665 | ;; INDEX is some non-integer value, so treat VALUE as an alist. |
| 666 | (cdr (assoc index value)))) | ||
| 666 | 667 | ||
| 667 | ;;;_* Variable name completion | 668 | ;;;_* Variable name completion |
| 668 | 669 | ||
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 @@ | |||
| 105 | 105 | ||
| 106 | (ert-deftest esh-var-test/interp-var-assoc () | 106 | (ert-deftest esh-var-test/interp-var-assoc () |
| 107 | "Interpolate alist variable with index" | 107 | "Interpolate alist variable with index" |
| 108 | (let ((eshell-test-value '(("foo" . 1)))) | 108 | (let ((eshell-test-value '(("foo" . 1) (bar . 2)))) |
| 109 | (eshell-command-result-equal "echo $eshell-test-value[foo]" | 109 | (eshell-command-result-equal "echo $eshell-test-value[foo]" |
| 110 | 1))) | 110 | 1) |
| 111 | (eshell-command-result-equal "echo $eshell-test-value[#'bar]" | ||
| 112 | 2))) | ||
| 111 | 113 | ||
| 112 | (ert-deftest esh-var-test/interp-var-length-list () | 114 | (ert-deftest esh-var-test/interp-var-length-list () |
| 113 | "Interpolate length of list variable" | 115 | "Interpolate length of list variable" |
| @@ -257,9 +259,11 @@ inside double-quotes" | |||
| 257 | 259 | ||
| 258 | (ert-deftest esh-var-test/quoted-interp-var-assoc () | 260 | (ert-deftest esh-var-test/quoted-interp-var-assoc () |
| 259 | "Interpolate alist variable with index inside double-quotes" | 261 | "Interpolate alist variable with index inside double-quotes" |
| 260 | (let ((eshell-test-value '(("foo" . 1)))) | 262 | (let ((eshell-test-value '(("foo" . 1) (bar . 2)))) |
| 261 | (eshell-command-result-equal "echo \"$eshell-test-value[foo]\"" | 263 | (eshell-command-result-equal "echo \"$eshell-test-value[foo]\"" |
| 262 | "1"))) | 264 | "1") |
| 265 | (eshell-command-result-equal "echo \"$eshell-test-value[#'bar]\"" | ||
| 266 | "2"))) | ||
| 263 | 267 | ||
| 264 | (ert-deftest esh-var-test/quoted-interp-var-length-list () | 268 | (ert-deftest esh-var-test/quoted-interp-var-length-list () |
| 265 | "Interpolate length of list variable inside double-quotes" | 269 | "Interpolate length of list variable inside double-quotes" |