From 51dac383392a723aa77b0496cf12c593b013cb2b Mon Sep 17 00:00:00 2001 From: Alex Kost Date: Sun, 16 Aug 2015 07:11:57 +0300 Subject: emacs: Add and use alist accessors. * emacs/guix-utils.el (guix-define-alist-accessor): New macro. (guix-assq-value, guix-assoc-value): New functions. (guix-get-key-val): Remove. * emacs/guix-base.el: Replace 'guix-get-key-val' with 'guix-assq-value' everywhere. * emacs/guix-info.el: Likewise. * emacs/guix-list.el: Likewise. * emacs/guix-messages.el: Likewise. --- emacs/guix-utils.el | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'emacs/guix-utils.el') diff --git a/emacs/guix-utils.el b/emacs/guix-utils.el index 0b8a760af8..78ea3545c6 100644 --- a/emacs/guix-utils.el +++ b/emacs/guix-utils.el @@ -193,14 +193,6 @@ Return time value." (require 'org) (org-read-date nil t nil prompt)) -(defun guix-get-key-val (alist &rest keys) - "Return value from ALIST by KEYS. -ALIST is alist of alists of alists ... which can be consecutively -accessed with KEYS." - (let ((val alist)) - (dolist (key keys val) - (setq val (cdr (assq key val)))))) - (defun guix-find-file (file) "Find FILE if it exists." (if (file-exists-p file) @@ -223,6 +215,25 @@ Return nil otherwise." (or (funcall pred (car lst)) (guix-any pred (cdr lst))))) + +;;; Alist accessors + +(defmacro guix-define-alist-accessor (name assoc-fun) + "Define NAME function to access alist values using ASSOC-FUN." + `(defun ,name (alist &rest keys) + ,(format "Return value from ALIST by KEYS using `%s'. +ALIST is alist of alists of alists ... which can be consecutively +accessed with KEYS." + assoc-fun) + (if (or (null alist) (null keys)) + alist + (apply #',name + (cdr (,assoc-fun (car keys) alist)) + (cdr keys))))) + +(guix-define-alist-accessor guix-assq-value assq) +(guix-define-alist-accessor guix-assoc-value assoc) + ;;; Diff -- cgit v1.2.3