2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1991, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 static const char sccsid
[] = "$Id: ex_args.c,v 10.18 2001/06/25 15:19:14 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:14 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
27 #include "../common/common.h"
30 static int ex_N_next
__P((SCR
*, EXCMD
*));
33 * ex_next -- :next [+cmd] [files]
34 * Edit the next file, optionally setting the list of files.
37 * The :next command behaved differently from the :rewind command in
38 * historic vi. See nvi/docs/autowrite for details, but the basic
39 * idea was that it ignored the force flag if the autowrite flag was
40 * set. This implementation handles them all identically.
42 * PUBLIC: int ex_next __P((SCR *, EXCMD *));
45 ex_next(SCR
*sp
, EXCMD
*cmdp
)
56 /* Check for file to move to. */
57 if (cmdp
->argc
== 0 && (sp
->cargv
== NULL
|| sp
->cargv
[1] == NULL
)) {
58 msgq(sp
, M_ERR
, "111|No more files to edit");
62 if (F_ISSET(cmdp
, E_NEWSCREEN
)) {
63 /* By default, edit the next file in the old argument list. */
64 if (cmdp
->argc
== 0) {
65 CHAR2INT(sp
, sp
->cargv
[1], strlen(sp
->cargv
[1]) + 1,
67 if (argv_exp0(sp
, cmdp
, wp
, wlen
- 1))
69 return (ex_edit(sp
, cmdp
));
71 return (ex_N_next(sp
, cmdp
));
74 /* Check modification. */
76 FL_ISSET(cmdp
->iflags
, E_C_FORCE
), FS_ALL
| FS_POSSIBLE
))
79 /* Any arguments are a replacement file list. */
81 /* Free the current list. */
82 if (!F_ISSET(sp
, SC_ARGNOFREE
) && sp
->argv
!= NULL
) {
83 for (ap
= sp
->argv
; *ap
!= NULL
; ++ap
)
87 F_CLR(sp
, SC_ARGNOFREE
| SC_ARGRECOVER
);
90 /* Create a new list. */
92 sp
->argv
, char **, cmdp
->argc
+ 1, sizeof(char *));
94 argv
= cmdp
->argv
; argv
[0]->len
!= 0; ++ap
, ++argv
) {
95 INT2CHAR(sp
, argv
[0]->bp
, argv
[0]->len
, np
, nlen
);
96 if ((*ap
= v_strdup(sp
, np
, nlen
)) == NULL
)
101 /* Switch to the first file. */
102 sp
->cargv
= sp
->argv
;
103 if ((frp
= file_add(sp
, *sp
->cargv
)) == NULL
)
107 /* Display a file count with the welcome message. */
108 F_SET(sp
, SC_STATUS_CNT
);
110 if ((frp
= file_add(sp
, sp
->cargv
[1])) == NULL
)
112 if (F_ISSET(sp
, SC_ARGRECOVER
))
113 F_SET(frp
, FR_RECOVER
);
117 if (file_init(sp
, frp
, NULL
, FS_SETALT
|
118 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0)))
123 F_SET(sp
, SC_FSWITCH
);
129 * New screen version of ex_next.
132 ex_N_next(SCR
*sp
, EXCMD
*cmdp
)
139 /* Get a new screen. */
140 if (screen_init(sp
->gp
, sp
, &new))
142 if (vs_split(sp
, new, 0)) {
143 (void)screen_end(new);
147 /* Get a backing file. */
148 INT2CHAR(sp
, cmdp
->argv
[0]->bp
, cmdp
->argv
[0]->len
+ 1, np
, nlen
);
149 if ((frp
= file_add(new, np
)) == NULL
||
150 file_init(new, frp
, NULL
,
151 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0))) {
152 (void)vs_discard(new, NULL
);
153 (void)screen_end(new);
157 /* The arguments are a replacement file list. */
158 new->cargv
= new->argv
= ex_buildargv(sp
, cmdp
, NULL
);
160 /* Display a file count with the welcome message. */
161 F_SET(new, SC_STATUS_CNT
);
163 /* Set up the switch. */
165 F_SET(sp
, SC_SSWITCH
);
172 * Edit the previous file.
174 * PUBLIC: int ex_prev __P((SCR *, EXCMD *));
177 ex_prev(SCR
*sp
, EXCMD
*cmdp
)
183 if (sp
->cargv
== sp
->argv
) {
184 msgq(sp
, M_ERR
, "112|No previous files to edit");
188 if (F_ISSET(cmdp
, E_NEWSCREEN
)) {
189 CHAR2INT(sp
, sp
->cargv
[-1], strlen(sp
->cargv
[-1]) + 1,
191 if (argv_exp0(sp
, cmdp
, wp
, wlen
- 1))
193 return (ex_edit(sp
, cmdp
));
197 FL_ISSET(cmdp
->iflags
, E_C_FORCE
), FS_ALL
| FS_POSSIBLE
))
200 if ((frp
= file_add(sp
, sp
->cargv
[-1])) == NULL
)
203 if (file_init(sp
, frp
, NULL
, FS_SETALT
|
204 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0)))
208 F_SET(sp
, SC_FSWITCH
);
214 * Re-edit the list of files.
217 * Historic practice was that all files would start editing at the beginning
218 * of the file. We don't get this right because we may have multiple screens
219 * and we can't clear the FR_CURSORSET bit for a single screen. I don't see
220 * anyone noticing, but if they do, we'll have to put information into the SCR
221 * structure so we can keep track of it.
223 * PUBLIC: int ex_rew __P((SCR *, EXCMD *));
226 ex_rew(SCR
*sp
, EXCMD
*cmdp
)
232 * Historic practice -- you can rewind to the current file.
234 if (sp
->argv
== NULL
) {
235 msgq(sp
, M_ERR
, "113|No previous files to rewind");
240 FL_ISSET(cmdp
->iflags
, E_C_FORCE
), FS_ALL
| FS_POSSIBLE
))
243 /* Switch to the first one. */
244 sp
->cargv
= sp
->argv
;
245 if ((frp
= file_add(sp
, *sp
->cargv
)) == NULL
)
247 if (file_init(sp
, frp
, NULL
, FS_SETALT
|
248 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0)))
251 /* Switch and display a file count with the welcome message. */
252 F_SET(sp
, SC_FSWITCH
| SC_STATUS_CNT
);
259 * Display the list of files.
261 * PUBLIC: int ex_args __P((SCR *, EXCMD *));
264 ex_args(SCR
*sp
, EXCMD
*cmdp
)
267 int cnt
, col
, len
, sep
;
270 if (sp
->argv
== NULL
) {
271 (void)msgq(sp
, M_ERR
, "114|No file list to display");
277 for (cnt
= 1, ap
= sp
->argv
; *ap
!= NULL
; ++ap
) {
278 col
+= len
= strlen(*ap
) + sep
+ (ap
== sp
->cargv
? 2 : 0);
279 if (col
>= sp
->cols
- 1) {
282 (void)ex_puts(sp
, "\n");
283 } else if (cnt
!= 1) {
285 (void)ex_puts(sp
, " ");
289 (void)ex_printf(sp
, "%s%s%s", ap
== sp
->cargv
? "[" : "",
290 *ap
, ap
== sp
->cargv
? "]" : "");
294 (void)ex_puts(sp
, "\n");
300 * Build a new file argument list.
302 * PUBLIC: char **ex_buildargv __P((SCR *, EXCMD *, char *));
305 ex_buildargv(SCR
*sp
, EXCMD
*cmdp
, char *name
)
313 argc
= cmdp
== NULL
? 1 : cmdp
->argc
;
314 CALLOC(sp
, s_argv
, char **, argc
+ 1, sizeof(char *));
315 if ((ap
= s_argv
) == NULL
)
319 if ((*ap
= v_strdup(sp
, name
, strlen(name
))) == NULL
)
323 for (argv
= cmdp
->argv
; argv
[0]->len
!= 0; ++ap
, ++argv
) {
324 INT2CHAR(sp
, cmdp
->argv
[0]->bp
, cmdp
->argv
[0]->len
,
326 if ((*ap
= v_strdup(sp
, np
, nlen
)) == NULL
)