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