Mercurial > emacs
annotate lisp/emacs-lisp/map-ynp.el @ 64751:5b1a238fcbb4
Update years in copyright notice; nfc.
author | Thien-Thi Nguyen <ttn@gnuvola.org> |
---|---|
date | Sat, 06 Aug 2005 17:48:15 +0000 |
parents | 18a818a2ee7c |
children | 9c1dcec0d1fb |
rev | line source |
---|---|
51349 | 1 ;;; map-ynp.el --- general-purpose boolean question-asker |
2 | |
64751
5b1a238fcbb4
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64085
diff
changeset
|
3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 2000, 2002, 2003, |
5b1a238fcbb4
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64085
diff
changeset
|
4 ;; 2004, 2005 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 | |
12 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
13 ;; it under the terms of the GNU General Public License as published by | |
14 ;; the Free Software Foundation; either version 2, or (at your option) | |
15 ;; any later version. | |
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 | |
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
64085 | 24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
25 ;; Boston, MA 02110-1301, USA. | |
51349 | 26 |
27 ;;; Commentary: | |
28 | |
29 ;; map-y-or-n-p is a general-purpose question-asking function. | |
30 ;; It asks a series of y/n questions (a la y-or-n-p), and decides to | |
31 ;; apply an action to each element of a list based on the answer. | |
32 ;; The nice thing is that you also get some other possible answers | |
33 ;; to use, reminiscent of query-replace: ! to answer y to all remaining | |
34 ;; questions; ESC or q to answer n to all remaining questions; . to answer | |
35 ;; y once and then n for the remainder; and you can get help with C-h. | |
36 | |
37 ;;; Code: | |
38 | |
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 | |
86 (next (if (or (and list (symbolp list)) | |
87 (subrp list) | |
88 (byte-code-function-p list) | |
89 (and (consp list) | |
90 (eq (car list) 'lambda))) | |
91 (function (lambda () | |
92 (setq elt (funcall list)))) | |
93 (function (lambda () | |
94 (if list | |
95 (progn | |
96 (setq elt (car list) | |
97 list (cdr list)) | |
98 t) | |
99 nil)))))) | |
100 (if (and (listp last-nonmenu-event) | |
101 use-dialog-box) | |
102 ;; Make a list describing a dialog box. | |
103 (let ((object (if help (capitalize (nth 0 help)))) | |
104 (objects (if help (capitalize (nth 1 help)))) | |
105 (action (if help (capitalize (nth 2 help))))) | |
106 (setq map `(("Yes" . act) ("No" . skip) ("Quit" . exit) | |
107 (,(if help (concat action " " object " And Quit") | |
108 "Do it and Quit") . act-and-exit) | |
109 (,(if help (concat action " All " objects) | |
110 "Do All") . automatic) | |
111 ,@(mapcar (lambda (elt) | |
112 (cons (capitalize (nth 2 elt)) | |
113 (vector (nth 1 elt)))) | |
114 action-alist)) | |
115 use-menus t | |
116 mouse-event last-nonmenu-event)) | |
117 (setq user-keys (if action-alist | |
118 (concat (mapconcat (function | |
119 (lambda (elt) | |
120 (key-description | |
121 (char-to-string (car elt))))) | |
122 action-alist ", ") | |
123 " ") | |
124 "") | |
125 ;; Make a map that defines each user key as a vector containing | |
126 ;; its definition. | |
127 map (cons 'keymap | |
128 (append (mapcar (lambda (elt) | |
129 (cons (car elt) (vector (nth 1 elt)))) | |
130 action-alist) | |
131 query-replace-map)))) | |
132 (unwind-protect | |
133 (progn | |
134 (if (stringp prompter) | |
135 (setq prompter `(lambda (object) | |
136 (format ,prompter object)))) | |
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)) | |
149 (message "%s(y, n, !, ., q, %sor %s) " | |
150 prompt user-keys | |
151 (key-description (vector help-char))) | |
152 (if minibuffer-auto-raise | |
153 (raise-frame (window-frame (minibuffer-window)))) | |
154 (while (progn | |
155 (setq char (read-event)) | |
156 ;; If we get -1, from end of keyboard | |
157 ;; macro, try again. | |
158 (equal char -1))) | |
159 ;; Show the answer to the question. | |
160 (message "%s(y, n, !, ., q, %sor %s) %s" | |
161 prompt user-keys | |
162 (key-description (vector help-char)) | |
163 (single-key-description char))) | |
164 (setq def (lookup-key map (vector char)))) | |
165 (cond ((eq def 'exit) | |
166 (setq next (function (lambda () nil)))) | |
167 ((eq def 'act) | |
168 ;; Act on the object. | |
169 (funcall actor elt) | |
170 (setq actions (1+ actions))) | |
171 ((eq def 'skip) | |
172 ;; Skip the object. | |
173 ) | |
174 ((eq def 'act-and-exit) | |
175 ;; Act on the object and then exit. | |
176 (funcall actor elt) | |
177 (setq actions (1+ actions) | |
178 next (function (lambda () nil)))) | |
179 ((eq def 'quit) | |
180 (setq quit-flag t) | |
181 (setq next `(lambda () | |
182 (setq next ',next) | |
183 ',elt))) | |
184 ((eq def 'automatic) | |
185 ;; Act on this and all following objects. | |
186 (if (funcall prompter elt) | |
187 (progn | |
188 (funcall actor elt) | |
189 (setq actions (1+ actions)))) | |
190 (while (funcall next) | |
191 (if (funcall prompter elt) | |
192 (progn | |
193 (funcall actor elt) | |
194 (setq actions (1+ actions)))))) | |
195 ((eq def 'help) | |
196 (with-output-to-temp-buffer "*Help*" | |
197 (princ | |
198 (let ((object (if help (nth 0 help) "object")) | |
199 (objects (if help (nth 1 help) "objects")) | |
200 (action (if help (nth 2 help) "act on"))) | |
201 (concat | |
202 (format "Type SPC or `y' to %s the current %s; | |
203 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
|
204 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
|
205 C-g to quit (cancel the whole command); |
51349 | 206 ! 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
|
207 action object object action objects action |
51349 | 208 objects) |
209 (mapconcat (function | |
210 (lambda (elt) | |
211 (format "%s to %s" | |
212 (single-key-description | |
213 (nth 0 elt)) | |
214 (nth 2 elt)))) | |
215 action-alist | |
216 ";\n") | |
217 (if action-alist ";\n") | |
218 (format "or . (period) to %s \ | |
219 the current %s and exit." | |
220 action object)))) | |
221 (save-excursion | |
222 (set-buffer standard-output) | |
223 (help-mode))) | |
224 | |
225 (setq next `(lambda () | |
226 (setq next ',next) | |
227 ',elt))) | |
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. | |
234 (setq next `(lambda () | |
235 (setq next ',next) | |
236 ',elt)))) | |
237 ((and (consp char) | |
238 (eq (car char) 'switch-frame)) | |
239 ;; switch-frame event. Put it off until we're done. | |
240 (setq delayed-switch-frame char) | |
241 (setq next `(lambda () | |
242 (setq next ',next) | |
243 ',elt))) | |
244 (t | |
245 ;; Random char. | |
246 (message "Type %s for help." | |
247 (key-description (vector help-char))) | |
248 (beep) | |
249 (sit-for 1) | |
250 (setq next `(lambda () | |
251 (setq next ',next) | |
252 ',elt))))) | |
253 (prompt | |
254 (funcall actor elt) | |
255 (setq actions (1+ actions)))))) | |
256 (if delayed-switch-frame | |
257 (setq unread-command-events | |
258 (cons delayed-switch-frame unread-command-events)))) | |
259 ;; Clear the last prompt from the minibuffer. | |
260 (let ((message-log-max nil)) | |
261 (message "")) | |
262 ;; Return the number of actions that were taken. | |
263 actions)) | |
264 | |
52401 | 265 ;;; arch-tag: 1d0a3201-a151-4c10-b231-4da47c9e6dc3 |
51349 | 266 ;;; map-ynp.el ends here |