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