]> code.delx.au - gnu-emacs/blobdiff - lisp/emacs-lisp/assoc.el
Merge from trunk.
[gnu-emacs] / lisp / emacs-lisp / assoc.el
index 8082069a34b04a9397320844e8912141ac05054e..264374ed7216d9465851de0d452a4ebc71cc7281 100644 (file)
@@ -1,7 +1,6 @@
-;;; assoc.el --- insert/delete/sort functions on association lists
+;;; assoc.el --- insert/delete functions on association lists
 
-;; Copyright (C) 1996, 2001, 2002, 2003, 2004, 2005,
-;;   2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
+;; Copyright (C) 1996, 2001-2012  Free Software Foundation, Inc.
 
 ;; Author: Barry A. Warsaw <bwarsaw@cen.com>
 ;; Keywords: extensions
@@ -27,6 +26,7 @@
 ;; fetching off key-value pairs in association lists.
 
 ;;; Code:
+(eval-when-compile (require 'cl))
 
 (defun asort (alist-symbol key)
   "Move a specified key-value pair to the head of an alist.
@@ -35,13 +35,14 @@ head is one matching KEY.  Returns the sorted list and doesn't affect
 the order of any other key-value pair.  Side effect sets alist to new
 sorted list."
   (set alist-symbol
-       (sort (copy-alist (eval alist-symbol))
+       (sort (copy-alist (symbol-value alist-symbol))
             (function (lambda (a b) (equal (car a) key))))))
 
 
 (defun aelement (key value)
   "Make a list of a cons cell containing car of KEY and cdr of VALUE.
-The returned list is suitable as an element of an alist."
+The returned list is suitable for concatenating with an existing
+alist, via `nconc'."
   (list (cons key value)))
 
 
@@ -60,10 +61,9 @@ pair is not at the head of alist.  ALIST is not altered."
 
 
 (defun aput (alist-symbol key &optional value)
-  "Inserts a key-value pair into an alist.
+  "Insert a key-value pair into an alist.
 The alist is referenced by ALIST-SYMBOL.  The key-value pair is made
-from KEY and optionally, VALUE.  Returns the altered alist or nil if
-ALIST is nil.
+from KEY and optionally, VALUE.  Returns the altered alist.
 
 If the key-value pair referenced by KEY can be found in the alist, and
 VALUE is supplied non-nil, then the value of KEY will be set to VALUE.
@@ -71,13 +71,13 @@ If VALUE is not supplied, or is nil, the key-value pair will not be
 modified, but will be moved to the head of the alist.  If the key-value
 pair cannot be found in the alist, it will be inserted into the head
 of the alist (with value nil if VALUE is nil or not supplied)."
-  (let ((elem (aelement key value))
-       alist)
+  (lexical-let ((elem (aelement key value))
+               alist)
     (asort alist-symbol key)
-    (setq alist (eval alist-symbol))
+    (setq alist (symbol-value alist-symbol))
     (cond ((null alist) (set alist-symbol elem))
          ((anot-head-p alist key) (set alist-symbol (nconc elem alist)))
-         (value (setcar alist (car elem)))
+         (value (setcar alist (car elem)) alist)
          (t alist))))
 
 
@@ -86,7 +86,7 @@ of the alist (with value nil if VALUE is nil or not supplied)."
 Alist is referenced by ALIST-SYMBOL and the key-value pair to remove
 is pair matching KEY.  Returns the altered alist."
   (asort alist-symbol key)
-  (let ((alist (eval alist-symbol)))
+  (lexical-let ((alist (symbol-value alist-symbol)))
     (cond ((null alist) nil)
          ((anot-head-p alist key) alist)
          (t (set alist-symbol (cdr alist))))))
@@ -123,18 +123,17 @@ KEYLIST and VALUELIST should have the same number of elements, but
 this isn't enforced.  If VALUELIST is smaller than KEYLIST, remaining
 keys are associated with nil.  If VALUELIST is larger than KEYLIST,
 extra values are ignored.  Returns the created alist."
-  (let ((keycar (car keylist))
-       (keycdr (cdr keylist))
-       (valcar (car valuelist))
-       (valcdr (cdr valuelist)))
+  (lexical-let ((keycar (car keylist))
+               (keycdr (cdr keylist))
+               (valcar (car valuelist))
+               (valcdr (cdr valuelist)))
     (cond ((null keycdr)
           (aput alist-symbol keycar valcar))
          (t
           (amake alist-symbol keycdr valcdr)
           (aput alist-symbol keycar valcar))))
-  (eval alist-symbol))
+  (symbol-value alist-symbol))
 
 (provide 'assoc)
 
-;; arch-tag: 3e58bd89-d912-4b74-a0dc-6ed9735922bc
 ;;; assoc.el ends here