unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* HOW TO GIVE A DEFAULT TO A TRULY INTERACTIVE FUNCTION
@ 2002-10-14 16:02 gnuist006
  2002-10-14 17:22 ` Joe Casadonte
                   ` (2 more replies)
  0 siblings, 3 replies; 16+ messages in thread
From: gnuist006 @ 2002-10-14 16:02 UTC (permalink / raw)


(defun demo (&optional number)
"Demo of an     optional argument of the function."
(interactive "nEnter a number:")
(setq defaultval 5)
(if (null number) (setq number defaultval))
(insert (format "%s" number))
)                                        (demo)                       
           ; works due to optional argument

I want the default to work on the command line also.
They say emacs is customizable. OK it is if you only want to do what
it
can do, and never try to do what you want to do.

I want to invoke it on command line ie M-x demo
It comes and says
Enter a number:_

I prefer it display the default 5 in the message and there is
SINGLE instance of 5 in the function. ie using defaultval.

If this cannot do this and you were all lying to yourself that
emacs is customizable, infinitely extensible, then I atleast want
quick fix for this.

When I hit RTN after it begs for number once, it go away and use
defaultval and stop buggin me.

At present it does not stap and keeps bugging till I enter 5.
Why do I have to remember the default when I am giving it that much
money?

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

* Re: HOW TO GIVE A DEFAULT TO A TRULY INTERACTIVE FUNCTION
  2002-10-14 16:02 gnuist006
@ 2002-10-14 17:22 ` Joe Casadonte
  2002-10-14 20:07 ` Edi Weitz
  2002-10-18  7:07 ` Friedrich Dominicus
  2 siblings, 0 replies; 16+ messages in thread
From: Joe Casadonte @ 2002-10-14 17:22 UTC (permalink / raw)


On 14 Oct 2002, gnuist006@hotmail.com wrote:

> If this cannot do this and you were all lying to yourself that
> emacs is customizable, infinitely extensible, then I atleast want
> quick fix for this.

Boy, and I think *I* have an attitude at times.  Ttry
`completing-read'.  There may be an easier way, but that's what popped
into my head.

--
Regards,

joe
Joe Casadonte
jcasadonte@northbound-train.com

------------------------------------------------------------------------------
         Llama Fresh Farms => http://www.northbound-train.com
   Gay Media Resource List => http://www.northbound-train.com/gaymedia.html
            Perl for Win32 => http://www.northbound-train.com/perlwin32.html
               Emacs Stuff => http://www.northbound-train.com/emacs.html
          Music CD Trading => http://www.northbound-train.com/cdr.html
------------------------------------------------------------------------------
                       Live Free, that's the message!
------------------------------------------------------------------------------

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

* RE: HOW TO GIVE A DEFAULT TO A TRULY INTERACTIVE FUNCTION
@ 2002-10-14 18:27 Bingham, Jay
  0 siblings, 0 replies; 16+ messages in thread
From: Bingham, Jay @ 2002-10-14 18:27 UTC (permalink / raw)


The reason that this function does not do what you want it to is that the interactive code, "n", in the string type arg-descriptor specifies that the interactive value must be a number, emacs will not let you enter a non-numeric value which is what just typing return at the prompt amounts to.  So emacs tells you to enter a number and redisplays the prompt.
Using a string type arg-descriptor the only way that you can set up a function to allow you to enter a number or nothing is to use the "s" interactive code character (which does not provide any number validation)  If you are using emacs 21 the easiest way to do number validation is to use the string-match function with the [:alpha:] character class as the regexp.
Also when the argument is a string the "if (null number)" construct cannot be used, use "if (string= "" number)" instead.

Here is your function using a string arg-descriptor, that does some very crude numeric validation and inserts a 5 when nothing is entered at the prompt.

(defun demo (&optional number)
  "Demo of an     optional argument of the function."
  (interactive "sEnter a numeric value:")
  (setq defaultval "5")
  (if (string= "" number) (setq number defaultval))
  (if (not (string-match "[:alpha:]" number))
      (insert (format "%s" number))
    (message "non-numeric value entered, please re-enter"))
  )

