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