From: Stefan Monnier Date: Tue, 18 Oct 2022 20:06:42 +0000 (-0400) Subject: functions.texi: Fix bug#58602 X-Git-Tag: emacs-29.0.90~1616^2~546 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=254f4766e6ef5370f75530752db5640895b9066a;p=emacs.git functions.texi: Fix bug#58602 * doc/lispref/functions.texi (Function Documentation): Document `:documentation` and `function-documentation`. --- diff --git a/doc/lispref/functions.texi b/doc/lispref/functions.texi index 8b858e0aa01..e1102832529 100644 --- a/doc/lispref/functions.texi +++ b/doc/lispref/functions.texi @@ -533,6 +533,44 @@ Instead, use the @code{advertised-calling-convention} declaration compiler emit a warning message when it compiles Lisp programs which use the deprecated calling convention. +@cindex computed documentation string +@kindex{:documentation} +Documentation strings are usually static, but occasionally it can be +necessary to generate them dynamically. In some cases you can do so +by writing a macro which generates at compile time the code of the +function, including the desired documentation string. But you can +also generate the docstring dynamically by writing +@code{(:documentation @var{form})} instead of the documentation +string. This will evaluate @var{form} at run-time when the function +is defined and use it as the documentation string@footnote{This only +works in code using @code{lexical-binding}.}. You can also compute +the documentation string on the fly when it is requested, by setting +the @code{function-documentation} property of the function's symbol to +a Lisp form that evaluates to a string. + +For example: +@example +@group +(defun adder (x) + (lambda (y) + (:documentation (format "Add %S to the argument Y." x)) + (+ x y))) +(defalias 'adder5 (adder 5)) +(documentation 'adder5) + @result{} "Add 5 to the argument Y." +@end group + +@group +(put 'adder5 'function-documentation + '(concat (documentation (symbol-function 'adder5) 'raw) + " Consulted at " (format-time-string "%H:%M:%S"))) +(documentation 'adder5) + @result{} "Add 5 to the argument Y. Consulted at 15:52:13" +(documentation 'adder5) + @result{} "Add 5 to the argument Y. Consulted at 15:52:18" +@end group +@end example + @node Function Names @section Naming a Function @cindex function definition