209
|
1 ;;; map-ynp.el --- General-purpose boolean question-asker.
|
|
2
|
|
3 ;; Copyright (C) 1991-1995, 1997 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
|
|
6 ;; Keywords: lisp, extensions, dumped
|
|
7
|
|
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.
|
|
24
|
|
25 ;;; Synched up with: Emacs/Mule zeta.
|
|
26
|
|
27 ;;; Commentary:
|
|
28
|
|
29 ;; This file is dumped with XEmacs.
|
|
30
|
|
31 ;; map-y-or-n-p is a general-purpose question-asking function.
|
|
32 ;; It asks a series of y/n questions (a la y-or-n-p), and decides to
|
|
33 ;; applies an action to each element of a list based on the answer.
|
|
34 ;; The nice thing is that you also get some other possible answers
|
|
35 ;; to use, reminiscent of query-replace: ! to answer y to all remaining
|
|
36 ;; questions; ESC or q to answer n to all remaining questions; . to answer
|
|
37 ;; y once and then n for the remainder; and you can get help with C-h.
|
|
38
|
|
39 ;;; Code:
|
|
40
|
|
41 (defun map-y-or-n-p (prompter actor list &optional help action-alist
|
|
42 no-cursor-in-echo-area)
|
|
43 "Ask a series of boolean questions.
|
|
44 Takes args PROMPTER ACTOR LIST, and optional args HELP and ACTION-ALIST.
|
|
45
|
|
46 LIST is a list of objects, or a function of no arguments to return the next
|
|
47 object or nil.
|
|
48
|
|
49 If PROMPTER is a string, the prompt is \(format PROMPTER OBJECT\). If not
|
|
50 a string, PROMPTER is a function of one arg (an object from LIST), which
|
|
51 returns a string to be used as the prompt for that object. If the return
|
|
52 value is not a string, it may be nil to ignore the object or non-nil to act
|
|
53 on the object without asking the user.
|
|
54
|
|
55 ACTOR is a function of one arg (an object from LIST),
|
|
56 which gets called with each object that the user answers `yes' for.
|
|
57
|
|
58 If HELP is given, it is a list (OBJECT OBJECTS ACTION),
|
|
59 where OBJECT is a string giving the singular noun for an elt of LIST;
|
|
60 OBJECTS is the plural noun for elts of LIST, and ACTION is a transitive
|
|
61 verb describing ACTOR. The default is \(\"object\" \"objects\" \"act on\"\).
|
|
62
|
|
63 At the prompts, the user may enter y, Y, or SPC to act on that object;
|
|
64 n, N, or DEL to skip that object; ! to act on all following objects;
|
|
65 ESC or q to exit (skip all following objects); . (period) to act on the
|
|
66 current object and then exit; or \\[help-command] to get help.
|
|
67
|
|
68 If ACTION-ALIST is given, it is an alist (KEY FUNCTION HELP) of extra keys
|
|
69 that will be accepted. KEY is a character; FUNCTION is a function of one
|
|
70 arg (an object from LIST); HELP is a string. When the user hits KEY,
|
|
71 FUNCTION is called. If it returns non-nil, the object is considered
|
|
72 \"acted upon\", and the next object from LIST is processed. If it returns
|
|
73 nil, the prompt is repeated for the same object.
|
|
74
|
|
75 Final optional argument NO-CURSOR-IN-ECHO-AREA non-nil says not to set
|
|
76 `cursor-in-echo-area' while prompting.
|
|
77
|
|
78 This function uses `query-replace-map' to define the standard responses,
|
|
79 but not all of the responses which `query-replace' understands
|
|
80 are meaningful here.
|
|
81
|
|
82 Returns the number of actions taken."
|
|
83 (let* ((actions 0)
|
|
84 user-keys mouse-event map prompt char elt def
|
|
85 ;; Non-nil means we should use mouse menus to ask.
|
|
86 ;; use-menus
|
|
87 ;;delayed-switch-frame
|
|
88 (next (if (or (and list (symbolp list))
|
|
89 (subrp list)
|
|
90 (compiled-function-p list)
|
|
91 (and (consp list)
|
|
92 (eq (car list) 'lambda)))
|
|
93 (function (lambda ()
|
|
94 (setq elt (funcall list))))
|
|
95 (function (lambda ()
|
|
96 (if list
|
|
97 (progn
|
|
98 (setq elt (car list)
|
|
99 list (cdr list))
|
|
100 t)
|
|
101 nil))))))
|
|
102 (if (should-use-dialog-box-p)
|
|
103 ;; Make a list describing a dialog box.
|
|
104 (let (;; (object (capitalize (or (nth 0 help) "object")))
|
|
105 ;; (objects (capitalize (or (nth 1 help) "objects")))
|
|
106 ;; (action (capitalize (or (nth 2 help) "act on")))
|
|
107 )
|
|
108 (setq map `(("Yes" . act) ("No" . skip)
|
|
109 ; bogus crap. --ben
|
|
110 ; ((, (if help
|
|
111 ; (capitalize
|
|
112 ; (or (nth 3 help)
|
|
113 ; (concat action " All " objects)))
|
|
114 ; "Do All")) . automatic)
|
|
115 ; ((, (if help
|
|
116 ; (capitalize
|
|
117 ; (or (nth 4 help)
|
|
118 ; (concat action " " object " And Quit")))
|
|
119 ; "Do it and Quit")) . act-and-exit)
|
|
120 ; ((, (capitalize
|
|
121 ; (or (and help (nth 5 help)) "Quit")))
|
|
122 ; . exit)
|
|
123 ("Yes All" . automatic)
|
|
124 ("No All" . exit)
|
|
125 ("Cancel" . quit)
|
|
126 ,@(mapcar (lambda (elt)
|
|
127 (cons (capitalize (nth 2 elt))
|
|
128 (vector (nth 1 elt))))
|
|
129 action-alist))
|
|
130 mouse-event last-command-event))
|
|
131 (setq user-keys (if action-alist
|
|
132 (concat (mapconcat (function
|
|
133 (lambda (elt)
|
|
134 (key-description
|
|
135 (if (characterp (car elt))
|
|
136 ;; XEmacs
|
|
137 (char-to-string (car elt))
|
|
138 (car elt)))))
|
|
139 action-alist ", ")
|
|
140 " ")
|
|
141 "")
|
|
142 ;; Make a map that defines each user key as a vector containing
|
|
143 ;; its definition.
|
|
144 ;; XEmacs
|
|
145 map (let ((foomap (make-sparse-keymap)))
|
|
146 (mapcar #'(lambda (elt)
|
|
147 (define-key
|
|
148 foomap
|
|
149 (if (characterp (car elt))
|
|
150 (char-to-string (car elt))
|
|
151 (car elt))
|
|
152 (vector (nth 1 elt))))
|
|
153 action-alist)
|
|
154 (set-keymap-parents foomap (list query-replace-map))
|
|
155 foomap)))
|
|
156 (unwind-protect
|
|
157 (progn
|
|
158 (if (stringp prompter)
|
|
159 (setq prompter (` (lambda (object)
|
|
160 (format (, prompter) object)))))
|
|
161 (while (funcall next)
|
|
162 (setq prompt (funcall prompter elt))
|
|
163 (cond ((stringp prompt)
|
|
164 ;; Prompt the user about this object.
|
|
165 (setq quit-flag nil)
|
|
166 (if mouse-event ; XEmacs
|
|
167 (setq def (or (get-dialog-box-response
|
|
168 mouse-event
|
|
169 (cons prompt map))
|
|
170 'quit))
|
|
171 ;; Prompt in the echo area.
|
|
172 (let ((cursor-in-echo-area (not no-cursor-in-echo-area)))
|
|
173 (display-message
|
|
174 'prompt
|
|
175 (format "%s(y, n, !, ., q, %sor %s) "
|
|
176 prompt user-keys
|
|
177 (key-description (vector help-char))))
|
|
178 (setq char (next-command-event))
|
|
179 ;; Show the answer to the question.
|
|
180 (display-message
|
|
181 'prompt
|
|
182 (format
|
|
183 "%s(y, n, !, ., q, %sor %s) %s"
|
|
184 prompt user-keys
|
|
185 (key-description (vector help-char))
|
|
186 (single-key-description char))))
|
|
187 (setq def (lookup-key map (vector char))))
|
|
188 (cond ((eq def 'exit)
|
|
189 (setq next (function (lambda () nil))))
|
|
190 ((eq def 'act)
|
|
191 ;; Act on the object.
|
|
192 (funcall actor elt)
|
|
193 (setq actions (1+ actions)))
|
|
194 ((eq def 'skip)
|
|
195 ;; Skip the object.
|
|
196 )
|
|
197 ((eq def 'act-and-exit)
|
|
198 ;; Act on the object and then exit.
|
|
199 (funcall actor elt)
|
|
200 (setq actions (1+ actions)
|
|
201 next (function (lambda () nil))))
|
|
202 ((or (eq def 'quit) (eq def 'exit-prefix))
|
|
203 (setq quit-flag t)
|
|
204 (setq next (` (lambda ()
|
|
205 (setq next '(, next))
|
|
206 '(, elt)))))
|
|
207 ((eq def 'automatic)
|
|
208 ;; Act on this and all following objects.
|
|
209 ;; (if (funcall prompter elt) ; Emacs
|
|
210 (if (eval (funcall prompter elt))
|
|
211 (progn
|
|
212 (funcall actor elt)
|
|
213 (setq actions (1+ actions))))
|
|
214 (while (funcall next)
|
|
215 ;; (funcall prompter elt) ; Emacs
|
|
216 (if (eval (funcall prompter elt))
|
|
217 (progn
|
|
218 (funcall actor elt)
|
|
219 (setq actions (1+ actions))))))
|
|
220 ((eq def 'help)
|
|
221 (with-output-to-temp-buffer "*Help*"
|
|
222 (princ
|
|
223 (let ((object (if help (nth 0 help) "object"))
|
|
224 (objects (if help (nth 1 help) "objects"))
|
|
225 (action (if help (nth 2 help) "act on")))
|
|
226 (concat
|
|
227 (format "Type SPC or `y' to %s the current %s;
|
|
228 DEL or `n' to skip the current %s;
|
|
229 ! to %s all remaining %s;
|
|
230 ESC or `q' to exit;\n"
|
|
231 action object object action objects)
|
|
232 (mapconcat (function
|
|
233 (lambda (elt)
|
|
234 (format "%c to %s"
|
|
235 (nth 0 elt)
|
|
236 (nth 2 elt))))
|
|
237 action-alist
|
|
238 ";\n")
|
|
239 (if action-alist ";\n")
|
|
240 (format "or . (period) to %s \
|
|
241 the current %s and exit."
|
|
242 action object))))
|
|
243 (save-excursion
|
|
244 (set-buffer standard-output)
|
|
245 (help-mode)))
|
|
246
|
|
247 (setq next (` (lambda ()
|
|
248 (setq next '(, next))
|
|
249 '(, elt)))))
|
|
250 ((vectorp def)
|
|
251 ;; A user-defined key.
|
|
252 (if (funcall (aref def 0) elt) ;Call its function.
|
|
253 ;; The function has eaten this object.
|
|
254 (setq actions (1+ actions))
|
|
255 ;; Regurgitated; try again.
|
|
256 (setq next (` (lambda ()
|
|
257 (setq next '(, next))
|
|
258 '(, elt))))))
|
|
259 ;((and (consp char) ; Emacs
|
|
260 ; (eq (car char) 'switch-frame))
|
|
261 ; ;; switch-frame event. Put it off until we're done.
|
|
262 ; (setq delayed-switch-frame char)
|
|
263 ; (setq next (` (lambda ()
|
|
264 ; (setq next '(, next))
|
|
265 ; '(, elt)))))
|
|
266 (t
|
|
267 ;; Random char.
|
|
268 (message "Type %s for help."
|
|
269 (key-description (vector help-char)))
|
|
270 (beep)
|
|
271 (sit-for 1)
|
|
272 (setq next (` (lambda ()
|
|
273 (setq next '(, next))
|
|
274 '(, elt)))))))
|
|
275 ((eval prompt)
|
|
276 (progn
|
|
277 (funcall actor elt)
|
|
278 (setq actions (1+ actions)))))))
|
|
279 ;;(if delayed-switch-frame
|
|
280 ;; (setq unread-command-events
|
|
281 ;; (cons delayed-switch-frame unread-command-events))))
|
|
282 ;; ((eval prompt)
|
|
283 ;; (progn
|
|
284 ;; (funcall actor elt)
|
|
285 ;; (setq actions (1+ actions)))))
|
|
286 )
|
|
287 ;; Clear the last prompt from the minibuffer.
|
|
288 (clear-message 'prompt)
|
|
289 ;; Return the number of actions that were taken.
|
|
290 actions))
|
|
291
|
|
292 ;;; map-ynp.el ends here
|