If you want a function that prompts for a number until one is entered you will have to use an elisp expression type arg-descriptor and do your own number validation.  I used to have a function that did its own number validation but I lost it when I left my last employer, so I can't send you an example without going to a lot of work.  As I recall the lisp expression prompted for the number, did the validation and re-issued the prompt if the string entered was not numeric.  There may be some examples in the emacs lisp reference manual (see: http://www.gnu.org/manual/elisp-manual-21-2.8).

-_
J_)
C_)ingham
.    HP - NonStop Austin Software & Services - Software Product Assurance
.    Austin, TX
. Language is the apparel in which your thoughts parade in public.
. Never clothe them in vulgar and shoddy attire.          -Dr. George W. Crane-

 -----Original Message-----
From: 	gnuist006 [mailto:gnuist006@hotmail.com] 
Sent:	Monday, October 14, 2002 11:02 AM
To:	help-gnu-emacs@gnu.org
Subject:	HOW TO GIVE A DEFAULT TO A TRULY INTERACTIVE FUNCTION

(defun demo (&optional number)
"Demo of an     optional argument of the function."
(interactive "nEnter a number:")
(setq defaultval 5)
(if (null number) (setq number defaultval))
(insert (format "%s" number))
)                                        (demo)                       
           ; works due to optional argument

I want the default to work on the command line also.
They say emacs is customizable. OK it is if you only want to do what
it
can do, and never try to do what you want to do.

I want to invoke it on command line ie M-x demo
It comes and says
Enter a number:_

I prefer it display the default 5 in the message and there is
SINGLE instance of 5 in the function. ie using defaultval.

If this cannot do this and you were all lying to yourself that
emacs is customizable, infinitely extensible, then I atleast want
quick fix for this.

When I hit RTN after it begs for number once, it go away and use
defaultval and stop buggin me.

At present it does not stap and keeps bugging till I enter 5.
Why do I have to remember the default when I am giving it that much
money?
_______________________________________________
Help-gnu-emacs mailing list
Help-gnu-emacs@gnu.org
http://mail.gnu.org/mailman/listinfo/help-gnu-emacs

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

* Re: HOW TO GIVE A DEFAULT TO A TRULY INTERACTIVE FUNCTION
  2002-10-14 16:02 gnuist006
  2002-10-14 17:22 ` Joe Casadonte
@ 2002-10-14 20:07 ` Edi Weitz
  2002-10-17 14:27   ` gnuist006
  2002-10-17 14:35   ` gnuist006
  2002-10-18  7:07 ` Friedrich Dominicus
  2 siblings, 2 replies; 16+ messages in thread
From: Edi Weitz @ 2002-10-14 20:07 UTC (permalink / raw)


gnuist006@hotmail.com (gnuist006) writes:

> (defun demo (&optional number)
> "Demo of an     optional argument of the function."
> (interactive "nEnter a number:")
> (setq defaultval 5)
> (if (null number) (setq number defaultval))
> (insert (format "%s" number))
> )                                        (demo)                       
>            ; works due to optional argument
> 
> I want the default to work on the command line also.
> They say emacs is customizable. OK it is if you only want to do what
> it
> can do, and never try to do what you want to do.
> 
> I want to invoke it on command line ie M-x demo
> It comes and says
> Enter a number:_
> 
> I prefer it display the default 5 in the message and there is
> SINGLE instance of 5 in the function. ie using defaultval.
> 
> If this cannot do this and you were all lying to yourself that
> emacs is customizable, infinitely extensible, then I atleast want
> quick fix for this.
> 
> When I hit RTN after it begs for number once, it go away and use
> defaultval and stop buggin me.
> 
> At present it does not stap and keeps bugging till I enter 5.
> Why do I have to remember the default when I am giving it that much
> money?

This guy is obviously a troll who doesn't deserve an answer but for
the sake of others who might read this:

  (require 'cl)

  (defun* bar (&optional (default 5))
    (let* ((default-string (format "%s" default))
           (string (read-string "Number: "
                                default-string
                                nil
                                default-string)))
      ;; needs check for wrong input
      (list (string-to-number string))))
  
  (defun foo (n)
    (interactive
     (bar 42))
    (insert (format "%s" (* 3 n))))
  
I'm sure the real Emacs experts know better ways to do this, I just
wanted to show that it's doable.

Edi.

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

* Re: HOW TO GIVE A DEFAULT TO A TRULY INTERACTIVE FUNCTION
       [not found] <mailman.1034620095.15632.help-gnu-emacs@gnu.org>
@ 2002-10-17 14:22 ` gnuist006
  2002-10-18  7:14   ` Friedrich Dominicus
  0 siblings, 1 reply; 16+ messages in thread
From: gnuist006 @ 2002-10-17 14:22 UTC (permalink / raw)


"Bingham, Jay" <Jay.Bingham@hp.com> wrote in message news:<mailman.1034620095.15632.help-gnu-emacs@gnu.org>...

> 
> Here is your function using a string arg-descriptor, that does some very 
> crude numeric validation and inserts a 5 when nothing is entered at the 
> prompt.
> 
> (defun demo (&optional number)
>   "Demo of an     optional argument of the function."
>   (interactive "sEnter a numeric value:")
>   (setq defaultval "5")
>   (if (string= "" number) (setq number defaultval))
>   (if (not (string-match "[:alpha:]" number))
>       (insert (format "%s" number))
>     (message "non-numeric value entered, please re-enter"))
>   )

It works on my GNU Emacs 20.3.1 when modified to:

  (if (string-match "[0-9]+" number)
      (insert (format "%s" number))
    (message "non-numeric value entered, please re-enter"))
  )


But as you said below fails to ask number again when "x" is given
as the value. I am still lookin for a good solution that does validation
and probably displays the default value when it asks the number
first time without ruining single instantiation of the default value.

> If you want a function that prompts for a number until one is entered 
> you will have to use an elisp expression type arg-descriptor and do your 
> own number validation.

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

* Re: HOW TO GIVE A DEFAULT TO A TRULY INTERACTIVE FUNCTION
  2002-10-14 20:07 ` Edi Weitz
