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