Replace version 24.2 with 24.3 where appropriate (hopefully)
[bpt/emacs.git] / lisp / obsolete / assoc.el
CommitLineData
e95a67dc 1;;; assoc.el --- insert/delete functions on association lists -*- lexical-binding: t -*-
cdccfc0d 2
acaf905b 3;; Copyright (C) 1996, 2001-2012 Free Software Foundation, Inc.
b578f267 4
cdccfc0d
ER
5;; Author: Barry A. Warsaw <bwarsaw@cen.com>
6;; Keywords: extensions
2a1e2476 7;; Obsolete-since: 24.3
cdccfc0d 8
b578f267
EN
9;; This file is part of GNU Emacs.
10
d6cba7ae 11;; GNU Emacs is free software: you can redistribute it and/or modify
b578f267 12;; it under the terms of the GNU General Public License as published by
d6cba7ae
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
b578f267
EN
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
d6cba7ae 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
cdccfc0d
ER
23
24;;; Commentary:
25
26;; Association list utilities providing insertion, deletion, sorting
27;; fetching off key-value pairs in association lists.
28
29;;; Code:
064eee03 30(eval-when-compile (require 'cl))
cdccfc0d 31
6476a5fa 32(defun asort (alist-symbol key)
cdccfc0d 33 "Move a specified key-value pair to the head of an alist.
6cda144f 34The alist is referenced by ALIST-SYMBOL. Key-value pair to move to
cdccfc0d
ER
35head is one matching KEY. Returns the sorted list and doesn't affect
36the order of any other key-value pair. Side effect sets alist to new
37sorted list."
38 (set alist-symbol
e573299d 39 (sort (copy-alist (symbol-value alist-symbol))
e95a67dc 40 (lambda (a _b) (equal (car a) key)))))
cdccfc0d
ER
41
42
43(defun aelement (key value)
6cda144f 44 "Make a list of a cons cell containing car of KEY and cdr of VALUE.
ac59f99d 45The returned list is suitable for concatenating with an existing
245e0faf 46alist, via `nconc'."
cdccfc0d
ER
47 (list (cons key value)))
48
49
50(defun aheadsym (alist)
51 "Return the key symbol at the head of ALIST."
52 (car (car alist)))
53
54
55(defun anot-head-p (alist key)
56 "Find out if a specified key-value pair is not at the head of an alist.
57The alist to check is specified by ALIST and the key-value pair is the
58one matching the supplied KEY. Returns nil if ALIST is nil, or if
59key-value pair is at the head of the alist. Returns t if key-value
60pair is not at the head of alist. ALIST is not altered."
61 (not (equal (aheadsym alist) key)))
62
63
64(defun aput (alist-symbol key &optional value)
e65adfac 65 "Insert a key-value pair into an alist.
6cda144f 66The alist is referenced by ALIST-SYMBOL. The key-value pair is made
e65adfac 67from KEY and optionally, VALUE. Returns the altered alist.
cdccfc0d
ER
68
69If the key-value pair referenced by KEY can be found in the alist, and
70VALUE is supplied non-nil, then the value of KEY will be set to VALUE.
71If VALUE is not supplied, or is nil, the key-value pair will not be
6cda144f 72modified, but will be moved to the head of the alist. If the key-value
cdccfc0d
ER
73pair cannot be found in the alist, it will be inserted into the head
74of the alist (with value nil if VALUE is nil or not supplied)."
e95a67dc
SM
75 (let ((elem (aelement key value))
76 alist)
cdccfc0d 77 (asort alist-symbol key)
e573299d 78 (setq alist (symbol-value alist-symbol))
cdccfc0d
ER
79 (cond ((null alist) (set alist-symbol elem))
80 ((anot-head-p alist key) (set alist-symbol (nconc elem alist)))
e65adfac 81 (value (setcar alist (car elem)) alist)
cdccfc0d
ER
82 (t alist))))
83
84
85(defun adelete (alist-symbol key)
86 "Delete a key-value pair from the alist.
87Alist is referenced by ALIST-SYMBOL and the key-value pair to remove
88is pair matching KEY. Returns the altered alist."
89 (asort alist-symbol key)
e95a67dc 90 (let ((alist (symbol-value alist-symbol)))
cdccfc0d
ER
91 (cond ((null alist) nil)
92 ((anot-head-p alist key) alist)
93 (t (set alist-symbol (cdr alist))))))
94
95
96(defun aget (alist key &optional keynil-p)
6cda144f 97 "Return the value in ALIST that is associated with KEY.
cdccfc0d
ER
98Optional KEYNIL-P describes what to do if the value associated with
99KEY is nil. If KEYNIL-P is not supplied or is nil, and the value is
100nil, then KEY is returned. If KEYNIL-P is non-nil, then nil would be
101returned.
102
103If no key-value pair matching KEY could be found in ALIST, or ALIST is
6cda144f 104nil then nil is returned. ALIST is not altered."
657c21e4 105 (defvar copy)
cdccfc0d
ER
106 (let ((copy (copy-alist alist)))
107 (cond ((null alist) nil)
108 ((progn (asort 'copy key)
109 (anot-head-p copy key)) nil)
110 ((cdr (car copy)))
111 (keynil-p nil)
112 ((car (car copy)))
113 (t nil))))
114
115
116(defun amake (alist-symbol keylist &optional valuelist)
117 "Make an association list.
118The association list is attached to the alist referenced by
6cda144f
JB
119ALIST-SYMBOL. Each element in the KEYLIST becomes a key and is
120associated with the value in VALUELIST with the same index. If
cdccfc0d
ER
121VALUELIST is not supplied or is nil, then each key in KEYLIST is
122associated with nil.
123
124KEYLIST and VALUELIST should have the same number of elements, but
125this isn't enforced. If VALUELIST is smaller than KEYLIST, remaining
126keys are associated with nil. If VALUELIST is larger than KEYLIST,
127extra values are ignored. Returns the created alist."
e95a67dc
SM
128 (let ((keycar (car keylist))
129 (keycdr (cdr keylist))
130 (valcar (car valuelist))
131 (valcdr (cdr valuelist)))
cdccfc0d
ER
132 (cond ((null keycdr)
133 (aput alist-symbol keycar valcar))
134 (t
135 (amake alist-symbol keycdr valcdr)
136 (aput alist-symbol keycar valcar))))
e573299d 137 (symbol-value alist-symbol))
cdccfc0d
ER
138
139(provide 'assoc)
140
141;;; assoc.el ends here