annotate lisp/map-ynp.el @ 670:bff41708644e

*** empty log message ***
author Richard M. Stallman <rms@gnu.org>
date Tue, 02 Jun 1992 02:33:00 +0000
parents 505130d1ddf8
children 4f28bd14272c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
659
505130d1ddf8 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 580
diff changeset
1 ;;; map-ynp.el --- General-purpose boolean question-asker.
505130d1ddf8 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 580
diff changeset
2
572
0c2e4ee42304 *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 474
diff changeset
3 ;;; Copyright (C) 1991, 1992 Free Software Foundation, Inc.
304
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
4 ;;; Written by Roland McGrath.
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
5 ;;;
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
6 ;;; This program is free software; you can redistribute it and/or modify
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
7 ;;; it under the terms of the GNU General Public License as published by
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
8 ;;; the Free Software Foundation; either version 1, or (at your option)
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
9 ;;; any later version.
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
10 ;;;
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
11 ;;; This program is distributed in the hope that it will be useful,
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
14 ;;; GNU General Public License for more details.
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
15 ;;;
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
16 ;;; A copy of the GNU General Public License can be obtained from this
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
17 ;;; program's author (send electronic mail to roland@ai.mit.edu) or from
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
18 ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
19 ;;; 02139, USA.
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
20 ;;;
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
21 ;;; map-y-or-n-p is a general-purpose question-asking function.
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
22 ;;; It asks a series of y/n questions (a la y-or-n-p), and decides to
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
23 ;;; applies an action to each element of a list based on the answer.
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
24 ;;; The nice thing is that you also get some other possible answers
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
25 ;;; to use, reminiscent of query-replace: ! to answer y to all remaining
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
26 ;;; questions; ESC or q to answer n to all remaining questions; . to answer
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
27 ;;; y once and then n for the remainder; and you can get help with C-h.
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
28
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
29 (defun map-y-or-n-p-help (object objects action)
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
30 (format "Type SPC or `y' to %s the current %s;
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
31 DEL or `n' to skip the current %s;
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
32 ! to %s all remaining %s;
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
33 ESC or `q' to exit;
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
34 or . (period) to %s the current %s and exit."
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
35 action object object action objects action object))
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
36
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
37 ;;;###autoload
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
38 (defun map-y-or-n-p (prompter actor list &optional help)
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
39 "Ask a series of boolean questions.
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
40 Takes args PROMPTER ACTOR LIST, and optional arg HELP.
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
41
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
42 LIST is a list of objects, or a function of no arguments to return the next
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
43 object or nil.
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
44
580
c8860f81ccf7 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 572
diff changeset
45 If PROMPTER is a string, the prompt is \(format PROMPTER OBJECT\). If not
572
0c2e4ee42304 *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 474
diff changeset
46 a string, PROMPTER is a function of one arg (an object from LIST), which
0c2e4ee42304 *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 474
diff changeset
47 returns a string to be used as the prompt for that object. If the return
0c2e4ee42304 *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 474
diff changeset
48 value is not a string, it is eval'd to get the answer; it may be nil to
0c2e4ee42304 *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 474
diff changeset
49 ignore the object, t to act on the object without asking the user, or a
0c2e4ee42304 *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 474
diff changeset
50 form to do a more complex prompt.
0c2e4ee42304 *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 474
diff changeset
51
304
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
52
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
53 ACTOR is a function of one arg (an object from LIST),
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
54 which gets called with each object that the user answers `yes' for.
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
55
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
56 If HELP is given, it is a list (OBJECT OBJECTS ACTION),
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
57 where OBJECT is a string giving the singular noun for an elt of LIST;
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
58 OBJECTS is the plural noun for elts of LIST, and ACTION is a transitive
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
59 verb describing ACTOR. The default is \(\"object\" \"objects\" \"act on\"\).
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
60
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
61 At the prompts, the user may enter y, Y, or SPC to act on that object;
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
62 n, N, or DEL to skip that object; ! to act on all following objects;
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
63 ESC or q to exit (skip all following objects); . (period) to act on the
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
64 current object and then exit; or \\[help-command] to get help.
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
65
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
66 Returns the number of actions taken."
474
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 415
diff changeset
67 (let* ((old-help-form help-form)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 415
diff changeset
68 (help-form (cons 'map-y-or-n-p-help
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 415
diff changeset
69 (or help '("object" "objects" "act on"))))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 415
diff changeset
70 (actions 0)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 415
diff changeset
71 prompt
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 415
diff changeset
72 char
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 415
diff changeset
73 elt
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 415
diff changeset
74 (next (if (or (symbolp list)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 415
diff changeset
75 (subrp list)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 415
diff changeset
76 (compiled-function-p list)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 415
diff changeset
77 (and (consp list)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 415
diff changeset
78 (eq (car list) 'lambda)))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 415
diff changeset
79 (function (lambda ()
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 415
diff changeset
80 (setq elt (funcall list))))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 415
diff changeset
81 (function (lambda ()
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 415
diff changeset
82 (if list
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 415
diff changeset
83 (progn
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 415
diff changeset
84 (setq elt (car list)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 415
diff changeset
85 list (cdr list))
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 415
diff changeset
86 t)
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 415
diff changeset
87 nil))))))
304
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
88 (if (stringp prompter)
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
89 (setq prompter (` (lambda (object)
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
90 (format (, prompter) object)))))
415
ba116e58de49 *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 408
diff changeset
91 (while (funcall next)
304
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
92 (setq prompt (funcall prompter elt))
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
93 (if (stringp prompt)
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
94 (progn
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
95 ;; Prompt the user about this object.
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
96 (let ((cursor-in-echo-area t))
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
97 (message "%s(y, n, ! ., q, or %s)"
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
98 prompt (key-description (char-to-string help-char)))
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
99 (setq char (read-char)))
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
100 (cond ((or (= ?q char)
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
101 (= ?\e char))
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
102 (setq next (function (lambda () nil))))
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
103 ((or (= ?y char)
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
104 (= ?Y char)
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
105 (= ? char))
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
106 ;; Act on the object.
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
107 (let ((help-form old-help-form))
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
108 (funcall actor elt))
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
109 (setq actions (1+ actions)))
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
110 ((or (= ?n char)
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
111 (= ?N char)
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
112 (= ?\^? char))
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
113 ;; Skip the object.
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
114 )
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
115 ((= ?. char)
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
116 ;; Act on the object and then exit.
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
117 (funcall actor elt)
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
118 (setq actions (1+ actions)
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
119 next (function (lambda () nil))))
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
120 ((= ?! char)
319
828a35833c4a *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 308
diff changeset
121 ;; Act on this and all following objects.
828a35833c4a *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 308
diff changeset
122 (if (eval (funcall prompter elt))
828a35833c4a *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 308
diff changeset
123 (progn
828a35833c4a *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 308
diff changeset
124 (funcall actor elt)
828a35833c4a *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 308
diff changeset
125 (setq actions (1+ actions))))
474
c3bbd755b7da *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 415
diff changeset
126 (while (funcall next)
319
828a35833c4a *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 308
diff changeset
127 (if (eval (funcall prompter elt))
304
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
128 (progn
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
129 (funcall actor elt)
308
71090e169ac8 *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 304
diff changeset
130 (setq actions (1+ actions))))))
304
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
131 ((= ?? char)
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
132 (setq unread-command-char help-char)
319
828a35833c4a *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 308
diff changeset
133 (setq next (` (lambda ()
828a35833c4a *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 308
diff changeset
134 (setq next '(, next))
828a35833c4a *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 308
diff changeset
135 '(, elt)))))
304
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
136 (t
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
137 ;; Random char.
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
138 (message "Type %s for help."
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
139 (key-description (char-to-string help-char)))
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
140 (beep)
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
141 (sit-for 1)
319
828a35833c4a *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 308
diff changeset
142 (setq next (` (lambda ()
828a35833c4a *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 308
diff changeset
143 (setq next '(, next))
828a35833c4a *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 308
diff changeset
144 '(, elt)))))))
304
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
145 (if (eval prompt)
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
146 (progn
319
828a35833c4a *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 308
diff changeset
147 (funcall actor elt)
828a35833c4a *** empty log message ***
Roland McGrath <roland@gnu.org>
parents: 308
diff changeset
148 (setq actions (1+ actions))))))
304
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
149 ;; Clear the last prompt from the minibuffer.
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
150 (message "")
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
151 ;; Return the number of actions that were taken.
f3dd86b71a52 Initial revision
Roland McGrath <roland@gnu.org>
parents:
diff changeset
152 actions))
659
505130d1ddf8 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 580
diff changeset
153
505130d1ddf8 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 580
diff changeset
154 ;;; map-ynp.el ends here