This commit was generated by cvs2svn to track changes on a CVS vendor
[unix-history] / usr.bin / vi / ex / ex_args.c
CommitLineData
395c4480
AM
1/*-
2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char sccsid[] = "@(#)ex_args.c 8.16 (Berkeley) 3/14/94";
36#endif /* not lint */
37
38#include <sys/types.h>
39#include <queue.h>
40#include <sys/time.h>
41
42#include <bitstring.h>
43#include <errno.h>
44#include <limits.h>
45#include <signal.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49#include <termios.h>
50
51#include <db.h>
52#include <regex.h>
53
54#include "vi.h"
55#include "excmd.h"
56
57/*
58 * ex_next -- :next [files]
59 * Edit the next file, optionally setting the list of files.
60 *
61 * !!!
62 * The :next command behaved differently from the :rewind command in
63 * historic vi. See nvi/docs/autowrite for details, but the basic
64 * idea was that it ignored the force flag if the autowrite flag was
65 * set. This implementation handles them all identically.
66 */
67int
68ex_next(sp, ep, cmdp)
69 SCR *sp;
70 EXF *ep;
71 EXCMDARG *cmdp;
72{
73 ARGS **argv;
74 FREF *frp;
75 char *name;
76
77 MODIFY_RET(sp, ep, F_ISSET(cmdp, E_FORCE));
78
79 if (cmdp->argc) {
80 /* Mark all the current files as ignored. */
81 for (frp = sp->frefq.cqh_first;
82 frp != (FREF *)&sp->frefq; frp = frp->q.cqe_next)
83 F_SET(frp, FR_IGNORE);
84
85 /* Add the new files into the file list. */
86 for (argv = cmdp->argv; argv[0]->len != 0; ++argv)
87 if (file_add(sp, NULL, argv[0]->bp, 0) == NULL)
88 return (1);
89
90 if ((frp = file_first(sp)) == NULL)
91 return (1);
92 } else if ((frp = file_next(sp, sp->a_frp)) == NULL) {
93 msgq(sp, M_ERR, "No more files to edit.");
94 return (1);
95 }
96
97 /*
98 * There's a tricky sequence, where the user edits two files, e.g.
99 * "x" and "y". While in "x", they do ":e y|:f foo", which changes
100 * the name of that FRP entry. Then, the :n command finds the file
101 * "y" with a name change. If the file name has been changed, get
102 * a new FREF for the original file name, and make it be the one that
103 * is displayed in the argument list, not the one with the name change.
104 */
105 if (frp->cname != NULL) {
106 F_SET(frp, FR_IGNORE);
107 name = frp->name == NULL ? frp->tname : frp->name;
108 if ((frp = file_add(sp, sp->a_frp, name, 0)) == NULL)
109 return (1);
110 }
111 if (file_init(sp, frp, NULL, F_ISSET(cmdp, E_FORCE)))
112 return (1);
113 sp->a_frp = frp;
114 F_SET(sp, S_FSWITCH);
115 return (0);
116}
117
118/*
119 * ex_prev -- :prev
120 * Edit the previous file.
121 */
122int
123ex_prev(sp, ep, cmdp)
124 SCR *sp;
125 EXF *ep;
126 EXCMDARG *cmdp;
127{
128 FREF *frp;
129 char *name;
130
131 MODIFY_RET(sp, ep, F_ISSET(cmdp, E_FORCE));
132
133 if ((frp = file_prev(sp, sp->a_frp)) == NULL) {
134 msgq(sp, M_ERR, "No previous files to edit.");
135 return (1);
136 }
137
138 /* See comment in ex_next(). */
139 if (frp->cname != NULL) {
140 F_SET(frp, FR_IGNORE);
141 name = frp->name == NULL ? frp->tname : frp->name;
142 if ((frp = file_add(sp, frp, name, 0)) == NULL)
143 return (1);
144 }
145 if (file_init(sp, frp, NULL, F_ISSET(cmdp, E_FORCE)))
146 return (1);
147 sp->a_frp = frp;
148 F_SET(sp, S_FSWITCH);
149 return (0);
150}
151
152/*
153 * ex_rew -- :rew
154 * Re-edit the list of files.
155 */
156int
157ex_rew(sp, ep, cmdp)
158 SCR *sp;
159 EXF *ep;
160 EXCMDARG *cmdp;
161{
162 FREF *frp, *tfrp;
163
164 /*
165 * !!!
166 * Historic practice -- you can rewind to the current file.
167 */
168 if ((frp = file_first(sp)) == NULL) {
169 msgq(sp, M_ERR, "No previous files to rewind.");
170 return (1);
171 }
172
173 MODIFY_RET(sp, ep, F_ISSET(cmdp, E_FORCE));
174
175 /*
176 * !!!
177 * Historic practice, turn off the edited bit. The :next and :prev
178 * code will discard any name changes, so ignore them here. Start
179 * at the beginning of the file, too.
180 */
181 for (tfrp = sp->frefq.cqh_first;
182 tfrp != (FREF *)&sp->frefq; tfrp = tfrp->q.cqe_next)
183 F_CLR(tfrp, FR_CHANGEWRITE | FR_CURSORSET | FR_EDITED);
184
185 if (file_init(sp, frp, NULL, F_ISSET(cmdp, E_FORCE)))
186 return (1);
187 sp->a_frp = frp;
188 F_SET(sp, S_FSWITCH);
189 return (0);
190}
191
192/*
193 * ex_args -- :args
194 * Display the list of files.
195 */
196int
197ex_args(sp, ep, cmdp)
198 SCR *sp;
199 EXF *ep;
200 EXCMDARG *cmdp;
201{
202 FREF *frp;
203 int cnt, col, iscur, len, nlen, sep;
204 char *name;
205
206 /*
207 * !!!
208 * Ignore files that aren't in the "argument" list unless they are the
209 * one we're currently editing. I'm not sure this is right, but the
210 * historic vi behavior of not showing the current file if it was the
211 * result of a ":e" command, or if the file name was changed was wrong.
212 * This is actually pretty tricky, don't modify it without thinking it
213 * through. There have been a lot of problems in here.
214 *
215 * Also, historic practice was to display the original name of the file
216 * even if the user had used a file command to change the file name.
217 * Confusing, at best. We show both names: the original as that's what
218 * the user will get in a next, prev or rewind, and the new one since
219 * that's what the user is actually editing now.
220 *
221 * When we find the "argument" FREF, i.e. the current location in the
222 * user's argument list, if it's not the same as the current FREF, we
223 * display the current FREF as following the argument in the list.
224 * This means that if the user edits three files, "x", "y" and "z", and
225 * then does a :e command in the file "x" to edit "z", "z" will appear
226 * in the list twice.
227 */
228 col = len = sep = 0;
229 for (cnt = 1, frp = sp->frefq.cqh_first;
230 frp != (FREF *)&sp->frefq; frp = frp->q.cqe_next) {
231 iscur = 0;
232 /*
233 * If the last argument FREF structure, and we're editing
234 * it, set the current bit. Otherwise, we'll display it,
235 * then the file we're editing, and the latter will have
236 * the current bit set.
237 */
238 if (frp == sp->a_frp) {
239 if (frp == sp->frp && frp->cname == NULL)
240 iscur = 1;
241 } else if (F_ISSET(frp, FR_IGNORE))
242 continue;
243 name = frp->name == NULL ? frp->tname : frp->name;
244 /*
245 * Mistake. The user edited a temporary file (vi /tmp), then
246 * switched to another file (:e file). The argument FREF is
247 * pointing to the temporary file, but it doesn't have a name.
248 * Gracefully recover through the creative use of goto's.
249 */
250 if (name == NULL)
251 goto testcur;
252extra: nlen = strlen(name);
253 col += len = nlen + sep + (iscur ? 2 : 0);
254 if (col >= sp->cols - 1) {
255 col = len;
256 sep = 0;
257 (void)ex_printf(EXCOOKIE, "\n");
258 } else if (cnt != 1) {
259 sep = 1;
260 (void)ex_printf(EXCOOKIE, " ");
261 }
262 ++cnt;
263
264 if (iscur)
265 (void)ex_printf(EXCOOKIE, "[%s]", name);
266 else {
267 (void)ex_printf(EXCOOKIE, "%s", name);
268testcur: if (frp == sp->a_frp) {
269 if (frp != sp->frp)
270 name = FILENAME(sp->frp);
271 else
272 name = frp->cname;
273 iscur = 1;
274 goto extra;
275 }
276 }
277 }
278 /* This should never happen; left in because it's been known to. */
279 if (cnt == 1)
280 (void)ex_printf(EXCOOKIE, "No files.\n");
281 else
282 (void)ex_printf(EXCOOKIE, "\n");
283 return (0);
284}