unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Kevin Rodgers <ihs_4664@yahoo.com>
Subject: Re: How many parameters does an elisp function take?
Date: Wed, 16 Feb 2005 16:56:47 -0700	[thread overview]
Message-ID: <37i4u2F5b1ro7U1@individual.net> (raw)
In-Reply-To: <x5vf8s43s2.fsf@lola.goethe.zz>

David Kastrup wrote:
> Kevin Rodgers <ihs_4664@yahoo.com> writes:
>>Alan Mackenzie wrote:
>>>Is it possible to determine at run time how many parameters an elisp
>>>function takes?  For example, I'd like to write something like:
>>>(how-many-params 'null)
>>>and have it evaluate to 1.  Or something like that.  Together with
>>>some
>>>reasonable convention for indicating &optional and &rest arguments.
>>
>>I would start with eldoc-function-arglist.
> 
> For built-in functions, subr-arity might help.

And now for lisp functions, lambda-arity:

(require 'eldoc)

(defun lambda-arity (function)
   "Return minimum and maximum number of args allowed for FUNCTION.
FUNCTION must be a symbol whose function binding is a lambda expression
or a macro.
The returned value is a pair (MIN . MAX).  MIN is the minimum number
of args.  MAX is the maximum number or the symbol `many', for a lambda
or macro with `&rest' args."
   (let* ((arglist (eldoc-function-arglist function))
          (optional-arglist (memq '&optional arglist))
          (rest-arglist (memq '&rest arglist)))
     (cons (- (length arglist)
              (cond (optional-arglist (length optional-arglist))
                    (rest-arglist (length rest-arglist))
                    (t 0)))
           (cond (rest-arglist 'many)
                 (optional-arglist (+ (length arglist)
                                      (length optional-arglist)
                                      -1))
                 (t (length arglist))))))

-- 
Kevin Rodgers

  reply	other threads:[~2005-02-16 23:56 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-02-16 20:11 How many parameters does an elisp function take? Alan Mackenzie
2005-02-16 20:45 ` Kevin Rodgers
2005-02-16 21:02   ` David Kastrup
2005-02-16 23:56     ` Kevin Rodgers [this message]
2005-02-18 13:59 ` Stefan Monnier
2005-02-18 17:43   ` Alan Mackenzie
2005-02-22 14:41     ` Stefan Monnier

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=37i4u2F5b1ro7U1@individual.net \
    --to=ihs_4664@yahoo.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.
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).