Commit | Line | Data |
---|---|---|
5e046f6d JB |
1 | ;;; map-ynp.el --- general-purpose boolean question-asker |
2 | ||
acaf905b | 3 | ;; Copyright (C) 1991-1995, 2000-2012 Free Software Foundation, Inc. |
5e046f6d JB |
4 | |
5 | ;; Author: Roland McGrath <roland@gnu.org> | |
6 | ;; Maintainer: FSF | |
7 | ;; Keywords: lisp, extensions | |
d0b822e3 | 8 | ;; Package: emacs |
5e046f6d JB |
9 | |
10 | ;; This file is part of GNU Emacs. | |
11 | ||
d6cba7ae | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
5e046f6d | 13 | ;; it under the terms of the GNU General Public License as published by |
d6cba7ae GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
5e046f6d JB |
16 | |
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
d6cba7ae | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
5e046f6d JB |
24 | |
25 | ;;; Commentary: | |
26 | ||
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 | ;; apply 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. | |
34 | ||
35 | ;;; Code: | |
36 | ||
aa360da1 GM |
37 | (declare-function x-popup-dialog "xmenu.c" (position contents &optional header)) |
38 | ||
5e046f6d JB |
39 | (defun map-y-or-n-p (prompter actor list &optional help action-alist |
40 | no-cursor-in-echo-area) | |
41 | "Ask a series of boolean questions. | |
42 | Takes args PROMPTER ACTOR LIST, and optional args HELP and ACTION-ALIST. | |
43 | ||
44 | LIST is a list of objects, or a function of no arguments to return the next | |
45 | object or nil. | |
46 | ||
47 | If PROMPTER is a string, the prompt is \(format PROMPTER OBJECT\). If not | |
48 | a string, PROMPTER is a function of one arg (an object from LIST), which | |
49 | returns a string to be used as the prompt for that object. If the return | |
50 | value is not a string, it may be nil to ignore the object or non-nil to act | |
51 | on the object without asking the user. | |
52 | ||
53 | ACTOR is a function of one arg (an object from LIST), | |
54 | which gets called with each object that the user answers `yes' for. | |
55 | ||
56 | If HELP is given, it is a list (OBJECT OBJECTS ACTION), | |
57 | where OBJECT is a string giving the singular noun for an elt of LIST; | |
58 | OBJECTS is the plural noun for elts of LIST, and ACTION is a transitive | |
59 | verb describing ACTOR. The default is \(\"object\" \"objects\" \"act on\"\). | |
60 | ||
61 | At the prompts, the user may enter y, Y, or SPC to act on that object; | |
62 | n, N, or DEL to skip that object; ! to act on all following objects; | |
63 | ESC or q to exit (skip all following objects); . (period) to act on the | |
64 | current object and then exit; or \\[help-command] to get help. | |
65 | ||
66 | If ACTION-ALIST is given, it is an alist (KEY FUNCTION HELP) of extra keys | |
67 | that will be accepted. KEY is a character; FUNCTION is a function of one | |
68 | arg (an object from LIST); HELP is a string. When the user hits KEY, | |
69 | FUNCTION is called. If it returns non-nil, the object is considered | |
70 | \"acted upon\", and the next object from LIST is processed. If it returns | |
71 | nil, the prompt is repeated for the same object. | |
72 | ||
73 | Final optional argument NO-CURSOR-IN-ECHO-AREA non-nil says not to set | |
74 | `cursor-in-echo-area' while prompting. | |
75 | ||
76 | This function uses `query-replace-map' to define the standard responses, | |
77 | but not all of the responses which `query-replace' understands | |
78 | are meaningful here. | |
79 | ||
80 | Returns the number of actions taken." | |
81 | (let* ((actions 0) | |
82 | user-keys mouse-event map prompt char elt tail def | |
83 | ;; Non-nil means we should use mouse menus to ask. | |
84 | use-menus | |
85 | delayed-switch-frame | |
9b106871 SM |
86 | ;; Rebind other-window-scroll-buffer so that subfunctions can set |
87 | ;; it temporarily, without risking affecting the caller. | |
88 | (other-window-scroll-buffer other-window-scroll-buffer) | |
89 | (next (if (functionp list) | |
90 | (lambda () (setq elt (funcall list))) | |
91 | (lambda () (when list | |
92 | (setq elt (pop list)) | |
93 | t))))) | |
5e046f6d JB |
94 | (if (and (listp last-nonmenu-event) |
95 | use-dialog-box) | |
96 | ;; Make a list describing a dialog box. | |
97 | (let ((object (if help (capitalize (nth 0 help)))) | |
98 | (objects (if help (capitalize (nth 1 help)))) | |
99 | (action (if help (capitalize (nth 2 help))))) | |
369a47a4 | 100 | (setq map `(("Yes" . act) ("No" . skip) |
5e046f6d | 101 | ,@(mapcar (lambda (elt) |
369a47a4 RS |
102 | (cons (with-syntax-table |
103 | text-mode-syntax-table | |
104 | (capitalize (nth 2 elt))) | |
5e046f6d | 105 | (vector (nth 1 elt)))) |
369a47a4 RS |
106 | action-alist) |
107 | (,(if help (concat action " This But No More") | |
108 | "Do This But No More") . act-and-exit) | |
109 | (,(if help (concat action " All " objects) | |
110 | "Do All") . automatic) | |
111 | ("No For All" . exit)) | |
5e046f6d JB |
112 | use-menus t |
113 | mouse-event last-nonmenu-event)) | |
114 | (setq user-keys (if action-alist | |
2456bb63 SM |
115 | (concat (mapconcat (lambda (elt) |
116 | (key-description | |
117 | (vector (car elt)))) | |
5e046f6d JB |
118 | action-alist ", ") |
119 | " ") | |
120 | "") | |
121 | ;; Make a map that defines each user key as a vector containing | |
122 | ;; its definition. | |
9b106871 SM |
123 | map |
124 | (let ((map (make-sparse-keymap))) | |
125 | (set-keymap-parent map query-replace-map) | |
9b106871 SM |
126 | (dolist (elt action-alist) |
127 | (define-key map (vector (car elt)) (vector (nth 1 elt)))) | |
128 | map))) | |
5e046f6d JB |
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)) | |
f7f2cc5d KS |
146 | (message (apply 'propertize "%s(y, n, !, ., q, %sor %s) " |
147 | minibuffer-prompt-properties) | |
5e046f6d JB |
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) | |
9b106871 | 164 | (setq next (lambda () nil))) |
5e046f6d JB |
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) | |
9b106871 | 176 | next (lambda () nil))) |
5e046f6d JB |
177 | ((eq def 'quit) |
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; | |
90d0422a RS |
202 | RET or `q' to give up on the %s (skip all remaining %s); |
203 | C-g to quit (cancel the whole command); | |
5e046f6d | 204 | ! to %s all remaining %s;\n" |
90d0422a | 205 | action object object action objects action |
5e046f6d JB |
206 | objects) |
207 | (mapconcat (function | |
208 | (lambda (elt) | |
209 | (format "%s to %s" | |
210 | (single-key-description | |
211 | (nth 0 elt)) | |
212 | (nth 2 elt)))) | |
213 | action-alist | |
214 | ";\n") | |
215 | (if action-alist ";\n") | |
216 | (format "or . (period) to %s \ | |
217 | the current %s and exit." | |
218 | action object)))) | |
9b106871 | 219 | (with-current-buffer standard-output |
5e046f6d JB |
220 | (help-mode))) |
221 | ||
222 | (setq next `(lambda () | |
223 | (setq next ',next) | |
224 | ',elt))) | |
9b106871 SM |
225 | ((and (symbolp def) (commandp def)) |
226 | (call-interactively def) | |
227 | ;; Regurgitated; try again. | |
228 | (setq next `(lambda () | |
229 | (setq next ',next) | |
230 | ',elt))) | |
5e046f6d JB |
231 | ((vectorp def) |
232 | ;; A user-defined key. | |
233 | (if (funcall (aref def 0) elt) ;Call its function. | |
234 | ;; The function has eaten this object. | |
235 | (setq actions (1+ actions)) | |
236 | ;; Regurgitated; try again. | |
237 | (setq next `(lambda () | |
238 | (setq next ',next) | |
239 | ',elt)))) | |
240 | ((and (consp char) | |
241 | (eq (car char) 'switch-frame)) | |
242 | ;; switch-frame event. Put it off until we're done. | |
243 | (setq delayed-switch-frame char) | |
244 | (setq next `(lambda () | |
245 | (setq next ',next) | |
246 | ',elt))) | |
247 | (t | |
248 | ;; Random char. | |
249 | (message "Type %s for help." | |
250 | (key-description (vector help-char))) | |
251 | (beep) | |
252 | (sit-for 1) | |
253 | (setq next `(lambda () | |
254 | (setq next ',next) | |
255 | ',elt))))) | |
256 | (prompt | |
257 | (funcall actor elt) | |
258 | (setq actions (1+ actions)))))) | |
259 | (if delayed-switch-frame | |
260 | (setq unread-command-events | |
261 | (cons delayed-switch-frame unread-command-events)))) | |
262 | ;; Clear the last prompt from the minibuffer. | |
263 | (let ((message-log-max nil)) | |
264 | (message "")) | |
265 | ;; Return the number of actions that were taken. | |
266 | actions)) | |
267 | ||
268 | ;;; map-ynp.el ends here |