don't require grep in vc-git
[bpt/emacs.git] / lisp / emacs-lisp / map-ynp.el
CommitLineData
0b31660d 1;;; map-ynp.el --- general-purpose boolean question-asker -*- lexical-binding:t -*-
5e046f6d 2
ba318903 3;; Copyright (C) 1991-1995, 2000-2014 Free Software Foundation, Inc.
5e046f6d
JB
4
5;; Author: Roland McGrath <roland@gnu.org>
34dc21db 6;; Maintainer: emacs-devel@gnu.org
5e046f6d 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
cc0f2ece 37(declare-function x-popup-dialog "menu.c" (position contents &optional header))
aa360da1 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.
42Takes args PROMPTER ACTOR LIST, and optional args HELP and ACTION-ALIST.
43
44LIST is a list of objects, or a function of no arguments to return the next
45object or nil.
46
47If PROMPTER is a string, the prompt is \(format PROMPTER OBJECT\). If not
48a string, PROMPTER is a function of one arg (an object from LIST), which
49returns a string to be used as the prompt for that object. If the return
50value is not a string, it may be nil to ignore the object or non-nil to act
51on the object without asking the user.
52
53ACTOR is a function of one arg (an object from LIST),
54which gets called with each object that the user answers `yes' for.
55
56If HELP is given, it is a list (OBJECT OBJECTS ACTION),
57where OBJECT is a string giving the singular noun for an elt of LIST;
58OBJECTS is the plural noun for elts of LIST, and ACTION is a transitive
59verb describing ACTOR. The default is \(\"object\" \"objects\" \"act on\"\).
60
61At the prompts, the user may enter y, Y, or SPC to act on that object;
62n, N, or DEL to skip that object; ! to act on all following objects;
63ESC or q to exit (skip all following objects); . (period) to act on the
64current object and then exit; or \\[help-command] to get help.
65
66If ACTION-ALIST is given, it is an alist (KEY FUNCTION HELP) of extra keys
67that will be accepted. KEY is a character; FUNCTION is a function of one
68arg (an object from LIST); HELP is a string. When the user hits KEY,
69FUNCTION 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
71nil, the prompt is repeated for the same object.
72
73Final optional argument NO-CURSOR-IN-ECHO-AREA non-nil says not to set
74`cursor-in-echo-area' while prompting.
75
76This function uses `query-replace-map' to define the standard responses,
77but not all of the responses which `query-replace' understands
78are meaningful here.
79
80Returns the number of actions taken."
81 (let* ((actions 0)
0b31660d 82 user-keys mouse-event map prompt char elt def
5e046f6d
JB
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
0b31660d
SM
92 (setq elt (pop list))
93 t))))
94 (try-again (lambda ()
95 (let ((x next))
96 (setq next (lambda () (setq next x) elt))))))
5e046f6d
JB
97 (if (and (listp last-nonmenu-event)
98 use-dialog-box)
99 ;; Make a list describing a dialog box.
0b31660d 100 (let ((objects (if help (capitalize (nth 1 help))))
5e046f6d 101 (action (if help (capitalize (nth 2 help)))))
369a47a4 102 (setq map `(("Yes" . act) ("No" . skip)
5e046f6d 103 ,@(mapcar (lambda (elt)
369a47a4
RS
104 (cons (with-syntax-table
105 text-mode-syntax-table
106 (capitalize (nth 2 elt)))
5e046f6d 107 (vector (nth 1 elt))))
369a47a4
RS
108 action-alist)
109 (,(if help (concat action " This But No More")
110 "Do This But No More") . act-and-exit)
111 (,(if help (concat action " All " objects)
112 "Do All") . automatic)
113 ("No For All" . exit))
5e046f6d
JB
114 use-menus t
115 mouse-event last-nonmenu-event))
116 (setq user-keys (if action-alist
2456bb63
SM
117 (concat (mapconcat (lambda (elt)
118 (key-description
119 (vector (car elt))))
5e046f6d
JB
120 action-alist ", ")
121 " ")
122 "")
123 ;; Make a map that defines each user key as a vector containing
124 ;; its definition.
9b106871
SM
125 map
126 (let ((map (make-sparse-keymap)))
127 (set-keymap-parent map query-replace-map)
9b106871
SM
128 (dolist (elt action-alist)
129 (define-key map (vector (car elt)) (vector (nth 1 elt))))
130 map)))
5e046f6d
JB
131 (unwind-protect
132 (progn
133 (if (stringp prompter)
051f2775
AS
134 (setq prompter (let ((prompter prompter))
135 (lambda (object)
136 (format prompter object)))))
5e046f6d
JB
137 (while (funcall next)
138 (setq prompt (funcall prompter elt))
139 (cond ((stringp prompt)
140 ;; Prompt the user about this object.
141 (setq quit-flag nil)
142 (if use-menus
143 (setq def (or (x-popup-dialog (or mouse-event use-menus)
144 (cons prompt map))
145 'quit))
146 ;; Prompt in the echo area.
147 (let ((cursor-in-echo-area (not no-cursor-in-echo-area))
148 (message-log-max nil))
f7f2cc5d
KS
149 (message (apply 'propertize "%s(y, n, !, ., q, %sor %s) "
150 minibuffer-prompt-properties)
5e046f6d
JB
151 prompt user-keys
152 (key-description (vector help-char)))
153 (if minibuffer-auto-raise
154 (raise-frame (window-frame (minibuffer-window))))
155 (while (progn
156 (setq char (read-event))
157 ;; If we get -1, from end of keyboard
158 ;; macro, try again.
159 (equal char -1)))
160 ;; Show the answer to the question.
161 (message "%s(y, n, !, ., q, %sor %s) %s"
162 prompt user-keys
163 (key-description (vector help-char))
164 (single-key-description char)))
165 (setq def (lookup-key map (vector char))))
166 (cond ((eq def 'exit)
9b106871 167 (setq next (lambda () nil)))
5e046f6d
JB
168 ((eq def 'act)
169 ;; Act on the object.
170 (funcall actor elt)
171 (setq actions (1+ actions)))
172 ((eq def 'skip)
173 ;; Skip the object.
174 )
175 ((eq def 'act-and-exit)
176 ;; Act on the object and then exit.
177 (funcall actor elt)
178 (setq actions (1+ actions)
9b106871 179 next (lambda () nil)))
5e046f6d
JB
180 ((eq def 'quit)
181 (setq quit-flag t)
0b31660d 182 (funcall try-again))
5e046f6d
JB
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;
202DEL or `n' to skip the current %s;
90d0422a
RS
203RET or `q' to give up on the %s (skip all remaining %s);
204C-g to quit (cancel the whole command);
5e046f6d 205! to %s all remaining %s;\n"
90d0422a 206 action object object action objects action
5e046f6d
JB
207 objects)
208 (mapconcat (function
209 (lambda (elt)
210 (format "%s to %s"
211 (single-key-description
212 (nth 0 elt))
213 (nth 2 elt))))
214 action-alist
215 ";\n")
216 (if action-alist ";\n")
217 (format "or . (period) to %s \
218the current %s and exit."
219 action object))))
9b106871 220 (with-current-buffer standard-output
5e046f6d
JB
221 (help-mode)))
222
0b31660d
SM
223 (funcall try-again))
224 ((and (symbolp def) (commandp def))
225 (call-interactively def)
226 ;; Regurgitated; try again.
227 (funcall try-again))
5e046f6d
JB
228 ((vectorp def)
229 ;; A user-defined key.
230 (if (funcall (aref def 0) elt) ;Call its function.
231 ;; The function has eaten this object.
232 (setq actions (1+ actions))
233 ;; Regurgitated; try again.
0b31660d 234 (funcall try-again)))
5e046f6d
JB
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)
0b31660d 239 (funcall try-again))
5e046f6d
JB
240 (t
241 ;; Random char.
242 (message "Type %s for help."
243 (key-description (vector help-char)))
244 (beep)
245 (sit-for 1)
0b31660d 246 (funcall try-again))))
5e046f6d
JB
247 (prompt
248 (funcall actor elt)
249 (setq actions (1+ actions))))))
250 (if delayed-switch-frame
251 (setq unread-command-events
252 (cons delayed-switch-frame unread-command-events))))
253 ;; Clear the last prompt from the minibuffer.
254 (let ((message-log-max nil))
255 (message ""))
256 ;; Return the number of actions that were taken.
257 actions))
258
259;;; map-ynp.el ends here