unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* [elisp]How to ask and provide a Default value?
@ 2004-03-04  6:54 FKtPp@Office ;)
  2004-03-04 15:54 ` Kevin Rodgers
  0 siblings, 1 reply; 9+ messages in thread
From: FKtPp@Office ;) @ 2004-03-04  6:54 UTC (permalink / raw)


hi all,
   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: ")
  (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))))
    (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))))

best wishes and thanks for any suggestions!! :)
yours FKtPp 

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

* Re: [elisp]How to ask and provide a Default value?
  2004-03-04  6:54 [elisp]How to ask and provide a Default value? FKtPp@Office ;)
@ 2004-03-04 15:54 ` Kevin Rodgers
  2004-03-05  1:02   ` FKtPp@Office ;)
  0 siblings, 1 reply; 9+ messages in thread
From: Kevin Rodgers @ 2004-03-04 15:54 UTC (permalink / raw)


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

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

* Re: [elisp]How to ask and provide a Default value?
  2004-03-04 15:54 ` Kevin Rodgers
@ 2004-03-05  1:02   ` FKtPp@Office ;)
  2004-03-05 18:16     ` Kevin Rodgers
  0 siblings, 1 reply; 9+ messages in thread
From: FKtPp@Office ;) @ 2004-03-05  1:02 UTC (permalink / raw)


Kevin Rodgers <ihs_4664@yahoo.com> writes:


[...]

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

Thank you. I got it too :) 

> So evidently Emacs does not provide a default for the `n' character
> code.

But, I still don't know how to use the default character description
:P Would u please give me a sample?

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

And thank u again. I will take ur advise :P

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

* Re: [elisp]How to ask and provide a Default value?
@ 2004-03-05  7:45 Joakim Hove
  2004-03-05  8:04 ` Joakim Hove
  2004-03-06  7:52 ` FKtPp@Office ;)
  0 siblings, 2 replies; 9+ messages in thread
From: Joakim Hove @ 2004-03-05  7:45 UTC (permalink / raw)



Hello,

maybe you can use this little function:

(defun read-number-with-default (prompt &optional default)
  (let ((default-string (if default (number-to-string default) "")))
    (string-to-number (read-from-minibuffer prompt default-string))))

Observe two things:

 1. The default value is presented to the read routine as
    "INITIAL-CONTENTS", and *not* "DEFAULT-VALUE".

 2. The routine does not check the number format, and
    (string-to-number ) seemingly eats anything with digits in it.

In case you want to use this function you would typically read the
input in the body of the function, i.e. something like:

(defun sleeve-step-region (beginning end base step column)
  "Insert a column of increasing numbers in the region."
  (let* ((beginning (read-number-with-default "Beginning: " 100))
         (end       (read-number-with-default "End: " (+ beginning 10)))

...

HTH - Joakim



-- 
  /--------------------------------------------------------------------\
 / Joakim Hove  / hove@bccs.no  /  (55 5) 84076       |                 \
 | Unifob AS, Avdeling for Beregningsvitenskap (BCCS) | Stabburveien 18 |
 | CMU                                                | 5231 Paradis    |
 \ Thormøhlensgt.55, 5020 Bergen.                     | 55 91 28 18     /
  \--------------------------------------------------------------------/

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

* Re: [elisp]How to ask and provide a Default value?
  2004-03-05  7:45 Joakim Hove
@ 2004-03-05  8:04 ` Joakim Hove
  2004-03-06  7:52 ` FKtPp@Office ;)
  1 sibling, 0 replies; 9+ messages in thread
From: Joakim Hove @ 2004-03-05  8:04 UTC (permalink / raw)



Joakim Hove <hove@bccs.no> writes:


> (defun sleeve-step-region (beginning end base step column)
>   "Insert a column of increasing numbers in the region."
>   (let* ((beginning (read-number-with-default "Beginning: " 100))
>          (end       (read-number-with-default "End: " (+ beginning 10)))

Sorry, the whole point of displaying this function header was that
the arguments are *not* input to the function, i.e.

(defun sleeve-step-region ()
  "Insert a column of increasing numbers in the region."
  (let* ((beginning (read-number-with-default "Beginning: " 100))
         (end       (read-number-with-default "End: " (+ beginning 10)))


Joakim

-- 
  /--------------------------------------------------------------------\
 / Joakim Hove  / hove@bccs.no  /  (55 5) 84076       |                 \
 | Unifob AS, Avdeling for Beregningsvitenskap (BCCS) | Stabburveien 18 |
 | CMU                                                | 5231 Paradis    |
 \ Thormøhlensgt.55, 5020 Bergen.                     | 55 91 28 18     /
  \--------------------------------------------------------------------/

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

* Re: [elisp]How to ask and provide a Default value?
  2004-03-05  1:02   ` FKtPp@Office ;)
