Function: yes-or-no-p

yes-or-no-p is a function defined in fns.c.

Signature

(yes-or-no-p PROMPT)

Documentation

Ask user a yes-or-no question.

Return t if answer is yes, and nil if the answer is no.

PROMPT is the string to display to ask the question; yes-or-no-p appends yes-or-no-prompt (default "(yes or no) ") to it. If PROMPT is a non-empty string, and it ends with a non-space character, a space character will be appended to it.

The user must confirm the answer with RET, and can edit it until it has been confirmed.

If the use-short-answers variable is non-nil, instead of asking for
"yes" or "no", this function will ask for "y" or "n" (and
ignore the value of yes-or-no-prompt).

If dialog boxes are supported, this function will use a dialog box if use-dialog-box is non-nil and the last input event was produced by a mouse, or by some window-system gesture, or via a menu.

View in manual

Probably introduced at or before Emacs version 19.23.

Source Code

// Defined in /usr/src/emacs/src/fns.c
{
  Lisp_Object ans, val;

  CHECK_STRING (prompt);

  if (!NILP (last_input_event)
      && (CONSP (last_nonmenu_event)
	  || (NILP (last_nonmenu_event) && CONSP (last_input_event))
	  || (val = find_symbol_value (Qfrom__tty_menu_p),
	      (!NILP (val) && !BASE_EQ (val, Qunbound))))
      && use_dialog_box)
    {
      Lisp_Object pane, menu, obj;
      redisplay_preserve_echo_area (4);
      pane = list2 (Fcons (build_string ("Yes"), Qt),
		    Fcons (build_string ("No"), Qnil));
      menu = Fcons (prompt, pane);
      obj = Fx_popup_dialog (Qt, menu, Qnil);
      return obj;
    }

  if (use_short_answers)
    return calln (Qy_or_n_p, prompt);

  ptrdiff_t promptlen = SCHARS (prompt);
  bool prompt_ends_in_nonspace
    = (0 < promptlen
       && !blankp (XFIXNAT (Faref (prompt, make_fixnum (promptlen - 1)))));
  AUTO_STRING (space_string, " ");
  prompt = CALLN (Fconcat, prompt,
		  prompt_ends_in_nonspace ? space_string : empty_unibyte_string,
		  Vyes_or_no_prompt);

  specpdl_ref count = SPECPDL_INDEX ();
  specbind (Qenable_recursive_minibuffers, Qt);
  /* Preserve the actual command that eventually called `yes-or-no-p'
     (otherwise `repeat' will be repeating `exit-minibuffer').  */
  specbind (Qreal_this_command, Vreal_this_command);

  while (1)
    {
      ans = Fdowncase (Fread_from_minibuffer (prompt, Qnil, Qnil, Qnil,
					      Qyes_or_no_p_history, Qnil,
					      Qnil));
      if (SCHARS (ans) == 3 && !strcmp (SSDATA (ans), "yes"))
	return unbind_to (count, Qt);
      if (SCHARS (ans) == 2 && !strcmp (SSDATA (ans), "no"))
	return unbind_to (count, Qnil);

      Fding (Qnil);
      Fdiscard_input ();
      message1 ("Please answer yes or no.");
      Fsleep_for (make_fixnum (2), Qnil);
    }
}