@ 2002-10-17 14:27   ` gnuist006
  2002-10-17 17:35     ` Barry Margolin
  2002-10-18 10:36     ` Oliver Scholz
  2002-10-17 14:35   ` gnuist006
  1 sibling, 2 replies; 16+ messages in thread
From: gnuist006 @ 2002-10-17 14:27 UTC (permalink / raw)


Edi Weitz <edi@agharta.de> wrote in message 

> This guy is obviously a troll who doesn't deserve an answer but for
> the sake of others who might read this:

> I'm sure the real Emacs experts know better ways to do this, I just
> wanted to show that it's doable.

You are sure of too many things about others but how much are you sure
of yourself?

Anyways, I am still looking for "the real Emacs experts" that you are
so sure of.

The problem is restated below for convenience of the "rEes":

(defun demo (&optional number)
"Demo of an     optional argument of the function."
(interactive "nEnter a number:")
(setq defaultval 5)
(if (null number) (setq number defaultval))
(insert (format "%s" number))
)                                        (demo)                       
           ; works due to optional argument

I want the default to work on the command line also.
They say emacs is customizable. OK it is if you only want to do what
it
can do, and never try to do what you want to do.

I want to invoke it on command line ie M-x demo
It comes and says
Enter a number:_

I prefer it display the default 5 in the message and there is
SINGLE instance of 5 in the function. ie using defaultval.

If this cannot do this and you were all lying to yourself that
emacs is customizable, infinitely extensible, then I atleast want
quick fix for this.

When I hit RTN after it begs for number once, it go away and use
defaultval and stop buggin me.

At present it does not stap and keeps bugging till I enter 5.
Why do I have to remember the default when I am giving it that much
money?

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

* Re: HOW TO GIVE A DEFAULT TO A TRULY INTERACTIVE FUNCTION
  2002-10-14 20:07 ` Edi Weitz
  2002-10-17 14:27   ` gnuist006
@ 2002-10-17 14:35   ` gnuist006
  2002-10-17 15:03     ` Edi Weitz
  2002-10-18  7:13     ` Friedrich Dominicus
  1 sibling, 2 replies; 16+ messages in thread
From: gnuist006 @ 2002-10-17 14:35 UTC (permalink / raw)


Edi Weitz <edi@agharta.de> wrote in message 

>   (require 'cl)
> 
>   (defun* bar (&optional (default 5))
>     (let* ((default-string (format "%s" default))
>            (string (read-string "Number: "
>                                 default-string
>                                 nil
>                                 default-string)))
>       ;; needs check for wrong input
>       (list (string-to-number string))))
>   
>   (defun foo (n)
>     (interactive
>      (bar 42))
>     (insert (format "%s" (* 3 n))))

Pathetic solution. breaks one, fixes noone.
It just accepted "u" as the input and did not do number
validation.

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

* Re: HOW TO GIVE A DEFAULT TO A TRULY INTERACTIVE FUNCTION
  2002-10-17 14:35   ` gnuist006
