]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/let-alist.el
; Fix breakage from previous commit
[gnu-emacs] / lisp / emacs-lisp / let-alist.el
1 ;;; let-alist.el --- Easily let-bind values of an assoc-list by their names -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2014-2016 Free Software Foundation, Inc.
4
5 ;; Author: Artur Malabarba <emacs@endlessparentheses.com>
6 ;; Package-Requires: ((emacs "24.1"))
7 ;; Version: 1.0.4
8 ;; Keywords: extensions lisp
9 ;; Prefix: let-alist
10 ;; Separator: -
11
12 ;; This is an Elpa :core package. Don't use functionality that is not
13 ;; compatible with Emacs 24.1.
14
15 ;; This file is part of GNU Emacs.
16
17 ;; GNU Emacs is free software: you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation, either version 3 of the License, or
20 ;; (at your option) any later version.
21
22 ;; GNU Emacs is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
26
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29
30 ;;; Commentary:
31
32 ;; This package offers a single macro, `let-alist'. This macro takes a
33 ;; first argument (whose value must be an alist) and a body.
34 ;;
35 ;; The macro expands to a let form containing body, where each dotted
36 ;; symbol inside body is let-bound to their cdrs in the alist. Dotted
37 ;; symbol is any symbol starting with a `.'. Only those present in
38 ;; the body are let-bound and this search is done at compile time.
39 ;;
40 ;; For instance, the following code
41 ;;
42 ;; (let-alist alist
43 ;; (if (and .title .body)
44 ;; .body
45 ;; .site
46 ;; .site.contents))
47 ;;
48 ;; essentially expands to
49 ;;
50 ;; (let ((.title (cdr (assq 'title alist)))
51 ;; (.body (cdr (assq 'body alist)))
52 ;; (.site (cdr (assq 'site alist)))
53 ;; (.site.contents (cdr (assq 'contents (cdr (assq 'site alist))))))
54 ;; (if (and .title .body)
55 ;; .body
56 ;; .site
57 ;; .site.contents))
58 ;;
59 ;; If you nest `let-alist' invocations, the inner one can't access
60 ;; the variables of the outer one. You can, however, access alists
61 ;; inside the original alist by using dots inside the symbol, as
62 ;; displayed in the example above by the `.site.contents'.
63 ;;
64 ;;; Code:
65 \f
66
67 (defun let-alist--deep-dot-search (data)
68 "Return alist of symbols inside DATA that start with a `.'.
69 Perform a deep search and return an alist where each car is the
70 symbol, and each cdr is the same symbol without the `.'."
71 (cond
72 ((symbolp data)
73 (let ((name (symbol-name data)))
74 (when (string-match "\\`\\." name)
75 ;; Return the cons cell inside a list, so it can be appended
76 ;; with other results in the clause below.
77 (list (cons data (intern (replace-match "" nil nil name)))))))
78 ((not (consp data)) nil)
79 (t (append (let-alist--deep-dot-search (car data))
80 (let-alist--deep-dot-search (cdr data))))))
81
82 (defun let-alist--access-sexp (symbol variable)
83 "Return a sexp used to access SYMBOL inside VARIABLE."
84 (let* ((clean (let-alist--remove-dot symbol))
85 (name (symbol-name clean)))
86 (if (string-match "\\`\\." name)
87 clean
88 (let-alist--list-to-sexp
89 (mapcar #'intern (nreverse (split-string name "\\.")))
90 variable))))
91
92 (defun let-alist--list-to-sexp (list var)
93 "Turn symbols LIST into recursive calls to `cdr' `assq' on VAR."
94 `(cdr (assq ',(car list)
95 ,(if (cdr list) (let-alist--list-to-sexp (cdr list) var)
96 var))))
97
98 (defun let-alist--remove-dot (symbol)
99 "Return SYMBOL, sans an initial dot."
100 (let ((name (symbol-name symbol)))
101 (if (string-match "\\`\\." name)
102 (intern (replace-match "" nil nil name))
103 symbol)))
104
105 \f
106 ;;; The actual macro.
107 ;;;###autoload
108 (defmacro let-alist (alist &rest body)
109 "Let-bind dotted symbols to their cdrs in ALIST and execute BODY.
110 Dotted symbol is any symbol starting with a `.'. Only those present
111 in BODY are let-bound and this search is done at compile time.
112
113 For instance, the following code
114
115 (let-alist alist
116 (if (and .title .body)
117 .body
118 .site
119 .site.contents))
120
121 essentially expands to
122
123 (let ((.title (cdr (assq \\='title alist)))
124 (.body (cdr (assq \\='body alist)))
125 (.site (cdr (assq \\='site alist)))
126 (.site.contents (cdr (assq \\='contents (cdr (assq \\='site alist))))))
127 (if (and .title .body)
128 .body
129 .site
130 .site.contents))
131
132 If you nest `let-alist' invocations, the inner one can't access
133 the variables of the outer one. You can, however, access alists
134 inside the original alist by using dots inside the symbol, as
135 displayed in the example above."
136 (declare (indent 1) (debug t))
137 (let ((var (make-symbol "alist")))
138 `(let ((,var ,alist))
139 (let ,(mapcar (lambda (x) `(,(car x) ,(let-alist--access-sexp (car x) var)))
140 (delete-dups (let-alist--deep-dot-search body)))
141 ,@body))))
142
143 (provide 'let-alist)
144
145 ;;; let-alist.el ends here