155
|
1 ;;; map-ynp.el --- General-purpose boolean question-asker.
|
0
|
2
|
155
|
3 ;; Copyright (C) 1991-1995, 1997 Free Software Foundation, Inc.
|
0
|
4
|
|
5 ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
|
|
6 ;; Keywords: lisp, extensions
|
|
7
|
78
|
8 ;; This file is part of XEmacs.
|
|
9
|
|
10 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
11 ;; under the terms of the GNU General Public License as published by
|
|
12 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
13 ;; any later version.
|
|
14
|
|
15 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
18 ;; General Public License for more details.
|
|
19
|
|
20 ;; You should have received a copy of the GNU General Public License
|
|
21 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
|
22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
23 ;; 02111-1307, USA.
|
0
|
24
|
155
|
25 ;;; Synched up with: Emacs/Mule zeta.
|
0
|
26
|
|
27 ;;; Commentary:
|
|
28
|
155
|
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 ;; applies 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.
|
0
|
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
|
155
|
56 If HELP is given, it is a list (OBJECT OBJECTS ACTION),
|
0
|
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."
|
155
|
81 (let* ((actions 0)
|
0
|
82 user-keys mouse-event map prompt char elt def
|
155
|
83 ;; Non-nil means we should use mouse menus to ask.
|
|
84 ;; use-menus
|
|
85 ;;delayed-switch-frame
|
0
|
86 (next (if (or (and list (symbolp list))
|
|
87 (subrp list)
|
|
88 (compiled-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 (should-use-dialog-box-p)
|
|
101 ;; Make a list describing a dialog box.
|
155
|
102 (let (;; (object (capitalize (or (nth 0 help) "object")))
|
|
103 ;; (objects (capitalize (or (nth 1 help) "objects")))
|
|
104 ;; (action (capitalize (or (nth 2 help) "act on")))
|
|
105 )
|
0
|
106 (setq map `(("Yes" . act) ("No" . skip)
|
|
107 ; bogus crap. --ben
|
|
108 ; ((, (if help
|
|
109 ; (capitalize
|
|
110 ; (or (nth 3 help)
|
|
111 ; (concat action " All " objects)))
|
|
112 ; "Do All")) . automatic)
|
|
113 ; ((, (if help
|
|
114 ; (capitalize
|
|
115 ; (or (nth 4 help)
|
|
116 ; (concat action " " object " And Quit")))
|
|
117 ; "Do it and Quit")) . act-and-exit)
|
|
118 ; ((, (capitalize
|
|
119 ; (or (and help (nth 5 help)) "Quit")))
|
|
120 ; . exit)
|
|
121 ("Yes All" . automatic)
|
|
122 ("No All" . exit)
|
|
123 ("Cancel" . quit)
|
|
124 ,@(mapcar (lambda (elt)
|
|
125 (cons (capitalize (nth 2 elt))
|
|
126 (vector (nth 1 elt))))
|
|
127 action-alist))
|
|
128 mouse-event last-command-event))
|
|
129 (setq user-keys (if action-alist
|
|
130 (concat (mapconcat (function
|
|
131 (lambda (elt)
|
|
132 (key-description
|
|
133 (if (characterp (car elt))
|
155
|
134 ;; XEmacs
|
0
|
135 (char-to-string (car elt))
|
|
136 (car elt)))))
|
|
137 action-alist ", ")
|
|
138 " ")
|
|
139 "")
|
|
140 ;; Make a map that defines each user key as a vector containing
|
|
141 ;; its definition.
|
155
|
142 ;; XEmacs
|
0
|
143 map (let ((foomap (make-sparse-keymap)))
|
|
144 (mapcar #'(lambda (elt)
|
|
145 (define-key
|
|
146 foomap
|
|
147 (if (characterp (car elt))
|
|
148 (char-to-string (car elt))
|
|
149 (car elt))
|
|
150 (vector (nth 1 elt))))
|
|
151 action-alist)
|
|
152 (set-keymap-parents foomap (list query-replace-map))
|
|
153 foomap)))
|
|
154 (unwind-protect
|
|
155 (progn
|
|
156 (if (stringp prompter)
|
|
157 (setq prompter (` (lambda (object)
|
|
158 (format (, prompter) object)))))
|
|
159 (while (funcall next)
|
|
160 (setq prompt (funcall prompter elt))
|
|
161 (cond ((stringp prompt)
|
|
162 ;; Prompt the user about this object.
|
|
163 (setq quit-flag nil)
|
155
|
164 (if mouse-event ; XEmacs
|
0
|
165 (setq def (or (get-dialog-box-response
|
|
166 mouse-event
|
|
167 (cons prompt map))
|
|
168 'quit))
|
|
169 ;; Prompt in the echo area.
|
|
170 (let ((cursor-in-echo-area (not no-cursor-in-echo-area)))
|
|
171 (display-message
|
|
172 'prompt
|
|
173 (format "%s(y, n, !, ., q, %sor %s) "
|
|
174 prompt user-keys
|
|
175 (key-description (vector help-char))))
|
|
176 (setq char (next-command-event))
|
|
177 ;; Show the answer to the question.
|
|
178 (display-message
|
|
179 'prompt
|
|
180 (format
|
|
181 "%s(y, n, !, ., q, %sor %s) %s"
|
|
182 prompt user-keys
|
|
183 (key-description (vector help-char))
|
|
184 (single-key-description char))))
|
|
185 (setq def (lookup-key map (vector char))))
|
|
186 (cond ((eq def 'exit)
|
|
187 (setq next (function (lambda () nil))))
|
|
188 ((eq def 'act)
|
|
189 ;; Act on the object.
|
|
190 (funcall actor elt)
|
|
191 (setq actions (1+ actions)))
|
|
192 ((eq def 'skip)
|
|
193 ;; Skip the object.
|
|
194 )
|
|
195 ((eq def 'act-and-exit)
|
|
196 ;; Act on the object and then exit.
|
|
197 (funcall actor elt)
|
|
198 (setq actions (1+ actions)
|
|
199 next (function (lambda () nil))))
|
|
200 ((or (eq def 'quit) (eq def 'exit-prefix))
|
|
201 (setq quit-flag t)
|
|
202 (setq next (` (lambda ()
|
|
203 (setq next '(, next))
|
|
204 '(, elt)))))
|
|
205 ((eq def 'automatic)
|
|
206 ;; Act on this and all following objects.
|
155
|
207 ;; (if (funcall prompter elt) ; Emacs
|
0
|
208 (if (eval (funcall prompter elt))
|
|
209 (progn
|
|
210 (funcall actor elt)
|
|
211 (setq actions (1+ actions))))
|
|
212 (while (funcall next)
|
155
|
213 ;; (funcall prompter elt) ; Emacs
|
0
|
214 (if (eval (funcall prompter elt))
|
|
215 (progn
|
|
216 (funcall actor elt)
|
|
217 (setq actions (1+ actions))))))
|
|
218 ((eq def 'help)
|
|
219 (with-output-to-temp-buffer "*Help*"
|
|
220 (princ
|
|
221 (let ((object (if help (nth 0 help) "object"))
|
|
222 (objects (if help (nth 1 help) "objects"))
|
|
223 (action (if help (nth 2 help) "act on")))
|
|
224 (concat
|
|
225 (format "Type SPC or `y' to %s the current %s;
|
|
226 DEL or `n' to skip the current %s;
|
|
227 ! to %s all remaining %s;
|
|
228 ESC or `q' to exit;\n"
|
|
229 action object object action objects)
|
|
230 (mapconcat (function
|
|
231 (lambda (elt)
|
|
232 (format "%c to %s"
|
|
233 (nth 0 elt)
|
|
234 (nth 2 elt))))
|
|
235 action-alist
|
|
236 ";\n")
|
|
237 (if action-alist ";\n")
|
|
238 (format "or . (period) to %s \
|
|
239 the current %s and exit."
|
|
240 action object))))
|
|
241 (save-excursion
|
|
242 (set-buffer standard-output)
|
|
243 (help-mode)))
|
|
244
|
|
245 (setq next (` (lambda ()
|
|
246 (setq next '(, next))
|
|
247 '(, elt)))))
|
|
248 ((vectorp def)
|
|
249 ;; A user-defined key.
|
|
250 (if (funcall (aref def 0) elt) ;Call its function.
|
|
251 ;; The function has eaten this object.
|
|
252 (setq actions (1+ actions))
|
|
253 ;; Regurgitated; try again.
|
|
254 (setq next (` (lambda ()
|
|
255 (setq next '(, next))
|
|
256 '(, elt))))))
|
155
|
257 ;((and (consp char) ; Emacs
|
0
|
258 ; (eq (car char) 'switch-frame))
|
|
259 ; ;; switch-frame event. Put it off until we're done.
|
|
260 ; (setq delayed-switch-frame char)
|
|
261 ; (setq next (` (lambda ()
|
|
262 ; (setq next '(, next))
|
|
263 ; '(, elt)))))
|
|
264 (t
|
|
265 ;; Random char.
|
|
266 (message "Type %s for help."
|
|
267 (key-description (vector help-char)))
|
|
268 (beep)
|
|
269 (sit-for 1)
|
|
270 (setq next (` (lambda ()
|
|
271 (setq next '(, next))
|
|
272 '(, elt)))))))
|
|
273 ((eval prompt)
|
|
274 (progn
|
|
275 (funcall actor elt)
|
|
276 (setq actions (1+ actions)))))))
|
155
|
277 ;;(if delayed-switch-frame
|
|
278 ;; (setq unread-command-events
|
|
279 ;; (cons delayed-switch-frame unread-command-events))))
|
|
280 ;; ((eval prompt)
|
|
281 ;; (progn
|
|
282 ;; (funcall actor elt)
|
|
283 ;; (setq actions (1+ actions)))))
|
0
|
284 )
|
|
285 ;; Clear the last prompt from the minibuffer.
|
|
286 (clear-message 'prompt)
|
|
287 ;; Return the number of actions that were taken.
|
155
|
288 actions))
|
0
|
289
|
|
290 ;;; map-ynp.el ends here
|