Mercurial > emacs
annotate lisp/map-ynp.el @ 909:4c6cdb66c74c
*** empty log message ***
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Mon, 03 Aug 1992 02:02:37 +0000 |
parents | f7de428cb8bf |
children | e18a3f6689a9 |
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 |
840
113281b361ec
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
811
diff
changeset
|
3 ;;; Copyright (C) 1991, 1992 Free Software Foundation, Inc. |
113281b361ec
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
811
diff
changeset
|
4 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
659
diff
changeset
|
5 ;; Author: Roland McGrath <roland@gnu.ai.mit.edu> |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
807
diff
changeset
|
6 ;; Keywords: lisp, extensions |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
659
diff
changeset
|
7 |
304 | 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 ;;;###autoload |
891 | 36 (defun map-y-or-n-p (prompter actor list &optional help action-alist) |
304 | 37 "Ask a series of boolean questions. |
891 | 38 Takes args PROMPTER ACTOR LIST, and optional args HELP and ACTION-ALIST. |
304 | 39 |
40 LIST is a list of objects, or a function of no arguments to return the next | |
41 object or nil. | |
42 | |
580 | 43 If PROMPTER is a string, the prompt is \(format PROMPTER OBJECT\). If not |
572 | 44 a string, PROMPTER is a function of one arg (an object from LIST), which |
45 returns a string to be used as the prompt for that object. If the return | |
46 value is not a string, it is eval'd to get the answer; it may be nil to | |
47 ignore the object, t to act on the object without asking the user, or a | |
48 form to do a more complex prompt. | |
49 | |
304 | 50 ACTOR is a function of one arg (an object from LIST), |
51 which gets called with each object that the user answers `yes' for. | |
52 | |
53 If HELP is given, it is a list (OBJECT OBJECTS ACTION), | |
54 where OBJECT is a string giving the singular noun for an elt of LIST; | |
55 OBJECTS is the plural noun for elts of LIST, and ACTION is a transitive | |
56 verb describing ACTOR. The default is \(\"object\" \"objects\" \"act on\"\). | |
57 | |
58 At the prompts, the user may enter y, Y, or SPC to act on that object; | |
59 n, N, or DEL to skip that object; ! to act on all following objects; | |
60 ESC or q to exit (skip all following objects); . (period) to act on the | |
61 current object and then exit; or \\[help-command] to get help. | |
62 | |
891 | 63 If ACTION-ALIST is given, it is an alist (KEY FUNCTION HELP) of extra keys |
64 that will be accepted. KEY is a character; FUNCTION is a function of one | |
65 arg (an object from LIST); HELP is a string. When the user hits KEY, | |
66 FUNCTION is called. If it returns non-nil, the object is considered | |
67 \"acted upon\", and the next object from LIST is processed. If it returns | |
68 nil, the prompt is repeated for the same object. | |
69 | |
304 | 70 Returns the number of actions taken." |
474 | 71 (let* ((old-help-form help-form) |
891 | 72 (help-form (let ((object (if help (nth 0 help) "object")) |
73 (objects (if help (nth 1 help) "objects")) | |
74 (action (if help (nth 2 help) "act on"))) | |
75 (concat (format "Type SPC or `y' to %s the current %s; | |
76 DEL or `n' to skip the current %s; | |
77 ! to %s all remaining %s; | |
78 ESC or `q' to exit;\n" | |
79 action object object action objects) | |
80 (mapconcat (lambda (elt) | |
81 (format "%c to %s" | |
82 (nth 0 elt) | |
83 (nth 2 elt))) | |
84 action-alist | |
85 ";\n") | |
86 (if action-alist ";\n") | |
87 (format "or . (period) to %s \ | |
88 the current %s and exit." | |
89 action object)))) | |
90 (user-keys (if action-alist | |
91 (concat (mapconcat (lambda (elt) | |
92 (char-to-string (car elt))) | |
93 action-alist ", ") | |
94 " ") | |
95 "")) | |
474 | 96 (actions 0) |
891 | 97 prompt char elt tail |
474 | 98 (next (if (or (symbolp list) |
99 (subrp list) | |
100 (compiled-function-p list) | |
101 (and (consp list) | |
102 (eq (car list) 'lambda))) | |
103 (function (lambda () | |
104 (setq elt (funcall list)))) | |
105 (function (lambda () | |
106 (if list | |
107 (progn | |
108 (setq elt (car list) | |
109 list (cdr list)) | |
110 t) | |
111 nil)))))) | |
304 | 112 (if (stringp prompter) |
113 (setq prompter (` (lambda (object) | |
114 (format (, prompter) object))))) | |
415 | 115 (while (funcall next) |
304 | 116 (setq prompt (funcall prompter elt)) |
117 (if (stringp prompt) | |
118 (progn | |
119 ;; Prompt the user about this object. | |
120 (let ((cursor-in-echo-area t)) | |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
891
diff
changeset
|
121 (message "%s(y, n, !, ., q, %sor %s) " |
891 | 122 prompt user-keys |
123 (key-description (char-to-string help-char))) | |
304 | 124 (setq char (read-char))) |
125 (cond ((or (= ?q char) | |
126 (= ?\e char)) | |
127 (setq next (function (lambda () nil)))) | |
128 ((or (= ?y char) | |
129 (= ?Y char) | |
130 (= ? char)) | |
131 ;; Act on the object. | |
132 (let ((help-form old-help-form)) | |
133 (funcall actor elt)) | |
134 (setq actions (1+ actions))) | |
135 ((or (= ?n char) | |
136 (= ?N char) | |
137 (= ?\^? char)) | |
138 ;; Skip the object. | |
139 ) | |
140 ((= ?. char) | |
141 ;; Act on the object and then exit. | |
142 (funcall actor elt) | |
143 (setq actions (1+ actions) | |
144 next (function (lambda () nil)))) | |
145 ((= ?! char) | |
319 | 146 ;; Act on this and all following objects. |
147 (if (eval (funcall prompter elt)) | |
148 (progn | |
149 (funcall actor elt) | |
150 (setq actions (1+ actions)))) | |
474 | 151 (while (funcall next) |
319 | 152 (if (eval (funcall prompter elt)) |
304 | 153 (progn |
154 (funcall actor elt) | |
308 | 155 (setq actions (1+ actions)))))) |
304 | 156 ((= ?? char) |
157 (setq unread-command-char help-char) | |
319 | 158 (setq next (` (lambda () |
159 (setq next '(, next)) | |
160 '(, elt))))) | |
891 | 161 ((setq tail (assq char action-alist)) |
162 ;; A user-defined key. | |
163 (if (funcall (nth 1 tail) elt) ;Call its function. | |
164 ;; The function has eaten this object. | |
165 (setq actions (1+ actions)) | |
166 ;; Regurgitated; try again. | |
167 (setq next (` (lambda () | |
168 (setq next '(, next)) | |
169 '(, elt)))))) | |
304 | 170 (t |
171 ;; Random char. | |
172 (message "Type %s for help." | |
173 (key-description (char-to-string help-char))) | |
174 (beep) | |
175 (sit-for 1) | |
319 | 176 (setq next (` (lambda () |
177 (setq next '(, next)) | |
178 '(, elt))))))) | |
304 | 179 (if (eval prompt) |
180 (progn | |
319 | 181 (funcall actor elt) |
182 (setq actions (1+ actions)))))) | |
304 | 183 ;; Clear the last prompt from the minibuffer. |
184 (message "") | |
185 ;; Return the number of actions that were taken. | |
186 actions)) | |
659
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
580
diff
changeset
|
187 |
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
580
diff
changeset
|
188 ;;; map-ynp.el ends here |