1 ;;; map-ynp.el --- general-purpose boolean question-asker
3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Roland McGrath <roland@gnu.org>
8 ;; Keywords: lisp, extensions
10 ;; This file is part of GNU Emacs.
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)
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.
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
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
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.
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.
44 LIST is a list of objects, or a function of no arguments to return the next
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.
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.
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\"\).
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.
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.
73 Final optional argument NO-CURSOR-IN-ECHO-AREA non-nil says not to set
74 `cursor-in-echo-area' while prompting.
76 This function uses `query-replace-map' to define the standard responses,
77 but not all of the responses which `query-replace' understands
80 Returns the number of actions taken."
82 user-keys mouse-event map prompt char elt tail def
83 ;; Non-nil means we should use mouse menus to ask.
86 (next (if (or (and list
(symbolp list
))
88 (byte-code-function-p list
)
90 (eq (car list
) 'lambda
)))
92 (setq elt
(funcall list
))))
100 (if (and (listp last-nonmenu-event
)
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
)
107 ,@(mapcar (lambda (elt)
108 (cons (with-syntax-table
109 text-mode-syntax-table
110 (capitalize (nth 2 elt
)))
111 (vector (nth 1 elt
))))
113 (,(if help
(concat action
" This But No More")
114 "Do This But No More") . act-and-exit
)
115 (,(if help
(concat action
" All " objects
)
116 "Do All") . automatic
)
117 ("No For All" . exit
))
119 mouse-event last-nonmenu-event
))
120 (setq user-keys
(if action-alist
121 (concat (mapconcat (function
124 (char-to-string (car elt
)))))
128 ;; Make a map that defines each user key as a vector containing
131 (append (mapcar (lambda (elt)
132 (cons (car elt
) (vector (nth 1 elt
))))
134 query-replace-map
))))
137 (if (stringp prompter
)
138 (setq prompter
`(lambda (object)
139 (format ,prompter object
))))
140 (while (funcall next
)
141 (setq prompt
(funcall prompter elt
))
142 (cond ((stringp prompt
)
143 ;; Prompt the user about this object.
146 (setq def
(or (x-popup-dialog (or mouse-event use-menus
)
149 ;; Prompt in the echo area.
150 (let ((cursor-in-echo-area (not no-cursor-in-echo-area
))
151 (message-log-max nil
))
152 (message "%s(y, n, !, ., q, %sor %s) "
154 (key-description (vector help-char
)))
155 (if minibuffer-auto-raise
156 (raise-frame (window-frame (minibuffer-window))))
158 (setq char
(read-event))
159 ;; If we get -1, from end of keyboard
162 ;; Show the answer to the question.
163 (message "%s(y, n, !, ., q, %sor %s) %s"
165 (key-description (vector help-char
))
166 (single-key-description char
)))
167 (setq def
(lookup-key map
(vector char
))))
168 (cond ((eq def
'exit
)
169 (setq next
(function (lambda () nil
))))
171 ;; Act on the object.
173 (setq actions
(1+ actions
)))
177 ((eq def
'act-and-exit
)
178 ;; Act on the object and then exit.
180 (setq actions
(1+ actions
)
181 next
(function (lambda () nil
))))
184 (setq next
`(lambda ()
188 ;; Act on this and all following objects.
189 (if (funcall prompter elt
)
192 (setq actions
(1+ actions
))))
193 (while (funcall next
)
194 (if (funcall prompter elt
)
197 (setq actions
(1+ actions
))))))
199 (with-output-to-temp-buffer "*Help*"
201 (let ((object (if help
(nth 0 help
) "object"))
202 (objects (if help
(nth 1 help
) "objects"))
203 (action (if help
(nth 2 help
) "act on")))
205 (format "Type SPC or `y' to %s the current %s;
206 DEL or `n' to skip the current %s;
207 RET or `q' to give up on the %s (skip all remaining %s);
208 C-g to quit (cancel the whole command);
209 ! to %s all remaining %s;\n"
210 action object object action objects action
215 (single-key-description
220 (if action-alist
";\n")
221 (format "or . (period) to %s \
222 the current %s and exit."
225 (set-buffer standard-output
)
228 (setq next
`(lambda ()
232 ;; A user-defined key.
233 (if (funcall (aref def
0) elt
) ;Call its function.
234 ;; The function has eaten this object.
235 (setq actions
(1+ actions
))
236 ;; Regurgitated; try again.
237 (setq next
`(lambda ()
241 (eq (car char
) 'switch-frame
))
242 ;; switch-frame event. Put it off until we're done.
243 (setq delayed-switch-frame char
)
244 (setq next
`(lambda ()
249 (message "Type %s for help."
250 (key-description (vector help-char
)))
253 (setq next
`(lambda ()
258 (setq actions
(1+ actions
))))))
259 (if delayed-switch-frame
260 (setq unread-command-events
261 (cons delayed-switch-frame unread-command-events
))))
262 ;; Clear the last prompt from the minibuffer.
263 (let ((message-log-max nil
))
265 ;; Return the number of actions that were taken.
268 ;;; arch-tag: 1d0a3201-a151-4c10-b231-4da47c9e6dc3
269 ;;; map-ynp.el ends here