diff options
| author | Paul Eggert | 2016-05-27 09:46:44 -0700 |
|---|---|---|
| committer | Paul Eggert | 2016-05-27 09:47:19 -0700 |
| commit | b342815c0a9af8d94d4290d17882de73f6fd9373 (patch) | |
| tree | 6dccd2ee6891d29a3f134f542a94b9f1317fe488 | |
| parent | 09b72fc38aee1032b6d2c8476bcc22a2e7ec5642 (diff) | |
| download | emacs-b342815c0a9af8d94d4290d17882de73f6fd9373.tar.gz emacs-b342815c0a9af8d94d4290d17882de73f6fd9373.zip | |
Improve define-function omitted-arg documentation
* doc/lispref/functions.texi (Declaring Functions):
* lisp/subr.el (declare-function):
Be clearer when documenting omitted args for define-function.
| -rw-r--r-- | doc/lispref/functions.texi | 41 | ||||
| -rw-r--r-- | lisp/subr.el | 18 |
2 files changed, 31 insertions, 28 deletions
diff --git a/doc/lispref/functions.texi b/doc/lispref/functions.texi index ff21abba61e..7513adfbbef 100644 --- a/doc/lispref/functions.texi +++ b/doc/lispref/functions.texi | |||
| @@ -2172,44 +2172,49 @@ Byte-compiling a file often produces warnings about functions that the | |||
| 2172 | compiler doesn't know about (@pxref{Compiler Errors}). Sometimes this | 2172 | compiler doesn't know about (@pxref{Compiler Errors}). Sometimes this |
| 2173 | indicates a real problem, but usually the functions in question are | 2173 | indicates a real problem, but usually the functions in question are |
| 2174 | defined in other files which would be loaded if that code is run. For | 2174 | defined in other files which would be loaded if that code is run. For |
| 2175 | example, byte-compiling @file{fortran.el} used to warn: | 2175 | example, byte-compiling @file{simple.el} used to warn: |
| 2176 | 2176 | ||
| 2177 | @example | 2177 | @example |
| 2178 | In end of data: | 2178 | simple.el:8727:1:Warning: the function ‘shell-mode’ is not known to be |
| 2179 | fortran.el:2152:1:Warning: the function ‘gud-find-c-expr’ is not | 2179 | defined. |
| 2180 | known to be defined. | ||
| 2181 | @end example | 2180 | @end example |
| 2182 | 2181 | ||
| 2183 | In fact, @code{gud-find-c-expr} is only used in the function that | 2182 | In fact, @code{shell-mode} is used only in a function that executes |
| 2184 | Fortran mode uses for the local value of | 2183 | @code{(require 'shell)} before calling @code{shell-mode}, so |
| 2185 | @code{gud-find-expr-function}, which is a callback from GUD; if it is | 2184 | @code{shell-mode} will be defined properly at run-time. When you know |
| 2186 | called, the GUD functions will be loaded. When you know that such a | 2185 | that such a warning does not indicate a real problem, it is good to |
| 2187 | warning does not indicate a real problem, it is good to suppress the | 2186 | suppress the warning. That makes new warnings which might mean real |
| 2188 | warning. That makes new warnings which might mean real problems more | 2187 | problems more visible. You do that with @code{declare-function}. |
| 2189 | visible. You do that with @code{declare-function}. | ||
| 2190 | 2188 | ||
| 2191 | All you need to do is add a @code{declare-function} statement before the | 2189 | All you need to do is add a @code{declare-function} statement before the |
| 2192 | first use of the function in question: | 2190 | first use of the function in question: |
| 2193 | 2191 | ||
| 2194 | @example | 2192 | @example |
| 2195 | (declare-function gud-find-c-expr "gud.el" nil) | 2193 | (declare-function shell-mode "shell" ()) |
| 2196 | @end example | 2194 | @end example |
| 2197 | 2195 | ||
| 2198 | This says that @code{gud-find-c-expr} is defined in @file{gud.el} (the | 2196 | This says that @code{shell-mode} is defined in @file{shell.el} (the |
| 2199 | @samp{.el} can be omitted). The compiler takes for granted that that file | 2197 | @samp{.el} can be omitted). The compiler takes for granted that that file |
| 2200 | really defines the function, and does not check. | 2198 | really defines the function, and does not check. |
| 2201 | 2199 | ||
| 2202 | The optional third argument specifies the argument list of | 2200 | The optional third argument specifies the argument list of |
| 2203 | @code{gud-find-c-expr}. In this case, it takes no arguments | 2201 | @code{shell-mode}. In this case, it takes no arguments |
| 2204 | (@code{nil} is different from not specifying a value). In other | 2202 | (@code{nil} is different from not specifying a value). In other |
| 2205 | cases, this might be something like @code{(file &optional overwrite)}. | 2203 | cases, this might be something like @code{(file &optional overwrite)}. |
| 2206 | You don't have to specify the argument list, but if you do the | 2204 | You don't have to specify the argument list, but if you do the |
| 2207 | byte compiler can check that the calls match the declaration. | 2205 | byte compiler can check that the calls match the declaration. |
| 2208 | 2206 | ||
| 2209 | @defmac declare-function function file &optional arglist fileonly | 2207 | @defmac declare-function function file &rest args |
| 2210 | Tell the byte compiler to assume that @var{function} is defined, with | 2208 | Tell the byte compiler to assume that @var{function} is defined in the |
| 2211 | arguments @var{arglist}, and that the definition should come from the | 2209 | file @var{file}. The trailing arguments @var{args} can contain one or |
| 2212 | file @var{file}. @var{fileonly} non-@code{nil} means only check that | 2210 | two optional arguments. The first optional argument @var{arglist} is |
| 2211 | either @code{t}, meaning the argument list is unspecified, or a list | ||
| 2212 | of formal parameters in the same style as @code{defun}.@footnote{An | ||
| 2213 | omitted @var{arglist} defaults to @code{t}, not @code{nil}; this | ||
| 2214 | atypical behavior is why @code{declare-function} is defined to have | ||
| 2215 | the formal parameters @code{(function file &rest args)}, not | ||
| 2216 | @code{(function file &optional arglist fileonly)}.} The second | ||
| 2217 | optional argument @var{fileonly} non-@code{nil} means only check that | ||
| 2213 | @var{file} exists, not that it actually defines @var{function}. | 2218 | @var{file} exists, not that it actually defines @var{function}. |
| 2214 | @end defmac | 2219 | @end defmac |
| 2215 | 2220 | ||
diff --git a/lisp/subr.el b/lisp/subr.el index 44d7eaccf02..239bdc08d6b 100644 --- a/lisp/subr.el +++ b/lisp/subr.el | |||
| @@ -31,7 +31,6 @@ | |||
| 31 | 31 | ||
| 32 | (defmacro declare-function (_fn _file &rest _args) | 32 | (defmacro declare-function (_fn _file &rest _args) |
| 33 | "Tell the byte-compiler that function FN is defined, in FILE. | 33 | "Tell the byte-compiler that function FN is defined, in FILE. |
| 34 | Optional ARGLIST is the argument list used by the function. | ||
| 35 | The FILE argument is not used by the byte-compiler, but by the | 34 | The FILE argument is not used by the byte-compiler, but by the |
| 36 | `check-declare' package, which checks that FILE contains a | 35 | `check-declare' package, which checks that FILE contains a |
| 37 | definition for FN. Remaining ARGS are used by both the | 36 | definition for FN. Remaining ARGS are used by both the |
| @@ -47,15 +46,14 @@ declaration. A FILE with an \"ext:\" prefix is an external file. | |||
| 47 | them without error if they are not. | 46 | them without error if they are not. |
| 48 | 47 | ||
| 49 | ARGS can contain one or two optional args. First optional arg | 48 | ARGS can contain one or two optional args. First optional arg |
| 50 | ARGLIST specifies the function arguments. Second optional arg | 49 | ARGLIST specifies FN's arguments, or is t to not specify FN's |
| 51 | FILEONLY non-nil means that `check-declare' will only check that | 50 | arguments. An omitted ARGLIST defaults to t, not nil: a nil |
| 52 | FILE exists, not that it defines FN. This is intended for | 51 | ARGLIST specifies an empty argument list, and an explicit t |
| 53 | function-definitions that `check-declare' does not recognize, e.g. | 52 | ARGLIST is a placeholder that allows supplying a later arg. |
| 54 | `defstruct'. | 53 | Second optional arg FILEONLY non-nil means that `check-declare' |
| 55 | 54 | will check only that FILE exists, not that it defines FN. This | |
| 56 | To specify a value for FILEONLY without passing an argument list, | 55 | is intended for function definitions that `check-declare' does |
| 57 | set ARGLIST to t. This is necessary because nil means an | 56 | not recognize, e.g., `defstruct'. |
| 58 | empty argument list, rather than an unspecified one. | ||
| 59 | 57 | ||
| 60 | Note that for the purposes of `check-declare', this statement | 58 | Note that for the purposes of `check-declare', this statement |
| 61 | must be the first non-whitespace on a line. | 59 | must be the first non-whitespace on a line. |