Mercurial > emacs
annotate lisp/emacs-lisp/map-ynp.el @ 94712:349582c9a664
(vc-dir-insert-directories): Default to t.
author | Dan Nicolaescu <dann@ics.uci.edu> |
---|---|
date | Wed, 07 May 2008 03:45:57 +0000 |
parents | 90a2847062be |
children | 2e2ae1dd33cc |
rev | line source |
---|---|
51349 | 1 ;;; map-ynp.el --- general-purpose boolean question-asker |
2 | |
74466 | 3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 2000, 2001, 2002, 2003, |
79704 | 4 ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
51349 | 5 |
6 ;; Author: Roland McGrath <roland@gnu.org> | |
7 ;; Maintainer: FSF | |
8 ;; Keywords: lisp, extensions | |
9 | |
10 ;; This file is part of GNU Emacs. | |
11 | |
94655
90a2847062be
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
12 ;; GNU Emacs is free software: you can redistribute it and/or modify |
51349 | 13 ;; it under the terms of the GNU General Public License as published by |
94655
90a2847062be
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
14 ;; the Free Software Foundation, either version 3 of the License, or |
90a2847062be
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
15 ;; (at your option) any later version. |
51349 | 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 | |
94655
90a2847062be
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
51349 | 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 | |
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. | |
41 | |
42 LIST is a list of objects, or a function of no arguments to return the next | |
43 object or nil. | |
44 | |
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. | |
50 | |
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. | |
53 | |
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\"\). | |
58 | |
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. | |
63 | |
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. | |
70 | |
71 Final optional argument NO-CURSOR-IN-ECHO-AREA non-nil says not to set | |
72 `cursor-in-echo-area' while prompting. | |
73 | |
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. | |
77 | |
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)) | |
96 t) | |
97 nil)))))) | |
98 (if (and (listp last-nonmenu-event) | |
99 use-dialog-box) | |
100 ;; Make a list describing a dialog box. | |
101 (let ((object (if help (capitalize (nth 0 help)))) | |
102 (objects (if help (capitalize (nth 1 help)))) | |
103 (action (if help (capitalize (nth 2 help))))) | |
64829
9c1dcec0d1fb
(map-y-or-n-p): Reorder the options
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
104 (setq map `(("Yes" . act) ("No" . skip) |
9c1dcec0d1fb
(map-y-or-n-p): Reorder the options
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
105 ,@(mapcar (lambda (elt) |
9c1dcec0d1fb
(map-y-or-n-p): Reorder the options
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
106 (cons (with-syntax-table |
9c1dcec0d1fb
(map-y-or-n-p): Reorder the options
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
107 text-mode-syntax-table |
9c1dcec0d1fb
(map-y-or-n-p): Reorder the options
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
108 (capitalize (nth 2 elt))) |
9c1dcec0d1fb
(map-y-or-n-p): Reorder the options
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
109 (vector (nth 1 elt)))) |
9c1dcec0d1fb
(map-y-or-n-p): Reorder the options
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
110 action-alist) |
9c1dcec0d1fb
(map-y-or-n-p): Reorder the options
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
111 (,(if help (concat action " This But No More") |
9c1dcec0d1fb
(map-y-or-n-p): Reorder the options
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
112 "Do This But No More") . act-and-exit) |
51349 | 113 (,(if help (concat action " All " objects) |
114 "Do All") . automatic) | |
64829
9c1dcec0d1fb
(map-y-or-n-p): Reorder the options
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
115 ("No For All" . exit)) |
51349 | 116 use-menus t |
117 mouse-event last-nonmenu-event)) | |
118 (setq user-keys (if action-alist | |
119 (concat (mapconcat (function | |
120 (lambda (elt) | |
121 (key-description | |
122 (char-to-string (car elt))))) | |
123 action-alist ", ") | |
124 " ") | |
125 "") | |
126 ;; Make a map that defines each user key as a vector containing | |
127 ;; its definition. | |
128 map (cons 'keymap | |
129 (append (mapcar (lambda (elt) | |
130 (cons (car elt) (vector (nth 1 elt)))) | |
131 action-alist) | |
132 query-replace-map)))) | |
133 (unwind-protect | |
134 (progn | |
135 (if (stringp prompter) | |
136 (setq prompter `(lambda (object) | |
137 (format ,prompter object)))) | |
138 (while (funcall next) | |
139 (setq prompt (funcall prompter elt)) | |
140 (cond ((stringp prompt) | |
141 ;; Prompt the user about this object. | |
142 (setq quit-flag nil) | |
143 (if use-menus | |
144 (setq def (or (x-popup-dialog (or mouse-event use-menus) | |
145 (cons prompt map)) | |
146 'quit)) | |
147 ;; Prompt in the echo area. | |
148 (let ((cursor-in-echo-area (not no-cursor-in-echo-area)) | |
149 (message-log-max nil)) | |
76146
77405acb8332
(map-y-or-n-p): Apply minibuffer-prompt-properties.
Kim F. Storm <storm@cua.dk>
parents:
75346
diff
changeset
|
150 (message (apply 'propertize "%s(y, n, !, ., q, %sor %s) " |
77405acb8332
(map-y-or-n-p): Apply minibuffer-prompt-properties.
Kim F. Storm <storm@cua.dk>
parents:
75346
diff
changeset
|
151 minibuffer-prompt-properties) |
51349 | 152 prompt user-keys |
153 (key-description (vector help-char))) | |
154 (if minibuffer-auto-raise | |
155 (raise-frame (window-frame (minibuffer-window)))) | |
156 (while (progn | |
157 (setq char (read-event)) | |
158 ;; If we get -1, from end of keyboard | |
159 ;; macro, try again. | |
160 (equal char -1))) | |
161 ;; Show the answer to the question. | |
162 (message "%s(y, n, !, ., q, %sor %s) %s" | |
163 prompt user-keys | |
164 (key-description (vector help-char)) | |
165 (single-key-description char))) | |
166 (setq def (lookup-key map (vector char)))) | |
167 (cond ((eq def 'exit) | |
168 (setq next (function (lambda () nil)))) | |
169 ((eq def 'act) | |
170 ;; Act on the object. | |
171 (funcall actor elt) | |
172 (setq actions (1+ actions))) | |
173 ((eq def 'skip) | |
174 ;; Skip the object. | |
175 ) | |
176 ((eq def 'act-and-exit) | |
177 ;; Act on the object and then exit. | |
178 (funcall actor elt) | |
179 (setq actions (1+ actions) | |
180 next (function (lambda () nil)))) | |
181 ((eq def 'quit) | |
182 (setq quit-flag t) | |
183 (setq next `(lambda () | |
184 (setq next ',next) | |
185 ',elt))) | |
186 ((eq def 'automatic) | |
187 ;; Act on this and all following objects. | |
188 (if (funcall prompter elt) | |
189 (progn | |
190 (funcall actor elt) | |
191 (setq actions (1+ actions)))) | |
192 (while (funcall next) | |
193 (if (funcall prompter elt) | |
194 (progn | |
195 (funcall actor elt) | |
196 (setq actions (1+ actions)))))) | |
197 ((eq def 'help) | |
198 (with-output-to-temp-buffer "*Help*" | |
199 (princ | |
200 (let ((object (if help (nth 0 help) "object")) | |
201 (objects (if help (nth 1 help) "objects")) | |
202 (action (if help (nth 2 help) "act on"))) | |
203 (concat | |
204 (format "Type SPC or `y' to %s the current %s; | |
205 DEL or `n' to skip the current %s; | |
61447
ff2428b81ec5
(map-y-or-n-p): Clarify RET/q in help message.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
206 RET or `q' to give up on the %s (skip all remaining %s); |
ff2428b81ec5
(map-y-or-n-p): Clarify RET/q in help message.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
207 C-g to quit (cancel the whole command); |
51349 | 208 ! to %s all remaining %s;\n" |
61447
ff2428b81ec5
(map-y-or-n-p): Clarify RET/q in help message.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
209 action object object action objects action |
51349 | 210 objects) |
211 (mapconcat (function | |
212 (lambda (elt) | |
213 (format "%s to %s" | |
214 (single-key-description | |
215 (nth 0 elt)) | |
216 (nth 2 elt)))) | |
217 action-alist | |
218 ";\n") | |
219 (if action-alist ";\n") | |
220 (format "or . (period) to %s \ | |
221 the current %s and exit." | |
222 action object)))) | |
223 (save-excursion | |
224 (set-buffer standard-output) | |
225 (help-mode))) | |
226 | |
227 (setq next `(lambda () | |
228 (setq next ',next) | |
229 ',elt))) | |
230 ((vectorp def) | |
231 ;; A user-defined key. | |
232 (if (funcall (aref def 0) elt) ;Call its function. | |
233 ;; The function has eaten this object. | |
234 (setq actions (1+ actions)) | |
235 ;; Regurgitated; try again. | |
236 (setq next `(lambda () | |
237 (setq next ',next) | |
238 ',elt)))) | |
239 ((and (consp char) | |
240 (eq (car char) 'switch-frame)) | |
241 ;; switch-frame event. Put it off until we're done. | |
242 (setq delayed-switch-frame char) | |
243 (setq next `(lambda () | |
244 (setq next ',next) | |
245 ',elt))) | |
246 (t | |
247 ;; Random char. | |
248 (message "Type %s for help." | |
249 (key-description (vector help-char))) | |
250 (beep) | |
251 (sit-for 1) | |
252 (setq next `(lambda () | |
253 (setq next ',next) | |
254 ',elt))))) | |
255 (prompt | |
256 (funcall actor elt) | |
257 (setq actions (1+ actions)))))) | |
258 (if delayed-switch-frame | |
259 (setq unread-command-events | |
260 (cons delayed-switch-frame unread-command-events)))) | |
261 ;; Clear the last prompt from the minibuffer. | |
262 (let ((message-log-max nil)) | |
263 (message "")) | |
264 ;; Return the number of actions that were taken. | |
265 actions)) | |
266 | |
93975
1e3a407766b9
Fix up comment convention on the arch-tag lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
79704
diff
changeset
|
267 ;; arch-tag: 1d0a3201-a151-4c10-b231-4da47c9e6dc3 |
51349 | 268 ;;; map-ynp.el ends here |