[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Numeric argument defaulting
From: |
Kevin Rodgers |
Subject: |
Re: Numeric argument defaulting |
Date: |
Mon, 21 Jun 2004 09:27:38 -0600 |
User-agent: |
Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:0.9.4.1) Gecko/20020406 Netscape6/6.2.2 |
Bill Brodie wrote:
> I have several utility functions that take optional numeric arguments and
> that I'd also like to make interactive. There are two ways to do this:
>
> 1. Define the function to accept either a raw or a numeric argument.
>
> (defun f (&optional x)
> (interactive "P")
> (cond
> ((numberp x))
> ((null x) (setq x <default value>))
> (t (setq x (prefix-numeric-value x))))
> ...)
>
> -or-
>
> 2. Define an interactive wrapper around the function.
>
> (defun f (&optional x)
> (if (null x) (setq x <default value>))
> ...)
> (defun f-interactive (x)
> (interactive "P")
> (if x (f (prefix-numeric-value x)) (f)))
>
>
> Neither of these seems especially clean. Is there a standard Emacs Lisp
> idiom for this?
The argument is numeric, so use (interactive "p"). It will default to 1
when the argument isn't specified interactively. If you want to provide
a default when it's called programmatically, you have to do that
explicitly.
(defun f (&optional x)
(interactive "p")
(if (null x) (setq x 1))
...)
--
Kevin Rodgers