[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#58602: 29.0.50; Please document (:documentation FORM) spec for closu
From: |
Stefan Monnier |
Subject: |
bug#58602: 29.0.50; Please document (:documentation FORM) spec for closures |
Date: |
Tue, 18 Oct 2022 16:13:25 -0400 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux) |
I pushed the text below.
Thanks to Eli and Drew for their review.
Stefan
PS: BTW, I noticed that `i :documentation RET` doesn't bring me to this
text, instead it just looks for "documentation" (which matches many
more index entries), disregarding the `:` and making this `@kindex` much
less useful.
PPS: Every time you use this `function-documentation` property, kitten
suffer atrocious pains.
diff --git a/doc/lispref/functions.texi b/doc/lispref/functions.texi
index 8b858e0aa01..94c42ba260d 100644
--- a/doc/lispref/functions.texi
+++ b/doc/lispref/functions.texi
@@ -533,6 +533,46 @@ Function Documentation
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
- bug#58602: 29.0.50; Please document (:documentation FORM) spec for closures, Michael Heerdegen, 2022/10/17
- bug#58602: 29.0.50; Please document (:documentation FORM) spec for closures, Stefan Monnier, 2022/10/17
- bug#58602: 29.0.50; Please document (:documentation FORM) spec for closures, Christopher Dimech, 2022/10/17
- bug#58602: 29.0.50; Please document (:documentation FORM) spec for closures, Michael Heerdegen, 2022/10/17
- bug#58602: 29.0.50; Please document (:documentation FORM) spec for closures, Stefan Monnier, 2022/10/18
- bug#58602: 29.0.50; Please document (:documentation FORM) spec for closures,
Stefan Monnier <=
- bug#58602: 29.0.50; Please document (:documentation FORM) spec for closures, Drew Adams, 2022/10/18
- bug#58602: 29.0.50; Please document (:documentation FORM) spec for closures, Michael Heerdegen, 2022/10/18
- bug#58602: 29.0.50; Please document (:documentation FORM) spec for closures, Michael Heerdegen, 2022/10/19
- bug#58602: 29.0.50; Please document (:documentation FORM) spec for closures, Michael Albinus, 2022/10/20
- bug#58602: 29.0.50; Please document (:documentation FORM) spec for closures, Drew Adams, 2022/10/20