summaryrefslogtreecommitdiff
path: root/devdocs/elisp/yes_002dor_002dno-queries.html
blob: d293fd09ea5a153a856d7802813600b2cd38fa48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 <h3 class="section">Yes-or-No Queries</h3>    <p>This section describes functions used to ask the user a yes-or-no question. The function <code>y-or-n-p</code> can be answered with a single character; it is useful for questions where an inadvertent wrong answer will not have serious consequences. <code>yes-or-no-p</code> is suitable for more momentous questions, since it requires three or four characters to answer. </p> <p>If either of these functions is called in a command that was invoked using the mouse—more precisely, if <code>last-nonmenu-event</code> (see <a href="command-loop-info">Command Loop Info</a>) is either <code>nil</code> or a list—then it uses a dialog box or pop-up menu to ask the question. Otherwise, it uses keyboard input. You can force use either of the mouse or of keyboard input by binding <code>last-nonmenu-event</code> to a suitable value around the call. </p> <p>Both <code>yes-or-no-p</code> and <code>y-or-n-p</code> use the minibuffer. </p> <dl> <dt id="y-or-n-p">Function: <strong>y-or-n-p</strong> <em>prompt</em>
</dt> <dd>
<p>This function asks the user a question, expecting input in the minibuffer. It returns <code>t</code> if the user types <kbd>y</kbd>, <code>nil</code> if the user types <kbd>n</kbd>. This function also accepts <tt class="key">SPC</tt> to mean yes and <tt class="key">DEL</tt> to mean no. It accepts <kbd>C-]</kbd> and <kbd>C-g</kbd> to quit, because the question uses the minibuffer and for that reason the user might try to use <kbd>C-]</kbd> to get out. The answer is a single character, with no <tt class="key">RET</tt> needed to terminate it. Upper and lower case are equivalent. </p> <p>“Asking the question” means printing <var>prompt</var> in the minibuffer, followed by the string ‘<samp>(y or n) </samp>’. If the input is not one of the expected answers (<kbd>y</kbd>, <kbd>n</kbd>, <kbd><span class="key">SPC</span></kbd>, <kbd><span class="key">DEL</span></kbd>, or something that quits), the function responds ‘<samp>Please answer y or n.</samp>’, and repeats the request. </p> <p>This function actually uses the minibuffer, but does not allow editing of the answer. The cursor moves to the minibuffer while the question is being asked. </p> <p>The answers and their meanings, even ‘<samp>y</samp>’ and ‘<samp>n</samp>’, are not hardwired, and are specified by the keymap <code>query-replace-map</code> (see <a href="search-and-replace">Search and Replace</a>). In particular, if the user enters the special responses <code>recenter</code>, <code>scroll-up</code>, <code>scroll-down</code>, <code>scroll-other-window</code>, or <code>scroll-other-window-down</code> (respectively bound to <kbd>C-l</kbd>, <kbd>C-v</kbd>, <kbd>M-v</kbd>, <kbd>C-M-v</kbd> and <kbd>C-M-S-v</kbd> in <code>query-replace-map</code>), this function performs the specified window recentering or scrolling operation, and poses the question again. </p> <p>If you bind <code>help-form</code> (see <a href="help-functions">Help Functions</a>) to a non-<code>nil</code> value while calling <code>y-or-n-p</code>, then pressing <code>help-char</code> causes it to evaluate <code>help-form</code> and display the result. <code>help-char</code> is automatically added to <var>prompt</var>. </p>
</dd>
</dl> <dl> <dt id="y-or-n-p-with-timeout">Function: <strong>y-or-n-p-with-timeout</strong> <em>prompt seconds default</em>
</dt> <dd><p>Like <code>y-or-n-p</code>, except that if the user fails to answer within <var>seconds</var> seconds, this function stops waiting and returns <var>default</var>. It works by setting up a timer; see <a href="timers">Timers</a>. The argument <var>seconds</var> should be a number. </p></dd>
</dl> <dl> <dt id="yes-or-no-p">Function: <strong>yes-or-no-p</strong> <em>prompt</em>
</dt> <dd>
<p>This function asks the user a question, expecting input in the minibuffer. It returns <code>t</code> if the user enters ‘<samp>yes</samp>’, <code>nil</code> if the user types ‘<samp>no</samp>’. The user must type <tt class="key">RET</tt> to finalize the response. Upper and lower case are equivalent. </p> <p><code>yes-or-no-p</code> starts by displaying <var>prompt</var> in the minibuffer, followed by ‘<samp>(yes or no) </samp>’. The user must type one of the expected responses; otherwise, the function responds ‘<samp>Please answer yes or no.</samp>’, waits about two seconds and repeats the request. </p> <p><code>yes-or-no-p</code> requires more work from the user than <code>y-or-n-p</code> and is appropriate for more crucial decisions. </p> <p>Here is an example: </p> <div class="example"> <pre class="example">(yes-or-no-p "Do you really want to remove everything?")

;; <span class="roman">After evaluation of the preceding expression,</span>
;;   <span class="roman">the following prompt appears,</span>
;;   <span class="roman">with an empty minibuffer:</span>
</pre>

<pre class="example">---------- Buffer: minibuffer ----------
Do you really want to remove everything? (yes or no)
---------- Buffer: minibuffer ----------
</pre>
</div> <p>If the user first types <kbd>y <span class="key">RET</span></kbd>, which is invalid because this function demands the entire word ‘<samp>yes</samp>’, it responds by displaying these prompts, with a brief pause between them: </p> <div class="example"> <pre class="example">---------- Buffer: minibuffer ----------
Please answer yes or no.
Do you really want to remove everything? (yes or no)
---------- Buffer: minibuffer ----------
</pre>
</div> </dd>
</dl><div class="_attribution">
  <p class="_attribution-p">
    Copyright &copy; 1990-1996, 1998-2022 Free Software Foundation, Inc. <br>Licensed under the GNU GPL license.<br>
    <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Yes_002dor_002dNo-Queries.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Yes_002dor_002dNo-Queries.html</a>
  </p>
</div>