help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How many parameters does an elisp function take?


From: Kevin Rodgers
Subject: Re: How many parameters does an elisp function take?
Date: Wed, 16 Feb 2005 16:56:47 -0700
User-agent: Mozilla Thunderbird 0.9 (X11/20041105)

David Kastrup wrote:
Kevin Rodgers <address@hidden> 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 via email to

[Prev in Thread] Current Thread [Next in Thread]