This document describes a set of Emacs Lisp facilities borrowed from Common Lisp. All the facilities are described here in detail. While this document does not assume any prior knowledge of Common Lisp, it does assume a basic familiarity with Emacs Lisp.
Common Lisp is a huge language, and Common Lisp systems tend to be massive and extremely complex. Emacs Lisp, by contrast, is rather minimalist in the choice of Lisp features it offers the programmer. As Emacs Lisp programmers have grown in number, and the applications they write have grown more ambitious, it has become clear that Emacs Lisp could benefit from many of the conveniences of Common Lisp.
The CL package adds a number of Common Lisp functions and control structures to Emacs Lisp. While not a 100% complete implementation of Common Lisp, CL adds enough functionality to make Emacs Lisp programming significantly more convenient.
Please note: the CL functions are not standard parts of
the Emacs Lisp name space, so it is legitimate for users to define
them with other, conflicting meanings. To avoid conflicting with
those user activities, we have a policy that packages installed in
Emacs must not load CL at run time. (It is ok for them to load
CL at compile time only, with eval-when-compile, and use
the macros it provides.) If you are writing packages that you plan to
distribute and invite widespread use for, you might want to observe
the same rule.
Some Common Lisp features have been omitted from this package for various reasons:
assoc function is incompatible with the
Common Lisp assoc. In such cases, this package usually
adds the suffix `*' to the function name of the Common
Lisp version of the function (e.g., assoc*).
The package described here was written by Dave Gillespie, daveg@synaptics.com. It is a total rewrite of the original 1986 cl.el package by Cesar Quiroz. Most features of the Quiroz package have been retained; any incompatibilities are noted in the descriptions below. Care has been taken in this version to ensure that each function is defined efficiently, concisely, and with minimal impact on the rest of the Emacs environment.
Lisp code that uses features from the CL package should include at the beginning:
(require 'cl)
If you want to ensure that the new (Gillespie) version of CL
is the one that is present, add an additional (require 'cl-19)
call:
(require 'cl)
(require 'cl-19)
The second call will fail (with “cl-19.el not found”) if the old cl.el package was in use.
It is safe to arrange to load CL at all times, e.g.,
in your .emacs file. But it's a good idea, for portability,
to (require 'cl) in your code even if you do this.
The Common Lisp package is organized into four files:
cadr function won't need to pay
the overhead of loading the more advanced functions.
delete-if and assoc*.
The file cl.el includes all necessary autoload
commands for the functions and macros in the other three files.
All you have to do is (require 'cl), and cl.el
will take care of pulling in the other files when they are
needed.
There is another file, cl-compat.el, which defines some
routines from the older cl.el package that are no longer
present in the new package. This includes internal routines
like setelt and zip-lists, deprecated features
like defkeyword, and an emulation of the old-style
multiple-values feature. See Old CL Compatibility.
Installation of the CL package is simple: Just put the
byte-compiled files cl.elc, cl-extra.elc,
cl-seq.elc, cl-macs.elc, and cl-compat.elc
into a directory on your load-path.
There are no special requirements to compile this package: The files do not have to be loaded before they are compiled, nor do they need to be compiled in any particular order.
You may choose to put the files into your main lisp/
directory, replacing the original cl.el file there. Or,
you could put them into a directory that comes before lisp/
on your load-path so that the old cl.el is
effectively hidden.
Also, format the cl.texinfo file and put the resulting Info files in the info/ directory or another suitable place.
You may instead wish to leave this package's components all in
their own directory, and then add this directory to your
load-path and Info-directory-list.
Add the directory to the front of the list so the old CL
package and its documentation are hidden.
Except where noted, all functions defined by this package have the same names and calling conventions as their Common Lisp counterparts.
Following is a complete list of functions whose names were changed from Common Lisp, usually to avoid conflicts with Emacs. In each case, a `*' has been appended to the Common Lisp name to obtain the Emacs name:
defun* defsubst* defmacro* function*
member* assoc* rassoc* get*
remove* delete* mapcar* sort*
floor* ceiling* truncate* round*
mod* rem* random*
Internal function and variable names in the package are prefixed
by cl-. Here is a complete list of functions not
prefixed by cl- which were not taken from Common Lisp:
floatp-safe lexical-let lexical-let*
callf callf2 letf letf*
defsubst*
The following simple functions and macros are defined in cl.el; they do not cause other components like cl-extra to be loaded.
eql floatp-safe endp
evenp oddp plusp minusp
caaar .. cddddr
list* ldiff rest first .. tenth
copy-list subst mapcar* [2]
adjoin [3] acons pairlis pop [4]
push [4] pushnew [3,4] incf [4] decf [4]
proclaim declaim
[2] Only for one sequence argument or two list arguments.
[3] Only if :test is eq, equal, or unspecified,
and :key is not used.
[4] Only when place is a plain variable name.
This section describes features of the CL package which have to
do with programs as a whole: advanced argument lists for functions,
and the eval-when construct.
Emacs Lisp's notation for argument lists of functions is a subset of
the Common Lisp notation. As well as the familiar &optional
and &rest markers, Common Lisp allows you to specify default
values for optional arguments, and it provides the additional markers
&key and &aux.
Since argument parsing is built-in to Emacs, there is no way for this package to implement Common Lisp argument lists seamlessly. Instead, this package defines alternates for several Lisp forms which you must use if you need Common Lisp argument lists.
This form is identical to the regular
defunform, except that arglist is allowed to be a full Common Lisp argument list. Also, the function body is enclosed in an implicit block called name; see Blocks and Exits.
This is just like
defun*, except that the function that is defined is automatically proclaimedinline, i.e., calls to it may be expanded into in-line code by the byte compiler. This is analogous to thedefsubstform;defsubst*uses a different method (compiler macros) which works in all version of Emacs, and also generates somewhat more efficient inline expansions. In particular,defsubst*arranges for the processing of keyword arguments, default values, etc., to be done at compile-time whenever possible.
This is identical to the regular
defmacroform, except that arglist is allowed to be a full Common Lisp argument list. The&environmentkeyword is supported as described in Steele. The&wholekeyword is supported only within destructured lists (see below); top-level&wholecannot be implemented with the current Emacs Lisp interpreter. The macro expander body is enclosed in an implicit block called name.
This is identical to the regular
functionform, except that if the argument is alambdaform then that form may use a full Common Lisp argument list.
Also, all forms (such as defsetf and flet) defined
in this package that include arglists in their syntax allow
full Common Lisp argument lists.
Note that it is not necessary to use defun* in
order to have access to most CL features in your function.
These features are always present; defun*'s only
difference from defun is its more flexible argument
lists and its implicit block.
The full form of a Common Lisp argument list is
(var...
&optional (var initform svar)...
&rest var
&key ((keyword var) initform svar)...
&aux (var initform)...)
Each of the five argument list sections is optional. The svar, initform, and keyword parts are optional; if they are omitted, then `(var)' may be written simply `var'.
The first section consists of zero or more required arguments. These arguments must always be specified in a call to the function; there is no difference between Emacs Lisp and Common Lisp as far as required arguments are concerned.
The second section consists of optional arguments. These
arguments may be specified in the function call; if they are not,
initform specifies the default value used for the argument.
(No initform means to use nil as the default.) The
initform is evaluated with the bindings for the preceding
arguments already established; (a &optional (b (1+ a)))
matches one or two arguments, with the second argument defaulting
to one plus the first argument. If the svar is specified,
it is an auxiliary variable which is bound to t if the optional
argument was specified, or to nil if the argument was omitted.
If you don't use an svar, then there will be no way for your
function to tell whether it was called with no argument, or with
the default value passed explicitly as an argument.
The third section consists of a single rest argument. If
more arguments were passed to the function than are accounted for
by the required and optional arguments, those extra arguments are
collected into a list and bound to the “rest” argument variable.
Common Lisp's &rest is equivalent to that of Emacs Lisp.
Common Lisp accepts &body as a synonym for &rest in
macro contexts; this package accepts it all the time.
The fourth section consists of keyword arguments. These are optional arguments which are specified by name rather than positionally in the argument list. For example,
(defun* foo (a &optional b &key c d (e 17)))
defines a function which may be called with one, two, or more
arguments. The first two arguments are bound to a and
b in the usual way. The remaining arguments must be
pairs of the form :c, :d, or :e followed
by the value to be bound to the corresponding argument variable.
(Symbols whose names begin with a colon are called keywords,
and they are self-quoting in the same way as nil and
t.)
For example, the call (foo 1 2 :d 3 :c 4) sets the five
arguments to 1, 2, 4, 3, and 17, respectively. If the same keyword
appears more than once in the function call, the first occurrence
takes precedence over the later ones. Note that it is not possible
to specify keyword arguments without specifying the optional
argument b as well, since (foo 1 :c 2) would bind
b to the keyword :c, then signal an error because
2 is not a valid keyword.
If a keyword symbol is explicitly specified in the argument list as shown in the above diagram, then that keyword will be used instead of just the variable name prefixed with a colon. You can specify a keyword symbol which does not begin with a colon at all, but such symbols will not be self-quoting; you will have to quote them explicitly with an apostrophe in the function call.
Ordinarily it is an error to pass an unrecognized keyword to
a function, e.g., (foo 1 2 :c 3 :goober 4). You can ask
Lisp to ignore unrecognized keywords, either by adding the
marker &allow-other-keys after the keyword section
of the argument list, or by specifying an :allow-other-keys
argument in the call whose value is non-nil. If the
function uses both &rest and &key at the same time,
the “rest” argument is bound to the keyword list as it appears
in the call. For example:
(defun* find-thing (thing &rest rest &key need &allow-other-keys)
(or (apply 'member* thing thing-list :allow-other-keys t rest)
(if need (error "Thing not found"))))
This function takes a :need keyword argument, but also
accepts other keyword arguments which are passed on to the
member* function. allow-other-keys is used to
keep both find-thing and member* from complaining
about each others' keywords in the arguments.
The fifth section of the argument list consists of auxiliary
variables. These are not really arguments at all, but simply
variables which are bound to nil or to the specified
initforms during execution of the function. There is no
difference between the following two functions, except for a
matter of stylistic taste:
(defun* foo (a b &aux (c (+ a b)) d)
body)
(defun* foo (a b)
(let ((c (+ a b)) d)
body))
Argument lists support destructuring. In Common Lisp,
destructuring is only allowed with defmacro; this package
allows it with defun* and other argument lists as well.
In destructuring, any argument variable (var in the above
diagram) can be replaced by a list of variables, or more generally,
a recursive argument list. The corresponding argument value must
be a list whose elements match this recursive argument list.
For example:
(defmacro* dolist ((var listform &optional resultform)
&rest body)
...)
This says that the first argument of dolist must be a list
of two or three items; if there are other arguments as well as this
list, they are stored in body. All features allowed in
regular argument lists are allowed in these recursive argument lists.
In addition, the clause `&whole var' is allowed at the
front of a recursive argument list. It binds var to the
whole list being matched; thus (&whole all a b) matches
a list of two things, with a bound to the first thing,
b bound to the second thing, and all bound to the
list itself. (Common Lisp allows &whole in top-level
defmacro argument lists as well, but Emacs Lisp does not
support this usage.)
One last feature of destructuring is that the argument list may be
dotted, so that the argument list (a b . c) is functionally
equivalent to (a b &rest c).
If the optimization quality safety is set to 0
(see Declarations), error checking for wrong number of
arguments and invalid keyword arguments is disabled. By default,
argument lists are rigorously checked.
Normally, the byte-compiler does not actually execute the forms in
a file it compiles. For example, if a file contains (setq foo t),
the act of compiling it will not actually set foo to t.
This is true even if the setq was a top-level form (i.e., not
enclosed in a defun or other form). Sometimes, though, you
would like to have certain top-level forms evaluated at compile-time.
For example, the compiler effectively evaluates defmacro forms
at compile-time so that later parts of the file can refer to the
macros that are defined.
This form controls when the body forms are evaluated. The situations list may contain any set of the symbols
compile,load, andeval(or their long-winded ANSI equivalents,:compile-toplevel,:load-toplevel, and:execute).The
eval-whenform is handled differently depending on whether or not it is being compiled as a top-level form. Specifically, it gets special treatment if it is being compiled by a command such asbyte-compile-filewhich compiles files or buffers of code, and it appears either literally at the top level of the file or inside a top-levelprogn.For compiled top-level
eval-whens, the body forms are executed at compile-time ifcompileis in the situations list, and the forms are written out to the file (to be executed at load-time) ifloadis in the situations list.For non-compiled-top-level forms, only the
evalsituation is relevant. (This includes forms executed by the interpreter, forms compiled withbyte-compilerather thanbyte-compile-file, and non-top-level forms.) Theeval-whenacts like aprognifevalis specified, and likenil(ignoring the body forms) if not.The rules become more subtle when
eval-whens are nested; consult Steele (second edition) for the gruesome details (and some gruesome examples).Some simple examples:
;; Top-level forms in foo.el: (eval-when (compile) (setq foo1 'bar)) (eval-when (load) (setq foo2 'bar)) (eval-when (compile load) (setq foo3 'bar)) (eval-when (eval) (setq foo4 'bar)) (eval-when (eval compile) (setq foo5 'bar)) (eval-when (eval load) (setq foo6 'bar)) (eval-when (eval compile load) (setq foo7 'bar))When foo.el is compiled, these variables will be set during the compilation itself:
foo1 foo3 foo5 foo7 ; `compile'When foo.elc is loaded, these variables will be set:
foo2 foo3 foo6 foo7 ; `load'And if foo.el is loaded uncompiled, these variables will be set:
foo4 foo5 foo6 foo7 ; `eval'If these seven
eval-whens had been, say, inside adefun, then the first three would have been equivalent toniland the last four would have been equivalent to the correspondingsetqs.Note that
(eval-when (load eval) ...)is equivalent to(progn ...)in all contexts. The compiler treats certain top-level forms, likedefmacro(sort-of) andrequire, as if they were wrapped in(eval-when (compile load eval) ...).
Emacs includes two special forms related to eval-when.
One of these, eval-when-compile, is not quite equivalent to
any eval-when construct and is described below.
The other form, (eval-and-compile ...), is exactly
equivalent to `(eval-when (compile load eval) ...)' and
so is not itself defined by this package.
The forms are evaluated at compile-time; at execution time, this form acts like a quoted constant of the resulting value. Used at top-level,
eval-when-compileis just like `eval-when (compile eval)'. In other contexts,eval-when-compileallows code to be evaluated once at compile-time for efficiency or other reasons.This form is similar to the `#.' syntax of true Common Lisp.
The form is evaluated at load-time; at execution time, this form acts like a quoted constant of the resulting value.
Early Common Lisp had a `#,' syntax that was similar to this, but ANSI Common Lisp replaced it with
load-time-valueand gave it more well-defined semantics.In a compiled file,
load-time-valuearranges for form to be evaluated when the .elc file is loaded and then used as if it were a quoted constant. In code compiled bybyte-compilerather thanbyte-compile-file, the effect is identical toeval-when-compile. In uncompiled code, botheval-when-compileandload-time-valueact exactly likeprogn.(defun report () (insert "This function was executed on: " (current-time-string) ", compiled on: " (eval-when-compile (current-time-string)) ;; or '#.(current-time-string) in real Common Lisp ", and loaded on: " (load-time-value (current-time-string))))Byte-compiled, the above defun will result in the following code (or its compiled equivalent, of course) in the .elc file:
(setq --temp-- (current-time-string)) (defun report () (insert "This function was executed on: " (current-time-string) ", compiled on: " '"Wed Jun 23 18:33:43 1993" ", and loaded on: " --temp--))
This section describes functions for testing whether various facts are true or false.
The CL package defines a version of the Common Lisp typep
predicate.
Check if object is of type type, where type is a (quoted) type name of the sort used by Common Lisp. For example,
(typep foo 'integer)is equivalent to(integerp foo).
The type argument to the above function is either a symbol or a list beginning with a symbol.
t stands for the union of all types.
(typep object t) is always true. Likewise, the
type symbol nil stands for nothing at all, and
(typep object nil) is always false.
null represents the symbol nil.
Thus (typep object 'null) is equivalent to
(null object).
atom represents all objects that are not cons
cells. Thus (typep object 'atom) is equivalent to
(atom object).
real is a synonym for number, and
fixnum is a synonym for integer.
character and string-char match
integers in the range from 0 to 255.
float uses the floatp-safe predicate
defined by this package rather than floatp, so it will work
correctly even in Emacs versions without floating-point support.
(integer low high) represents all
integers between low and high, inclusive. Either bound
may be a list of a single integer to specify an exclusive limit,
or a * to specify no limit. The type (integer * *)
is thus equivalent to integer.
float, real, or
number represent numbers of that type falling in a particular
range.
and, or, and not form
combinations of types. For example, (or integer (float 0 *))
represents all objects that are integers or non-negative floats.
member or member* represent
objects eql to any of the following values. For example,
(member 1 2 3 4) is equivalent to (integer 1 4),
and (member nil) is equivalent to null.
(satisfies predicate) represent
all objects for which predicate returns true when called
with that object as an argument.
The following function and macro (not technically predicates) are
related to typep.
This function attempts to convert object to the specified type. If object is already of that type as determined by
typep, it is simply returned. Otherwise, certain types of conversions will be made: If type is any sequence type (string,list, etc.) then object will be converted to that type if possible. If type ischaracter, then strings of length one and symbols with one-character names can be coerced. If type isfloat, then integers can be coerced in versions of Emacs that support floats. In all other circumstances,coercesignals an error.
This macro defines a new type called name. It is similar to
defmacroin many ways; when name is encountered as a type name, the body forms are evaluated and should return a type specifier that is equivalent to the type. The arglist is a Common Lisp argument list of the sort accepted bydefmacro*. The type specifier `(name args...)' is expanded by calling the expander with those arguments; the type symbol `name' is expanded by calling the expander with no arguments. The arglist is processed the same as fordefmacro*except that optional arguments without explicit defaults use*instead ofnilas the “default” default. Some examples:(deftype null () '(satisfies null)) ; predefined (deftype list () '(or null cons)) ; predefined (deftype unsigned-byte (&optional bits) (list 'integer 0 (if (eq bits '*) bits (1- (lsh 1 bits))))) (unsigned-byte 8) == (integer 0 255) (unsigned-byte) == (integer 0 *) unsigned-byte == (integer 0 *)The last example shows how the Common Lisp
unsigned-bytetype specifier could be implemented if desired; this package does not implementunsigned-byteby default.
The typecase and check-type macros also use type
names. See Conditionals. See Assertions. The map,
concatenate, and merge functions take type-name
arguments to specify the type of sequence to return. See Sequences.
This package defines two Common Lisp predicates, eql and
equalp.
This function is almost the same as
eq, except that if a and b are numbers of the same type, it compares them for numeric equality (as if byequalinstead ofeq). This makes a difference only for versions of Emacs that are compiled with floating-point support. Emacs floats are allocated objects just like cons cells, which means that(eq 3.0 3.0)will not necessarily be true—if the two3.0s were allocated separately, the pointers will be different even though the numbers are the same. But(eql 3.0 3.0)will always be true.The types of the arguments must match, so
(eql 3 3.0)is still false.Note that Emacs integers are “direct” rather than allocated, which basically means
(eq 3 3)will always be true. Thuseqandeqlbehave differently only if floating-point numbers are involved, and are indistinguishable on Emacs versions that don't support floats.There is a slight inconsistency with Common Lisp in the treatment of positive and negative zeros. Some machines, notably those with IEEE standard arithmetic, represent
+0and-0as distinct values. Normally this doesn't matter because the standard specifies that(= 0.0 -0.0)should always be true, and this is indeed what Emacs Lisp and Common Lisp do. But the Common Lisp standard states that(eql 0.0 -0.0)and(equal 0.0 -0.0)should be false on IEEE-like machines; Emacs Lisp does not do this, and in fact the only known way to distinguish between the two zeros in Emacs Lisp is toformatthem and check for a minus sign.
This function is a more flexible version of
equal. In particular, it compares strings case-insensitively, and it compares numbers without regard to type (so that(equalp 3 3.0)is true). Vectors and conses are compared recursively. All other objects are compared as if byequal.This function differs from Common Lisp
equalpin several respects. First, Common Lisp'sequalpalso compares characters case-insensitively, which would be impractical in this package since Emacs does not distinguish between integers and characters. In keeping with the idea that strings are less vector-like in Emacs Lisp, this package'sequalpalso will not compare strings against vectors of integers.
Also note that the Common Lisp functions member and assoc
use eql to compare elements, whereas Emacs Lisp follows the
MacLisp tradition and uses equal for these two functions.
In Emacs, use member* and assoc* to get functions
which use eql for comparisons.
The features described in the following sections implement
various advanced control structures, including the powerful
setf facility and a number of looping and conditional
constructs.
The psetq form is just like setq, except that multiple
assignments are done in parallel rather than sequentially.
This special form (actually a macro) is used to assign to several variables simultaneously. Given only one symbol and form, it has the same effect as
setq. Given several symbol and form pairs, it evaluates all the forms in advance and then stores the corresponding variables afterwards.(setq x 2 y 3) (setq x (+ x y) y (* x y)) x => 5 y ;ywas computed afterxwas set. => 15 (setq x 2 y 3) (psetq x (+ x y) y (* x y)) x => 5 y ;ywas computed beforexwas set. => 6The simplest use of
psetqis(psetq x y y x), which exchanges the values of two variables. (Therotatefform provides an even more convenient way to swap two variables; see Modify Macros.)
psetqalways returnsnil.
A “generalized variable” or “place form” is one of the many places in Lisp memory where values can be stored. The simplest place form is a regular Lisp variable. But the cars and cdrs of lists, elements of arrays, properties of symbols, and many other locations are also places where Lisp values are stored.
The setf form is like setq, except that it accepts
arbitrary place forms on the left side rather than just
symbols. For example, (setf (car a) b) sets the car of
a to b, doing the same operation as (setcar a b)
but without having to remember two separate functions for setting
and accessing every type of place.
Generalized variables are analogous to “lvalues” in the C
language, where `x = a[i]' gets an element from an array
and `a[i] = x' stores an element using the same notation.
Just as certain forms like a[i] can be lvalues in C, there
is a set of forms that can be generalized variables in Lisp.
The setf macro is the most basic way to operate on generalized
variables.
This macro evaluates form and stores it in place, which must be a valid generalized variable form. If there are several place and form pairs, the assignments are done sequentially just as with
setq.setfreturns the value of the last form.The following Lisp forms will work as generalized variables, and so may appear in the place argument of
setf:
- A symbol naming a variable. In other words,
(setf x y)is exactly equivalent to(setq x y), andsetqitself is strictly speaking redundant now thatsetfexists. Many programmers continue to prefersetqfor setting simple variables, though, purely for stylistic or historical reasons. The macro(setf x y)actually expands to(setq x y), so there is no performance penalty for using it in compiled code.- A call to any of the following Lisp functions:
car cdr caar .. cddddr nth rest first .. tenth aref elt nthcdr symbol-function symbol-value symbol-plist get get* getf gethash subseqNote that for
nthcdrandgetf, the list argument of the function must itself be a valid place form. For example,(setf (nthcdr 0 foo) 7)will setfooitself to 7. Note thatpushandpopon annthcdrplace can be used to insert or delete at any position in a list. The use ofnthcdras a place form is an extension to standard Common Lisp.- The following Emacs-specific functions are also
setf-able.buffer-file-name marker-position buffer-modified-p match-data buffer-name mouse-position buffer-string overlay-end buffer-substring overlay-get current-buffer overlay-start current-case-table point current-column point-marker current-global-map point-max current-input-mode point-min current-local-map process-buffer current-window-configuration process-filter default-file-modes process-sentinel default-value read-mouse-position documentation-property screen-height extent-data screen-menubar extent-end-position screen-width extent-start-position selected-window face-background selected-screen face-background-pixmap selected-frame face-font standard-case-table face-foreground syntax-table face-underline-p window-buffer file-modes window-dedicated-p frame-height window-display-table frame-parameters window-height frame-visible-p window-hscroll frame-width window-point get-register window-start getenv window-width global-key-binding x-get-cut-buffer keymap-parent x-get-cutbuffer local-key-binding x-get-secondary-selection mark x-get-selection mark-markerMost of these have directly corresponding “set” functions, like
use-local-mapforcurrent-local-map, orgoto-charforpoint. A few, likepoint-min, expand to longer sequences of code when they aresetf'd ((narrow-to-region x (point-max))in this case).- A call of the form
(substringsubplace n[m]), where subplace is itself a valid generalized variable whose current value is a string, and where the value stored is also a string. The new string is spliced into the specified part of the destination string. For example:(setq a (list "hello" "world")) => ("hello" "world") (cadr a) => "world" (substring (cadr a) 2 4) => "rl" (setf (substring (cadr a) 2 4) "o") => "o" (cadr a) => "wood" a => ("hello" "wood")The generalized variable
buffer-substring, listed above, also works in this way by replacing a portion of the current buffer.- A call of the form
(apply 'func...)or(apply (functionfunc) ...), where func is asetf-able function whose store function is “suitable” in the sense described in Steele's book; since none of the standard Emacs place functions are suitable in this sense, this feature is only interesting when used with places you define yourself withdefine-setf-methodor the long form ofdefsetf.- A macro call, in which case the macro is expanded and
setfis applied to the resulting form.- Any form for which a
defsetfordefine-setf-methodhas been made.Using any forms other than these in the place argument to
setfwill signal an error.The
setfmacro takes care to evaluate all subforms in the proper left-to-right order; for example,(setf (aref vec (incf i)) i)looks like it will evaluate
(incf i)exactly once, before the following access toi; thesetfexpander will insert temporary variables as necessary to ensure that it does in fact work this way no matter what setf-method is defined foraref. (In this case,asetwould be used and no such steps would be necessary sinceasettakes its arguments in a convenient order.)However, if the place form is a macro which explicitly evaluates its arguments in an unusual order, this unusual order will be preserved. Adapting an example from Steele, given
(defmacro wrong-order (x y) (list 'aref y x))the form
(setf (wrong-ordera b) 17)will evaluate b first, then a, just as in an actual call towrong-order.
This package defines a number of other macros besides setf
that operate on generalized variables. Many are interesting and
useful even when the place is just a variable name.
This macro is to
setfwhatpsetqis tosetq: When several places and forms are involved, the assignments take place in parallel rather than sequentially. Specifically, all subforms are evaluated from left to right, then all the assignments are done (in an undefined order).
This macro increments the number stored in place by one, or by x if specified. The incremented value is returned. For example,
(incf i)is equivalent to(setq i (1+ i)), and(incf (car x) 2)is equivalent to(setcar x (+ (car x) 2)).Once again, care is taken to preserve the “apparent” order of evaluation. For example,
(incf (aref vec (incf i)))appears to increment
ionce, then increment the element ofvecaddressed byi; this is indeed exactly what it does, which means the above form is not equivalent to the “obvious” expansion,(setf (aref vec (incf i)) (1+ (aref vec (incf i)))) ; Wrong!but rather to something more like
(let ((temp (incf i))) (setf (aref vec temp) (1+ (aref vec temp))))Again, all of this is taken care of automatically by
incfand the other generalized-variable macros.As a more Emacs-specific example of
incf, the expression(incf (point)n)is essentially equivalent to(forward-charn).
This macro decrements the number stored in place by one, or by x if specified.
This macro removes and returns the first element of the list stored in place. It is analogous to
(prog1 (carplace) (setfplace(cdrplace))), except that it takes care to evaluate all subforms only once.
This macro inserts x at the front of the list stored in place. It is analogous to
(setfplace(consx place)), except for evaluation of the subforms.
This macro inserts x at the front of the list stored in place, but only if x was not
eqlto any existing element of the list. The optional keyword arguments are interpreted in the same way as foradjoin. See Lists as Sets.
This macro shifts the places left by one, shifting in the value of newvalue (which may be any Lisp expression, not just a generalized variable), and returning the value shifted out of the first place. Thus,
(shiftfa b c d)is equivalent to(prog1 a (psetf a b b c c d))except that the subforms of a, b, and c are actually evaluated only once each and in the apparent order.
This macro rotates the places left by one in circular fashion. Thus,
(rotatefa b c d)is equivalent to(psetf a b b c c d d a)except for the evaluation of subforms.
rotatefalways returnsnil. Note that(rotatefa b)conveniently exchanges a and b.
The following macros were invented for this package; they have no analogues in Common Lisp.
This macro is analogous to
let, but for generalized variables rather than just symbols. Each binding should be of the form(place value); the original contents of the places are saved, the values are stored in them, and then the body forms are executed. Afterwards, the places are set back to their original saved contents. This cleanup happens even if the forms exit irregularly due to athrowor an error.For example,
(letf (((point) (point-min)) (a 17)) ...)moves “point” in the current buffer to the beginning of the buffer, and also binds
ato 17 (as if by a normallet, sinceais just a regular variable). After the body exits,ais set back to its original value and point is moved back to its original position.Note that
letfon(point)is not quite like asave-excursion, as the latter effectively saves a marker which tracks insertions and deletions in the buffer. Actually, aletfof(point-marker)is much closer to this behavior. (pointandpoint-markerare equivalent assetfplaces; each will accept either an integer or a marker as the stored value.)Since generalized variables look like lists,
let's shorthand of using `foo' for `(foo nil)' as a binding would be ambiguous inletfand is not allowed.However, a binding specifier may be a one-element list `(place)', which is similar to `(place place)'. In other words, the place is not disturbed on entry to the body, and the only effect of the
letfis to restore the original value of place afterwards. (The redundant access-and-store suggested by the(place place)example does not actually occur.)In most cases, the place must have a well-defined value on entry to the
letfform. The only exceptions are plain variables and calls tosymbol-valueandsymbol-function. If the symbol is not bound on entry, it is simply made unbound bymakunboundorfmakunboundon exit.
This macro is to
letfwhatlet*is tolet: It does the bindings in sequential rather than parallel order.
This is the “generic” modify macro. It calls function, which should be an unquoted function name, macro name, or lambda. It passes place and args as arguments, and assigns the result back to place. For example,
(incfplace n)is the same as(callf +place n). Some more examples:(callf abs my-number) (callf concat (buffer-name) "<" (int-to-string n) ">") (callf union happy-people (list joe bob) :test 'same-person)See Customizing Setf, for
define-modify-macro, a way to create even more concise notations for modify macros. Note again thatcallfis an extension to standard Common Lisp.
This macro is like
callf, except that place is the second argument of function rather than the first. For example,(pushx place)is equivalent to(callf2 consx place).
The callf and callf2 macros serve as building
blocks for other macros like incf, pushnew, and
define-modify-macro. The letf and letf*
macros are used in the processing of symbol macros;
see Macro Bindings.
Common Lisp defines three macros, define-modify-macro,
defsetf, and define-setf-method, that allow the
user to extend generalized variables in various ways.
This macro defines a “read-modify-write” macro similar to
incfanddecf. The macro name is defined to take a place argument followed by additional arguments described by arglist. The call(name place args...)will be expanded to
(callf func place args...)which in turn is roughly equivalent to
(setf place (func place args...))For example:
(define-modify-macro incf (&optional (n 1)) +) (define-modify-macro concatf (&rest args) concat)Note that
&keyis not allowed in arglist, but&restis sufficient to pass keywords on to the function.Most of the modify macros defined by Common Lisp do not exactly follow the pattern of
define-modify-macro. For example,pushtakes its arguments in the wrong order, andpopis completely irregular. You can define these macros “by hand” usingget-setf-method, or consult the source file cl-macs.el to see how to use the internalsetfbuilding blocks.
This is the simpler of two
defsetfforms. Where access-fn is the name of a function which accesses a place, this declares update-fn to be the corresponding store function. From now on,(setf (access-fn arg1 arg2 arg3) value)will be expanded to
(update-fn arg1 arg2 arg3 value)The update-fn is required to be either a true function, or a macro which evaluates its arguments in a function-like way. Also, the update-fn is expected to return value as its result. Otherwise, the above expansion would not obey the rules for the way
setfis supposed to behave.As a special (non-Common-Lisp) extension, a third argument of
ttodefsetfsays that theupdate-fn's return value is not suitable, so that the abovesetfshould be expanded to something more like(let ((temp value)) (update-fn arg1 arg2 arg3 temp) temp)Some examples of the use of
defsetf, drawn from the standard suite of setf methods, are:(defsetf car setcar) (defsetf symbol-value set) (defsetf buffer-name rename-buffer t)
This is the second, more complex, form of
defsetf. It is rather likedefmacroexcept for the additional store-var argument. The forms should return a Lisp form which stores the value of store-var into the generalized variable formed by a call to access-fn with arguments described by arglist. The forms may begin with a string which documents thesetfmethod (analogous to the doc string that appears at the front of a function).For example, the simple form of
defsetfis shorthand for(defsetf access-fn (&rest args) (store) (append '(update-fn) args (list store)))The Lisp form that is returned can access the arguments from arglist and store-var in an unrestricted fashion; macros like
setfandincfwhich invoke this setf-method will insert temporary variables as needed to make sure the apparent order of evaluation is preserved.Another example drawn from the standard package:
(defsetf nth (n x) (store) (list 'setcar (list 'nthcdr n x) store))
This is the most general way to create new place forms. When a
setfto access-fn with arguments described by arglist is expanded, the forms are evaluated and must return a list of five items:
- A list of temporary variables.
- A list of value forms corresponding to the temporary variables above. The temporary variables will be bound to these value forms as the first step of any operation on the generalized variable.
- A list of exactly one store variable (generally obtained from a call to
gensym).- A Lisp form which stores the contents of the store variable into the generalized variable, assuming the temporaries have been bound as described above.
- A Lisp form which accesses the contents of the generalized variable, assuming the temporaries have been bound.
This is exactly like the Common Lisp macro of the same name, except that the method returns a list of five values rather than the five values themselves, since Emacs Lisp does not support Common Lisp's notion of multiple return values.
Once again, the forms may begin with a documentation string.
A setf-method should be maximally conservative with regard to temporary variables. In the setf-methods generated by
defsetf, the second return value is simply the list of arguments in the place form, and the first return value is a list of a corresponding number of temporary variables generated bygensym. Macros likesetfandincfwhich use this setf-method will optimize away most temporaries that turn out to be unnecessary, so there is little reason for the setf-method itself to optimize.
This function returns the setf-method for place, by invoking the definition previously recorded by
defsetfordefine-setf-method. The result is a list of five values as described above. You can use this function to build your ownincf-like modify macros. (Actually, it is better to use the internal functionscl-setf-do-modifyandcl-setf-do-store, which are a bit easier to use and which also do a number of optimizations; consult the source code for theincffunction for a simple example.)The argument env specifies the “environment” to be passed on to
macroexpandifget-setf-methodshould need to expand a macro in place. It should come from an&environmentargument to the macro or setf-method that calledget-setf-method.See also the source code for the setf-methods for
applyandsubstring, each of which works by callingget-setf-methodon a simpler case, then massaging the result in various ways.
Modern Common Lisp defines a second, independent way to specify
the setf behavior of a function, namely “setf
functions” whose names are lists (setf name)
rather than symbols. For example, (defun (setf foo) ...)
defines the function that is used when setf is applied to
foo. This package does not currently support setf
functions. In particular, it is a compile-time error to use
setf on a form which has not already been defsetf'd
or otherwise declared; in newer Common Lisps, this would not be
an error since the function (setf func) might be
defined later.
These Lisp forms make bindings to variables and function names,
analogous to Lisp's built-in let form.
See Modify Macros, for the letf and letf* forms which
are also related to variable bindings.
The standard let form binds variables whose names are known
at compile-time. The progv form provides an easy way to
bind variables whose names are computed at run-time.
This form establishes
let-style variable bindings on a set of variables computed at run-time. The expressions symbols and values are evaluated, and must return lists of symbols and values, respectively. The symbols are bound to the corresponding values for the duration of the body forms. If values is shorter than symbols, the last few symbols are made unbound (as if bymakunbound) inside the body. If symbols is shorter than values, the excess values are ignored.
The CL package defines the following macro which
more closely follows the Common Lisp let form:
This form is exactly like
letexcept that the bindings it establishes are purely lexical. Lexical bindings are similar to local variables in a language like C: Only the code physically within the body of thelexical-let(after macro expansion) may refer to the bound variables.(setq a 5) (defun foo (b) (+ a b)) (let ((a 2)) (foo a)) => 4 (lexical-let ((a 2)) (foo a)) => 7In this example, a regular
letbinding ofaactually makes a temporary change to the global variablea, sofoois able to see the binding ofato 2. Butlexical-letactually creates a distinct local variableafor use within its body, without any effect on the global variable of the same name.The most important use of lexical bindings is to create closures. A closure is a function object that refers to an outside lexical variable. For example:
(defun make-adder (n) (lexical-let ((n n)) (function (lambda (m) (+ n m))))) (setq add17 (make-adder 17)) (funcall add17 4) => 21The call
(make-adder 17)returns a function object which adds 17 to its argument. Iflethad been used instead oflexical-let, the function object would have referred to the globaln, which would have been bound to 17 only during the call tomake-adderitself.(defun make-counter () (lexical-let ((n 0)) (function* (lambda (&optional (m 1)) (incf n m))))) (setq count-1 (make-counter)) (funcall count-1 3) => 3 (funcall count-1 14) => 17 (setq count-2 (make-counter)) (funcall count-2 5) => 5 (funcall count-1 2) => 19 (funcall count-2) => 6Here we see that each call to
make-countercreates a distinct local variablen, which serves as a private counter for the function object that is returned.Closed-over lexical variables persist until the last reference to them goes away, just like all other Lisp objects. For example,
count-2refers to a function object which refers to an instance of the variablen; this is the only reference to that variable, so after(setq count-2 nil)the garbage collector would be able to delete this instance ofn. Of course, if alexical-letdoes not actually create any closures, then the lexical variables are free as soon as thelexical-letreturns.Many closures are used only during the extent of the bindings they refer to; these are known as “downward funargs” in Lisp parlance. When a closure is used in this way, regular Emacs Lisp dynamic bindings suffice and will be more efficient than
lexical-letclosures:(defun add-to-list (x list) (mapcar (lambda (y) (+ x y))) list) (add-to-list 7 '(1 2 5)) => (8 9 12)Since this lambda is only used while
xis still bound, it is not necessary to make a true closure out of it.You can use
defunorfletinside alexical-letto create a named closure. If several closures are created in the body of a singlelexical-let, they all close over the same instance of the lexical variable.The
lexical-letform is an extension to Common Lisp. In true Common Lisp, all bindings are lexical unless declared otherwise.
This form is just like
lexical-let, except that the bindings are made sequentially in the manner oflet*.
These forms make let-like bindings to functions instead
of variables.
This form establishes
let-style bindings on the function cells of symbols rather than on the value cells. Each binding must be a list of the form `(name arglist forms...)', which defines a function exactly as if it were adefun*form. The function name is defined accordingly for the duration of the body of theflet; then the old function definition, or lack thereof, is restored.While
fletin Common Lisp establishes a lexical binding of name, Emacs Lispfletmakes a dynamic binding. The result is thatfletaffects indirect calls to a function as well as calls directly inside thefletform itself.You can use
fletto disable or modify the behavior of a function in a temporary fashion. This will even work on Emacs primitives, although note that some calls to primitive functions internal to Emacs are made without going through the symbol's function cell, and so will not be affected byflet. For example,(flet ((message (&rest args) (push args saved-msgs))) (do-something))This code attempts to replace the built-in function
messagewith a function that simply saves the messages in a list rather than displaying them. The original definition ofmessagewill be restored afterdo-somethingexits. This code will work fine on messages generated by other Lisp code, but messages generated directly inside Emacs will not be caught since they make direct C-language calls to the message routines rather than going through the Lispmessagefunction.Functions defined by
fletmay use the full Common Lisp argument notation supported bydefun*; also, the function body is enclosed in an implicit block as if bydefun*. See Program Structure.
The
labelsform is likeflet, except that it makes lexical bindings of the function names rather than dynamic bindings. (In true Common Lisp, bothfletandlabelsmake lexical bindings of slightly different sorts; since Emacs Lisp is dynamically bound by default, it seemed more appropriate forfletalso to use dynamic binding. Thelabelsform, with its lexical binding, is fully compatible with Common Lisp.)Lexical scoping means that all references to the named functions must appear physically within the body of the
labelsform. References may appear both in the body forms oflabelsitself, and in the bodies of the functions themselves. Thus,labelscan define local recursive functions, or mutually-recursive sets of functions.A “reference” to a function name is either a call to that function, or a use of its name quoted by
quoteorfunctionto be passed on to, say,mapcar.
These forms create local macros and “symbol macros.”
This form is analogous to
flet, but for macros instead of functions. Each binding is a list of the same form as the arguments todefmacro*(i.e., a macro name, argument list, and macro-expander forms). The macro is defined accordingly for use within the body of themacrolet.Because of the nature of macros,
macroletis lexically scoped even in Emacs Lisp: Themacroletbinding will affect only calls that appear physically within the body forms, possibly after expansion of other macros in the body.
This form creates symbol macros, which are macros that look like variable references rather than function calls. Each binding is a list `(var expansion)'; any reference to var within the body forms is replaced by expansion.
(setq bar '(5 . 9)) (symbol-macrolet ((foo (car bar))) (incf foo)) bar => (6 . 9)A
setqof a symbol macro is treated the same as asetf. I.e.,(setq foo 4)in the above would be equivalent to(setf foo 4), which in turn expands to(setf (car bar) 4).Likewise, a
letorlet*binding a symbol macro is treated like aletforletf*. This differs from true Common Lisp, where the rules of lexical scoping cause aletbinding to shadow asymbol-macroletbinding. In this package, onlylexical-letandlexical-let*will shadow a symbol macro.There is no analogue of
defmacrofor symbol macros; all symbol macros are local. A typical use ofsymbol-macroletis in the expansion of another macro:(defmacro* my-dolist ((x list) &rest body) (let ((var (gensym))) (list 'loop 'for var 'on list 'do (list* 'symbol-macrolet (list (list x (list 'car var))) body)))) (setq mylist '(1 2 3 4)) (my-dolist (x mylist) (incf x)) mylist => (2 3 4 5)In this example, the
my-dolistmacro is similar todolist(see Iteration) except that the variablexbecomes a true reference onto the elements of the list. Themy-dolistcall shown here expands to(loop for G1234 on mylist do (symbol-macrolet ((x (car G1234))) (incf x)))which in turn expands to
(loop for G1234 on mylist do (incf (car G1234)))See Loop Facility, for a description of the
loopmacro. This package defines a nonstandardin-refloop clause that works much likemy-dolist.
These conditional forms augment Emacs Lisp's simple if,
and, or, and cond forms.
This macro evaluates keyform, then compares it with the key values listed in the various clauses. Whichever clause matches the key is executed; comparison is done by
eql. If no clause matches, thecaseform returnsnil. The clauses are of the form(keylist body-forms...)where keylist is a list of key values. If there is exactly one value, and it is not a cons cell or the symbol
nilort, then it can be used by itself as a keylist without being enclosed in a list. All key values in thecaseform must be distinct. The final clauses may usetin place of a keylist to indicate a default clause that should be taken if none of the other clauses match. (The symbolotherwiseis also recognized in place oft. To make a clause that matches the actual symbolt,nil, orotherwise, enclose the symbol in a list.)For example, this expression reads a keystroke, then does one of four things depending on whether it is an `a', a `b', a <RET> or C-j, or anything else.
(case (read-char) (?a (do-a-thing)) (?b (do-b-thing)) ((?\r ?\n) (do-ret-thing)) (t (do-other-thing)))
This macro is just like
case, except that if the key does not match any of the clauses, an error is signaled rather than simply returningnil.
This macro is a version of
casethat checks for types rather than values. Each clause is of the form `(type body...)'. See Type Predicates, for a description of type specifiers. For example,(typecase x (integer (munch-integer x)) (float (munch-float x)) (string (munch-integer (string-to-int x))) (t (munch-anything x)))The type specifier
tmatches any type of object; the wordotherwiseis also allowed. To make one clause match any of several types, use an(or ...)type specifier.
This macro is just like
typecase, except that if the key does not match any of the clauses, an error is signaled rather than simply returningnil.
Common Lisp blocks provide a non-local exit mechanism very
similar to catch and throw, but lexically rather than
dynamically scoped. This package actually implements block
in terms of catch; however, the lexical scoping allows the
optimizing byte-compiler to omit the costly catch step if the
body of the block does not actually return-from the block.
The forms are evaluated as if by a
progn. However, if any of the forms execute(return-fromname), they will jump out and return directly from theblockform. Theblockreturns the result of the last form unless areturn-fromoccurs.The
block/return-frommechanism is quite similar to thecatch/throwmechanism. The main differences are that block names are unevaluated symbols, rather than forms (such as quoted symbols) which evaluate to a tag at run-time; and also that blocks are lexically scoped whereascatch/throware dynamically scoped. This means that functions called from the body of acatchcan alsothrowto thecatch, but thereturn-fromreferring to a block name must appear physically within the forms that make up the body of the block. They may not appear within other called functions, although they may appear within macro expansions orlambdas in the body. Block names andcatchnames form independent name-spaces.In true Common Lisp,
defunanddefmacrosurround the function or expander bodies with implicit blocks with the same name as the function or macro. This does not occur in Emacs Lisp, but this package providesdefun*anddefmacro*forms which do create the implicit block.The Common Lisp looping constructs defined by this package, such as
loopanddolist, also create implicit blocks just as in Common Lisp.Because they are implemented in terms of Emacs Lisp
catchandthrow, blocks have the same overhead as actualcatchconstructs (roughly two function calls). However, the optimizing byte compiler will optimize away thecatchif the block does not in fact contain anyreturnorreturn-fromcalls that jump to it. This means thatdoloops anddefun*functions which don't usereturndon't pay the overhead to support it.
This macro returns from the block named name, which must be an (unevaluated) symbol. If a result form is specified, it is evaluated to produce the result returned from the
block. Otherwise,nilis returned.
This macro is exactly like
(return-from nilresult). Common Lisp loops likedoanddolistimplicitly enclose themselves innilblocks.
The macros described here provide more sophisticated, high-level
looping constructs to complement Emacs Lisp's basic while
loop.
The CL package supports both the simple, old-style meaning of
loopand the extremely powerful and flexible feature known as the Loop Facility or Loop Macro. This more advanced facility is discussed in the following section; see Loop Facility. The simple form ofloopis described here.If
loopis followed by zero or more Lisp expressions, then(loopexprs...)simply creates an infinite loop executing the expressions over and over. The loop is enclosed in an implicitnilblock. Thus,(loop (foo) (if (no-more) (return 72)) (bar))is exactly equivalent to
(block nil (while t (foo) (if (no-more) (return 72)) (bar)))If any of the expressions are plain symbols, the loop is instead interpreted as a Loop Macro specification as described later. (This is not a restriction in practice, since a plain symbol in the above notation would simply access and throw away the value of a variable.)
This macro creates a general iterative loop. Each spec is of the form
(var [init [step]])The loop works as follows: First, each var is bound to the associated init value as if by a
letform. Then, in each iteration of the loop, the end-test is evaluated; if true, the loop is finished. Otherwise, the body forms are evaluated, then each var is set to the associated step expression (as if by apsetqform) and the next iteration begins. Once the end-test becomes true, the result forms are evaluated (with the vars still bound to their values) to produce the result returned bydo.The entire
doloop is enclosed in an implicitnilblock, so that you can use(return)to break out of the loop at any time.If there are no result forms, the loop returns
nil. If a given var has no step form, it is bound to its init value but not otherwise modified during thedoloop (unless the code explicitly modifies it); this case is just a shorthand for putting a(let ((var init)) ...)around the loop. If init is also omitted it defaults tonil, and in this case a plain `var' can be used in place of `(var)', again following the analogy withlet.This example (from Steele) illustrates a loop which applies the function
fto successive pairs of values from the listsfooandbar; it is equivalent to the call(mapcar* 'f foo bar). Note that this loop has no body forms at all, performing all its work as side effects of the rest of the loop.(do ((x foo (cdr x)) (y bar (cdr y)) (z nil (cons (f (car x) (car y)) z))) ((or (null x) (null y)) (nreverse z)))
This is to
dowhatlet*is tolet. In particular, the initial values are bound as if bylet*rather thanlet, and the steps are assigned as if bysetqrather thanpsetq.Here is another way to write the above loop:
(do* ((xp foo (cdr xp)) (yp bar (cdr yp)) (x (car xp) (car xp)) (y (car yp) (car yp)) z) ((or (null xp) (null yp)) (nreverse z)) (push (f x y) z))
This is a more specialized loop which iterates across the elements of a list. list should evaluate to a list; the body forms are executed with var bound to each element of the list in turn. Finally, the result form (or
nil) is evaluated with var bound tonilto produce the result returned by the loop. Unlike with Emacs's built indolist, the loop is surrounded by an implicitnilblock.
This is a more specialized loop which iterates a specified number of times. The body is executed with var bound to the integers from zero (inclusive) to count (exclusive), in turn. Then the
resultform is evaluated with var bound to the total number of iterations that were done (i.e.,(max 0count)) to get the return value for the loop form. Unlike with Emacs's built indolist, the loop is surrounded by an implicitnilblock.
This loop iterates over all interned symbols. If obarray is specified and is not
nil, it loops over all symbols in that obarray. For each symbol, the body forms are evaluated with var bound to that symbol. The symbols are visited in an unspecified order. Afterward the result form, if any, is evaluated (with var bound tonil) to get the return value. The loop is surrounded by an implicitnilblock.
This is identical to
do-symbolsexcept that the obarray argument is omitted; it always iterates over the default obarray.
See Mapping over Sequences, for some more functions for iterating over vectors or lists.
A common complaint with Lisp's traditional looping constructs is
that they are either too simple and limited, such as Common Lisp's
dotimes or Emacs Lisp's while, or too unreadable and
obscure, like Common Lisp's do loop.
To remedy this, recent versions of Common Lisp have added a new
construct called the “Loop Facility” or “loop macro,”
with an easy-to-use but very powerful and expressive syntax.
The loop macro essentially creates a mini-language within
Lisp that is specially tailored for describing loops. While this
language is a little strange-looking by the standards of regular Lisp,
it turns out to be very easy to learn and well-suited to its purpose.
Since loop is a macro, all parsing of the loop language
takes place at byte-compile time; compiled loops are just
as efficient as the equivalent while loops written longhand.
A loop construct consists of a series of clauses, each introduced by a symbol like
forordo. Clauses are simply strung together in the argument list ofloop, with minimal extra parentheses. The various types of clauses specify initializations, such as the binding of temporary variables, actions to be taken in the loop, stepping actions, and final cleanup.Common Lisp specifies a certain general order of clauses in a loop:
(loop name-clause var-clauses... action-clauses...)The name-clause optionally gives a name to the implicit block that surrounds the loop. By default, the implicit block is named
nil. The var-clauses specify what variables should be bound during the loop, and how they should be modified or iterated throughout the course of the loop. The action-clauses are things to be done during the loop, such as computing, collecting, and returning values.The Emacs version of the
loopmacro is less restrictive about the order of clauses, but things will behave most predictably if you put the variable-binding clauseswith,for, andrepeatbefore the action clauses. As in Common Lisp,initiallyandfinallyclauses can go anywhere.Loops generally return
nilby default, but you can cause them to return a value by using an accumulation clause likecollect, an end-test clause likealways, or an explicitreturnclause to jump out of the implicit block. (Because the loop body is enclosed in an implicit block, you can also use regular Lispreturnorreturn-fromto break out of the loop.)
The following sections give some examples of the Loop Macro in
action, and describe the particular loop clauses in great detail.
Consult the second edition of Steele's Common Lisp, the Language,
for additional discussion and examples of the loop macro.
Before listing the full set of clauses that are allowed, let's
look at a few example loops just to get a feel for the loop
language.
(loop for buf in (buffer-list)
collect (buffer-file-name buf))
This loop iterates over all Emacs buffers, using the list
returned by buffer-list. For each buffer buf,
it calls buffer-file-name and collects the results into
a list, which is then returned from the loop construct.
The result is a list of the file names of all the buffers in
Emacs' memory. The words for, in, and collect
are reserved words in the loop language.
(loop repeat 20 do (insert "Yowsa\n"))
This loop inserts the phrase “Yowsa” twenty times in the current buffer.
(loop until (eobp) do (munch-line) (forward-line 1))
This loop calls munch-line on every line until the end
of the buffer. If point is already at the end of the buffer,
the loop exits immediately.
(loop do (munch-line) until (eobp) do (forward-line 1))
This loop is similar to the above one, except that munch-line
is always called at least once.
(loop for x from 1 to 100
for y = (* x x)
until (>= y 729)
finally return (list x (= y 729)))
This more complicated loop searches for a number x whose
square is 729. For safety's sake it only examines x
values up to 100; dropping the phrase `to 100' would
cause the loop to count upwards with no limit. The second
for clause defines y to be the square of x
within the loop; the expression after the = sign is
reevaluated each time through the loop. The until
clause gives a condition for terminating the loop, and the
finally clause says what to do when the loop finishes.
(This particular example was written less concisely than it
could have been, just for the sake of illustration.)
Note that even though this loop contains three clauses (two
fors and an until) that would have been enough to
define loops all by themselves, it still creates a single loop
rather than some sort of triple-nested loop. You must explicitly
nest your loop constructs if you want nested loops.
Most loops are governed by one or more for clauses.
A for clause simultaneously describes variables to be
bound, how those variables are to be stepped during the loop,
and usually an end condition based on those variables.
The word as is a synonym for the word for. This
word is followed by a variable name, then a word like from
or across that describes the kind of iteration desired.
In Common Lisp, the phrase being the sometimes precedes
the type of iteration; in this package both being and
the are optional. The word each is a synonym
for the, and the word that follows it may be singular
or plural: `for x being the elements of y' or
`for x being each element of y'. Which form you use
is purely a matter of style.
The variable is bound around the loop as if by let:
(setq i 'happy)
(loop for i from 1 to 10 do (do-something-with i))
i
=> happy
for var from expr1 to expr2 by expr3for clause creates a counting loop. Each of
the three sub-terms is optional, though there must be at least one
term so that the clause is marked as a counting clause.
The three expressions are the starting value, the ending value, and
the step value, respectively, of the variable. The loop counts
upwards by default (expr3 must be positive), from expr1
to expr2 inclusively. If you omit the from term, the
loop counts from zero; if you omit the to term, the loop
counts forever without stopping (unless stopped by some other
loop clause, of course); if you omit the by term, the loop
counts in steps of one.
You can replace the word from with upfrom or
downfrom to indicate the direction of the loop. Likewise,
you can replace to with upto or downto.
For example, `for x from 5 downto 1' executes five times
with x taking on the integers from 5 down to 1 in turn.
Also, you can replace to with below or above,
which are like upto and downto respectively except
that they are exclusive rather than inclusive limits:
(loop for x to 10 collect x)
=> (0 1 2 3 4 5 6 7 8 9 10)
(loop for x below 10 collect x)
=> (0 1 2 3 4 5 6 7 8 9)
The by value is always positive, even for downward-counting
loops. Some sort of from value is required for downward
loops; `for x downto 5' is not a valid loop clause all by
itself.
for var in list by functionby term, then function
is used to traverse the list instead of cdr; it must be a
function taking one argument. For example:
(loop for x in '(1 2 3 4 5 6) collect (* x x))
=> (1 4 9 16 25 36)
(loop for x in '(1 2 3 4 5 6) by 'cddr collect (* x x))
=> (1 9 25)
for var on list by function (loop for x on '(1 2 3 4) collect x)
=> ((1 2 3 4) (2 3 4) (3 4) (4))
With by, there is no real reason that the on expression
must be a list. For example:
(loop for x on first-animal by 'next-animal collect x)
where (next-animal x) takes an “animal” x and returns
the next in the (assumed) sequence of animals, or nil if
x was the last animal in the sequence.
for var in-ref list by functionin clause, but var becomes
a setf-able “reference” onto the elements of the list
rather than just a temporary variable. For example,
(loop for x in-ref my-list do (incf x))
increments every element of my-list in place. This clause
is an extension to standard Common Lisp.
for var across array (loop for x across "aeiou"
do (use-vowel (char-to-string x)))
for var across-ref arraysetf-able
reference onto the elements; see in-ref above.
for var being the elements of sequencein or
across. The clause may be followed by the additional term
`using (index var2)' to cause var2 to be bound to
the successive indices (starting at 0) of the elements.
This clause type is taken from older versions of the loop macro,
and is not present in modern Common Lisp. The `using (sequence ...)'
term of the older macros is not supported.
for var being the elements of-ref sequencesetf-able
reference onto the elements; see in-ref above.
for var being the symbols [of obarray]As an example,
(loop for sym being the symbols
when (fboundp sym)
when (string-match "^map" (symbol-name sym))
collect sym)
returns a list of all the functions whose names begin with `map'.
The Common Lisp words external-symbols and present-symbols
are also recognized but are equivalent to symbols in Emacs Lisp.
Due to a minor implementation restriction, it will not work to have
more than one for clause iterating over symbols, hash tables,
keymaps, overlays, or intervals in a given loop. Fortunately,
it would rarely if ever be useful to do so. It is valid to mix
one of these types of clauses with other clauses like for ... to
or while.
for var being the hash-keys of hash-tablehash-values
is the opposite word of the word following the) to cause
var and var2 to be bound to the two parts of each
hash table entry.
for var being the key-codes of keymap