* rlogin.el (rlogin): recognise the `-l user' option to rlogin and
[bpt/emacs.git] / lisp / map-ynp.el
CommitLineData
6594deb0
ER
1;;; map-ynp.el --- General-purpose boolean question-asker.
2
c5068ec5 3;;; Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
eea8d4ef 4
e5167999 5;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
fd7fa35a 6;; Keywords: lisp, extensions
e5167999 7
60205e0b
RM
8;;; This program is free software; you can redistribute it and/or modify
9;;; it under the terms of the GNU General Public License as published by
e5167999 10;;; the Free Software Foundation; either version 2, or (at your option)
60205e0b
RM
11;;; any later version.
12;;;
13;;; This program is distributed in the hope that it will be useful,
14;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16;;; GNU General Public License for more details.
17;;;
18;;; A copy of the GNU General Public License can be obtained from this
19;;; program's author (send electronic mail to roland@ai.mit.edu) or from
20;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
21;;; 02139, USA.
e5167999
ER
22
23;;; Commentary:
24
60205e0b
RM
25;;; map-y-or-n-p is a general-purpose question-asking function.
26;;; It asks a series of y/n questions (a la y-or-n-p), and decides to
27;;; applies an action to each element of a list based on the answer.
28;;; The nice thing is that you also get some other possible answers
29;;; to use, reminiscent of query-replace: ! to answer y to all remaining
30;;; questions; ESC or q to answer n to all remaining questions; . to answer
31;;; y once and then n for the remainder; and you can get help with C-h.
32
e5167999
ER
33;;; Code:
34
60205e0b 35;;;###autoload
fd95709e
RM
36(defun map-y-or-n-p (prompter actor list &optional help action-alist
37 no-cursor-in-echo-area)
60205e0b 38 "Ask a series of boolean questions.
907482b9 39Takes args PROMPTER ACTOR LIST, and optional args HELP and ACTION-ALIST.
60205e0b
RM
40
41LIST is a list of objects, or a function of no arguments to return the next
42object or nil.
43
9fd54390 44If PROMPTER is a string, the prompt is \(format PROMPTER OBJECT\). If not
03759fe0
RM
45a string, PROMPTER is a function of one arg (an object from LIST), which
46returns a string to be used as the prompt for that object. If the return
47value is not a string, it is eval'd to get the answer; it may be nil to
48ignore the object, t to act on the object without asking the user, or a
49form to do a more complex prompt.
50
60205e0b
RM
51ACTOR is a function of one arg (an object from LIST),
52which gets called with each object that the user answers `yes' for.
53
54If HELP is given, it is a list (OBJECT OBJECTS ACTION),
55where OBJECT is a string giving the singular noun for an elt of LIST;
56OBJECTS is the plural noun for elts of LIST, and ACTION is a transitive
57verb describing ACTOR. The default is \(\"object\" \"objects\" \"act on\"\).
58
59At the prompts, the user may enter y, Y, or SPC to act on that object;
60n, N, or DEL to skip that object; ! to act on all following objects;
61ESC or q to exit (skip all following objects); . (period) to act on the
62current object and then exit; or \\[help-command] to get help.
63
907482b9
RM
64If ACTION-ALIST is given, it is an alist (KEY FUNCTION HELP) of extra keys
65that will be accepted. KEY is a character; FUNCTION is a function of one
66arg (an object from LIST); HELP is a string. When the user hits KEY,
67FUNCTION is called. If it returns non-nil, the object is considered
68\"acted upon\", and the next object from LIST is processed. If it returns
69nil, the prompt is repeated for the same object.
70
fd95709e
RM
71Final optional argument NO-CURSOR-IN-ECHO-AREA non-nil says not to set
72`cursor-in-echo-area' while prompting.
73
0a2eb25e
RS
74This function uses `query-replace-map' to define the standard responses,
75but not all of the responses which `query-replace' understands
76are meaningful here.
77
60205e0b 78Returns the number of actions taken."
c5068ec5
RM
79 (let* ((actions 0)
80 user-keys mouse-event map prompt char elt tail def
81 delayed-switch-frame
fba98516 82 (next (if (or (and list (symbolp list))
aa228418 83 (subrp list)
dbc4e1c1 84 (byte-code-function-p list)
aa228418
JB
85 (and (consp list)
86 (eq (car list) 'lambda)))
87 (function (lambda ()
88 (setq elt (funcall list))))
89 (function (lambda ()
90 (if list
91 (progn
92 (setq elt (car list)
93 list (cdr list))
94 t)
95 nil))))))
c5068ec5
RM
96 (if (listp last-nonmenu-event)
97 ;; Make a list describing a dialog box.
98 (let ((object (capitalize (nth 0 help)))
99 (objects (capitalize (nth 1 help)))
100 (action (capitalize (nth 2 help))))
101 (setq map (` (("Yes" . act) ("No" . skip) ("Quit" . exit)
102 ((, (if help (concat action " " object " And Quit")
103 "Do it and Quit")) . act-and-exit)
104 ((, (if help (concat action " All " objects)
105 "Do All")) . automatic)
106 (,@ (mapcar (lambda (elt)
230ac7f6 107 (cons (capitalize (nth 2 elt))
c5068ec5
RM
108 (vector (nth 1 elt))))
109 action-alist))))
110 mouse-event last-nonmenu-event))
111 (setq user-keys (if action-alist
112 (concat (mapconcat (function
113 (lambda (elt)
114 (key-description
115 (char-to-string (car elt)))))
116 action-alist ", ")
117 " ")
118 "")
119 ;; Make a map that defines each user key as a vector containing
120 ;; its definition.
121 map (cons 'keymap
122 (append (mapcar (lambda (elt)
123 (cons (car elt) (vector (nth 1 elt))))
124 action-alist)
125 query-replace-map))))
3eea8aa2
JB
126 (unwind-protect
127 (progn
128 (if (stringp prompter)
129 (setq prompter (` (lambda (object)
130 (format (, prompter) object)))))
131 (while (funcall next)
132 (setq prompt (funcall prompter elt))
133 (if (stringp prompt)
134 (progn
135 (setq quit-flag nil)
136 ;; Prompt the user about this object.
c5068ec5
RM
137 (if mouse-event
138 (setq def (or (x-popup-dialog mouse-event
6f4387dd 139 (cons prompt map))
c5068ec5
RM
140 'quit))
141 ;; Prompt in the echo area.
142 (let ((cursor-in-echo-area (not no-cursor-in-echo-area)))
143 (message "%s(y, n, !, ., q, %sor %s) "
144 prompt user-keys
145 (key-description (vector help-char)))
146 (setq char (read-event)))
147 ;; Show the answer to the question.
148 (message "%s(y, n, !, ., q, %sor %s) %s"
3eea8aa2 149 prompt user-keys
c5068ec5
RM
150 (key-description (vector help-char))
151 (single-key-description char))
152 (setq def (lookup-key map (vector char))))
3eea8aa2
JB
153 (cond ((eq def 'exit)
154 (setq next (function (lambda () nil))))
155 ((eq def 'act)
156 ;; Act on the object.
157 (funcall actor elt)
158 (setq actions (1+ actions)))
159 ((eq def 'skip)
160 ;; Skip the object.
161 )
162 ((eq def 'act-and-exit)
163 ;; Act on the object and then exit.
0af46a8b 164 (funcall actor elt)
3eea8aa2
JB
165 (setq actions (1+ actions)
166 next (function (lambda () nil))))
167 ((eq def 'quit)
168 (setq quit-flag t)
169 (setq next (` (lambda ()
170 (setq next '(, next))
171 '(, elt)))))
172 ((eq def 'automatic)
173 ;; Act on this and all following objects.
174 (if (eval (funcall prompter elt))
175 (progn
176 (funcall actor elt)
177 (setq actions (1+ actions))))
178 (while (funcall next)
179 (if (eval (funcall prompter elt))
180 (progn
181 (funcall actor elt)
182 (setq actions (1+ actions))))))
183 ((eq def 'help)
184 (with-output-to-temp-buffer "*Help*"
185 (princ
186 (let ((object (if help (nth 0 help) "object"))
187 (objects (if help (nth 1 help) "objects"))
188 (action (if help (nth 2 help) "act on")))
189 (concat
190 (format "Type SPC or `y' to %s the current %s;
0a2eb25e
RS
191DEL or `n' to skip the current %s;
192! to %s all remaining %s;
193ESC or `q' to exit;\n"
3eea8aa2 194 action object object action objects)
c5068ec5
RM
195 (mapconcat (function
196 (lambda (elt)
197 (format "%c to %s"
198 (nth 0 elt)
199 (nth 2 elt))))
200 action-alist
201 ";\n")
202 (if action-alist ";\n")
203 (format "or . (period) to %s \
0a2eb25e 204the current %s and exit."
c5068ec5 205 action object)))))
3eea8aa2
JB
206
207 (setq next (` (lambda ()
208 (setq next '(, next))
209 '(, elt)))))
210 ((vectorp def)
211 ;; A user-defined key.
212 (if (funcall (aref def 0) elt) ;Call its function.
213 ;; The function has eaten this object.
214 (setq actions (1+ actions))
215 ;; Regurgitated; try again.
216 (setq next (` (lambda ()
217 (setq next '(, next))
218 '(, elt))))))
219 ((and (consp char)
220 (eq (car char) 'switch-frame))
221 ;; switch-frame event. Put it off until we're done.
222 (setq delayed-switch-frame char)
223 (setq next (` (lambda ()
224 (setq next '(, next))
225 '(, elt)))))
226 (t
227 ;; Random char.
228 (message "Type %s for help."
b26f0e4e 229 (key-description (vector help-char)))
3eea8aa2
JB
230 (beep)
231 (sit-for 1)
232 (setq next (` (lambda ()
233 (setq next '(, next))
234 '(, elt)))))))
235 (if (eval prompt)
236 (progn
237 (funcall actor elt)
238 (setq actions (1+ actions)))))))
239 (if delayed-switch-frame
240 (setq unread-command-events
241 (cons delayed-switch-frame unread-command-events))))
60205e0b
RM
242 ;; Clear the last prompt from the minibuffer.
243 (message "")
244 ;; Return the number of actions that were taken.
245 actions))
6594deb0
ER
246
247;;; map-ynp.el ends here