all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Xah Lee <xahlee@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: interact with user
Date: Wed, 26 Nov 2008 05:46:05 -0800 (PST)	[thread overview]
Message-ID: <5672ef09-72a1-4df4-851c-a416d05cb9c0@s1g2000prg.googlegroups.com> (raw)
In-Reply-To: 5e7d7456-b98e-4750-8f46-f05568f1db0c@a29g2000pra.googlegroups.com

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	other threads:[~2008-11-26 13:46 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-11-26 13:39 interact with user Xend
2008-11-26 13:46 ` Xah Lee [this message]
2008-11-28  2:23   ` Xend
2008-11-26 15:25 ` Drew Adams
     [not found] ` <mailman.1294.1227713142.26697.help-gnu-emacs@gnu.org>
2008-11-28  2:22   ` Xend

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5672ef09-72a1-4df4-851c-a416d05cb9c0@s1g2000prg.googlegroups.com \
    --to=xahlee@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.