@ 2002-10-17 15:03     ` Edi Weitz
  2002-10-18  5:51       ` huntingdon
  2002-10-18  7:13     ` Friedrich Dominicus
  1 sibling, 1 reply; 16+ messages in thread
From: Edi Weitz @ 2002-10-17 15:03 UTC (permalink / raw)


gnuist006@hotmail.com (gnuist006) writes:

> Edi Weitz <edi@agharta.de> wrote in message 
> 
> >   (require 'cl)
> > 
> >   (defun* bar (&optional (default 5))
> >     (let* ((default-string (format "%s" default))
> >            (string (read-string "Number: "
> >                                 default-string
> >                                 nil
> >                                 default-string)))
> >       ;; needs check for wrong input
> >       (list (string-to-number string))))
> >   
> >   (defun foo (n)
> >     (interactive
> >      (bar 42))
> >     (insert (format "%s" (* 3 n))))
> 
> Pathetic solution. breaks one, fixes noone.
> It just accepted "u" as the input and did not do number
> validation.

Looks like I was right in my first posting: You obviously are a troll.

You didn't say anything about number validation and your icky little
"I look like a C program" Elisp demo didn't do that either. If you
could read you could have seen that there's a comment with respect to
validation in my code. Should be easy to add for every non-idiot.

But count me out on providing further free help to you. The next time
someone posts a solution you'll complain that it doesn't help you with
your tax declaration.

Edi.

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

* Re: HOW TO GIVE A DEFAULT TO A TRULY INTERACTIVE FUNCTION
  2002-10-17 14:27   ` gnuist006
@ 2002-10-17 17:35     ` Barry Margolin
  2002-10-18 10:36     ` Oliver Scholz
  1 sibling, 0 replies; 16+ messages in thread
From: Barry Margolin @ 2002-10-17 17:35 UTC (permalink / raw)


In article <b00bb831.0210170627.2fe31d74@posting.google.com>,
gnuist006 <gnuist006@hotmail.com> wrote:
>If this cannot do this and you were all lying to yourself that
>emacs is customizable, infinitely extensible, then I atleast want
>quick fix for this.

It's extremely customizable, but some customizations are easier than
others.  It's very easy for fuctions to provide hooks for the user to
customize, but the author of the function has to think of it and program
for it.  In some cases you can use "defadvice" to make minor adjustments to
the behavior of a function.  And in the extreme case, when neither solution
is applicable, you can completely replace the function with your own
version.

The only exception is some low-level functions internal to Emacs, which are
written in C.  When these call each other, they don't go through the Lisp
interpreter, so you can't substitute your own versions.  But since Emacs
comes with full source code, you can modify the C function and recompile
Emacs if you really need to customize at this level.

-- 
Barry Margolin, barmar@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

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

* Re: HOW TO GIVE A DEFAULT TO A TRULY INTERACTIVE FUNCTION
  2002-10-17 15:03     ` Edi Weitz
@ 2002-10-18  5:51       ` huntingdon
  0 siblings, 0 replies; 16+ messages in thread
From: huntingdon @ 2002-10-18  5:51 UTC (permalink / raw)


On 17 Oct 2002, Edi Weitz wrote:
> gnuist006@hotmail.com (gnuist006) writes:
> 
>> Edi Weitz <edi@agharta.de> wrote in message 
>> 

[...]
 
>> Pathetic solution. breaks one, fixes noone.
>> It just accepted "u" as the input and did not do number
>> validation.
> 
> Looks like I was right in my first posting: You obviously are a
> troll.
> 
> You didn't say anything about number validation and your icky little
> "I look like a C program" Elisp demo didn't do that either. If you
> could read you could have seen that there's a comment with respect
> to validation in my code. Should be easy to add for every non-idiot.
> 
> But count me out on providing further free help to you. The next
> time someone posts a solution you'll complain that it doesn't help
> you with your tax declaration.
> 
> Edi.

