(unexec): Copy the .got section.
[emacs.git] / lisp / map-ynp.el
blobf45fe78e3c3f581fd985fee9536f821ff10ed854
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.ai.mit.edu>
6 ;; Keywords: lisp, extensions
8 ;; This file is part of GNU Emacs.
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.
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.
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.
25 ;;; Commentary:
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
29 ;; applies an action to each element of a list based on the answer.
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.
35 ;;; Code:
37 (defun map-y-or-n-p (prompter actor list &optional help action-alist
38 no-cursor-in-echo-area)
39 "Ask a series of boolean questions.
40 Takes args PROMPTER ACTOR LIST, and optional args HELP and ACTION-ALIST.
42 LIST is a list of objects, or a function of no arguments to return the next
43 object or nil.
45 If PROMPTER is a string, the prompt is \(format PROMPTER OBJECT\). If not
46 a string, PROMPTER is a function of one arg (an object from LIST), which
47 returns a string to be used as the prompt for that object. If the return
48 value is not a string, it may be nil to ignore the object or non-nil to act
49 on the object without asking the user.
51 ACTOR is a function of one arg (an object from LIST),
52 which gets called with each object that the user answers `yes' for.
54 If HELP is given, it is a list (OBJECT OBJECTS ACTION),
55 where OBJECT is a string giving the singular noun for an elt of LIST;
56 OBJECTS is the plural noun for elts of LIST, and ACTION is a transitive
57 verb describing ACTOR. The default is \(\"object\" \"objects\" \"act on\"\).
59 At the prompts, the user may enter y, Y, or SPC to act on that object;
60 n, N, or DEL to skip that object; ! to act on all following objects;
61 ESC or q to exit (skip all following objects); . (period) to act on the
62 current object and then exit; or \\[help-command] to get help.
64 If ACTION-ALIST is given, it is an alist (KEY FUNCTION HELP) of extra keys
65 that will be accepted. KEY is a character; FUNCTION is a function of one
66 arg (an object from LIST); HELP is a string. When the user hits KEY,
67 FUNCTION 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
69 nil, the prompt is repeated for the same object.
71 Final optional argument NO-CURSOR-IN-ECHO-AREA non-nil says not to set
72 `cursor-in-echo-area' while prompting.
74 This function uses `query-replace-map' to define the standard responses,
75 but not all of the responses which `query-replace' understands
76 are meaningful here.
78 Returns the number of actions taken."
79 (let* ((actions 0)
80 user-keys mouse-event map prompt char elt tail def
81 ;; Non-nil means we should use mouse menus to ask.
82 use-menus
83 delayed-switch-frame
84 (next (if (or (and list (symbolp list))
85 (subrp list)
86 (byte-code-function-p list)
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))
97 nil))))))
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)
109 (cons (capitalize (nth 2 elt))
110 (vector (nth 1 elt))))
111 action-alist))))
112 use-menus t
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 " ")
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))))
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))
136 (cond ((stringp prompt)
137 ;; Prompt the user about this object.
138 (setq quit-flag nil)
139 (if use-menus
140 (setq def (or (x-popup-dialog (or mouse-event use-menus)
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)))
149 (setq char (read-event))
150 ;; Show the answer to the question.
151 (message "%s(y, n, !, ., q, %sor %s) %s"
152 prompt user-keys
153 (key-description (vector help-char))
154 (single-key-description char)))
155 (setq def (lookup-key map (vector char))))
156 (cond ((eq def 'exit)
157 (setq next (function (lambda () nil))))
158 ((eq def 'act)
159 ;; Act on the object.
160 (funcall actor elt)
161 (setq actions (1+ actions)))
162 ((eq def 'skip)
163 ;; Skip the object.
165 ((eq def 'act-and-exit)
166 ;; Act on the object and then exit.
167 (funcall actor elt)
168 (setq actions (1+ actions)
169 next (function (lambda () nil))))
170 ((or (eq def 'quit) (eq def 'exit-prefix))
171 (setq quit-flag t)
172 (setq next (` (lambda ()
173 (setq next '(, next))
174 '(, elt)))))
175 ((eq def 'automatic)
176 ;; Act on this and all following objects.
177 (if (funcall prompter elt)
178 (progn
179 (funcall actor elt)
180 (setq actions (1+ actions))))
181 (while (funcall next)
182 (if (funcall prompter elt)
183 (progn
184 (funcall actor elt)
185 (setq actions (1+ actions))))))
186 ((eq def 'help)
187 (with-output-to-temp-buffer "*Help*"
188 (princ
189 (let ((object (if help (nth 0 help) "object"))
190 (objects (if help (nth 1 help) "objects"))
191 (action (if help (nth 2 help) "act on")))
192 (concat
193 (format "Type SPC or `y' to %s the current %s;
194 DEL or `n' to skip the current %s;
195 ! to %s all remaining %s;
196 ESC or `q' to exit;\n"
197 action object object action objects)
198 (mapconcat (function
199 (lambda (elt)
200 (format "%c to %s"
201 (nth 0 elt)
202 (nth 2 elt))))
203 action-alist
204 ";\n")
205 (if action-alist ";\n")
206 (format "or . (period) to %s \
207 the current %s and exit."
208 action object))))
209 (save-excursion
210 (set-buffer standard-output)
211 (help-mode)))
213 (setq next (` (lambda ()
214 (setq next '(, next))
215 '(, elt)))))
216 ((vectorp def)
217 ;; A user-defined key.
218 (if (funcall (aref def 0) elt) ;Call its function.
219 ;; The function has eaten this object.
220 (setq actions (1+ actions))
221 ;; Regurgitated; try again.
222 (setq next (` (lambda ()
223 (setq next '(, next))
224 '(, elt))))))
225 ((and (consp char)
226 (eq (car char) 'switch-frame))
227 ;; switch-frame event. Put it off until we're done.
228 (setq delayed-switch-frame char)
229 (setq next (` (lambda ()
230 (setq next '(, next))
231 '(, elt)))))
233 ;; Random char.
234 (message "Type %s for help."
235 (key-description (vector help-char)))
236 (beep)
237 (sit-for 1)
238 (setq next (` (lambda ()
239 (setq next '(, next))
240 '(, elt)))))))
241 (prompt
242 (funcall actor elt)
243 (setq actions (1+ actions))))))
244 (if delayed-switch-frame
245 (setq unread-command-events
246 (cons delayed-switch-frame unread-command-events))))
247 ;; Clear the last prompt from the minibuffer.
248 (let ((message-log-max nil))
249 (message ""))
250 ;; Return the number of actions that were taken.
251 actions))
253 ;;; map-ynp.el ends here