[PATCH] Add use cases of (fn) documentation facility.

  • Done
  • quality assurance status badge
Details
2 participants
  • Eli Zaretskii
  • Jeremy Bryant
Owner
unassigned
Submitted by
Jeremy Bryant
Severity
normal
J
J
Jeremy Bryant wrote on 28 Nov 2023 00:30
(address . bug-gnu-emacs@gnu.org)
87il5mai0x.fsf@jeremybryant.net
Tags: patch


I have written a proposed addition to the Elisp manual. In now-closed
bug 66928, there was a discussion related to the use of \(fn) in doc
strings, and I have drafted some examples to explain how this facility
could be used. The addition is presented as @ifnottex... in order to
reduce the cost of the printed manual.

Feedback welcome on draft before I refine further, on conventions, section of
manual, style etc.





Toggle quote (34 lines)
> Interesting, thank you
> Perhaps it would be of interest to add some text to the elisp manual in
> (elisp) Function Documentation

> Are there any conventions for manual additions?
> in particular to keep the number of printed pages limited is there a way
> to say 'don't print this node'?

> thanks
> Jeremy


> Stefan Monnier <monnier@iro.umontreal.ca> writes:

> >> One question, the elisp manual mentions that the \(fn ARGLIST) facility
> >> is particularly useful for macros.
> >>
> >> Why would we use this for defuns?
> >
> > We use it for some defuns where ELisp's arglist functionality is not
> > refined enough to express the intention of the programmer.
> > For example, the "real" arglist may be
> >
> > name args &optional docstring &rest body
> >
> > but the intended arglist is
> >
> > name args [docstring] &rest body
> >
> > i.e. if `docstring` is not a string it's taken as the first instruction
> > of `body`.
> >
> >
> > Stefan
From 34d0e7b4a6f15769d7b32dc16e3f283f947069ab Mon Sep 17 00:00:00 2001
From: Jeremy Bryant <jb@jeremybryant.net>
Date: Mon, 27 Nov 2023 23:18:03 +0000
Subject: [PATCH] Add use cases of (fn) documentation facility.

* doc/lispref/functions.texi (Function Documentation): Add examples
---
doc/lispref/functions.texi | 93 ++++++++++++++++++++++++++++++++++++++
1 file changed, 93 insertions(+)

