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