@ 2004-03-05 18:16     ` Kevin Rodgers
  2004-03-06  7:56       ` FKtPp@Office ;)
  0 siblings, 1 reply; 9+ messages in thread
From: Kevin Rodgers @ 2004-03-05 18:16 UTC (permalink / raw)


FKtPp@Office ;) wrote:
 > Kevin Rodgers <ihs_4664@yahoo.com> writes:
 >>(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.
 >
 > Thank you. I got it too :)
 >
 >>So evidently Emacs does not provide a default for the `n' character
 >>code.
 >
 > But, I still don't know how to use the default character description
 > :P Would u please give me a sample?

The `b' character code, which is used to return an existing buffer, does
provide a default (the current buffer):

(defun foo (buffer)
   (interactive "bBuffer: ")
   (message "buffer=%s" buffer))

-- 
Kevin Rodgers

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

* Re: [elisp]How to ask and provide a Default value?
  2004-03-05  7:45 Joakim Hove
  2004-03-05  8:04 ` Joakim Hove
@ 2004-03-06  7:52 ` FKtPp@Office ;)
  1 sibling, 0 replies; 9+ messages in thread
From: FKtPp@Office ;) @ 2004-03-06  7:52 UTC (permalink / raw)


thanks a lot :P

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

* Re: [elisp]How to ask and provide a Default value?
  2004-03-05 18:16     ` Kevin Rodgers
@ 2004-03-06  7:56       ` FKtPp@Office ;)
  2004-03-08 18:46         ` Kevin Rodgers
  0 siblings, 1 reply; 9+ messages in thread
From: FKtPp@Office ;) @ 2004-03-06  7:56 UTC (permalink / raw)


Kevin Rodgers <ihs_4664@yahoo.com> writes:

> The `b' character code, which is used to return an existing buffer, does
> provide a default (the current buffer):
>
> (defun foo (buffer)
>    (interactive "bBuffer: ")
>    (message "buffer=%s" buffer))

The "Default" character description not always available, is that true?

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

* Re: [elisp]How to ask and provide a Default value?
  2004-03-06  7:56       ` FKtPp@Office ;)
@ 2004-03-08 18:46         ` Kevin Rodgers
  0 siblings, 0 replies; 9+ messages in thread
From: Kevin Rodgers @ 2004-03-08 18:46 UTC (permalink / raw)


FKtPp@Office ;) wrote:
 > Kevin Rodgers <ihs_4664@yahoo.com> writes:
 >>The `b' character code, which is used to return an existing buffer, does
 >>provide a default (the current buffer):
 >>
 >>(defun foo (buffer)
 >>   (interactive "bBuffer: ")
 >>   (message "buffer=%s" buffer))
 >>
 >
 > The "Default" character description not always available, is that true?

That is what we discovered about the `n' character code just 2 days
before your most recent post.

-- 
Kevin Rodgers

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

end of thread, other threads:[~2004-03-08 18:46 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-03-04  6:54 [elisp]How to ask and provide a Default value? FKtPp@Office ;)
2004-03-04 15:54 ` Kevin Rodgers
2004-03-05  1:02   ` FKtPp@Office ;)
2004-03-05 18:16     ` Kevin Rodgers
2004-03-06  7:56       ` FKtPp@Office ;)
2004-03-08 18:46         ` Kevin Rodgers
  -- strict thread matches above, loose matches on Subject: below --
2004-03-05  7:45 Joakim Hove
2004-03-05  8:04 ` Joakim Hove
2004-03-06  7:52 ` FKtPp@Office ;)

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