cl.texi 198 KB
Newer Older
Glenn Morris's avatar
Glenn Morris committed
1
\input texinfo    @c -*-texinfo-*-
2
@setfilename ../../info/cl
Glenn Morris's avatar
Glenn Morris committed
3
@settitle Common Lisp Extensions
4
@include emacsver.texi
Glenn Morris's avatar
Glenn Morris committed
5 6 7 8

@copying
This file documents the GNU Emacs Common Lisp emulation package.

9
Copyright @copyright{} 1993, 2001-2012 Free Software Foundation, Inc.
Glenn Morris's avatar
Glenn Morris committed
10 11 12

@quotation
Permission is granted to copy, distribute and/or modify this document
13
under the terms of the GNU Free Documentation License, Version 1.3 or
Glenn Morris's avatar
Glenn Morris committed
14
any later version published by the Free Software Foundation; with no
15 16 17
Invariant Sections, with the Front-Cover texts being ``A GNU Manual'',
and with the Back-Cover Texts as in (a) below.  A copy of the license
is included in the section entitled ``GNU Free Documentation License''.
Glenn Morris's avatar
Glenn Morris committed
18

19 20 21
(a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
modify this GNU manual.  Buying copies from the FSF supports it in
developing GNU and promoting software freedom.''
Glenn Morris's avatar
Glenn Morris committed
22 23 24
@end quotation
@end copying

25
@dircategory Emacs lisp libraries
Glenn Morris's avatar
Glenn Morris committed
26
@direntry
Glenn Morris's avatar
Glenn Morris committed
27
* CL: (cl).                     Partial Common Lisp support for Emacs Lisp.
Glenn Morris's avatar
Glenn Morris committed
28 29 30 31 32 33 34 35 36 37
@end direntry

@finalout

@titlepage
@sp 6
@center @titlefont{Common Lisp Extensions}
@sp 4
@center For GNU Emacs Lisp
@sp 1
38
@center as distributed with Emacs @value{EMACSVER}
Glenn Morris's avatar
Glenn Morris committed
39 40 41 42 43 44 45 46
@sp 5
@center Dave Gillespie
@center daveg@@synaptics.com
@page
@vskip 0pt plus 1filll
@insertcopying
@end titlepage

47 48 49
@contents

@ifnottex
50 51 52
@node Top
@top GNU Emacs Common Lisp Emulation

53 54 55
@insertcopying
@end ifnottex

Glenn Morris's avatar
Glenn Morris committed
56
@menu
57 58 59
* Overview::             Basics, usage, etc.
* Program Structure::    Arglists, @code{cl-eval-when}, @code{defalias}.
* Predicates::           @code{cl-typep} and @code{cl-equalp}.
60
* Control Structure::    @code{cl-do}, @code{cl-loop}, etc.
61 62 63 64 65 66 67
* Macros::               Destructuring, @code{cl-define-compiler-macro}.
* Declarations::         @code{cl-proclaim}, @code{cl-declare}, etc.
* Symbols::              Property lists, @code{cl-gensym}.
* Numbers::              Predicates, functions, random numbers.
* Sequences::            Mapping, functions, searching, sorting.
* Lists::                @code{cl-caddr}, @code{cl-sublis}, @code{cl-member}, @code{cl-assoc}, etc.
* Structures::           @code{cl-defstruct}.
68
* Assertions::           @code{cl-check-type}, @code{cl-assert}.
69 70 71 72

* Efficiency Concerns::         Hints and techniques.
* Common Lisp Compatibility::   All known differences with Steele.
* Porting Common Lisp::         Hints for porting Common Lisp code.
73
* Obsolete Features::           Obsolete features.
Glenn Morris's avatar
Glenn Morris committed
74 75 76 77 78 79

* GNU Free Documentation License:: The license for this documentation.
* Function Index::
* Variable Index::
@end menu

80
@node Overview
Glenn Morris's avatar
Glenn Morris committed
81 82 83
@chapter Overview

@noindent
84 85 86 87 88
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.

Glenn Morris's avatar
Glenn Morris committed
89 90 91 92 93 94 95
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.

96
The @code{CL} package adds a number of Common Lisp functions and
Glenn Morris's avatar
Glenn Morris committed
97
control structures to Emacs Lisp.  While not a 100% complete
98
implementation of Common Lisp, @code{CL} adds enough functionality
Glenn Morris's avatar
Glenn Morris committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112
to make Emacs Lisp programming significantly more convenient.

Some Common Lisp features have been omitted from this package
for various reasons:

@itemize @bullet
@item
Some features are too complex or bulky relative to their benefit
to Emacs Lisp programmers.  CLOS and Common Lisp streams are fine
examples of this group.

@item
Other features cannot be implemented without modification to the
Emacs Lisp interpreter itself, such as multiple return values,
113 114
case-insensitive symbols, and complex numbers.
The @code{CL} package generally makes no attempt to emulate these
Glenn Morris's avatar
Glenn Morris committed
115 116 117 118
features.

@end itemize

Glenn Morris's avatar
Glenn Morris committed
119 120 121 122 123 124
This package was originally written by Dave Gillespie,
@file{daveg@@synaptics.com}, as a total rewrite of an earlier 1986
@file{cl.el} package by Cesar Quiroz.  Care has been taken to ensure
that each function is defined efficiently, concisely, and with minimal
impact on the rest of the Emacs environment.  Stefan Monnier added the
file @file{cl-lib.el} and rationalized the namespace for Emacs 24.3.
Glenn Morris's avatar
Glenn Morris committed
125 126

@menu
127
* Usage::                How to use the CL package.
Glenn Morris's avatar
Glenn Morris committed
128
* Organization::         The package's component files.
129
* Naming Conventions::   Notes on CL function names.
Glenn Morris's avatar
Glenn Morris committed
130 131
@end menu

132
@node Usage
Glenn Morris's avatar
Glenn Morris committed
133 134 135
@section Usage

@noindent
136 137 138 139
The @code{CL} package is distributed with Emacs, so there is no need
to install any additional files in order to start using it.  Lisp code
that uses features from the @code{CL} package should simply include at
the beginning:
Glenn Morris's avatar
Glenn Morris committed
140 141

@example
142
(require 'cl-lib)
Glenn Morris's avatar
Glenn Morris committed
143 144 145
@end example

@noindent
146 147
You may wish to add such a statement to your init file, if you
make frequent use of CL features.
Glenn Morris's avatar
Glenn Morris committed
148

149
@node Organization
Glenn Morris's avatar
Glenn Morris committed
150 151 152
@section Organization

@noindent
153
The Common Lisp package is organized into four main files:
Glenn Morris's avatar
Glenn Morris committed
154 155

@table @file
156 157 158
@item cl-lib.el
This is the main file, which contains basic functions
and information about the package.  This file is relatively compact.
Glenn Morris's avatar
Glenn Morris committed
159 160 161 162

@item cl-extra.el
This file contains the larger, more complex or unusual functions.
It is kept separate so that packages which only want to use Common
163
Lisp fundamentals like the @code{cl-incf} function won't need to pay
Glenn Morris's avatar
Glenn Morris committed
164 165 166 167
the overhead of loading the more advanced functions.

@item cl-seq.el
This file contains most of the advanced functions for operating
168
on sequences or lists, such as @code{cl-delete-if} and @code{cl-assoc}.
Glenn Morris's avatar
Glenn Morris committed
169 170

@item cl-macs.el
171 172 173 174 175 176
This file contains the features that are macros instead of functions.
Macros expand when the caller is compiled, not when it is run, so the
macros generally only need to be present when the byte-compiler is
running (or when the macros are used in uncompiled code).  Most of the
macros of this package are isolated in @file{cl-macs.el} so that they
won't take up memory unless you are compiling.
Glenn Morris's avatar
Glenn Morris committed
177 178
@end table

179
The file @file{cl-lib.el} includes all necessary @code{autoload}
Glenn Morris's avatar
Glenn Morris committed
180
commands for the functions and macros in the other three files.
181
All you have to do is @code{(require 'cl-lib)}, and @file{cl-lib.el}
Glenn Morris's avatar
Glenn Morris committed
182 183 184
will take care of pulling in the other files when they are
needed.

185 186 187 188 189 190 191 192 193 194 195 196 197
There is another file, @file{cl.el}, which was the main entry point
to the CL package prior to Emacs 24.3.  Nowadays, it is replaced
by @file{cl-lib.el}.  The two provide the same features, but use
different function names (in fact, @file{cl.el} just defines aliases
to the @file{cl-lib.el} definitions).  In particular, the old @file{cl.el}
does not use a clean namespace.  For this reason, Emacs has a policy
that packages distributed with Emacs must not load @code{cl} at run time.
(It is ok for them to load @code{cl} at @emph{compile} time, with
@code{eval-when-compile}, and use the macros it provides.)  There is
no such restriction on the use of @code{cl-lib}.  New code should use
@code{cl-lib} rather than @code{cl}.  @xref{Naming Conventions}.

There is one more file, @file{cl-compat.el}, which defines some
198 199 200
routines from the older Quiroz CL package that are not otherwise
present in the new package.  This file is obsolete and should not be
used in new code.
Glenn Morris's avatar
Glenn Morris committed
201

202
@node Naming Conventions
Glenn Morris's avatar
Glenn Morris committed
203 204 205 206
@section Naming Conventions

@noindent
Except where noted, all functions defined by this package have the
207 208
same calling conventions as their Common Lisp counterparts, and
names that are those of Common Lisp plus a @samp{cl-} prefix.
Glenn Morris's avatar
Glenn Morris committed
209 210

Internal function and variable names in the package are prefixed
211 212
by @code{cl--}.  Here is a complete list of functions prefixed by
@code{cl-} that were not taken from Common Lisp:
Glenn Morris's avatar
Glenn Morris committed
213 214

@example
215 216
cl-callf         cl-callf2        cl-defsubst
cl-floatp-safe   cl-letf          cl-letf*
Glenn Morris's avatar
Glenn Morris committed
217 218
@end example

219
The following simple functions and macros are defined in @file{cl-lib.el};
Glenn Morris's avatar
Glenn Morris committed
220 221 222
they do not cause other components like @file{cl-extra} to be loaded.

@example
223 224 225 226 227 228 229 230
cl-floatp-safe   cl-endp
cl-evenp         cl-oddp          cl-plusp         cl-minusp
cl-caaar .. cl-cddddr
cl-list*         cl-ldiff         cl-rest          cl-first .. cl-tenth
cl-copy-list     cl-subst         cl-mapcar [2]
cl-adjoin [3]    cl-acons         cl-pairlis
cl-pushnew [3,4] cl-incf [4]      cl-decf [4]
cl-proclaim      cl-declaim
Glenn Morris's avatar
Glenn Morris committed
231 232 233 234 235 236 237 238 239 240 241 242
@end example

@noindent
[2] Only for one sequence argument or two list arguments.

@noindent
[3] Only if @code{:test} is @code{eq}, @code{equal}, or unspecified,
and @code{:key} is not used.

@noindent
[4] Only when @var{place} is a plain variable name.

243
@node Program Structure
Glenn Morris's avatar
Glenn Morris committed
244 245 246
@chapter Program Structure

@noindent
247
This section describes features of the @code{CL} package that have to
Glenn Morris's avatar
Glenn Morris committed
248
do with programs as a whole: advanced argument lists for functions,
249
and the @code{cl-eval-when} construct.
Glenn Morris's avatar
Glenn Morris committed
250 251

@menu
252 253
* Argument Lists::       @code{&key}, @code{&aux}, @code{cl-defun}, @code{cl-defmacro}.
* Time of Evaluation::   The @code{cl-eval-when} construct.
Glenn Morris's avatar
Glenn Morris committed
254 255
@end menu

256
@node Argument Lists
Glenn Morris's avatar
Glenn Morris committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270
@section Argument Lists

@noindent
Emacs Lisp's notation for argument lists of functions is a subset of
the Common Lisp notation.  As well as the familiar @code{&optional}
and @code{&rest} markers, Common Lisp allows you to specify default
values for optional arguments, and it provides the additional markers
@code{&key} and @code{&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.

271
@defmac cl-defun name arglist body...
Glenn Morris's avatar
Glenn Morris committed
272 273 274 275
This form is identical to the regular @code{defun} form, except
that @var{arglist} is allowed to be a full Common Lisp argument
list.  Also, the function body is enclosed in an implicit block
called @var{name}; @pxref{Blocks and Exits}.
276
@end defmac
Glenn Morris's avatar
Glenn Morris committed
277

278
@defmac cl-defsubst name arglist body...
279
This is just like @code{cl-defun}, except that the function that
Glenn Morris's avatar
Glenn Morris committed
280 281 282
is defined is automatically proclaimed @code{inline}, i.e.,
calls to it may be expanded into in-line code by the byte compiler.
This is analogous to the @code{defsubst} form;
283
@code{cl-defsubst} uses a different method (compiler macros) which
284
works in all versions of Emacs, and also generates somewhat more
285
efficient inline expansions.  In particular, @code{cl-defsubst}
Glenn Morris's avatar
Glenn Morris committed
286 287
arranges for the processing of keyword arguments, default values,
etc., to be done at compile-time whenever possible.
288
@end defmac
Glenn Morris's avatar
Glenn Morris committed
289

290
@defmac cl-defmacro name arglist body...
Glenn Morris's avatar
Glenn Morris committed
291 292 293 294 295 296 297 298
This is identical to the regular @code{defmacro} form,
except that @var{arglist} is allowed to be a full Common Lisp
argument list.  The @code{&environment} keyword is supported as
described in Steele.  The @code{&whole} keyword is supported only
within destructured lists (see below); top-level @code{&whole}
cannot be implemented with the current Emacs Lisp interpreter.
The macro expander body is enclosed in an implicit block called
@var{name}.
299
@end defmac
Glenn Morris's avatar
Glenn Morris committed
300

301
@defmac cl-function symbol-or-lambda
Glenn Morris's avatar
Glenn Morris committed
302 303 304
This is identical to the regular @code{function} form,
except that if the argument is a @code{lambda} form then that
form may use a full Common Lisp argument list.
305
@end defmac
Glenn Morris's avatar
Glenn Morris committed
306

307
Also, all forms (such as @code{cl-flet} and @code{cl-labels}) defined
Glenn Morris's avatar
Glenn Morris committed
308 309 310
in this package that include @var{arglist}s in their syntax allow
full Common Lisp argument lists.

311 312 313
Note that it is @emph{not} necessary to use @code{cl-defun} in
order to have access to most @code{CL} features in your function.
These features are always present; @code{cl-defun}'s only
Glenn Morris's avatar
Glenn Morris committed
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
difference from @code{defun} is its more flexible argument
lists and its implicit block.

The full form of a Common Lisp argument list is

@example
(@var{var}...
 &optional (@var{var} @var{initform} @var{svar})...
 &rest @var{var}
 &key ((@var{keyword} @var{var}) @var{initform} @var{svar})...
 &aux (@var{var} @var{initform})...)
@end example

Each of the five argument list sections is optional.  The @var{svar},
@var{initform}, and @var{keyword} parts are optional; if they are
omitted, then @samp{(@var{var})} may be written simply @samp{@var{var}}.

The first section consists of zero or more @dfn{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 @dfn{optional} arguments.  These
arguments may be specified in the function call; if they are not,
@var{initform} specifies the default value used for the argument.
(No @var{initform} means to use @code{nil} as the default.)  The
@var{initform} is evaluated with the bindings for the preceding
arguments already established; @code{(a &optional (b (1+ a)))}
matches one or two arguments, with the second argument defaulting
to one plus the first argument.  If the @var{svar} is specified,
it is an auxiliary variable which is bound to @code{t} if the optional
argument was specified, or to @code{nil} if the argument was omitted.
If you don't use an @var{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 @dfn{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 @code{&rest} is equivalent to that of Emacs Lisp.
Common Lisp accepts @code{&body} as a synonym for @code{&rest} in
macro contexts; this package accepts it all the time.

The fourth section consists of @dfn{keyword} arguments.  These
are optional arguments which are specified by name rather than
positionally in the argument list.  For example,

@example
363
(cl-defun foo (a &optional b &key c d (e 17)))
Glenn Morris's avatar
Glenn Morris committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
@end example

@noindent
defines a function which may be called with one, two, or more
arguments.  The first two arguments are bound to @code{a} and
@code{b} in the usual way.  The remaining arguments must be
pairs of the form @code{:c}, @code{:d}, or @code{:e} followed
by the value to be bound to the corresponding argument variable.
(Symbols whose names begin with a colon are called @dfn{keywords},
and they are self-quoting in the same way as @code{nil} and
@code{t}.)

For example, the call @code{(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 @code{b} as well, since @code{(foo 1 :c 2)} would bind
@code{b} to the keyword @code{:c}, then signal an error because
@code{2} is not a valid keyword.

385 386 387 388
You can also explicitly specify the keyword argument; it need not be
simply the variable name prefixed with a colon.  For example,

@example
389
(cl-defun bar (&key (a 1) ((baz b) 4)))
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
@end example

@noindent

specifies a keyword @code{:a} that sets the variable @code{a} with
default value 1, as well as a keyword @code{baz} that sets the
variable @code{b} with default value 4.  In this case, because
@code{baz} is not self-quoting, you must quote it explicitly in the
function call, like this:

@example
(bar :a 10 'baz 42)
@end example

Ordinarily, it is an error to pass an unrecognized keyword to
Glenn Morris's avatar
Glenn Morris committed
405 406 407 408 409 410 411 412 413 414
a function, e.g., @code{(foo 1 2 :c 3 :goober 4)}.  You can ask
Lisp to ignore unrecognized keywords, either by adding the
marker @code{&allow-other-keys} after the keyword section
of the argument list, or by specifying an @code{:allow-other-keys}
argument in the call whose value is non-@code{nil}.  If the
function uses both @code{&rest} and @code{&key} at the same time,
the ``rest'' argument is bound to the keyword list as it appears
in the call.  For example:

@smallexample
415 416
(cl-defun find-thing (thing &rest rest &key need &allow-other-keys)
  (or (apply 'cl-member thing thing-list :allow-other-keys t rest)
Glenn Morris's avatar
Glenn Morris committed
417 418 419 420 421 422
      (if need (error "Thing not found"))))
@end smallexample

@noindent
This function takes a @code{:need} keyword argument, but also
accepts other keyword arguments which are passed on to the
423 424
@code{cl-member} function.  @code{allow-other-keys} is used to
keep both @code{find-thing} and @code{cl-member} from complaining
Glenn Morris's avatar
Glenn Morris committed
425 426 427 428 429 430 431 432 433 434
about each others' keywords in the arguments.

The fifth section of the argument list consists of @dfn{auxiliary
variables}.  These are not really arguments at all, but simply
variables which are bound to @code{nil} or to the specified
@var{initforms} during execution of the function.  There is no
difference between the following two functions, except for a
matter of stylistic taste:

@example
435
(cl-defun foo (a b &aux (c (+ a b)) d)
Glenn Morris's avatar
Glenn Morris committed
436 437
  @var{body})

438
(cl-defun foo (a b)
Glenn Morris's avatar
Glenn Morris committed
439 440 441 442 443 444
  (let ((c (+ a b)) d)
    @var{body}))
@end example

Argument lists support @dfn{destructuring}.  In Common Lisp,
destructuring is only allowed with @code{defmacro}; this package
445
allows it with @code{cl-defun} and other argument lists as well.
Glenn Morris's avatar
Glenn Morris committed
446 447 448 449 450 451 452
In destructuring, any argument variable (@var{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:

@example
453
(cl-defmacro dolist ((var listform &optional resultform)
Glenn Morris's avatar
Glenn Morris committed
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
                   &rest body)
  ...)
@end example

This says that the first argument of @code{dolist} must be a list
of two or three items; if there are other arguments as well as this
list, they are stored in @code{body}.  All features allowed in
regular argument lists are allowed in these recursive argument lists.
In addition, the clause @samp{&whole @var{var}} is allowed at the
front of a recursive argument list.  It binds @var{var} to the
whole list being matched; thus @code{(&whole all a b)} matches
a list of two things, with @code{a} bound to the first thing,
@code{b} bound to the second thing, and @code{all} bound to the
list itself.  (Common Lisp allows @code{&whole} in top-level
@code{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 @code{(a b . c)} is functionally
equivalent to @code{(a b &rest c)}.

If the optimization quality @code{safety} is set to 0
(@pxref{Declarations}), error checking for wrong number of
arguments and invalid keyword arguments is disabled.  By default,
argument lists are rigorously checked.

480
@node Time of Evaluation
Glenn Morris's avatar
Glenn Morris committed
481 482 483 484 485 486 487 488 489 490 491 492 493
@section Time of Evaluation

@noindent
Normally, the byte-compiler does not actually execute the forms in
a file it compiles.  For example, if a file contains @code{(setq foo t)},
the act of compiling it will not actually set @code{foo} to @code{t}.
This is true even if the @code{setq} was a top-level form (i.e., not
enclosed in a @code{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 @code{defmacro} forms
at compile-time so that later parts of the file can refer to the
macros that are defined.

494
@defmac cl-eval-when (situations...) forms...
Glenn Morris's avatar
Glenn Morris committed
495 496 497 498 499 500
This form controls when the body @var{forms} are evaluated.
The @var{situations} list may contain any set of the symbols
@code{compile}, @code{load}, and @code{eval} (or their long-winded
ANSI equivalents, @code{:compile-toplevel}, @code{:load-toplevel},
and @code{:execute}).

501
The @code{cl-eval-when} form is handled differently depending on
Glenn Morris's avatar
Glenn Morris committed
502 503 504 505 506 507
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 as @code{byte-compile-file} which compiles files
or buffers of code, and it appears either literally at the
top level of the file or inside a top-level @code{progn}.

508
For compiled top-level @code{cl-eval-when}s, the body @var{forms} are
Glenn Morris's avatar
Glenn Morris committed
509 510 511 512 513 514 515
executed at compile-time if @code{compile} is in the @var{situations}
list, and the @var{forms} are written out to the file (to be executed
at load-time) if @code{load} is in the @var{situations} list.

For non-compiled-top-level forms, only the @code{eval} situation is
relevant.  (This includes forms executed by the interpreter, forms
compiled with @code{byte-compile} rather than @code{byte-compile-file},
516
and non-top-level forms.)  The @code{cl-eval-when} acts like a
Glenn Morris's avatar
Glenn Morris committed
517 518 519
@code{progn} if @code{eval} is specified, and like @code{nil}
(ignoring the body @var{forms}) if not.

520
The rules become more subtle when @code{cl-eval-when}s are nested;
Glenn Morris's avatar
Glenn Morris committed
521 522 523 524 525 526 527
consult Steele (second edition) for the gruesome details (and
some gruesome examples).

Some simple examples:

@example
;; Top-level forms in foo.el:
528 529 530 531 532 533 534
(cl-eval-when (compile)           (setq foo1 'bar))
(cl-eval-when (load)              (setq foo2 'bar))
(cl-eval-when (compile load)      (setq foo3 'bar))
(cl-eval-when (eval)              (setq foo4 'bar))
(cl-eval-when (eval compile)      (setq foo5 'bar))
(cl-eval-when (eval load)         (setq foo6 'bar))
(cl-eval-when (eval compile load) (setq foo7 'bar))
Glenn Morris's avatar
Glenn Morris committed
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
@end example

When @file{foo.el} is compiled, these variables will be set during
the compilation itself:

@example
foo1  foo3  foo5  foo7      ; `compile'
@end example

When @file{foo.elc} is loaded, these variables will be set:

@example
foo2  foo3  foo6  foo7      ; `load'
@end example

And if @file{foo.el} is loaded uncompiled, these variables will
be set:

@example
foo4  foo5  foo6  foo7      ; `eval'
@end example

557
If these seven @code{cl-eval-when}s had been, say, inside a @code{defun},
Glenn Morris's avatar
Glenn Morris committed
558 559 560
then the first three would have been equivalent to @code{nil} and the
last four would have been equivalent to the corresponding @code{setq}s.

561
Note that @code{(cl-eval-when (load eval) @dots{})} is equivalent
Glenn Morris's avatar
Glenn Morris committed
562 563
to @code{(progn @dots{})} in all contexts.  The compiler treats
certain top-level forms, like @code{defmacro} (sort-of) and
Glenn Morris's avatar
Glenn Morris committed
564
@code{require}, as if they were wrapped in @code{(cl-eval-when
Glenn Morris's avatar
Glenn Morris committed
565
(compile load eval) @dots{})}.
566
@end defmac
Glenn Morris's avatar
Glenn Morris committed
567

568
Emacs includes two special forms related to @code{cl-eval-when}.
Glenn Morris's avatar
Glenn Morris committed
569
One of these, @code{eval-when-compile}, is not quite equivalent to
Glenn Morris's avatar
Glenn Morris committed
570
any @code{cl-eval-when} construct and is described below.
Glenn Morris's avatar
Glenn Morris committed
571 572

The other form, @code{(eval-and-compile @dots{})}, is exactly
Glenn Morris's avatar
Glenn Morris committed
573
equivalent to @samp{(cl-eval-when (compile load eval) @dots{})} and
Glenn Morris's avatar
Glenn Morris committed
574 575
so is not itself defined by this package.

576
@defmac eval-when-compile forms...
Glenn Morris's avatar
Glenn Morris committed
577 578 579 580 581 582 583 584
The @var{forms} are evaluated at compile-time; at execution time,
this form acts like a quoted constant of the resulting value.  Used
at top-level, @code{eval-when-compile} is just like @samp{eval-when
(compile eval)}.  In other contexts, @code{eval-when-compile}
allows code to be evaluated once at compile-time for efficiency
or other reasons.

This form is similar to the @samp{#.} syntax of true Common Lisp.
585
@end defmac
Glenn Morris's avatar
Glenn Morris committed
586

587
@defmac cl-load-time-value form
Glenn Morris's avatar
Glenn Morris committed
588 589 590 591 592 593 594
The @var{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 @samp{#,} syntax that was similar to
this, but ANSI Common Lisp replaced it with @code{load-time-value}
and gave it more well-defined semantics.

595
In a compiled file, @code{cl-load-time-value} arranges for @var{form}
Glenn Morris's avatar
Glenn Morris committed
596 597 598 599
to be evaluated when the @file{.elc} file is loaded and then used
as if it were a quoted constant.  In code compiled by
@code{byte-compile} rather than @code{byte-compile-file}, the
effect is identical to @code{eval-when-compile}.  In uncompiled
600
code, both @code{eval-when-compile} and @code{cl-load-time-value}
Glenn Morris's avatar
Glenn Morris committed
601 602 603 604 605 606 607 608 609 610
act exactly like @code{progn}.

@example
(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: "
611
          (cl-load-time-value (current-time-string))))
Glenn Morris's avatar
Glenn Morris committed
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
@end example

@noindent
Byte-compiled, the above defun will result in the following code
(or its compiled equivalent, of course) in the @file{.elc} file:

@example
(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--))
@end example
628
@end defmac
Glenn Morris's avatar
Glenn Morris committed
629

630
@node Predicates
Glenn Morris's avatar
Glenn Morris committed
631 632 633 634 635 636 637
@chapter Predicates

@noindent
This section describes functions for testing whether various
facts are true or false.

@menu
638 639
* Type Predicates::      @code{cl-typep}, @code{cl-deftype}, and @code{cl-coerce}.
* Equality Predicates::  @code{cl-equalp}.
Glenn Morris's avatar
Glenn Morris committed
640 641
@end menu

642
@node Type Predicates
Glenn Morris's avatar
Glenn Morris committed
643 644
@section Type Predicates

645
@defun cl-typep object type
Glenn Morris's avatar
Glenn Morris committed
646 647
Check if @var{object} is of type @var{type}, where @var{type} is a
(quoted) type name of the sort used by Common Lisp.  For example,
648
@code{(cl-typep foo 'integer)} is equivalent to @code{(integerp foo)}.
Glenn Morris's avatar
Glenn Morris committed
649 650 651 652 653 654 655 656 657 658 659 660 661 662
@end defun

The @var{type} argument to the above function is either a symbol
or a list beginning with a symbol.

@itemize @bullet
@item
If the type name is a symbol, Emacs appends @samp{-p} to the
symbol name to form the name of a predicate function for testing
the type.  (Built-in predicates whose names end in @samp{p} rather
than @samp{-p} are used when appropriate.)

@item
The type symbol @code{t} stands for the union of all types.
663
@code{(cl-typep @var{object} t)} is always true.  Likewise, the
Glenn Morris's avatar
Glenn Morris committed
664
type symbol @code{nil} stands for nothing at all, and
665
@code{(cl-typep @var{object} nil)} is always false.
Glenn Morris's avatar
Glenn Morris committed
666 667 668

@item
The type symbol @code{null} represents the symbol @code{nil}.
669
Thus @code{(cl-typep @var{object} 'null)} is equivalent to
Glenn Morris's avatar
Glenn Morris committed
670 671 672 673
@code{(null @var{object})}.

@item
The type symbol @code{atom} represents all objects that are not cons
674
cells. Thus @code{(cl-typep @var{object} 'atom)} is equivalent to
Glenn Morris's avatar
Glenn Morris committed
675 676 677 678 679 680 681 682 683 684 685
@code{(atom @var{object})}.

@item
The type symbol @code{real} is a synonym for @code{number}, and
@code{fixnum} is a synonym for @code{integer}.

@item
The type symbols @code{character} and @code{string-char} match
integers in the range from 0 to 255.

@item
686
The type symbol @code{float} uses the @code{cl-floatp-safe} predicate
Glenn Morris's avatar
Glenn Morris committed
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
defined by this package rather than @code{floatp}, so it will work
correctly even in Emacs versions without floating-point support.

@item
The type list @code{(integer @var{low} @var{high})} represents all
integers between @var{low} and @var{high}, inclusive.  Either bound
may be a list of a single integer to specify an exclusive limit,
or a @code{*} to specify no limit.  The type @code{(integer * *)}
is thus equivalent to @code{integer}.

@item
Likewise, lists beginning with @code{float}, @code{real}, or
@code{number} represent numbers of that type falling in a particular
range.

@item
Lists beginning with @code{and}, @code{or}, and @code{not} form
combinations of types.  For example, @code{(or integer (float 0 *))}
represents all objects that are integers or non-negative floats.

@item
708
Lists beginning with @code{member} or @code{cl-member} represent
Glenn Morris's avatar
Glenn Morris committed
709 710 711 712 713 714 715 716 717 718 719
objects @code{eql} to any of the following values.  For example,
@code{(member 1 2 3 4)} is equivalent to @code{(integer 1 4)},
and @code{(member nil)} is equivalent to @code{null}.

@item
Lists of the form @code{(satisfies @var{predicate})} represent
all objects for which @var{predicate} returns true when called
with that object as an argument.
@end itemize

The following function and macro (not technically predicates) are
720
related to @code{cl-typep}.
Glenn Morris's avatar
Glenn Morris committed
721

722
@defun cl-coerce object type
Glenn Morris's avatar
Glenn Morris committed
723 724
This function attempts to convert @var{object} to the specified
@var{type}.  If @var{object} is already of that type as determined by
Glenn Morris's avatar
Glenn Morris committed
725
@code{cl-typep}, it is simply returned.  Otherwise, certain types of
Glenn Morris's avatar
Glenn Morris committed
726 727 728 729 730 731
conversions will be made:  If @var{type} is any sequence type
(@code{string}, @code{list}, etc.) then @var{object} will be
converted to that type if possible.  If @var{type} is
@code{character}, then strings of length one and symbols with
one-character names can be coerced.  If @var{type} is @code{float},
then integers can be coerced in versions of Emacs that support
732
floats.  In all other circumstances, @code{cl-coerce} signals an
Glenn Morris's avatar
Glenn Morris committed
733 734 735
error.
@end defun

736
@defmac cl-deftype name arglist forms...
Glenn Morris's avatar
Glenn Morris committed
737 738 739 740 741
This macro defines a new type called @var{name}.  It is similar
to @code{defmacro} in many ways; when @var{name} is encountered
as a type name, the body @var{forms} are evaluated and should
return a type specifier that is equivalent to the type.  The
@var{arglist} is a Common Lisp argument list of the sort accepted
742
by @code{cl-defmacro}.  The type specifier @samp{(@var{name} @var{args}...)}
Glenn Morris's avatar
Glenn Morris committed
743 744 745
is expanded by calling the expander with those arguments; the type
symbol @samp{@var{name}} is expanded by calling the expander with
no arguments.  The @var{arglist} is processed the same as for
746
@code{cl-defmacro} except that optional arguments without explicit
Glenn Morris's avatar
Glenn Morris committed
747 748 749 750
defaults use @code{*} instead of @code{nil} as the ``default''
default.  Some examples:

@example
751 752 753
(cl-deftype null () '(satisfies null))    ; predefined
(cl-deftype list () '(or null cons))      ; predefined
(cl-deftype unsigned-byte (&optional bits)
Glenn Morris's avatar
Glenn Morris committed
754 755 756 757 758 759 760 761 762 763
  (list 'integer 0 (if (eq bits '*) bits (1- (lsh 1 bits)))))
(unsigned-byte 8)  @equiv{}  (integer 0 255)
(unsigned-byte)  @equiv{}  (integer 0 *)
unsigned-byte  @equiv{}  (integer 0 *)
@end example

@noindent
The last example shows how the Common Lisp @code{unsigned-byte}
type specifier could be implemented if desired; this package does
not implement @code{unsigned-byte} by default.
764
@end defmac
Glenn Morris's avatar
Glenn Morris committed
765

766 767 768
The @code{cl-typecase} and @code{cl-check-type} macros also use type
names.  @xref{Conditionals}.  @xref{Assertions}.  The @code{cl-map},
@code{cl-concatenate}, and @code{cl-merge} functions take type-name
Glenn Morris's avatar
Glenn Morris committed
769 770
arguments to specify the type of sequence to return.  @xref{Sequences}.

771
@node Equality Predicates
Glenn Morris's avatar
Glenn Morris committed
772 773 774
@section Equality Predicates

@noindent
775
This package defines the Common Lisp predicate @code{cl-equalp}.
Glenn Morris's avatar
Glenn Morris committed
776

777
@defun cl-equalp a b
Glenn Morris's avatar
Glenn Morris committed
778 779
This function is a more flexible version of @code{equal}.  In
particular, it compares strings case-insensitively, and it compares
780
numbers without regard to type (so that @code{(cl-equalp 3 3.0)} is
Glenn Morris's avatar
Glenn Morris committed
781 782 783 784 785 786 787 788
true).  Vectors and conses are compared recursively.  All other
objects are compared as if by @code{equal}.

This function differs from Common Lisp @code{equalp} in several
respects.  First, Common Lisp's @code{equalp} also compares
@emph{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
789
vector-like in Emacs Lisp, this package's @code{cl-equalp} also will
Glenn Morris's avatar
Glenn Morris committed
790 791 792 793 794 795
not compare strings against vectors of integers.
@end defun

Also note that the Common Lisp functions @code{member} and @code{assoc}
use @code{eql} to compare elements, whereas Emacs Lisp follows the
MacLisp tradition and uses @code{equal} for these two functions.
796 797
In Emacs, use @code{memq} (or @code{cl-member}) and @code{assq} (or
@code{cl-assoc}) to get functions which use @code{eql} for comparisons.
Glenn Morris's avatar
Glenn Morris committed
798

799
@node Control Structure
Glenn Morris's avatar
Glenn Morris committed
800 801 802 803
@chapter Control Structure

@noindent
The features described in the following sections implement
804 805
various advanced control structures, including extensions to the
standard @code{setf} facility, and a number of looping and conditional
Glenn Morris's avatar
Glenn Morris committed
806 807
constructs.

808
@c FIXME
809
@c flet is not cl-flet, values is not cl-values.
Glenn Morris's avatar
Glenn Morris committed
810
@menu
811
* Assignment::             The @code{cl-psetq} form.
812
* Generalized Variables::  Extensions to generalized variables.
813
* Variable Bindings::      @code{cl-progv}, @code{flet}, @code{cl-macrolet}.
814 815 816 817 818
* Conditionals::           @code{cl-case}, @code{cl-typecase}.
* Blocks and Exits::       @code{cl-block}, @code{cl-return}, @code{cl-return-from}.
* Iteration::              @code{cl-do}, @code{cl-dotimes}, @code{cl-dolist}, @code{cl-do-symbols}.
* Loop Facility::          The Common Lisp @code{cl-loop} macro.
* Multiple Values::        @code{values}, @code{cl-multiple-value-bind}, etc.
Glenn Morris's avatar
Glenn Morris committed
819 820
@end menu

821
@node Assignment
Glenn Morris's avatar
Glenn Morris committed
822 823 824
@section Assignment

@noindent
825
The @code{cl-psetq} form is just like @code{setq}, except that multiple
Glenn Morris's avatar
Glenn Morris committed
826 827
assignments are done in parallel rather than sequentially.

828
@defmac cl-psetq [symbol form]@dots{}
Glenn Morris's avatar
Glenn Morris committed
829 830 831 832 833 834 835 836 837 838 839 840 841 842
This special form (actually a macro) is used to assign to several
variables simultaneously.  Given only one @var{symbol} and @var{form},
it has the same effect as @code{setq}.  Given several @var{symbol}
and @var{form} pairs, it evaluates all the @var{form}s in advance
and then stores the corresponding variables afterwards.

@example
(setq x 2 y 3)
(setq x (+ x y)  y (* x y))
x
     @result{} 5
y                     ; @r{@code{y} was computed after @code{x} was set.}
     @result{} 15
(setq x 2 y 3)
843
(cl-psetq x (+ x y)  y (* x y))
Glenn Morris's avatar
Glenn Morris committed
844 845 846 847 848 849
x
     @result{} 5
y                     ; @r{@code{y} was computed before @code{x} was set.}
     @result{} 6
@end example

850 851
The simplest use of @code{cl-psetq} is @code{(cl-psetq x y y x)}, which
exchanges the values of two variables.  (The @code{cl-rotatef} form
Glenn Morris's avatar
Glenn Morris committed
852 853 854
provides an even more convenient way to swap two variables;
@pxref{Modify Macros}.)

855
@code{cl-psetq} always returns @code{nil}.
856
@end defmac
Glenn Morris's avatar
Glenn Morris committed
857

858
@node Generalized Variables
Glenn Morris's avatar
Glenn Morris committed
859 860
@section Generalized Variables

861 862 863 864 865 866 867 868
A @dfn{generalized variable} or @dfn{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.  For basic information,
@pxref{Generalized Variables,,,elisp,GNU Emacs Lisp Reference Manual}.
This package provides several additional features related to
generalized variables.
Glenn Morris's avatar
Glenn Morris committed
869 870

@menu
871 872
* Setf Extensions::    Additional @code{setf} places.
* Modify Macros::      @code{cl-incf}, @code{cl-rotatef}, @code{letf}, @code{cl-callf}, etc.
873
* Customizing Setf::   @code{define-modify-macro}, @code{defsetf}, @code{define-setf-method}.
Glenn Morris's avatar
Glenn Morris committed
874 875
@end menu

876 877
@node Setf Extensions
@subsection Setf Extensions
Glenn Morris's avatar
Glenn Morris committed
878

879 880 881
Several standard (e.g. @code{car}) and Emacs-specific
(e.g. @code{window-point}) Lisp functions are @code{setf}-able by default.
This package defines @code{setf} handlers for several additional functions:
Glenn Morris's avatar
Glenn Morris committed
882

883
@itemize
Glenn Morris's avatar
Glenn Morris committed
884
@item
885
Functions from @code{CL} itself:
Glenn Morris's avatar
Glenn Morris committed
886
@smallexample
887 888
cl-caaar .. cl-cddddr         cl-first .. cl-tenth
cl-rest     cl-get            cl-getf     cl-subseq
Glenn Morris's avatar
Glenn Morris committed
889 890 891
@end smallexample

@item
892
General Emacs Lisp functions:
Glenn Morris's avatar
Glenn Morris committed
893
@smallexample
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
buffer-file-name                   getenv
buffer-modified-p                  global-key-binding
buffer-name                        local-key-binding
buffer-string                      mark
buffer-substring                   mark-marker
current-buffer                     marker-position
current-case-table                 mouse-position
current-column                     point
current-global-map                 point-marker
current-input-mode                 point-max
current-local-map                  point-min
current-window-configuration       read-mouse-position
default-file-modes                 screen-height
documentation-property             screen-width
face-background                    selected-window
face-background-pixmap             selected-screen
face-font                          selected-frame
face-foreground                    standard-case-table
face-underline-p                   syntax-table
file-modes                         visited-file-modtime
frame-height                       window-height
frame-parameters                   window-width
frame-visible-p                    x-get-secondary-selection
frame-width                        x-get-selection
get-register
Glenn Morris's avatar
Glenn Morris committed
919 920 921 922 923
@end smallexample

Most of these have directly corresponding ``set'' functions, like
@code{use-local-map} for @code{current-local-map}, or @code{goto-char}
for @code{point}.  A few, like @code{point-min}, expand to longer
924 925
sequences of code when they are used with @code{setf}
(@code{(narrow-to-region x (point-max))} in this case).
Glenn Morris's avatar
Glenn Morris committed
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951

@item
A call of the form @code{(substring @var{subplace} @var{n} [@var{m}])},
where @var{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:

@example
(setq a (list "hello" "world"))
     @result{} ("hello" "world")
(cadr a)
     @result{} "world"
(substring (cadr a) 2 4)
     @result{} "rl"
(setf (substring (cadr a) 2 4) "o")
     @result{} "o"
(cadr a)
     @result{} "wood"
a
     @result{} ("hello" "wood")
@end example

The generalized variable @code{buffer-substring}, listed above,
also works in this way by replacing a portion of the current buffer.

952 953
@c FIXME? Also `eq'? (see cl-lib.el)

Glenn Morris's avatar
Glenn Morris committed
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
@item
A call of the form @code{(apply '@var{func} @dots{})} or
@code{(apply (function @var{func}) @dots{})}, where @var{func}
is a @code{setf}-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 with
@code{define-setf-method} or the long form of @code{defsetf}.

@item
A macro call, in which case the macro is expanded and @code{setf}
is applied to the resulting form.

@item
Any form for which a @code{defsetf} or @code{define-setf-method}
has been made.
@end itemize

972 973 974
@c FIXME should this be in lispref?  It seems self-evident.
@c Contrast with the cl-incf example later on.
@c Here it really only serves as a constrast to wrong-order.
Glenn Morris's avatar
Glenn Morris committed
975 976 977 978
The @code{setf} macro takes care to evaluate all subforms in
the proper left-to-right order; for example,

@example
979
(setf (aref vec (cl-incf i)) i)
Glenn Morris's avatar
Glenn Morris committed
980 981 982
@end example

@noindent
983
looks like it will evaluate @code{(cl-incf i)} exactly once, before the
Glenn Morris's avatar
Glenn Morris committed
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
following access to @code{i}; the @code{setf} expander will insert
temporary variables as necessary to ensure that it does in fact work
this way no matter what setf-method is defined for @code{aref}.
(In this case, @code{aset} would be used and no such steps would
be necessary since @code{aset} takes its arguments in a convenient
order.)

However, if the @var{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

@example
(defmacro wrong-order (x y) (list 'aref y x))
@end example

@noindent
the form @code{(setf (wrong-order @var{a} @var{b}) 17)} will
evaluate @var{b} first, then @var{a}, just as in an actual call
to @code{wrong-order}.

1004
@node Modify Macros
Glenn Morris's avatar
Glenn Morris committed
1005 1006 1007
@subsection Modify Macros

@noindent
1008 1009 1010
This package defines a number of macros that operate on generalized
variables.  Many are interesting and useful even when the @var{place}
is just a variable name.
Glenn Morris's avatar
Glenn Morris committed
1011

1012
@defmac cl-psetf [place form]@dots{}
1013
This macro is to @code{setf} what @code{cl-psetq} is to @code{setq}:
Glenn Morris's avatar
Glenn Morris committed
1014 1015 1016 1017
When several @var{place}s and @var{form}s 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).
1018
@end defmac
Glenn Morris's avatar
Glenn Morris committed
1019

1020
@defmac cl-incf place &optional x
Glenn Morris's avatar
Glenn Morris committed
1021 1022
This macro increments the number stored in @var{place} by one, or
by @var{x} if specified.  The incremented value is returned.  For
1023 1024
example, @code{(cl-incf i)} is equivalent to @code{(setq i (1+ i))}, and
@code{(cl-incf (car x) 2)} is equivalent to @code{(setcar x (+ (car x) 2))}.
Glenn Morris's avatar
Glenn Morris committed
1025

1026 1027
As with @code{setf}, care is taken to preserve the ``apparent'' order
of evaluation.  For example,
Glenn Morris's avatar
Glenn Morris committed
1028 1029

@example
1030
(cl-incf (aref vec (cl-incf i)))
Glenn Morris's avatar
Glenn Morris committed
1031 1032 1033 1034 1035 1036 1037 1038 1039
@end example

@noindent
appears to increment @code{i} once, then increment the element of
@code{vec} addressed by @code{i}; this is indeed exactly what it
does, which means the above form is @emph{not} equivalent to the
``obvious'' expansion,

@example
Glenn Morris's avatar
Glenn Morris committed
1040 1041
(setf (aref vec (cl-incf i))
      (1+ (aref vec (cl-incf i))))   ; wrong!
Glenn Morris's avatar
Glenn Morris committed
1042 1043 1044 1045 1046 1047
@end example

@noindent
but rather to something more like

@example
1048
(let ((temp (cl-incf i)))
Glenn Morris's avatar
Glenn Morris committed
1049 1050 1051 1052
  (setf (aref vec temp) (1+ (aref vec temp))))
@end example

@noindent
1053
Again, all of this is taken care of automatically by @code{cl-incf} and
Glenn Morris's avatar
Glenn Morris committed
1054 1055
the other generalized-variable macros.

1056 1057
As a more Emacs-specific example of @code{cl-incf}, the expression
@code{(cl-incf (point) @var{n})} is essentially equivalent to
Glenn Morris's avatar
Glenn Morris committed
1058
@code{(forward-char @var{n})}.
1059
@end defmac
Glenn Morris's avatar
Glenn Morris committed
1060

1061
@defmac cl-decf place &optional x
Glenn Morris's avatar
Glenn Morris committed
1062 1063
This macro decrements the number stored in @var{place} by one, or
by @var{x} if specified.
1064
@end defmac
Glenn Morris's avatar
Glenn Morris committed
1065

1066
@defmac cl-pushnew x place @t{&key :test :test-not :key}
Glenn Morris's avatar
Glenn Morris committed
1067 1068 1069
This macro inserts @var{x} at the front of the list stored in
@var{place}, but only if @var{x} was not @code{eql} to any
existing element of the list.  The optional keyword arguments
Glenn Morris's avatar
Glenn Morris committed
1070
are interpreted in the same way as for @code{cl-adjoin}.
Glenn Morris's avatar
Glenn Morris committed
1071
@xref{Lists as Sets}.
1072
@end defmac
Glenn Morris's avatar
Glenn Morris committed
1073

1074
@defmac cl-shiftf place@dots{} newvalue
Glenn Morris's avatar
Glenn Morris committed
1075 1076 1077
This macro shifts the @var{place}s left by one, shifting in the
value of @var{newvalue} (which may be any Lisp expression, not just
a generalized variable), and returning the value shifted out of
1078
the first @var{place}.  Thus, @code{(cl-shiftf @var{a} @var{b} @var{c}
Glenn Morris's avatar
Glenn Morris committed
1079 1080 1081 1082 1083
@var{d})} is equivalent to

@example
(prog1
    @var{a}
1084 1085 1086
  (cl-psetf @var{a} @var{b}
            @var{b} @var{c}
            @var{c} @var{d}))
Glenn Morris's avatar
Glenn Morris committed
1087 1088 1089 1090 1091
@end example

@noindent
except that the subforms of @var{a}, @var{b}, and @var{c} are actually
evaluated only once each and in the apparent order.
1092
@end defmac
Glenn Morris's avatar
Glenn Morris committed
1093

1094
@defmac cl-rotatef place@dots{}
Glenn Morris's avatar
Glenn Morris committed
1095
This macro rotates the @var{place}s left by one in circular fashion.
Glenn Morris's avatar
Glenn Morris committed
1096
Thus, @code{(cl-rotatef @var{a} @var{b} @var{c} @var{d})} is equivalent to
Glenn Morris's avatar
Glenn Morris committed
1097 1098

@example
1099 1100 1101 1102
(cl-psetf @var{a} @var{b}
          @var{b} @var{c}
          @var{c} @var{d}
          @var{d} @var{a})
Glenn Morris's avatar
Glenn Morris committed
1103 1104 1105
@end example

@noindent
Glenn Morris's avatar
Glenn Morris committed
1106 1107
except for the evaluation of subforms.  @code{cl-rotatef} always
returns @code{nil}.  Note that @code{(cl-rotatef @var{a} @var{b})}
Glenn Morris's avatar
Glenn Morris committed
1108
conveniently exchanges @var{a} and @var{b}.
1109
@end defmac
Glenn Morris's avatar
Glenn Morris committed
1110 1111 1112 1113

The following macros were invented for this package; they have no
analogues in Common Lisp.

1114
@defmac letf (bindings@dots{}) forms@dots{}
Glenn Morris's avatar
Glenn Morris committed
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
This macro is analogous to @code{let}, but for generalized variables
rather than just symbols.  Each @var{binding} should be of the form
@code{(@var{place} @var{value})}; the original contents of the
@var{place}s are saved, the @var{value}s are stored in them, and
then the body @var{form}s are executed.  Afterwards, the @var{places}
are set back to their original saved contents.  This cleanup happens
even if the @var{form}s exit irregularly due to a @code{throw} or an
error.

For example,

@example
(letf (((point) (point-min))
       (a 17))
  ...)
@end example

@noindent
moves ``point'' in the current buffer to the beginning of the buffer,
and also binds @code{a} to 17 (as if by a normal @code{let}, since
@code{a} is just a regular variable).  After the body exits, @code{a}
is set back to its original value and point is moved back to its
original position.

Note that @code{letf} on @code{(point)} is not quite like a
@code{save-excursion}, as the latter effectively saves a marker
which tracks insertions and deletions in the buffer.  Actually,
a @code{letf} of @code{(point-marker)} is much closer to this
behavior.  (@code{point} and @code{point-marker} are equivalent
as @code{setf} places; each will accept either an integer or a
marker as the stored value.)

Since generalized variables look like lists, @code{let}'s shorthand
of using @samp{foo} for @samp{(foo nil)} as a @var{binding} would
be ambiguous in @code{letf} and is not allowed.

However, a @var{binding} specifier may be a one-element list
@samp{(@var{place})}, which is similar to @samp{(@var{place}
@var{place})}.  In other words, the @var{place} is not disturbed
on entry to the body, and the only effect of the @code{letf} is
to restore the original value of @var{place} afterwards.  (The
redundant access-and-store suggested by the @code{(@var{place}
@var{place})} example does not actually occur.)

In most cases, the @var{place} must have a well-defined value on
entry to the @code{letf} form.  The only exceptions are plain
variables and calls to @code{symbol-value} and @code{symbol-function}.
If the symbol is not bound on entry, it is simply made unbound by
@code{makunbound} or @code{fmakunbound} on exit.
1164
@end defmac
Glenn Morris's avatar
Glenn Morris committed
1165

1166
@defmac cl-letf* (bindings@dots{}) forms@dots{}
Glenn Morris's avatar
Glenn Morris committed
1167 1168
This macro is to @code{letf} what @code{let*} is to @code{let}:
It does the bindings in sequential rather than parallel order.
1169
@end defmac
Glenn Morris's avatar
Glenn Morris committed
1170

1171
@defmac cl-callf @var{function} @var{place} @var{args}@dots{}
Glenn Morris's avatar
Glenn Morris committed
1172 1173 1174
This is the ``generic'' modify macro.  It calls @var{function},
which should be an unquoted function name, macro name, or lambda.
It passes @var{place} and @var{args} as arguments, and assigns the
1175
result back to @var{place}.  For example, @code{(cl-incf @var{place}
Glenn Morris's avatar
Glenn Morris committed
1176
@var{n})} is the same as @code{(cl-callf + @var{place} @var{n})}.
Glenn Morris's avatar
Glenn Morris committed
1177 1178 1179
Some more examples:

@example
Glenn Morris's avatar
Glenn Morris committed
1180 1181 1182
(cl-callf abs my-number)
(cl-callf concat (buffer-name) "<" (number-to-string n) ">")
(cl-callf cl-union happy-people (list joe bob) :test 'same-person)
Glenn Morris's avatar
Glenn Morris committed
1183 1184 1185 1186
@end example

@xref{Customizing Setf}, for @code{define-modify-macro}, a way
to create even more concise notations for modify macros.  Note
Glenn Morris's avatar
Glenn Morris committed
1187
again that @code{cl-callf} is an extension to standard Common Lisp.
1188
@end defmac
Glenn Morris's avatar
Glenn Morris committed
1189

1190
@defmac cl-callf2 @var{function} @var{arg1} @var{place} @var{args}@dots{}
Glenn Morris's avatar
Glenn Morris committed
1191
This macro is like @code{cl-callf}, except that @var{place} is
Glenn Morris's avatar
Glenn Morris committed
1192 1193
the @emph{second} argument of @var{function} rather than the
first.  For example, @code{(push @var{x} @var{place})} is
Glenn Morris's avatar
Glenn Morris committed
1194
equivalent to @code{(cl-callf2 cons @var{x} @var{place})}.
1195
@end defmac
Glenn Morris's avatar
Glenn Morris committed
1196

Glenn Morris's avatar
Glenn Morris committed
1197 1198 1199
The @code{cl-callf} and @code{cl-callf2} macros serve as building
blocks for other macros like @code{cl-incf}, @code{cl-pushnew}, and
@code{define-modify-macro}.  The @code{letf} and @code{cl-letf*}
Glenn Morris's avatar
Glenn Morris committed
1200 1201 1202
macros are used in the processing of symbol macros;
@pxref{Macro Bindings}.

Glenn Morris's avatar