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.16 1996/07/13 14:31:08 bostic Exp $ (Berkeley) $Date: 1996/07/13 14:31:08 $";
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 *));
54 /* Check for file to move to. */
55 if (cmdp
->argc
== 0 && (sp
->cargv
== NULL
|| sp
->cargv
[1] == NULL
)) {
56 msgq(sp
, M_ERR
, "111|No more files to edit");
60 if (F_ISSET(cmdp
, E_NEWSCREEN
)) {
61 /* By default, edit the next file in the old argument list. */
62 if (cmdp
->argc
== 0) {
64 cmdp
, sp
->cargv
[1], strlen(sp
->cargv
[1])))
66 return (ex_edit(sp
, cmdp
));
68 return (ex_N_next(sp
, cmdp
));
71 /* Check modification. */
73 FL_ISSET(cmdp
->iflags
, E_C_FORCE
), FS_ALL
| FS_POSSIBLE
))
76 /* Any arguments are a replacement file list. */
78 /* Free the current list. */
79 if (!F_ISSET(sp
, SC_ARGNOFREE
) && sp
->argv
!= NULL
) {
80 for (ap
= sp
->argv
; *ap
!= NULL
; ++ap
)
84 F_CLR(sp
, SC_ARGNOFREE
| SC_ARGRECOVER
);
87 /* Create a new list. */
89 sp
->argv
, char **, cmdp
->argc
+ 1, sizeof(char *));
91 argv
= cmdp
->argv
; argv
[0]->len
!= 0; ++ap
, ++argv
)
93 v_strdup(sp
, argv
[0]->bp
, argv
[0]->len
)) == NULL
)
97 /* Switch to the first file. */
99 if ((frp
= file_add(sp
, *sp
->cargv
)) == NULL
)
103 /* Display a file count with the welcome message. */
104 F_SET(sp
, SC_STATUS_CNT
);
106 if ((frp
= file_add(sp
, sp
->cargv
[1])) == NULL
)
108 if (F_ISSET(sp
, SC_ARGRECOVER
))
109 F_SET(frp
, FR_RECOVER
);
113 if (file_init(sp
, frp
, NULL
, FS_SETALT
|
114 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0)))
119 F_SET(sp
, SC_FSWITCH
);
125 * New screen version of ex_next.
135 /* Get a new screen. */
136 if (screen_init(sp
->gp
, sp
, &new))
138 if (vs_split(sp
, new, 0)) {
139 (void)screen_end(new);
143 /* Get a backing file. */
144 if ((frp
= file_add(new, cmdp
->argv
[0]->bp
)) == NULL
||
145 file_init(new, frp
, NULL
,
146 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0))) {
147 (void)vs_discard(new, NULL
);
148 (void)screen_end(new);
152 /* The arguments are a replacement file list. */
153 new->cargv
= new->argv
= ex_buildargv(sp
, cmdp
, NULL
);
155 /* Display a file count with the welcome message. */
156 F_SET(new, SC_STATUS_CNT
);
158 /* Set up the switch. */
160 F_SET(sp
, SC_SSWITCH
);
167 * Edit the previous file.
169 * PUBLIC: int ex_prev __P((SCR *, EXCMD *));
178 if (sp
->cargv
== sp
->argv
) {
179 msgq(sp
, M_ERR
, "112|No previous files to edit");
183 if (F_ISSET(cmdp
, E_NEWSCREEN
)) {
184 if (argv_exp0(sp
, cmdp
, sp
->cargv
[-1], strlen(sp
->cargv
[-1])))
186 return (ex_edit(sp
, cmdp
));
190 FL_ISSET(cmdp
->iflags
, E_C_FORCE
), FS_ALL
| FS_POSSIBLE
))
193 if ((frp
= file_add(sp
, sp
->cargv
[-1])) == NULL
)
196 if (file_init(sp
, frp
, NULL
, FS_SETALT
|
197 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0)))
201 F_SET(sp
, SC_FSWITCH
);
207 * Re-edit the list of files.
210 * Historic practice was that all files would start editing at the beginning
211 * of the file. We don't get this right because we may have multiple screens
212 * and we can't clear the FR_CURSORSET bit for a single screen. I don't see
213 * anyone noticing, but if they do, we'll have to put information into the SCR
214 * structure so we can keep track of it.
216 * PUBLIC: int ex_rew __P((SCR *, EXCMD *));
227 * Historic practice -- you can rewind to the current file.
229 if (sp
->argv
== NULL
) {
230 msgq(sp
, M_ERR
, "113|No previous files to rewind");
235 FL_ISSET(cmdp
->iflags
, E_C_FORCE
), FS_ALL
| FS_POSSIBLE
))
238 /* Switch to the first one. */
239 sp
->cargv
= sp
->argv
;
240 if ((frp
= file_add(sp
, *sp
->cargv
)) == NULL
)
242 if (file_init(sp
, frp
, NULL
, FS_SETALT
|
243 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0)))
246 /* Switch and display a file count with the welcome message. */
247 F_SET(sp
, SC_FSWITCH
| SC_STATUS_CNT
);
254 * Display the list of files.
256 * PUBLIC: int ex_args __P((SCR *, EXCMD *));
264 int cnt
, col
, len
, sep
;
267 if (sp
->argv
== NULL
) {
268 (void)msgq(sp
, M_ERR
, "114|No file list to display");
274 for (cnt
= 1, ap
= sp
->argv
; *ap
!= NULL
; ++ap
) {
275 col
+= len
= strlen(*ap
) + sep
+ (ap
== sp
->cargv
? 2 : 0);
276 if (col
>= sp
->cols
- 1) {
279 (void)ex_puts(sp
, "\n");
280 } else if (cnt
!= 1) {
282 (void)ex_puts(sp
, " ");
286 (void)ex_printf(sp
, "%s%s%s", ap
== sp
->cargv
? "[" : "",
287 *ap
, ap
== sp
->cargv
? "]" : "");
291 (void)ex_puts(sp
, "\n");
297 * Build a new file argument list.
299 * PUBLIC: char **ex_buildargv __P((SCR *, EXCMD *, char *));
302 ex_buildargv(sp
, cmdp
, name
)
311 argc
= cmdp
== NULL
? 1 : cmdp
->argc
;
312 CALLOC(sp
, s_argv
, char **, argc
+ 1, sizeof(char *));
313 if ((ap
= s_argv
) == NULL
)
317 if ((*ap
= v_strdup(sp
, name
, strlen(name
))) == NULL
)
321 for (argv
= cmdp
->argv
; argv
[0]->len
!= 0; ++ap
, ++argv
)
323 v_strdup(sp
, argv
[0]->bp
, argv
[0]->len
)) == NULL
)