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

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

Re: [elisp]How to ask and provide a Default value?


From: Kevin Rodgers
Subject: Re: [elisp]How to ask and provide a Default value?
Date: Thu, 04 Mar 2004 08:54:31 -0700
User-agent: Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:0.9.4.1) Gecko/20020406 Netscape6/6.2.2

FKtPp@Office ;) wrote:
>    I wrote a simple function like:
>
> (defun sleeve-step-region (beginning end base step column)
>   "Insert a column of increasing numbers in the region.
> "
>
> ;; And here, I want it ask some parameters when invoked. Now the
> ;; problem is: I want it provide a *Default* value, but don't know how
> ;; to write the character descriptions -_-!! In "(elisp) Interactive
> ;; Codes"
> ;; I find this:
> ;;
> ;; ,----
> ;; |  Default
> ;; |      A default value of some sort is used if the user enters no text in
> ;; |      the minibuffer.  The default depends on the code character.
> ;; `----
> ;;
> ;; Can anyone give me some sample to use this "Default" character
> ;; description?
> ;;
> ;; Somebody have suggested  me to use "Lisp expression" in the
> ;; interactive special form. But I still want to know how to use the
> ;; character descriptions :P
>
>   (interactive "r\nnTell me the base Number: \nnTell me the Step: \nnWhich column: 
")

OK, so you're using just `r' for the beginning and end args and `n' for
each of the base, step, and column args.  `r' doesn't prompt the user
for input, but provides the current region bounds automatically.  And
here's a simple way to test the `n' character code:

(defun foo (number)
  (interactive "nNumber: ")
  (message "number=%d" number))

When I type `M-x foo RET RET' on Emacs 19.34, I get an "End of file
during parsing" error.  When I type that on Emacs 21.3, I get a
transient "Please enter a number." Message, then the prompt is
redisplayed.  So evidently Emacs does not provide a default for the `n'
character code.

>   (let ((step-number (if (numberp step)
>              step
>            (string-to-number step)))
>    (base-number (if (numberp base)
>                     base
>                   (string-to-number base)))
>    (column-number (if (numberp column)
>                       column
>                     (string-to-number column))))

You are confused here: the `n' characters codes in the interactive form
will cause base, step, and column to be bound to numbers, not strings.

>     (save-excursion
>       (narrow-to-region beginning end)
>       (goto-char (point-min))
>       (beginning-of-line)
>       (while (< (point) (point-max))
>    (goto-char (+ (point) column-number))
>    (insert (number-to-string base-number))
>    (setq base-number (+ base-number step-number))
>    (forward-line))
>       (widen))))

When the save-excursion form returns, it will restore the previous
narrowing (if any), so you should not explicitly call widen there.

--
Kevin Rodgers



reply via email to

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