Add polyfill for Emacs 26+ alist-get

This commit is contained in:
Henrik Lissner 2019-07-22 22:31:09 +02:00
parent 0eb200c49f
commit be2ade62d2
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -12,7 +12,27 @@
;; if-let and when-let were moved to (if|when)-let* in Emacs 26+ so we alias ;; if-let and when-let were moved to (if|when)-let* in Emacs 26+ so we alias
;; them for 25 users. ;; them for 25 users.
(defalias 'if-let* #'if-let) (defalias 'if-let* #'if-let)
(defalias 'when-let* #'when-let))) (defalias 'when-let* #'when-let)
(defun alist-get (key alist &optional default remove testfn)
"Return the value associated with KEY in ALIST.
If KEY is not found in ALIST, return DEFAULT.
Use TESTFN to lookup in the alist if non-nil. Otherwise, use `assq'.
This is a generalized variable suitable for use with `setf'.
When using it to set a value, optional argument REMOVE non-nil
means to remove KEY from ALIST if the new value is `eql' to DEFAULT."
(ignore remove) ;;Silence byte-compiler.
(let ((x (if (not testfn)
(assq key alist)
;; In Emacs<26, `assoc' has no testfn arg, so we have to
;; implement it ourselves
(if testfn
(cl-loop for entry in alist
if (funcall testfn key entry)
return entry)
(assoc key alist)))))
(if x (cdr x) default)))))
;; ;;