From b610f36d44dda3beb5cf2b8b65bfb0d005afed5c Mon Sep 17 00:00:00 2001 From: Spencer Baugh Date: Thu, 28 Aug 2025 15:04:39 -0400 Subject: [PATCH] Document and test 'let-alist' support for indexing * etc/NEWS: Announce 'let-alist' support for indexing. * test/lisp/emacs-lisp/let-alist-tests.el (let-alist-numbers): Add a test for 'let-alist's support for indexing. * doc/lispref/lists.texi (Association Lists): Document indexing with 'let-alist'. (Bug#66509) --- doc/lispref/lists.texi | 13 +++++++++++++ etc/NEWS | 6 ++++++ test/lisp/emacs-lisp/let-alist-tests.el | 11 +++++++++++ 3 files changed, 30 insertions(+) diff --git a/doc/lispref/lists.texi b/doc/lispref/lists.texi index 37a07421e94..81edcc63d5b 100644 --- a/doc/lispref/lists.texi +++ b/doc/lispref/lists.texi @@ -1934,6 +1934,19 @@ Nested association lists is supported: Nesting @code{let-alist} inside each other is allowed, but the code in the inner @code{let-alist} can't access the variables bound by the outer @code{let-alist}. + +Indexing into lists is also supported: + +@lisp +(setq colors '((rose . red) (lily . (yellow pink)))) +(let-alist colors .lily.1) + @result{} pink +@end lisp + +Note that forms like @samp{.0} or @samp{.3} are interpreted as numbers +rather than as symbols, so they won't be bound to the corresponding +values in ALIST. + @end defmac @node Property Lists diff --git a/etc/NEWS b/etc/NEWS index 3dc0e0a7677..8a139cb03ca 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -2854,6 +2854,12 @@ function 'load-path-filter-cache-directory-files', calling 'load' will cache the directories it scans and their files, and the following lookups should be faster. ++++ +** 'let-alist' supports indexing into lists. +The macro 'let-alist' now interprets symbols containing numbers as list +indices. For example, '.key.0' looks up 'key' in the alist and then +returns its first element. + ** Lexical binding --- diff --git a/test/lisp/emacs-lisp/let-alist-tests.el b/test/lisp/emacs-lisp/let-alist-tests.el index 988b05b488c..b23178f5467 100644 --- a/test/lisp/emacs-lisp/let-alist-tests.el +++ b/test/lisp/emacs-lisp/let-alist-tests.el @@ -100,4 +100,15 @@ See Bug#24641." `[,(+ .a) ,(+ .a .b .b)]) [1 5]))) +(ert-deftest let-alist-numbers () + "Check that .num indexes into lists." + (should (equal + (let-alist + '(((a . val1) (b . (nil val2))) + (c . (val3))) + (list .0 .0.a .0.b.1 .c.0)) + ;; .0 is interpreted as a number, so we can't use `let-alist' + ;; to do indexing alone. Everything else works though. + '(0.0 val1 val2 val3)))) + ;;; let-alist-tests.el ends here