1 ;;; let-alist.el --- Easily let-bind values of an assoc-list by their names -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2014-2015 Free Software Foundation, Inc.
5 ;; Author: Artur Malabarba <bruce.connor.am@gmail.com>
6 ;; Maintainer: Artur Malabarba <bruce.connor.am@gmail.com>
8 ;; Keywords: extensions lisp
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; This package offers a single macro, `let-alist'. This macro takes a
30 ;; first argument (whose value must be an alist) and a body.
32 ;; The macro expands to a let form containing body, where each dotted
33 ;; symbol inside body is let-bound to their cdrs in the alist. Dotted
34 ;; symbol is any symbol starting with a `.'. Only those present in
35 ;; the body are let-bound and this search is done at compile time.
37 ;; For instance, the following code
40 ;; (if (and .title .body)
45 ;; essentially expands to
47 ;; (let ((.title (cdr (assq 'title alist)))
48 ;; (.body (cdr (assq 'body alist)))
49 ;; (.site (cdr (assq 'site alist)))
50 ;; (.site.contents (cdr (assq 'contents (cdr (assq 'site alist))))))
51 ;; (if (and .title .body)
56 ;; If you nest `let-alist' invocations, the inner one can't access
57 ;; the variables of the outer one. You can, however, access alists
58 ;; inside the original alist by using dots inside the symbol, as
59 ;; displayed in the example above by the `.site.contents'.
64 (defun let-alist--deep-dot-search (data)
65 "Return alist of symbols inside DATA that start with a `.'.
66 Perform a deep search and return an alist where each car is the
67 symbol, and each cdr is the same symbol without the `.'."
70 (let ((name (symbol-name data
)))
71 (when (string-match "\\`\\." name
)
72 ;; Return the cons cell inside a list, so it can be appended
73 ;; with other results in the clause below.
74 (list (cons data
(intern (replace-match "" nil nil name
)))))))
75 ((not (listp data
)) nil
)
77 (mapcar #'let-alist--deep-dot-search data
)))))
79 (defun let-alist--access-sexp (symbol variable
)
80 "Return a sexp used to access SYMBOL inside VARIABLE."
81 (let* ((clean (let-alist--remove-dot symbol
))
82 (name (symbol-name clean
)))
83 (if (string-match "\\`\\." name
)
85 (let-alist--list-to-sexp
86 (mapcar #'intern
(nreverse (split-string name
"\\.")))
89 (defun let-alist--list-to-sexp (list var
)
90 "Turn symbols LIST into recursive calls to `cdr' `assq' on VAR."
91 `(cdr (assq ',(car list
)
92 ,(if (cdr list
) (let-alist--list-to-sexp (cdr list
) var
)
95 (defun let-alist--remove-dot (symbol)
96 "Return SYMBOL, sans an initial dot."
97 (let ((name (symbol-name symbol
)))
98 (if (string-match "\\`\\." name
)
99 (intern (replace-match "" nil nil name
))
103 ;;; The actual macro.
105 (defmacro let-alist
(alist &rest body
)
106 "Let-bind dotted symbols to their cdrs in ALIST and execute BODY.
107 Dotted symbol is any symbol starting with a `.'. Only those present
108 in BODY are let-bound and this search is done at compile time.
110 For instance, the following code
113 (if (and .title .body)
118 essentially expands to
120 (let ((.title (cdr (assq 'title alist)))
121 (.body (cdr (assq 'body alist)))
122 (.site (cdr (assq 'site alist)))
123 (.site.contents (cdr (assq 'contents (cdr (assq 'site alist))))))
124 (if (and .title .body)
129 If you nest `let-alist' invocations, the inner one can't access
130 the variables of the outer one. You can, however, access alists
131 inside the original alist by using dots inside the symbol, as
132 displayed in the example above."
133 (declare (indent 1) (debug t
))
134 (let ((var (make-symbol "alist")))
135 `(let ((,var
,alist
))
136 (let ,(mapcar (lambda (x) `(,(car x
) ,(let-alist--access-sexp (car x
) var
)))
137 (delete-dups (let-alist--deep-dot-search body
)))
142 ;;; let-alist.el ends here