(replace_buffer_in_all_windows):
[bpt/emacs.git] / lisp / map-ynp.el
CommitLineData
6594deb0
ER
1;;; map-ynp.el --- General-purpose boolean question-asker.
2
b578f267 3;; Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
eea8d4ef 4
e5167999 5;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
fd7fa35a 6;; Keywords: lisp, extensions
e5167999 7
b578f267
EN
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 2, or (at your option)
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
e5167999
ER
24
25;;; Commentary:
26
b578f267
EN
27;; map-y-or-n-p is a general-purpose question-asking function.
28;; It asks a series of y/n questions (a la y-or-n-p), and decides to
68e3e5f5 29;; apply an action to each element of a list based on the answer.
b578f267
EN
30;; The nice thing is that you also get some other possible answers
31;; to use, reminiscent of query-replace: ! to answer y to all remaining
32;; questions; ESC or q to answer n to all remaining questions; . to answer
33;; y once and then n for the remainder; and you can get help with C-h.
60205e0b 34
e5167999
ER
35;;; Code:
36
fd95709e
RM
37(defun map-y-or-n-p (prompter actor list &optional help action-alist
38 no-cursor-in-echo-area)
60205e0b 39 "Ask a series of boolean questions.
907482b9 40Takes args PROMPTER ACTOR LIST, and optional args HELP and ACTION-ALIST.
60205e0b
RM
41
42LIST is a list of objects, or a function of no arguments to return the next
43object or nil.
44
9fd54390 45If PROMPTER is a string, the prompt is \(format PROMPTER OBJECT\). If not
03759fe0
RM
46a string, PROMPTER is a function of one arg (an object from LIST), which
47returns a string to be used as the prompt for that object. If the return
933fb957
RM
48value is not a string, it may be nil to ignore the object or non-nil to act
49on the object without asking the user.
03759fe0 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
5233c7f5
RS
81 ;; Non-nil means we should use mouse menus to ask.
82 use-menus
c5068ec5 83 delayed-switch-frame
fba98516 84 (next (if (or (and list (symbolp list))
aa228418 85 (subrp list)
dbc4e1c1 86 (byte-code-function-p list)
aa228418
JB
87 (and (consp list)
88 (eq (car list) 'lambda)))
89 (function (lambda ()
90 (setq elt (funcall list))))
91 (function (lambda ()
92 (if list
93 (progn
94 (setq elt (car list)
95 list (cdr list))
96 t)
97 nil))))))
c5068ec5
RM
98 (if (listp last-nonmenu-event)
99 ;; Make a list describing a dialog box.
100 (let ((object (capitalize (nth 0 help)))
101 (objects (capitalize (nth 1 help)))
102 (action (capitalize (nth 2 help))))
103 (setq map (` (("Yes" . act) ("No" . skip) ("Quit" . exit)
104 ((, (if help (concat action " " object " And Quit")
105 "Do it and Quit")) . act-and-exit)
106 ((, (if help (concat action " All " objects)
107 "Do All")) . automatic)
108 (,@ (mapcar (lambda (elt)
230ac7f6 109 (cons (capitalize (nth 2 elt))
c5068ec5
RM
110 (vector (nth 1 elt))))
111 action-alist))))
5233c7f5 112 use-menus t
c5068ec5
RM
113 mouse-event last-nonmenu-event))
114 (setq user-keys (if action-alist
115 (concat (mapconcat (function
116 (lambda (elt)
117 (key-description
118 (char-to-string (car elt)))))
119 action-alist ", ")
120 " ")
121 "")
122 ;; Make a map that defines each user key as a vector containing
123 ;; its definition.
124 map (cons 'keymap
125 (append (mapcar (lambda (elt)
126 (cons (car elt) (vector (nth 1 elt))))
127 action-alist)
128 query-replace-map))))
3eea8aa2
JB
129 (unwind-protect
130 (progn
131 (if (stringp prompter)
132 (setq prompter (` (lambda (object)
133 (format (, prompter) object)))))
134 (while (funcall next)
135 (setq prompt (funcall prompter elt))
933fb957
RM
136 (cond ((stringp prompt)
137 ;; Prompt the user about this object.
138 (setq quit-flag nil)
5233c7f5
RS
139 (if use-menus
140 (setq def (or (x-popup-dialog (or mouse-event use-menus)
933fb957
RM
141 (cons prompt map))
142 'quit))
143 ;; Prompt in the echo area.
144 (let ((cursor-in-echo-area (not no-cursor-in-echo-area))
145 (message-log-max nil))
146 (message "%s(y, n, !, ., q, %sor %s) "
147 prompt user-keys
148 (key-description (vector help-char)))
55b4b20a
RS
149 (if minibuffer-auto-raise
150 (raise-frame (window-frame (minibuffer-window))))
933fb957
RM
151 (setq char (read-event))
152 ;; Show the answer to the question.
153 (message "%s(y, n, !, ., q, %sor %s) %s"
154 prompt user-keys
155 (key-description (vector help-char))
156 (single-key-description char)))
157 (setq def (lookup-key map (vector char))))
158 (cond ((eq def 'exit)
159 (setq next (function (lambda () nil))))
160 ((eq def 'act)
161 ;; Act on the object.
162 (funcall actor elt)
163 (setq actions (1+ actions)))
164 ((eq def 'skip)
165 ;; Skip the object.
166 )
167 ((eq def 'act-and-exit)
168 ;; Act on the object and then exit.
169 (funcall actor elt)
170 (setq actions (1+ actions)
171 next (function (lambda () nil))))
172 ((or (eq def 'quit) (eq def 'exit-prefix))
173 (setq quit-flag t)
174 (setq next (` (lambda ()
175 (setq next '(, next))
176 '(, elt)))))
177 ((eq def 'automatic)
178 ;; Act on this and all following objects.
179 (if (funcall prompter elt)
180 (progn
181 (funcall actor elt)
182 (setq actions (1+ actions))))
183 (while (funcall next)
184 (if (funcall prompter elt)
185 (progn
186 (funcall actor elt)
187 (setq actions (1+ actions))))))
188 ((eq def 'help)
189 (with-output-to-temp-buffer "*Help*"
190 (princ
191 (let ((object (if help (nth 0 help) "object"))
192 (objects (if help (nth 1 help) "objects"))
193 (action (if help (nth 2 help) "act on")))
194 (concat
195 (format "Type SPC or `y' to %s the current %s;
0a2eb25e
RS
196DEL or `n' to skip the current %s;
197! to %s all remaining %s;
198ESC or `q' to exit;\n"
933fb957
RM
199 action object object action objects)
200 (mapconcat (function
201 (lambda (elt)
202 (format "%c to %s"
203 (nth 0 elt)
204 (nth 2 elt))))
205 action-alist
206 ";\n")
207 (if action-alist ";\n")
208 (format "or . (period) to %s \
0a2eb25e 209the current %s and exit."
933fb957
RM
210 action object))))
211 (save-excursion
212 (set-buffer standard-output)
213 (help-mode)))
214
215 (setq next (` (lambda ()
216 (setq next '(, next))
217 '(, elt)))))
218 ((vectorp def)
219 ;; A user-defined key.
220 (if (funcall (aref def 0) elt) ;Call its function.
221 ;; The function has eaten this object.
222 (setq actions (1+ actions))
223 ;; Regurgitated; try again.
224 (setq next (` (lambda ()
225 (setq next '(, next))
226 '(, elt))))))
227 ((and (consp char)
228 (eq (car char) 'switch-frame))
229 ;; switch-frame event. Put it off until we're done.
230 (setq delayed-switch-frame char)
231 (setq next (` (lambda ()
232 (setq next '(, next))
233 '(, elt)))))
234 (t
235 ;; Random char.
236 (message "Type %s for help."
237 (key-description (vector help-char)))
238 (beep)
239 (sit-for 1)
240 (setq next (` (lambda ()
241 (setq next '(, next))
242 '(, elt)))))))
243 (prompt
244 (funcall actor elt)
245 (setq actions (1+ actions))))))
3eea8aa2
JB
246 (if delayed-switch-frame
247 (setq unread-command-events
248 (cons delayed-switch-frame unread-command-events))))
60205e0b 249 ;; Clear the last prompt from the minibuffer.
a66839ce
KH
250 (let ((message-log-max nil))
251 (message ""))
60205e0b
RM
252 ;; Return the number of actions that were taken.
253 actions))
6594deb0
ER
254
255;;; map-ynp.el ends here