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