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

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

Re: interact with user


From: Xah Lee
Subject: Re: interact with user
Date: Wed, 26 Nov 2008 05:46:05 -0800 (PST)
User-agent: G2/1.0

On Nov 26, 5:39 am, Xend <firperf...@gmail.com> wrote:
> how can i get input from user (interact with user)in function with GNU/
> emacs,the function interactive only work to parse arguments. are there
> bulitin emacs function or i should programming to do it?

btw, The “interactive” function has a general form like this:
“(interactive lispExp)”, where in the lispExp is any lisp code, among
which you can use any lisp function that query user. The return value
of lispExp becomes the argument of your command.

more answer here:

• Emacs Lisp Idioms
  http://xahlee.org/emacs/elisp_idioms.html

here's relevant excerpt:

----------------------------------------
Prompting User for Input
Get User Input as Arguments

Idiom for promping user for input as the argument to your command.

Use this code “(interactive "‹code›‹promp string›")”. Example:

(defun query-friends-phone (name)
  "..."
  (interactive "sEnter friend's name: ")
  (message "Name: %s" name)
)

What the “(interactive "sEnter friend's name:")” does is that it will
ask user to input something, taken as string, and becomes the value of
your command's parameter.

The “interactive” can be used to get other types of input. Here are
some basic examples of using “interactive”.

    * “(interactive)” makes the function available thru interactive
use, where user can call it with execute-extended-command (M-x).
    * “(interactive "s")” will prompt user for input, taken as string,
as argument to the function.
    * “(interactive "n")” will prompt user for input, taken as number,
as argument to the function.

The prompt text can follow the single-letter code string.

If your function takes multiple inputs, you can promp user multiple
times, using a single call “interactive”, by joining the promp code
string with “\n” in between, like this:

(defun query-friends-phone (name age)
  "..."
  (interactive "sEnter friend's name: \nnEnter friend's age: ")
  (message "Name: %s, Age: %d" name age)
)

(info "(elisp)Defining Commands")
Query For User Input

The “(interactive ...)” clause is useful for filling parameters of
your command. But sometimes you need to promp user in the middle of a
program. For example: “Make change to this file?”. You can use “y-or-n-
p” function. Like this:

(if (y-or-n-p "Do it?")
    (progn
      ;; code to do something here
    )
  (progn
    ;; code if user answered no.
  )
)

The y-or-n-p will ask the user to type a “y” or “n” character. You can
also use “yes-and-no-p”, which forces user to type full “yes” and “no”
to answer. This can be used for example when you want to confirm
deleting files.

Yes-or-No-Queries

If you need more general mechanism for getting user input, you'll need
to use “read-from-minibuffer”. This can be useful for example, when
you want use features like keyword completion or input history.

(info "(elisp)Text from Minibuffer")

  Xah
∑ http://xahlee.org/

reply via email to

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