gnuist is a damned replicant, trying to humanize itself by insulting other
posters. After all this kind of behaviour is human.  


-- 

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

* Re: HOW TO GIVE A DEFAULT TO A TRULY INTERACTIVE FUNCTION
  2002-10-14 16:02 gnuist006
  2002-10-14 17:22 ` Joe Casadonte
  2002-10-14 20:07 ` Edi Weitz
@ 2002-10-18  7:07 ` Friedrich Dominicus
  2002-10-18  8:05   ` Friedrich Dominicus
  2002-10-18 17:05   ` Kevin Rodgers
  2 siblings, 2 replies; 16+ messages in thread
From: Friedrich Dominicus @ 2002-10-18  7:07 UTC (permalink / raw)


gnuist006@hotmail.com (gnuist006) writes:

> (defun demo (&optional number)
> "Demo of an     optional argument of the function."
> (interactive "nEnter a number:")
> (setq defaultval 5)
> (if (null number) (setq number defaultval))
> (insert (format "%s" number))
> )                                        (demo)                       
>            ; works due to optional argument
> 
> I want the default to work on the command line also.
> They say emacs is customizable. OK it is if you only want to do what
> it
> can do, and never try to do what you want to do.
> 
> I want to invoke it on command line ie M-x demo
> It comes and says
> Enter a number:_
> 
> I prefer it display the default 5 in the message and there is
> SINGLE instance of 5 in the function. ie using defaultval.

Easy but not a good idea nevertheless. 
(defun demo (&optional num)
  (interactive
   (let* ((defaultval 5)
          (prompt (format "Give me a number (default %d): " defaultval)))
       (list (read-number prompt t (prin1-to-string defaultval)))))
  (insert (format "got %d" num)))

Counterintuitive is (prin1-to-string) I would qualify it as a bug and
suggest following patch:
Original: 


(defun read-number (prompt &optional integers-only default-value)
  "Read a number from the minibuffer, prompting with PROMPT.
If optional second argument INTEGERS-ONLY is non-nil, accept
 only integer input.
If DEFAULT-VALUE is non-nil, return that if user enters an empty
 line."
  (let ((pred (if integers-only 'integerp 'numberp))
	num)
    (while (not (funcall pred num))
      (setq num (condition-case ()
		    (let ((minibuffer-completion-table nil))
		      (read-from-minibuffer
		       prompt (if num (prin1-to-string num)) nil t
		       nil nil default-value))
		  (input-error nil)
		  (invalid-read-syntax nil)
		  (end-of-file nil)))
      (or (funcall pred num) (beep)))
    num))


patched
(defun read-number (prompt &optional integers-only default-value)
  "Read a number from the minibuffer, prompting with PROMPT.
If optional second argument INTEGERS-ONLY is non-nil, accept
 only integer input.
If DEFAULT-VALUE is non-nil, return that if user enters an empty
 line."
  (let ((pred (if integers-only 'integerp 'numberp))
	num)
    (while (not (funcall pred num))
      (setq num (condition-case ()
		    (let ((minibuffer-completion-table nil))
		      (read-from-minibuffer
		       prompt (if num (prin1-to-string num)) nil t
		       nil nil (if (numberp default-value) 
                           (prin1-to-string default-value) 
                         default-value)))
            (input-error nil)
            (invalid-read-syntax nil)
            (end-of-file nil)))
      (or (funcall pred num) (beep)))
    num))

To the regulars here. Would you think I should file a bug report and
send that patch to the maintainers?


Now it get's clear:
(defun demo (&optional num)
  (interactive
   (let* ((defaultval 5)
          (prompt (format "Give me a number (default %d): " defaultval)))
       (list (read-number prompt t defaultval))))
  (insert (format "got %d" num)))


Well read-number should read a number therefor it does it is logical
to have defaultval which is a number too. With the provided patch this
will do.

> 
> If this cannot do this and you were all lying to yourself that
> emacs is customizable, infinitely extensible, then I atleast want
> quick fix for this.
Well I have showdn how easy it is and I even send a suggestion for
improvement. Your attitude show just that you do not have any clue
about Lisp. And I should probably not come up with an obvious
solution. You could have find out yourself if you really had looked
into Emacs Lisp. things which one can extend need soime understanding
about the tools for the extension. You're lacking it.

> 
> At present it does not stap and keeps bugging till I enter 5.
> Why do I have to remember the default when I am giving it that much
> money?
You don't have to, and I showed it. 

Friedrich

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

* Re: HOW TO GIVE A DEFAULT TO A TRULY INTERACTIVE FUNCTION
  2002-10-17 14:35   ` gnuist006
  2002-10-17 15:03     ` Edi Weitz
@ 2002-10-18  7:13     ` Friedrich Dominicus
  1 sibling, 0 replies; 16+ messages in thread
