2007-09-06 04:25:08 +00:00
|
|
|
@c -*-texinfo-*-
|
|
|
|
@c This is part of the GNU Emacs Lisp Reference Manual.
|
2025-01-02 18:32:51 +01:00
|
|
|
@c Copyright (C) 1990--1995, 1998--1999, 2001--2025 Free Software
|
2013-01-01 09:11:05 +00:00
|
|
|
@c Foundation, Inc.
|
2007-09-06 04:25:08 +00:00
|
|
|
@c See the file elisp.texi for copying conditions.
|
2012-05-26 18:34:14 -07:00
|
|
|
@node Lists
|
2007-09-06 04:25:08 +00:00
|
|
|
@chapter Lists
|
|
|
|
@cindex lists
|
|
|
|
@cindex element (of list)
|
|
|
|
|
|
|
|
A @dfn{list} represents a sequence of zero or more elements (which may
|
|
|
|
be any Lisp objects). The important difference between lists and
|
|
|
|
vectors is that two or more lists can share part of their structure; in
|
|
|
|
addition, you can insert or delete elements in a list without copying
|
|
|
|
the whole list.
|
|
|
|
|
|
|
|
@menu
|
|
|
|
* Cons Cells:: How lists are made out of cons cells.
|
|
|
|
* List-related Predicates:: Is this object a list? Comparing two lists.
|
|
|
|
* List Elements:: Extracting the pieces of a list.
|
|
|
|
* Building Lists:: Creating list structure.
|
|
|
|
* List Variables:: Modifying lists stored in variables.
|
|
|
|
* Modifying Lists:: Storing new pieces into an existing list.
|
|
|
|
* Sets And Lists:: A list can represent a finite mathematical set.
|
|
|
|
* Association Lists:: A list can represent a finite relation or mapping.
|
2012-12-02 17:14:16 +08:00
|
|
|
* Property Lists:: A list of paired elements.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end menu
|
|
|
|
|
|
|
|
@node Cons Cells
|
|
|
|
@section Lists and Cons Cells
|
|
|
|
@cindex lists and cons cells
|
|
|
|
|
|
|
|
Lists in Lisp are not a primitive data type; they are built up from
|
2012-01-23 12:23:50 +08:00
|
|
|
@dfn{cons cells} (@pxref{Cons Cell Type}). A cons cell is a data
|
|
|
|
object that represents an ordered pair. That is, it has two slots,
|
|
|
|
and each slot @dfn{holds}, or @dfn{refers to}, some Lisp object. One
|
|
|
|
slot is known as the @sc{car}, and the other is known as the @sc{cdr}.
|
|
|
|
(These names are traditional; see @ref{Cons Cell Type}.) @sc{cdr} is
|
2012-04-25 20:31:47 -04:00
|
|
|
pronounced ``could-er''.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
We say that ``the @sc{car} of this cons cell is'' whatever object
|
|
|
|
its @sc{car} slot currently holds, and likewise for the @sc{cdr}.
|
|
|
|
|
2015-09-15 08:46:48 -07:00
|
|
|
A list is a series of cons cells chained together, so that each
|
2012-01-23 12:23:50 +08:00
|
|
|
cell refers to the next one. There is one cons cell for each element
|
|
|
|
of the list. By convention, the @sc{car}s of the cons cells hold the
|
|
|
|
elements of the list, and the @sc{cdr}s are used to chain the list
|
|
|
|
(this asymmetry between @sc{car} and @sc{cdr} is entirely a matter of
|
|
|
|
convention; at the level of cons cells, the @sc{car} and @sc{cdr}
|
|
|
|
slots have similar properties). Hence, the @sc{cdr} slot of each cons
|
|
|
|
cell in a list refers to the following cons cell.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2018-07-10 19:51:28 -07:00
|
|
|
@cindex proper list
|
2007-09-06 04:25:08 +00:00
|
|
|
@cindex true list
|
2012-01-23 12:23:50 +08:00
|
|
|
Also by convention, the @sc{cdr} of the last cons cell in a list is
|
|
|
|
@code{nil}. We call such a @code{nil}-terminated structure a
|
2018-07-10 19:51:28 -07:00
|
|
|
@dfn{proper list}@footnote{It is sometimes also referred to as a
|
|
|
|
@dfn{true list}, but we generally do not use this terminology in this
|
|
|
|
manual.}. In Emacs Lisp, the symbol @code{nil} is both a symbol and a
|
|
|
|
list with no elements. For convenience, the symbol @code{nil} is
|
|
|
|
considered to have @code{nil} as its @sc{cdr} (and also as its
|
|
|
|
@sc{car}).
|
|
|
|
|
|
|
|
Hence, the @sc{cdr} of a proper list is always a proper list. The
|
|
|
|
@sc{cdr} of a nonempty proper list is a proper list containing all the
|
2012-01-23 12:23:50 +08:00
|
|
|
elements except the first.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@cindex dotted list
|
|
|
|
@cindex circular list
|
2012-01-23 12:23:50 +08:00
|
|
|
If the @sc{cdr} of a list's last cons cell is some value other than
|
|
|
|
@code{nil}, we call the structure a @dfn{dotted list}, since its
|
|
|
|
printed representation would use dotted pair notation (@pxref{Dotted
|
|
|
|
Pair Notation}). There is one other possibility: some cons cell's
|
|
|
|
@sc{cdr} could point to one of the previous cons cells in the list.
|
|
|
|
We call that structure a @dfn{circular list}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2018-07-10 19:51:28 -07:00
|
|
|
For some purposes, it does not matter whether a list is proper,
|
2012-01-23 12:23:50 +08:00
|
|
|
circular or dotted. If a program doesn't look far enough down the
|
2007-09-06 04:25:08 +00:00
|
|
|
list to see the @sc{cdr} of the final cons cell, it won't care.
|
2018-07-10 19:51:28 -07:00
|
|
|
However, some functions that operate on lists demand proper lists and
|
2007-09-06 04:25:08 +00:00
|
|
|
signal errors if given a dotted list. Most functions that try to find
|
2023-02-05 15:15:35 +02:00
|
|
|
the end of a list enter infinite loops if given a circular list. You
|
|
|
|
can use the function @code{proper-list-p}, described in the next
|
|
|
|
section (@pxref{List-related Predicates, proper-list-p}), to determine
|
|
|
|
whether a list is a proper one.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@cindex list structure
|
2012-01-23 12:23:50 +08:00
|
|
|
Because most cons cells are used as part of lists, we refer to any
|
|
|
|
structure made out of cons cells as a @dfn{list structure}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@node List-related Predicates
|
|
|
|
@section Predicates on Lists
|
Improve indexing on the chapter/section/subsection levels.
doc/lispref/windows.texi (Recombining Windows): Index subject of sections.
doc/lispref/variables.texi (Variables with Restricted Values)
(Generalized Variables): Index subject of sections.
doc/lispref/text.texi (Buffer Contents, Examining Properties)
(Changing Properties, Property Search, Substitution): Index
subject of sections.
doc/lispref/syntax.texi (Motion and Syntax, Parsing Expressions)
(Motion via Parsing, Position Parse, Control Parsing): Index
subject of sections.
doc/lispref/strings.texi (Predicates for Strings, Creating Strings)
(Modifying Strings, Text Comparison): Index subject of sections.
doc/lispref/searching.texi (Syntax of Regexps, Regexp Special)
(Regexp Functions, Regexp Functions): Index subject of sections.
doc/lispref/processes.texi (Subprocess Creation, Process Information): Index
subject of sections.
doc/lispref/positions.texi (Screen Lines): Index subject of sections.
doc/lispref/nonascii.texi (Scanning Charsets, Specifying Coding Systems):
Index subject of sections.
doc/lispref/minibuf.texi (Text from Minibuffer, Object from Minibuffer)
(Multiple Queries, Minibuffer Contents): Index subject of
sections.
doc/lispref/markers.texi (Predicates on Markers, Creating Markers)
(Information from Markers, Moving Markers): Index subject of
sections.
doc/lispref/macros.texi (Defining Macros, Problems with Macros): Index
subject of sections.
doc/lispref/loading.texi (Loading Non-ASCII, Where Defined): Index subject
of sections.
doc/lispref/lists.texi (List-related Predicates, List Variables, Setcar)
(Setcdr, Plist Access): Index subject of sections.
doc/lispref/keymaps.texi (Controlling Active Maps, Scanning Keymaps)
(Modifying Menus): Index subject of sections.
doc/lispref/help.texi (Accessing Documentation, Help Functions): Index
subject of sections.
doc/lispref/hash.texi (Hash Access): Index subject of sections.
doc/lispref/functions.texi (Core Advising Primitives)
(Advising Named Functions, Porting old advices): Index subject of
sections.
doc/lispref/frames.texi (Creating Frames, Initial Parameters)
(Position Parameters, Buffer Parameters, Minibuffers and Frames)
(Pop-Up Menus, Drag and Drop): Index subject of sections.
doc/lispref/files.texi (Visiting Functions, Kinds of Files)
(Unique File Names): Index subject of sections.
doc/lispref/display.texi (Refresh Screen, Echo Area Customization)
(Warning Variables, Warning Options, Delayed Warnings)
(Temporary Displays, Managing Overlays, Overlay Properties)
(Finding Overlays, Size of Displayed Text, Defining Faces)
(Attribute Functions, Displaying Faces, Face Remapping)
(Basic Faces, Font Lookup, Fontsets, Replacing Specs)
(Defining Images, Showing Images): Index subject of sections.
doc/lispref/debugging.texi (Debugging, Explicit Debug)
(Invoking the Debugger, Excess Open, Excess Close): Index subject
of sections.
doc/lispref/customize.texi (Defining New Types, Applying Customizations)
(Custom Themes): Index subject of sections.
doc/lispref/control.texi (Sequencing, Combining Conditions)
(Processing of Errors, Cleanups): Index subject of sections.
doc/lispref/compile.texi (Eval During Compile): Index subject of sections.
doc/lispref/commands.texi (Using Interactive, Distinguish Interactive)
(Command Loop Info, Classifying Events, Event Mod)
(Invoking the Input Method): Index subject of sections.
doc/lispref/buffers.texi (Buffer List, Buffer Gap): Index subject of sections.
doc/lispref/backups.texi (Making Backups, Numbered Backups, Backup Names)
(Reverting): Index subject of sections.
doc/lispref/abbrevs.texi (Abbrev Tables, Defining Abbrevs, Abbrev Files)
(Abbrev Expansion, Standard Abbrev Tables, Abbrev Properties)
(Abbrev Table Properties): Index subject of sections.
doc/lispref/os.texi (Time of Day, Time Conversion, Time Parsing)
(Time Calculations, Idle Timers): Index subject of sections.
2014-12-23 20:42:30 +02:00
|
|
|
@cindex predicates for lists
|
|
|
|
@cindex list predicates
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
The following predicates test whether a Lisp object is an atom,
|
|
|
|
whether it is a cons cell or is a list, or whether it is the
|
|
|
|
distinguished object @code{nil}. (Many of these predicates can be
|
|
|
|
defined in terms of the others, but they are used so often that it is
|
2012-05-05 12:32:58 +08:00
|
|
|
worth having them.)
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@defun consp object
|
|
|
|
This function returns @code{t} if @var{object} is a cons cell, @code{nil}
|
|
|
|
otherwise. @code{nil} is not a cons cell, although it @emph{is} a list.
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun atom object
|
|
|
|
This function returns @code{t} if @var{object} is an atom, @code{nil}
|
|
|
|
otherwise. All objects except cons cells are atoms. The symbol
|
|
|
|
@code{nil} is an atom and is also a list; it is the only Lisp object
|
|
|
|
that is both.
|
|
|
|
|
|
|
|
@example
|
|
|
|
(atom @var{object}) @equiv{} (not (consp @var{object}))
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun listp object
|
|
|
|
This function returns @code{t} if @var{object} is a cons cell or
|
|
|
|
@code{nil}. Otherwise, it returns @code{nil}.
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(listp '(1))
|
|
|
|
@result{} t
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(listp '())
|
|
|
|
@result{} t
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun nlistp object
|
|
|
|
This function is the opposite of @code{listp}: it returns @code{t} if
|
|
|
|
@var{object} is not a list. Otherwise, it returns @code{nil}.
|
|
|
|
|
|
|
|
@example
|
|
|
|
(listp @var{object}) @equiv{} (not (nlistp @var{object}))
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun null object
|
|
|
|
This function returns @code{t} if @var{object} is @code{nil}, and
|
|
|
|
returns @code{nil} otherwise. This function is identical to @code{not},
|
2024-04-15 14:52:03 +03:00
|
|
|
but as a matter of clarity we use @code{not} when @var{object} is
|
|
|
|
considered a truth value (see @code{not} in @ref{Combining
|
|
|
|
Conditions}) and @code{null} otherwise.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(null '(1))
|
|
|
|
@result{} nil
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(null '())
|
|
|
|
@result{} t
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
2018-07-09 18:46:33 -07:00
|
|
|
@defun proper-list-p object
|
|
|
|
This function returns the length of @var{object} if it is a proper
|
|
|
|
list, @code{nil} otherwise (@pxref{Cons Cells}). In addition to
|
|
|
|
satisfying @code{listp}, a proper list is neither circular nor dotted.
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(proper-list-p '(a b c))
|
|
|
|
@result{} 3
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(proper-list-p '(a b . c))
|
|
|
|
@result{} nil
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
@end defun
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@node List Elements
|
|
|
|
@section Accessing Elements of Lists
|
|
|
|
@cindex list elements
|
|
|
|
|
|
|
|
@defun car cons-cell
|
|
|
|
This function returns the value referred to by the first slot of the
|
2009-02-22 00:42:28 +00:00
|
|
|
cons cell @var{cons-cell}. In other words, it returns the @sc{car} of
|
|
|
|
@var{cons-cell}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2009-02-22 00:42:28 +00:00
|
|
|
As a special case, if @var{cons-cell} is @code{nil}, this function
|
|
|
|
returns @code{nil}. Therefore, any list is a valid argument. An
|
|
|
|
error is signaled if the argument is not a cons cell or @code{nil}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(car '(a b c))
|
|
|
|
@result{} a
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(car '())
|
|
|
|
@result{} nil
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun cdr cons-cell
|
2009-02-22 00:42:28 +00:00
|
|
|
This function returns the value referred to by the second slot of the
|
|
|
|
cons cell @var{cons-cell}. In other words, it returns the @sc{cdr} of
|
|
|
|
@var{cons-cell}.
|
|
|
|
|
|
|
|
As a special case, if @var{cons-cell} is @code{nil}, this function
|
|
|
|
returns @code{nil}; therefore, any list is a valid argument. An error
|
|
|
|
is signaled if the argument is not a cons cell or @code{nil}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(cdr '(a b c))
|
|
|
|
@result{} (b c)
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(cdr '())
|
|
|
|
@result{} nil
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun car-safe object
|
|
|
|
This function lets you take the @sc{car} of a cons cell while avoiding
|
|
|
|
errors for other data types. It returns the @sc{car} of @var{object} if
|
|
|
|
@var{object} is a cons cell, @code{nil} otherwise. This is in contrast
|
|
|
|
to @code{car}, which signals an error if @var{object} is not a list.
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(car-safe @var{object})
|
|
|
|
@equiv{}
|
|
|
|
(let ((x @var{object}))
|
|
|
|
(if (consp x)
|
|
|
|
(car x)
|
|
|
|
nil))
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun cdr-safe object
|
|
|
|
This function lets you take the @sc{cdr} of a cons cell while
|
|
|
|
avoiding errors for other data types. It returns the @sc{cdr} of
|
|
|
|
@var{object} if @var{object} is a cons cell, @code{nil} otherwise.
|
|
|
|
This is in contrast to @code{cdr}, which signals an error if
|
|
|
|
@var{object} is not a list.
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(cdr-safe @var{object})
|
|
|
|
@equiv{}
|
|
|
|
(let ((x @var{object}))
|
|
|
|
(if (consp x)
|
|
|
|
(cdr x)
|
|
|
|
nil))
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defmac pop listname
|
2012-11-07 13:22:10 +08:00
|
|
|
This macro provides a convenient way to examine the @sc{car} of a
|
|
|
|
list, and take it off the list, all at once. It operates on the list
|
|
|
|
stored in @var{listname}. It removes the first element from the list,
|
|
|
|
saves the @sc{cdr} into @var{listname}, then returns the removed
|
|
|
|
element.
|
|
|
|
|
|
|
|
In the simplest case, @var{listname} is an unquoted symbol naming a
|
|
|
|
list; in that case, this macro is equivalent to @w{@code{(prog1
|
|
|
|
(car listname) (setq listname (cdr listname)))}}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@example
|
|
|
|
x
|
|
|
|
@result{} (a b c)
|
|
|
|
(pop x)
|
|
|
|
@result{} a
|
|
|
|
x
|
|
|
|
@result{} (b c)
|
|
|
|
@end example
|
2012-01-23 12:23:50 +08:00
|
|
|
|
2012-11-07 13:22:10 +08:00
|
|
|
More generally, @var{listname} can be a generalized variable. In that
|
|
|
|
case, this macro saves into @var{listname} using @code{setf}.
|
|
|
|
@xref{Generalized Variables}.
|
|
|
|
|
2012-10-27 15:07:43 -07:00
|
|
|
For the @code{push} macro, which adds an element to a list,
|
2012-01-23 12:23:50 +08:00
|
|
|
@xref{List Variables}.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defmac
|
|
|
|
|
|
|
|
@defun nth n list
|
|
|
|
@anchor{Definition of nth}
|
|
|
|
This function returns the @var{n}th element of @var{list}. Elements
|
|
|
|
are numbered starting with zero, so the @sc{car} of @var{list} is
|
|
|
|
element number zero. If the length of @var{list} is @var{n} or less,
|
|
|
|
the value is @code{nil}.
|
|
|
|
|
2013-08-13 00:15:27 -07:00
|
|
|
@c Behavior for -ve n undefined since 2013/08; see bug#15059.
|
|
|
|
@ignore
|
|
|
|
If @var{n} is negative, @code{nth} returns the first element of @var{list}.
|
|
|
|
@end ignore
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(nth 2 '(1 2 3 4))
|
|
|
|
@result{} 3
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(nth 10 '(1 2 3 4))
|
|
|
|
@result{} nil
|
|
|
|
|
|
|
|
(nth n x) @equiv{} (car (nthcdr n x))
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
The function @code{elt} is similar, but applies to any kind of sequence.
|
|
|
|
For historical reasons, it takes its arguments in the opposite order.
|
|
|
|
@xref{Sequence Functions}.
|
|
|
|
@end defun
|
|
|
|
|
2024-04-28 18:38:44 +02:00
|
|
|
@findex drop
|
2007-09-06 04:25:08 +00:00
|
|
|
@defun nthcdr n list
|
|
|
|
This function returns the @var{n}th @sc{cdr} of @var{list}. In other
|
|
|
|
words, it skips past the first @var{n} links of @var{list} and returns
|
|
|
|
what follows.
|
|
|
|
|
2013-08-13 00:15:27 -07:00
|
|
|
@c "or negative" removed 2013/08; see bug#15059.
|
|
|
|
If @var{n} is zero, @code{nthcdr} returns all of
|
2007-09-06 04:25:08 +00:00
|
|
|
@var{list}. If the length of @var{list} is @var{n} or less,
|
|
|
|
@code{nthcdr} returns @code{nil}.
|
|
|
|
|
2024-04-28 18:38:44 +02:00
|
|
|
An alias for @code{nthcdr} is @code{drop}.
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(nthcdr 1 '(1 2 3 4))
|
|
|
|
@result{} (2 3 4)
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(nthcdr 10 '(1 2 3 4))
|
|
|
|
@result{} nil
|
|
|
|
@end group
|
|
|
|
@group
|
2013-08-13 00:15:27 -07:00
|
|
|
(nthcdr 0 '(1 2 3 4))
|
2007-09-06 04:25:08 +00:00
|
|
|
@result{} (1 2 3 4)
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
2022-07-13 13:46:52 +02:00
|
|
|
@defun take n list
|
|
|
|
This function returns the @var{n} first elements of @var{list}. Essentially,
|
|
|
|
it returns the part of @var{list} that @code{nthcdr} skips.
|
|
|
|
|
2022-07-18 12:19:38 +02:00
|
|
|
@code{take} returns @var{list} if shorter than @var{n} elements;
|
2022-07-13 13:46:52 +02:00
|
|
|
it returns @code{nil} if @var{n} is zero or negative.
|
|
|
|
|
2024-04-28 18:38:44 +02:00
|
|
|
In general, @code{(append (take @var{n} @var{list}) (drop @var{n} @var{list}))}
|
|
|
|
will return a list equal to @var{list}.
|
|
|
|
|
2022-07-13 13:46:52 +02:00
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(take 3 '(a b c d))
|
|
|
|
@result{} (a b c)
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(take 10 '(a b c d))
|
|
|
|
@result{} (a b c d)
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(take 0 '(a b c d))
|
|
|
|
@result{} nil
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun ntake n list
|
|
|
|
This is a version of @code{take} that works by destructively modifying
|
|
|
|
the list structure of the argument. That makes it faster, but the
|
2022-07-18 12:19:38 +02:00
|
|
|
original value of @var{list} may be lost.
|
|
|
|
|
|
|
|
@code{ntake} returns @var{list} unmodified if shorter than @var{n}
|
|
|
|
elements; it returns @code{nil} if @var{n} is zero or negative.
|
2022-07-18 15:46:18 +03:00
|
|
|
Otherwise, it returns @var{list} truncated to its first @var{n}
|
|
|
|
elements.
|
2022-07-18 12:19:38 +02:00
|
|
|
|
|
|
|
This means that it is usually a good idea to use the return value and
|
|
|
|
not just rely on the truncation effect unless @var{n} is known to be
|
|
|
|
positive.
|
2022-07-13 13:46:52 +02:00
|
|
|
@end defun
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@defun last list &optional n
|
|
|
|
This function returns the last link of @var{list}. The @code{car} of
|
|
|
|
this link is the list's last element. If @var{list} is null,
|
|
|
|
@code{nil} is returned. If @var{n} is non-@code{nil}, the
|
|
|
|
@var{n}th-to-last link is returned instead, or the whole of @var{list}
|
|
|
|
if @var{n} is bigger than @var{list}'s length.
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun safe-length list
|
|
|
|
@anchor{Definition of safe-length}
|
|
|
|
This function returns the length of @var{list}, with no risk of either
|
|
|
|
an error or an infinite loop. It generally returns the number of
|
|
|
|
distinct cons cells in the list. However, for circular lists,
|
|
|
|
the value is just an upper bound; it is often too large.
|
|
|
|
|
|
|
|
If @var{list} is not @code{nil} or a cons cell, @code{safe-length}
|
|
|
|
returns 0.
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
The most common way to compute the length of a list, when you are not
|
|
|
|
worried that it may be circular, is with @code{length}. @xref{Sequence
|
|
|
|
Functions}.
|
|
|
|
|
|
|
|
@defun caar cons-cell
|
|
|
|
This is the same as @code{(car (car @var{cons-cell}))}.
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun cadr cons-cell
|
|
|
|
This is the same as @code{(car (cdr @var{cons-cell}))}
|
|
|
|
or @code{(nth 1 @var{cons-cell})}.
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun cdar cons-cell
|
|
|
|
This is the same as @code{(cdr (car @var{cons-cell}))}.
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun cddr cons-cell
|
|
|
|
This is the same as @code{(cdr (cdr @var{cons-cell}))}
|
|
|
|
or @code{(nthcdr 2 @var{cons-cell})}.
|
|
|
|
@end defun
|
|
|
|
|
Move cXXXr and cXXXXr to subr.el
* etc/NEWS: Mention new core Elisp.
* doc/lispref/lists.texi (List Elements): Document and index the new
functions.
* doc/misc/cl.texi (List Functions): Change "defines" to "aliases".
* lisp/subr.el (caaar, caadr, cadar, caddr, cdaar, cdadr, cddar)
(cdddr, caaaar caaadr, caadar, caaddr, cadaar, cadadr, caddar):
(cadddr, cdaaar, cdaadr, cdadar, cdaddr, cddaar, cddadr, cdddar):
(cddddr): New functions.
* lisp/emacs-lisp/cl-lib.el (cl-caaar, cl-caadr, cl-cadar, cl-caddr):
(cl-cdaar, cl-cdadr, cl-cddar cl-cdddr, cl-caaaar cl-caaadr):
(cl-caadar, cl-caaddr, cl-cadaar, cl-cadadr, cl-caddar, cl-cadddr):
(cl-cdaaar, cl-cdaadr, cl-cdadar, cl-cdaddr, cl-cddaar, cl-cddadr):
(cl-cdddar, cl-cddddr): Alias to new subr functions.
* lisp/emacs-lisp/cl.el (cl-unload-function): Remove cXXXr and cXXXXr
elements.
2017-01-25 14:21:10 -05:00
|
|
|
@findex caaar
|
|
|
|
@findex caadr
|
|
|
|
@findex cadar
|
|
|
|
@findex caddr
|
|
|
|
@findex cdaar
|
|
|
|
@findex cdadr
|
|
|
|
@findex cddar
|
|
|
|
@findex cdddr
|
|
|
|
@findex caaaar
|
|
|
|
@findex caaadr
|
|
|
|
@findex caadar
|
|
|
|
@findex caaddr
|
|
|
|
@findex cadaar
|
|
|
|
@findex cadadr
|
|
|
|
@findex caddar
|
|
|
|
@findex cadddr
|
|
|
|
@findex cdaaar
|
|
|
|
@findex cdaadr
|
|
|
|
@findex cdadar
|
|
|
|
@findex cdaddr
|
|
|
|
@findex cddaar
|
|
|
|
@findex cddadr
|
|
|
|
@findex cdddar
|
|
|
|
@findex cddddr
|
|
|
|
In addition to the above, 24 additional compositions of @code{car} and
|
2017-01-25 22:49:35 +02:00
|
|
|
@code{cdr} are defined as @code{c@var{xxx}r} and @code{c@var{xxxx}r},
|
|
|
|
where each @code{@var{x}} is either @code{a} or @code{d}. @code{cadr},
|
|
|
|
@code{caddr}, and @code{cadddr} pick out the second, third or fourth
|
|
|
|
elements of a list, respectively. @file{cl-lib} provides the same
|
|
|
|
under the names @code{cl-second}, @code{cl-third}, and
|
|
|
|
@code{cl-fourth}. @xref{List Functions,,, cl, Common Lisp
|
|
|
|
Extensions}.
|
Move cXXXr and cXXXXr to subr.el
* etc/NEWS: Mention new core Elisp.
* doc/lispref/lists.texi (List Elements): Document and index the new
functions.
* doc/misc/cl.texi (List Functions): Change "defines" to "aliases".
* lisp/subr.el (caaar, caadr, cadar, caddr, cdaar, cdadr, cddar)
(cdddr, caaaar caaadr, caadar, caaddr, cadaar, cadadr, caddar):
(cadddr, cdaaar, cdaadr, cdadar, cdaddr, cddaar, cddadr, cdddar):
(cddddr): New functions.
* lisp/emacs-lisp/cl-lib.el (cl-caaar, cl-caadr, cl-cadar, cl-caddr):
(cl-cdaar, cl-cdadr, cl-cddar cl-cdddr, cl-caaaar cl-caaadr):
(cl-caadar, cl-caaddr, cl-cadaar, cl-cadadr, cl-caddar, cl-cadddr):
(cl-cdaaar, cl-cdaadr, cl-cdadar, cl-cdaddr, cl-cddaar, cl-cddadr):
(cl-cdddar, cl-cddddr): Alias to new subr functions.
* lisp/emacs-lisp/cl.el (cl-unload-function): Remove cXXXr and cXXXXr
elements.
2017-01-25 14:21:10 -05:00
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@defun butlast x &optional n
|
|
|
|
This function returns the list @var{x} with the last element,
|
|
|
|
or the last @var{n} elements, removed. If @var{n} is greater
|
|
|
|
than zero it makes a copy of the list so as not to damage the
|
|
|
|
original list. In general, @code{(append (butlast @var{x} @var{n})
|
|
|
|
(last @var{x} @var{n}))} will return a list equal to @var{x}.
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun nbutlast x &optional n
|
|
|
|
This is a version of @code{butlast} that works by destructively
|
|
|
|
modifying the @code{cdr} of the appropriate element, rather than
|
|
|
|
making a copy of the list.
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@node Building Lists
|
|
|
|
@section Building Cons Cells and Lists
|
|
|
|
@cindex cons cells
|
|
|
|
@cindex building lists
|
|
|
|
|
|
|
|
Many functions build lists, as lists reside at the very heart of Lisp.
|
|
|
|
@code{cons} is the fundamental list-building function; however, it is
|
|
|
|
interesting to note that @code{list} is used more times in the source
|
|
|
|
code for Emacs than @code{cons}.
|
|
|
|
|
|
|
|
@defun cons object1 object2
|
|
|
|
This function is the most basic function for building new list
|
|
|
|
structure. It creates a new cons cell, making @var{object1} the
|
|
|
|
@sc{car}, and @var{object2} the @sc{cdr}. It then returns the new
|
|
|
|
cons cell. The arguments @var{object1} and @var{object2} may be any
|
|
|
|
Lisp objects, but most often @var{object2} is a list.
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(cons 1 '(2))
|
|
|
|
@result{} (1 2)
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(cons 1 '())
|
|
|
|
@result{} (1)
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(cons 1 2)
|
|
|
|
@result{} (1 . 2)
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@cindex consing
|
|
|
|
@code{cons} is often used to add a single element to the front of a
|
|
|
|
list. This is called @dfn{consing the element onto the list}.
|
|
|
|
@footnote{There is no strictly equivalent way to add an element to
|
|
|
|
the end of a list. You can use @code{(append @var{listname} (list
|
|
|
|
@var{newelt}))}, which creates a whole new list by copying @var{listname}
|
|
|
|
and adding @var{newelt} to its end. Or you can use @code{(nconc
|
|
|
|
@var{listname} (list @var{newelt}))}, which modifies @var{listname}
|
|
|
|
by following all the @sc{cdr}s and then replacing the terminating
|
|
|
|
@code{nil}. Compare this to adding an element to the beginning of a
|
|
|
|
list with @code{cons}, which neither copies nor modifies the list.}
|
|
|
|
For example:
|
|
|
|
|
|
|
|
@example
|
|
|
|
(setq list (cons newelt list))
|
|
|
|
@end example
|
|
|
|
|
|
|
|
Note that there is no conflict between the variable named @code{list}
|
|
|
|
used in this example and the function named @code{list} described below;
|
|
|
|
any symbol can serve both purposes.
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun list &rest objects
|
|
|
|
This function creates a list with @var{objects} as its elements. The
|
|
|
|
resulting list is always @code{nil}-terminated. If no @var{objects}
|
|
|
|
are given, the empty list is returned.
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(list 1 2 3 4 5)
|
|
|
|
@result{} (1 2 3 4 5)
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(list 1 2 '(3 4 5) 'foo)
|
|
|
|
@result{} (1 2 (3 4 5) foo)
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(list)
|
|
|
|
@result{} nil
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun make-list length object
|
|
|
|
This function creates a list of @var{length} elements, in which each
|
|
|
|
element is @var{object}. Compare @code{make-list} with
|
|
|
|
@code{make-string} (@pxref{Creating Strings}).
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(make-list 3 'pigs)
|
|
|
|
@result{} (pigs pigs pigs)
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(make-list 0 'pigs)
|
|
|
|
@result{} nil
|
|
|
|
@end group
|
|
|
|
@group
|
2011-08-28 15:08:45 -04:00
|
|
|
(setq l (make-list 3 '(a b)))
|
2007-09-06 04:25:08 +00:00
|
|
|
@result{} ((a b) (a b) (a b))
|
|
|
|
(eq (car l) (cadr l))
|
|
|
|
@result{} t
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun append &rest sequences
|
|
|
|
@cindex copying lists
|
|
|
|
This function returns a list containing all the elements of
|
|
|
|
@var{sequences}. The @var{sequences} may be lists, vectors,
|
|
|
|
bool-vectors, or strings, but the last one should usually be a list.
|
|
|
|
All arguments except the last one are copied, so none of the arguments
|
|
|
|
is altered. (See @code{nconc} in @ref{Rearrangement}, for a way to join
|
|
|
|
lists with no copying.)
|
|
|
|
|
|
|
|
More generally, the final argument to @code{append} may be any Lisp
|
|
|
|
object. The final argument is not copied or converted; it becomes the
|
|
|
|
@sc{cdr} of the last cons cell in the new list. If the final argument
|
|
|
|
is itself a list, then its elements become in effect elements of the
|
|
|
|
result list. If the final element is not a list, the result is a
|
|
|
|
dotted list since its final @sc{cdr} is not @code{nil} as required
|
2018-07-10 19:51:28 -07:00
|
|
|
in a proper list (@pxref{Cons Cells}).
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
|
|
|
Here is an example of using @code{append}:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(setq trees '(pine oak))
|
|
|
|
@result{} (pine oak)
|
|
|
|
(setq more-trees (append '(maple birch) trees))
|
|
|
|
@result{} (maple birch pine oak)
|
|
|
|
@end group
|
|
|
|
|
|
|
|
@group
|
|
|
|
trees
|
|
|
|
@result{} (pine oak)
|
|
|
|
more-trees
|
|
|
|
@result{} (maple birch pine oak)
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(eq trees (cdr (cdr more-trees)))
|
|
|
|
@result{} t
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
You can see how @code{append} works by looking at a box diagram. The
|
|
|
|
variable @code{trees} is set to the list @code{(pine oak)} and then the
|
|
|
|
variable @code{more-trees} is set to the list @code{(maple birch pine
|
|
|
|
oak)}. However, the variable @code{trees} continues to refer to the
|
|
|
|
original list:
|
|
|
|
|
|
|
|
@smallexample
|
|
|
|
@group
|
|
|
|
more-trees trees
|
|
|
|
| |
|
|
|
|
| --- --- --- --- -> --- --- --- ---
|
|
|
|
--> | | |--> | | |--> | | |--> | | |--> nil
|
|
|
|
--- --- --- --- --- --- --- ---
|
|
|
|
| | | |
|
|
|
|
| | | |
|
|
|
|
--> maple -->birch --> pine --> oak
|
|
|
|
@end group
|
|
|
|
@end smallexample
|
|
|
|
|
|
|
|
An empty sequence contributes nothing to the value returned by
|
|
|
|
@code{append}. As a consequence of this, a final @code{nil} argument
|
|
|
|
forces a copy of the previous argument:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
trees
|
|
|
|
@result{} (pine oak)
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(setq wood (append trees nil))
|
|
|
|
@result{} (pine oak)
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
wood
|
|
|
|
@result{} (pine oak)
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(eq wood trees)
|
|
|
|
@result{} nil
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@noindent
|
|
|
|
This once was the usual way to copy a list, before the function
|
|
|
|
@code{copy-sequence} was invented. @xref{Sequences Arrays Vectors}.
|
|
|
|
|
|
|
|
Here we show the use of vectors and strings as arguments to @code{append}:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(append [a b] "cd" nil)
|
|
|
|
@result{} (a b 99 100)
|
|
|
|
@end group
|
2024-09-23 14:41:34 +03:00
|
|
|
@end example
|
|
|
|
|
|
|
|
@cindex list of characters of a string
|
|
|
|
@cindex convert string to list of its characters
|
2024-09-23 17:13:31 +03:00
|
|
|
@findex string-to-list
|
2024-09-23 14:41:34 +03:00
|
|
|
Here's how to convert a string into a list of its characters:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(append "abcd" nil)
|
|
|
|
@result{} (97 98 99 100)
|
|
|
|
@end group
|
2007-09-06 04:25:08 +00:00
|
|
|
@end example
|
|
|
|
|
2024-09-23 17:13:31 +03:00
|
|
|
@noindent
|
|
|
|
The function @code{string-to-list} is a handy shortcut for the above.
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
With the help of @code{apply} (@pxref{Calling Functions}), we can append
|
|
|
|
all the lists in a list of lists:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(apply 'append '((a b c) nil (x y z) nil))
|
|
|
|
@result{} (a b c x y z)
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
If no @var{sequences} are given, @code{nil} is returned:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(append)
|
|
|
|
@result{} nil
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
Here are some examples where the final argument is not a list:
|
|
|
|
|
|
|
|
@example
|
2024-09-23 14:41:34 +03:00
|
|
|
@group
|
2007-09-06 04:25:08 +00:00
|
|
|
(append '(x y) 'z)
|
|
|
|
@result{} (x y . z)
|
|
|
|
(append '(x y) [z])
|
|
|
|
@result{} (x y . [z])
|
2024-09-23 14:41:34 +03:00
|
|
|
@end group
|
2007-09-06 04:25:08 +00:00
|
|
|
@end example
|
|
|
|
|
|
|
|
@noindent
|
|
|
|
The second example shows that when the final argument is a sequence but
|
|
|
|
not a list, the sequence's elements do not become elements of the
|
|
|
|
resulting list. Instead, the sequence becomes the final @sc{cdr}, like
|
|
|
|
any other non-list final argument.
|
|
|
|
|
2024-09-23 14:41:34 +03:00
|
|
|
As an exception, if all the arguments but the last are @code{nil} and
|
|
|
|
the last argument is not a list, the return value is that last argument
|
2024-09-23 17:13:31 +03:00
|
|
|
unchanged (i.e., in this case the return value is not a list):
|
2024-09-23 14:41:34 +03:00
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(append nil nil "abcd")
|
|
|
|
@result{} "abcd"
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
2023-05-19 12:32:28 +02:00
|
|
|
@defun copy-tree tree &optional vectors-and-records
|
2018-12-11 09:16:21 +01:00
|
|
|
This function returns a copy of the tree @var{tree}. If @var{tree} is a
|
2007-09-06 04:25:08 +00:00
|
|
|
cons cell, this makes a new cons cell with the same @sc{car} and
|
|
|
|
@sc{cdr}, then recursively copies the @sc{car} and @sc{cdr} in the
|
|
|
|
same way.
|
|
|
|
|
|
|
|
Normally, when @var{tree} is anything other than a cons cell,
|
2023-05-14 21:02:15 -07:00
|
|
|
@code{copy-tree} simply returns @var{tree}. However, if
|
2023-05-19 12:32:28 +02:00
|
|
|
@var{vectors-and-records} is non-@code{nil}, it copies vectors and records
|
|
|
|
too (and operates recursively on their elements). The @var{tree}
|
|
|
|
argument must not contain cycles.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
New function flatten-tree
Co-authored-by: Basil L. Contovounesios <contovob@tcd.ie>
* doc/lispref/lists.texi: Document `flatten-tree'.
* lisp/progmodes/js.el (js--maybe-join):
* lisp/printing.el (pr-switches):
* lisp/lpr.el (lpr-print-region):
* lisp/gnus/nnimap.el (nnimap-find-wanted-parts):
* lisp/gnus/message.el (message-talkative-question):
* lisp/gnus/gnus-sum.el (gnus-remove-thread)
(gnus-thread-highest-number, gnus-thread-latest-date):
* lisp/eshell/esh-util.el (eshell-flatten-and-stringify):
* lisp/eshell/esh-opt.el (eshell-eval-using-options):
* lisp/eshell/esh-ext.el (eshell-external-command):
* lisp/eshell/em-xtra.el (eshell/expr):
* lisp/eshell/em-unix.el (eshell/rm, eshell-mvcpln-template)
(eshell/cat, eshell/make, eshell-poor-mans-grep, eshell-grep)
(eshell/du, eshell/time, eshell/diff, eshell/locate):
* lisp/eshell/em-tramp.el (eshell/su, eshell/sudo):
* lisp/eshell/em-term.el (eshell-exec-visual):
* lisp/eshell/em-dirs.el (eshell-dirs-substitute-cd, eshell/cd):
* lisp/eshell/em-basic.el (eshell/printnl):
Use new flatten-tree.
* lisp/progmodes/js.el (js--flatten-list):
* lisp/lpr.el (lpr-flatten-list):
* lisp/gnus/message.el (message-flatten-list):
* lisp/eshell/esh-util.el (eshell-flatten-list):
Obsolete in favor of Emacs-wide `flatten-tree'.
* lisp/subr.el (flatten-list): Alias to `flatten-tree' for
discoverability.
* lisp/subr.el (flatten-tree): New defun.
* test/lisp/subr-tests.el (subr-tests-flatten-tree): New test.
2018-12-17 12:15:09 +01:00
|
|
|
@defun flatten-tree tree
|
2018-12-17 09:55:06 -08:00
|
|
|
This function returns a ``flattened'' copy of @var{tree}, that is,
|
|
|
|
a list containing all the non-@code{nil} terminal nodes, or leaves, of
|
|
|
|
the tree of cons cells rooted at @var{tree}. Leaves in the returned
|
|
|
|
list are in the same order as in @var{tree}.
|
New function flatten-tree
Co-authored-by: Basil L. Contovounesios <contovob@tcd.ie>
* doc/lispref/lists.texi: Document `flatten-tree'.
* lisp/progmodes/js.el (js--maybe-join):
* lisp/printing.el (pr-switches):
* lisp/lpr.el (lpr-print-region):
* lisp/gnus/nnimap.el (nnimap-find-wanted-parts):
* lisp/gnus/message.el (message-talkative-question):
* lisp/gnus/gnus-sum.el (gnus-remove-thread)
(gnus-thread-highest-number, gnus-thread-latest-date):
* lisp/eshell/esh-util.el (eshell-flatten-and-stringify):
* lisp/eshell/esh-opt.el (eshell-eval-using-options):
* lisp/eshell/esh-ext.el (eshell-external-command):
* lisp/eshell/em-xtra.el (eshell/expr):
* lisp/eshell/em-unix.el (eshell/rm, eshell-mvcpln-template)
(eshell/cat, eshell/make, eshell-poor-mans-grep, eshell-grep)
(eshell/du, eshell/time, eshell/diff, eshell/locate):
* lisp/eshell/em-tramp.el (eshell/su, eshell/sudo):
* lisp/eshell/em-term.el (eshell-exec-visual):
* lisp/eshell/em-dirs.el (eshell-dirs-substitute-cd, eshell/cd):
* lisp/eshell/em-basic.el (eshell/printnl):
Use new flatten-tree.
* lisp/progmodes/js.el (js--flatten-list):
* lisp/lpr.el (lpr-flatten-list):
* lisp/gnus/message.el (message-flatten-list):
* lisp/eshell/esh-util.el (eshell-flatten-list):
Obsolete in favor of Emacs-wide `flatten-tree'.
* lisp/subr.el (flatten-list): Alias to `flatten-tree' for
discoverability.
* lisp/subr.el (flatten-tree): New defun.
* test/lisp/subr-tests.el (subr-tests-flatten-tree): New test.
2018-12-17 12:15:09 +01:00
|
|
|
@end defun
|
|
|
|
|
|
|
|
@example
|
|
|
|
(flatten-tree '(1 (2 . 3) nil (4 5 (6)) 7))
|
|
|
|
@result{}(1 2 3 4 5 6 7)
|
|
|
|
@end example
|
|
|
|
|
2021-09-21 20:30:57 +02:00
|
|
|
@defun ensure-list object
|
2021-09-21 21:51:38 +03:00
|
|
|
This function returns @var{object} as a list. If @var{object} is
|
|
|
|
already a list, the function returns it; otherwise, the function
|
|
|
|
returns a one-element list containing @var{object}.
|
2021-09-21 20:30:57 +02:00
|
|
|
|
|
|
|
This is usually useful if you have a variable that may or may not be a
|
|
|
|
list, and you can then say, for instance:
|
|
|
|
|
|
|
|
@lisp
|
|
|
|
(dolist (elem (ensure-list foo))
|
|
|
|
(princ elem))
|
|
|
|
@end lisp
|
|
|
|
@end defun
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@defun number-sequence from &optional to separation
|
2018-12-17 09:55:06 -08:00
|
|
|
This function returns a list of numbers starting with @var{from} and
|
2007-09-06 04:25:08 +00:00
|
|
|
incrementing by @var{separation}, and ending at or just before
|
|
|
|
@var{to}. @var{separation} can be positive or negative and defaults
|
|
|
|
to 1. If @var{to} is @code{nil} or numerically equal to @var{from},
|
|
|
|
the value is the one-element list @code{(@var{from})}. If @var{to} is
|
|
|
|
less than @var{from} with a positive @var{separation}, or greater than
|
|
|
|
@var{from} with a negative @var{separation}, the value is @code{nil}
|
|
|
|
because those arguments specify an empty sequence.
|
|
|
|
|
|
|
|
If @var{separation} is 0 and @var{to} is neither @code{nil} nor
|
|
|
|
numerically equal to @var{from}, @code{number-sequence} signals an
|
|
|
|
error, since those arguments specify an infinite sequence.
|
|
|
|
|
Style fixes for floating-point doc.
* commands.texi, customize.texi, display.texi, elisp.texi, files.texi:
* frames.texi, hash.texi, internals.texi, keymaps.texi, lists.texi:
* minibuf.texi, nonascii.texi, numbers.texi, objects.texi, os.texi:
* processes.texi, streams.texi, strings.texi, text.texi:
* variables.texi, windows.texi:
Hyphenate "floating-point" iff it precedes a noun.
Reword to avoid nouns and hyphenation when that's easy.
Prefer "integer" to "integer number" and "is floating point"
to "is a floating point number".
Prefer "@minus{}" to "-" when it's a minus.
2014-03-17 18:19:03 -07:00
|
|
|
All arguments are numbers.
|
|
|
|
Floating-point arguments can be tricky, because floating-point
|
2007-09-06 04:25:08 +00:00
|
|
|
arithmetic is inexact. For instance, depending on the machine, it may
|
|
|
|
quite well happen that @code{(number-sequence 0.4 0.6 0.2)} returns
|
|
|
|
the one element list @code{(0.4)}, whereas
|
|
|
|
@code{(number-sequence 0.4 0.8 0.2)} returns a list with three
|
|
|
|
elements. The @var{n}th element of the list is computed by the exact
|
|
|
|
formula @code{(+ @var{from} (* @var{n} @var{separation}))}. Thus, if
|
|
|
|
one wants to make sure that @var{to} is included in the list, one can
|
|
|
|
pass an expression of this exact type for @var{to}. Alternatively,
|
|
|
|
one can replace @var{to} with a slightly larger value (or a slightly
|
|
|
|
more negative value if @var{separation} is negative).
|
|
|
|
|
|
|
|
Some examples:
|
|
|
|
|
|
|
|
@example
|
|
|
|
(number-sequence 4 9)
|
|
|
|
@result{} (4 5 6 7 8 9)
|
|
|
|
(number-sequence 9 4 -1)
|
|
|
|
@result{} (9 8 7 6 5 4)
|
|
|
|
(number-sequence 9 4 -2)
|
|
|
|
@result{} (9 7 5)
|
|
|
|
(number-sequence 8)
|
|
|
|
@result{} (8)
|
|
|
|
(number-sequence 8 5)
|
|
|
|
@result{} nil
|
|
|
|
(number-sequence 5 8 -1)
|
|
|
|
@result{} nil
|
|
|
|
(number-sequence 1.5 6 2)
|
|
|
|
@result{} (1.5 3.5 5.5)
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@node List Variables
|
|
|
|
@section Modifying List Variables
|
Improve indexing on the chapter/section/subsection levels.
doc/lispref/windows.texi (Recombining Windows): Index subject of sections.
doc/lispref/variables.texi (Variables with Restricted Values)
(Generalized Variables): Index subject of sections.
doc/lispref/text.texi (Buffer Contents, Examining Properties)
(Changing Properties, Property Search, Substitution): Index
subject of sections.
doc/lispref/syntax.texi (Motion and Syntax, Parsing Expressions)
(Motion via Parsing, Position Parse, Control Parsing): Index
subject of sections.
doc/lispref/strings.texi (Predicates for Strings, Creating Strings)
(Modifying Strings, Text Comparison): Index subject of sections.
doc/lispref/searching.texi (Syntax of Regexps, Regexp Special)
(Regexp Functions, Regexp Functions): Index subject of sections.
doc/lispref/processes.texi (Subprocess Creation, Process Information): Index
subject of sections.
doc/lispref/positions.texi (Screen Lines): Index subject of sections.
doc/lispref/nonascii.texi (Scanning Charsets, Specifying Coding Systems):
Index subject of sections.
doc/lispref/minibuf.texi (Text from Minibuffer, Object from Minibuffer)
(Multiple Queries, Minibuffer Contents): Index subject of
sections.
doc/lispref/markers.texi (Predicates on Markers, Creating Markers)
(Information from Markers, Moving Markers): Index subject of
sections.
doc/lispref/macros.texi (Defining Macros, Problems with Macros): Index
subject of sections.
doc/lispref/loading.texi (Loading Non-ASCII, Where Defined): Index subject
of sections.
doc/lispref/lists.texi (List-related Predicates, List Variables, Setcar)
(Setcdr, Plist Access): Index subject of sections.
doc/lispref/keymaps.texi (Controlling Active Maps, Scanning Keymaps)
(Modifying Menus): Index subject of sections.
doc/lispref/help.texi (Accessing Documentation, Help Functions): Index
subject of sections.
doc/lispref/hash.texi (Hash Access): Index subject of sections.
doc/lispref/functions.texi (Core Advising Primitives)
(Advising Named Functions, Porting old advices): Index subject of
sections.
doc/lispref/frames.texi (Creating Frames, Initial Parameters)
(Position Parameters, Buffer Parameters, Minibuffers and Frames)
(Pop-Up Menus, Drag and Drop): Index subject of sections.
doc/lispref/files.texi (Visiting Functions, Kinds of Files)
(Unique File Names): Index subject of sections.
doc/lispref/display.texi (Refresh Screen, Echo Area Customization)
(Warning Variables, Warning Options, Delayed Warnings)
(Temporary Displays, Managing Overlays, Overlay Properties)
(Finding Overlays, Size of Displayed Text, Defining Faces)
(Attribute Functions, Displaying Faces, Face Remapping)
(Basic Faces, Font Lookup, Fontsets, Replacing Specs)
(Defining Images, Showing Images): Index subject of sections.
doc/lispref/debugging.texi (Debugging, Explicit Debug)
(Invoking the Debugger, Excess Open, Excess Close): Index subject
of sections.
doc/lispref/customize.texi (Defining New Types, Applying Customizations)
(Custom Themes): Index subject of sections.
doc/lispref/control.texi (Sequencing, Combining Conditions)
(Processing of Errors, Cleanups): Index subject of sections.
doc/lispref/compile.texi (Eval During Compile): Index subject of sections.
doc/lispref/commands.texi (Using Interactive, Distinguish Interactive)
(Command Loop Info, Classifying Events, Event Mod)
(Invoking the Input Method): Index subject of sections.
doc/lispref/buffers.texi (Buffer List, Buffer Gap): Index subject of sections.
doc/lispref/backups.texi (Making Backups, Numbered Backups, Backup Names)
(Reverting): Index subject of sections.
doc/lispref/abbrevs.texi (Abbrev Tables, Defining Abbrevs, Abbrev Files)
(Abbrev Expansion, Standard Abbrev Tables, Abbrev Properties)
(Abbrev Table Properties): Index subject of sections.
doc/lispref/os.texi (Time of Day, Time Conversion, Time Parsing)
(Time Calculations, Idle Timers): Index subject of sections.
2014-12-23 20:42:30 +02:00
|
|
|
@cindex modify a list
|
|
|
|
@cindex list modification
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
These functions, and one macro, provide convenient ways
|
|
|
|
to modify a list which is stored in a variable.
|
|
|
|
|
2012-11-07 13:22:10 +08:00
|
|
|
@defmac push element listname
|
|
|
|
This macro creates a new list whose @sc{car} is @var{element} and
|
|
|
|
whose @sc{cdr} is the list specified by @var{listname}, and saves that
|
|
|
|
list in @var{listname}. In the simplest case, @var{listname} is an
|
|
|
|
unquoted symbol naming a list, and this macro is equivalent
|
|
|
|
to @w{@code{(setq @var{listname} (cons @var{element} @var{listname}))}}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@example
|
|
|
|
(setq l '(a b))
|
|
|
|
@result{} (a b)
|
|
|
|
(push 'c l)
|
|
|
|
@result{} (c a b)
|
|
|
|
l
|
|
|
|
@result{} (c a b)
|
|
|
|
@end example
|
2012-01-23 12:23:50 +08:00
|
|
|
|
2012-11-07 13:22:10 +08:00
|
|
|
More generally, @code{listname} can be a generalized variable. In
|
|
|
|
that case, this macro does the equivalent of @w{@code{(setf
|
|
|
|
@var{listname} (cons @var{element} @var{listname}))}}.
|
|
|
|
@xref{Generalized Variables}.
|
|
|
|
|
2012-01-23 12:23:50 +08:00
|
|
|
For the @code{pop} macro, which removes the first element from a list,
|
|
|
|
@xref{List Elements}.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defmac
|
|
|
|
|
|
|
|
Two functions modify lists that are the values of variables.
|
|
|
|
|
|
|
|
@defun add-to-list symbol element &optional append compare-fn
|
|
|
|
This function sets the variable @var{symbol} by consing @var{element}
|
|
|
|
onto the old value, if @var{element} is not already a member of that
|
|
|
|
value. It returns the resulting list, whether updated or not. The
|
|
|
|
value of @var{symbol} had better be a list already before the call.
|
|
|
|
@code{add-to-list} uses @var{compare-fn} to compare @var{element}
|
|
|
|
against existing list members; if @var{compare-fn} is @code{nil}, it
|
|
|
|
uses @code{equal}.
|
|
|
|
|
|
|
|
Normally, if @var{element} is added, it is added to the front of
|
|
|
|
@var{symbol}, but if the optional argument @var{append} is
|
|
|
|
non-@code{nil}, it is added at the end.
|
|
|
|
|
|
|
|
The argument @var{symbol} is not implicitly quoted; @code{add-to-list}
|
|
|
|
is an ordinary function, like @code{set} and unlike @code{setq}. Quote
|
|
|
|
the argument yourself if that is what you want.
|
2020-02-01 20:11:11 +01:00
|
|
|
|
2024-08-09 08:45:30 +03:00
|
|
|
This function is for adding elements to configuration variables such as
|
|
|
|
@code{load-path} (@pxref{Library Search}), @code{image-load-path}
|
|
|
|
(@pxref{Defining Images}), etc. Its code includes quite a few special
|
|
|
|
checks for these uses, and emits warnings in support of them. For this
|
|
|
|
reason, we recommend against using it in Lisp programs for constructing
|
|
|
|
arbitrary lists; use @code{push} instead. @xref{List Variables}.
|
|
|
|
|
2020-02-01 20:11:11 +01:00
|
|
|
Do not use this function when @var{symbol} refers to a lexical
|
|
|
|
variable.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
|
|
|
Here's a scenario showing how to use @code{add-to-list}:
|
|
|
|
|
|
|
|
@example
|
|
|
|
(setq foo '(a b))
|
|
|
|
@result{} (a b)
|
|
|
|
|
|
|
|
(add-to-list 'foo 'c) ;; @r{Add @code{c}.}
|
|
|
|
@result{} (c a b)
|
|
|
|
|
|
|
|
(add-to-list 'foo 'b) ;; @r{No effect.}
|
|
|
|
@result{} (c a b)
|
|
|
|
|
|
|
|
foo ;; @r{@code{foo} was changed.}
|
|
|
|
@result{} (c a b)
|
|
|
|
@end example
|
|
|
|
|
|
|
|
An equivalent expression for @code{(add-to-list '@var{var}
|
|
|
|
@var{value})} is this:
|
|
|
|
|
|
|
|
@example
|
2020-02-01 20:11:11 +01:00
|
|
|
(if (member @var{value} @var{var})
|
|
|
|
@var{var}
|
|
|
|
(setq @var{var} (cons @var{value} @var{var})))
|
2007-09-06 04:25:08 +00:00
|
|
|
@end example
|
|
|
|
|
2021-01-01 05:48:02 +01:00
|
|
|
@defun add-to-ordered-list symbol element &optional order
|
2007-09-06 04:25:08 +00:00
|
|
|
This function sets the variable @var{symbol} by inserting
|
|
|
|
@var{element} into the old value, which must be a list, at the
|
|
|
|
position specified by @var{order}. If @var{element} is already a
|
2021-01-01 05:48:02 +01:00
|
|
|
member of the list, its position in the list is adjusted according
|
|
|
|
to @var{order}. Membership is tested using @code{eq}.
|
|
|
|
This function returns the resulting list, whether updated or not.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
The @var{order} is typically a number (integer or float), and the
|
|
|
|
elements of the list are sorted in non-decreasing numerical order.
|
|
|
|
|
|
|
|
@var{order} may also be omitted or @code{nil}. Then the numeric order
|
|
|
|
of @var{element} stays unchanged if it already has one; otherwise,
|
|
|
|
@var{element} has no numeric order. Elements without a numeric list
|
|
|
|
order are placed at the end of the list, in no particular order.
|
|
|
|
|
|
|
|
Any other value for @var{order} removes the numeric order of @var{element}
|
|
|
|
if it already has one; otherwise, it is equivalent to @code{nil}.
|
|
|
|
|
|
|
|
The argument @var{symbol} is not implicitly quoted;
|
|
|
|
@code{add-to-ordered-list} is an ordinary function, like @code{set}
|
2012-05-05 12:32:58 +08:00
|
|
|
and unlike @code{setq}. Quote the argument yourself if necessary.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
The ordering information is stored in a hash table on @var{symbol}'s
|
|
|
|
@code{list-order} property.
|
2020-02-02 12:48:51 +01:00
|
|
|
@var{symbol} cannot refer to a lexical variable.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
|
|
|
Here's a scenario showing how to use @code{add-to-ordered-list}:
|
|
|
|
|
|
|
|
@example
|
|
|
|
(setq foo '())
|
|
|
|
@result{} nil
|
|
|
|
|
|
|
|
(add-to-ordered-list 'foo 'a 1) ;; @r{Add @code{a}.}
|
|
|
|
@result{} (a)
|
|
|
|
|
|
|
|
(add-to-ordered-list 'foo 'c 3) ;; @r{Add @code{c}.}
|
|
|
|
@result{} (a c)
|
|
|
|
|
|
|
|
(add-to-ordered-list 'foo 'b 2) ;; @r{Add @code{b}.}
|
|
|
|
@result{} (a b c)
|
|
|
|
|
|
|
|
(add-to-ordered-list 'foo 'b 4) ;; @r{Move @code{b}.}
|
|
|
|
@result{} (a c b)
|
|
|
|
|
|
|
|
(add-to-ordered-list 'foo 'd) ;; @r{Append @code{d}.}
|
|
|
|
@result{} (a c b d)
|
|
|
|
|
|
|
|
(add-to-ordered-list 'foo 'e) ;; @r{Add @code{e}}.
|
|
|
|
@result{} (a c b e d)
|
|
|
|
|
|
|
|
foo ;; @r{@code{foo} was changed.}
|
|
|
|
@result{} (a c b e d)
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@node Modifying Lists
|
|
|
|
@section Modifying Existing List Structure
|
|
|
|
@cindex destructive list operations
|
Document constant vs mutable objects better
This patch builds on a suggested patch by Mattias Engdegård
and on further comments by Eli Zaretskii.
Original bug report by Kevin Vigouroux (Bug#40671).
* doc/lispintro/emacs-lisp-intro.texi (set & setq, Review)
(setcar, Lists diagrammed, Mail Aliases, Indent Tabs Mode):
setq is a special form, not a function or command.
* doc/lispintro/emacs-lisp-intro.texi (setcar):
* doc/lispref/lists.texi (Modifying Lists, Rearrangement):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions, Vectors):
* doc/lispref/strings.texi (String Basics, Modifying Strings):
Mention mutable vs constant objects.
* doc/lispintro/emacs-lisp-intro.texi (setcar, setcdr)
(kill-new function, cons & search-fwd Review):
* doc/lispref/edebug.texi (Printing in Edebug):
* doc/lispref/keymaps.texi (Changing Key Bindings):
* doc/lispref/lists.texi (Setcar, Setcdr, Rearrangement)
(Sets And Lists, Association Lists, Plist Access):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions):
* doc/lispref/strings.texi (Text Comparison):
Fix examples so that they do not try to change constants.
2020-04-18 12:59:17 -07:00
|
|
|
@cindex mutable lists
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
You can modify the @sc{car} and @sc{cdr} contents of a cons cell with the
|
2015-09-15 08:46:48 -07:00
|
|
|
primitives @code{setcar} and @code{setcdr}. These are destructive
|
2007-09-06 04:25:08 +00:00
|
|
|
operations because they change existing list structure.
|
2020-04-19 12:00:49 -07:00
|
|
|
Destructive operations should be applied only to mutable lists,
|
Document constant vs mutable objects better
This patch builds on a suggested patch by Mattias Engdegård
and on further comments by Eli Zaretskii.
Original bug report by Kevin Vigouroux (Bug#40671).
* doc/lispintro/emacs-lisp-intro.texi (set & setq, Review)
(setcar, Lists diagrammed, Mail Aliases, Indent Tabs Mode):
setq is a special form, not a function or command.
* doc/lispintro/emacs-lisp-intro.texi (setcar):
* doc/lispref/lists.texi (Modifying Lists, Rearrangement):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions, Vectors):
* doc/lispref/strings.texi (String Basics, Modifying Strings):
Mention mutable vs constant objects.
* doc/lispintro/emacs-lisp-intro.texi (setcar, setcdr)
(kill-new function, cons & search-fwd Review):
* doc/lispref/edebug.texi (Printing in Edebug):
* doc/lispref/keymaps.texi (Changing Key Bindings):
* doc/lispref/lists.texi (Setcar, Setcdr, Rearrangement)
(Sets And Lists, Association Lists, Plist Access):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions):
* doc/lispref/strings.texi (Text Comparison):
Fix examples so that they do not try to change constants.
2020-04-18 12:59:17 -07:00
|
|
|
that is, lists constructed via @code{cons}, @code{list} or similar
|
2020-05-16 17:17:00 -07:00
|
|
|
operations. Lists created by quoting are part of the program and
|
|
|
|
should not be changed by destructive operations. @xref{Mutability}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@cindex CL note---@code{rplaca} vs @code{setcar}
|
|
|
|
@quotation
|
|
|
|
@findex rplaca
|
|
|
|
@findex rplacd
|
|
|
|
@b{Common Lisp note:} Common Lisp uses functions @code{rplaca} and
|
|
|
|
@code{rplacd} to alter list structure; they change structure the same
|
|
|
|
way as @code{setcar} and @code{setcdr}, but the Common Lisp functions
|
|
|
|
return the cons cell while @code{setcar} and @code{setcdr} return the
|
|
|
|
new @sc{car} or @sc{cdr}.
|
|
|
|
@end quotation
|
|
|
|
|
|
|
|
@menu
|
|
|
|
* Setcar:: Replacing an element in a list.
|
|
|
|
* Setcdr:: Replacing part of the list backbone.
|
|
|
|
This can be used to remove or add elements.
|
|
|
|
* Rearrangement:: Reordering the elements in a list; combining lists.
|
|
|
|
@end menu
|
|
|
|
|
|
|
|
@node Setcar
|
|
|
|
@subsection Altering List Elements with @code{setcar}
|
Improve indexing on the chapter/section/subsection levels.
doc/lispref/windows.texi (Recombining Windows): Index subject of sections.
doc/lispref/variables.texi (Variables with Restricted Values)
(Generalized Variables): Index subject of sections.
doc/lispref/text.texi (Buffer Contents, Examining Properties)
(Changing Properties, Property Search, Substitution): Index
subject of sections.
doc/lispref/syntax.texi (Motion and Syntax, Parsing Expressions)
(Motion via Parsing, Position Parse, Control Parsing): Index
subject of sections.
doc/lispref/strings.texi (Predicates for Strings, Creating Strings)
(Modifying Strings, Text Comparison): Index subject of sections.
doc/lispref/searching.texi (Syntax of Regexps, Regexp Special)
(Regexp Functions, Regexp Functions): Index subject of sections.
doc/lispref/processes.texi (Subprocess Creation, Process Information): Index
subject of sections.
doc/lispref/positions.texi (Screen Lines): Index subject of sections.
doc/lispref/nonascii.texi (Scanning Charsets, Specifying Coding Systems):
Index subject of sections.
doc/lispref/minibuf.texi (Text from Minibuffer, Object from Minibuffer)
(Multiple Queries, Minibuffer Contents): Index subject of
sections.
doc/lispref/markers.texi (Predicates on Markers, Creating Markers)
(Information from Markers, Moving Markers): Index subject of
sections.
doc/lispref/macros.texi (Defining Macros, Problems with Macros): Index
subject of sections.
doc/lispref/loading.texi (Loading Non-ASCII, Where Defined): Index subject
of sections.
doc/lispref/lists.texi (List-related Predicates, List Variables, Setcar)
(Setcdr, Plist Access): Index subject of sections.
doc/lispref/keymaps.texi (Controlling Active Maps, Scanning Keymaps)
(Modifying Menus): Index subject of sections.
doc/lispref/help.texi (Accessing Documentation, Help Functions): Index
subject of sections.
doc/lispref/hash.texi (Hash Access): Index subject of sections.
doc/lispref/functions.texi (Core Advising Primitives)
(Advising Named Functions, Porting old advices): Index subject of
sections.
doc/lispref/frames.texi (Creating Frames, Initial Parameters)
(Position Parameters, Buffer Parameters, Minibuffers and Frames)
(Pop-Up Menus, Drag and Drop): Index subject of sections.
doc/lispref/files.texi (Visiting Functions, Kinds of Files)
(Unique File Names): Index subject of sections.
doc/lispref/display.texi (Refresh Screen, Echo Area Customization)
(Warning Variables, Warning Options, Delayed Warnings)
(Temporary Displays, Managing Overlays, Overlay Properties)
(Finding Overlays, Size of Displayed Text, Defining Faces)
(Attribute Functions, Displaying Faces, Face Remapping)
(Basic Faces, Font Lookup, Fontsets, Replacing Specs)
(Defining Images, Showing Images): Index subject of sections.
doc/lispref/debugging.texi (Debugging, Explicit Debug)
(Invoking the Debugger, Excess Open, Excess Close): Index subject
of sections.
doc/lispref/customize.texi (Defining New Types, Applying Customizations)
(Custom Themes): Index subject of sections.
doc/lispref/control.texi (Sequencing, Combining Conditions)
(Processing of Errors, Cleanups): Index subject of sections.
doc/lispref/compile.texi (Eval During Compile): Index subject of sections.
doc/lispref/commands.texi (Using Interactive, Distinguish Interactive)
(Command Loop Info, Classifying Events, Event Mod)
(Invoking the Input Method): Index subject of sections.
doc/lispref/buffers.texi (Buffer List, Buffer Gap): Index subject of sections.
doc/lispref/backups.texi (Making Backups, Numbered Backups, Backup Names)
(Reverting): Index subject of sections.
doc/lispref/abbrevs.texi (Abbrev Tables, Defining Abbrevs, Abbrev Files)
(Abbrev Expansion, Standard Abbrev Tables, Abbrev Properties)
(Abbrev Table Properties): Index subject of sections.
doc/lispref/os.texi (Time of Day, Time Conversion, Time Parsing)
(Time Calculations, Idle Timers): Index subject of sections.
2014-12-23 20:42:30 +02:00
|
|
|
@cindex replace list element
|
|
|
|
@cindex list, replace element
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
Changing the @sc{car} of a cons cell is done with @code{setcar}. When
|
|
|
|
used on a list, @code{setcar} replaces one element of a list with a
|
|
|
|
different element.
|
|
|
|
|
|
|
|
@defun setcar cons object
|
|
|
|
This function stores @var{object} as the new @sc{car} of @var{cons},
|
|
|
|
replacing its previous @sc{car}. In other words, it changes the
|
|
|
|
@sc{car} slot of @var{cons} to refer to @var{object}. It returns the
|
|
|
|
value @var{object}. For example:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
2020-05-16 17:17:00 -07:00
|
|
|
(setq x (list 1 2))
|
2007-09-06 04:25:08 +00:00
|
|
|
@result{} (1 2)
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(setcar x 4)
|
|
|
|
@result{} 4
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
x
|
|
|
|
@result{} (4 2)
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
When a cons cell is part of the shared structure of several lists,
|
|
|
|
storing a new @sc{car} into the cons changes one element of each of
|
|
|
|
these lists. Here is an example:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
2020-05-16 17:17:00 -07:00
|
|
|
;; @r{Create two lists that are partly shared.}
|
Document constant vs mutable objects better
This patch builds on a suggested patch by Mattias Engdegård
and on further comments by Eli Zaretskii.
Original bug report by Kevin Vigouroux (Bug#40671).
* doc/lispintro/emacs-lisp-intro.texi (set & setq, Review)
(setcar, Lists diagrammed, Mail Aliases, Indent Tabs Mode):
setq is a special form, not a function or command.
* doc/lispintro/emacs-lisp-intro.texi (setcar):
* doc/lispref/lists.texi (Modifying Lists, Rearrangement):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions, Vectors):
* doc/lispref/strings.texi (String Basics, Modifying Strings):
Mention mutable vs constant objects.
* doc/lispintro/emacs-lisp-intro.texi (setcar, setcdr)
(kill-new function, cons & search-fwd Review):
* doc/lispref/edebug.texi (Printing in Edebug):
* doc/lispref/keymaps.texi (Changing Key Bindings):
* doc/lispref/lists.texi (Setcar, Setcdr, Rearrangement)
(Sets And Lists, Association Lists, Plist Access):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions):
* doc/lispref/strings.texi (Text Comparison):
Fix examples so that they do not try to change constants.
2020-04-18 12:59:17 -07:00
|
|
|
(setq x1 (list 'a 'b 'c))
|
2007-09-06 04:25:08 +00:00
|
|
|
@result{} (a b c)
|
|
|
|
(setq x2 (cons 'z (cdr x1)))
|
|
|
|
@result{} (z b c)
|
|
|
|
@end group
|
|
|
|
|
|
|
|
@group
|
|
|
|
;; @r{Replace the @sc{car} of a shared link.}
|
|
|
|
(setcar (cdr x1) 'foo)
|
|
|
|
@result{} foo
|
|
|
|
x1 ; @r{Both lists are changed.}
|
|
|
|
@result{} (a foo c)
|
|
|
|
x2
|
|
|
|
@result{} (z foo c)
|
|
|
|
@end group
|
|
|
|
|
|
|
|
@group
|
|
|
|
;; @r{Replace the @sc{car} of a link that is not shared.}
|
|
|
|
(setcar x1 'baz)
|
|
|
|
@result{} baz
|
|
|
|
x1 ; @r{Only one list is changed.}
|
|
|
|
@result{} (baz foo c)
|
|
|
|
x2
|
|
|
|
@result{} (z foo c)
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
Here is a graphical depiction of the shared structure of the two lists
|
|
|
|
in the variables @code{x1} and @code{x2}, showing why replacing @code{b}
|
|
|
|
changes them both:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
--- --- --- --- --- ---
|
|
|
|
x1---> | | |----> | | |--> | | |--> nil
|
|
|
|
--- --- --- --- --- ---
|
|
|
|
| --> | |
|
|
|
|
| | | |
|
|
|
|
--> a | --> b --> c
|
|
|
|
|
|
|
|
|
--- --- |
|
|
|
|
x2--> | | |--
|
|
|
|
--- ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
--> z
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
Here is an alternative form of box diagram, showing the same relationship:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
x1:
|
|
|
|
-------------- -------------- --------------
|
|
|
|
| car | cdr | | car | cdr | | car | cdr |
|
|
|
|
| a | o------->| b | o------->| c | nil |
|
|
|
|
| | | -->| | | | | |
|
|
|
|
-------------- | -------------- --------------
|
|
|
|
|
|
|
|
|
x2: |
|
|
|
|
-------------- |
|
|
|
|
| car | cdr | |
|
|
|
|
| z | o----
|
|
|
|
| | |
|
|
|
|
--------------
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@node Setcdr
|
|
|
|
@subsection Altering the CDR of a List
|
Improve indexing on the chapter/section/subsection levels.
doc/lispref/windows.texi (Recombining Windows): Index subject of sections.
doc/lispref/variables.texi (Variables with Restricted Values)
(Generalized Variables): Index subject of sections.
doc/lispref/text.texi (Buffer Contents, Examining Properties)
(Changing Properties, Property Search, Substitution): Index
subject of sections.
doc/lispref/syntax.texi (Motion and Syntax, Parsing Expressions)
(Motion via Parsing, Position Parse, Control Parsing): Index
subject of sections.
doc/lispref/strings.texi (Predicates for Strings, Creating Strings)
(Modifying Strings, Text Comparison): Index subject of sections.
doc/lispref/searching.texi (Syntax of Regexps, Regexp Special)
(Regexp Functions, Regexp Functions): Index subject of sections.
doc/lispref/processes.texi (Subprocess Creation, Process Information): Index
subject of sections.
doc/lispref/positions.texi (Screen Lines): Index subject of sections.
doc/lispref/nonascii.texi (Scanning Charsets, Specifying Coding Systems):
Index subject of sections.
doc/lispref/minibuf.texi (Text from Minibuffer, Object from Minibuffer)
(Multiple Queries, Minibuffer Contents): Index subject of
sections.
doc/lispref/markers.texi (Predicates on Markers, Creating Markers)
(Information from Markers, Moving Markers): Index subject of
sections.
doc/lispref/macros.texi (Defining Macros, Problems with Macros): Index
subject of sections.
doc/lispref/loading.texi (Loading Non-ASCII, Where Defined): Index subject
of sections.
doc/lispref/lists.texi (List-related Predicates, List Variables, Setcar)
(Setcdr, Plist Access): Index subject of sections.
doc/lispref/keymaps.texi (Controlling Active Maps, Scanning Keymaps)
(Modifying Menus): Index subject of sections.
doc/lispref/help.texi (Accessing Documentation, Help Functions): Index
subject of sections.
doc/lispref/hash.texi (Hash Access): Index subject of sections.
doc/lispref/functions.texi (Core Advising Primitives)
(Advising Named Functions, Porting old advices): Index subject of
sections.
doc/lispref/frames.texi (Creating Frames, Initial Parameters)
(Position Parameters, Buffer Parameters, Minibuffers and Frames)
(Pop-Up Menus, Drag and Drop): Index subject of sections.
doc/lispref/files.texi (Visiting Functions, Kinds of Files)
(Unique File Names): Index subject of sections.
doc/lispref/display.texi (Refresh Screen, Echo Area Customization)
(Warning Variables, Warning Options, Delayed Warnings)
(Temporary Displays, Managing Overlays, Overlay Properties)
(Finding Overlays, Size of Displayed Text, Defining Faces)
(Attribute Functions, Displaying Faces, Face Remapping)
(Basic Faces, Font Lookup, Fontsets, Replacing Specs)
(Defining Images, Showing Images): Index subject of sections.
doc/lispref/debugging.texi (Debugging, Explicit Debug)
(Invoking the Debugger, Excess Open, Excess Close): Index subject
of sections.
doc/lispref/customize.texi (Defining New Types, Applying Customizations)
(Custom Themes): Index subject of sections.
doc/lispref/control.texi (Sequencing, Combining Conditions)
(Processing of Errors, Cleanups): Index subject of sections.
doc/lispref/compile.texi (Eval During Compile): Index subject of sections.
doc/lispref/commands.texi (Using Interactive, Distinguish Interactive)
(Command Loop Info, Classifying Events, Event Mod)
(Invoking the Input Method): Index subject of sections.
doc/lispref/buffers.texi (Buffer List, Buffer Gap): Index subject of sections.
doc/lispref/backups.texi (Making Backups, Numbered Backups, Backup Names)
(Reverting): Index subject of sections.
doc/lispref/abbrevs.texi (Abbrev Tables, Defining Abbrevs, Abbrev Files)
(Abbrev Expansion, Standard Abbrev Tables, Abbrev Properties)
(Abbrev Table Properties): Index subject of sections.
doc/lispref/os.texi (Time of Day, Time Conversion, Time Parsing)
(Time Calculations, Idle Timers): Index subject of sections.
2014-12-23 20:42:30 +02:00
|
|
|
@cindex replace part of list
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
The lowest-level primitive for modifying a @sc{cdr} is @code{setcdr}:
|
|
|
|
|
|
|
|
@defun setcdr cons object
|
|
|
|
This function stores @var{object} as the new @sc{cdr} of @var{cons},
|
|
|
|
replacing its previous @sc{cdr}. In other words, it changes the
|
|
|
|
@sc{cdr} slot of @var{cons} to refer to @var{object}. It returns the
|
|
|
|
value @var{object}.
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
Here is an example of replacing the @sc{cdr} of a list with a
|
|
|
|
different list. All but the first element of the list are removed in
|
|
|
|
favor of a different sequence of elements. The first element is
|
|
|
|
unchanged, because it resides in the @sc{car} of the list, and is not
|
|
|
|
reached via the @sc{cdr}.
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
2020-05-16 17:17:00 -07:00
|
|
|
(setq x (list 1 2 3))
|
2007-09-06 04:25:08 +00:00
|
|
|
@result{} (1 2 3)
|
|
|
|
@end group
|
|
|
|
@group
|
2020-05-16 17:17:00 -07:00
|
|
|
(setcdr x '(4))
|
2007-09-06 04:25:08 +00:00
|
|
|
@result{} (4)
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
x
|
|
|
|
@result{} (1 4)
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
You can delete elements from the middle of a list by altering the
|
|
|
|
@sc{cdr}s of the cons cells in the list. For example, here we delete
|
|
|
|
the second element, @code{b}, from the list @code{(a b c)}, by changing
|
|
|
|
the @sc{cdr} of the first cons cell:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
Document constant vs mutable objects better
This patch builds on a suggested patch by Mattias Engdegård
and on further comments by Eli Zaretskii.
Original bug report by Kevin Vigouroux (Bug#40671).
* doc/lispintro/emacs-lisp-intro.texi (set & setq, Review)
(setcar, Lists diagrammed, Mail Aliases, Indent Tabs Mode):
setq is a special form, not a function or command.
* doc/lispintro/emacs-lisp-intro.texi (setcar):
* doc/lispref/lists.texi (Modifying Lists, Rearrangement):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions, Vectors):
* doc/lispref/strings.texi (String Basics, Modifying Strings):
Mention mutable vs constant objects.
* doc/lispintro/emacs-lisp-intro.texi (setcar, setcdr)
(kill-new function, cons & search-fwd Review):
* doc/lispref/edebug.texi (Printing in Edebug):
* doc/lispref/keymaps.texi (Changing Key Bindings):
* doc/lispref/lists.texi (Setcar, Setcdr, Rearrangement)
(Sets And Lists, Association Lists, Plist Access):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions):
* doc/lispref/strings.texi (Text Comparison):
Fix examples so that they do not try to change constants.
2020-04-18 12:59:17 -07:00
|
|
|
(setq x1 (list 'a 'b 'c))
|
2007-09-06 04:25:08 +00:00
|
|
|
@result{} (a b c)
|
|
|
|
(setcdr x1 (cdr (cdr x1)))
|
|
|
|
@result{} (c)
|
|
|
|
x1
|
|
|
|
@result{} (a c)
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
Here is the result in box notation:
|
|
|
|
|
|
|
|
@smallexample
|
|
|
|
@group
|
|
|
|
--------------------
|
|
|
|
| |
|
|
|
|
-------------- | -------------- | --------------
|
|
|
|
| car | cdr | | | car | cdr | -->| car | cdr |
|
|
|
|
| a | o----- | b | o-------->| c | nil |
|
|
|
|
| | | | | | | | |
|
|
|
|
-------------- -------------- --------------
|
|
|
|
@end group
|
|
|
|
@end smallexample
|
|
|
|
|
|
|
|
@noindent
|
|
|
|
The second cons cell, which previously held the element @code{b}, still
|
|
|
|
exists and its @sc{car} is still @code{b}, but it no longer forms part
|
|
|
|
of this list.
|
|
|
|
|
|
|
|
It is equally easy to insert a new element by changing @sc{cdr}s:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
Document constant vs mutable objects better
This patch builds on a suggested patch by Mattias Engdegård
and on further comments by Eli Zaretskii.
Original bug report by Kevin Vigouroux (Bug#40671).
* doc/lispintro/emacs-lisp-intro.texi (set & setq, Review)
(setcar, Lists diagrammed, Mail Aliases, Indent Tabs Mode):
setq is a special form, not a function or command.
* doc/lispintro/emacs-lisp-intro.texi (setcar):
* doc/lispref/lists.texi (Modifying Lists, Rearrangement):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions, Vectors):
* doc/lispref/strings.texi (String Basics, Modifying Strings):
Mention mutable vs constant objects.
* doc/lispintro/emacs-lisp-intro.texi (setcar, setcdr)
(kill-new function, cons & search-fwd Review):
* doc/lispref/edebug.texi (Printing in Edebug):
* doc/lispref/keymaps.texi (Changing Key Bindings):
* doc/lispref/lists.texi (Setcar, Setcdr, Rearrangement)
(Sets And Lists, Association Lists, Plist Access):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions):
* doc/lispref/strings.texi (Text Comparison):
Fix examples so that they do not try to change constants.
2020-04-18 12:59:17 -07:00
|
|
|
(setq x1 (list 'a 'b 'c))
|
2007-09-06 04:25:08 +00:00
|
|
|
@result{} (a b c)
|
|
|
|
(setcdr x1 (cons 'd (cdr x1)))
|
|
|
|
@result{} (d b c)
|
|
|
|
x1
|
|
|
|
@result{} (a d b c)
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
Here is this result in box notation:
|
|
|
|
|
|
|
|
@smallexample
|
|
|
|
@group
|
|
|
|
-------------- ------------- -------------
|
|
|
|
| car | cdr | | car | cdr | | car | cdr |
|
|
|
|
| a | o | -->| b | o------->| c | nil |
|
|
|
|
| | | | | | | | | | |
|
|
|
|
--------- | -- | ------------- -------------
|
|
|
|
| |
|
|
|
|
----- --------
|
|
|
|
| |
|
|
|
|
| --------------- |
|
|
|
|
| | car | cdr | |
|
|
|
|
-->| d | o------
|
|
|
|
| | |
|
|
|
|
---------------
|
|
|
|
@end group
|
|
|
|
@end smallexample
|
|
|
|
|
|
|
|
@node Rearrangement
|
|
|
|
@subsection Functions that Rearrange Lists
|
|
|
|
@cindex rearrangement of lists
|
2013-11-05 19:47:48 +02:00
|
|
|
@cindex reordering, of elements in lists
|
2007-09-06 04:25:08 +00:00
|
|
|
@cindex modification of lists
|
|
|
|
|
2015-09-15 08:46:48 -07:00
|
|
|
Here are some functions that rearrange lists destructively by
|
|
|
|
modifying the @sc{cdr}s of their component cons cells. These functions
|
|
|
|
are destructive because they chew up the original lists passed
|
2007-09-06 04:25:08 +00:00
|
|
|
to them as arguments, relinking their cons cells to form a new list that
|
|
|
|
is the returned value.
|
|
|
|
|
|
|
|
@ifnottex
|
|
|
|
See @code{delq}, in @ref{Sets And Lists}, for another function
|
|
|
|
that modifies cons cells.
|
|
|
|
@end ifnottex
|
|
|
|
@iftex
|
|
|
|
The function @code{delq} in the following section is another example
|
|
|
|
of destructive list manipulation.
|
|
|
|
@end iftex
|
|
|
|
|
|
|
|
@defun nconc &rest lists
|
|
|
|
@cindex concatenating lists
|
|
|
|
@cindex joining lists
|
|
|
|
This function returns a list containing all the elements of @var{lists}.
|
|
|
|
Unlike @code{append} (@pxref{Building Lists}), the @var{lists} are
|
|
|
|
@emph{not} copied. Instead, the last @sc{cdr} of each of the
|
|
|
|
@var{lists} is changed to refer to the following list. The last of the
|
|
|
|
@var{lists} is not altered. For example:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
2020-05-16 17:17:00 -07:00
|
|
|
(setq x (list 1 2 3))
|
2007-09-06 04:25:08 +00:00
|
|
|
@result{} (1 2 3)
|
|
|
|
@end group
|
|
|
|
@group
|
2020-05-16 17:17:00 -07:00
|
|
|
(nconc x '(4 5))
|
2007-09-06 04:25:08 +00:00
|
|
|
@result{} (1 2 3 4 5)
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
x
|
|
|
|
@result{} (1 2 3 4 5)
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
Since the last argument of @code{nconc} is not itself modified, it is
|
|
|
|
reasonable to use a constant list, such as @code{'(4 5)}, as in the
|
|
|
|
above example. For the same reason, the last argument need not be a
|
|
|
|
list:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
Document constant vs mutable objects better
This patch builds on a suggested patch by Mattias Engdegård
and on further comments by Eli Zaretskii.
Original bug report by Kevin Vigouroux (Bug#40671).
* doc/lispintro/emacs-lisp-intro.texi (set & setq, Review)
(setcar, Lists diagrammed, Mail Aliases, Indent Tabs Mode):
setq is a special form, not a function or command.
* doc/lispintro/emacs-lisp-intro.texi (setcar):
* doc/lispref/lists.texi (Modifying Lists, Rearrangement):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions, Vectors):
* doc/lispref/strings.texi (String Basics, Modifying Strings):
Mention mutable vs constant objects.
* doc/lispintro/emacs-lisp-intro.texi (setcar, setcdr)
(kill-new function, cons & search-fwd Review):
* doc/lispref/edebug.texi (Printing in Edebug):
* doc/lispref/keymaps.texi (Changing Key Bindings):
* doc/lispref/lists.texi (Setcar, Setcdr, Rearrangement)
(Sets And Lists, Association Lists, Plist Access):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions):
* doc/lispref/strings.texi (Text Comparison):
Fix examples so that they do not try to change constants.
2020-04-18 12:59:17 -07:00
|
|
|
(setq x (list 1 2 3))
|
2007-09-06 04:25:08 +00:00
|
|
|
@result{} (1 2 3)
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(nconc x 'z)
|
|
|
|
@result{} (1 2 3 . z)
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
x
|
|
|
|
@result{} (1 2 3 . z)
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
2021-02-07 14:47:09 +01:00
|
|
|
However, the other arguments (all but the last) should be mutable
|
2023-04-27 13:52:57 +02:00
|
|
|
lists. They can be dotted lists, whose last @sc{cdr}s are then
|
|
|
|
replaced with the next argument:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(nconc (cons 1 2) (cons 3 (cons 4 5)) 'z)
|
|
|
|
@result{} (1 3 4 . z)
|
|
|
|
@end group
|
|
|
|
@end example
|
2021-02-07 14:47:09 +01:00
|
|
|
|
|
|
|
A common pitfall is to use a constant list as a non-last argument to
|
|
|
|
@code{nconc}. If you do this, the resulting behavior is undefined
|
|
|
|
(@pxref{Self-Evaluating Forms}). It is possible that your program
|
|
|
|
will change each time you run it! Here is what might happen (though
|
|
|
|
this is not guaranteed to happen):
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@smallexample
|
|
|
|
@group
|
|
|
|
(defun add-foo (x) ; @r{We want this function to add}
|
|
|
|
(nconc '(foo) x)) ; @r{@code{foo} to the front of its arg.}
|
|
|
|
@end group
|
|
|
|
|
|
|
|
@group
|
|
|
|
(symbol-function 'add-foo)
|
Use a dedicated type to represent interpreted-function values
Change `function` so that when evaluating #'(lambda ...)
we return an object of type `interpreted-function` rather than
a list starting with one of `lambda` or `closure`.
The new type reuses the existing PVEC_CLOSURE (nee PVEC_COMPILED)
tag and tries to align the corresponding elements:
- the arglist, the docstring, and the interactive-form go in the
same slots as for byte-code functions.
- the body of the function goes in the slot used for the bytecode string.
- the lexical context goes in the slot used for the constants of
bytecoded functions.
The first point above means that `help-function-arglist`,
`documentation`, and `interactive-form`s don't need to
distinguish interpreted and bytecode functions any more.
Main benefits of the change:
- We can now reliably distinguish a list from a function value.
- `cl-defmethod` can dispatch on `interactive-function` and `closure`.
Dispatch on `function` also works now for interpreted functions but still
won't work for functions represented as lists or as symbols, of course.
- Function values are now self-evaluating. That was alrready the case
when byte-compiled, but not when interpreted since
(eval '(closure ...)) signals a void-function error.
That also avoids false-positive warnings about "don't quote your lambdas"
when doing things like `(mapcar ',func ...)`.
* src/eval.c (Fmake_interpreted_closure): New function.
(Ffunction): Use it and change calling convention of
`Vinternal_make_interpreted_closure_function`.
(FUNCTIONP, Fcommandp, eval_sub, funcall_general, funcall_lambda)
(Ffunc_arity, lambda_arity): Simplify.
(funcall_lambda): Adjust to new representation.
(syms_of_eval): `defsubr` the new function. Remove definition of `Qclosure`.
* lisp/emacs-lisp/cconv.el (cconv-make-interpreted-closure):
Change calling convention and use `make-interpreted-closure`.
* src/data.c (Fcl_type_of): Distinguish `byte-code-function`s from
`interpreted-function`s.
(Fclosurep, finterpreted_function_p): New functions.
(Fbyte_code_function_p): Don't be confused by `interpreted-function`s.
(Finteractive_form, Fcommand_modes): Simplify.
(syms_of_data): Define new type symbols and `defsubr` the two
new functions.
* lisp/emacs-lisp/cl-print.el (cl-print-object) <interpreted-function>:
New method.
* lisp/emacs-lisp/oclosure.el (oclosure): Refine the parent
to be `closure`.
(oclosure--fix-type, oclosure-type): Simplify.
(oclosure--copy, oclosure--get, oclosure--set): Adjust to
new representation.
* src/callint.c (Fcall_interactively): Adjust to new representation.
* src/lread.c (bytecode_from_rev_list):
* lisp/simple.el (function-documentation):
* lisp/help.el (help-function-arglist): Remove the old `closure` case
and adjust the byte-code case so it handles `interpreted-function`s.
* lisp/emacs-lisp/cl-preloaded.el (closure): New type.
(byte-code-function): Add it as a parent.
(interpreted-function): Adjust parent (the type itself was already
added earlier by accident).
* lisp/emacs-lisp/bytecomp.el (byte-compile--reify-function): Adjust to
new representation.
(byte-compile): Use `interpreted-function-p`.
* lisp/emacs-lisp/byte-opt.el (byte-compile-inline-expand): Adjust to
new representation.
(side-effect-free-fns): Add `interpreted-function-p` and `closurep`.
* src/profiler.c (trace_hash, ffunction_equal): Simplify.
* lisp/profiler.el (profiler-function-equal): Simplify.
* lisp/emacs-lisp/nadvice.el (advice--interactive-form-1):
Use `interpreted-function-p`; adjust to new representation; and take
advantage of the fact that function values are now self-evaluating.
* lisp/emacs-lisp/lisp-mode.el (closure):
Remove `lisp-indent-function` property.
* lisp/emacs-lisp/disass.el (disassemble-internal): Adjust to
new representation.
* lisp/emacs-lisp/edebug.el (edebug--strip-instrumentation):
Use `interpreted-function-p`.
* lisp/emacs-lisp/comp-common.el (comp-known-type-specifiers):
Add `closurep` and `interpreted-function-p`.
* test/lisp/help-fns-tests.el (help-fns-test-lisp-defun): Adjust to
more precise type info in `describe-function`.
* test/lisp/erc/resources/erc-d/erc-d-tests.el (erc-d--render-entries):
Use `interpreted-function-p`.
* test/lisp/emacs-lisp/macroexp-resources/vk.el (vk-f4, vk-f5):
Don't hardcode function values.
* doc/lispref/functions.texi (Anonymous Functions): Don't suggest that
function values are lists. Reword "self-quoting" to reflect the
fact that #' doesn't return the exact same object. Update examples
with the new shape of the return value.
* doc/lispref/variables.texi (Lexical Binding):
* doc/lispref/lists.texi (Rearrangement):
* doc/lispref/control.texi (Handling Errors): Update examples to reflect
new representation of function values.
2024-03-11 16:12:26 -04:00
|
|
|
@result{} #f(lambda (x) [t] (nconc '(foo) x))
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
|
|
|
|
@group
|
|
|
|
(setq xx (add-foo '(1 2))) ; @r{It seems to work.}
|
|
|
|
@result{} (foo 1 2)
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(setq xy (add-foo '(3 4))) ; @r{What happened?}
|
|
|
|
@result{} (foo 1 2 3 4)
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(eq xx xy)
|
|
|
|
@result{} t
|
|
|
|
@end group
|
|
|
|
|
|
|
|
@group
|
|
|
|
(symbol-function 'add-foo)
|
Use a dedicated type to represent interpreted-function values
Change `function` so that when evaluating #'(lambda ...)
we return an object of type `interpreted-function` rather than
a list starting with one of `lambda` or `closure`.
The new type reuses the existing PVEC_CLOSURE (nee PVEC_COMPILED)
tag and tries to align the corresponding elements:
- the arglist, the docstring, and the interactive-form go in the
same slots as for byte-code functions.
- the body of the function goes in the slot used for the bytecode string.
- the lexical context goes in the slot used for the constants of
bytecoded functions.
The first point above means that `help-function-arglist`,
`documentation`, and `interactive-form`s don't need to
distinguish interpreted and bytecode functions any more.
Main benefits of the change:
- We can now reliably distinguish a list from a function value.
- `cl-defmethod` can dispatch on `interactive-function` and `closure`.
Dispatch on `function` also works now for interpreted functions but still
won't work for functions represented as lists or as symbols, of course.
- Function values are now self-evaluating. That was alrready the case
when byte-compiled, but not when interpreted since
(eval '(closure ...)) signals a void-function error.
That also avoids false-positive warnings about "don't quote your lambdas"
when doing things like `(mapcar ',func ...)`.
* src/eval.c (Fmake_interpreted_closure): New function.
(Ffunction): Use it and change calling convention of
`Vinternal_make_interpreted_closure_function`.
(FUNCTIONP, Fcommandp, eval_sub, funcall_general, funcall_lambda)
(Ffunc_arity, lambda_arity): Simplify.
(funcall_lambda): Adjust to new representation.
(syms_of_eval): `defsubr` the new function. Remove definition of `Qclosure`.
* lisp/emacs-lisp/cconv.el (cconv-make-interpreted-closure):
Change calling convention and use `make-interpreted-closure`.
* src/data.c (Fcl_type_of): Distinguish `byte-code-function`s from
`interpreted-function`s.
(Fclosurep, finterpreted_function_p): New functions.
(Fbyte_code_function_p): Don't be confused by `interpreted-function`s.
(Finteractive_form, Fcommand_modes): Simplify.
(syms_of_data): Define new type symbols and `defsubr` the two
new functions.
* lisp/emacs-lisp/cl-print.el (cl-print-object) <interpreted-function>:
New method.
* lisp/emacs-lisp/oclosure.el (oclosure): Refine the parent
to be `closure`.
(oclosure--fix-type, oclosure-type): Simplify.
(oclosure--copy, oclosure--get, oclosure--set): Adjust to
new representation.
* src/callint.c (Fcall_interactively): Adjust to new representation.
* src/lread.c (bytecode_from_rev_list):
* lisp/simple.el (function-documentation):
* lisp/help.el (help-function-arglist): Remove the old `closure` case
and adjust the byte-code case so it handles `interpreted-function`s.
* lisp/emacs-lisp/cl-preloaded.el (closure): New type.
(byte-code-function): Add it as a parent.
(interpreted-function): Adjust parent (the type itself was already
added earlier by accident).
* lisp/emacs-lisp/bytecomp.el (byte-compile--reify-function): Adjust to
new representation.
(byte-compile): Use `interpreted-function-p`.
* lisp/emacs-lisp/byte-opt.el (byte-compile-inline-expand): Adjust to
new representation.
(side-effect-free-fns): Add `interpreted-function-p` and `closurep`.
* src/profiler.c (trace_hash, ffunction_equal): Simplify.
* lisp/profiler.el (profiler-function-equal): Simplify.
* lisp/emacs-lisp/nadvice.el (advice--interactive-form-1):
Use `interpreted-function-p`; adjust to new representation; and take
advantage of the fact that function values are now self-evaluating.
* lisp/emacs-lisp/lisp-mode.el (closure):
Remove `lisp-indent-function` property.
* lisp/emacs-lisp/disass.el (disassemble-internal): Adjust to
new representation.
* lisp/emacs-lisp/edebug.el (edebug--strip-instrumentation):
Use `interpreted-function-p`.
* lisp/emacs-lisp/comp-common.el (comp-known-type-specifiers):
Add `closurep` and `interpreted-function-p`.
* test/lisp/help-fns-tests.el (help-fns-test-lisp-defun): Adjust to
more precise type info in `describe-function`.
* test/lisp/erc/resources/erc-d/erc-d-tests.el (erc-d--render-entries):
Use `interpreted-function-p`.
* test/lisp/emacs-lisp/macroexp-resources/vk.el (vk-f4, vk-f5):
Don't hardcode function values.
* doc/lispref/functions.texi (Anonymous Functions): Don't suggest that
function values are lists. Reword "self-quoting" to reflect the
fact that #' doesn't return the exact same object. Update examples
with the new shape of the return value.
* doc/lispref/variables.texi (Lexical Binding):
* doc/lispref/lists.texi (Rearrangement):
* doc/lispref/control.texi (Handling Errors): Update examples to reflect
new representation of function values.
2024-03-11 16:12:26 -04:00
|
|
|
@result{} #f(lambda (x) [t] (nconc '(foo 1 2 3 4) x))
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
@end smallexample
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@node Sets And Lists
|
|
|
|
@section Using Lists as Sets
|
|
|
|
@cindex lists as sets
|
|
|
|
@cindex sets
|
|
|
|
|
2021-10-05 09:11:33 +02:00
|
|
|
A list can represent an unordered mathematical set---simply consider
|
|
|
|
a value an element of a set if it appears in the list, and ignore the
|
|
|
|
order of the list. To form the union of two sets, use @code{append}
|
|
|
|
(as long as you don't mind having duplicate elements). You can remove
|
|
|
|
@code{equal} duplicates using @code{delete-dups} or @code{seq-uniq}.
|
|
|
|
Other useful functions for sets include @code{memq} and @code{delq},
|
|
|
|
and their @code{equal} versions, @code{member} and @code{delete}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@cindex CL note---lack @code{union}, @code{intersection}
|
|
|
|
@quotation
|
|
|
|
@b{Common Lisp note:} Common Lisp has functions @code{union} (which
|
2011-05-18 23:59:50 -07:00
|
|
|
avoids duplicate elements) and @code{intersection} for set operations.
|
2019-11-08 11:55:36 +02:00
|
|
|
In Emacs Lisp, variants of these facilities are provided by the
|
|
|
|
@file{cl-lib} library. @xref{Lists as Sets,,,cl,Common Lisp Extensions}.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end quotation
|
|
|
|
|
|
|
|
@defun memq object list
|
|
|
|
@cindex membership in a list
|
|
|
|
This function tests to see whether @var{object} is a member of
|
|
|
|
@var{list}. If it is, @code{memq} returns a list starting with the
|
|
|
|
first occurrence of @var{object}. Otherwise, it returns @code{nil}.
|
|
|
|
The letter @samp{q} in @code{memq} says that it uses @code{eq} to
|
|
|
|
compare @var{object} against the elements of the list. For example:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(memq 'b '(a b c b a))
|
|
|
|
@result{} (b c b a)
|
|
|
|
@end group
|
|
|
|
@group
|
2020-05-02 13:48:21 -07:00
|
|
|
(memq '(2) '((1) (2))) ; @r{The two @code{(2)}s need not be @code{eq}.}
|
|
|
|
@result{} @r{Unspecified; might be @code{nil} or @code{((2))}.}
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun delq object list
|
|
|
|
@cindex deleting list elements
|
|
|
|
This function destructively removes all elements @code{eq} to
|
2012-09-09 15:50:45 +08:00
|
|
|
@var{object} from @var{list}, and returns the resulting list. The
|
|
|
|
letter @samp{q} in @code{delq} says that it uses @code{eq} to compare
|
|
|
|
@var{object} against the elements of the list, like @code{memq} and
|
|
|
|
@code{remq}.
|
|
|
|
|
|
|
|
Typically, when you invoke @code{delq}, you should use the return
|
|
|
|
value by assigning it to the variable which held the original list.
|
|
|
|
The reason for this is explained below.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
2012-09-09 15:50:45 +08:00
|
|
|
The @code{delq} function deletes elements from the front of the list
|
|
|
|
by simply advancing down the list, and returning a sublist that starts
|
|
|
|
after those elements. For example:
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
2020-04-19 13:22:10 -07:00
|
|
|
(delq 'a '(a b c)) @equiv{} (cdr '(a b c))
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
2012-09-09 15:50:45 +08:00
|
|
|
@noindent
|
2007-09-06 04:25:08 +00:00
|
|
|
When an element to be deleted appears in the middle of the list,
|
|
|
|
removing it involves changing the @sc{cdr}s (@pxref{Setcdr}).
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
Document constant vs mutable objects better
This patch builds on a suggested patch by Mattias Engdegård
and on further comments by Eli Zaretskii.
Original bug report by Kevin Vigouroux (Bug#40671).
* doc/lispintro/emacs-lisp-intro.texi (set & setq, Review)
(setcar, Lists diagrammed, Mail Aliases, Indent Tabs Mode):
setq is a special form, not a function or command.
* doc/lispintro/emacs-lisp-intro.texi (setcar):
* doc/lispref/lists.texi (Modifying Lists, Rearrangement):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions, Vectors):
* doc/lispref/strings.texi (String Basics, Modifying Strings):
Mention mutable vs constant objects.
* doc/lispintro/emacs-lisp-intro.texi (setcar, setcdr)
(kill-new function, cons & search-fwd Review):
* doc/lispref/edebug.texi (Printing in Edebug):
* doc/lispref/keymaps.texi (Changing Key Bindings):
* doc/lispref/lists.texi (Setcar, Setcdr, Rearrangement)
(Sets And Lists, Association Lists, Plist Access):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions):
* doc/lispref/strings.texi (Text Comparison):
Fix examples so that they do not try to change constants.
2020-04-18 12:59:17 -07:00
|
|
|
(setq sample-list (list 'a 'b 'c '(4)))
|
2007-09-06 04:25:08 +00:00
|
|
|
@result{} (a b c (4))
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(delq 'a sample-list)
|
|
|
|
@result{} (b c (4))
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
sample-list
|
|
|
|
@result{} (a b c (4))
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(delq 'c sample-list)
|
|
|
|
@result{} (a b (4))
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
sample-list
|
|
|
|
@result{} (a b (4))
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
Note that @code{(delq 'c sample-list)} modifies @code{sample-list} to
|
|
|
|
splice out the third element, but @code{(delq 'a sample-list)} does not
|
|
|
|
splice anything---it just returns a shorter list. Don't assume that a
|
|
|
|
variable which formerly held the argument @var{list} now has fewer
|
|
|
|
elements, or that it still holds the original list! Instead, save the
|
|
|
|
result of @code{delq} and use that. Most often we store the result back
|
|
|
|
into the variable that held the original list:
|
|
|
|
|
|
|
|
@example
|
|
|
|
(setq flowers (delq 'rose flowers))
|
|
|
|
@end example
|
|
|
|
|
Document constant vs mutable objects better
This patch builds on a suggested patch by Mattias Engdegård
and on further comments by Eli Zaretskii.
Original bug report by Kevin Vigouroux (Bug#40671).
* doc/lispintro/emacs-lisp-intro.texi (set & setq, Review)
(setcar, Lists diagrammed, Mail Aliases, Indent Tabs Mode):
setq is a special form, not a function or command.
* doc/lispintro/emacs-lisp-intro.texi (setcar):
* doc/lispref/lists.texi (Modifying Lists, Rearrangement):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions, Vectors):
* doc/lispref/strings.texi (String Basics, Modifying Strings):
Mention mutable vs constant objects.
* doc/lispintro/emacs-lisp-intro.texi (setcar, setcdr)
(kill-new function, cons & search-fwd Review):
* doc/lispref/edebug.texi (Printing in Edebug):
* doc/lispref/keymaps.texi (Changing Key Bindings):
* doc/lispref/lists.texi (Setcar, Setcdr, Rearrangement)
(Sets And Lists, Association Lists, Plist Access):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions):
* doc/lispref/strings.texi (Text Comparison):
Fix examples so that they do not try to change constants.
2020-04-18 12:59:17 -07:00
|
|
|
In the following example, the @code{(list 4)} that @code{delq} attempts to match
|
|
|
|
and the @code{(4)} in the @code{sample-list} are @code{equal} but not @code{eq}:
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
Document constant vs mutable objects better
This patch builds on a suggested patch by Mattias Engdegård
and on further comments by Eli Zaretskii.
Original bug report by Kevin Vigouroux (Bug#40671).
* doc/lispintro/emacs-lisp-intro.texi (set & setq, Review)
(setcar, Lists diagrammed, Mail Aliases, Indent Tabs Mode):
setq is a special form, not a function or command.
* doc/lispintro/emacs-lisp-intro.texi (setcar):
* doc/lispref/lists.texi (Modifying Lists, Rearrangement):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions, Vectors):
* doc/lispref/strings.texi (String Basics, Modifying Strings):
Mention mutable vs constant objects.
* doc/lispintro/emacs-lisp-intro.texi (setcar, setcdr)
(kill-new function, cons & search-fwd Review):
* doc/lispref/edebug.texi (Printing in Edebug):
* doc/lispref/keymaps.texi (Changing Key Bindings):
* doc/lispref/lists.texi (Setcar, Setcdr, Rearrangement)
(Sets And Lists, Association Lists, Plist Access):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions):
* doc/lispref/strings.texi (Text Comparison):
Fix examples so that they do not try to change constants.
2020-04-18 12:59:17 -07:00
|
|
|
(delq (list 4) sample-list)
|
2007-09-06 04:25:08 +00:00
|
|
|
@result{} (a c (4))
|
|
|
|
@end group
|
2011-05-29 13:18:46 -04:00
|
|
|
@end example
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
If you want to delete elements that are @code{equal} to a given value,
|
|
|
|
use @code{delete} (see below).
|
|
|
|
|
|
|
|
@defun remq object list
|
|
|
|
This function returns a copy of @var{list}, with all elements removed
|
|
|
|
which are @code{eq} to @var{object}. The letter @samp{q} in @code{remq}
|
|
|
|
says that it uses @code{eq} to compare @var{object} against the elements
|
|
|
|
of @code{list}.
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
Document constant vs mutable objects better
This patch builds on a suggested patch by Mattias Engdegård
and on further comments by Eli Zaretskii.
Original bug report by Kevin Vigouroux (Bug#40671).
* doc/lispintro/emacs-lisp-intro.texi (set & setq, Review)
(setcar, Lists diagrammed, Mail Aliases, Indent Tabs Mode):
setq is a special form, not a function or command.
* doc/lispintro/emacs-lisp-intro.texi (setcar):
* doc/lispref/lists.texi (Modifying Lists, Rearrangement):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions, Vectors):
* doc/lispref/strings.texi (String Basics, Modifying Strings):
Mention mutable vs constant objects.
* doc/lispintro/emacs-lisp-intro.texi (setcar, setcdr)
(kill-new function, cons & search-fwd Review):
* doc/lispref/edebug.texi (Printing in Edebug):
* doc/lispref/keymaps.texi (Changing Key Bindings):
* doc/lispref/lists.texi (Setcar, Setcdr, Rearrangement)
(Sets And Lists, Association Lists, Plist Access):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions):
* doc/lispref/strings.texi (Text Comparison):
Fix examples so that they do not try to change constants.
2020-04-18 12:59:17 -07:00
|
|
|
(setq sample-list (list 'a 'b 'c 'a 'b 'c))
|
2007-09-06 04:25:08 +00:00
|
|
|
@result{} (a b c a b c)
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(remq 'a sample-list)
|
|
|
|
@result{} (b c b c)
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
sample-list
|
|
|
|
@result{} (a b c a b c)
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun memql object list
|
|
|
|
The function @code{memql} tests to see whether @var{object} is a member
|
|
|
|
of @var{list}, comparing members with @var{object} using @code{eql},
|
Style fixes for floating-point doc.
* commands.texi, customize.texi, display.texi, elisp.texi, files.texi:
* frames.texi, hash.texi, internals.texi, keymaps.texi, lists.texi:
* minibuf.texi, nonascii.texi, numbers.texi, objects.texi, os.texi:
* processes.texi, streams.texi, strings.texi, text.texi:
* variables.texi, windows.texi:
Hyphenate "floating-point" iff it precedes a noun.
Reword to avoid nouns and hyphenation when that's easy.
Prefer "integer" to "integer number" and "is floating point"
to "is a floating point number".
Prefer "@minus{}" to "-" when it's a minus.
2014-03-17 18:19:03 -07:00
|
|
|
so floating-point elements are compared by value.
|
2007-09-06 04:25:08 +00:00
|
|
|
If @var{object} is a member, @code{memql} returns a list starting with
|
|
|
|
its first occurrence in @var{list}. Otherwise, it returns @code{nil}.
|
|
|
|
|
|
|
|
Compare this with @code{memq}:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
2020-05-02 13:48:21 -07:00
|
|
|
(memql 1.2 '(1.1 1.2 1.3)) ; @r{@code{1.2} and @code{1.2} are @code{eql}.}
|
2007-09-06 04:25:08 +00:00
|
|
|
@result{} (1.2 1.3)
|
|
|
|
@end group
|
|
|
|
@group
|
2020-05-02 13:48:21 -07:00
|
|
|
(memq 1.2 '(1.1 1.2 1.3)) ; @r{The two @code{1.2}s need not be @code{eq}.}
|
|
|
|
@result{} @r{Unspecified; might be @code{nil} or @code{(1.2 1.3)}.}
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
The following three functions are like @code{memq}, @code{delq} and
|
|
|
|
@code{remq}, but use @code{equal} rather than @code{eq} to compare
|
|
|
|
elements. @xref{Equality Predicates}.
|
|
|
|
|
|
|
|
@defun member object list
|
|
|
|
The function @code{member} tests to see whether @var{object} is a member
|
|
|
|
of @var{list}, comparing members with @var{object} using @code{equal}.
|
|
|
|
If @var{object} is a member, @code{member} returns a list starting with
|
|
|
|
its first occurrence in @var{list}. Otherwise, it returns @code{nil}.
|
|
|
|
|
|
|
|
Compare this with @code{memq}:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
2020-05-02 13:48:21 -07:00
|
|
|
(member '(2) '((1) (2))) ; @r{@code{(2)} and @code{(2)} are @code{equal}.}
|
2007-09-06 04:25:08 +00:00
|
|
|
@result{} ((2))
|
|
|
|
@end group
|
|
|
|
@group
|
2020-05-02 13:48:21 -07:00
|
|
|
(memq '(2) '((1) (2))) ; @r{The two @code{(2)}s need not be @code{eq}.}
|
|
|
|
@result{} @r{Unspecified; might be @code{nil} or @code{(2)}.}
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
;; @r{Two strings with the same contents are @code{equal}.}
|
|
|
|
(member "foo" '("foo" "bar"))
|
|
|
|
@result{} ("foo" "bar")
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun delete object sequence
|
2012-09-09 15:50:45 +08:00
|
|
|
This function removes all elements @code{equal} to @var{object} from
|
|
|
|
@var{sequence}, and returns the resulting sequence.
|
|
|
|
|
|
|
|
If @var{sequence} is a list, @code{delete} is to @code{delq} as
|
|
|
|
@code{member} is to @code{memq}: it uses @code{equal} to compare
|
|
|
|
elements with @var{object}, like @code{member}; when it finds an
|
|
|
|
element that matches, it cuts the element out just as @code{delq}
|
|
|
|
would. As with @code{delq}, you should typically use the return value
|
|
|
|
by assigning it to the variable which held the original list.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
If @code{sequence} is a vector or string, @code{delete} returns a copy
|
|
|
|
of @code{sequence} with all elements @code{equal} to @code{object}
|
|
|
|
removed.
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
Document constant vs mutable objects better
This patch builds on a suggested patch by Mattias Engdegård
and on further comments by Eli Zaretskii.
Original bug report by Kevin Vigouroux (Bug#40671).
* doc/lispintro/emacs-lisp-intro.texi (set & setq, Review)
(setcar, Lists diagrammed, Mail Aliases, Indent Tabs Mode):
setq is a special form, not a function or command.
* doc/lispintro/emacs-lisp-intro.texi (setcar):
* doc/lispref/lists.texi (Modifying Lists, Rearrangement):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions, Vectors):
* doc/lispref/strings.texi (String Basics, Modifying Strings):
Mention mutable vs constant objects.
* doc/lispintro/emacs-lisp-intro.texi (setcar, setcdr)
(kill-new function, cons & search-fwd Review):
* doc/lispref/edebug.texi (Printing in Edebug):
* doc/lispref/keymaps.texi (Changing Key Bindings):
* doc/lispref/lists.texi (Setcar, Setcdr, Rearrangement)
(Sets And Lists, Association Lists, Plist Access):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions):
* doc/lispref/strings.texi (Text Comparison):
Fix examples so that they do not try to change constants.
2020-04-18 12:59:17 -07:00
|
|
|
(setq l (list '(2) '(1) '(2)))
|
2007-09-06 04:25:08 +00:00
|
|
|
(delete '(2) l)
|
|
|
|
@result{} ((1))
|
|
|
|
l
|
|
|
|
@result{} ((2) (1))
|
|
|
|
;; @r{If you want to change @code{l} reliably,}
|
2011-09-13 23:52:54 -07:00
|
|
|
;; @r{write @code{(setq l (delete '(2) l))}.}
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
@group
|
Document constant vs mutable objects better
This patch builds on a suggested patch by Mattias Engdegård
and on further comments by Eli Zaretskii.
Original bug report by Kevin Vigouroux (Bug#40671).
* doc/lispintro/emacs-lisp-intro.texi (set & setq, Review)
(setcar, Lists diagrammed, Mail Aliases, Indent Tabs Mode):
setq is a special form, not a function or command.
* doc/lispintro/emacs-lisp-intro.texi (setcar):
* doc/lispref/lists.texi (Modifying Lists, Rearrangement):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions, Vectors):
* doc/lispref/strings.texi (String Basics, Modifying Strings):
Mention mutable vs constant objects.
* doc/lispintro/emacs-lisp-intro.texi (setcar, setcdr)
(kill-new function, cons & search-fwd Review):
* doc/lispref/edebug.texi (Printing in Edebug):
* doc/lispref/keymaps.texi (Changing Key Bindings):
* doc/lispref/lists.texi (Setcar, Setcdr, Rearrangement)
(Sets And Lists, Association Lists, Plist Access):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions):
* doc/lispref/strings.texi (Text Comparison):
Fix examples so that they do not try to change constants.
2020-04-18 12:59:17 -07:00
|
|
|
(setq l (list '(2) '(1) '(2)))
|
2007-09-06 04:25:08 +00:00
|
|
|
(delete '(1) l)
|
|
|
|
@result{} ((2) (2))
|
|
|
|
l
|
|
|
|
@result{} ((2) (2))
|
|
|
|
;; @r{In this case, it makes no difference whether you set @code{l},}
|
|
|
|
;; @r{but you should do so for the sake of the other case.}
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(delete '(2) [(2) (1) (2)])
|
|
|
|
@result{} [(1)]
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun remove object sequence
|
|
|
|
This function is the non-destructive counterpart of @code{delete}. It
|
|
|
|
returns a copy of @code{sequence}, a list, vector, or string, with
|
|
|
|
elements @code{equal} to @code{object} removed. For example:
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(remove '(2) '((2) (1) (2)))
|
|
|
|
@result{} ((1))
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(remove '(2) [(2) (1) (2)])
|
|
|
|
@result{} [(1)]
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@quotation
|
|
|
|
@b{Common Lisp note:} The functions @code{member}, @code{delete} and
|
|
|
|
@code{remove} in GNU Emacs Lisp are derived from Maclisp, not Common
|
|
|
|
Lisp. The Common Lisp versions do not use @code{equal} to compare
|
|
|
|
elements.
|
|
|
|
@end quotation
|
|
|
|
|
|
|
|
@defun member-ignore-case object list
|
|
|
|
This function is like @code{member}, except that @var{object} should
|
|
|
|
be a string and that it ignores differences in letter-case and text
|
|
|
|
representation: upper-case and lower-case letters are treated as
|
|
|
|
equal, and unibyte strings are converted to multibyte prior to
|
|
|
|
comparison.
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun delete-dups list
|
|
|
|
This function destructively removes all @code{equal} duplicates from
|
|
|
|
@var{list}, stores the result in @var{list} and returns it. Of
|
|
|
|
several @code{equal} occurrences of an element in @var{list},
|
2021-10-05 09:11:33 +02:00
|
|
|
@code{delete-dups} keeps the first one. See @code{seq-uniq} for
|
|
|
|
non-destructive operation (@pxref{Sequence Functions}).
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
|
|
|
See also the function @code{add-to-list}, in @ref{List Variables},
|
|
|
|
for a way to add an element to a list stored in a variable and used as a
|
|
|
|
set.
|
|
|
|
|
|
|
|
@node Association Lists
|
|
|
|
@section Association Lists
|
|
|
|
@cindex association list
|
|
|
|
@cindex alist
|
|
|
|
|
|
|
|
An @dfn{association list}, or @dfn{alist} for short, records a mapping
|
|
|
|
from keys to values. It is a list of cons cells called
|
|
|
|
@dfn{associations}: the @sc{car} of each cons cell is the @dfn{key}, and the
|
|
|
|
@sc{cdr} is the @dfn{associated value}.@footnote{This usage of ``key''
|
|
|
|
is not related to the term ``key sequence''; it means a value used to
|
|
|
|
look up an item in a table. In this case, the table is the alist, and
|
|
|
|
the alist associations are the items.}
|
|
|
|
|
|
|
|
Here is an example of an alist. The key @code{pine} is associated with
|
|
|
|
the value @code{cones}; the key @code{oak} is associated with
|
|
|
|
@code{acorns}; and the key @code{maple} is associated with @code{seeds}.
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
((pine . cones)
|
|
|
|
(oak . acorns)
|
|
|
|
(maple . seeds))
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
Both the values and the keys in an alist may be any Lisp objects.
|
|
|
|
For example, in the following alist, the symbol @code{a} is
|
|
|
|
associated with the number @code{1}, and the string @code{"b"} is
|
|
|
|
associated with the @emph{list} @code{(2 3)}, which is the @sc{cdr} of
|
|
|
|
the alist element:
|
|
|
|
|
|
|
|
@example
|
|
|
|
((a . 1) ("b" 2 3))
|
|
|
|
@end example
|
|
|
|
|
|
|
|
Sometimes it is better to design an alist to store the associated
|
|
|
|
value in the @sc{car} of the @sc{cdr} of the element. Here is an
|
|
|
|
example of such an alist:
|
|
|
|
|
|
|
|
@example
|
|
|
|
((rose red) (lily white) (buttercup yellow))
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@noindent
|
|
|
|
Here we regard @code{red} as the value associated with @code{rose}. One
|
|
|
|
advantage of this kind of alist is that you can store other related
|
|
|
|
information---even a list of other items---in the @sc{cdr} of the
|
|
|
|
@sc{cdr}. One disadvantage is that you cannot use @code{rassq} (see
|
|
|
|
below) to find the element containing a given value. When neither of
|
|
|
|
these considerations is important, the choice is a matter of taste, as
|
|
|
|
long as you are consistent about it for any given alist.
|
|
|
|
|
|
|
|
The same alist shown above could be regarded as having the
|
|
|
|
associated value in the @sc{cdr} of the element; the value associated
|
|
|
|
with @code{rose} would be the list @code{(red)}.
|
|
|
|
|
|
|
|
Association lists are often used to record information that you might
|
|
|
|
otherwise keep on a stack, since new associations may be added easily to
|
|
|
|
the front of the list. When searching an association list for an
|
|
|
|
association with a given key, the first one found is returned, if there
|
|
|
|
is more than one.
|
|
|
|
|
|
|
|
In Emacs Lisp, it is @emph{not} an error if an element of an
|
|
|
|
association list is not a cons cell. The alist search functions simply
|
|
|
|
ignore such elements. Many other versions of Lisp signal errors in such
|
|
|
|
cases.
|
|
|
|
|
|
|
|
Note that property lists are similar to association lists in several
|
|
|
|
respects. A property list behaves like an association list in which
|
|
|
|
each key can occur only once. @xref{Property Lists}, for a comparison
|
|
|
|
of property lists and association lists.
|
|
|
|
|
2017-07-07 21:21:55 +02:00
|
|
|
@defun assoc key alist &optional testfn
|
2007-09-06 04:25:08 +00:00
|
|
|
This function returns the first association for @var{key} in
|
|
|
|
@var{alist}, comparing @var{key} against the alist elements using
|
2021-08-18 22:07:30 +03:00
|
|
|
@var{testfn} if it is a function, and @code{equal} otherwise
|
|
|
|
(@pxref{Equality Predicates}). If @var{testfn} is a function, it is
|
|
|
|
called with two arguments: the @sc{car} of an element from @var{alist}
|
|
|
|
and @var{key}. The function returns @code{nil} if no
|
|
|
|
association in @var{alist} has a @sc{car} equal to @var{key}, as
|
|
|
|
tested by @var{testfn}. For example:
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@smallexample
|
|
|
|
(setq trees '((pine . cones) (oak . acorns) (maple . seeds)))
|
|
|
|
@result{} ((pine . cones) (oak . acorns) (maple . seeds))
|
|
|
|
(assoc 'oak trees)
|
|
|
|
@result{} (oak . acorns)
|
|
|
|
(cdr (assoc 'oak trees))
|
|
|
|
@result{} acorns
|
|
|
|
(assoc 'birch trees)
|
|
|
|
@result{} nil
|
|
|
|
@end smallexample
|
|
|
|
|
|
|
|
Here is another example, in which the keys and values are not symbols:
|
|
|
|
|
|
|
|
@smallexample
|
|
|
|
(setq needles-per-cluster
|
|
|
|
'((2 "Austrian Pine" "Red Pine")
|
|
|
|
(3 "Pitch Pine")
|
|
|
|
(5 "White Pine")))
|
|
|
|
|
|
|
|
(cdr (assoc 3 needles-per-cluster))
|
|
|
|
@result{} ("Pitch Pine")
|
|
|
|
(cdr (assoc 2 needles-per-cluster))
|
|
|
|
@result{} ("Austrian Pine" "Red Pine")
|
|
|
|
@end smallexample
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
The function @code{assoc-string} is much like @code{assoc} except
|
|
|
|
that it ignores certain differences between strings. @xref{Text
|
|
|
|
Comparison}.
|
|
|
|
|
|
|
|
@defun rassoc value alist
|
|
|
|
This function returns the first association with value @var{value} in
|
|
|
|
@var{alist}. It returns @code{nil} if no association in @var{alist} has
|
|
|
|
a @sc{cdr} @code{equal} to @var{value}.
|
|
|
|
|
|
|
|
@code{rassoc} is like @code{assoc} except that it compares the @sc{cdr} of
|
|
|
|
each @var{alist} association instead of the @sc{car}. You can think of
|
2015-09-15 08:46:48 -07:00
|
|
|
this as reverse @code{assoc}, finding the key for a given value.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun assq key alist
|
|
|
|
This function is like @code{assoc} in that it returns the first
|
|
|
|
association for @var{key} in @var{alist}, but it makes the comparison
|
2017-07-07 21:21:55 +02:00
|
|
|
using @code{eq}. @code{assq} returns @code{nil} if no association in
|
|
|
|
@var{alist} has a @sc{car} @code{eq} to @var{key}. This function is
|
|
|
|
used more often than @code{assoc}, since @code{eq} is faster than
|
|
|
|
@code{equal} and most alists use symbols as keys. @xref{Equality
|
|
|
|
Predicates}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@smallexample
|
|
|
|
(setq trees '((pine . cones) (oak . acorns) (maple . seeds)))
|
|
|
|
@result{} ((pine . cones) (oak . acorns) (maple . seeds))
|
|
|
|
(assq 'pine trees)
|
|
|
|
@result{} (pine . cones)
|
|
|
|
@end smallexample
|
|
|
|
|
|
|
|
On the other hand, @code{assq} is not usually useful in alists where the
|
|
|
|
keys may not be symbols:
|
|
|
|
|
|
|
|
@smallexample
|
|
|
|
(setq leaves
|
|
|
|
'(("simple leaves" . oak)
|
|
|
|
("compound leaves" . horsechestnut)))
|
|
|
|
|
2020-04-22 10:42:09 -07:00
|
|
|
(assq "simple leaves" leaves)
|
2020-05-02 13:48:21 -07:00
|
|
|
@result{} @r{Unspecified; might be @code{nil} or @code{("simple leaves" . oak)}.}
|
2020-04-22 10:42:09 -07:00
|
|
|
(assoc "simple leaves" leaves)
|
2007-09-06 04:25:08 +00:00
|
|
|
@result{} ("simple leaves" . oak)
|
|
|
|
@end smallexample
|
|
|
|
@end defun
|
|
|
|
|
2017-07-17 21:30:50 +09:00
|
|
|
@defun alist-get key alist &optional default remove testfn
|
|
|
|
This function is similar to @code{assq}. It finds the first
|
|
|
|
association @w{@code{(@var{key} . @var{value})}} by comparing
|
|
|
|
@var{key} with @var{alist} elements, and, if found, returns the
|
|
|
|
@var{value} of that association. If no association is found, the
|
|
|
|
function returns @var{default}. Comparison of @var{key} against
|
|
|
|
@var{alist} elements uses the function specified by @var{testfn},
|
|
|
|
defaulting to @code{eq}.
|
|
|
|
|
|
|
|
This is a generalized variable (@pxref{Generalized Variables})
|
|
|
|
that can be used to change a value with @code{setf}. When
|
|
|
|
using it to set a value, optional argument @var{remove} non-@code{nil}
|
|
|
|
means to remove @var{key}'s association from @var{alist} if the new
|
|
|
|
value is @code{eql} to @var{default}.
|
2016-01-16 16:32:05 +02:00
|
|
|
@end defun
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@defun rassq value alist
|
|
|
|
This function returns the first association with value @var{value} in
|
|
|
|
@var{alist}. It returns @code{nil} if no association in @var{alist} has
|
|
|
|
a @sc{cdr} @code{eq} to @var{value}.
|
|
|
|
|
|
|
|
@code{rassq} is like @code{assq} except that it compares the @sc{cdr} of
|
|
|
|
each @var{alist} association instead of the @sc{car}. You can think of
|
2015-09-15 08:46:48 -07:00
|
|
|
this as reverse @code{assq}, finding the key for a given value.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
@smallexample
|
|
|
|
(setq trees '((pine . cones) (oak . acorns) (maple . seeds)))
|
|
|
|
|
|
|
|
(rassq 'acorns trees)
|
|
|
|
@result{} (oak . acorns)
|
|
|
|
(rassq 'spores trees)
|
|
|
|
@result{} nil
|
|
|
|
@end smallexample
|
|
|
|
|
|
|
|
@code{rassq} cannot search for a value stored in the @sc{car}
|
|
|
|
of the @sc{cdr} of an element:
|
|
|
|
|
|
|
|
@smallexample
|
|
|
|
(setq colors '((rose red) (lily white) (buttercup yellow)))
|
|
|
|
|
|
|
|
(rassq 'white colors)
|
|
|
|
@result{} nil
|
|
|
|
@end smallexample
|
|
|
|
|
|
|
|
In this case, the @sc{cdr} of the association @code{(lily white)} is not
|
|
|
|
the symbol @code{white}, but rather the list @code{(white)}. This
|
|
|
|
becomes clearer if the association is written in dotted pair notation:
|
|
|
|
|
|
|
|
@smallexample
|
|
|
|
(lily white) @equiv{} (lily . (white))
|
|
|
|
@end smallexample
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun assoc-default key alist &optional test default
|
|
|
|
This function searches @var{alist} for a match for @var{key}. For each
|
|
|
|
element of @var{alist}, it compares the element (if it is an atom) or
|
|
|
|
the element's @sc{car} (if it is a cons) against @var{key}, by calling
|
|
|
|
@var{test} with two arguments: the element or its @sc{car}, and
|
|
|
|
@var{key}. The arguments are passed in that order so that you can get
|
|
|
|
useful results using @code{string-match} with an alist that contains
|
|
|
|
regular expressions (@pxref{Regexp Search}). If @var{test} is omitted
|
|
|
|
or @code{nil}, @code{equal} is used for comparison.
|
|
|
|
|
|
|
|
If an alist element matches @var{key} by this criterion,
|
|
|
|
then @code{assoc-default} returns a value based on this element.
|
|
|
|
If the element is a cons, then the value is the element's @sc{cdr}.
|
|
|
|
Otherwise, the return value is @var{default}.
|
|
|
|
|
|
|
|
If no alist element matches @var{key}, @code{assoc-default} returns
|
|
|
|
@code{nil}.
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun copy-alist alist
|
|
|
|
@cindex copying alists
|
|
|
|
This function returns a two-level deep copy of @var{alist}: it creates a
|
|
|
|
new copy of each association, so that you can alter the associations of
|
|
|
|
the new alist without changing the old one.
|
|
|
|
|
|
|
|
@smallexample
|
|
|
|
@group
|
|
|
|
(setq needles-per-cluster
|
|
|
|
'((2 . ("Austrian Pine" "Red Pine"))
|
|
|
|
(3 . ("Pitch Pine"))
|
|
|
|
@end group
|
|
|
|
(5 . ("White Pine"))))
|
|
|
|
@result{}
|
|
|
|
((2 "Austrian Pine" "Red Pine")
|
|
|
|
(3 "Pitch Pine")
|
|
|
|
(5 "White Pine"))
|
|
|
|
|
|
|
|
(setq copy (copy-alist needles-per-cluster))
|
|
|
|
@result{}
|
|
|
|
((2 "Austrian Pine" "Red Pine")
|
|
|
|
(3 "Pitch Pine")
|
|
|
|
(5 "White Pine"))
|
|
|
|
|
|
|
|
(eq needles-per-cluster copy)
|
|
|
|
@result{} nil
|
|
|
|
(equal needles-per-cluster copy)
|
|
|
|
@result{} t
|
|
|
|
(eq (car needles-per-cluster) (car copy))
|
|
|
|
@result{} nil
|
|
|
|
(cdr (car (cdr needles-per-cluster)))
|
|
|
|
@result{} ("Pitch Pine")
|
|
|
|
@group
|
|
|
|
(eq (cdr (car (cdr needles-per-cluster)))
|
|
|
|
(cdr (car (cdr copy))))
|
|
|
|
@result{} t
|
|
|
|
@end group
|
|
|
|
@end smallexample
|
|
|
|
|
|
|
|
This example shows how @code{copy-alist} makes it possible to change
|
|
|
|
the associations of one copy without affecting the other:
|
|
|
|
|
|
|
|
@smallexample
|
|
|
|
@group
|
|
|
|
(setcdr (assq 3 copy) '("Martian Vacuum Pine"))
|
|
|
|
(cdr (assq 3 needles-per-cluster))
|
|
|
|
@result{} ("Pitch Pine")
|
|
|
|
@end group
|
|
|
|
@end smallexample
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun assq-delete-all key alist
|
|
|
|
This function deletes from @var{alist} all the elements whose @sc{car}
|
|
|
|
is @code{eq} to @var{key}, much as if you used @code{delq} to delete
|
|
|
|
each such element one by one. It returns the shortened alist, and
|
|
|
|
often modifies the original list structure of @var{alist}. For
|
|
|
|
correct results, use the return value of @code{assq-delete-all} rather
|
|
|
|
than looking at the saved value of @var{alist}.
|
|
|
|
|
|
|
|
@example
|
Document constant vs mutable objects better
This patch builds on a suggested patch by Mattias Engdegård
and on further comments by Eli Zaretskii.
Original bug report by Kevin Vigouroux (Bug#40671).
* doc/lispintro/emacs-lisp-intro.texi (set & setq, Review)
(setcar, Lists diagrammed, Mail Aliases, Indent Tabs Mode):
setq is a special form, not a function or command.
* doc/lispintro/emacs-lisp-intro.texi (setcar):
* doc/lispref/lists.texi (Modifying Lists, Rearrangement):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions, Vectors):
* doc/lispref/strings.texi (String Basics, Modifying Strings):
Mention mutable vs constant objects.
* doc/lispintro/emacs-lisp-intro.texi (setcar, setcdr)
(kill-new function, cons & search-fwd Review):
* doc/lispref/edebug.texi (Printing in Edebug):
* doc/lispref/keymaps.texi (Changing Key Bindings):
* doc/lispref/lists.texi (Setcar, Setcdr, Rearrangement)
(Sets And Lists, Association Lists, Plist Access):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions):
* doc/lispref/strings.texi (Text Comparison):
Fix examples so that they do not try to change constants.
2020-04-18 12:59:17 -07:00
|
|
|
(setq alist (list '(foo 1) '(bar 2) '(foo 3) '(lose 4)))
|
2007-09-06 04:25:08 +00:00
|
|
|
@result{} ((foo 1) (bar 2) (foo 3) (lose 4))
|
|
|
|
(assq-delete-all 'foo alist)
|
|
|
|
@result{} ((bar 2) (lose 4))
|
|
|
|
alist
|
|
|
|
@result{} ((foo 1) (bar 2) (lose 4))
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
2018-01-28 13:05:54 +09:00
|
|
|
@defun assoc-delete-all key alist &optional test
|
|
|
|
This function is like @code{assq-delete-all} except that it accepts
|
|
|
|
an optional argument @var{test}, a predicate function to compare the
|
2018-09-15 09:20:32 -07:00
|
|
|
keys in @var{alist}. If omitted or @code{nil}, @var{test} defaults to
|
2018-01-28 13:05:54 +09:00
|
|
|
@code{equal}. As @code{assq-delete-all}, this function often modifies
|
|
|
|
the original list structure of @var{alist}.
|
|
|
|
@end defun
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@defun rassq-delete-all value alist
|
|
|
|
This function deletes from @var{alist} all the elements whose @sc{cdr}
|
|
|
|
is @code{eq} to @var{value}. It returns the shortened alist, and
|
|
|
|
often modifies the original list structure of @var{alist}.
|
|
|
|
@code{rassq-delete-all} is like @code{assq-delete-all} except that it
|
|
|
|
compares the @sc{cdr} of each @var{alist} association instead of the
|
|
|
|
@sc{car}.
|
|
|
|
@end defun
|
2012-12-02 17:14:16 +08:00
|
|
|
|
2019-10-13 01:29:32 +02:00
|
|
|
@defmac let-alist alist body
|
|
|
|
Creates a binding for each symbol used as keys the association list
|
|
|
|
@var{alist}, prefixed with dot. This can be useful when accessing
|
|
|
|
several items in the same association list, and it's best understood
|
|
|
|
through a simple example:
|
|
|
|
|
|
|
|
@lisp
|
|
|
|
(setq colors '((rose . red) (lily . white) (buttercup . yellow)))
|
|
|
|
(let-alist colors
|
|
|
|
(if (eq .rose 'red)
|
|
|
|
.lily))
|
2021-07-09 14:37:50 +01:00
|
|
|
@result{} white
|
2019-10-13 01:29:32 +02:00
|
|
|
@end lisp
|
|
|
|
|
|
|
|
The @var{body} is inspected at compilation time, and only the symbols
|
|
|
|
that appear in @var{body} with a @samp{.} as the first character in
|
|
|
|
the symbol name will be bound. Finding the keys is done with
|
|
|
|
@code{assq}, and the @code{cdr} of the return value of this
|
|
|
|
@code{assq} is assigned as the value for the binding.
|
|
|
|
|
|
|
|
Nested association lists is supported:
|
|
|
|
|
|
|
|
@lisp
|
|
|
|
(setq colors '((rose . red) (lily (belladonna . yellow) (brindisi . pink))))
|
|
|
|
(let-alist colors
|
|
|
|
(if (eq .rose 'red)
|
|
|
|
.lily.belladonna))
|
2021-07-09 14:37:50 +01:00
|
|
|
@result{} yellow
|
2019-10-13 01:29:32 +02:00
|
|
|
@end lisp
|
|
|
|
|
|
|
|
Nesting @code{let-alist} inside each other is allowed, but the code in
|
|
|
|
the inner @code{let-alist} can't access the variables bound by the
|
|
|
|
outer @code{let-alist}.
|
|
|
|
@end defmac
|
|
|
|
|
2012-12-02 17:14:16 +08:00
|
|
|
@node Property Lists
|
|
|
|
@section Property Lists
|
|
|
|
@cindex property list
|
|
|
|
@cindex plist
|
|
|
|
|
|
|
|
A @dfn{property list} (@dfn{plist} for short) is a list of paired
|
|
|
|
elements. Each of the pairs associates a property name (usually a
|
|
|
|
symbol) with a property or value. Here is an example of a property
|
|
|
|
list:
|
|
|
|
|
|
|
|
@example
|
|
|
|
(pine cones numbers (1 2 3) color "blue")
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@noindent
|
|
|
|
This property list associates @code{pine} with @code{cones},
|
|
|
|
@code{numbers} with @code{(1 2 3)}, and @code{color} with
|
|
|
|
@code{"blue"}. The property names and values can be any Lisp objects,
|
|
|
|
but the names are usually symbols (as they are in this example).
|
|
|
|
|
|
|
|
Property lists are used in several contexts. For instance, the
|
|
|
|
function @code{put-text-property} takes an argument which is a
|
|
|
|
property list, specifying text properties and associated values which
|
|
|
|
are to be applied to text in a string or buffer. @xref{Text
|
|
|
|
Properties}.
|
|
|
|
|
|
|
|
Another prominent use of property lists is for storing symbol
|
|
|
|
properties. Every symbol possesses a list of properties, used to
|
|
|
|
record miscellaneous information about the symbol; these properties
|
|
|
|
are stored in the form of a property list. @xref{Symbol Properties}.
|
|
|
|
|
2023-02-05 14:09:35 +02:00
|
|
|
@defun plistp object
|
|
|
|
This predicate function returns non-@code{nil} if @var{object} is a
|
|
|
|
valid property list.
|
|
|
|
@end defun
|
|
|
|
|
2012-12-02 17:14:16 +08:00
|
|
|
@menu
|
|
|
|
* Plists and Alists:: Comparison of the advantages of property
|
|
|
|
lists and association lists.
|
|
|
|
* Plist Access:: Accessing property lists stored elsewhere.
|
|
|
|
@end menu
|
|
|
|
|
|
|
|
@node Plists and Alists
|
|
|
|
@subsection Property Lists and Association Lists
|
Avoid double spaces around abbrevations in Texinfo
* doc/emacs/custom.texi (Init Rebinding):
* doc/emacs/dired.texi (Operating on Files):
* doc/emacs/emacs.texi (Top):
* doc/emacs/emerge-xtra.texi (Emerge):
* doc/emacs/files.texi (Files):
* doc/emacs/frames.texi (Drag and Drop):
* doc/emacs/misc.texi (Hyperlinking):
* doc/emacs/modes.texi (Modes):
* doc/emacs/mule.texi (Input Methods):
* doc/emacs/windows.texi (Window Tool Bar):
* doc/lispintro/emacs-lisp-intro.texi
(Lexical & Dynamic Binding Differences):
* doc/lispref/elisp.texi (Top):
* doc/lispref/functions.texi (Functions, Generic Functions):
* doc/lispref/hash.texi (Defining Hash):
* doc/lispref/keymaps.texi (Creating Keymaps):
* doc/lispref/lists.texi (Property Lists):
* doc/lispref/modes.texi (%-Constructs):
* doc/lispref/nonascii.texi (Character Properties):
* doc/lispref/processes.texi (Misc Network):
* doc/lispref/searching.texi (Regexp Functions):
* doc/lispref/syntax.texi (Syntax Table Internals):
* doc/lispref/text.texi (Filling, Checksum/Hash)
(JSONRPC deferred requests):
* doc/misc/calc.texi (What is Calc, Modes Tutorial):
* doc/misc/cc-mode.texi (List Line-Up, Operator Line-Up)
(Custom Macros, Indenting Directives):
* doc/misc/efaq.texi (Colors on a TTY, Security risks with Emacs):
* doc/misc/eglot.texi (Eglot Variables):
* doc/misc/erc.texi (Connecting):
* doc/misc/eshell.texi (Aliases, Completion):
* doc/misc/flymake.texi (Backend functions):
* doc/misc/gnus-faq.texi (FAQ 5 - Composing messages):
* doc/misc/gnus.texi (Gnus Unplugged, Window Layout)
(Filtering Incoming Mail, History):
* doc/misc/idlwave.texi (Online Help, Catalogs):
* doc/misc/wisent.texi (Wisent Overview): Be more consistent with
abbreviations. Use @: or comma or rewrite to avoid double spaces.
Ref: https://lists.gnu.org/r/emacs-devel/2025-01/msg00909.html
2025-01-24 17:32:17 +01:00
|
|
|
@cindex plist vs.@: alist
|
|
|
|
@cindex alist vs.@: plist
|
2012-12-02 17:14:16 +08:00
|
|
|
|
|
|
|
@cindex property lists vs association lists
|
|
|
|
Association lists (@pxref{Association Lists}) are very similar to
|
|
|
|
property lists. In contrast to association lists, the order of the
|
|
|
|
pairs in the property list is not significant, since the property
|
|
|
|
names must be distinct.
|
|
|
|
|
|
|
|
Property lists are better than association lists for attaching
|
|
|
|
information to various Lisp function names or variables. If your
|
|
|
|
program keeps all such information in one association list, it will
|
|
|
|
typically need to search that entire list each time it checks for an
|
|
|
|
association for a particular Lisp function name or variable, which
|
|
|
|
could be slow. By contrast, if you keep the same information in the
|
|
|
|
property lists of the function names or variables themselves, each
|
|
|
|
search will scan only the length of one property list, which is
|
|
|
|
usually short. This is why the documentation for a variable is
|
|
|
|
recorded in a property named @code{variable-documentation}. The byte
|
|
|
|
compiler likewise uses properties to record those functions needing
|
|
|
|
special treatment.
|
|
|
|
|
|
|
|
However, association lists have their own advantages. Depending on
|
|
|
|
your application, it may be faster to add an association to the front of
|
|
|
|
an association list than to update a property. All properties for a
|
|
|
|
symbol are stored in the same property list, so there is a possibility
|
|
|
|
of a conflict between different uses of a property name. (For this
|
|
|
|
reason, it is a good idea to choose property names that are probably
|
|
|
|
unique, such as by beginning the property name with the program's usual
|
|
|
|
name-prefix for variables and functions.) An association list may be
|
|
|
|
used like a stack where associations are pushed on the front of the list
|
|
|
|
and later discarded; this is not possible with a property list.
|
|
|
|
|
|
|
|
@node Plist Access
|
|
|
|
@subsection Property Lists Outside Symbols
|
Improve indexing on the chapter/section/subsection levels.
doc/lispref/windows.texi (Recombining Windows): Index subject of sections.
doc/lispref/variables.texi (Variables with Restricted Values)
(Generalized Variables): Index subject of sections.
doc/lispref/text.texi (Buffer Contents, Examining Properties)
(Changing Properties, Property Search, Substitution): Index
subject of sections.
doc/lispref/syntax.texi (Motion and Syntax, Parsing Expressions)
(Motion via Parsing, Position Parse, Control Parsing): Index
subject of sections.
doc/lispref/strings.texi (Predicates for Strings, Creating Strings)
(Modifying Strings, Text Comparison): Index subject of sections.
doc/lispref/searching.texi (Syntax of Regexps, Regexp Special)
(Regexp Functions, Regexp Functions): Index subject of sections.
doc/lispref/processes.texi (Subprocess Creation, Process Information): Index
subject of sections.
doc/lispref/positions.texi (Screen Lines): Index subject of sections.
doc/lispref/nonascii.texi (Scanning Charsets, Specifying Coding Systems):
Index subject of sections.
doc/lispref/minibuf.texi (Text from Minibuffer, Object from Minibuffer)
(Multiple Queries, Minibuffer Contents): Index subject of
sections.
doc/lispref/markers.texi (Predicates on Markers, Creating Markers)
(Information from Markers, Moving Markers): Index subject of
sections.
doc/lispref/macros.texi (Defining Macros, Problems with Macros): Index
subject of sections.
doc/lispref/loading.texi (Loading Non-ASCII, Where Defined): Index subject
of sections.
doc/lispref/lists.texi (List-related Predicates, List Variables, Setcar)
(Setcdr, Plist Access): Index subject of sections.
doc/lispref/keymaps.texi (Controlling Active Maps, Scanning Keymaps)
(Modifying Menus): Index subject of sections.
doc/lispref/help.texi (Accessing Documentation, Help Functions): Index
subject of sections.
doc/lispref/hash.texi (Hash Access): Index subject of sections.
doc/lispref/functions.texi (Core Advising Primitives)
(Advising Named Functions, Porting old advices): Index subject of
sections.
doc/lispref/frames.texi (Creating Frames, Initial Parameters)
(Position Parameters, Buffer Parameters, Minibuffers and Frames)
(Pop-Up Menus, Drag and Drop): Index subject of sections.
doc/lispref/files.texi (Visiting Functions, Kinds of Files)
(Unique File Names): Index subject of sections.
doc/lispref/display.texi (Refresh Screen, Echo Area Customization)
(Warning Variables, Warning Options, Delayed Warnings)
(Temporary Displays, Managing Overlays, Overlay Properties)
(Finding Overlays, Size of Displayed Text, Defining Faces)
(Attribute Functions, Displaying Faces, Face Remapping)
(Basic Faces, Font Lookup, Fontsets, Replacing Specs)
(Defining Images, Showing Images): Index subject of sections.
doc/lispref/debugging.texi (Debugging, Explicit Debug)
(Invoking the Debugger, Excess Open, Excess Close): Index subject
of sections.
doc/lispref/customize.texi (Defining New Types, Applying Customizations)
(Custom Themes): Index subject of sections.
doc/lispref/control.texi (Sequencing, Combining Conditions)
(Processing of Errors, Cleanups): Index subject of sections.
doc/lispref/compile.texi (Eval During Compile): Index subject of sections.
doc/lispref/commands.texi (Using Interactive, Distinguish Interactive)
(Command Loop Info, Classifying Events, Event Mod)
(Invoking the Input Method): Index subject of sections.
doc/lispref/buffers.texi (Buffer List, Buffer Gap): Index subject of sections.
doc/lispref/backups.texi (Making Backups, Numbered Backups, Backup Names)
(Reverting): Index subject of sections.
doc/lispref/abbrevs.texi (Abbrev Tables, Defining Abbrevs, Abbrev Files)
(Abbrev Expansion, Standard Abbrev Tables, Abbrev Properties)
(Abbrev Table Properties): Index subject of sections.
doc/lispref/os.texi (Time of Day, Time Conversion, Time Parsing)
(Time Calculations, Idle Timers): Index subject of sections.
2014-12-23 20:42:30 +02:00
|
|
|
@cindex plist access
|
|
|
|
@cindex accessing plist properties
|
2012-12-02 17:14:16 +08:00
|
|
|
|
|
|
|
The following functions can be used to manipulate property lists.
|
Audit some plist uses with new predicate argument
* doc/lispref/lists.texi (Plist Access): Improve description of
default predicate.
* lisp/emacs-lisp/cl-extra.el (cl-getf, cl--set-getf): Assume
plist-member always returns a cons.
* lisp/emacs-lisp/gv.el (plist-get): Support new optional predicate
argument (bug#47425#91).
* lisp/emacs-lisp/map.el: Bump minor version.
(map--dispatch): Remove now that bug#58563 is fixed. Break two
remaining uses out into corresponding cl-defmethods.
(map--plist-p): Add docstring.
(map--plist-has-predicate, map--plist-member-1, map--plist-member)
(map--plist-put-1, map--plist-put): New definitions for supporting
predicate argument backward compatibly.
(map-elt): Fix generalized variable getter under a
predicate (bug#58531). Use predicate when given a plist.
(map-put): Avoid gratuitous warnings when called without the hidden
predicate argument. Improve obsoletion message.
(map-put!): Use predicate when given a plist.
(map-contains-key): Ditto. Declare forgotten
advertised-calling-convention (bug#58531#19).
(map--put): Group definition in file together with that of map-put!.
* lisp/files-x.el (connection-local-normalize-criteria): Simplify
using mapcan + plist-get.
* lisp/net/eudc.el (eudc--plist-member): New convenience function.
(eudc-plist-member, eudc-plist-get, eudc-lax-plist-get): Use it
instead of open-coding plist-member.
* src/fns.c (Fplist_get, plist_get, Fplist_put, plist_put): Pass the
plist element as the first argument to the predicate, for
consistency with assoc + alist-get.
(Fplist_member, plist_member): Move from widget to plist section.
Open-code the EQ case in plist_member, and call it from
Fplist_member in that case, rather than the other way around.
* test/lisp/apropos-tests.el (apropos-tests-format-plist): Avoid
polluting obarray.
* test/lisp/emacs-lisp/cl-extra-tests.el (cl-getf): Extend test with
generalized variables, degenerate plists, and improper lists.
* test/lisp/emacs-lisp/gv-tests.el: Byte-compile file; in the
meantime bug#24402 seems to have been fixed or worked around.
(gv-setter-edebug): Inhibit printing messages.
(gv-plist-get): Avoid modifying constant literals. Also test with a
predicate argument.
* test/lisp/emacs-lisp/map-tests.el (with-maps-do): Simplify
docstring.
(test-map-elt-testfn): Rename...
(test-map-elt-testfn-alist): ...to this. Also test with a predicate
argument.
(test-map-elt-testfn-plist, test-map-elt-gv, test-map-elt-signature)
(test-map-put!-plist, test-map-put!-signature)
(test-map-contains-key-signature, test-map-plist-member)
(test-map-plist-put): New tests.
(test-map-contains-key-testfn): Also test with a predicate argument.
(test-map-setf-alist-overwrite-key, test-map-setf-plist-insert-key)
(test-map-setf-plist-overwrite-key): Avoid modifying constant
literals.
(test-hash-table-setf-insert-key)
(test-hash-table-setf-overwrite-key): Fix indentation.
(test-setf-map-with-function): Make test more precise.
* test/lisp/net/eudc-tests.el: New file.
* test/lisp/subr-tests.el (test-plistp): Extend test with circular
list.
* test/src/fns-tests.el (test-cycle-equal, test-cycle-nconc): Move
from plist section to circular list section.
(plist-put/odd-number-of-elements): Avoid modifying constant
literals.
(plist-member/improper-list): Simplify.
(test-plist): Move to plist section. Also test with a predicate
argument.
2022-08-20 16:32:33 +03:00
|
|
|
They all default to comparing property names using @code{eq}.
|
2012-12-02 17:14:16 +08:00
|
|
|
|
2022-06-27 12:22:05 +02:00
|
|
|
@defun plist-get plist property &optional predicate
|
2012-12-02 17:14:16 +08:00
|
|
|
This returns the value of the @var{property} property stored in the
|
2022-06-27 12:22:05 +02:00
|
|
|
property list @var{plist}. Comparisons are done with @var{predicate},
|
Audit some plist uses with new predicate argument
* doc/lispref/lists.texi (Plist Access): Improve description of
default predicate.
* lisp/emacs-lisp/cl-extra.el (cl-getf, cl--set-getf): Assume
plist-member always returns a cons.
* lisp/emacs-lisp/gv.el (plist-get): Support new optional predicate
argument (bug#47425#91).
* lisp/emacs-lisp/map.el: Bump minor version.
(map--dispatch): Remove now that bug#58563 is fixed. Break two
remaining uses out into corresponding cl-defmethods.
(map--plist-p): Add docstring.
(map--plist-has-predicate, map--plist-member-1, map--plist-member)
(map--plist-put-1, map--plist-put): New definitions for supporting
predicate argument backward compatibly.
(map-elt): Fix generalized variable getter under a
predicate (bug#58531). Use predicate when given a plist.
(map-put): Avoid gratuitous warnings when called without the hidden
predicate argument. Improve obsoletion message.
(map-put!): Use predicate when given a plist.
(map-contains-key): Ditto. Declare forgotten
advertised-calling-convention (bug#58531#19).
(map--put): Group definition in file together with that of map-put!.
* lisp/files-x.el (connection-local-normalize-criteria): Simplify
using mapcan + plist-get.
* lisp/net/eudc.el (eudc--plist-member): New convenience function.
(eudc-plist-member, eudc-plist-get, eudc-lax-plist-get): Use it
instead of open-coding plist-member.
* src/fns.c (Fplist_get, plist_get, Fplist_put, plist_put): Pass the
plist element as the first argument to the predicate, for
consistency with assoc + alist-get.
(Fplist_member, plist_member): Move from widget to plist section.
Open-code the EQ case in plist_member, and call it from
Fplist_member in that case, rather than the other way around.
* test/lisp/apropos-tests.el (apropos-tests-format-plist): Avoid
polluting obarray.
* test/lisp/emacs-lisp/cl-extra-tests.el (cl-getf): Extend test with
generalized variables, degenerate plists, and improper lists.
* test/lisp/emacs-lisp/gv-tests.el: Byte-compile file; in the
meantime bug#24402 seems to have been fixed or worked around.
(gv-setter-edebug): Inhibit printing messages.
(gv-plist-get): Avoid modifying constant literals. Also test with a
predicate argument.
* test/lisp/emacs-lisp/map-tests.el (with-maps-do): Simplify
docstring.
(test-map-elt-testfn): Rename...
(test-map-elt-testfn-alist): ...to this. Also test with a predicate
argument.
(test-map-elt-testfn-plist, test-map-elt-gv, test-map-elt-signature)
(test-map-put!-plist, test-map-put!-signature)
(test-map-contains-key-signature, test-map-plist-member)
(test-map-plist-put): New tests.
(test-map-contains-key-testfn): Also test with a predicate argument.
(test-map-setf-alist-overwrite-key, test-map-setf-plist-insert-key)
(test-map-setf-plist-overwrite-key): Avoid modifying constant
literals.
(test-hash-table-setf-insert-key)
(test-hash-table-setf-overwrite-key): Fix indentation.
(test-setf-map-with-function): Make test more precise.
* test/lisp/net/eudc-tests.el: New file.
* test/lisp/subr-tests.el (test-plistp): Extend test with circular
list.
* test/src/fns-tests.el (test-cycle-equal, test-cycle-nconc): Move
from plist section to circular list section.
(plist-put/odd-number-of-elements): Avoid modifying constant
literals.
(plist-member/improper-list): Simplify.
(test-plist): Move to plist section. Also test with a predicate
argument.
2022-08-20 16:32:33 +03:00
|
|
|
which defaults to @code{eq}. It accepts a malformed @var{plist}
|
2012-12-02 17:14:16 +08:00
|
|
|
argument. If @var{property} is not found in the @var{plist}, it
|
|
|
|
returns @code{nil}. For example,
|
|
|
|
|
|
|
|
@example
|
|
|
|
(plist-get '(foo 4) 'foo)
|
|
|
|
@result{} 4
|
|
|
|
(plist-get '(foo 4 bad) 'foo)
|
|
|
|
@result{} 4
|
|
|
|
(plist-get '(foo 4 bad) 'bad)
|
|
|
|
@result{} nil
|
|
|
|
(plist-get '(foo 4 bad) 'bar)
|
|
|
|
@result{} nil
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
2022-06-27 12:22:05 +02:00
|
|
|
@defun plist-put plist property value &optional predicate
|
2012-12-02 17:14:16 +08:00
|
|
|
This stores @var{value} as the value of the @var{property} property in
|
2022-06-27 12:22:05 +02:00
|
|
|
the property list @var{plist}. Comparisons are done with @var{predicate},
|
Audit some plist uses with new predicate argument
* doc/lispref/lists.texi (Plist Access): Improve description of
default predicate.
* lisp/emacs-lisp/cl-extra.el (cl-getf, cl--set-getf): Assume
plist-member always returns a cons.
* lisp/emacs-lisp/gv.el (plist-get): Support new optional predicate
argument (bug#47425#91).
* lisp/emacs-lisp/map.el: Bump minor version.
(map--dispatch): Remove now that bug#58563 is fixed. Break two
remaining uses out into corresponding cl-defmethods.
(map--plist-p): Add docstring.
(map--plist-has-predicate, map--plist-member-1, map--plist-member)
(map--plist-put-1, map--plist-put): New definitions for supporting
predicate argument backward compatibly.
(map-elt): Fix generalized variable getter under a
predicate (bug#58531). Use predicate when given a plist.
(map-put): Avoid gratuitous warnings when called without the hidden
predicate argument. Improve obsoletion message.
(map-put!): Use predicate when given a plist.
(map-contains-key): Ditto. Declare forgotten
advertised-calling-convention (bug#58531#19).
(map--put): Group definition in file together with that of map-put!.
* lisp/files-x.el (connection-local-normalize-criteria): Simplify
using mapcan + plist-get.
* lisp/net/eudc.el (eudc--plist-member): New convenience function.
(eudc-plist-member, eudc-plist-get, eudc-lax-plist-get): Use it
instead of open-coding plist-member.
* src/fns.c (Fplist_get, plist_get, Fplist_put, plist_put): Pass the
plist element as the first argument to the predicate, for
consistency with assoc + alist-get.
(Fplist_member, plist_member): Move from widget to plist section.
Open-code the EQ case in plist_member, and call it from
Fplist_member in that case, rather than the other way around.
* test/lisp/apropos-tests.el (apropos-tests-format-plist): Avoid
polluting obarray.
* test/lisp/emacs-lisp/cl-extra-tests.el (cl-getf): Extend test with
generalized variables, degenerate plists, and improper lists.
* test/lisp/emacs-lisp/gv-tests.el: Byte-compile file; in the
meantime bug#24402 seems to have been fixed or worked around.
(gv-setter-edebug): Inhibit printing messages.
(gv-plist-get): Avoid modifying constant literals. Also test with a
predicate argument.
* test/lisp/emacs-lisp/map-tests.el (with-maps-do): Simplify
docstring.
(test-map-elt-testfn): Rename...
(test-map-elt-testfn-alist): ...to this. Also test with a predicate
argument.
(test-map-elt-testfn-plist, test-map-elt-gv, test-map-elt-signature)
(test-map-put!-plist, test-map-put!-signature)
(test-map-contains-key-signature, test-map-plist-member)
(test-map-plist-put): New tests.
(test-map-contains-key-testfn): Also test with a predicate argument.
(test-map-setf-alist-overwrite-key, test-map-setf-plist-insert-key)
(test-map-setf-plist-overwrite-key): Avoid modifying constant
literals.
(test-hash-table-setf-insert-key)
(test-hash-table-setf-overwrite-key): Fix indentation.
(test-setf-map-with-function): Make test more precise.
* test/lisp/net/eudc-tests.el: New file.
* test/lisp/subr-tests.el (test-plistp): Extend test with circular
list.
* test/src/fns-tests.el (test-cycle-equal, test-cycle-nconc): Move
from plist section to circular list section.
(plist-put/odd-number-of-elements): Avoid modifying constant
literals.
(plist-member/improper-list): Simplify.
(test-plist): Move to plist section. Also test with a predicate
argument.
2022-08-20 16:32:33 +03:00
|
|
|
which defaults to @code{eq}. It may modify @var{plist} destructively,
|
2012-12-02 17:14:16 +08:00
|
|
|
or it may construct a new list structure without altering the old. The
|
|
|
|
function returns the modified property list, so you can store that back
|
|
|
|
in the place where you got @var{plist}. For example,
|
|
|
|
|
|
|
|
@example
|
Document constant vs mutable objects better
This patch builds on a suggested patch by Mattias Engdegård
and on further comments by Eli Zaretskii.
Original bug report by Kevin Vigouroux (Bug#40671).
* doc/lispintro/emacs-lisp-intro.texi (set & setq, Review)
(setcar, Lists diagrammed, Mail Aliases, Indent Tabs Mode):
setq is a special form, not a function or command.
* doc/lispintro/emacs-lisp-intro.texi (setcar):
* doc/lispref/lists.texi (Modifying Lists, Rearrangement):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions, Vectors):
* doc/lispref/strings.texi (String Basics, Modifying Strings):
Mention mutable vs constant objects.
* doc/lispintro/emacs-lisp-intro.texi (setcar, setcdr)
(kill-new function, cons & search-fwd Review):
* doc/lispref/edebug.texi (Printing in Edebug):
* doc/lispref/keymaps.texi (Changing Key Bindings):
* doc/lispref/lists.texi (Setcar, Setcdr, Rearrangement)
(Sets And Lists, Association Lists, Plist Access):
* doc/lispref/sequences.texi (Sequence Functions)
(Array Functions):
* doc/lispref/strings.texi (Text Comparison):
Fix examples so that they do not try to change constants.
2020-04-18 12:59:17 -07:00
|
|
|
(setq my-plist (list 'bar t 'foo 4))
|
2012-12-02 17:14:16 +08:00
|
|
|
@result{} (bar t foo 4)
|
|
|
|
(setq my-plist (plist-put my-plist 'foo 69))
|
|
|
|
@result{} (bar t foo 69)
|
|
|
|
(setq my-plist (plist-put my-plist 'quux '(a)))
|
|
|
|
@result{} (bar t foo 69 quux (a))
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun lax-plist-get plist property
|
2022-06-27 12:22:05 +02:00
|
|
|
This obsolete function is like @code{plist-get} except that it
|
|
|
|
compares properties using @code{equal} instead of @code{eq}.
|
2012-12-02 17:14:16 +08:00
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun lax-plist-put plist property value
|
2022-06-27 12:22:05 +02:00
|
|
|
This obsolete function is like @code{plist-put} except that it
|
|
|
|
compares properties using @code{equal} instead of @code{eq}.
|
2012-12-02 17:14:16 +08:00
|
|
|
@end defun
|
|
|
|
|
2022-06-27 12:22:05 +02:00
|
|
|
@defun plist-member plist property &optional predicate
|
2012-12-02 17:14:16 +08:00
|
|
|
This returns non-@code{nil} if @var{plist} contains the given
|
Audit some plist uses with new predicate argument
* doc/lispref/lists.texi (Plist Access): Improve description of
default predicate.
* lisp/emacs-lisp/cl-extra.el (cl-getf, cl--set-getf): Assume
plist-member always returns a cons.
* lisp/emacs-lisp/gv.el (plist-get): Support new optional predicate
argument (bug#47425#91).
* lisp/emacs-lisp/map.el: Bump minor version.
(map--dispatch): Remove now that bug#58563 is fixed. Break two
remaining uses out into corresponding cl-defmethods.
(map--plist-p): Add docstring.
(map--plist-has-predicate, map--plist-member-1, map--plist-member)
(map--plist-put-1, map--plist-put): New definitions for supporting
predicate argument backward compatibly.
(map-elt): Fix generalized variable getter under a
predicate (bug#58531). Use predicate when given a plist.
(map-put): Avoid gratuitous warnings when called without the hidden
predicate argument. Improve obsoletion message.
(map-put!): Use predicate when given a plist.
(map-contains-key): Ditto. Declare forgotten
advertised-calling-convention (bug#58531#19).
(map--put): Group definition in file together with that of map-put!.
* lisp/files-x.el (connection-local-normalize-criteria): Simplify
using mapcan + plist-get.
* lisp/net/eudc.el (eudc--plist-member): New convenience function.
(eudc-plist-member, eudc-plist-get, eudc-lax-plist-get): Use it
instead of open-coding plist-member.
* src/fns.c (Fplist_get, plist_get, Fplist_put, plist_put): Pass the
plist element as the first argument to the predicate, for
consistency with assoc + alist-get.
(Fplist_member, plist_member): Move from widget to plist section.
Open-code the EQ case in plist_member, and call it from
Fplist_member in that case, rather than the other way around.
* test/lisp/apropos-tests.el (apropos-tests-format-plist): Avoid
polluting obarray.
* test/lisp/emacs-lisp/cl-extra-tests.el (cl-getf): Extend test with
generalized variables, degenerate plists, and improper lists.
* test/lisp/emacs-lisp/gv-tests.el: Byte-compile file; in the
meantime bug#24402 seems to have been fixed or worked around.
(gv-setter-edebug): Inhibit printing messages.
(gv-plist-get): Avoid modifying constant literals. Also test with a
predicate argument.
* test/lisp/emacs-lisp/map-tests.el (with-maps-do): Simplify
docstring.
(test-map-elt-testfn): Rename...
(test-map-elt-testfn-alist): ...to this. Also test with a predicate
argument.
(test-map-elt-testfn-plist, test-map-elt-gv, test-map-elt-signature)
(test-map-put!-plist, test-map-put!-signature)
(test-map-contains-key-signature, test-map-plist-member)
(test-map-plist-put): New tests.
(test-map-contains-key-testfn): Also test with a predicate argument.
(test-map-setf-alist-overwrite-key, test-map-setf-plist-insert-key)
(test-map-setf-plist-overwrite-key): Avoid modifying constant
literals.
(test-hash-table-setf-insert-key)
(test-hash-table-setf-overwrite-key): Fix indentation.
(test-setf-map-with-function): Make test more precise.
* test/lisp/net/eudc-tests.el: New file.
* test/lisp/subr-tests.el (test-plistp): Extend test with circular
list.
* test/src/fns-tests.el (test-cycle-equal, test-cycle-nconc): Move
from plist section to circular list section.
(plist-put/odd-number-of-elements): Avoid modifying constant
literals.
(plist-member/improper-list): Simplify.
(test-plist): Move to plist section. Also test with a predicate
argument.
2022-08-20 16:32:33 +03:00
|
|
|
@var{property}. Comparisons are done with @var{predicate}, which
|
2022-06-27 12:22:05 +02:00
|
|
|
defaults to @code{eq}. Unlike @code{plist-get}, this allows you to
|
|
|
|
distinguish between a missing property and a property with the value
|
|
|
|
@code{nil}. The value is actually the tail of @var{plist} whose
|
|
|
|
@code{car} is @var{property}.
|
2012-12-02 17:14:16 +08:00
|
|
|
@end defun
|