]> code.delx.au - gnu-emacs/blobdiff - lisp/emacs-lisp/assoc.el
Merge from emacs-23; up to 2010-06-12T08:59:37Z!albinus@detlef.
[gnu-emacs] / lisp / emacs-lisp / assoc.el
index e68ac56f62209287ed287775c879ea533c8a7957..31be851f2dd5ad5e9cda5899cb0ab29343b77a98 100644 (file)
@@ -1,17 +1,16 @@
-;;; 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 Free Software Foundation, Inc.
+;; Copyright (C) 1996, 2001-2011  Free Software Foundation, Inc.
 
 ;; Author: Barry A. Warsaw <bwarsaw@cen.com>
 ;; Keywords: extensions
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 3, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -19,9 +18,7 @@
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
 ;; 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.
-The alist is referenced by ALIST-SYMBOL. Key-value pair to move to
+The alist is referenced by ALIST-SYMBOL.  Key-value pair to move to
 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)
-  "Makes 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."
+  "Make a list of a cons cell containing car of KEY and cdr of VALUE.
+The returned list is suitable for concatenating with an existing
+alist, via `nconc'."
   (list (cons key value)))
 
 
@@ -63,20 +62,20 @@ 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.
-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
+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.
 
 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.
 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
+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)))
@@ -88,21 +87,21 @@ 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))))))
 
 
 (defun aget (alist key &optional keynil-p)
-  "Returns the value in ALIST that is associated with KEY.
+  "Return the value in ALIST that is associated with KEY.
 Optional KEYNIL-P describes what to do if the value associated with
 KEY is nil.  If KEYNIL-P is not supplied or is nil, and the value is
 nil, then KEY is returned.  If KEYNIL-P is non-nil, then nil would be
 returned.
 
 If no key-value pair matching KEY could be found in ALIST, or ALIST is
-nil then nil is returned. ALIST is not altered."
+nil then nil is returned.  ALIST is not altered."
   (let ((copy (copy-alist alist)))
     (cond ((null alist) nil)
          ((progn (asort 'copy key)
@@ -116,8 +115,8 @@ nil then nil is returned. ALIST is not altered."
 (defun amake (alist-symbol keylist &optional valuelist)
   "Make an association list.
 The association list is attached to the alist referenced by
-ALIST-SYMBOL. Each element in the KEYLIST becomes a key and is
-associated with the value in VALUELIST with the same index. If
+ALIST-SYMBOL.  Each element in the KEYLIST becomes a key and is
+associated with the value in VALUELIST with the same index.  If
 VALUELIST is not supplied or is nil, then each key in KEYLIST is
 associated with nil.
 
@@ -125,18 +124,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