1 ;;; map-ynp.el --- General-purpose boolean question-asker.
3 ;;; Copyright (C) 1991, 1992 Free Software Foundation, Inc.
4 ;;; Written by Roland McGrath.
6 ;;; This program is free software; you can redistribute it and/or modify
7 ;;; it under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 1, or (at your option)
11 ;;; This program is distributed in the hope that it will be useful,
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; A copy of the GNU General Public License can be obtained from this
17 ;;; program's author (send electronic mail to roland@ai.mit.edu) or from
18 ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
21 ;;; map-y-or-n-p is a general-purpose question-asking function.
22 ;;; It asks a series of y/n questions (a la y-or-n-p), and decides to
23 ;;; applies an action to each element of a list based on the answer.
24 ;;; The nice thing is that you also get some other possible answers
25 ;;; to use, reminiscent of query-replace: ! to answer y to all remaining
26 ;;; questions; ESC or q to answer n to all remaining questions; . to answer
27 ;;; y once and then n for the remainder; and you can get help with C-h.
29 (defun map-y-or-n-p-help (object objects action
)
30 (format "Type SPC or `y' to %s the current %s;
31 DEL or `n' to skip the current %s;
32 ! to %s all remaining %s;
34 or . (period) to %s the current %s and exit."
35 action object object action objects action object
))
38 (defun map-y-or-n-p (prompter actor list
&optional help
)
39 "Ask a series of boolean questions.
40 Takes args PROMPTER ACTOR LIST, and optional arg HELP.
42 LIST is a list of objects, or a function of no arguments to return the next
45 If PROMPTER is a string, the prompt is \(format PROMPTER OBJECT\). If not
46 a string, PROMPTER is a function of one arg (an object from LIST), which
47 returns a string to be used as the prompt for that object. If the return
48 value is not a string, it is eval'd to get the answer; it may be nil to
49 ignore the object, t to act on the object without asking the user, or a
50 form to do a more complex prompt.
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 Returns the number of actions taken."
67 (let* ((old-help-form help-form
)
68 (help-form (cons 'map-y-or-n-p-help
69 (or help
'("object" "objects" "act on"))))
74 (next (if (or (symbolp list
)
76 (compiled-function-p list
)
78 (eq (car list
) 'lambda
)))
80 (setq elt
(funcall list
))))
88 (if (stringp prompter
)
89 (setq prompter
(` (lambda (object)
90 (format (, prompter
) object
)))))
92 (setq prompt
(funcall prompter elt
))
95 ;; Prompt the user about this object.
96 (let ((cursor-in-echo-area t
))
97 (message "%s(y, n, ! ., q, or %s)"
98 prompt
(key-description (char-to-string help-char
)))
99 (setq char
(read-char)))
100 (cond ((or (= ?q char
)
102 (setq next
(function (lambda () nil
))))
106 ;; Act on the object.
107 (let ((help-form old-help-form
))
109 (setq actions
(1+ actions
)))
116 ;; Act on the object and then exit.
118 (setq actions
(1+ actions
)
119 next
(function (lambda () nil
))))
121 ;; Act on this and all following objects.
122 (if (eval (funcall prompter elt
))
125 (setq actions
(1+ actions
))))
126 (while (funcall next
)
127 (if (eval (funcall prompter elt
))
130 (setq actions
(1+ actions
))))))
132 (setq unread-command-char help-char
)
133 (setq next
(` (lambda ()
134 (setq next
'(, next
))
138 (message "Type %s for help."
139 (key-description (char-to-string help-char
)))
142 (setq next
(` (lambda ()
143 (setq next
'(, next
))
148 (setq actions
(1+ actions
))))))
149 ;; Clear the last prompt from the minibuffer.
151 ;; Return the number of actions that were taken.
154 ;;; map-ynp.el ends here