304
|
1 ;;; map-ynp.el -- General-purpose boolean question-asker.
|
572
|
2 ;;; Copyright (C) 1991, 1992 Free Software Foundation, Inc.
|
304
|
3 ;;; Written by Roland McGrath.
|
|
4 ;;;
|
|
5 ;;; This program is free software; you can redistribute it and/or modify
|
|
6 ;;; it under the terms of the GNU General Public License as published by
|
|
7 ;;; the Free Software Foundation; either version 1, or (at your option)
|
|
8 ;;; any later version.
|
|
9 ;;;
|
|
10 ;;; This program is distributed in the hope that it will be useful,
|
|
11 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 ;;; GNU General Public License for more details.
|
|
14 ;;;
|
|
15 ;;; A copy of the GNU General Public License can be obtained from this
|
|
16 ;;; program's author (send electronic mail to roland@ai.mit.edu) or from
|
|
17 ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
|
|
18 ;;; 02139, USA.
|
|
19 ;;;
|
|
20 ;;; map-y-or-n-p is a general-purpose question-asking function.
|
|
21 ;;; It asks a series of y/n questions (a la y-or-n-p), and decides to
|
|
22 ;;; applies an action to each element of a list based on the answer.
|
|
23 ;;; The nice thing is that you also get some other possible answers
|
|
24 ;;; to use, reminiscent of query-replace: ! to answer y to all remaining
|
|
25 ;;; questions; ESC or q to answer n to all remaining questions; . to answer
|
|
26 ;;; y once and then n for the remainder; and you can get help with C-h.
|
|
27
|
|
28 (defun map-y-or-n-p-help (object objects action)
|
|
29 (format "Type SPC or `y' to %s the current %s;
|
|
30 DEL or `n' to skip the current %s;
|
|
31 ! to %s all remaining %s;
|
|
32 ESC or `q' to exit;
|
|
33 or . (period) to %s the current %s and exit."
|
|
34 action object object action objects action object))
|
|
35
|
|
36 ;;;###autoload
|
|
37 (defun map-y-or-n-p (prompter actor list &optional help)
|
|
38 "Ask a series of boolean questions.
|
|
39 Takes args PROMPTER ACTOR LIST, and optional arg HELP.
|
|
40
|
|
41 LIST is a list of objects, or a function of no arguments to return the next
|
|
42 object or nil.
|
|
43
|
580
|
44 If PROMPTER is a string, the prompt is \(format PROMPTER OBJECT\). If not
|
572
|
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
|
304
|
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 Returns the number of actions taken."
|
474
|
66 (let* ((old-help-form help-form)
|
|
67 (help-form (cons 'map-y-or-n-p-help
|
|
68 (or help '("object" "objects" "act on"))))
|
|
69 (actions 0)
|
|
70 prompt
|
|
71 char
|
|
72 elt
|
|
73 (next (if (or (symbolp list)
|
|
74 (subrp list)
|
|
75 (compiled-function-p list)
|
|
76 (and (consp list)
|
|
77 (eq (car list) 'lambda)))
|
|
78 (function (lambda ()
|
|
79 (setq elt (funcall list))))
|
|
80 (function (lambda ()
|
|
81 (if list
|
|
82 (progn
|
|
83 (setq elt (car list)
|
|
84 list (cdr list))
|
|
85 t)
|
|
86 nil))))))
|
304
|
87 (if (stringp prompter)
|
|
88 (setq prompter (` (lambda (object)
|
|
89 (format (, prompter) object)))))
|
415
|
90 (while (funcall next)
|
304
|
91 (setq prompt (funcall prompter elt))
|
|
92 (if (stringp prompt)
|
|
93 (progn
|
|
94 ;; Prompt the user about this object.
|
|
95 (let ((cursor-in-echo-area t))
|
|
96 (message "%s(y, n, ! ., q, or %s)"
|
|
97 prompt (key-description (char-to-string help-char)))
|
|
98 (setq char (read-char)))
|
|
99 (cond ((or (= ?q char)
|
|
100 (= ?\e char))
|
|
101 (setq next (function (lambda () nil))))
|
|
102 ((or (= ?y char)
|
|
103 (= ?Y char)
|
|
104 (= ? char))
|
|
105 ;; Act on the object.
|
|
106 (let ((help-form old-help-form))
|
|
107 (funcall actor elt))
|
|
108 (setq actions (1+ actions)))
|
|
109 ((or (= ?n char)
|
|
110 (= ?N char)
|
|
111 (= ?\^? char))
|
|
112 ;; Skip the object.
|
|
113 )
|
|
114 ((= ?. char)
|
|
115 ;; Act on the object and then exit.
|
|
116 (funcall actor elt)
|
|
117 (setq actions (1+ actions)
|
|
118 next (function (lambda () nil))))
|
|
119 ((= ?! char)
|
319
|
120 ;; Act on this and all following objects.
|
|
121 (if (eval (funcall prompter elt))
|
|
122 (progn
|
|
123 (funcall actor elt)
|
|
124 (setq actions (1+ actions))))
|
474
|
125 (while (funcall next)
|
319
|
126 (if (eval (funcall prompter elt))
|
304
|
127 (progn
|
|
128 (funcall actor elt)
|
308
|
129 (setq actions (1+ actions))))))
|
304
|
130 ((= ?? char)
|
|
131 (setq unread-command-char help-char)
|
319
|
132 (setq next (` (lambda ()
|
|
133 (setq next '(, next))
|
|
134 '(, elt)))))
|
304
|
135 (t
|
|
136 ;; Random char.
|
|
137 (message "Type %s for help."
|
|
138 (key-description (char-to-string help-char)))
|
|
139 (beep)
|
|
140 (sit-for 1)
|
319
|
141 (setq next (` (lambda ()
|
|
142 (setq next '(, next))
|
|
143 '(, elt)))))))
|
304
|
144 (if (eval prompt)
|
|
145 (progn
|
319
|
146 (funcall actor elt)
|
|
147 (setq actions (1+ actions))))))
|
304
|
148 ;; Clear the last prompt from the minibuffer.
|
|
149 (message "")
|
|
150 ;; Return the number of actions that were taken.
|
|
151 actions))
|