From: Friedrich Dominicus @ 2002-10-18  7:13 UTC (permalink / raw)


gnuist006@hotmail.com (gnuist006) writes:

> Edi Weitz <edi@agharta.de> wrote in message 
> 
> >   (require 'cl)
> > 
> >   (defun* bar (&optional (default 5))
> >     (let* ((default-string (format "%s" default))
> >            (string (read-string "Number: "
> >                                 default-string
> >                                 nil
> >                                 default-string)))
> >       ;; needs check for wrong input
> >       (list (string-to-number string))))
> >   
> >   (defun foo (n)
> >     (interactive
> >      (bar 42))
> >     (insert (format "%s" (* 3 n))))
> 
> Pathetic solution. breaks one, fixes noone.
> It just accepted "u" as the input and did not do number
> validation.
Well people who can read do definitly have an advantage. Edi has
written 
;; needs check ....

So it's either up to you as programmer to check the input or let Emacs
do the job. I have posted a solution in which I used a better suited
input routine. 

Friedrich

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

* Re: HOW TO GIVE A DEFAULT TO A TRULY INTERACTIVE FUNCTION
  2002-10-17 14:22 ` HOW TO GIVE A DEFAULT TO A TRULY INTERACTIVE FUNCTION gnuist006
@ 2002-10-18  7:14   ` Friedrich Dominicus
  0 siblings, 0 replies; 16+ messages in thread
From: Friedrich Dominicus @ 2002-10-18  7:14 UTC (permalink / raw)


gnuist006@hotmail.com (gnuist006) writes:
> 
> > If you want a function that prompts for a number until one is entered 
> > you will have to use an elisp expression type arg-descriptor and do your 
> > own number validation.
Not necessary. 

Regards
Friedrich

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

* Re: HOW TO GIVE A DEFAULT TO A TRULY INTERACTIVE FUNCTION
  2002-10-18  7:07 ` Friedrich Dominicus
@ 2002-10-18  8:05   ` Friedrich Dominicus
  2002-10-18 17:05   ` Kevin Rodgers
  1 sibling, 0 replies; 16+ messages in thread
From: Friedrich Dominicus @ 2002-10-18  8:05 UTC (permalink / raw)


Friedrich Dominicus <frido@q-software-solutions.com> writes:

sorry for following-up myself, but I guess 
>     (while (not (funcall pred num))
>       (setq num (condition-case ()
> 		    (let ((minibuffer-completion-table nil))
> 		      (read-from-minibuffer
> 		       prompt (if num (prin1-to-string num)) nil t
> 		       nil nil (if (numberp default-value)
                            ^^^^^^^ this better would be better integerp
>                            (prin1-to-string default-value) 
>                          default-value)))
>             (input-error nil)
>             (invalid-read-syntax nil)
>             (end-of-file nil)))
>       (or (funcall pred num) (beep)))
>     num))


Friedrich

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

* Re: HOW TO GIVE A DEFAULT TO A TRULY INTERACTIVE FUNCTION
  2002-10-17 14:27   ` gnuist006
  2002-10-17 17:35     ` Barry Margolin
@ 2002-10-18 10:36     ` Oliver Scholz
  1 sibling, 0 replies; 16+ messages in thread
From: Oliver Scholz @ 2002-10-18 10:36 UTC (permalink / raw)


gnuist006@hotmail.com (gnuist006) writes:
[...]
> They say emacs is customizable. OK it is if you only want to do what
> it
> can do, and never try to do what you want to do.

I don't understand this.

[...]
> I prefer it display the default 5 in the message and there is
> SINGLE instance of 5 in the function. ie using defaultval.

AFAIK you can not get this with the spec-string to `interactive', but
there is nothing wrong with an explicit call to `read-from-minibuffer'
in this case. 

(read-from-minibuffer "Hello: " (number-to-string 5))
 
The return value is a string. I only glanced quickly over this thread
(sorry for this!), but I seem to recall that someone already told how
to check if a string contains the decimal representation of a number.

To repeat this until the input is a valid number, use `while'.

[untested:]

(while (not (string-match 
              "^[-0-9]+\\(?:\\\\.[0-9]\\)*$"
              (setq my-number
                (read-from-minibuffer "Hello: "
                                      (number-to-string 5))))))

... or something less cluttered ...

> If this cannot do this and you were all lying to yourself that
> emacs is customizable, infinitely extensible, then I atleast want
> quick fix for this.

I can not parse this.

    -- Oliver

-- 
27 Vendémiaire an 211 de la Révolution
Liberté, Egalité, Fraternité!

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

* Re: HOW TO GIVE A DEFAULT TO A TRULY INTERACTIVE FUNCTION
  2002-10-18  7:07 ` Friedrich Dominicus
  2002-10-18  8:05   ` Friedrich Dominicus
@ 2002-10-18 17:05   ` Kevin Rodgers
  1 sibling, 0 replies; 16+ messages in thread
From: Kevin Rodgers @ 2002-10-18 17:05 UTC (permalink / raw)


Friedrich Dominicus wrote:

> Counterintuitive is (prin1-to-string) I would qualify it as a bug and
> suggest following patch:
...
> patched
> (defun read-number (prompt &optional integers-only default-value)
>   "Read a number from the minibuffer, prompting with PROMPT.
> If optional second argument INTEGERS-ONLY is non-nil, accept
>  only integer input.
> If DEFAULT-VALUE is non-nil, return that if user enters an empty
>  line."
>   (let ((pred (if integers-only 'integerp 'numberp))
> 	num)
>     (while (not (funcall pred num))
>       (setq num (condition-case ()
> 		    (let ((minibuffer-completion-table nil))
> 		      (read-from-minibuffer
> 		       prompt (if num (prin1-to-string num)) nil t
> 		       nil nil (if (numberp default-value) 
>                            (prin1-to-string default-value) 
>                          default-value)))
>             (input-error nil)
>             (invalid-read-syntax nil)
>             (end-of-file nil)))
>       (or (funcall pred num) (beep)))
>     num))
> 
> To the regulars here. Would you think I should file a bug report and
> send that patch to the maintainers?


Yes.  (But send it as a diff.)


> Now it get's clear:
> (defun demo (&optional num)
>   (interactive
>    (let* ((defaultval 5)
>           (prompt (format "Give me a number (default %d): " defaultval)))
>        (list (read-number prompt t defaultval))))
>   (insert (format "got %d" num)))
> 
> Well read-number should read a number therefor it does it is logical
> to have defaultval which is a number too. With the provided patch this
> will do.


Exactly.

-- 
<a href="mailto:&lt;kevinr&#64;ihs.com&gt;">Kevin Rodgers</a>

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

end of thread, other threads:[~2002-10-18 17:05 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.1034620095.15632.help-gnu-emacs@gnu.org>
2002-10-17 14:22 ` HOW TO GIVE A DEFAULT TO A TRULY INTERACTIVE FUNCTION gnuist006
2002-10-18  7:14   ` Friedrich Dominicus
2002-10-14 18:27 Bingham, Jay
  -- strict thread matches above, loose matches on Subject: below --
2002-10-14 16:02 gnuist006
2002-10-14 17:22 ` Joe Casadonte
2002-10-14 20:07 ` Edi Weitz
2002-10-17 14:27   ` gnuist006
2002-10-17 17:35     ` Barry Margolin
2002-10-18 10:36     ` Oliver Scholz
2002-10-17 14:35   ` gnuist006
2002-10-17 15:03     ` Edi Weitz
2002-10-18  5:51       ` huntingdon
2002-10-18  7:13     ` Friedrich Dominicus
2002-10-18  7:07 ` Friedrich Dominicus
2002-10-18  8:05   ` Friedrich Dominicus
2002-10-18 17:05   ` 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).