1 ;;; map-ynp.el --- General-purpose boolean question-asker.
3 ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
4 ;; Last-Modified: 14 Mar 1992
6 ;;; Copyright (C) 1991, 1992 Free Software Foundation, Inc.
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.
35 (defun map-y-or-n-p-help (object objects action
)
36 (format "Type SPC or `y' to %s the current %s;
37 DEL or `n' to skip the current %s;
38 ! to %s all remaining %s;
40 or . (period) to %s the current %s and exit."
41 action object object action objects action object
))
44 (defun map-y-or-n-p (prompter actor list
&optional help
)
45 "Ask a series of boolean questions.
46 Takes args PROMPTER ACTOR LIST, and optional arg HELP.
48 LIST is a list of objects, or a function of no arguments to return the next
51 If PROMPTER is a string, the prompt is \(format PROMPTER OBJECT\). If not
52 a string, PROMPTER is a function of one arg (an object from LIST), which
53 returns a string to be used as the prompt for that object. If the return
54 value is not a string, it is eval'd to get the answer; it may be nil to
55 ignore the object, t to act on the object without asking the user, or a
56 form to do a more complex prompt.
59 ACTOR is a function of one arg (an object from LIST),
60 which gets called with each object that the user answers `yes' for.
62 If HELP is given, it is a list (OBJECT OBJECTS ACTION),
63 where OBJECT is a string giving the singular noun for an elt of LIST;
64 OBJECTS is the plural noun for elts of LIST, and ACTION is a transitive
65 verb describing ACTOR. The default is \(\"object\" \"objects\" \"act on\"\).
67 At the prompts, the user may enter y, Y, or SPC to act on that object;
68 n, N, or DEL to skip that object; ! to act on all following objects;
69 ESC or q to exit (skip all following objects); . (period) to act on the
70 current object and then exit; or \\[help-command] to get help.
72 Returns the number of actions taken."
73 (let* ((old-help-form help-form
)
74 (help-form (cons 'map-y-or-n-p-help
75 (or help
'("object" "objects" "act on"))))
80 (next (if (or (symbolp list
)
82 (compiled-function-p list
)
84 (eq (car list
) 'lambda
)))
86 (setq elt
(funcall list
))))
94 (if (stringp prompter
)
95 (setq prompter
(` (lambda (object)
96 (format (, prompter
) object
)))))
98 (setq prompt
(funcall prompter elt
))
101 ;; Prompt the user about this object.
102 (let ((cursor-in-echo-area t
))
103 (message "%s(y, n, ! ., q, or %s)"
104 prompt
(key-description (char-to-string help-char
)))
105 (setq char
(read-char)))
106 (cond ((or (= ?q char
)
108 (setq next
(function (lambda () nil
))))
112 ;; Act on the object.
113 (let ((help-form old-help-form
))
115 (setq actions
(1+ actions
)))
122 ;; Act on the object and then exit.
124 (setq actions
(1+ actions
)
125 next
(function (lambda () nil
))))
127 ;; Act on this and all following objects.
128 (if (eval (funcall prompter elt
))
131 (setq actions
(1+ actions
))))
132 (while (funcall next
)
133 (if (eval (funcall prompter elt
))
136 (setq actions
(1+ actions
))))))
138 (setq unread-command-char help-char
)
139 (setq next
(` (lambda ()
140 (setq next
'(, next
))
144 (message "Type %s for help."
145 (key-description (char-to-string help-char
)))
148 (setq next
(` (lambda ()
149 (setq next
'(, next
))
154 (setq actions
(1+ actions
))))))
155 ;; Clear the last prompt from the minibuffer.
157 ;; Return the number of actions that were taken.
160 ;;; map-ynp.el ends here