1 |
@c -*-texinfo-*- |
@c -*-texinfo-*- |
2 |
@c This is part of the GNU Emacs Lisp Reference Manual. |
@c This is part of the GNU Emacs Lisp Reference Manual. |
3 |
@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999 |
@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999 |
4 |
@c Free Software Foundation, Inc. |
@c Free Software Foundation, Inc. |
5 |
@c See the file elisp.texi for copying conditions. |
@c See the file elisp.texi for copying conditions. |
6 |
@setfilename ../info/functions |
@setfilename ../info/functions |
7 |
@node Functions, Macros, Variables, Top |
@node Functions, Macros, Variables, Top |
18 |
* Defining Functions:: Lisp expressions for defining functions. |
* Defining Functions:: Lisp expressions for defining functions. |
19 |
* Calling Functions:: How to use an existing function. |
* Calling Functions:: How to use an existing function. |
20 |
* Mapping Functions:: Applying a function to each element of a list, etc. |
* Mapping Functions:: Applying a function to each element of a list, etc. |
21 |
* Anonymous Functions:: Lambda expressions are functions with no names. |
* Anonymous Functions:: Lambda expressions are functions with no names. |
22 |
* Function Cells:: Accessing or setting the function definition |
* Function Cells:: Accessing or setting the function definition |
23 |
of a symbol. |
of a symbol. |
24 |
* Inline Functions:: Defining functions that the compiler will open code. |
* Inline Functions:: Defining functions that the compiler will open code. |
25 |
|
* Function safety:: Determining whether a function is safe to call. |
26 |
* Related Topics:: Cross-references to specific Lisp primitives |
* Related Topics:: Cross-references to specific Lisp primitives |
27 |
that have a special bearing on how functions work. |
that have a special bearing on how functions work. |
28 |
@end menu |
@end menu |
746 |
"Apply FUNCTION to successive cars of all ARGS. |
"Apply FUNCTION to successive cars of all ARGS. |
747 |
Return the list of results." |
Return the list of results." |
748 |
;; @r{If no list is exhausted,} |
;; @r{If no list is exhausted,} |
749 |
(if (not (memq 'nil args)) |
(if (not (memq 'nil args)) |
750 |
;; @r{apply function to @sc{car}s.} |
;; @r{apply function to @sc{car}s.} |
751 |
(cons (apply function (mapcar 'car args)) |
(cons (apply function (mapcar 'car args)) |
752 |
(apply 'mapcar* function |
(apply 'mapcar* function |
753 |
;; @r{Recurse for rest of elements.} |
;; @r{Recurse for rest of elements.} |
754 |
(mapcar 'cdr args))))) |
(mapcar 'cdr args))))) |
755 |
@end group |
@end group |
779 |
argument and return a string. The argument @var{sequence} can be any |
argument and return a string. The argument @var{sequence} can be any |
780 |
kind of sequence except a char-table; that is, a list, a vector, a |
kind of sequence except a char-table; that is, a list, a vector, a |
781 |
bool-vector, or a string. |
bool-vector, or a string. |
782 |
|
|
783 |
@smallexample |
@smallexample |
784 |
@group |
@group |
785 |
(mapconcat 'symbol-name |
(mapconcat 'symbol-name |
841 |
the function @code{mapcar}, which applies any given function to each |
the function @code{mapcar}, which applies any given function to each |
842 |
element of a list. |
element of a list. |
843 |
|
|
844 |
Here we define a function @code{change-property} which |
Here we define a function @code{change-property} which |
845 |
uses a function as its third argument: |
uses a function as its third argument: |
846 |
|
|
847 |
@example |
@example |
915 |
|
|
916 |
@cindex @samp{#'} syntax |
@cindex @samp{#'} syntax |
917 |
The read syntax @code{#'} is a short-hand for using @code{function}. |
The read syntax @code{#'} is a short-hand for using @code{function}. |
918 |
For example, |
For example, |
919 |
|
|
920 |
@example |
@example |
921 |
#'(lambda (x) (* x x)) |
#'(lambda (x) (* x x)) |
1158 |
Inline functions can be used and open-coded later on in the same file, |
Inline functions can be used and open-coded later on in the same file, |
1159 |
following the definition, just like macros. |
following the definition, just like macros. |
1160 |
|
|
1161 |
|
@node Function safety |
1162 |
|
@section Determining whether a function is safe to call |
1163 |
|
@cindex function safety |
1164 |
|
@cindex safety of functions |
1165 |
|
@cindex virus detection |
1166 |
|
@cindex Trojan-horse detection |
1167 |
|
@cindex DDoS attacks |
1168 |
|
|
1169 |
|
Some major modes such as SES (see @pxref{Top,,,ses}) will call |
1170 |
|
functions that are stored in user files. User files sometimes have |
1171 |
|
poor pedigrees---you can get a spreadsheet from someone you've just |
1172 |
|
met, or you can get one through email from someone you've never met. |
1173 |
|
Such files can contain viruses and other Trojan horses that could |
1174 |
|
corrupt your operating system environment, delete your files, or even |
1175 |
|
turn your computer into a DDoS zombie! To avoid this terrible fate, |
1176 |
|
you should not call a function whose source code is stored in a user |
1177 |
|
file until you have determined that it is safe. |
1178 |
|
|
1179 |
|
@defun unsafep form &optional unsafep-vars |
1180 |
|
Returns nil if @var{form} is a @dfn{safe} lisp expression, or returns |
1181 |
|
a list that describes why it might be unsafe. The argument |
1182 |
|
@var{unsafep-vars} is a list of symbols known to have temporary |
1183 |
|
bindings at this point; it is mainly used for internal recursive |
1184 |
|
calls. The current buffer is an implicit argument, which provides a |
1185 |
|
list of buffer-local bindings. |
1186 |
|
@end defun |
1187 |
|
|
1188 |
|
Being quick and simple, @code{unsafep} does a very light analysis and |
1189 |
|
rejects many Lisp expressions that are actually safe. There are no |
1190 |
|
known cases where @code{unsafep} returns nil for an unsafe expression. |
1191 |
|
However, a ``safe'' Lisp expression can return a string with a |
1192 |
|
@code{display} property, containing an associated Lisp expression to |
1193 |
|
be executed after the string is inserted into a buffer. This |
1194 |
|
associated expression can be a virus. In order to be safe, you must |
1195 |
|
delete properties from all strings calculated by user code before |
1196 |
|
inserting them into buffers. |
1197 |
|
|
1198 |
|
What is a safe Lisp expression? Basically, it's an expression that |
1199 |
|
calls only built-in functions with no side effects (or only innocuous |
1200 |
|
ones). Innocuous side effects include displaying messages and |
1201 |
|
altering non-risky buffer-local variables (but not global variables). |
1202 |
|
|
1203 |
|
@table @dfn |
1204 |
|
@item Safe expression |
1205 |
|
@itemize |
1206 |
|
@item |
1207 |
|
An atom or quoted thing. |
1208 |
|
@item |
1209 |
|
A call to a safe function (see below), if all its arguments are |
1210 |
|
safe expressions. |
1211 |
|
@item |
1212 |
|
One of the special forms [and, catch, cond, if, or, prog1, prog2, |
1213 |
|
progn, while, unwind-protect], if all its arguments are safe. |
1214 |
|
@item |
1215 |
|
A form that creates temporary bindings [condition-case, dolist, |
1216 |
|
dotimes, lambda, let, let*], if all args are safe and the symbols to |
1217 |
|
be bound are not explicitly risky (see @pxref{File Local Variables}). |
1218 |
|
@item |
1219 |
|
An assignment [add-to-list, setq, push, pop], if all args are safe and |
1220 |
|
the symbols to be assigned are not explicitly risky and they already |
1221 |
|
have temporary or buffer-local bindings. |
1222 |
|
@item |
1223 |
|
One of [apply, mapc, mapcar, mapconcat] if the first argument is a |
1224 |
|
safe explicit lambda and the other args are safe expressions. |
1225 |
|
@end itemize |
1226 |
|
|
1227 |
|
@item Safe function |
1228 |
|
@itemize |
1229 |
|
@item |
1230 |
|
A lambda containing safe expressions. |
1231 |
|
@item |
1232 |
|
A symbol on the list @code{safe-functions}, so the user says it's safe. |
1233 |
|
@item |
1234 |
|
A symbol with a non-nil @code{side-effect-free} property. |
1235 |
|
@item |
1236 |
|
A symbol with a non-nil @code{safe-function} property. Value t |
1237 |
|
indicates a function that is safe but has innocuous side effects. |
1238 |
|
Other values will someday indicate functions with classes of side |
1239 |
|
effects that are not always safe. |
1240 |
|
@end itemize |
1241 |
|
|
1242 |
|
The @code{side-effect-free} and @code{safe-function} properties are |
1243 |
|
provided for built-in functions and for low-level functions and macros |
1244 |
|
defined in @file{subr.el}. You can assign these properties for the |
1245 |
|
functions you write. |
1246 |
|
|
1247 |
|
@end table |
1248 |
|
|
1249 |
|
|
1250 |
@c Emacs versions prior to 19 did not have inline functions. |
@c Emacs versions prior to 19 did not have inline functions. |
1251 |
|
|
1252 |
@node Related Topics |
@node Related Topics |