🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.emacs.bug
Article #99591

y-or-n-p

#99591
From: Roland McGrath
Date: Sat, 21 Jul 1990 05:42
49 lines
2189 bytes
It would be nice if RET made y-or-n-p succeed.

In fact, this would be nice:

(defvar y-or-n-alist '((?y . t)
		       (?n . nil)
		       (?Y . t)
		       (?N . nil)
		       (?  . t)
		       (?\^? . nil)
		       (?\r . t))
  "Alist of acceptable responses for `y-or-n-p'.
Each element is a cons (CHAR . VALUE).  If CHAR is entered in response to
`y-or-n-p', VALUE is returned.  The first CHAR with a non-nil VALUE and the
first CHAR with a nil VALUE in the alist are used to tell the user what he
can answer.")

(defun y-or-n-p (prompt)
  "Ask user a \"y or n\" question.  Return non-nil if answer is true.
No confirmation of the answer is requested; a single character is enough.
Also accepts SPC or RET for yes, and DEL for no.
See `y-or-n-alist'."
  (let ((alist y-or-n-alist)
	yes no answer dinged)
    (while (and alist (or (null yes) (null no)))
      (or yes
	  (if (cdr (car alist))
	      (setq yes (car (car alist)))))
      (or no
	  (or (cdr (car alist))
	      (setq no (car (car alist)))))
      (setq alist (cdr alist)))
    ;; Sanity checking.
    (or (integerp yes)
	(setq yes ?y
	      y-or-n-alist (cons '(?y . t) y-or-n-alist)))
    (or (integerp no)
	(setq no ?n
	      y-or-n-alist (cons '(?n . nil) y-or-n-alist)))
    (setq prompt (format "%s(%c or %c) " prompt yes no))
  (while (not (prog2
	       (message "%s" prompt)
	       (setq answer (assq (read-char) y-or-n-alist))))
    (ding nil)
    (discard-input)
    (or dinged
	(setq prompt (format "Please answer %c or %c. %s" yes no prompt)
	      dinged t)))
  (cdr answer)))
Technical Details
Message-ID: <9007210542.AA03768@paris.Berkeley.EDU>
Path: arc1-ca.pugleaf.net!gmdzi!unido!mcsun!uunet!ogicse!ucsd!tut.cis.ohio-state.edu!PARIS.BERKELEY.EDU!mcgrath

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: fb2f1,2066d3d1ed47cf7f,start
X-Google-Attributes: gidfb2f1,public
X-Google-ArrivalTime: 1990-07-21 00:49:28 PST
Path: arc1-ca.pugleaf.net!gmdzi!unido!mcsun!uunet!ogicse!ucsd!tut.cis.ohio-state.edu!PARIS.BERKELEY.EDU!mcgrath
From: mcgrath@PARIS.BERKELEY.EDU (Roland McGrath)
Newsgroups: gnu.emacs.bug
Subject: y-or-n-p
Message-ID: <9007210542.AA03768@paris.Berkeley.EDU>
Date: 21 Jul 90 05:42:49 GMT
Sender: daemon@tut.cis.ohio-state.edu
Distribution: gnu
Organization: GNUs Not Usenet
Lines: 49
Posted: Sat Jul 21 06:42:49 1990