Toggle diff (106 lines)
diff --git a/doc/lispref/functions.texi b/doc/lispref/functions.texi
index e646e7c8b0a..92972d070ee 100644
--- a/doc/lispref/functions.texi
+++ b/doc/lispref/functions.texi
@@ -533,6 +533,99 @@ Function Documentation
compiler emit a warning message when it compiles Lisp programs which
use the deprecated calling convention.
+
+@c We use it for some defuns where ELisp's arglist functionality is not
+@c refined enough to express the intention of the programmer.
+@c For example, the "real" arglist may be
+
+@c name args &optional docstring &rest body
+
+@c but the intended arglist is
+
+@c name args [docstring] &rest body
+
+@c i.e. if `docstring` is not a string it's taken as the first instruction
+@c of `body`.
+
+@ifnottex
+In this section we outline examples of the use of the (fn) facility.
+
+Example with defmacro:
+Here is a canonical example where arguments are unclear.
+In subr.el, the definition of lambda is as below, and the (fn) facility
+helps to spell out the arglist &rest.
+@example
+(defmacro lambda (&rest cdr)
+ "Return an anonymous function.
+\(fn ARGS [DOCSTRING] [INTERACTIVE] BODY)"
+ )
+@end example
+
+Similar example with defun:
+define-keymap in keymap.el
+The &rest definitions list is not clear, so the fn facility explains it.
+@example
+(defun define-keymap (&rest definitions)
+ "Create a new keymap and define KEY/DEFINITION pairs as key bindings.
+Return the new keymap.
+...
+\(fn &key FULL PARENT SUPPRESS NAME PREFIX KEYMAP &rest [KEY DEFINITION]...)"
+@end example
+
+
+Example with defmacro of a required argument which gets renamed:
+In macroexp.el, the macro @code{macroexp--accumulate}, has a first
+argument of @code{var+list}, which is more clearly described to the
+user as @code{(var list)}. Thus the arguments are clarified.
+See example below:
+@example
+(defmacro macroexp--accumulate (var+list &rest body)
+ "Return a list of the results of evaluating BODY for each element of LIST.
+Evaluate BODY with VAR bound to each `car' from LIST, in turn.
+Return a list of the values of the final form in BODY.
+The list structure of the result will share as much with LIST as
+possible (for instance, when BODY just returns VAR unchanged, the
+result will be eq to LIST).
+
+\(fn (VAR LIST) BODY...)"
+ (declare (indent 1))
+ (let ((var (car var+list))
+ (list (cadr var+list))
+...)))
+@end example
+
+Example with defalias:
+Example of abbrev-get in abbrev.el:
+In this example, the general get facility to get the value of a
+symbol's property, is better described as getting the relevant
+abbrev's property value.
+@example
+(defalias 'abbrev-get 'get
+ "Get the property PROP of abbrev ABBREV
+See `define-abbrev' for the effect of some special properties.
+
+\(fn ABBREV PROP)")
+@end example
+
+Similar example of cl--defalias in cl-lib.el:
+In this example, the arguments are the same, but for list they are called
+OBJECTS, and for cl-values they are called VALUES.
+
+@example
+(cl--defalias 'cl-values #'list
+ "Return multiple values, Common Lisp style.
+The arguments of `cl-values' are the values
+that the containing function should return.
+
+\(fn &rest VALUES)")
+@end example
+
+
+% See (elisp) Autoload for a mention of \fn to reference.
+
+@end ifnottex
+
+
@cindex computed documentation string
@kindex :documentation
Documentation strings are usually static, but occasionally it can be
--
2.42.0
E
E
Eli Zaretskii wrote on 29 Nov 2023 15:42
(name . Jeremy Bryant)(address . jb@jeremybryant.net)(address . 67499@debbugs.gnu.org)
834jh47h99.fsf@gnu.org
Toggle quote (13 lines)
> Date: Mon, 27 Nov 2023 23:30:33 +0000
> From: Jeremy Bryant via "Bug reports for GNU Emacs,
> the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
>
> I have written a proposed addition to the Elisp manual. In now-closed
> bug 66928, there was a discussion related to the use of \(fn) in doc
> strings, and I have drafted some examples to explain how this facility
> could be used. The addition is presented as @ifnottex... in order to
> reduce the cost of the printed manual.
>
> Feedback welcome on draft before I refine further, on conventions, section of
> manual, style etc.

Thanks.

I wonder whether we need this to be said in so many words. Can't we
instead just enumerate the uses, describing each one in a couple of
sentences, and format that as, say, an @itemize'd list? IOW, do we
really need to show an explicit example for each use? Examples are
useful when an example is worth a thousand words, which is not the
case here, I think.

A minor stylistic comments:

Toggle quote (1 lines)
> +In subr.el, the definition of lambda is as below, and the (fn) facility
^^^^^^^ ^^^^^^
File names should have the @file markup, and symbols and other code
fragments (like "&rest" and "defun") should have the @code markup.
J
J
Jeremy Bryant wrote on 30 Nov 2023 00:29
(name . Eli Zaretskii)(address . eliz@gnu.org)(address . 67499@debbugs.gnu.org)
87edg89lsn.fsf@jeremybryant.net
Eli Zaretskii <eliz@gnu.org> writes:

Toggle quote (22 lines)
>> Date: Mon, 27 Nov 2023 23:30:33 +0000
>> From: Jeremy Bryant via "Bug reports for GNU Emacs,
>> the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
>>
>> I have written a proposed addition to the Elisp manual. In now-closed
>> bug 66928, there was a discussion related to the use of \(fn) in doc
>> strings, and I have drafted some examples to explain how this facility
>> could be used. The addition is presented as @ifnottex... in order to
>> reduce the cost of the printed manual.
>>
>> Feedback welcome on draft before I refine further, on conventions, section of
>> manual, style etc.
>
> Thanks.
>
> I wonder whether we need this to be said in so many words. Can't we
> instead just enumerate the uses, describing each one in a couple of
> sentences, and format that as, say, an @itemize'd list? IOW, do we
> really need to show an explicit example for each use? Examples are
> useful when an example is worth a thousand words, which is not the
> case here, I think.

Understood, and substantially rewritten as attached, as an @itemize'd list.

The reader can then use find-function at point in the info manual, to
read the code.
From 8ff1589630cb723857b6886c56ac836a333d69e5 Mon Sep 17 00:00:00 2001
From: Jeremy Bryant <jb@jeremybryant.net>
Date: Mon, 27 Nov 2023 23:18:03 +0000
Subject: [PATCH] Add use cases of (fn) documentation facility.

* doc/lispref/functions.texi (Function Documentation): Add examples
---
doc/lispref/functions.texi | 36 ++++++++++++++++++++++++++++++++++++
1 file changed, 36 insertions(+)

Toggle diff (49 lines)
diff --git a/doc/lispref/functions.texi b/doc/lispref/functions.texi
index e646e7c8b0a..fe7ed78d568 100644
--- a/doc/lispref/functions.texi
+++ b/doc/lispref/functions.texi
@@ -533,6 +533,42 @@ Function Documentation
compiler emit a warning message when it compiles Lisp programs which
use the deprecated calling convention.
+@ifnottex
+In this section we outline examples of the use of the @code{(fn)} facility.
+
+@itemize @minus
+@item Explaining @code{&rest} in a @code{defmacro}:
+
+@file{subr.el}: @code{lambda}. This is a canonical example where the
+arguments are unclear, and the @code{(fn)} facility helps to spell out
+the arglist @code{&rest}.
+
+@example
+(defmacro lambda (&rest cdr)
+ "Return an anonymous function. (...)
+\(fn ARGS [DOCSTRING] [INTERACTIVE] BODY)"...)
+@end example
+
+@item Explaining @code{&rest} in a @code{defun}, concretely:
+
+@file{keymap.el} : @code{define-keymap}. The single parameter
+@code{&rest definitions} list is not clear, so the @code{fn} facility
+explains it.
+
+@item Renaming parameters to give context in a @code{defalias}:
+
+@file{abbrev.el} : @code{(abbrev-get)}. Here @code{get}'s general argument of
+@code{symbol} is renamed as @code{abbrev}.
+
+@item Naming list constituents in a required argument:
+
+@file{macroexp.el} : @code{macroexp--accumulate}. The argument of
+@code{var+list}, which is more clearly described as @code{(var list)}.
+@end itemize
+
+@end ifnottex
+
+
@cindex computed documentation string
@kindex :documentation
Documentation strings are usually static, but occasionally it can be
--
2.42.0
Toggle quote (8 lines)
>
> A minor stylistic comments:
>
>> +In subr.el, the definition of lambda is as below, and the (fn) facility
> ^^^^^^^ ^^^^^^
> File names should have the @file markup, and symbols and other code
> fragments (like "&rest" and "defun") should have the @code markup.

Added @file and @code for all applicable markups.


In summary, the new compact text should help new contributors understand how the
(fn) magic can be used.

If it's not good to install, please let me know what to do.
E
E
Eli Zaretskii wrote on 2 Dec 2023 14:44
(name . Jeremy Bryant)(address . jb@jeremybryant.net)(address . 67499@debbugs.gnu.org)
83bkb890sc.fsf@gnu.org
Toggle quote (16 lines)
> From: Jeremy Bryant <jb@jeremybryant.net>
> Cc: 67499@debbugs.gnu.org
> Date: Wed, 29 Nov 2023 23:29:58 +0000
>
>
> Eli Zaretskii <eliz@gnu.org> writes:
>
> > I wonder whether we need this to be said in so many words. Can't we
> > instead just enumerate the uses, describing each one in a couple of
> > sentences, and format that as, say, an @itemize'd list? IOW, do we
> > really need to show an explicit example for each use? Examples are
> > useful when an example is worth a thousand words, which is not the
> > case here, I think.
>
> Understood, and substantially rewritten as attached, as an @itemize'd list.

Thanks. I actually had in mind an even shorter variant:

The @code{(fn)} feature is typically used in the following situations:

@itemize @minus
@item To spell out arguments and their purposes in a macro or a
function. Example:

@example
(defmacro lambda (&rest cdr)
"@dots{}
\(fn ARGS [DOCSTRING] [INTERACTIVE] BODY)"@dots{})
@end example

@item To provide a more detailed description and names of arguments.
Example:

@example
(defmacro macroexp--accumulate (var+list &rest body)
"@dots{}
\(fn (VAR LIST) BODY@dots{})"
(declare (indent 1))
(let ((var (car var+list))
(list (cadr var+list))
@dots{})))
@end example

@item To better explain the purpose of a @code{defalias}. Example:

@example
(defalias 'abbrev-get 'get
"@dots{}
\(fn ABBREV PROP)")
@end example

WDYT?

Toggle quote (3 lines)
> The reader can then use find-function at point in the info manual, to
> read the code.

We don't include pointers to our sources in the manual, except in very
rare cases. I'm not sure we need to do it here. One disadvantage of
having pointers to files is that we need to keep the pointers
up-to-date as development continues.

Thanks.
J
J
Jeremy Bryant wrote on 3 Dec 2023 22:35
(name . Eli Zaretskii)(address . eliz@gnu.org)(address . 67499@debbugs.gnu.org)
871qc39d9k.fsf@jeremybryant.net
From 3f427ea7e437a83b5e62121032d0352abf6b4bd8 Mon Sep 17 00:00:00 2001
From: Jeremy Bryant <jb@jeremybryant.net>
Date: Sun, 3 Dec 2023 21:32:01 +0000
Subject: [PATCH] Add use cases of (fn) documentation facility.

* doc/lispref/functions.texi (Function Documentation): Add examples

Co-authored-by: Eli Zaretskii <eliz@gnu.org>
---
doc/lispref/functions.texi | 33 +++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+)

Toggle diff (46 lines)
diff --git a/doc/lispref/functions.texi b/doc/lispref/functions.texi
index e646e7c8b0a..34a568b728e 100644
--- a/doc/lispref/functions.texi
+++ b/doc/lispref/functions.texi
@@ -533,6 +533,39 @@ Function Documentation
compiler emit a warning message when it compiles Lisp programs which
use the deprecated calling convention.
+@ifnottex
+The @code{(fn)} feature is typically used in the following situations:
+
+@itemize @minus
+@item To spell out arguments and their purposes in a macro or a function. Example:
+
+@example
+(defmacro lambda (&rest cdr)
+ "@dots{}
+\(fn ARGS [DOCSTRING] [INTERACTIVE] BODY)"@dots{})
+@end example
+
+@item To provide a more detailed description and names of arguments. Example:
+
+@example
+(defmacro macroexp--accumulate (var+list &rest body)
+ "@dots{}
+\(fn (VAR LIST) BODY@dots{})"
+ (declare (indent 1))
+ (let ((var (car var+list))
+ (list (cadr var+list))
+@dots{})))
+@end example
+
+@item To better explain the purpose of a @code{defalias}. Example:
+
+@example
+(defalias 'abbrev-get 'get
+ "@dots{}
+\(fn ABBREV PROP)")
+@end example
+@end ifnottex
+
@cindex computed documentation string
@kindex :documentation
Documentation strings are usually static, but occasionally it can be
--
2.42.0
Toggle quote (39 lines)
>
> Thanks. I actually had in mind an even shorter variant:
>
> The @code{(fn)} feature is typically used in the following situations:
>
> @itemize @minus
> @item To spell out arguments and their purposes in a macro or a
> function. Example:
>
> @example
> (defmacro lambda (&rest cdr)
> "@dots{}
> \(fn ARGS [DOCSTRING] [INTERACTIVE] BODY)"@dots{})
> @end example
>
> @item To provide a more detailed description and names of arguments.
> Example:
>
> @example
> (defmacro macroexp--accumulate (var+list &rest body)
> "@dots{}
> \(fn (VAR LIST) BODY@dots{})"
> (declare (indent 1))
> (let ((var (car var+list))
> (list (cadr var+list))
> @dots{})))
> @end example
>
> @item To better explain the purpose of a @code{defalias}. Example:
>
> @example
> (defalias 'abbrev-get 'get
> "@dots{}
> \(fn ABBREV PROP)")
> @end example
>
> WDYT?
>

Agree this is a more readable version of my initial attempts.

The only thing that is not clear in my mind is the use of the @ifnottex.
I have left it in the attached patch, as I understand we are trying to
reduce the size of the printed manual?

I have a 2-volume Elisp manual, which is out of date as the current ones
don't seem to be printed.

I have also added a line to the commit message which seems appropriate
here given the rewrites:
Co-authored-by: Eli Zaretskii <eliz@gnu.org>

If this is all suitable to install I agree to close the original bug.
J
J
Jeremy Bryant wrote on 12 Dec 2023 23:15
Fwd: bug#67499: [PATCH] Add use cases of (fn) documentation facility.
8734w783oj.fsf@jeremybryant.net
Jeremy Bryant via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org> writes:

Toggle quote (57 lines)
> [1. text/x-diff; 0001-Add-use-cases-of-fn-documentation-facility.patch]...
>
>
>>
>> Thanks. I actually had in mind an even shorter variant:
>>
>> The @code{(fn)} feature is typically used in the following situations:
>>
>> @itemize @minus
>> @item To spell out arguments and their purposes in a macro or a
>> function. Example:
>>
>> @example
>> (defmacro lambda (&rest cdr)
>> "@dots{}
>> \(fn ARGS [DOCSTRING] [INTERACTIVE] BODY)"@dots{})
>> @end example
>>
>> @item To provide a more detailed description and names of arguments.
>> Example:
>>
>> @example
>> (defmacro macroexp--accumulate (var+list &rest body)
>> "@dots{}
>> \(fn (VAR LIST) BODY@dots{})"
>> (declare (indent 1))
>> (let ((var (car var+list))
>> (list (cadr var+list))
>> @dots{})))
>> @end example
>>
>> @item To better explain the purpose of a @code{defalias}. Example:
>>
>> @example
>> (defalias 'abbrev-get 'get
>> "@dots{}
>> \(fn ABBREV PROP)")
>> @end example
>>
>> WDYT?
>>
>
> Agree this is a more readable version of my initial attempts.
>
> The only thing that is not clear in my mind is the use of the @ifnottex.
> I have left it in the attached patch, as I understand we are trying to
> reduce the size of the printed manual?
>
> I have a 2-volume Elisp manual, which is out of date as the current ones
> don't seem to be printed.
>
> I have also added a line to the commit message which seems appropriate
> here given the rewrites:
> Co-authored-by: Eli Zaretskii <eliz@gnu.org>
>
> If this is all suitable to install I agree to close the original bug.

Eli,
Kindly let me know if the attached patch is good to install or if
anything else is needed to work on for this bug report?
Apologies if it's too soon to follow-up, as a newer contributor I do
not yet have a good sense of timing on these things.
Best, Jeremy
From 3f427ea7e437a83b5e62121032d0352abf6b4bd8 Mon Sep 17 00:00:00 2001
From: Jeremy Bryant <jb@jeremybryant.net>
Date: Sun, 3 Dec 2023 21:32:01 +0000
Subject: [PATCH] Add use cases of (fn) documentation facility.

* doc/lispref/functions.texi (Function Documentation): Add examples

Co-authored-by: Eli Zaretskii <eliz@gnu.org>
---
doc/lispref/functions.texi | 33 +++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+)

Toggle diff (46 lines)
diff --git a/doc/lispref/functions.texi b/doc/lispref/functions.texi
index e646e7c8b0a..34a568b728e 100644
--- a/doc/lispref/functions.texi
+++ b/doc/lispref/functions.texi
@@ -533,6 +533,39 @@ Function Documentation
compiler emit a warning message when it compiles Lisp programs which
use the deprecated calling convention.
+@ifnottex
+The @code{(fn)} feature is typically used in the following situations:
+
+@itemize @minus
+@item To spell out arguments and their purposes in a macro or a function. Example:
+
+@example
+(defmacro lambda (&rest cdr)
+ "@dots{}
+\(fn ARGS [DOCSTRING] [INTERACTIVE] BODY)"@dots{})
+@end example
+
+@item To provide a more detailed description and names of arguments. Example:
+
+@example
+(defmacro macroexp--accumulate (var+list &rest body)
+ "@dots{}
+\(fn (VAR LIST) BODY@dots{})"
+ (declare (indent 1))
+ (let ((var (car var+list))
+ (list (cadr var+list))
+@dots{})))
+@end example
+
+@item To better explain the purpose of a @code{defalias}. Example:
+
+@example
+(defalias 'abbrev-get 'get
+ "@dots{}
+\(fn ABBREV PROP)")
+@end example
+@end ifnottex
+
@cindex computed documentation string
@kindex :documentation
Documentation strings are usually static, but occasionally it can be
--
2.42.0
E
E
Eli Zaretskii wrote on 15 Dec 2023 14:50
(name . Jeremy Bryant)(address . jb@jeremybryant.net)(address . 67499@debbugs.gnu.org)
83ttojo9q6.fsf@gnu.org
Toggle quote (6 lines)
> From: Jeremy Bryant <jb@jeremybryant.net>
> Date: Tue, 12 Dec 2023 22:15:59 +0000
>
> Apologies if it's too soon to follow-up, as a newer contributor I do
> not yet have a good sense of timing on these things.

Usually two weeks with no progress are a good reason for a ping.

I will get to this soon.
E
E
Eli Zaretskii wrote on 16 Dec 2023 13:58
(name . Jeremy Bryant)(address . jb@jeremybryant.net)(address . 67499-done@debbugs.gnu.org)
83h6kimhho.fsf@gnu.org
Toggle quote (6 lines)
> From: Jeremy Bryant <jb@jeremybryant.net>
> Date: Tue, 12 Dec 2023 22:15:59 +0000
>
> Kindly let me know if the attached patch is good to install or if
> anything else is needed to work on for this bug report?

Thanks, now installed on the emacs-29 branch, and closing the bug.
Closed
?