From be2ade62d2c5fe7167995ebf47e5baa32267a95a Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Mon, 22 Jul 2019 22:31:09 +0200 Subject: [PATCH] Add polyfill for Emacs 26+ alist-get --- core/core-lib.el | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/core/core-lib.el b/core/core-lib.el index 22c887724..ffa092609 100644 --- a/core/core-lib.el +++ b/core/core-lib.el @@ -12,7 +12,27 @@ ;; if-let and when-let were moved to (if|when)-let* in Emacs 26+ so we alias ;; them for 25 users. (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))))) ;;