]> code.delx.au - gnu-emacs/blob - lisp/map-ynp.el
Comment changes.
[gnu-emacs] / lisp / map-ynp.el
1 ;;; map-ynp.el --- General-purpose boolean question-asker.
2
3 ;;; Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
4
5 ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
6 ;; Keywords: lisp, extensions
7
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.
12 ;;;
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.
17 ;;;
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
21 ;;; 02139, USA.
22
23 ;;; Commentary:
24
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.
32
33 ;;; Code:
34
35 ;;;###autoload
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.
40
41 LIST is a list of objects, or a function of no arguments to return the next
42 object or nil.
43
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.
50
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.
53
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\"\).
58
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.
63
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.
70
71 Final optional argument NO-CURSOR-IN-ECHO-AREA non-nil says not to set
72 `cursor-in-echo-area' while prompting.
73
74 This function uses `query-replace-map' to define the standard responses,
75 but not all of the responses which `query-replace' understands
76 are meaningful here.
77
78 Returns the number of actions taken."
79 (let* ((user-keys (if action-alist
80 (concat (mapconcat (function
81 (lambda (elt)
82 (key-description
83 (char-to-string (car elt)))))
84 action-alist ", ")
85 " ")
86 ""))
87 ;; Make a map that defines each user key as a vector containing
88 ;; its definition.
89 (map (cons 'keymap
90 (append (mapcar (lambda (elt)
91 (cons (car elt) (vector (nth 1 elt))))
92 action-alist)
93 query-replace-map)))
94 (actions 0)
95 prompt char elt tail def delayed-switch-frame
96 (next (if (or (and list (symbolp list))
97 (subrp list)
98 (byte-code-function-p list)
99 (and (consp list)
100 (eq (car list) 'lambda)))
101 (function (lambda ()
102 (setq elt (funcall list))))
103 (function (lambda ()
104 (if list
105 (progn
106 (setq elt (car list)
107 list (cdr list))
108 t)
109 nil))))))
110 (unwind-protect
111 (progn
112 (if (stringp prompter)
113 (setq prompter (` (lambda (object)
114 (format (, prompter) object)))))
115 (while (funcall next)
116 (setq prompt (funcall prompter elt))
117 (if (stringp prompt)
118 (progn
119 (setq quit-flag nil)
120 ;; Prompt the user about this object.
121 (let ((cursor-in-echo-area (not no-cursor-in-echo-area)))
122 (message "%s(y, n, !, ., q, %sor %s) "
123 prompt user-keys
124 (key-description (vector help-char)))
125 (setq char (read-event)))
126 ;; Show the answer to the question.
127 (message "%s(y, n, !, ., q, %sor %s) %s"
128 prompt user-keys
129 (key-description (vector help-char))
130 (single-key-description char))
131 (setq def (lookup-key map (vector char)))
132 (cond ((eq def 'exit)
133 (setq next (function (lambda () nil))))
134 ((eq def 'act)
135 ;; Act on the object.
136 (funcall actor elt)
137 (setq actions (1+ actions)))
138 ((eq def 'skip)
139 ;; Skip the object.
140 )
141 ((eq def 'act-and-exit)
142 ;; Act on the object and then exit.
143 (funcall actor elt)
144 (setq actions (1+ actions)
145 next (function (lambda () nil))))
146 ((eq def 'quit)
147 (setq quit-flag t)
148 (setq next (` (lambda ()
149 (setq next '(, next))
150 '(, elt)))))
151 ((eq def 'automatic)
152 ;; Act on this and all following objects.
153 (if (eval (funcall prompter elt))
154 (progn
155 (funcall actor elt)
156 (setq actions (1+ actions))))
157 (while (funcall next)
158 (if (eval (funcall prompter elt))
159 (progn
160 (funcall actor elt)
161 (setq actions (1+ actions))))))
162 ((eq def 'help)
163 (with-output-to-temp-buffer "*Help*"
164 (princ
165 (let ((object (if help (nth 0 help) "object"))
166 (objects (if help (nth 1 help) "objects"))
167 (action (if help (nth 2 help) "act on")))
168 (concat
169 (format "Type SPC or `y' to %s the current %s;
170 DEL or `n' to skip the current %s;
171 ! to %s all remaining %s;
172 ESC or `q' to exit;\n"
173 action object object action objects)
174 (mapconcat (function
175 (lambda (elt)
176 (format "%c to %s"
177 (nth 0 elt)
178 (nth 2 elt))))
179 action-alist
180 ";\n")
181 (if action-alist ";\n")
182 (format "or . (period) to %s \
183 the current %s and exit."
184 action object)))))
185
186 (setq next (` (lambda ()
187 (setq next '(, next))
188 '(, elt)))))
189 ((vectorp def)
190 ;; A user-defined key.
191 (if (funcall (aref def 0) elt) ;Call its function.
192 ;; The function has eaten this object.
193 (setq actions (1+ actions))
194 ;; Regurgitated; try again.
195 (setq next (` (lambda ()
196 (setq next '(, next))
197 '(, elt))))))
198 ((and (consp char)
199 (eq (car char) 'switch-frame))
200 ;; switch-frame event. Put it off until we're done.
201 (setq delayed-switch-frame char)
202 (setq next (` (lambda ()
203 (setq next '(, next))
204 '(, elt)))))
205 (t
206 ;; Random char.
207 (message "Type %s for help."
208 (key-description (vector help-char)))
209 (beep)
210 (sit-for 1)
211 (setq next (` (lambda ()
212 (setq next '(, next))
213 '(, elt)))))))
214 (if (eval prompt)
215 (progn
216 (funcall actor elt)
217 (setq actions (1+ actions)))))))
218 (if delayed-switch-frame
219 (setq unread-command-events
220 (cons delayed-switch-frame unread-command-events))))
221 ;; Clear the last prompt from the minibuffer.
222 (message "")
223 ;; Return the number of actions that were taken.
224 actions))
225
226 ;;; map-ynp.el ends here