unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: npostavs@users.sourceforge.net
To: 26270@debbugs.gnu.org
Cc: Philipp Stephani <p.stephani2@gmail.com>
Subject: bug#26270: help-function-arglist doesn't respect "(fn ARGS...)" in docstring
Date: Fri, 02 Jun 2017 23:03:14 -0400	[thread overview]
Message-ID: <87r2z122od.fsf@users.sourceforge.net> (raw)
In-Reply-To: <8737dz4bj8.fsf@users.sourceforge.net> (npostavs@users.sourceforge.net's message of "Sun, 26 Mar 2017 23:57:15 -0400")

[-- Attachment #1: Type: text/plain, Size: 339 bytes --]

npostavs@users.sourceforge.net writes:

>     (defun foo (&rest args)
>       "Do foo.
>
>     \(fn ARG1 ARG2)")
>
>     (help-function-arglist 'foo) ;=> (&rest args)

Here's a patch which puts the check of the docstring before the lambda
list.  I'm not sure what to do with the PRESERVE-NAMES argument of
`help-function-arglist' though.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: patch --]
[-- Type: text/x-diff, Size: 4451 bytes --]

From 0a84e9441d94485ba20c2e44cb9062a7e4259968 Mon Sep 17 00:00:00 2001
From: Noam Postavsky <npostavs@gmail.com>
Date: Fri, 2 Jun 2017 22:34:32 -0400
Subject: [PATCH v1] Prefer docstring as source of arglist with names
 (Bug#26270)

* lisp/help.el (help-arglist-from-docstring): New function, extracted
from `help-function-arglist'.
(help-function-arglist): Use it before looking for a function's lambda
list.
---
 lisp/help.el | 61 ++++++++++++++++++++++++++++++++----------------------------
 1 file changed, 33 insertions(+), 28 deletions(-)

diff --git a/lisp/help.el b/lisp/help.el
index 361ab2a01e..4d98c1dc81 100644
--- a/lisp/help.el
+++ b/lisp/help.el
@@ -1417,6 +1417,23 @@ (defun help-add-fundoc-usage (docstring arglist)
                   (error "Unrecognized usage format"))
 	      (help--make-usage-docstring 'fn arglist)))))
 
+(defun help-arglist-from-docstring (doc)
+  "Return argument list parsed from the docstring, DOC.
+DOC should end with \"(fn ARGLIST)\", see Info node `(elisp)
+Function Documentation'."
+  (let* ((docargs (if doc (car (help-split-fundoc doc nil))))
+         (arglist (if docargs (cdr (read (downcase docargs)))))
+         (valid t))
+    ;; Check validity.
+    (dolist (arg arglist)
+      (unless (and (symbolp arg)
+                   (let ((name (symbol-name arg)))
+                     (if (eq (aref name 0) ?&)
+                         (memq arg '(&rest &optional))
+                       (not (string-match "\\." name)))))
+        (setq valid nil)))
+    (when valid arglist)))
+
 (defun help-function-arglist (def &optional preserve-names)
   "Return a formal argument list for the function DEF.
 If PRESERVE-NAMES is non-nil, return a formal arglist that uses
@@ -1425,40 +1442,28 @@ (defun help-function-arglist (def &optional preserve-names)
   (if (and (symbolp def) (fboundp def)) (setq def (indirect-function def)))
   ;; If definition is a macro, find the function inside it.
   (if (eq (car-safe def) 'macro) (setq def (cdr def)))
+  ;; FIXME: What does PRESERVE-NAMES really mean??
   (cond
+   ((help-arglist-from-docstring
+     (ignore-errors (documentation def))))
    ((and (byte-code-function-p def) (listp (aref def 0))) (aref def 0))
    ((eq (car-safe def) 'lambda) (nth 1 def))
    ((eq (car-safe def) 'closure) (nth 2 def))
    ((or (and (byte-code-function-p def) (integerp (aref def 0)))
         (subrp def) (module-function-p def))
-    (or (when preserve-names
-          (let* ((doc (condition-case nil (documentation def) (error nil)))
-                 (docargs (if doc (car (help-split-fundoc doc nil))))
-                 (arglist (if docargs
-                              (cdar (read-from-string (downcase docargs)))))
-                 (valid t))
-            ;; Check validity.
-            (dolist (arg arglist)
-              (unless (and (symbolp arg)
-                           (let ((name (symbol-name arg)))
-                             (if (eq (aref name 0) ?&)
-                                 (memq arg '(&rest &optional))
-                               (not (string-match "\\." name)))))
-                (setq valid nil)))
-            (when valid arglist)))
-        (let* ((arity (func-arity def))
-               (max (cdr arity))
-               (min (car arity))
-               (arglist ()))
-          (dotimes (i min)
-            (push (intern (concat "arg" (number-to-string (1+ i)))) arglist))
-          (when (and (integerp max) (> max min))
-            (push '&optional arglist)
-            (dotimes (i (- max min))
-              (push (intern (concat "arg" (number-to-string (+ 1 i min))))
-                    arglist)))
-          (unless (integerp max) (push '&rest arglist) (push 'rest arglist))
-          (nreverse arglist))))
+    (let* ((arity (func-arity def))
+           (max (cdr arity))
+           (min (car arity))
+           (arglist ()))
+      (dotimes (i min)
+        (push (intern (concat "arg" (number-to-string (1+ i)))) arglist))
+      (when (and (integerp max) (> max min))
+        (push '&optional arglist)
+        (dotimes (i (- max min))
+          (push (intern (concat "arg" (number-to-string (+ 1 i min))))
+                arglist)))
+      (unless (integerp max) (push '&rest arglist) (push 'rest arglist))
+      (nreverse arglist)))
    ((and (autoloadp def) (not (eq (nth 4 def) 'keymap)))
     "[Arg list not available until function definition is loaded.]")
    (t t)))
-- 
2.11.1


  reply	other threads:[~2017-06-03  3:03 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-27  3:57 bug#26270: help-function-arglist doesn't respect "(fn ARGS...)" in docstring npostavs
2017-06-03  3:03 ` npostavs [this message]
2020-08-19 11:31 ` Lars Ingebrigtsen
2020-10-11  2:19   ` Lars Ingebrigtsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87r2z122od.fsf@users.sourceforge.net \
    --to=npostavs@users.sourceforge.net \
    --cc=26270@debbugs.gnu.org \
    --cc=p.stephani2@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).