Mercurial > emacs
annotate lisp/emacs-lisp/map-ynp.el @ 101953:7abfdad8fc00
Tiny change is tiny.
author | Glenn Morris <rgm@gnu.org> |
---|---|
date | Thu, 12 Feb 2009 03:30:26 +0000 |
parents | a9dc0e7c3f2b |
children | 1d1d5d9bd884 |
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, |
100908 | 4 ;; 2004, 2005, 2006, 2007, 2008, 2009 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 | |
95841
b4e36ff621b3
Add some compiler declarations, for builds without X.
Glenn Morris <rgm@gnu.org>
parents:
95806
diff
changeset
|
37 (declare-function x-popup-dialog "xmenu.c" (position contents &optional header)) |
b4e36ff621b3
Add some compiler declarations, for builds without X.
Glenn Morris <rgm@gnu.org>
parents:
95806
diff
changeset
|
38 |
51349 | 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 | |
95806
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
86 ;; Rebind other-window-scroll-buffer so that subfunctions can set |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
87 ;; it temporarily, without risking affecting the caller. |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
88 (other-window-scroll-buffer other-window-scroll-buffer) |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
89 (next (if (functionp list) |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
90 (lambda () (setq elt (funcall list))) |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
91 (lambda () (when list |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
92 (setq elt (pop list)) |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
93 t))))) |
51349 | 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))))) | |
64829
9c1dcec0d1fb
(map-y-or-n-p): Reorder the options
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
100 (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
|
101 ,@(mapcar (lambda (elt) |
9c1dcec0d1fb
(map-y-or-n-p): Reorder the options
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
102 (cons (with-syntax-table |
9c1dcec0d1fb
(map-y-or-n-p): Reorder the options
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
103 text-mode-syntax-table |
9c1dcec0d1fb
(map-y-or-n-p): Reorder the options
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
104 (capitalize (nth 2 elt))) |
9c1dcec0d1fb
(map-y-or-n-p): Reorder the options
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
105 (vector (nth 1 elt)))) |
9c1dcec0d1fb
(map-y-or-n-p): Reorder the options
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
106 action-alist) |
9c1dcec0d1fb
(map-y-or-n-p): Reorder the options
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
107 (,(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
|
108 "Do This But No More") . act-and-exit) |
51349 | 109 (,(if help (concat action " All " objects) |
110 "Do All") . automatic) | |
64829
9c1dcec0d1fb
(map-y-or-n-p): Reorder the options
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
111 ("No For All" . exit)) |
51349 | 112 use-menus t |
113 mouse-event last-nonmenu-event)) | |
114 (setq user-keys (if action-alist | |
95857
5d1e0dab59fb
(map-y-or-n-p): Accept non-char events.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
95841
diff
changeset
|
115 (concat (mapconcat (lambda (elt) |
5d1e0dab59fb
(map-y-or-n-p): Accept non-char events.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
95841
diff
changeset
|
116 (key-description |
5d1e0dab59fb
(map-y-or-n-p): Accept non-char events.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
95841
diff
changeset
|
117 (vector (car elt)))) |
51349 | 118 action-alist ", ") |
119 " ") | |
120 "") | |
121 ;; Make a map that defines each user key as a vector containing | |
122 ;; its definition. | |
95806
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
123 map |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
124 (let ((map (make-sparse-keymap))) |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
125 (set-keymap-parent map query-replace-map) |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
126 (define-key map [?\C-\M-v] 'scroll-other-window) |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
127 (define-key map [M-next] 'scroll-other-window) |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
128 (define-key map [?\C-\M-\S-v] 'scroll-other-window-down) |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
129 (define-key map [M-prior] 'scroll-other-window-down) |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
130 ;; The above are rather inconvenient, so maybe we should |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
131 ;; provide the non-other keys for the other-scroll as well. |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
132 ;; (define-key map [?\C-v] 'scroll-other-window) |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
133 ;; (define-key map [next] 'scroll-other-window) |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
134 ;; (define-key map [?\M-v] 'scroll-other-window-down) |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
135 ;; (define-key map [prior] 'scroll-other-window-down) |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
136 (dolist (elt action-alist) |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
137 (define-key map (vector (car elt)) (vector (nth 1 elt)))) |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
138 map))) |
51349 | 139 (unwind-protect |
140 (progn | |
141 (if (stringp prompter) | |
142 (setq prompter `(lambda (object) | |
143 (format ,prompter object)))) | |
144 (while (funcall next) | |
145 (setq prompt (funcall prompter elt)) | |
146 (cond ((stringp prompt) | |
147 ;; Prompt the user about this object. | |
148 (setq quit-flag nil) | |
149 (if use-menus | |
150 (setq def (or (x-popup-dialog (or mouse-event use-menus) | |
151 (cons prompt map)) | |
152 'quit)) | |
153 ;; Prompt in the echo area. | |
154 (let ((cursor-in-echo-area (not no-cursor-in-echo-area)) | |
155 (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
|
156 (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
|
157 minibuffer-prompt-properties) |
51349 | 158 prompt user-keys |
159 (key-description (vector help-char))) | |
160 (if minibuffer-auto-raise | |
161 (raise-frame (window-frame (minibuffer-window)))) | |
162 (while (progn | |
163 (setq char (read-event)) | |
164 ;; If we get -1, from end of keyboard | |
165 ;; macro, try again. | |
166 (equal char -1))) | |
167 ;; Show the answer to the question. | |
168 (message "%s(y, n, !, ., q, %sor %s) %s" | |
169 prompt user-keys | |
170 (key-description (vector help-char)) | |
171 (single-key-description char))) | |
172 (setq def (lookup-key map (vector char)))) | |
173 (cond ((eq def 'exit) | |
95806
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
174 (setq next (lambda () nil))) |
51349 | 175 ((eq def 'act) |
176 ;; Act on the object. | |
177 (funcall actor elt) | |
178 (setq actions (1+ actions))) | |
179 ((eq def 'skip) | |
180 ;; Skip the object. | |
181 ) | |
182 ((eq def 'act-and-exit) | |
183 ;; Act on the object and then exit. | |
184 (funcall actor elt) | |
185 (setq actions (1+ actions) | |
95806
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
186 next (lambda () nil))) |
51349 | 187 ((eq def 'quit) |
188 (setq quit-flag t) | |
189 (setq next `(lambda () | |
190 (setq next ',next) | |
191 ',elt))) | |
192 ((eq def 'automatic) | |
193 ;; Act on this and all following objects. | |
194 (if (funcall prompter elt) | |
195 (progn | |
196 (funcall actor elt) | |
197 (setq actions (1+ actions)))) | |
198 (while (funcall next) | |
199 (if (funcall prompter elt) | |
200 (progn | |
201 (funcall actor elt) | |
202 (setq actions (1+ actions)))))) | |
203 ((eq def 'help) | |
204 (with-output-to-temp-buffer "*Help*" | |
205 (princ | |
206 (let ((object (if help (nth 0 help) "object")) | |
207 (objects (if help (nth 1 help) "objects")) | |
208 (action (if help (nth 2 help) "act on"))) | |
209 (concat | |
210 (format "Type SPC or `y' to %s the current %s; | |
211 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
|
212 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
|
213 C-g to quit (cancel the whole command); |
51349 | 214 ! 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
|
215 action object object action objects action |
51349 | 216 objects) |
217 (mapconcat (function | |
218 (lambda (elt) | |
219 (format "%s to %s" | |
220 (single-key-description | |
221 (nth 0 elt)) | |
222 (nth 2 elt)))) | |
223 action-alist | |
224 ";\n") | |
225 (if action-alist ";\n") | |
226 (format "or . (period) to %s \ | |
227 the current %s and exit." | |
228 action object)))) | |
95806
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
229 (with-current-buffer standard-output |
51349 | 230 (help-mode))) |
231 | |
232 (setq next `(lambda () | |
233 (setq next ',next) | |
234 ',elt))) | |
95806
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
235 ((and (symbolp def) (commandp def)) |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
236 (call-interactively def) |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
237 ;; Regurgitated; try again. |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
238 (setq next `(lambda () |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
239 (setq next ',next) |
2e2ae1dd33cc
* emacs-lisp/map-ynp.el (map-y-or-n-p): Add support for other-window-scroll.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
94655
diff
changeset
|
240 ',elt))) |
51349 | 241 ((vectorp def) |
242 ;; A user-defined key. | |
243 (if (funcall (aref def 0) elt) ;Call its function. | |
244 ;; The function has eaten this object. | |
245 (setq actions (1+ actions)) | |
246 ;; Regurgitated; try again. | |
247 (setq next `(lambda () | |
248 (setq next ',next) | |
249 ',elt)))) | |
250 ((and (consp char) | |
251 (eq (car char) 'switch-frame)) | |
252 ;; switch-frame event. Put it off until we're done. | |
253 (setq delayed-switch-frame char) | |
254 (setq next `(lambda () | |
255 (setq next ',next) | |
256 ',elt))) | |
257 (t | |
258 ;; Random char. | |
259 (message "Type %s for help." | |
260 (key-description (vector help-char))) | |
261 (beep) | |
262 (sit-for 1) | |
263 (setq next `(lambda () | |
264 (setq next ',next) | |
265 ',elt))))) | |
266 (prompt | |
267 (funcall actor elt) | |
268 (setq actions (1+ actions)))))) | |
269 (if delayed-switch-frame | |
270 (setq unread-command-events | |
271 (cons delayed-switch-frame unread-command-events)))) | |
272 ;; Clear the last prompt from the minibuffer. | |
273 (let ((message-log-max nil)) | |
274 (message "")) | |
275 ;; Return the number of actions that were taken. | |
276 actions)) | |
277 | |
93975
1e3a407766b9
Fix up comment convention on the arch-tag lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
79704
diff
changeset
|
278 ;; arch-tag: 1d0a3201-a151-4c10-b231-4da47c9e6dc3 |
51349 | 279 ;;; map-ynp.el ends here |