summaryrefslogtreecommitdiff
path: root/lisp/emacs-lisp/map.el
diff options
context:
space:
mode:
authorNicolas Petton <nicolas@petton.fr>2015-06-21 23:44:50 +0200
committerNicolas Petton <nicolas@petton.fr>2015-06-21 23:44:50 +0200
commitfa52edd4c4eb9e2d8ae2e43821460cfd594593b5 (patch)
tree15e99f0614f5cefc8218b54c3112a4e2dd235ecc /lisp/emacs-lisp/map.el
parent5fac0dee87ea5d4aa90ee93606c19785919da105 (diff)
downloademacs-fa52edd4c4eb9e2d8ae2e43821460cfd594593b5.tar.gz
emacs-fa52edd4c4eb9e2d8ae2e43821460cfd594593b5.tar.bz2
emacs-fa52edd4c4eb9e2d8ae2e43821460cfd594593b5.zip
Revert "Define `map-elt' as a generalized variable"
This reverts commit 8b6d82d3ca86f76ed964063b3941a7c6ab0bf1c6.
Diffstat (limited to 'lisp/emacs-lisp/map.el')
-rw-r--r--lisp/emacs-lisp/map.el52
1 files changed, 24 insertions, 28 deletions
diff --git a/lisp/emacs-lisp/map.el b/lisp/emacs-lisp/map.el
index 8759616053a..1d8a3126bba 100644
--- a/lisp/emacs-lisp/map.el
+++ b/lisp/emacs-lisp/map.el
@@ -82,21 +82,25 @@ The following keyword types are meaningful: `:list',
An error is thrown if MAP is neither a list, hash-table nor array.
-Return RESULT if non-nil or the result of evaluation of the form.
+Return RESULT if non-nil or the result of evaluation of the
+form.
\(fn (VAR MAP [RESULT]) &rest ARGS)"
(declare (debug t) (indent 1))
(unless (listp spec)
(setq spec `(,spec ,spec)))
- (let ((map-var (car spec)))
- `(let* ,(unless (eq map-var (cadr spec)) `((,map-var ,(cadr spec))))
- (cond ((listp ,map-var) ,(plist-get args :list))
- ((hash-table-p ,map-var) ,(plist-get args :hash-table))
- ((arrayp ,map-var) ,(plist-get args :array))
- (t (error "Unsupported map: %s" ,map-var)))
- ,@(cddr spec))))
-
-(put 'map--raw-place 'gv-expander #'funcall)
+ (let ((map-var (car spec))
+ (result-var (make-symbol "result")))
+ `(let ((,map-var ,(cadr spec))
+ ,result-var)
+ (setq ,result-var
+ (cond ((listp ,map-var) ,(plist-get args :list))
+ ((hash-table-p ,map-var) ,(plist-get args :hash-table))
+ ((arrayp ,map-var) ,(plist-get args :array))
+ (t (error "Unsupported map: %s" ,map-var))))
+ ,@(when (cddr spec)
+ `((setq ,result-var ,@(cddr spec))))
+ ,result-var)))
(defun map-elt (map key &optional default)
"Perform a lookup in MAP of KEY and return its associated value.
@@ -105,34 +109,26 @@ If KEY is not found, return DEFAULT which defaults to nil.
If MAP is a list, `eql' is used to lookup KEY.
MAP can be a list, hash-table or array."
- (declare
- (gv-expander
- (lambda (do)
- (gv-letplace (mgetter msetter) map
- (macroexp-let2* nil
- ;; Eval them once and for all in the right order.
- ((key key) (default default))
- `(map--dispatch ,mgetter
- :list ,(gv-get `(alist-get ,key (map--raw-place ,mgetter ,msetter)
- ,default)
- do)
- :hash-table ,(gv-get `(gethash ,key (map--raw-place ,mgetter ,msetter)
- ,default))
- :array ,(gv-get (aref (map--raw-place ,mgetter ,msetter) ,key)
- do)))))))
(map--dispatch map
:list (alist-get key map default)
:hash-table (gethash key map default)
:array (map--elt-array map key default)))
-(defun map-put (map key value)
+(defmacro map-put (map key value)
"In MAP, associate KEY with VALUE and return MAP.
If KEY is already present in MAP, replace the associated value
with VALUE.
MAP can be a list, hash-table or array."
- (setf (map-elt map key) value)
- map)
+ (declare (debug t))
+ (let ((symbol (symbolp map)))
+ `(progn
+ (map--dispatch (m ,map m)
+ :list (if ,symbol
+ (setq ,map (cons (cons ,key ,value) m))
+ (error "Literal lists are not allowed, %s must be a symbol" ',map))
+ :hash-table (puthash ,key ,value m)
+ :array (aset m ,key ,value)))))
(defmacro map-delete (map key)
"In MAP, delete the key KEY if present and return MAP.