(parse_charset_map): Remove an unused variable.
[bpt/emacs.git] / lisp / map-ynp.el
CommitLineData
55535639 1;;; map-ynp.el --- general-purpose boolean question-asker
6594deb0 2
b9b1c3a9 3;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 2000 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))))))
b9b1c3a9
DL
99 (if (and (listp last-nonmenu-event)
100 use-dialog-box)
c5068ec5 101 ;; Make a list describing a dialog box.
b9b1c3a9
DL
102 (let ((object (if help (capitalize (nth 0 help))))
103 (objects (if help (capitalize (nth 1 help))))
104 (action (if help (capitalize (nth 2 help)))))
105 (setq map `(("Yes" . act) ("No" . skip) ("Quit" . exit)
106 (,(if help (concat action " " object " And Quit")
107 "Do it and Quit") . act-and-exit)
108 (,(if help (concat action " All " objects)
109 "Do All") . automatic)
110 ,@(mapcar (lambda (elt)
111 (cons (capitalize (nth 2 elt))
112 (vector (nth 1 elt))))
113 action-alist))
5233c7f5 114 use-menus t
4f837d3b 115 mouse-event last-nonmenu-event))
c5068ec5
RM
116 (setq user-keys (if action-alist
117 (concat (mapconcat (function
118 (lambda (elt)
119 (key-description
120 (char-to-string (car elt)))))
121 action-alist ", ")
122 " ")
123 "")
124 ;; Make a map that defines each user key as a vector containing
125 ;; its definition.
126 map (cons 'keymap
127 (append (mapcar (lambda (elt)
128 (cons (car elt) (vector (nth 1 elt))))
129 action-alist)
130 query-replace-map))))
3eea8aa2
JB
131 (unwind-protect
132 (progn
133 (if (stringp prompter)
b9b1c3a9
DL
134 (setq prompter `(lambda (object)
135 (format ,prompter object))))
3eea8aa2
JB
136 (while (funcall next)
137 (setq prompt (funcall prompter elt))
933fb957
RM
138 (cond ((stringp prompt)
139 ;; Prompt the user about this object.
140 (setq quit-flag nil)
5233c7f5
RS
141 (if use-menus
142 (setq def (or (x-popup-dialog (or mouse-event use-menus)
933fb957
RM
143 (cons prompt map))
144 'quit))
145 ;; Prompt in the echo area.
146 (let ((cursor-in-echo-area (not no-cursor-in-echo-area))
147 (message-log-max nil))
148 (message "%s(y, n, !, ., q, %sor %s) "
149 prompt user-keys
150 (key-description (vector help-char)))
55b4b20a
RS
151 (if minibuffer-auto-raise
152 (raise-frame (window-frame (minibuffer-window))))
3f72fac8
PR
153 (while (progn
154 (setq char (read-event))
155 ;; If we get -1, from end of keyboard
156 ;; macro, try again.
03d50ed6 157 (equal char -1)))
933fb957
RM
158 ;; Show the answer to the question.
159 (message "%s(y, n, !, ., q, %sor %s) %s"
160 prompt user-keys
161 (key-description (vector help-char))
162 (single-key-description char)))
163 (setq def (lookup-key map (vector char))))
164 (cond ((eq def 'exit)
165 (setq next (function (lambda () nil))))
166 ((eq def 'act)
167 ;; Act on the object.
168 (funcall actor elt)
169 (setq actions (1+ actions)))
170 ((eq def 'skip)
171 ;; Skip the object.
172 )
173 ((eq def 'act-and-exit)
174 ;; Act on the object and then exit.
175 (funcall actor elt)
176 (setq actions (1+ actions)
177 next (function (lambda () nil))))
178 ((or (eq def 'quit) (eq def 'exit-prefix))
179 (setq quit-flag t)
b9b1c3a9
DL
180 (setq next `(lambda ()
181 (setq next ',next)
182 ',elt)))
933fb957
RM
183 ((eq def 'automatic)
184 ;; Act on this and all following objects.
185 (if (funcall prompter elt)
186 (progn
187 (funcall actor elt)
188 (setq actions (1+ actions))))
189 (while (funcall next)
190 (if (funcall prompter elt)
191 (progn
192 (funcall actor elt)
193 (setq actions (1+ actions))))))
194 ((eq def 'help)
195 (with-output-to-temp-buffer "*Help*"
196 (princ
197 (let ((object (if help (nth 0 help) "object"))
198 (objects (if help (nth 1 help) "objects"))
199 (action (if help (nth 2 help) "act on")))
200 (concat
201 (format "Type SPC or `y' to %s the current %s;
0a2eb25e 202DEL or `n' to skip the current %s;
c733d4ce 203RET or `q' to exit (skip all remaining %s);
0a2eb25e
RS
204! to %s all remaining %s;
205ESC or `q' to exit;\n"
c733d4ce
DL
206 action object object objects action
207 objects)
933fb957
RM
208 (mapconcat (function
209 (lambda (elt)
210 (format "%c to %s"
211 (nth 0 elt)
212 (nth 2 elt))))
213 action-alist
214 ";\n")
215 (if action-alist ";\n")
216 (format "or . (period) to %s \
0a2eb25e 217the current %s and exit."
933fb957
RM
218 action object))))
219 (save-excursion
220 (set-buffer standard-output)
221 (help-mode)))
222
4f837d3b 223 (setq next `(lambda ()
b9b1c3a9
DL
224 (setq next ',next)
225 ',elt)))
933fb957
RM
226 ((vectorp def)
227 ;; A user-defined key.
228 (if (funcall (aref def 0) elt) ;Call its function.
229 ;; The function has eaten this object.
230 (setq actions (1+ actions))
231 ;; Regurgitated; try again.
4f837d3b 232 (setq next `(lambda ()
b9b1c3a9
DL
233 (setq next ',next)
234 ',elt))))
933fb957
RM
235 ((and (consp char)
236 (eq (car char) 'switch-frame))
237 ;; switch-frame event. Put it off until we're done.
238 (setq delayed-switch-frame char)
4f837d3b 239 (setq next `(lambda ()
b9b1c3a9
DL
240 (setq next ',next)
241 ',elt)))
933fb957
RM
242 (t
243 ;; Random char.
244 (message "Type %s for help."
245 (key-description (vector help-char)))
246 (beep)
247 (sit-for 1)
4f837d3b 248 (setq next `(lambda ()
b9b1c3a9
DL
249 (setq next ',next)
250 ',elt)))))
933fb957
RM
251 (prompt
252 (funcall actor elt)
253 (setq actions (1+ actions))))))
3eea8aa2
JB
254 (if delayed-switch-frame
255 (setq unread-command-events
256 (cons delayed-switch-frame unread-command-events))))
60205e0b 257 ;; Clear the last prompt from the minibuffer.
a66839ce
KH
258 (let ((message-log-max nil))
259 (message ""))
60205e0b
RM
260 ;; Return the number of actions that were taken.
261 actions))
6594deb0
ER
262
263;;; map-ynp.el ends here