comparison lisp/emacs-lisp/assoc.el @ 107027:a64b73ff7ed5

Merge from mainline.
author Katsumi Yamaoka <yamaoka@jpl.org>
date Sun, 24 Jan 2010 21:58:18 +0000
parents f611f11aaf1f
children 376148b31b5e
comparison
equal deleted inserted replaced
107026:746534c20a06 107027:a64b73ff7ed5
1 ;;; assoc.el --- insert/delete/sort functions on association lists 1 ;;; assoc.el --- insert/delete/sort functions on association lists
2 2
3 ;; Copyright (C) 1996, 2001, 2002, 2003, 2004, 2005, 3 ;; Copyright (C) 1996, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 ;; 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. 4 ;; 2009, 2010 Free Software Foundation, Inc.
5 5
6 ;; Author: Barry A. Warsaw <bwarsaw@cen.com> 6 ;; Author: Barry A. Warsaw <bwarsaw@cen.com>
7 ;; Keywords: extensions 7 ;; Keywords: extensions
8 8
9 ;; This file is part of GNU Emacs. 9 ;; This file is part of GNU Emacs.
25 25
26 ;; Association list utilities providing insertion, deletion, sorting 26 ;; Association list utilities providing insertion, deletion, sorting
27 ;; fetching off key-value pairs in association lists. 27 ;; fetching off key-value pairs in association lists.
28 28
29 ;;; Code: 29 ;;; Code:
30 (eval-when-compile (require 'cl))
30 31
31 (defun asort (alist-symbol key) 32 (defun asort (alist-symbol key)
32 "Move a specified key-value pair to the head of an alist. 33 "Move a specified key-value pair to the head of an alist.
33 The alist is referenced by ALIST-SYMBOL. Key-value pair to move to 34 The alist is referenced by ALIST-SYMBOL. Key-value pair to move to
34 head is one matching KEY. Returns the sorted list and doesn't affect 35 head is one matching KEY. Returns the sorted list and doesn't affect
39 (function (lambda (a b) (equal (car a) key)))))) 40 (function (lambda (a b) (equal (car a) key))))))
40 41
41 42
42 (defun aelement (key value) 43 (defun aelement (key value)
43 "Make a list of a cons cell containing car of KEY and cdr of VALUE. 44 "Make a list of a cons cell containing car of KEY and cdr of VALUE.
44 The returned list is suitable as an element of an alist." 45 The returned list is suitable for concatenating with an existing
46 alist, via `nconc'."
45 (list (cons key value))) 47 (list (cons key value)))
46 48
47 49
48 (defun aheadsym (alist) 50 (defun aheadsym (alist)
49 "Return the key symbol at the head of ALIST." 51 "Return the key symbol at the head of ALIST."
69 VALUE is supplied non-nil, then the value of KEY will be set to VALUE. 71 VALUE is supplied non-nil, then the value of KEY will be set to VALUE.
70 If VALUE is not supplied, or is nil, the key-value pair will not be 72 If VALUE is not supplied, or is nil, the key-value pair will not be
71 modified, but will be moved to the head of the alist. If the key-value 73 modified, but will be moved to the head of the alist. If the key-value
72 pair cannot be found in the alist, it will be inserted into the head 74 pair cannot be found in the alist, it will be inserted into the head
73 of the alist (with value nil if VALUE is nil or not supplied)." 75 of the alist (with value nil if VALUE is nil or not supplied)."
74 (let ((elem (aelement key value)) 76 (lexical-let ((elem (aelement key value))
75 alist) 77 alist)
76 (asort alist-symbol key) 78 (asort alist-symbol key)
77 (setq alist (eval alist-symbol)) 79 (setq alist (eval alist-symbol))
78 (cond ((null alist) (set alist-symbol elem)) 80 (cond ((null alist) (set alist-symbol elem))
79 ((anot-head-p alist key) (set alist-symbol (nconc elem alist))) 81 ((anot-head-p alist key) (set alist-symbol (nconc elem alist)))
80 (value (setcar alist (car elem))) 82 (value (setcar alist (car elem)))
84 (defun adelete (alist-symbol key) 86 (defun adelete (alist-symbol key)
85 "Delete a key-value pair from the alist. 87 "Delete a key-value pair from the alist.
86 Alist is referenced by ALIST-SYMBOL and the key-value pair to remove 88 Alist is referenced by ALIST-SYMBOL and the key-value pair to remove
87 is pair matching KEY. Returns the altered alist." 89 is pair matching KEY. Returns the altered alist."
88 (asort alist-symbol key) 90 (asort alist-symbol key)
89 (let ((alist (eval alist-symbol))) 91 (lexical-let ((alist (eval alist-symbol)))
90 (cond ((null alist) nil) 92 (cond ((null alist) nil)
91 ((anot-head-p alist key) alist) 93 ((anot-head-p alist key) alist)
92 (t (set alist-symbol (cdr alist)))))) 94 (t (set alist-symbol (cdr alist))))))
93 95
94 96
121 123
122 KEYLIST and VALUELIST should have the same number of elements, but 124 KEYLIST and VALUELIST should have the same number of elements, but
123 this isn't enforced. If VALUELIST is smaller than KEYLIST, remaining 125 this isn't enforced. If VALUELIST is smaller than KEYLIST, remaining
124 keys are associated with nil. If VALUELIST is larger than KEYLIST, 126 keys are associated with nil. If VALUELIST is larger than KEYLIST,
125 extra values are ignored. Returns the created alist." 127 extra values are ignored. Returns the created alist."
126 (let ((keycar (car keylist)) 128 (lexical-let ((keycar (car keylist))
127 (keycdr (cdr keylist)) 129 (keycdr (cdr keylist))
128 (valcar (car valuelist)) 130 (valcar (car valuelist))
129 (valcdr (cdr valuelist))) 131 (valcdr (cdr valuelist)))
130 (cond ((null keycdr) 132 (cond ((null keycdr)
131 (aput alist-symbol keycar valcar)) 133 (aput alist-symbol keycar valcar))
132 (t 134 (t
133 (amake alist-symbol keycdr valcdr) 135 (amake alist-symbol keycdr valcdr)
134 (aput alist-symbol keycar valcar)))) 136 (aput alist-symbol keycar valcar))))