1 ;;; map-ynp.el --- General-purpose boolean question-asker.
3 ;;; Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
5 ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
6 ;; Keywords: lisp, extensions
8 ;;; This program is free software; you can redistribute it and/or modify
9 ;;; it under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 2, or (at your option)
11 ;;; any later version.
13 ;;; This program is distributed in the hope that it will be useful,
14 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; A copy of the GNU General Public License can be obtained from this
19 ;;; program's author (send electronic mail to roland@ai.mit.edu) or from
20 ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
25 ;;; map-y-or-n-p is a general-purpose question-asking function.
26 ;;; It asks a series of y/n questions (a la y-or-n-p), and decides to
27 ;;; applies an action to each element of a list based on the answer.
28 ;;; The nice thing is that you also get some other possible answers
29 ;;; to use, reminiscent of query-replace: ! to answer y to all remaining
30 ;;; questions; ESC or q to answer n to all remaining questions; . to answer
31 ;;; y once and then n for the remainder; and you can get help with C-h.
36 (defun map-y-or-n-p (prompter actor list
&optional help action-alist
37 no-cursor-in-echo-area
)
38 "Ask a series of boolean questions.
39 Takes args PROMPTER ACTOR LIST, and optional args HELP and ACTION-ALIST.
41 LIST is a list of objects, or a function of no arguments to return the next
44 If PROMPTER is a string, the prompt is \(format PROMPTER OBJECT\). If not
45 a string, PROMPTER is a function of one arg (an object from LIST), which
46 returns a string to be used as the prompt for that object. If the return
47 value is not a string, it is eval'd to get the answer; it may be nil to
48 ignore the object, t to act on the object without asking the user, or a
49 form to do a more complex prompt.
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.
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\"\).
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.
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.
71 Final optional argument NO-CURSOR-IN-ECHO-AREA non-nil says not to set
72 `cursor-in-echo-area' while prompting.
74 This function uses `query-replace-map' to define the standard responses,
75 but not all of the responses which `query-replace' understands
78 Returns the number of actions taken."
80 user-keys mouse-event map prompt char elt tail def
82 (next (if (or (and list
(symbolp list
))
84 (byte-code-function-p list
)
86 (eq (car list
) 'lambda
)))
88 (setq elt
(funcall list
))))
96 (if (listp last-nonmenu-event
)
97 ;; Make a list describing a dialog box.
98 (let ((object (capitalize (nth 0 help
)))
99 (objects (capitalize (nth 1 help
)))
100 (action (capitalize (nth 2 help
))))
101 (setq map
(` (("Yes" . act
) ("No" . skip
) ("Quit" . exit
)
102 ((, (if help
(concat action
" " object
" And Quit")
103 "Do it and Quit")) . act-and-exit
)
104 ((, (if help
(concat action
" All " objects
)
105 "Do All")) . automatic
)
106 (,@ (mapcar (lambda (elt)
107 (cons (capitalize (nth 2 elt
))
108 (vector (nth 1 elt
))))
110 mouse-event last-nonmenu-event
))
111 (setq user-keys
(if action-alist
112 (concat (mapconcat (function
115 (char-to-string (car elt
)))))
119 ;; Make a map that defines each user key as a vector containing
122 (append (mapcar (lambda (elt)
123 (cons (car elt
) (vector (nth 1 elt
))))
125 query-replace-map
))))
128 (if (stringp prompter
)
129 (setq prompter
(` (lambda (object)
130 (format (, prompter
) object
)))))
131 (while (funcall next
)
132 (setq prompt
(funcall prompter elt
))
136 ;; Prompt the user about this object.
138 (setq def
(or (x-popup-dialog mouse-event
141 ;; Prompt in the echo area.
142 (let ((cursor-in-echo-area (not no-cursor-in-echo-area
)))
143 (message "%s(y, n, !, ., q, %sor %s) "
145 (key-description (vector help-char
)))
146 (setq char
(read-event)))
147 ;; Show the answer to the question.
148 (message "%s(y, n, !, ., q, %sor %s) %s"
150 (key-description (vector help-char
))
151 (single-key-description char
))
152 (setq def
(lookup-key map
(vector char
))))
153 (cond ((eq def
'exit
)
154 (setq next
(function (lambda () nil
))))
156 ;; Act on the object.
158 (setq actions
(1+ actions
)))
162 ((eq def
'act-and-exit
)
163 ;; Act on the object and then exit.
165 (setq actions
(1+ actions
)
166 next
(function (lambda () nil
))))
167 ((or (eq def
'quit
) (eq def
'exit-prefix
))
169 (setq next
(` (lambda ()
170 (setq next
'(, next
))
173 ;; Act on this and all following objects.
174 (if (eval (funcall prompter elt
))
177 (setq actions
(1+ actions
))))
178 (while (funcall next
)
179 (if (eval (funcall prompter elt
))
182 (setq actions
(1+ actions
))))))
184 (with-output-to-temp-buffer "*Help*"
186 (let ((object (if help
(nth 0 help
) "object"))
187 (objects (if help
(nth 1 help
) "objects"))
188 (action (if help
(nth 2 help
) "act on")))
190 (format "Type SPC or `y' to %s the current %s;
191 DEL or `n' to skip the current %s;
192 ! to %s all remaining %s;
193 ESC or `q' to exit;\n"
194 action object object action objects
)
202 (if action-alist
";\n")
203 (format "or . (period) to %s \
204 the current %s and exit."
207 (set-buffer standard-output
)
210 (setq next
(` (lambda ()
211 (setq next
'(, next
))
214 ;; A user-defined key.
215 (if (funcall (aref def
0) elt
) ;Call its function.
216 ;; The function has eaten this object.
217 (setq actions
(1+ actions
))
218 ;; Regurgitated; try again.
219 (setq next
(` (lambda ()
220 (setq next
'(, next
))
223 (eq (car char
) 'switch-frame
))
224 ;; switch-frame event. Put it off until we're done.
225 (setq delayed-switch-frame char
)
226 (setq next
(` (lambda ()
227 (setq next
'(, next
))
231 (message "Type %s for help."
232 (key-description (vector help-char
)))
235 (setq next
(` (lambda ()
236 (setq next
'(, next
))
241 (setq actions
(1+ actions
)))))))
242 (if delayed-switch-frame
243 (setq unread-command-events
244 (cons delayed-switch-frame unread-command-events
))))
245 ;; Clear the last prompt from the minibuffer.
247 ;; Return the number of actions that were taken.
250 ;;; map-ynp.el ends here