annotate lisp/map-ynp.el @ 381:0673e72f6c8c

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