[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
09/37: emacs: Add 'guix-alist-put'.
From: |
Alex Kost |
Subject: |
09/37: emacs: Add 'guix-alist-put'. |
Date: |
Fri, 18 Dec 2015 18:51:23 +0000 |
alezost pushed a commit to branch wip-refactor-emacs-ui
in repository guix.
commit 8289dd3f713e5b574ff31975707bde88f55e5c89
Author: Alex Kost <address@hidden>
Date: Thu Nov 19 01:17:32 2015 +0300
emacs: Add 'guix-alist-put'.
* emacs/guix-utils.el (guix-alist-put, guix-alist-put-1)
(guix-alist-put!): New procedures.
---
emacs/guix-utils.el | 44 +++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 43 insertions(+), 1 deletions(-)
diff --git a/emacs/guix-utils.el b/emacs/guix-utils.el
index 3748350..fbe0a61 100644
--- a/emacs/guix-utils.el
+++ b/emacs/guix-utils.el
@@ -307,7 +307,7 @@ Example:
,@body)))
-;;; Alist accessors
+;;; Alist procedures
(defmacro guix-define-alist-accessor (name assoc-fun)
"Define NAME function to access alist values using ASSOC-FUN."
@@ -325,6 +325,48 @@ accessed with KEYS."
(guix-define-alist-accessor guix-assq-value assq)
(guix-define-alist-accessor guix-assoc-value assoc)
+(defun guix-alist-put (value alist &rest keys)
+ "Put (add or replace if exists) VALUE to ALIST using KEYS.
+Return the new alist.
+
+ALIST is alist of alists of alists ... which can be consecutively
+accessed with KEYS.
+
+Example:
+
+ (guix-alist-put
+ 'foo
+ '((one (a . 1) (b . 2))
+ (two (m . 7) (n . 8)))
+ 'one 'b)
+
+ => ((one (a . 1) (b . foo))
+ (two (m . 7) (n . 8)))"
+ (or keys (error "Keys should be specified"))
+ (guix-alist-put-1 value alist keys))
+
+(defun guix-alist-put-1 (value alist keys)
+ "Subroutine of `guix-alist-put'."
+ (cond
+ ((null keys)
+ value)
+ ((null alist)
+ (list (cons (car keys)
+ (guix-alist-put-1 value nil (cdr keys)))))
+ ((eq (car keys) (caar alist))
+ (cons (cons (car keys)
+ (guix-alist-put-1 value (cdar alist) (cdr keys)))
+ (cdr alist)))
+ (t
+ (cons (car alist)
+ (guix-alist-put-1 value (cdr alist) keys)))))
+
+(defun guix-alist-put! (value variable &rest keys)
+ "Modify alist VARIABLE (symbol) by putting VALUE using KEYS.
+See `guix-alist-put' for details."
+ (set variable
+ (apply #'guix-alist-put value (symbol-value variable) keys)))
+
;;; Diff
- branch wip-refactor-emacs-ui created (now c787c7b), Alex Kost, 2015/12/18
- 01/37: emacs: Use "cl-list*" instead of "apply #'list"., Alex Kost, 2015/12/18
- 02/37: emacs: Add 'guix-with-indent'., Alex Kost, 2015/12/18
- 04/37: emacs: Simplify defining parameter titles., Alex Kost, 2015/12/18
- 03/37: emacs: Add API for 'guix-entry'., Alex Kost, 2015/12/18
- 05/37: emacs: list: Generate numerical sort predicates., Alex Kost, 2015/12/18
- 07/37: emacs: list: Configure format in one place., Alex Kost, 2015/12/18
- 09/37: emacs: Add 'guix-alist-put'.,
Alex Kost <=
- 08/37: emacs: Add 'guix-keyword-args-let'., Alex Kost, 2015/12/18
- 12/37: emacs: list: Generalize 'sort-key' code., Alex Kost, 2015/12/18
- 10/37: emacs: list: Generalize 'marks' code., Alex Kost, 2015/12/18
- 11/37: emacs: list: Generalize 'describe' code., Alex Kost, 2015/12/18
- 13/37: emacs: list: Add 'guix-list-mode-initialize'., Alex Kost, 2015/12/18
- 14/37: emacs: list: Split 'guix-list-format' variable., Alex Kost, 2015/12/18
- 15/37: emacs: list: Factorize "edit package" commands., Alex Kost, 2015/12/18
- 06/37: emacs: Rename internal procedures., Alex Kost, 2015/12/18
- 20/37: emacs: Add wrappers for 'history-size' and 'revert-confirm'., Alex Kost, 2015/12/18
- 24/37: emacs: Improve messages for packages found by ID., Alex Kost, 2015/12/18