1 ;;; map-ynp.el --- General-purpose boolean question-asker.
3 ;; Copyright (C) 1991-1995, 1997 Free Software Foundation, Inc.
5 ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
6 ;; Keywords: lisp, extensions, dumped
8 ;; This file is part of XEmacs.
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)
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.
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
25 ;;; Synched up with: Emacs/Mule zeta.
29 ;; This file is dumped with XEmacs.
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.
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.
46 LIST is a list of objects, or a function of no arguments to return the next
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.
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.
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\"\).
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.
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.
75 Final optional argument NO-CURSOR-IN-ECHO-AREA non-nil says not to set
76 `cursor-in-echo-area' while prompting.
78 This function uses `query-replace-map' to define the standard responses,
79 but not all of the responses which `query-replace' understands
82 Returns the number of actions taken."
84 user-keys mouse-event map prompt char elt def
85 ;; Non-nil means we should use mouse menus to ask.
87 ;;delayed-switch-frame
88 (next (if (or (and list (symbolp list))
90 (compiled-function-p list)
92 (eq (car list) 'lambda)))
93 #'(lambda () (setq elt (funcall list)))
101 (if (should-use-dialog-box-p)
102 ;; Make a list describing a dialog box.
103 (let (;; (object (capitalize (or (nth 0 help) "object")))
104 ;; (objects (capitalize (or (nth 1 help) "objects")))
105 ;; (action (capitalize (or (nth 2 help) "act on")))
107 (setq map `(("%_Yes" . act) ("%_No" . skip)
112 ; (concat action " All " objects)))
113 ; "Do All")) . automatic)
117 ; (concat action " " object " And Quit")))
118 ; "Do it and Quit")) . act-and-exit)
120 ; (or (and help (nth 5 help)) "Quit")))
122 ("Yes %_All" . automatic)
125 ,@(mapcar #'(lambda (elt)
126 (cons (capitalize (nth 2 elt))
127 (vector (nth 1 elt))))
129 mouse-event last-command-event))
130 (setq user-keys (if action-alist
131 (concat (mapconcat #'(lambda (elt)
133 (if (characterp (car elt))
135 (char-to-string (car elt))
140 ;; Make a map that defines each user key as a vector containing
143 map (let ((foomap (make-sparse-keymap)))
144 (mapcar #'(lambda (elt)
147 (if (characterp (car elt))
148 (char-to-string (car elt))
150 (vector (nth 1 elt))))
152 (set-keymap-parents foomap (list query-replace-map))
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.
164 (if mouse-event ; XEmacs
165 (setq def (or (get-dialog-box-response
169 ;; Prompt in the echo area.
170 (let ((cursor-in-echo-area (not no-cursor-in-echo-area)))
173 (format "%s(y, n, !, ., q, %sor %s) "
175 (key-description (vector help-char))))
176 (setq char (next-command-event))
177 ;; Show the answer to the question.
181 "%s(y, n, !, ., q, %sor %s) %s"
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 #'(lambda () nil)))
189 ;; Act on the object.
191 (setq actions (1+ actions)))
195 ((eq def 'act-and-exit)
196 ;; Act on the object and then exit.
198 (setq actions (1+ actions)
199 next (function (lambda () nil))))
200 ((or (eq def 'quit) (eq def 'exit-prefix))
202 (setq next `(lambda ()
206 ;; Act on this and all following objects.
207 ;; (if (funcall prompter elt) ; Emacs
208 (if (eval (funcall prompter elt))
211 (setq actions (1+ actions))))
212 (while (funcall next)
213 ;; (funcall prompter elt) ; Emacs
214 (if (eval (funcall prompter elt))
217 (setq actions (1+ actions))))))
219 (with-output-to-temp-buffer "*Help*"
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")))
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)
234 (normalize-menu-item-name
238 (if action-alist ";\n")
239 (format "or . (period) to %s \
240 the current %s and exit."
243 (set-buffer standard-output)
246 (setq next `(lambda ()
250 ;; A user-defined key.
251 (if (funcall (aref def 0) elt) ;Call its function.
252 ;; The function has eaten this object.
253 (setq actions (1+ actions))
254 ;; Regurgitated; try again.
255 (setq next `(lambda ()
258 ;((and (consp char) ; Emacs
259 ; (eq (car char) 'switch-frame))
260 ; ;; switch-frame event. Put it off until we're done.
261 ; (setq delayed-switch-frame char)
262 ; (setq next `(lambda ()
267 (message "Type %s for help."
268 (key-description (vector help-char)))
271 (setq next `(lambda ()
277 (setq actions (1+ actions)))))))
278 ;;(if delayed-switch-frame
279 ;; (setq unread-command-events
280 ;; (cons delayed-switch-frame unread-command-events))))
283 ;; (funcall actor elt)
284 ;; (setq actions (1+ actions)))))
286 ;; Clear the last prompt from the minibuffer.
287 (clear-message 'prompt)
288 ;; Return the number of actions that were taken.
291 ;;; map-ynp.el ends here