Copyright up-date.
[emacs.git] / lisp / map-ynp.el
blobbe664e02e1115eed77e212e1c5c321756a171148
1 ;;; map-ynp.el --- General-purpose boolean question-asker.
3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
5 ;; Author: Roland McGrath <roland@gnu.org>
6 ;; Maintainer: FSF
7 ;; Keywords: lisp, extensions
9 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;;; Commentary:
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
30 ;; apply an action to each element of a list based on the answer.
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.
36 ;;; Code:
38 (defun map-y-or-n-p (prompter actor list &optional help action-alist
39 no-cursor-in-echo-area)
40 "Ask a series of boolean questions.
41 Takes args PROMPTER ACTOR LIST, and optional args HELP and ACTION-ALIST.
43 LIST is a list of objects, or a function of no arguments to return the next
44 object or nil.
46 If PROMPTER is a string, the prompt is \(format PROMPTER OBJECT\). If not
47 a string, PROMPTER is a function of one arg (an object from LIST), which
48 returns a string to be used as the prompt for that object. If the return
49 value is not a string, it may be nil to ignore the object or non-nil to act
50 on the object without asking the user.
52 ACTOR is a function of one arg (an object from LIST),
53 which gets called with each object that the user answers `yes' for.
55 If HELP is given, it is a list (OBJECT OBJECTS ACTION),
56 where OBJECT is a string giving the singular noun for an elt of LIST;
57 OBJECTS is the plural noun for elts of LIST, and ACTION is a transitive
58 verb describing ACTOR. The default is \(\"object\" \"objects\" \"act on\"\).
60 At the prompts, the user may enter y, Y, or SPC to act on that object;
61 n, N, or DEL to skip that object; ! to act on all following objects;
62 ESC or q to exit (skip all following objects); . (period) to act on the
63 current object and then exit; or \\[help-command] to get help.
65 If ACTION-ALIST is given, it is an alist (KEY FUNCTION HELP) of extra keys
66 that will be accepted. KEY is a character; FUNCTION is a function of one
67 arg (an object from LIST); HELP is a string. When the user hits KEY,
68 FUNCTION 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
70 nil, the prompt is repeated for the same object.
72 Final optional argument NO-CURSOR-IN-ECHO-AREA non-nil says not to set
73 `cursor-in-echo-area' while prompting.
75 This function uses `query-replace-map' to define the standard responses,
76 but not all of the responses which `query-replace' understands
77 are meaningful here.
79 Returns the number of actions taken."
80 (let* ((actions 0)
81 user-keys mouse-event map prompt char elt tail def
82 ;; Non-nil means we should use mouse menus to ask.
83 use-menus
84 delayed-switch-frame
85 (next (if (or (and list (symbolp list))
86 (subrp list)
87 (byte-code-function-p list)
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))
98 nil))))))
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)
110 (cons (capitalize (nth 2 elt))
111 (vector (nth 1 elt))))
112 action-alist))))
113 use-menus t
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 " ")
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))))
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))
137 (cond ((stringp prompt)
138 ;; Prompt the user about this object.
139 (setq quit-flag nil)
140 (if use-menus
141 (setq def (or (x-popup-dialog (or mouse-event use-menus)
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)))
150 (if minibuffer-auto-raise
151 (raise-frame (window-frame (minibuffer-window))))
152 (while (progn
153 (setq char (read-event))
154 ;; If we get -1, from end of keyboard
155 ;; macro, try again.
156 (equal char -1)))
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.
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;
201 DEL or `n' to skip the current %s;
202 RET or `q' to exit (skip all remaining %s);
203 ! to %s all remaining %s;
204 ESC or `q' to exit;\n"
205 action object object objects action
206 objects)
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 \
216 the current %s and exit."
217 action object))))
218 (save-excursion
219 (set-buffer standard-output)
220 (help-mode)))
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)))))
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))))))
253 (if delayed-switch-frame
254 (setq unread-command-events
255 (cons delayed-switch-frame unread-command-events))))
256 ;; Clear the last prompt from the minibuffer.
257 (let ((message-log-max nil))
258 (message ""))
259 ;; Return the number of actions that were taken.
260 actions))
262 ;;; map-ynp.el ends here