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

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

Re: HOW TO GIVE A DEFAULT TO A TRULY INTERACTIVE FUNCTION


From: Friedrich Dominicus
Subject: Re: HOW TO GIVE A DEFAULT TO A TRULY INTERACTIVE FUNCTION
Date: 18 Oct 2002 09:07:44 +0200
User-agent: Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.4 (Common Lisp)

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



reply via email to

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