Mercurial > hg > xemacs-beta
comparison lisp/utils/map-ynp.el @ 0:376386a54a3c r19-14
Import from CVS: tag r19-14
author | cvs |
---|---|
date | Mon, 13 Aug 2007 08:45:50 +0200 |
parents | |
children | bcdc7deadc19 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:376386a54a3c |
---|---|
1 ;;; map-ynp.el --- General-purpose boolean question-asker | |
2 | |
3 ;;; Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc. | |
4 | |
5 ;; Author: Roland McGrath <roland@gnu.ai.mit.edu> | |
6 ;; Keywords: lisp, extensions | |
7 | |
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 1, or (at your option) | |
11 ;;; any later version. | |
12 ;;; | |
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. | |
17 ;;; | |
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 | |
21 ;;; 02139, USA. | |
22 | |
23 ;;; Synched up with: FSF 19.30. | |
24 | |
25 ;;; Commentary: | |
26 | |
27 ;;; map-y-or-n-p is a general-purpose question-asking function. | |
28 ;;; It asks a series of y/n questions (a la y-or-n-p), and decides to | |
29 ;;; applies an action to each element of a list based on the answer. | |
30 ;;; The nice thing is that you also get some other possible answers | |
31 ;;; to use, reminiscent of query-replace: ! to answer y to all remaining | |
32 ;;; questions; ESC or q to answer n to all remaining questions; . to answer | |
33 ;;; y once and then n for the remainder; and you can get help with C-h. | |
34 | |
35 ;;; Code: | |
36 | |
37 ;; Note: the old code used help-form. That might be a good | |
38 ;; idea in general, but the code for execute-help-form needs | |
39 ;; to be moved into Lisp so that it can do things like put | |
40 ;; the buffer into `help-mode'. | |
41 | |
42 (defun map-y-or-n-p (prompter actor list &optional help action-alist | |
43 no-cursor-in-echo-area) | |
44 "Ask a series of boolean questions. | |
45 Takes args PROMPTER ACTOR LIST, and optional args HELP and ACTION-ALIST. | |
46 | |
47 LIST is a list of objects, or a function of no arguments to return the next | |
48 object or nil. | |
49 | |
50 If PROMPTER is a string, the prompt is \(format PROMPTER OBJECT\). If not | |
51 a string, PROMPTER is a function of one arg (an object from LIST), which | |
52 returns a string to be used as the prompt for that object. If the return | |
53 value is not a string, it may be nil to ignore the object or non-nil to act | |
54 on the object without asking the user. | |
55 | |
56 ACTOR is a function of one arg (an object from LIST), | |
57 which gets called with each object that the user answers `yes' for. | |
58 | |
59 If HELP is given, it is a list | |
60 (OBJECT OBJECTS ACTION), | |
61 where OBJECT is a string giving the singular noun for an elt of LIST; | |
62 OBJECTS is the plural noun for elts of LIST, and ACTION is a transitive | |
63 verb describing ACTOR. The default is \(\"object\" \"objects\" \"act on\"\). | |
64 | |
65 At the prompts, the user may enter y, Y, or SPC to act on that object; | |
66 n, N, or DEL to skip that object; ! to act on all following objects; | |
67 ESC or q to exit (skip all following objects); . (period) to act on the | |
68 current object and then exit; or \\[help-command] to get help. | |
69 | |
70 If ACTION-ALIST is given, it is an alist (KEY FUNCTION HELP) of extra keys | |
71 that will be accepted. KEY is a character; FUNCTION is a function of one | |
72 arg (an object from LIST); HELP is a string. When the user hits KEY, | |
73 FUNCTION is called. If it returns non-nil, the object is considered | |
74 \"acted upon\", and the next object from LIST is processed. If it returns | |
75 nil, the prompt is repeated for the same object. | |
76 | |
77 Final optional argument NO-CURSOR-IN-ECHO-AREA non-nil says not to set | |
78 `cursor-in-echo-area' while prompting. | |
79 | |
80 This function uses `query-replace-map' to define the standard responses, | |
81 but not all of the responses which `query-replace' understands | |
82 are meaningful here. | |
83 | |
84 Returns the number of actions taken." | |
85 (let* (;(old-help-form help-form) | |
86 ;(help-form (cons 'map-y-or-n-p-help | |
87 ; (or help '("object" "objects" "act on")))) | |
88 (actions 0) | |
89 user-keys mouse-event map prompt char elt def | |
90 ;delayed-switch-frame | |
91 (next (if (or (and list (symbolp list)) | |
92 (subrp list) | |
93 (compiled-function-p list) | |
94 (and (consp list) | |
95 (eq (car list) 'lambda))) | |
96 (function (lambda () | |
97 (setq elt (funcall list)))) | |
98 (function (lambda () | |
99 (if list | |
100 (progn | |
101 (setq elt (car list) | |
102 list (cdr list)) | |
103 t) | |
104 nil)))))) | |
105 (if (should-use-dialog-box-p) | |
106 ;; Make a list describing a dialog box. | |
107 (let ((object (capitalize (or (nth 0 help) "object"))) | |
108 (objects (capitalize (or (nth 1 help) "objects"))) | |
109 (action (capitalize (or (nth 2 help) "act on")))) | |
110 (setq map `(("Yes" . act) ("No" . skip) | |
111 ; bogus crap. --ben | |
112 ; ((, (if help | |
113 ; (capitalize | |
114 ; (or (nth 3 help) | |
115 ; (concat action " All " objects))) | |
116 ; "Do All")) . automatic) | |
117 ; ((, (if help | |
118 ; (capitalize | |
119 ; (or (nth 4 help) | |
120 ; (concat action " " object " And Quit"))) | |
121 ; "Do it and Quit")) . act-and-exit) | |
122 ; ((, (capitalize | |
123 ; (or (and help (nth 5 help)) "Quit"))) | |
124 ; . exit) | |
125 ("Yes All" . automatic) | |
126 ("No All" . exit) | |
127 ("Cancel" . quit) | |
128 ,@(mapcar (lambda (elt) | |
129 (cons (capitalize (nth 2 elt)) | |
130 (vector (nth 1 elt)))) | |
131 action-alist)) | |
132 mouse-event last-command-event)) | |
133 (setq user-keys (if action-alist | |
134 (concat (mapconcat (function | |
135 (lambda (elt) | |
136 (key-description | |
137 (if (characterp (car elt)) | |
138 (char-to-string (car elt)) | |
139 (car elt))))) | |
140 action-alist ", ") | |
141 " ") | |
142 "") | |
143 ;; Make a map that defines each user key as a vector containing | |
144 ;; its definition. | |
145 map (let ((foomap (make-sparse-keymap))) | |
146 (mapcar #'(lambda (elt) | |
147 (define-key | |
148 foomap | |
149 (if (characterp (car elt)) | |
150 (char-to-string (car elt)) | |
151 (car elt)) | |
152 (vector (nth 1 elt)))) | |
153 action-alist) | |
154 (set-keymap-parents foomap (list query-replace-map)) | |
155 foomap))) | |
156 (unwind-protect | |
157 (progn | |
158 (if (stringp prompter) | |
159 (setq prompter (` (lambda (object) | |
160 (format (, prompter) object))))) | |
161 (while (funcall next) | |
162 (setq prompt (funcall prompter elt)) | |
163 (cond ((stringp prompt) | |
164 ;; Prompt the user about this object. | |
165 (setq quit-flag nil) | |
166 (if mouse-event | |
167 (setq def (or (get-dialog-box-response | |
168 mouse-event | |
169 (cons prompt map)) | |
170 'quit)) | |
171 ;; Prompt in the echo area. | |
172 (let ((cursor-in-echo-area (not no-cursor-in-echo-area))) | |
173 (display-message | |
174 'prompt | |
175 (format "%s(y, n, !, ., q, %sor %s) " | |
176 prompt user-keys | |
177 (key-description (vector help-char)))) | |
178 (setq char (next-command-event)) | |
179 ;; Show the answer to the question. | |
180 (display-message | |
181 'prompt | |
182 (format | |
183 "%s(y, n, !, ., q, %sor %s) %s" | |
184 prompt user-keys | |
185 (key-description (vector help-char)) | |
186 (single-key-description char)))) | |
187 (setq def (lookup-key map (vector char)))) | |
188 (cond ((eq def 'exit) | |
189 (setq next (function (lambda () nil)))) | |
190 ((eq def 'act) | |
191 ;; Act on the object. | |
192 ;(let ((help-form old-help-form)) | |
193 (funcall actor elt) | |
194 (setq actions (1+ actions))) | |
195 ((eq def 'skip) | |
196 ;; Skip the object. | |
197 ) | |
198 ((eq def 'act-and-exit) | |
199 ;; Act on the object and then exit. | |
200 (funcall actor elt) | |
201 (setq actions (1+ actions) | |
202 next (function (lambda () nil)))) | |
203 ((or (eq def 'quit) (eq def 'exit-prefix)) | |
204 (setq quit-flag t) | |
205 (setq next (` (lambda () | |
206 (setq next '(, next)) | |
207 '(, elt))))) | |
208 | |
209 ((eq def 'automatic) | |
210 ;; Act on this and all following objects. | |
211 (if (eval (funcall prompter elt)) | |
212 (progn | |
213 (funcall actor elt) | |
214 (setq actions (1+ actions)))) | |
215 (while (funcall next) | |
216 (if (eval (funcall prompter elt)) | |
217 (progn | |
218 (funcall actor elt) | |
219 (setq actions (1+ actions)))))) | |
220 ((eq def 'help) | |
221 (with-output-to-temp-buffer "*Help*" | |
222 (princ | |
223 (let ((object (if help (nth 0 help) "object")) | |
224 (objects (if help (nth 1 help) "objects")) | |
225 (action (if help (nth 2 help) "act on"))) | |
226 (concat | |
227 (format "Type SPC or `y' to %s the current %s; | |
228 DEL or `n' to skip the current %s; | |
229 ! to %s all remaining %s; | |
230 ESC or `q' to exit;\n" | |
231 action object object action objects) | |
232 (mapconcat (function | |
233 (lambda (elt) | |
234 (format "%c to %s" | |
235 (nth 0 elt) | |
236 (nth 2 elt)))) | |
237 action-alist | |
238 ";\n") | |
239 (if action-alist ";\n") | |
240 (format "or . (period) to %s \ | |
241 the current %s and exit." | |
242 action object)))) | |
243 (save-excursion | |
244 (set-buffer standard-output) | |
245 (help-mode))) | |
246 | |
247 (setq next (` (lambda () | |
248 (setq next '(, next)) | |
249 '(, elt))))) | |
250 ((vectorp def) | |
251 ;; A user-defined key. | |
252 (if (funcall (aref def 0) elt) ;Call its function. | |
253 ;; The function has eaten this object. | |
254 (setq actions (1+ actions)) | |
255 ;; Regurgitated; try again. | |
256 (setq next (` (lambda () | |
257 (setq next '(, next)) | |
258 '(, elt)))))) | |
259 ;((and (consp char) | |
260 ; (eq (car char) 'switch-frame)) | |
261 ; ;; switch-frame event. Put it off until we're done. | |
262 ; (setq delayed-switch-frame char) | |
263 ; (setq next (` (lambda () | |
264 ; (setq next '(, next)) | |
265 ; '(, elt))))) | |
266 (t | |
267 ;; Random char. | |
268 (message "Type %s for help." | |
269 (key-description (vector help-char))) | |
270 (beep) | |
271 (sit-for 1) | |
272 (setq next (` (lambda () | |
273 (setq next '(, next)) | |
274 '(, elt))))))) | |
275 ((eval prompt) | |
276 (progn | |
277 (funcall actor elt) | |
278 (setq actions (1+ actions))))))) | |
279 ;(if delayed-switch-frame | |
280 ; (setq unread-command-events | |
281 ; (cons delayed-switch-frame unread-command-events))) | |
282 ) | |
283 ;; Clear the last prompt from the minibuffer. | |
284 (clear-message 'prompt) | |
285 ;; Return the number of actions that were taken. | |
286 actions)) | |
287 | |
288 ;;; map-ynp.el ends here |