annotate lisp/emacs-lisp/assoc.el @ 9428:3d5070024b68

(tcl-mode): imenu-create-index-function made buffer local.
author Tom Tromey <tromey@redhat.com>
date Tue, 11 Oct 1994 02:01:27 +0000
parents 0a02227f8417
children 83f275dcd93a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2567
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1 ;;; assoc.el --- insert/delete/sort functions on association lists
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
2
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
3 ;; Author: Barry A. Warsaw <bwarsaw@cen.com>
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
4 ;; Keywords: extensions
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
5
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
6 ;; This software is distributed in the hope that it will be useful,
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
7 ;; but WITHOUT ANY WARRANTY. No author or distributor accepts
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
8 ;; responsibility to anyone for the consequences of using it or for
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
9 ;; whether it serves any particular purpose or works at all, unless he
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
10 ;; says so in writing.
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
11
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
12 ;; This software was written as part of the supercite author's
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
13 ;; official duty as an employee of the United States Government and is
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
14 ;; thus in the public domain. You are free to use that particular
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
15 ;; software as you wish, but WITHOUT ANY WARRANTY WHATSOEVER. It
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
16 ;; would be nice, though if when you use any of this code, you give
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
17 ;; due credit to the author.
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
18
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
19 ;;; Commentary:
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
20
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
21 ;; Association list utilities providing insertion, deletion, sorting
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
22 ;; fetching off key-value pairs in association lists.
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
23
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
24 ;;; Code:
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
25
4098
0a02227f8417 * assoc.el (asort): First argument should be named alist-symbol,
Jim Blandy <jimb@redhat.com>
parents: 3768
diff changeset
26 (defun asort (alist-symbol key)
2567
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
27 "Move a specified key-value pair to the head of an alist.
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
28 The alist is referenced by ALIST-SYMBOL. Key-value pair to move to
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
29 head is one matching KEY. Returns the sorted list and doesn't affect
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
30 the order of any other key-value pair. Side effect sets alist to new
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
31 sorted list."
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
32 (set alist-symbol
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
33 (sort (copy-alist (eval alist-symbol))
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
34 (function (lambda (a b) (equal (car a) key))))))
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
35
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
36
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
37 (defun aelement (key value)
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
38 "Makes a list of a cons cell containing car of KEY and cdr of VALUE.
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
39 The returned list is suitable as an element of an alist."
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
40 (list (cons key value)))
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
41
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
42
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
43 (defun aheadsym (alist)
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
44 "Return the key symbol at the head of ALIST."
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
45 (car (car alist)))
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
46
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
47
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
48 (defun anot-head-p (alist key)
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
49 "Find out if a specified key-value pair is not at the head of an alist.
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
50 The alist to check is specified by ALIST and the key-value pair is the
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
51 one matching the supplied KEY. Returns nil if ALIST is nil, or if
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
52 key-value pair is at the head of the alist. Returns t if key-value
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
53 pair is not at the head of alist. ALIST is not altered."
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
54 (not (equal (aheadsym alist) key)))
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
55
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
56
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
57 (defun aput (alist-symbol key &optional value)
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
58 "Inserts a key-value pair into an alist.
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
59 The alist is referenced by ALIST-SYMBOL. The key-value pair is made
3768
d3fd7bcefd84 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 2567
diff changeset
60 from KEY and optionally, VALUE. Returns the altered alist or nil if
2567
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
61 ALIST is nil.
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
62
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
63 If the key-value pair referenced by KEY can be found in the alist, and
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
64 VALUE is supplied non-nil, then the value of KEY will be set to VALUE.
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
65 If VALUE is not supplied, or is nil, the key-value pair will not be
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
66 modified, but will be moved to the head of the alist. If the key-value
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
67 pair cannot be found in the alist, it will be inserted into the head
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
68 of the alist (with value nil if VALUE is nil or not supplied)."
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
69 (let ((elem (aelement key value))
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
70 alist)
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
71 (asort alist-symbol key)
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
72 (setq alist (eval alist-symbol))
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
73 (cond ((null alist) (set alist-symbol elem))
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
74 ((anot-head-p alist key) (set alist-symbol (nconc elem alist)))
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
75 (value (setcar alist (car elem)))
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
76 (t alist))))
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
77
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
78
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
79 (defun adelete (alist-symbol key)
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
80 "Delete a key-value pair from the alist.
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
81 Alist is referenced by ALIST-SYMBOL and the key-value pair to remove
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
82 is pair matching KEY. Returns the altered alist."
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
83 (asort alist-symbol key)
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
84 (let ((alist (eval alist-symbol)))
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
85 (cond ((null alist) nil)
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
86 ((anot-head-p alist key) alist)
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
87 (t (set alist-symbol (cdr alist))))))
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
88
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
89
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
90 (defun aget (alist key &optional keynil-p)
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
91 "Returns the value in ALIST that is associated with KEY.
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
92 Optional KEYNIL-P describes what to do if the value associated with
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
93 KEY is nil. If KEYNIL-P is not supplied or is nil, and the value is
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
94 nil, then KEY is returned. If KEYNIL-P is non-nil, then nil would be
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
95 returned.
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
96
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
97 If no key-value pair matching KEY could be found in ALIST, or ALIST is
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
98 nil then nil is returned. ALIST is not altered."
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
99 (let ((copy (copy-alist alist)))
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
100 (cond ((null alist) nil)
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
101 ((progn (asort 'copy key)
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
102 (anot-head-p copy key)) nil)
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
103 ((cdr (car copy)))
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
104 (keynil-p nil)
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
105 ((car (car copy)))
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
106 (t nil))))
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
107
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
108
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
109 (defun amake (alist-symbol keylist &optional valuelist)
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
110 "Make an association list.
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
111 The association list is attached to the alist referenced by
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
112 ALIST-SYMBOL. Each element in the KEYLIST becomes a key and is
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
113 associated with the value in VALUELIST with the same index. If
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
114 VALUELIST is not supplied or is nil, then each key in KEYLIST is
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
115 associated with nil.
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
116
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
117 KEYLIST and VALUELIST should have the same number of elements, but
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
118 this isn't enforced. If VALUELIST is smaller than KEYLIST, remaining
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
119 keys are associated with nil. If VALUELIST is larger than KEYLIST,
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
120 extra values are ignored. Returns the created alist."
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
121 (let ((keycar (car keylist))
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
122 (keycdr (cdr keylist))
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
123 (valcar (car valuelist))
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
124 (valcdr (cdr valuelist)))
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
125 (cond ((null keycdr)
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
126 (aput alist-symbol keycar valcar))
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
127 (t
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
128 (amake alist-symbol keycdr valcdr)
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
129 (aput alist-symbol keycar valcar))))
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
130 (eval alist-symbol))
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
131
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
132 (provide 'assoc)
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
133
2cd6cb337d7c Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
134 ;;; assoc.el ends here