1993-04-23 03:54:38 +00:00
|
|
|
;;; assoc.el --- insert/delete/sort functions on association lists
|
|
|
|
|
2006-12-07 05:06:17 +00:00
|
|
|
;; Copyright (C) 1996, 2001, 2002, 2003, 2004, 2005,
|
2009-01-05 03:18:22 +00:00
|
|
|
;; 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
|
1996-01-14 07:34:30 +00:00
|
|
|
|
1993-04-23 03:54:38 +00:00
|
|
|
;; Author: Barry A. Warsaw <bwarsaw@cen.com>
|
|
|
|
;; Keywords: extensions
|
|
|
|
|
1996-01-14 07:34:30 +00:00
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
2008-05-06 03:21:21 +00:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
1996-01-14 07:34:30 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2008-05-06 03:21:21 +00:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
1996-01-14 07:34:30 +00:00
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
2008-05-06 03:21:21 +00:00
|
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
1993-04-23 03:54:38 +00:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Association list utilities providing insertion, deletion, sorting
|
|
|
|
;; fetching off key-value pairs in association lists.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
1993-07-15 05:08:23 +00:00
|
|
|
(defun asort (alist-symbol key)
|
1993-04-23 03:54:38 +00:00
|
|
|
"Move a specified key-value pair to the head of an alist.
|
Fix typos in docstrings.
* cus-edit.el (customize-apropos-options, custom-comment)
(custom-comment-tag, custom-face-edit-attribute-tag):
Fix typos in docstrings.
(custom-buffer-done-kill): Remove * from defcustom docstring.
(custom-file): Fix typo in doc of defcustom choice.
* frame.el (display-visual-class): Fix typo in docstring.
(initial-frame-alist, minibuffer-frame-alist, pop-up-frame-alist)
(special-display-frame-alist, show-trailing-whitespace)
(auto-hscroll-mode, blink-cursor-delay, blink-cursor-interval)
(display-hourglass, hourglass-delay, cursor-in-non-selected-windows):
Remove * from defcustom docstrings.
* md4.el (md4-buffer): Fix typo in docstring.
(md4, md4-64): Doc fixes.
(md4-pack-int32): Reflow docstring.
* paths.el (rmail-file-name): Remove * from defcustom docstring.
(prune-directory-list, gnus-nntp-service): Fix typos in docstrings.
* rect.el (open-rectangle): Reflow docstring.
(spaces-string): Fix docstring typo. Use "?\s" instead of "? ".
* select.el (x-get-cut-buffer): Fix typo in docstring.
* timezone.el (timezone-zone-to-minute, timezone-time-from-absolute)
(timezone-time-zone-from-absolute, timezone-leap-year-p):
Fix typos in docstrings.
* emacs-lisp/assoc.el (asort, aelement, aput, aget, amake):
Fix typos in docstrings.
2008-11-01 01:24:33 +00:00
|
|
|
The alist is referenced by ALIST-SYMBOL. Key-value pair to move to
|
1993-04-23 03:54:38 +00:00
|
|
|
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))
|
|
|
|
(function (lambda (a b) (equal (car a) key))))))
|
|
|
|
|
|
|
|
|
|
|
|
(defun aelement (key value)
|
Fix typos in docstrings.
* cus-edit.el (customize-apropos-options, custom-comment)
(custom-comment-tag, custom-face-edit-attribute-tag):
Fix typos in docstrings.
(custom-buffer-done-kill): Remove * from defcustom docstring.
(custom-file): Fix typo in doc of defcustom choice.
* frame.el (display-visual-class): Fix typo in docstring.
(initial-frame-alist, minibuffer-frame-alist, pop-up-frame-alist)
(special-display-frame-alist, show-trailing-whitespace)
(auto-hscroll-mode, blink-cursor-delay, blink-cursor-interval)
(display-hourglass, hourglass-delay, cursor-in-non-selected-windows):
Remove * from defcustom docstrings.
* md4.el (md4-buffer): Fix typo in docstring.
(md4, md4-64): Doc fixes.
(md4-pack-int32): Reflow docstring.
* paths.el (rmail-file-name): Remove * from defcustom docstring.
(prune-directory-list, gnus-nntp-service): Fix typos in docstrings.
* rect.el (open-rectangle): Reflow docstring.
(spaces-string): Fix docstring typo. Use "?\s" instead of "? ".
* select.el (x-get-cut-buffer): Fix typo in docstring.
* timezone.el (timezone-zone-to-minute, timezone-time-from-absolute)
(timezone-time-zone-from-absolute, timezone-leap-year-p):
Fix typos in docstrings.
* emacs-lisp/assoc.el (asort, aelement, aput, aget, amake):
Fix typos in docstrings.
2008-11-01 01:24:33 +00:00
|
|
|
"Make a list of a cons cell containing car of KEY and cdr of VALUE.
|
1993-04-23 03:54:38 +00:00
|
|
|
The returned list is suitable as an element of an alist."
|
|
|
|
(list (cons key value)))
|
|
|
|
|
|
|
|
|
|
|
|
(defun aheadsym (alist)
|
|
|
|
"Return the key symbol at the head of ALIST."
|
|
|
|
(car (car alist)))
|
|
|
|
|
|
|
|
|
|
|
|
(defun anot-head-p (alist key)
|
|
|
|
"Find out if a specified key-value pair is not at the head of an alist.
|
|
|
|
The alist to check is specified by ALIST and the key-value pair is the
|
|
|
|
one matching the supplied KEY. Returns nil if ALIST is nil, or if
|
|
|
|
key-value pair is at the head of the alist. Returns t if key-value
|
|
|
|
pair is not at the head of alist. ALIST is not altered."
|
|
|
|
(not (equal (aheadsym alist) key)))
|
|
|
|
|
|
|
|
|
|
|
|
(defun aput (alist-symbol key &optional value)
|
|
|
|
"Inserts a key-value pair into an alist.
|
Fix typos in docstrings.
* cus-edit.el (customize-apropos-options, custom-comment)
(custom-comment-tag, custom-face-edit-attribute-tag):
Fix typos in docstrings.
(custom-buffer-done-kill): Remove * from defcustom docstring.
(custom-file): Fix typo in doc of defcustom choice.
* frame.el (display-visual-class): Fix typo in docstring.
(initial-frame-alist, minibuffer-frame-alist, pop-up-frame-alist)
(special-display-frame-alist, show-trailing-whitespace)
(auto-hscroll-mode, blink-cursor-delay, blink-cursor-interval)
(display-hourglass, hourglass-delay, cursor-in-non-selected-windows):
Remove * from defcustom docstrings.
* md4.el (md4-buffer): Fix typo in docstring.
(md4, md4-64): Doc fixes.
(md4-pack-int32): Reflow docstring.
* paths.el (rmail-file-name): Remove * from defcustom docstring.
(prune-directory-list, gnus-nntp-service): Fix typos in docstrings.
* rect.el (open-rectangle): Reflow docstring.
(spaces-string): Fix docstring typo. Use "?\s" instead of "? ".
* select.el (x-get-cut-buffer): Fix typo in docstring.
* timezone.el (timezone-zone-to-minute, timezone-time-from-absolute)
(timezone-time-zone-from-absolute, timezone-leap-year-p):
Fix typos in docstrings.
* emacs-lisp/assoc.el (asort, aelement, aput, aget, amake):
Fix typos in docstrings.
2008-11-01 01:24:33 +00:00
|
|
|
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
|
1993-04-23 03:54:38 +00:00
|
|
|
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
|
Fix typos in docstrings.
* cus-edit.el (customize-apropos-options, custom-comment)
(custom-comment-tag, custom-face-edit-attribute-tag):
Fix typos in docstrings.
(custom-buffer-done-kill): Remove * from defcustom docstring.
(custom-file): Fix typo in doc of defcustom choice.
* frame.el (display-visual-class): Fix typo in docstring.
(initial-frame-alist, minibuffer-frame-alist, pop-up-frame-alist)
(special-display-frame-alist, show-trailing-whitespace)
(auto-hscroll-mode, blink-cursor-delay, blink-cursor-interval)
(display-hourglass, hourglass-delay, cursor-in-non-selected-windows):
Remove * from defcustom docstrings.
* md4.el (md4-buffer): Fix typo in docstring.
(md4, md4-64): Doc fixes.
(md4-pack-int32): Reflow docstring.
* paths.el (rmail-file-name): Remove * from defcustom docstring.
(prune-directory-list, gnus-nntp-service): Fix typos in docstrings.
* rect.el (open-rectangle): Reflow docstring.
(spaces-string): Fix docstring typo. Use "?\s" instead of "? ".
* select.el (x-get-cut-buffer): Fix typo in docstring.
* timezone.el (timezone-zone-to-minute, timezone-time-from-absolute)
(timezone-time-zone-from-absolute, timezone-leap-year-p):
Fix typos in docstrings.
* emacs-lisp/assoc.el (asort, aelement, aput, aget, amake):
Fix typos in docstrings.
2008-11-01 01:24:33 +00:00
|
|
|
modified, but will be moved to the head of the alist. If the key-value
|
1993-04-23 03:54:38 +00:00
|
|
|
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)
|
|
|
|
(asort alist-symbol key)
|
|
|
|
(setq alist (eval 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)))
|
|
|
|
(t alist))))
|
|
|
|
|
|
|
|
|
|
|
|
(defun adelete (alist-symbol key)
|
|
|
|
"Delete a key-value pair from the alist.
|
|
|
|
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)))
|
|
|
|
(cond ((null alist) nil)
|
|
|
|
((anot-head-p alist key) alist)
|
|
|
|
(t (set alist-symbol (cdr alist))))))
|
|
|
|
|
|
|
|
|
|
|
|
(defun aget (alist key &optional keynil-p)
|
Fix typos in docstrings.
* cus-edit.el (customize-apropos-options, custom-comment)
(custom-comment-tag, custom-face-edit-attribute-tag):
Fix typos in docstrings.
(custom-buffer-done-kill): Remove * from defcustom docstring.
(custom-file): Fix typo in doc of defcustom choice.
* frame.el (display-visual-class): Fix typo in docstring.
(initial-frame-alist, minibuffer-frame-alist, pop-up-frame-alist)
(special-display-frame-alist, show-trailing-whitespace)
(auto-hscroll-mode, blink-cursor-delay, blink-cursor-interval)
(display-hourglass, hourglass-delay, cursor-in-non-selected-windows):
Remove * from defcustom docstrings.
* md4.el (md4-buffer): Fix typo in docstring.
(md4, md4-64): Doc fixes.
(md4-pack-int32): Reflow docstring.
* paths.el (rmail-file-name): Remove * from defcustom docstring.
(prune-directory-list, gnus-nntp-service): Fix typos in docstrings.
* rect.el (open-rectangle): Reflow docstring.
(spaces-string): Fix docstring typo. Use "?\s" instead of "? ".
* select.el (x-get-cut-buffer): Fix typo in docstring.
* timezone.el (timezone-zone-to-minute, timezone-time-from-absolute)
(timezone-time-zone-from-absolute, timezone-leap-year-p):
Fix typos in docstrings.
* emacs-lisp/assoc.el (asort, aelement, aput, aget, amake):
Fix typos in docstrings.
2008-11-01 01:24:33 +00:00
|
|
|
"Return the value in ALIST that is associated with KEY.
|
1993-04-23 03:54:38 +00:00
|
|
|
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
|
Fix typos in docstrings.
* cus-edit.el (customize-apropos-options, custom-comment)
(custom-comment-tag, custom-face-edit-attribute-tag):
Fix typos in docstrings.
(custom-buffer-done-kill): Remove * from defcustom docstring.
(custom-file): Fix typo in doc of defcustom choice.
* frame.el (display-visual-class): Fix typo in docstring.
(initial-frame-alist, minibuffer-frame-alist, pop-up-frame-alist)
(special-display-frame-alist, show-trailing-whitespace)
(auto-hscroll-mode, blink-cursor-delay, blink-cursor-interval)
(display-hourglass, hourglass-delay, cursor-in-non-selected-windows):
Remove * from defcustom docstrings.
* md4.el (md4-buffer): Fix typo in docstring.
(md4, md4-64): Doc fixes.
(md4-pack-int32): Reflow docstring.
* paths.el (rmail-file-name): Remove * from defcustom docstring.
(prune-directory-list, gnus-nntp-service): Fix typos in docstrings.
* rect.el (open-rectangle): Reflow docstring.
(spaces-string): Fix docstring typo. Use "?\s" instead of "? ".
* select.el (x-get-cut-buffer): Fix typo in docstring.
* timezone.el (timezone-zone-to-minute, timezone-time-from-absolute)
(timezone-time-zone-from-absolute, timezone-leap-year-p):
Fix typos in docstrings.
* emacs-lisp/assoc.el (asort, aelement, aput, aget, amake):
Fix typos in docstrings.
2008-11-01 01:24:33 +00:00
|
|
|
nil then nil is returned. ALIST is not altered."
|
1993-04-23 03:54:38 +00:00
|
|
|
(let ((copy (copy-alist alist)))
|
|
|
|
(cond ((null alist) nil)
|
|
|
|
((progn (asort 'copy key)
|
|
|
|
(anot-head-p copy key)) nil)
|
|
|
|
((cdr (car copy)))
|
|
|
|
(keynil-p nil)
|
|
|
|
((car (car copy)))
|
|
|
|
(t nil))))
|
|
|
|
|
|
|
|
|
|
|
|
(defun amake (alist-symbol keylist &optional valuelist)
|
|
|
|
"Make an association list.
|
|
|
|
The association list is attached to the alist referenced by
|
Fix typos in docstrings.
* cus-edit.el (customize-apropos-options, custom-comment)
(custom-comment-tag, custom-face-edit-attribute-tag):
Fix typos in docstrings.
(custom-buffer-done-kill): Remove * from defcustom docstring.
(custom-file): Fix typo in doc of defcustom choice.
* frame.el (display-visual-class): Fix typo in docstring.
(initial-frame-alist, minibuffer-frame-alist, pop-up-frame-alist)
(special-display-frame-alist, show-trailing-whitespace)
(auto-hscroll-mode, blink-cursor-delay, blink-cursor-interval)
(display-hourglass, hourglass-delay, cursor-in-non-selected-windows):
Remove * from defcustom docstrings.
* md4.el (md4-buffer): Fix typo in docstring.
(md4, md4-64): Doc fixes.
(md4-pack-int32): Reflow docstring.
* paths.el (rmail-file-name): Remove * from defcustom docstring.
(prune-directory-list, gnus-nntp-service): Fix typos in docstrings.
* rect.el (open-rectangle): Reflow docstring.
(spaces-string): Fix docstring typo. Use "?\s" instead of "? ".
* select.el (x-get-cut-buffer): Fix typo in docstring.
* timezone.el (timezone-zone-to-minute, timezone-time-from-absolute)
(timezone-time-zone-from-absolute, timezone-leap-year-p):
Fix typos in docstrings.
* emacs-lisp/assoc.el (asort, aelement, aput, aget, amake):
Fix typos in docstrings.
2008-11-01 01:24:33 +00:00
|
|
|
ALIST-SYMBOL. Each element in the KEYLIST becomes a key and is
|
|
|
|
associated with the value in VALUELIST with the same index. If
|
1993-04-23 03:54:38 +00:00
|
|
|
VALUELIST is not supplied or is nil, then each key in KEYLIST is
|
|
|
|
associated with nil.
|
|
|
|
|
|
|
|
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)))
|
|
|
|
(cond ((null keycdr)
|
|
|
|
(aput alist-symbol keycar valcar))
|
|
|
|
(t
|
|
|
|
(amake alist-symbol keycdr valcdr)
|
|
|
|
(aput alist-symbol keycar valcar))))
|
|
|
|
(eval alist-symbol))
|
|
|
|
|
|
|
|
(provide 'assoc)
|
|
|
|
|
2008-04-10 14:10:46 +00:00
|
|
|
;; arch-tag: 3e58bd89-d912-4b74-a0dc-6ed9735922bc
|
1993-04-23 03:54:38 +00:00
|
|
|
;;; assoc.el ends here
|