]> code.delx.au - gnu-emacs/blob - lisp/map-ynp.el
(imenu--generic-function): Ignore text properties.
[gnu-emacs] / lisp / map-ynp.el
1 ;;; map-ynp.el --- General-purpose boolean question-asker.
2
3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
4
5 ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
6 ;; Keywords: lisp, extensions
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it 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 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; map-y-or-n-p is a general-purpose question-asking function.
28 ;; It asks a series of y/n questions (a la y-or-n-p), and decides to
29 ;; applies an action to each element of a list based on the answer.
30 ;; The nice thing is that you also get some other possible answers
31 ;; to use, reminiscent of query-replace: ! to answer y to all remaining
32 ;; questions; ESC or q to answer n to all remaining questions; . to answer
33 ;; y once and then n for the remainder; and you can get help with C-h.
34
35 ;;; Code:
36
37 ;;;###autoload
38 (defun map-y-or-n-p (prompter actor list &optional help action-alist
39 no-cursor-in-echo-area)
40 "Ask a series of boolean questions.
41 Takes args PROMPTER ACTOR LIST, and optional args HELP and ACTION-ALIST.
42
43 LIST is a list of objects, or a function of no arguments to return the next
44 object or nil.
45
46 If PROMPTER is a string, the prompt is \(format PROMPTER OBJECT\). If not
47 a string, PROMPTER is a function of one arg (an object from LIST), which
48 returns a string to be used as the prompt for that object. If the return
49 value is not a string, it may be nil to ignore the object or non-nil to act
50 on the object without asking the user.
51
52 ACTOR is a function of one arg (an object from LIST),
53 which gets called with each object that the user answers `yes' for.
54
55 If HELP is given, it is a list (OBJECT OBJECTS ACTION),
56 where OBJECT is a string giving the singular noun for an elt of LIST;
57 OBJECTS is the plural noun for elts of LIST, and ACTION is a transitive
58 verb describing ACTOR. The default is \(\"object\" \"objects\" \"act on\"\).
59
60 At the prompts, the user may enter y, Y, or SPC to act on that object;
61 n, N, or DEL to skip that object; ! to act on all following objects;
62 ESC or q to exit (skip all following objects); . (period) to act on the
63 current object and then exit; or \\[help-command] to get help.
64
65 If ACTION-ALIST is given, it is an alist (KEY FUNCTION HELP) of extra keys
66 that will be accepted. KEY is a character; FUNCTION is a function of one
67 arg (an object from LIST); HELP is a string. When the user hits KEY,
68 FUNCTION is called. If it returns non-nil, the object is considered
69 \"acted upon\", and the next object from LIST is processed. If it returns
70 nil, the prompt is repeated for the same object.
71
72 Final optional argument NO-CURSOR-IN-ECHO-AREA non-nil says not to set
73 `cursor-in-echo-area' while prompting.
74
75 This function uses `query-replace-map' to define the standard responses,
76 but not all of the responses which `query-replace' understands
77 are meaningful here.
78
79 Returns the number of actions taken."
80 (let* ((actions 0)
81 user-keys mouse-event map prompt char elt tail def
82 delayed-switch-frame
83 (next (if (or (and list (symbolp list))
84 (subrp list)
85 (byte-code-function-p list)
86 (and (consp list)
87 (eq (car list) 'lambda)))
88 (function (lambda ()
89 (setq elt (funcall list))))
90 (function (lambda ()
91 (if list
92 (progn
93 (setq elt (car list)
94 list (cdr list))
95 t)
96 nil))))))
97 (if (listp last-nonmenu-event)
98 ;; Make a list describing a dialog box.
99 (let ((object (capitalize (nth 0 help)))
100 (objects (capitalize (nth 1 help)))
101 (action (capitalize (nth 2 help))))
102 (setq map (` (("Yes" . act) ("No" . skip) ("Quit" . exit)
103 ((, (if help (concat action " " object " And Quit")
104 "Do it and Quit")) . act-and-exit)
105 ((, (if help (concat action " All " objects)
106 "Do All")) . automatic)
107 (,@ (mapcar (lambda (elt)
108 (cons (capitalize (nth 2 elt))
109 (vector (nth 1 elt))))
110 action-alist))))
111 mouse-event last-nonmenu-event))
112 (setq user-keys (if action-alist
113 (concat (mapconcat (function
114 (lambda (elt)
115 (key-description
116 (char-to-string (car elt)))))
117 action-alist ", ")
118 " ")
119 "")
120 ;; Make a map that defines each user key as a vector containing
121 ;; its definition.
122 map (cons 'keymap
123 (append (mapcar (lambda (elt)
124 (cons (car elt) (vector (nth 1 elt))))
125 action-alist)
126 query-replace-map))))
127 (unwind-protect
128 (progn
129 (if (stringp prompter)
130 (setq prompter (` (lambda (object)
131 (format (, prompter) object)))))
132 (while (funcall next)
133 (setq prompt (funcall prompter elt))
134 (cond ((stringp prompt)
135 ;; Prompt the user about this object.
136 (setq quit-flag nil)
137 (if mouse-event
138 (setq def (or (x-popup-dialog mouse-event
139 (cons prompt map))
140 'quit))
141 ;; Prompt in the echo area.
142 (let ((cursor-in-echo-area (not no-cursor-in-echo-area))
143 (message-log-max nil))
144 (message "%s(y, n, !, ., q, %sor %s) "
145 prompt user-keys
146 (key-description (vector help-char)))
147 (setq char (read-event))
148 ;; Show the answer to the question.
149 (message "%s(y, n, !, ., q, %sor %s) %s"
150 prompt user-keys
151 (key-description (vector help-char))
152 (single-key-description char)))
153 (setq def (lookup-key map (vector char))))
154 (cond ((eq def 'exit)
155 (setq next (function (lambda () nil))))
156 ((eq def 'act)
157 ;; Act on the object.
158 (funcall actor elt)
159 (setq actions (1+ actions)))
160 ((eq def 'skip)
161 ;; Skip the object.
162 )
163 ((eq def 'act-and-exit)
164 ;; Act on the object and then exit.
165 (funcall actor elt)
166 (setq actions (1+ actions)
167 next (function (lambda () nil))))
168 ((or (eq def 'quit) (eq def 'exit-prefix))
169 (setq quit-flag t)
170 (setq next (` (lambda ()
171 (setq next '(, next))
172 '(, elt)))))
173 ((eq def 'automatic)
174 ;; Act on this and all following objects.
175 (if (funcall prompter elt)
176 (progn
177 (funcall actor elt)
178 (setq actions (1+ actions))))
179 (while (funcall next)
180 (if (funcall prompter elt)
181 (progn
182 (funcall actor elt)
183 (setq actions (1+ actions))))))
184 ((eq def 'help)
185 (with-output-to-temp-buffer "*Help*"
186 (princ
187 (let ((object (if help (nth 0 help) "object"))
188 (objects (if help (nth 1 help) "objects"))
189 (action (if help (nth 2 help) "act on")))
190 (concat
191 (format "Type SPC or `y' to %s the current %s;
192 DEL or `n' to skip the current %s;
193 ! to %s all remaining %s;
194 ESC or `q' to exit;\n"
195 action object object action objects)
196 (mapconcat (function
197 (lambda (elt)
198 (format "%c to %s"
199 (nth 0 elt)
200 (nth 2 elt))))
201 action-alist
202 ";\n")
203 (if action-alist ";\n")
204 (format "or . (period) to %s \
205 the current %s and exit."
206 action object))))
207 (save-excursion
208 (set-buffer standard-output)
209 (help-mode)))
210
211 (setq next (` (lambda ()
212 (setq next '(, next))
213 '(, elt)))))
214 ((vectorp def)
215 ;; A user-defined key.
216 (if (funcall (aref def 0) elt) ;Call its function.
217 ;; The function has eaten this object.
218 (setq actions (1+ actions))
219 ;; Regurgitated; try again.
220 (setq next (` (lambda ()
221 (setq next '(, next))
222 '(, elt))))))
223 ((and (consp char)
224 (eq (car char) 'switch-frame))
225 ;; switch-frame event. Put it off until we're done.
226 (setq delayed-switch-frame char)
227 (setq next (` (lambda ()
228 (setq next '(, next))
229 '(, elt)))))
230 (t
231 ;; Random char.
232 (message "Type %s for help."
233 (key-description (vector help-char)))
234 (beep)
235 (sit-for 1)
236 (setq next (` (lambda ()
237 (setq next '(, next))
238 '(, elt)))))))
239 (prompt
240 (funcall actor elt)
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.
246 (let ((message-log-max nil))
247 (message ""))
248 ;; Return the number of actions that were taken.
249 actions))
250
251 ;;; map-ynp.el ends here