unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Numeric argument defaulting
@ 2004-06-20 12:58 Bill Brodie
  2004-06-21 15:27 ` Kevin Rodgers
  0 siblings, 1 reply; 2+ messages in thread
From: Bill Brodie @ 2004-06-20 12:58 UTC (permalink / raw)


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?

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Numeric argument defaulting
  2004-06-20 12:58 Numeric argument defaulting Bill Brodie
@ 2004-06-21 15:27 ` Kevin Rodgers
  0 siblings, 0 replies; 2+ messages in thread
From: Kevin Rodgers @ 2004-06-21 15:27 UTC (permalink / raw)


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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2004-06-21 15:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-06-20 12:58 Numeric argument defaulting Bill Brodie
2004-06-21 15:27 ` Kevin Rodgers

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).