Merge from emacs-24; up to 2012-04-21T14:12:27Z!sdl.web@gmail.com
[bpt/emacs.git] / doc / lispref / functions.texi
CommitLineData
b8d4c8d0
GM
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
acaf905b 3@c Copyright (C) 1990-1995, 1998-1999, 2001-2012
d24880de 4@c Free Software Foundation, Inc.
b8d4c8d0 5@c See the file elisp.texi for copying conditions.
b8d4c8d0
GM
6@node Functions, Macros, Variables, Top
7@chapter Functions
8
9 A Lisp program is composed mainly of Lisp functions. This chapter
10explains what functions are, how they accept arguments, and how to
11define them.
12
13@menu
14* What Is a Function:: Lisp functions vs. primitives; terminology.
15* Lambda Expressions:: How functions are expressed as Lisp objects.
16* Function Names:: A symbol can serve as the name of a function.
17* Defining Functions:: Lisp expressions for defining functions.
18* Calling Functions:: How to use an existing function.
19* Mapping Functions:: Applying a function to each element of a list, etc.
20* Anonymous Functions:: Lambda expressions are functions with no names.
21* Function Cells:: Accessing or setting the function definition
22 of a symbol.
735cc5ca 23* Closures:: Functions that enclose a lexical environment.
b8d4c8d0 24* Obsolete Functions:: Declaring functions obsolete.
735cc5ca 25* Inline Functions:: Functions that the compiler will expand inline.
e31dfb12 26* Declaring Functions:: Telling the compiler that a function is defined.
b8d4c8d0
GM
27* Function Safety:: Determining whether a function is safe to call.
28* Related Topics:: Cross-references to specific Lisp primitives
29 that have a special bearing on how functions work.
30@end menu
31
32@node What Is a Function
33@section What Is a Function?
34
735cc5ca
CY
35@cindex return value
36@cindex value of function
37@cindex argument
38 In a general sense, a function is a rule for carrying out a
39computation given input values called @dfn{arguments}. The result of
40the computation is called the @dfn{value} or @dfn{return value} of the
41function. The computation can also have side effects, such as lasting
42changes in the values of variables or the contents of data structures.
43
44 In most computer languages, every function has a name. But in Lisp,
45a function in the strictest sense has no name: it is an object which
46can @emph{optionally} be associated with a symbol (e.g.@: @code{car})
47that serves as the function name. @xref{Function Names}. When a
48function has been given a name, we usually also refer to that symbol
49as a ``function'' (e.g.@: we refer to ``the function @code{car}'').
50In this manual, the distinction between a function name and the
51function object itself is usually unimportant, but we will take note
52wherever it is relevant.
53
54 Certain function-like objects, called @dfn{special forms} and
55@dfn{macros}, also accept arguments to carry out computations.
56However, as explained below, these are not considered functions in
57Emacs Lisp.
58
59 Here are important terms for functions and function-like objects:
b8d4c8d0
GM
60
61@table @dfn
735cc5ca
CY
62@item lambda expression
63A function (in the strict sense, i.e.@: a function object) which is
64written in Lisp. These are described in the following section.
65@ifnottex
66@xref{Lambda Expressions}.
67@end ifnottex
b8d4c8d0
GM
68
69@item primitive
70@cindex primitive
71@cindex subr
72@cindex built-in function
735cc5ca
CY
73A function which is callable from Lisp but is actually written in C.
74Primitives are also called @dfn{built-in functions}, or @dfn{subrs}.
75Examples include functions like @code{car} and @code{append}. In
76addition, all special forms (see below) are also considered
77primitives.
78
79Usually, a function is implemented as a primitive because it is a
80fundamental part of Lisp (e.g.@: @code{car}), or because it provides a
81low-level interface to operating system services, or because it needs
82to run fast. Unlike functions defined in Lisp, primitives can be
83modified or added only by changing the C sources and recompiling
84Emacs. See @ref{Writing Emacs Primitives}.
b8d4c8d0
GM
85
86@item special form
735cc5ca
CY
87A primitive that is like a function but does not evaluate all of its
88arguments in the usual way. It may evaluate only some of the
89arguments, or may evaluate them in an unusual order, or several times.
90Examples include @code{if}, @code{and}, and @code{while}.
91@xref{Special Forms}.
b8d4c8d0
GM
92
93@item macro
94@cindex macro
735cc5ca
CY
95A construct defined in Lisp, which differs from a function in that it
96translates a Lisp expression into another expression which is to be
97evaluated instead of the original expression. Macros enable Lisp
98programmers to do the sorts of things that special forms can do.
99@xref{Macros}.
b8d4c8d0
GM
100
101@item command
102@cindex command
735cc5ca
CY
103An object which can be invoked via the @code{command-execute}
104primitive, usually due to the user typing in a key sequence
105@dfn{bound} to that command. @xref{Interactive Call}. A command is
106usually a function; if the function is written in Lisp, it is made
107into a command by an @code{interactive} form in the function
108definition (@pxref{Defining Commands}). Commands that are functions
109can also be called from Lisp expressions, just like other functions.
b8d4c8d0
GM
110
111Keyboard macros (strings and vectors) are commands also, even though
735cc5ca
CY
112they are not functions. @xref{Keyboard Macros}. We say that a symbol
113is a command if its function cell contains a command (@pxref{Symbol
114Components}); such a @dfn{named command} can be invoked with
115@kbd{M-x}.
116
117@item closure
118A function object that is much like a lambda expression, except that
119it also encloses an ``environment'' of lexical variable bindings.
120@xref{Closures}.
b8d4c8d0
GM
121
122@item byte-code function
735cc5ca
CY
123A function that has been compiled by the byte compiler.
124@xref{Byte-Code Type}.
c7844a27
LMI
125
126@item autoload object
127@cindex autoload object
735cc5ca
CY
128A place-holder for a real function. If the autoload object is called,
129Emacs loads the file containing the definition of the real function,
130and then calls the real function. @xref{Autoload}.
b8d4c8d0
GM
131@end table
132
735cc5ca
CY
133 You can use the function @code{functionp} to test if an object is a
134function:
135
b8d4c8d0
GM
136@defun functionp object
137This function returns @code{t} if @var{object} is any kind of
9b240644 138function, i.e.@: can be passed to @code{funcall}. Note that
735cc5ca
CY
139@code{functionp} returns @code{t} for symbols that are function names,
140and returns @code{nil} for special forms.
b8d4c8d0
GM
141@end defun
142
735cc5ca
CY
143@noindent
144Unlike @code{functionp}, the next three functions do @emph{not} treat
145a symbol as its function definition.
b8d4c8d0
GM
146
147@defun subrp object
148This function returns @code{t} if @var{object} is a built-in function
149(i.e., a Lisp primitive).
150
151@example
152@group
153(subrp 'message) ; @r{@code{message} is a symbol,}
154 @result{} nil ; @r{not a subr object.}
155@end group
156@group
157(subrp (symbol-function 'message))
158 @result{} t
159@end group
160@end example
161@end defun
162
163@defun byte-code-function-p object
164This function returns @code{t} if @var{object} is a byte-code
165function. For example:
166
167@example
168@group
169(byte-code-function-p (symbol-function 'next-line))
170 @result{} t
171@end group
172@end example
173@end defun
174
175@defun subr-arity subr
176This function provides information about the argument list of a
177primitive, @var{subr}. The returned value is a pair
178@code{(@var{min} . @var{max})}. @var{min} is the minimum number of
179args. @var{max} is the maximum number or the symbol @code{many}, for a
180function with @code{&rest} arguments, or the symbol @code{unevalled} if
181@var{subr} is a special form.
182@end defun
183
184@node Lambda Expressions
185@section Lambda Expressions
186@cindex lambda expression
187
735cc5ca
CY
188 A lambda expression is a function object written in Lisp. Here is
189an example:
b8d4c8d0
GM
190
191@example
735cc5ca
CY
192(lambda (x)
193 "Return the hyperbolic cosine of X."
194 (* 0.5 (+ (exp x) (exp (- x)))))
b8d4c8d0
GM
195@end example
196
197@noindent
735cc5ca
CY
198In Emacs Lisp, such a list is valid as an expression---it evaluates to
199itself. But its main use is not to be evaluated as an expression, but
200to be called as a function.
201
202 A lambda expression, by itself, has no name; it is an @dfn{anonymous
203function}. Although lambda expressions can be used this way
204(@pxref{Anonymous Functions}), they are more commonly associated with
205symbols to make @dfn{named functions} (@pxref{Function Names}).
206Before going into these details, the following subsections describe
207the components of a lambda expression and what they do.
b8d4c8d0
GM
208
209@menu
210* Lambda Components:: The parts of a lambda expression.
211* Simple Lambda:: A simple example.
212* Argument List:: Details and special features of argument lists.
213* Function Documentation:: How to put documentation in a function.
214@end menu
215
216@node Lambda Components
217@subsection Components of a Lambda Expression
218
735cc5ca 219 A lambda expression is a list that looks like this:
b8d4c8d0
GM
220
221@example
222(lambda (@var{arg-variables}@dots{})
223 [@var{documentation-string}]
224 [@var{interactive-declaration}]
225 @var{body-forms}@dots{})
226@end example
b8d4c8d0
GM
227
228@cindex lambda list
229 The first element of a lambda expression is always the symbol
230@code{lambda}. This indicates that the list represents a function. The
231reason functions are defined to start with @code{lambda} is so that
232other lists, intended for other uses, will not accidentally be valid as
233functions.
234
235 The second element is a list of symbols---the argument variable names.
236This is called the @dfn{lambda list}. When a Lisp function is called,
237the argument values are matched up against the variables in the lambda
238list, which are given local bindings with the values provided.
239@xref{Local Variables}.
240
241 The documentation string is a Lisp string object placed within the
242function definition to describe the function for the Emacs help
243facilities. @xref{Function Documentation}.
244
245 The interactive declaration is a list of the form @code{(interactive
246@var{code-string})}. This declares how to provide arguments if the
247function is used interactively. Functions with this declaration are called
248@dfn{commands}; they can be called using @kbd{M-x} or bound to a key.
249Functions not intended to be called in this way should not have interactive
250declarations. @xref{Defining Commands}, for how to write an interactive
251declaration.
252
253@cindex body of function
254 The rest of the elements are the @dfn{body} of the function: the Lisp
255code to do the work of the function (or, as a Lisp programmer would say,
256``a list of Lisp forms to evaluate''). The value returned by the
257function is the value returned by the last element of the body.
258
259@node Simple Lambda
735cc5ca 260@subsection A Simple Lambda Expression Example
b8d4c8d0 261
735cc5ca 262 Consider the following example:
b8d4c8d0
GM
263
264@example
265(lambda (a b c) (+ a b c))
266@end example
267
268@noindent
88ed9e87 269We can call this function by passing it to @code{funcall}, like this:
b8d4c8d0
GM
270
271@example
272@group
88ed9e87
SM
273(funcall (lambda (a b c) (+ a b c))
274 1 2 3)
b8d4c8d0
GM
275@end group
276@end example
277
278@noindent
279This call evaluates the body of the lambda expression with the variable
280@code{a} bound to 1, @code{b} bound to 2, and @code{c} bound to 3.
281Evaluation of the body adds these three numbers, producing the result 6;
282therefore, this call to the function returns the value 6.
283
284 Note that the arguments can be the results of other function calls, as in
285this example:
286
287@example
288@group
88ed9e87
SM
289(funcall (lambda (a b c) (+ a b c))
290 1 (* 2 3) (- 5 4))
b8d4c8d0
GM
291@end group
292@end example
293
294@noindent
295This evaluates the arguments @code{1}, @code{(* 2 3)}, and @code{(- 5
2964)} from left to right. Then it applies the lambda expression to the
297argument values 1, 6 and 1 to produce the value 8.
298
735cc5ca
CY
299 As these examples show, you can use a form with a lambda expression
300as its @sc{car} to make local variables and give them values. In the
301old days of Lisp, this technique was the only way to bind and
302initialize local variables. But nowadays, it is clearer to use the
303special form @code{let} for this purpose (@pxref{Local Variables}).
304Lambda expressions are mainly used as anonymous functions for passing
305as arguments to other functions (@pxref{Anonymous Functions}), or
306stored as symbol function definitions to produce named functions
307(@pxref{Function Names}).
b8d4c8d0
GM
308
309@node Argument List
310@subsection Other Features of Argument Lists
311@kindex wrong-number-of-arguments
312@cindex argument binding
313@cindex binding arguments
314@cindex argument lists, features
315
316 Our simple sample function, @code{(lambda (a b c) (+ a b c))},
317specifies three argument variables, so it must be called with three
318arguments: if you try to call it with only two arguments or four
319arguments, you get a @code{wrong-number-of-arguments} error.
320
321 It is often convenient to write a function that allows certain
322arguments to be omitted. For example, the function @code{substring}
323accepts three arguments---a string, the start index and the end
324index---but the third argument defaults to the @var{length} of the
325string if you omit it. It is also convenient for certain functions to
326accept an indefinite number of arguments, as the functions @code{list}
327and @code{+} do.
328
329@cindex optional arguments
330@cindex rest arguments
331@kindex &optional
332@kindex &rest
333 To specify optional arguments that may be omitted when a function
334is called, simply include the keyword @code{&optional} before the optional
335arguments. To specify a list of zero or more extra arguments, include the
336keyword @code{&rest} before one final argument.
337
338 Thus, the complete syntax for an argument list is as follows:
339
340@example
341@group
342(@var{required-vars}@dots{}
343 @r{[}&optional @var{optional-vars}@dots{}@r{]}
344 @r{[}&rest @var{rest-var}@r{]})
345@end group
346@end example
347
348@noindent
349The square brackets indicate that the @code{&optional} and @code{&rest}
350clauses, and the variables that follow them, are optional.
351
352 A call to the function requires one actual argument for each of the
353@var{required-vars}. There may be actual arguments for zero or more of
354the @var{optional-vars}, and there cannot be any actual arguments beyond
355that unless the lambda list uses @code{&rest}. In that case, there may
356be any number of extra actual arguments.
357
358 If actual arguments for the optional and rest variables are omitted,
359then they always default to @code{nil}. There is no way for the
360function to distinguish between an explicit argument of @code{nil} and
361an omitted argument. However, the body of the function is free to
362consider @code{nil} an abbreviation for some other meaningful value.
363This is what @code{substring} does; @code{nil} as the third argument to
364@code{substring} means to use the length of the string supplied.
365
366@cindex CL note---default optional arg
367@quotation
368@b{Common Lisp note:} Common Lisp allows the function to specify what
369default value to use when an optional argument is omitted; Emacs Lisp
370always uses @code{nil}. Emacs Lisp does not support ``supplied-p''
371variables that tell you whether an argument was explicitly passed.
372@end quotation
373
374 For example, an argument list that looks like this:
375
376@example
377(a b &optional c d &rest e)
378@end example
379
380@noindent
381binds @code{a} and @code{b} to the first two actual arguments, which are
382required. If one or two more arguments are provided, @code{c} and
383@code{d} are bound to them respectively; any arguments after the first
384four are collected into a list and @code{e} is bound to that list. If
385there are only two arguments, @code{c} is @code{nil}; if two or three
386arguments, @code{d} is @code{nil}; if four arguments or fewer, @code{e}
387is @code{nil}.
388
389 There is no way to have required arguments following optional
390ones---it would not make sense. To see why this must be so, suppose
391that @code{c} in the example were optional and @code{d} were required.
392Suppose three actual arguments are given; which variable would the
393third argument be for? Would it be used for the @var{c}, or for
394@var{d}? One can argue for both possibilities. Similarly, it makes
395no sense to have any more arguments (either required or optional)
396after a @code{&rest} argument.
397
398 Here are some examples of argument lists and proper calls:
399
ddff3351 400@example
88ed9e87
SM
401(funcall (lambda (n) (1+ n)) ; @r{One required:}
402 1) ; @r{requires exactly one argument.}
b8d4c8d0 403 @result{} 2
88ed9e87
SM
404(funcall (lambda (n &optional n1) ; @r{One required and one optional:}
405 (if n1 (+ n n1) (1+ n))) ; @r{1 or 2 arguments.}
406 1 2)
b8d4c8d0 407 @result{} 3
88ed9e87
SM
408(funcall (lambda (n &rest ns) ; @r{One required and one rest:}
409 (+ n (apply '+ ns))) ; @r{1 or more arguments.}
410 1 2 3 4 5)
b8d4c8d0 411 @result{} 15
ddff3351 412@end example
b8d4c8d0
GM
413
414@node Function Documentation
415@subsection Documentation Strings of Functions
416@cindex documentation of function
417
735cc5ca
CY
418 A lambda expression may optionally have a @dfn{documentation string}
419just after the lambda list. This string does not affect execution of
420the function; it is a kind of comment, but a systematized comment
421which actually appears inside the Lisp world and can be used by the
422Emacs help facilities. @xref{Documentation}, for how the
423documentation string is accessed.
b8d4c8d0
GM
424
425 It is a good idea to provide documentation strings for all the
426functions in your program, even those that are called only from within
427your program. Documentation strings are like comments, except that they
428are easier to access.
429
430 The first line of the documentation string should stand on its own,
431because @code{apropos} displays just this first line. It should consist
432of one or two complete sentences that summarize the function's purpose.
433
434 The start of the documentation string is usually indented in the
435source file, but since these spaces come before the starting
436double-quote, they are not part of the string. Some people make a
437practice of indenting any additional lines of the string so that the
438text lines up in the program source. @emph{That is a mistake.} The
439indentation of the following lines is inside the string; what looks
440nice in the source code will look ugly when displayed by the help
441commands.
442
443 You may wonder how the documentation string could be optional, since
444there are required components of the function that follow it (the body).
445Since evaluation of a string returns that string, without any side effects,
446it has no effect if it is not the last form in the body. Thus, in
447practice, there is no confusion between the first form of the body and the
448documentation string; if the only body form is a string then it serves both
449as the return value and as the documentation.
450
451 The last line of the documentation string can specify calling
452conventions different from the actual function arguments. Write
453text like this:
454
455@example
456\(fn @var{arglist})
457@end example
458
459@noindent
460following a blank line, at the beginning of the line, with no newline
461following it inside the documentation string. (The @samp{\} is used
462to avoid confusing the Emacs motion commands.) The calling convention
463specified in this way appears in help messages in place of the one
464derived from the actual arguments of the function.
465
466 This feature is particularly useful for macro definitions, since the
467arguments written in a macro definition often do not correspond to the
468way users think of the parts of the macro call.
469
470@node Function Names
471@section Naming a Function
472@cindex function definition
473@cindex named function
474@cindex function name
475
735cc5ca
CY
476 A symbol can serve as the name of a function. This happens when the
477symbol's @dfn{function cell} (@pxref{Symbol Components}) contains a
478function object (e.g.@: a lambda expression). Then the symbol itself
479becomes a valid, callable function, equivalent to the function object
480in its function cell.
481
482 The contents of the function cell are also called the symbol's
483@dfn{function definition}. The procedure of using a symbol's function
484definition in place of the symbol is called @dfn{symbol function
485indirection}; see @ref{Function Indirection}. If you have not given a
486symbol a function definition, its function cell is said to be
487@dfn{void}, and it cannot be used as a function.
488
489 In practice, nearly all functions have names, and are referred to by
490their names. You can create a named Lisp function by defining a
491lambda expression and putting it in a function cell (@pxref{Function
492Cells}). However, it is more common to use the @code{defun} special
493form, described in the next section.
494@ifnottex
495@xref{Defining Functions}.
496@end ifnottex
b8d4c8d0
GM
497
498 We give functions names because it is convenient to refer to them by
735cc5ca
CY
499their names in Lisp expressions. Also, a named Lisp function can
500easily refer to itself---it can be recursive. Furthermore, primitives
501can only be referred to textually by their names, since primitive
502function objects (@pxref{Primitive Function Type}) have no read
503syntax.
504
505 A function need not have a unique name. A given function object
506@emph{usually} appears in the function cell of only one symbol, but
507this is just a convention. It is easy to store it in several symbols
508using @code{fset}; then each of the symbols is a valid name for the
509same function.
510
511 Note that a symbol used as a function name may also be used as a
512variable; these two uses of a symbol are independent and do not
513conflict. (This is not the case in some dialects of Lisp, like
514Scheme.)
b8d4c8d0
GM
515
516@node Defining Functions
517@section Defining Functions
518@cindex defining a function
519
520 We usually give a name to a function when it is first created. This
521is called @dfn{defining a function}, and it is done with the
522@code{defun} special form.
523
735cc5ca 524@defspec defun name argument-list body-forms...
b8d4c8d0
GM
525@code{defun} is the usual way to define new Lisp functions. It
526defines the symbol @var{name} as a function that looks like this:
527
528@example
529(lambda @var{argument-list} . @var{body-forms})
530@end example
531
532@code{defun} stores this lambda expression in the function cell of
533@var{name}. It returns the value @var{name}, but usually we ignore this
534value.
535
536As described previously, @var{argument-list} is a list of argument
735cc5ca
CY
537names and may include the keywords @code{&optional} and @code{&rest}.
538Also, the first two of the @var{body-forms} may be a documentation
539string and an interactive declaration. @xref{Lambda Components}.
b8d4c8d0
GM
540
541Here are some examples:
542
543@example
544@group
545(defun foo () 5)
546 @result{} foo
547@end group
548@group
549(foo)
550 @result{} 5
551@end group
552
553@group
554(defun bar (a &optional b &rest c)
555 (list a b c))
556 @result{} bar
557@end group
558@group
559(bar 1 2 3 4 5)
560 @result{} (1 2 (3 4 5))
561@end group
562@group
563(bar 1)
564 @result{} (1 nil nil)
565@end group
566@group
567(bar)
568@error{} Wrong number of arguments.
569@end group
570
571@group
572(defun capitalize-backwards ()
735cc5ca 573 "Upcase the last letter of the word at point."
b8d4c8d0
GM
574 (interactive)
575 (backward-word 1)
576 (forward-word 1)
577 (backward-char 1)
578 (capitalize-word 1))
579 @result{} capitalize-backwards
580@end group
581@end example
582
583Be careful not to redefine existing functions unintentionally.
584@code{defun} redefines even primitive functions such as @code{car}
735cc5ca
CY
585without any hesitation or notification. Emacs does not prevent you
586from doing this, because redefining a function is sometimes done
587deliberately, and there is no way to distinguish deliberate
588redefinition from unintentional redefinition.
b8d4c8d0
GM
589@end defspec
590
591@cindex function aliases
592@defun defalias name definition &optional docstring
593@anchor{Definition of defalias}
594This special form defines the symbol @var{name} as a function, with
595definition @var{definition} (which can be any valid Lisp function).
596It returns @var{definition}.
597
598If @var{docstring} is non-@code{nil}, it becomes the function
599documentation of @var{name}. Otherwise, any documentation provided by
600@var{definition} is used.
601
602The proper place to use @code{defalias} is where a specific function
603name is being defined---especially where that name appears explicitly in
604the source file being loaded. This is because @code{defalias} records
605which file defined the function, just like @code{defun}
606(@pxref{Unloading}).
607
608By contrast, in programs that manipulate function definitions for other
609purposes, it is better to use @code{fset}, which does not keep such
610records. @xref{Function Cells}.
611@end defun
612
613 You cannot create a new primitive function with @code{defun} or
614@code{defalias}, but you can use them to change the function definition of
615any symbol, even one such as @code{car} or @code{x-popup-menu} whose
616normal definition is a primitive. However, this is risky: for
617instance, it is next to impossible to redefine @code{car} without
618breaking Lisp completely. Redefining an obscure function such as
619@code{x-popup-menu} is less dangerous, but it still may not work as
620you expect. If there are calls to the primitive from C code, they
621call the primitive's C definition directly, so changing the symbol's
622definition will have no effect on them.
623
624 See also @code{defsubst}, which defines a function like @code{defun}
735cc5ca
CY
625and tells the Lisp compiler to perform inline expansion on it.
626@xref{Inline Functions}.
b8d4c8d0
GM
627
628@node Calling Functions
629@section Calling Functions
630@cindex function invocation
631@cindex calling a function
632
633 Defining functions is only half the battle. Functions don't do
634anything until you @dfn{call} them, i.e., tell them to run. Calling a
635function is also known as @dfn{invocation}.
636
637 The most common way of invoking a function is by evaluating a list.
638For example, evaluating the list @code{(concat "a" "b")} calls the
639function @code{concat} with arguments @code{"a"} and @code{"b"}.
640@xref{Evaluation}, for a description of evaluation.
641
642 When you write a list as an expression in your program, you specify
643which function to call, and how many arguments to give it, in the text
644of the program. Usually that's just what you want. Occasionally you
645need to compute at run time which function to call. To do that, use
646the function @code{funcall}. When you also need to determine at run
647time how many arguments to pass, use @code{apply}.
648
649@defun funcall function &rest arguments
650@code{funcall} calls @var{function} with @var{arguments}, and returns
651whatever @var{function} returns.
652
653Since @code{funcall} is a function, all of its arguments, including
654@var{function}, are evaluated before @code{funcall} is called. This
655means that you can use any expression to obtain the function to be
656called. It also means that @code{funcall} does not see the
657expressions you write for the @var{arguments}, only their values.
658These values are @emph{not} evaluated a second time in the act of
659calling @var{function}; the operation of @code{funcall} is like the
660normal procedure for calling a function, once its arguments have
661already been evaluated.
662
663The argument @var{function} must be either a Lisp function or a
664primitive function. Special forms and macros are not allowed, because
665they make sense only when given the ``unevaluated'' argument
666expressions. @code{funcall} cannot provide these because, as we saw
667above, it never knows them in the first place.
668
669@example
670@group
671(setq f 'list)
672 @result{} list
673@end group
674@group
675(funcall f 'x 'y 'z)
676 @result{} (x y z)
677@end group
678@group
679(funcall f 'x 'y '(z))
680 @result{} (x y (z))
681@end group
682@group
683(funcall 'and t nil)
684@error{} Invalid function: #<subr and>
685@end group
686@end example
687
688Compare these examples with the examples of @code{apply}.
689@end defun
690
691@defun apply function &rest arguments
692@code{apply} calls @var{function} with @var{arguments}, just like
693@code{funcall} but with one difference: the last of @var{arguments} is a
694list of objects, which are passed to @var{function} as separate
695arguments, rather than a single list. We say that @code{apply}
696@dfn{spreads} this list so that each individual element becomes an
697argument.
698
699@code{apply} returns the result of calling @var{function}. As with
700@code{funcall}, @var{function} must either be a Lisp function or a
701primitive function; special forms and macros do not make sense in
702@code{apply}.
703
704@example
705@group
706(setq f 'list)
707 @result{} list
708@end group
709@group
710(apply f 'x 'y 'z)
711@error{} Wrong type argument: listp, z
712@end group
713@group
714(apply '+ 1 2 '(3 4))
715 @result{} 10
716@end group
717@group
718(apply '+ '(1 2 3 4))
719 @result{} 10
720@end group
721
722@group
723(apply 'append '((a b c) nil (x y z) nil))
724 @result{} (a b c x y z)
725@end group
726@end example
727
728For an interesting example of using @code{apply}, see @ref{Definition
729of mapcar}.
730@end defun
731
80f85d7c
EZ
732@cindex partial application of functions
733@cindex currying
a18a6d49 734 Sometimes it is useful to fix some of the function's arguments at
80f85d7c
EZ
735certain values, and leave the rest of arguments for when the function
736is actually called. The act of fixing some of the function's
737arguments is called @dfn{partial application} of the function@footnote{
738This is related to, but different from @dfn{currying}, which
739transforms a function that takes multiple arguments in such a way that
740it can be called as a chain of functions, each one with a single
741argument.}.
742The result is a new function that accepts the rest of
743arguments and calls the original function with all the arguments
a18a6d49
EZ
744combined.
745
746 Here's how to do partial application in Emacs Lisp:
80f85d7c
EZ
747
748@defun apply-partially func &rest args
749This function returns a new function which, when called, will call
750@var{func} with the list of arguments composed from @var{args} and
751additional arguments specified at the time of the call. If @var{func}
752accepts @var{n} arguments, then a call to @code{apply-partially} with
753@w{@code{@var{m} < @var{n}}} arguments will produce a new function of
754@w{@code{@var{n} - @var{m}}} arguments.
755
834b5485
EZ
756Here's how we could define the built-in function @code{1+}, if it
757didn't exist, using @code{apply-partially} and @code{+}, another
758built-in function:
80f85d7c
EZ
759
760@example
80f85d7c 761@group
834b5485
EZ
762(defalias '1+ (apply-partially '+ 1)
763 "Increment argument by one.")
764@end group
765@group
766(1+ 10)
80f85d7c
EZ
767 @result{} 11
768@end group
769@end example
770@end defun
771
b8d4c8d0
GM
772@cindex functionals
773 It is common for Lisp functions to accept functions as arguments or
774find them in data structures (especially in hook variables and property
775lists) and call them using @code{funcall} or @code{apply}. Functions
776that accept function arguments are often called @dfn{functionals}.
777
778 Sometimes, when you call a functional, it is useful to supply a no-op
779function as the argument. Here are two different kinds of no-op
780function:
781
782@defun identity arg
783This function returns @var{arg} and has no side effects.
784@end defun
785
786@defun ignore &rest args
787This function ignores any arguments and returns @code{nil}.
788@end defun
789
735cc5ca
CY
790 Some functions are user-visible @dfn{commands}, which can be called
791interactively (usually by a key sequence). It is possible to invoke
792such a command exactly as though it was called interactively, by using
793the @code{call-interactively} function. @xref{Interactive Call}.
413c488d 794
b8d4c8d0
GM
795@node Mapping Functions
796@section Mapping Functions
797@cindex mapping functions
798
799 A @dfn{mapping function} applies a given function (@emph{not} a
800special form or macro) to each element of a list or other collection.
735cc5ca
CY
801Emacs Lisp has several such functions; this section describes
802@code{mapcar}, @code{mapc}, and @code{mapconcat}, which map over a
803list. @xref{Definition of mapatoms}, for the function @code{mapatoms}
804which maps over the symbols in an obarray. @xref{Definition of
805maphash}, for the function @code{maphash} which maps over key/value
806associations in a hash table.
b8d4c8d0
GM
807
808 These mapping functions do not allow char-tables because a char-table
809is a sparse array whose nominal range of indices is very large. To map
810over a char-table in a way that deals properly with its sparse nature,
811use the function @code{map-char-table} (@pxref{Char-Tables}).
812
813@defun mapcar function sequence
814@anchor{Definition of mapcar}
815@code{mapcar} applies @var{function} to each element of @var{sequence}
816in turn, and returns a list of the results.
817
818The argument @var{sequence} can be any kind of sequence except a
819char-table; that is, a list, a vector, a bool-vector, or a string. The
820result is always a list. The length of the result is the same as the
821length of @var{sequence}. For example:
822
ddff3351 823@example
b8d4c8d0
GM
824@group
825(mapcar 'car '((a b) (c d) (e f)))
826 @result{} (a c e)
827(mapcar '1+ [1 2 3])
828 @result{} (2 3 4)
3e99b825 829(mapcar 'string "abc")
b8d4c8d0
GM
830 @result{} ("a" "b" "c")
831@end group
832
833@group
834;; @r{Call each function in @code{my-hooks}.}
835(mapcar 'funcall my-hooks)
836@end group
837
838@group
839(defun mapcar* (function &rest args)
840 "Apply FUNCTION to successive cars of all ARGS.
841Return the list of results."
842 ;; @r{If no list is exhausted,}
843 (if (not (memq nil args))
844 ;; @r{apply function to @sc{car}s.}
845 (cons (apply function (mapcar 'car args))
846 (apply 'mapcar* function
847 ;; @r{Recurse for rest of elements.}
848 (mapcar 'cdr args)))))
849@end group
850
851@group
852(mapcar* 'cons '(a b c) '(1 2 3 4))
853 @result{} ((a . 1) (b . 2) (c . 3))
854@end group
ddff3351 855@end example
b8d4c8d0
GM
856@end defun
857
858@defun mapc function sequence
859@code{mapc} is like @code{mapcar} except that @var{function} is used for
860side-effects only---the values it returns are ignored, not collected
861into a list. @code{mapc} always returns @var{sequence}.
862@end defun
863
864@defun mapconcat function sequence separator
865@code{mapconcat} applies @var{function} to each element of
866@var{sequence}: the results, which must be strings, are concatenated.
867Between each pair of result strings, @code{mapconcat} inserts the string
868@var{separator}. Usually @var{separator} contains a space or comma or
869other suitable punctuation.
870
871The argument @var{function} must be a function that can take one
872argument and return a string. The argument @var{sequence} can be any
873kind of sequence except a char-table; that is, a list, a vector, a
874bool-vector, or a string.
875
ddff3351 876@example
b8d4c8d0
GM
877@group
878(mapconcat 'symbol-name
879 '(The cat in the hat)
880 " ")
881 @result{} "The cat in the hat"
882@end group
883
884@group
885(mapconcat (function (lambda (x) (format "%c" (1+ x))))
886 "HAL-8000"
887 "")
888 @result{} "IBM.9111"
889@end group
ddff3351 890@end example
b8d4c8d0
GM
891@end defun
892
893@node Anonymous Functions
894@section Anonymous Functions
895@cindex anonymous function
896
735cc5ca
CY
897 Although functions are usually defined with @code{defun} and given
898names at the same time, it is sometimes convenient to use an explicit
899lambda expression---an @dfn{anonymous function}. Anonymous functions
900are valid wherever function names are. They are often assigned as
901variable values, or as arguments to functions; for instance, you might
902pass one as the @var{function} argument to @code{mapcar}, which
903applies that function to each element of a list (@pxref{Mapping
904Functions}). @xref{describe-symbols example}, for a realistic example
905of this.
906
907 When defining a lambda expression that is to be used as an anonymous
908function, you can in principle use any method to construct the list.
909But typically you should use the @code{lambda} macro, or the
910@code{function} special form, or the @code{#'} read syntax:
911
912@defmac lambda args body...
913This macro returns an anonymous function with argument list @var{args}
914and body forms given by @var{body}. In effect, this macro makes
915@code{lambda} forms ``self-quoting'': evaluating a form whose @sc{car}
916is @code{lambda} yields the form itself:
b8d4c8d0 917
735cc5ca
CY
918@example
919(lambda (x) (* x x))
920 @result{} (lambda (x) (* x x))
921@end example
b8d4c8d0 922
735cc5ca
CY
923The @code{lambda} form has one other effect: it tells the Emacs
924evaluator and byte-compiler that its argument is a function, by using
925@code{function} as a subroutine (see below).
926@end defmac
b8d4c8d0 927
735cc5ca
CY
928@defspec function function-object
929@cindex function quoting
930This special form returns @var{function-object} without evaluating it.
931In this, it is similar to @code{quote} (@pxref{Quoting}). But unlike
932@code{quote}, it also serves as a note to the Emacs evaluator and
933byte-compiler that @var{function-object} is intended to be used as a
934function. Assuming @var{function-object} is a valid lambda
935expression, this has two effects:
b8d4c8d0 936
735cc5ca
CY
937@itemize
938@item
939When the code is byte-compiled, @var{function-object} is compiled into
940a byte-code function object (@pxref{Byte Compilation}).
b8d4c8d0 941
735cc5ca
CY
942@item
943When lexical binding is enabled, @var{function-object} is converted
944into a closure. @xref{Closures}.
945@end itemize
946@end defspec
b8d4c8d0 947
735cc5ca
CY
948@cindex @samp{#'} syntax
949The read syntax @code{#'} is a short-hand for using @code{function}.
950The following forms are all equivalent:
b8d4c8d0 951
735cc5ca
CY
952@example
953(lambda (x) (* x x))
954(function (lambda (x) (* x x)))
955#'(lambda (x) (* x x))
956@end example
b8d4c8d0 957
5d6ab672
CY
958 In the following example, we define a @code{change-property}
959function that takes a function as its third argument, followed by a
960@code{double-property} function that makes use of
961@code{change-property} by passing it an anonymous function:
b8d4c8d0
GM
962
963@example
964@group
965(defun change-property (symbol prop function)
966 (let ((value (get symbol prop)))
967 (put symbol prop (funcall function value))))
968@end group
b8d4c8d0 969
b8d4c8d0
GM
970@group
971(defun double-property (symbol prop)
5d6ab672 972 (change-property symbol prop (lambda (x) (* 2 x))))
b8d4c8d0
GM
973@end group
974@end example
975
b8d4c8d0 976@noindent
735cc5ca 977Note that we do not quote the @code{lambda} form.
b8d4c8d0 978
735cc5ca
CY
979 If you compile the above code, the anonymous function is also
980compiled. This would not happen if, say, you had constructed the
981anonymous function by quoting it as a list:
b8d4c8d0
GM
982
983@example
984@group
985(defun double-property (symbol prop)
5d6ab672 986 (change-property symbol prop '(lambda (x) (* 2 x))))
b8d4c8d0
GM
987@end group
988@end example
989
990@noindent
735cc5ca
CY
991In that case, the anonymous function is kept as a lambda expression in
992the compiled code. The byte-compiler cannot assume this list is a
993function, even though it looks like one, since it does not know that
994@code{change-property} intends to use it as a function.
b8d4c8d0
GM
995
996@node Function Cells
997@section Accessing Function Cell Contents
998
999 The @dfn{function definition} of a symbol is the object stored in the
1000function cell of the symbol. The functions described here access, test,
1001and set the function cell of symbols.
1002
1003 See also the function @code{indirect-function}. @xref{Definition of
1004indirect-function}.
1005
1006@defun symbol-function symbol
1007@kindex void-function
1008This returns the object in the function cell of @var{symbol}. If the
1009symbol's function cell is void, a @code{void-function} error is
1010signaled.
1011
1012This function does not check that the returned object is a legitimate
1013function.
1014
1015@example
1016@group
1017(defun bar (n) (+ n 2))
1018 @result{} bar
1019@end group
1020@group
1021(symbol-function 'bar)
1022 @result{} (lambda (n) (+ n 2))
1023@end group
1024@group
1025(fset 'baz 'bar)
1026 @result{} bar
1027@end group
1028@group
1029(symbol-function 'baz)
1030 @result{} bar
1031@end group
1032@end example
1033@end defun
1034
1035@cindex void function cell
1036 If you have never given a symbol any function definition, we say that
1037that symbol's function cell is @dfn{void}. In other words, the function
1038cell does not have any Lisp object in it. If you try to call such a symbol
1039as a function, it signals a @code{void-function} error.
1040
1041 Note that void is not the same as @code{nil} or the symbol
1042@code{void}. The symbols @code{nil} and @code{void} are Lisp objects,
1043and can be stored into a function cell just as any other object can be
1044(and they can be valid functions if you define them in turn with
1045@code{defun}). A void function cell contains no object whatsoever.
1046
1047 You can test the voidness of a symbol's function definition with
1048@code{fboundp}. After you have given a symbol a function definition, you
1049can make it void once more using @code{fmakunbound}.
1050
1051@defun fboundp symbol
1052This function returns @code{t} if the symbol has an object in its
1053function cell, @code{nil} otherwise. It does not check that the object
1054is a legitimate function.
1055@end defun
1056
1057@defun fmakunbound symbol
1058This function makes @var{symbol}'s function cell void, so that a
1059subsequent attempt to access this cell will cause a
1060@code{void-function} error. It returns @var{symbol}. (See also
1061@code{makunbound}, in @ref{Void Variables}.)
1062
1063@example
1064@group
1065(defun foo (x) x)
1066 @result{} foo
1067@end group
1068@group
1069(foo 1)
1070 @result{}1
1071@end group
1072@group
1073(fmakunbound 'foo)
1074 @result{} foo
1075@end group
1076@group
1077(foo 1)
1078@error{} Symbol's function definition is void: foo
1079@end group
1080@end example
1081@end defun
1082
1083@defun fset symbol definition
1084This function stores @var{definition} in the function cell of
1085@var{symbol}. The result is @var{definition}. Normally
1086@var{definition} should be a function or the name of a function, but
1087this is not checked. The argument @var{symbol} is an ordinary evaluated
1088argument.
1089
735cc5ca
CY
1090The primary use of this function is as a subroutine by constructs that
1091define or alter functions, like @code{defadvice} (@pxref{Advising
1092Functions}). (If @code{defun} were not a primitive, it could be
1093written as a Lisp macro using @code{fset}.) You can also use it to
1094give a symbol a function definition that is not a list, e.g.@: a
1095keyboard macro (@pxref{Keyboard Macros}):
b8d4c8d0 1096
735cc5ca
CY
1097@example
1098;; @r{Define a named keyboard macro.}
1099(fset 'kill-two-lines "\^u2\^k")
1100 @result{} "\^u2\^k"
1101@end example
b8d4c8d0 1102
735cc5ca
CY
1103It you wish to use @code{fset} to make an alternate name for a
1104function, consider using @code{defalias} instead. @xref{Definition of
1105defalias}.
1106@end defun
b8d4c8d0 1107
735cc5ca
CY
1108@node Closures
1109@section Closures
b8d4c8d0 1110
735cc5ca
CY
1111 As explained in @ref{Variable Scoping}, Emacs can optionally enable
1112lexical binding of variables. When lexical binding is enabled, any
1113named function that you create (e.g.@: with @code{defun}), as well as
1114any anonymous function that you create using the @code{lambda} macro
1115or the @code{function} special form or the @code{#'} syntax
1116(@pxref{Anonymous Functions}), is automatically converted into a
1117closure.
b8d4c8d0 1118
735cc5ca
CY
1119 A closure is a function that also carries a record of the lexical
1120environment that existed when the function was defined. When it is
1121invoked, any lexical variable references within its definition use the
1122retained lexical environment. In all other respects, closures behave
1123much like ordinary functions; in particular, they can be called in the
1124same way as ordinary functions.
b8d4c8d0 1125
735cc5ca 1126 @xref{Lexical Binding}, for an example of using a closure.
b8d4c8d0 1127
735cc5ca
CY
1128 Currently, an Emacs Lisp closure object is represented by a list
1129with the symbol @code{closure} as the first element, a list
1130representing the lexical environment as the second element, and the
1131argument list and body forms as the remaining elements:
b8d4c8d0 1132
735cc5ca
CY
1133@example
1134;; @r{lexical binding is enabled.}
1135(lambda (x) (* x x))
1136 @result{} (closure (t) (x) (* x x))
b8d4c8d0 1137@end example
b8d4c8d0 1138
735cc5ca
CY
1139@noindent
1140However, the fact that the internal structure of a closure is
1141``exposed'' to the rest of the Lisp world is considered an internal
1142implementation detail. For this reason, we recommend against directly
1143examining or altering the structure of closure objects.
b8d4c8d0
GM
1144
1145@node Obsolete Functions
1146@section Declaring Functions Obsolete
1147
1148You can use @code{make-obsolete} to declare a function obsolete. This
1149indicates that the function may be removed at some stage in the future.
1150
1151@defun make-obsolete obsolete-name current-name &optional when
1152This function makes the byte compiler warn that the function
1153@var{obsolete-name} is obsolete. If @var{current-name} is a symbol, the
1154warning message says to use @var{current-name} instead of
1155@var{obsolete-name}. @var{current-name} does not need to be an alias for
1156@var{obsolete-name}; it can be a different function with similar
1157functionality. If @var{current-name} is a string, it is the warning
1158message.
1159
1160If provided, @var{when} should be a string indicating when the function
1161was first made obsolete---for example, a date or a release number.
1162@end defun
1163
1164You can define a function as an alias and declare it obsolete at the
eb5ed549 1165same time using the macro @code{define-obsolete-function-alias}:
b8d4c8d0
GM
1166
1167@defmac define-obsolete-function-alias obsolete-name current-name &optional when docstring
1168This macro marks the function @var{obsolete-name} obsolete and also
1169defines it as an alias for the function @var{current-name}. It is
1170equivalent to the following:
1171
1172@example
1173(defalias @var{obsolete-name} @var{current-name} @var{docstring})
1174(make-obsolete @var{obsolete-name} @var{current-name} @var{when})
1175@end example
1176@end defmac
1177
eb5ed549
CY
1178In addition, you can mark a certain a particular calling convention
1179for a function as obsolete:
1180
1181@defun set-advertised-calling-convention function signature
1182This function specifies the argument list @var{signature} as the
1183correct way to call @var{function}. This causes the Emacs byte
1184compiler to issue a warning whenever it comes across an Emacs Lisp
1185program that calls @var{function} any other way (however, it will
1186still allow the code to be byte compiled).
1187
1188For instance, in old versions of Emacs the @code{sit-for} function
1189accepted three arguments, like this
1190
ddff3351 1191@example
eb5ed549 1192 (sit-for seconds milliseconds nodisp)
ddff3351 1193@end example
eb5ed549
CY
1194
1195However, calling @code{sit-for} this way is considered obsolete
1196(@pxref{Waiting}). The old calling convention is deprecated like
1197this:
1198
ddff3351 1199@example
eb5ed549
CY
1200(set-advertised-calling-convention
1201 'sit-for '(seconds &optional nodisp))
ddff3351 1202@end example
eb5ed549
CY
1203@end defun
1204
b8d4c8d0
GM
1205@node Inline Functions
1206@section Inline Functions
1207@cindex inline functions
1208
735cc5ca
CY
1209@defmac defsubst name argument-list body-forms...
1210Define an inline function. The syntax is exactly the same as
1211@code{defun} (@pxref{Defining Functions}).
1212@end defmac
1213
1214 You can define an @dfn{inline function} by using @code{defsubst}
1215instead of @code{defun}. An inline function works just like an
1216ordinary function except for one thing: when you byte-compile a call
1217to the function (@pxref{Byte Compilation}), the function's definition
1218is expanded into the caller.
b8d4c8d0 1219
735cc5ca
CY
1220 Making a function inline often makes its function calls run faster.
1221But it also has disadvantages. For one thing, it reduces flexibility;
1222if you change the definition of the function, calls already inlined
1223still use the old definition until you recompile them.
b8d4c8d0 1224
735cc5ca
CY
1225 Another disadvantage is that making a large function inline can
1226increase the size of compiled code both in files and in memory. Since
1227the speed advantage of inline functions is greatest for small
1228functions, you generally should not make large functions inline.
b8d4c8d0 1229
735cc5ca 1230 Also, inline functions do not behave well with respect to debugging,
b8d4c8d0
GM
1231tracing, and advising (@pxref{Advising Functions}). Since ease of
1232debugging and the flexibility of redefining functions are important
1233features of Emacs, you should not make a function inline, even if it's
1234small, unless its speed is really crucial, and you've timed the code
1235to verify that using @code{defun} actually has performance problems.
1236
735cc5ca
CY
1237 It's possible to define a macro to expand into the same code that an
1238inline function would execute (@pxref{Macros}). But the macro would
1239be limited to direct use in expressions---a macro cannot be called
1240with @code{apply}, @code{mapcar} and so on. Also, it takes some work
1241to convert an ordinary function into a macro. To convert it into an
1242inline function is easy; just replace @code{defun} with
1243@code{defsubst}. Since each argument of an inline function is
1244evaluated exactly once, you needn't worry about how many times the
1245body uses the arguments, as you do for macros.
b8d4c8d0 1246
735cc5ca
CY
1247 After an inline function is defined, its inline expansion can be
1248performed later on in the same file, just like macros.
b8d4c8d0 1249
e31dfb12
GM
1250@node Declaring Functions
1251@section Telling the Compiler that a Function is Defined
1252@cindex function declaration
1253@cindex declaring functions
c4540067 1254@findex declare-function
e31dfb12 1255
a0925923
RS
1256Byte-compiling a file often produces warnings about functions that the
1257compiler doesn't know about (@pxref{Compiler Errors}). Sometimes this
1258indicates a real problem, but usually the functions in question are
1259defined in other files which would be loaded if that code is run. For
1260example, byte-compiling @file{fortran.el} used to warn:
e31dfb12 1261
ddff3351 1262@example
e31dfb12 1263In end of data:
bfd0fe7b
GM
1264fortran.el:2152:1:Warning: the function `gud-find-c-expr' is not known
1265 to be defined.
ddff3351 1266@end example
e31dfb12 1267
a0925923
RS
1268In fact, @code{gud-find-c-expr} is only used in the function that
1269Fortran mode uses for the local value of
1270@code{gud-find-expr-function}, which is a callback from GUD; if it is
1271called, the GUD functions will be loaded. When you know that such a
1272warning does not indicate a real problem, it is good to suppress the
1273warning. That makes new warnings which might mean real problems more
1274visible. You do that with @code{declare-function}.
e31dfb12
GM
1275
1276All you need to do is add a @code{declare-function} statement before the
1277first use of the function in question:
1278
ddff3351 1279@example
e31dfb12 1280(declare-function gud-find-c-expr "gud.el" nil)
ddff3351 1281@end example
e31dfb12
GM
1282
1283This says that @code{gud-find-c-expr} is defined in @file{gud.el} (the
a0925923
RS
1284@samp{.el} can be omitted). The compiler takes for granted that that file
1285really defines the function, and does not check.
7a6a1728 1286
a0925923
RS
1287 The optional third argument specifies the argument list of
1288@code{gud-find-c-expr}. In this case, it takes no arguments
1289(@code{nil} is different from not specifying a value). In other
1290cases, this might be something like @code{(file &optional overwrite)}.
1291You don't have to specify the argument list, but if you do the
1292byte compiler can check that the calls match the declaration.
1293
8f4b37d8 1294@defmac declare-function function file &optional arglist fileonly
a0925923 1295Tell the byte compiler to assume that @var{function} is defined, with
b0fbc500
CY
1296arguments @var{arglist}, and that the definition should come from the
1297file @var{file}. @var{fileonly} non-@code{nil} means only check that
8f4b37d8 1298@var{file} exists, not that it actually defines @var{function}.
a0925923
RS
1299@end defmac
1300
1301 To verify that these functions really are declared where
1302@code{declare-function} says they are, use @code{check-declare-file}
1303to check all @code{declare-function} calls in one source file, or use
1304@code{check-declare-directory} check all the files in and under a
1305certain directory.
1306
1307 These commands find the file that ought to contain a function's
1308definition using @code{locate-library}; if that finds no file, they
1309expand the definition file name relative to the directory of the file
1310that contains the @code{declare-function} call.
1311
735cc5ca
CY
1312 You can also say that a function is a primitive by specifying a file
1313name ending in @samp{.c} or @samp{.m}. This is useful only when you
1314call a primitive that is defined only on certain systems. Most
1315primitives are always defined, so they will never give you a warning.
e31dfb12 1316
c4540067
GM
1317 Sometimes a file will optionally use functions from an external package.
1318If you prefix the filename in the @code{declare-function} statement with
1319@samp{ext:}, then it will be checked if it is found, otherwise skipped
1320without error.
1321
8f4b37d8
GM
1322 There are some function definitions that @samp{check-declare} does not
1323understand (e.g. @code{defstruct} and some other macros). In such cases,
6297397b
GM
1324you can pass a non-@code{nil} @var{fileonly} argument to
1325@code{declare-function}, meaning to only check that the file exists, not
1326that it actually defines the function. Note that to do this without
1327having to specify an argument list, you should set the @var{arglist}
1328argument to @code{t} (because @code{nil} means an empty argument list, as
1329opposed to an unspecified one).
8f4b37d8 1330
b8d4c8d0
GM
1331@node Function Safety
1332@section Determining whether a Function is Safe to Call
1333@cindex function safety
1334@cindex safety of functions
1335
1336Some major modes such as SES call functions that are stored in user
1337files. (@inforef{Top, ,ses}, for more information on SES.) User
1338files sometimes have poor pedigrees---you can get a spreadsheet from
1339someone you've just met, or you can get one through email from someone
1340you've never met. So it is risky to call a function whose source code
1341is stored in a user file until you have determined that it is safe.
1342
1343@defun unsafep form &optional unsafep-vars
1344Returns @code{nil} if @var{form} is a @dfn{safe} Lisp expression, or
1345returns a list that describes why it might be unsafe. The argument
1346@var{unsafep-vars} is a list of symbols known to have temporary
1347bindings at this point; it is mainly used for internal recursive
1348calls. The current buffer is an implicit argument, which provides a
1349list of buffer-local bindings.
1350@end defun
1351
1352Being quick and simple, @code{unsafep} does a very light analysis and
1353rejects many Lisp expressions that are actually safe. There are no
1354known cases where @code{unsafep} returns @code{nil} for an unsafe
1355expression. However, a ``safe'' Lisp expression can return a string
1356with a @code{display} property, containing an associated Lisp
1357expression to be executed after the string is inserted into a buffer.
1358This associated expression can be a virus. In order to be safe, you
1359must delete properties from all strings calculated by user code before
1360inserting them into buffers.
1361
1362@ignore
1363What is a safe Lisp expression? Basically, it's an expression that
1364calls only built-in functions with no side effects (or only innocuous
1365ones). Innocuous side effects include displaying messages and
1366altering non-risky buffer-local variables (but not global variables).
1367
1368@table @dfn
1369@item Safe expression
1370@itemize
1371@item
1372An atom or quoted thing.
1373@item
1374A call to a safe function (see below), if all its arguments are
1375safe expressions.
1376@item
1377One of the special forms @code{and}, @code{catch}, @code{cond},
1378@code{if}, @code{or}, @code{prog1}, @code{prog2}, @code{progn},
1379@code{while}, and @code{unwind-protect}], if all its arguments are
1380safe.
1381@item
1382A form that creates temporary bindings (@code{condition-case},
1383@code{dolist}, @code{dotimes}, @code{lambda}, @code{let}, or
1384@code{let*}), if all args are safe and the symbols to be bound are not
1385explicitly risky (see @pxref{File Local Variables}).
1386@item
1387An assignment using @code{add-to-list}, @code{setq}, @code{push}, or
1388@code{pop}, if all args are safe and the symbols to be assigned are
1389not explicitly risky and they already have temporary or buffer-local
1390bindings.
1391@item
1392One of [apply, mapc, mapcar, mapconcat] if the first argument is a
1393safe explicit lambda and the other args are safe expressions.
1394@end itemize
1395
1396@item Safe function
1397@itemize
1398@item
1399A lambda containing safe expressions.
1400@item
1401A symbol on the list @code{safe-functions}, so the user says it's safe.
1402@item
1403A symbol with a non-@code{nil} @code{side-effect-free} property.
1404@item
27610f35
RS
1405A symbol with a non-@code{nil} @code{safe-function} property. The
1406value @code{t} indicates a function that is safe but has innocuous
1407side effects. Other values will someday indicate functions with
1408classes of side effects that are not always safe.
b8d4c8d0
GM
1409@end itemize
1410
1411The @code{side-effect-free} and @code{safe-function} properties are
1412provided for built-in functions and for low-level functions and macros
1413defined in @file{subr.el}. You can assign these properties for the
1414functions you write.
1415@end table
1416@end ignore
1417
1418@node Related Topics
1419@section Other Topics Related to Functions
1420
1421 Here is a table of several functions that do things related to
1422function calling and function definitions. They are documented
1423elsewhere, but we provide cross references here.
1424
1425@table @code
1426@item apply
1427See @ref{Calling Functions}.
1428
1429@item autoload
1430See @ref{Autoload}.
1431
1432@item call-interactively
1433See @ref{Interactive Call}.
1434
39dc0d57
RS
1435@item called-interactively-p
1436See @ref{Distinguish Interactive}.
1437
b8d4c8d0
GM
1438@item commandp
1439See @ref{Interactive Call}.
1440
1441@item documentation
1442See @ref{Accessing Documentation}.
1443
1444@item eval
1445See @ref{Eval}.
1446
1447@item funcall
1448See @ref{Calling Functions}.
1449
1450@item function
1451See @ref{Anonymous Functions}.
1452
1453@item ignore
1454See @ref{Calling Functions}.
1455
1456@item indirect-function
1457See @ref{Function Indirection}.
1458
1459@item interactive
1460See @ref{Using Interactive}.
1461
1462@item interactive-p
39dc0d57 1463See @ref{Distinguish Interactive}.
b8d4c8d0
GM
1464
1465@item mapatoms
1466See @ref{Creating Symbols}.
1467
1468@item mapcar
1469See @ref{Mapping Functions}.
1470
1471@item map-char-table
1472See @ref{Char-Tables}.
1473
1474@item mapconcat
1475See @ref{Mapping Functions}.
1476
1477@item undefined
1478See @ref{Functions for Key Lookup}.
1479@end table