1 ;;; map-ynp.el --- General-purpose boolean question-asker.
3 ;;; Copyright (C) 1991, 1992 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 "Ask a series of boolean questions.
38 Takes args PROMPTER ACTOR LIST, and optional args HELP and ACTION-ALIST.
40 LIST is a list of objects, or a function of no arguments to return the next
43 If PROMPTER is a string, the prompt is \(format PROMPTER OBJECT\). If not
44 a string, PROMPTER is a function of one arg (an object from LIST), which
45 returns a string to be used as the prompt for that object. If the return
46 value is not a string, it is eval'd to get the answer; it may be nil to
47 ignore the object, t to act on the object without asking the user, or a
48 form to do a more complex prompt.
50 ACTOR is a function of one arg (an object from LIST),
51 which gets called with each object that the user answers `yes' for.
53 If HELP is given, it is a list (OBJECT OBJECTS ACTION),
54 where OBJECT is a string giving the singular noun for an elt of LIST;
55 OBJECTS is the plural noun for elts of LIST, and ACTION is a transitive
56 verb describing ACTOR. The default is \(\"object\" \"objects\" \"act on\"\).
58 At the prompts, the user may enter y, Y, or SPC to act on that object;
59 n, N, or DEL to skip that object; ! to act on all following objects;
60 ESC or q to exit (skip all following objects); . (period) to act on the
61 current object and then exit; or \\[help-command] to get help.
63 If ACTION-ALIST is given, it is an alist (KEY FUNCTION HELP) of extra keys
64 that will be accepted. KEY is a character; FUNCTION is a function of one
65 arg (an object from LIST); HELP is a string. When the user hits KEY,
66 FUNCTION is called. If it returns non-nil, the object is considered
67 \"acted upon\", and the next object from LIST is processed. If it returns
68 nil, the prompt is repeated for the same object.
70 Returns the number of actions taken."
71 (let* ((old-help-form help-form
)
72 (help-form (let ((object (if help
(nth 0 help
) "object"))
73 (objects (if help
(nth 1 help
) "objects"))
74 (action (if help
(nth 2 help
) "act on")))
75 (concat (format "Type SPC or `y' to %s the current %s;
76 DEL or `n' to skip the current %s;
77 ! to %s all remaining %s;
78 ESC or `q' to exit;\n"
79 action object object action objects
)
80 (mapconcat (lambda (elt)
86 (if action-alist
";\n")
87 (format "or . (period) to %s \
88 the current %s and exit."
90 (user-keys (if action-alist
91 (concat (mapconcat (lambda (elt)
93 (char-to-string (car elt
))))
99 (next (if (or (symbolp list
)
101 (compiled-function-p list
)
103 (eq (car list
) 'lambda
)))
105 (setq elt
(funcall list
))))
113 (if (stringp prompter
)
114 (setq prompter
(` (lambda (object)
115 (format (, prompter
) object
)))))
116 (while (funcall next
)
117 (setq prompt
(funcall prompter elt
))
120 ;; Prompt the user about this object.
121 (let ((cursor-in-echo-area t
))
122 (message "%s(y, n, !, ., q, %sor %s) "
124 (key-description (char-to-string help-char
)))
125 (setq char
(read-char)))
126 (cond ((or (= ?q char
)
128 (setq next
(function (lambda () nil
))))
132 ;; Act on the object.
133 (let ((help-form old-help-form
))
135 (setq actions
(1+ actions
)))
142 ;; Act on the object and then exit.
144 (setq actions
(1+ actions
)
145 next
(function (lambda () nil
))))
147 ;; Act on this and all following objects.
148 (if (eval (funcall prompter elt
))
151 (setq actions
(1+ actions
))))
152 (while (funcall next
)
153 (if (eval (funcall prompter elt
))
156 (setq actions
(1+ actions
))))))
158 (setq unread-command-char help-char
)
159 (setq next
(` (lambda ()
160 (setq next
'(, next
))
162 ((setq tail
(assq char action-alist
))
163 ;; A user-defined key.
164 (if (funcall (nth 1 tail
) elt
) ;Call its function.
165 ;; The function has eaten this object.
166 (setq actions
(1+ actions
))
167 ;; Regurgitated; try again.
168 (setq next
(` (lambda ()
169 (setq next
'(, next
))
173 (message "Type %s for help."
174 (key-description (char-to-string help-char
)))
177 (setq next
(` (lambda ()
178 (setq next
'(, next
))
183 (setq actions
(1+ actions
))))))
184 ;; Clear the last prompt from the minibuffer.
186 ;; Return the number of actions that were taken.
189 ;;; map-ynp.el ends here