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.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
23 #include "../common/common.h"
26 static int ex_N_next(SCR
*, EXCMD
*);
29 * ex_next -- :next [+cmd] [files]
30 * Edit the next file, optionally setting the list of files.
33 * The :next command behaved differently from the :rewind command in
34 * historic vi. See nvi/docs/autowrite for details, but the basic
35 * idea was that it ignored the force flag if the autowrite flag was
36 * set. This implementation handles them all identically.
38 * PUBLIC: int ex_next(SCR *, EXCMD *);
41 ex_next(SCR
*sp
, EXCMD
*cmdp
)
52 /* Check for file to move to. */
53 if (cmdp
->argc
== 0 && (sp
->cargv
== NULL
|| sp
->cargv
[1] == NULL
)) {
54 msgq(sp
, M_ERR
, "111|No more files to edit");
58 if (F_ISSET(cmdp
, E_NEWSCREEN
)) {
59 /* By default, edit the next file in the old argument list. */
60 if (cmdp
->argc
== 0) {
61 CHAR2INT(sp
, sp
->cargv
[1], strlen(sp
->cargv
[1]) + 1,
63 if (argv_exp0(sp
, cmdp
, wp
, wlen
- 1))
65 return (ex_edit(sp
, cmdp
));
67 return (ex_N_next(sp
, cmdp
));
70 /* Check modification. */
72 FL_ISSET(cmdp
->iflags
, E_C_FORCE
), FS_ALL
| FS_POSSIBLE
))
75 /* Any arguments are a replacement file list. */
77 /* Free the current list. */
78 if (!F_ISSET(sp
, SC_ARGNOFREE
) && sp
->argv
!= NULL
) {
79 for (ap
= sp
->argv
; *ap
!= NULL
; ++ap
)
83 F_CLR(sp
, SC_ARGNOFREE
| SC_ARGRECOVER
);
86 /* Create a new list. */
87 CALLOC_RET(sp
, sp
->argv
, cmdp
->argc
+ 1, sizeof(char *));
89 argv
= cmdp
->argv
; argv
[0]->len
!= 0; ++ap
, ++argv
) {
90 INT2CHAR(sp
, argv
[0]->bp
, argv
[0]->len
, np
, nlen
);
91 if ((*ap
= v_strdup(sp
, np
, nlen
)) == NULL
)
96 /* Switch to the first file. */
98 if ((frp
= file_add(sp
, *sp
->cargv
)) == NULL
)
102 /* Display a file count with the welcome message. */
103 F_SET(sp
, SC_STATUS_CNT
);
105 if ((frp
= file_add(sp
, sp
->cargv
[1])) == NULL
)
107 if (F_ISSET(sp
, SC_ARGRECOVER
))
108 F_SET(frp
, FR_RECOVER
);
112 if (file_init(sp
, frp
, NULL
, FS_SETALT
|
113 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0)))
118 F_SET(sp
, SC_FSWITCH
);
124 * New screen version of ex_next.
127 ex_N_next(SCR
*sp
, EXCMD
*cmdp
)
134 /* Get a new screen. */
135 if (screen_init(sp
->gp
, sp
, &new))
137 if (vs_split(sp
, new, 0)) {
138 (void)screen_end(new);
142 /* Get a backing file. */
143 INT2CHAR(sp
, cmdp
->argv
[0]->bp
, cmdp
->argv
[0]->len
+ 1, np
, nlen
);
144 if ((frp
= file_add(new, np
)) == 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(SCR *, EXCMD *);
172 ex_prev(SCR
*sp
, EXCMD
*cmdp
)
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 CHAR2INT(sp
, sp
->cargv
[-1], strlen(sp
->cargv
[-1]) + 1,
186 if (argv_exp0(sp
, cmdp
, wp
, wlen
- 1))
188 return (ex_edit(sp
, cmdp
));
192 FL_ISSET(cmdp
->iflags
, E_C_FORCE
), FS_ALL
| FS_POSSIBLE
))
195 if ((frp
= file_add(sp
, sp
->cargv
[-1])) == NULL
)
198 if (file_init(sp
, frp
, NULL
, FS_SETALT
|
199 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0)))
203 F_SET(sp
, SC_FSWITCH
);
209 * Re-edit the list of files.
212 * Historic practice was that all files would start editing at the beginning
213 * of the file. We don't get this right because we may have multiple screens
214 * and we can't clear the FR_CURSORSET bit for a single screen. I don't see
215 * anyone noticing, but if they do, we'll have to put information into the SCR
216 * structure so we can keep track of it.
218 * PUBLIC: int ex_rew(SCR *, EXCMD *);
221 ex_rew(SCR
*sp
, EXCMD
*cmdp
)
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(SCR *, EXCMD *);
259 ex_args(SCR
*sp
, EXCMD
*cmdp
)
262 int cnt
, col
, len
, sep
;
265 if (sp
->argv
== NULL
) {
266 (void)msgq(sp
, M_ERR
, "114|No file list to display");
272 for (cnt
= 1, ap
= sp
->argv
; *ap
!= NULL
; ++ap
) {
273 col
+= len
= strlen(*ap
) + sep
+ (ap
== sp
->cargv
? 2 : 0);
274 if (col
>= sp
->cols
- 1) {
277 (void)ex_puts(sp
, "\n");
278 } else if (cnt
!= 1) {
280 (void)ex_puts(sp
, " ");
284 (void)ex_printf(sp
, "%s%s%s", ap
== sp
->cargv
? "[" : "",
285 *ap
, ap
== sp
->cargv
? "]" : "");
289 (void)ex_puts(sp
, "\n");
295 * Build a new file argument list.
297 * PUBLIC: char **ex_buildargv(SCR *, EXCMD *, char *);
300 ex_buildargv(SCR
*sp
, EXCMD
*cmdp
, char *name
)
308 argc
= cmdp
== NULL
? 1 : cmdp
->argc
;
309 CALLOC(sp
, s_argv
, argc
+ 1, sizeof(char *));
310 if ((ap
= s_argv
) == NULL
)
314 if ((*ap
= v_strdup(sp
, name
, strlen(name
))) == NULL
)
318 for (argv
= cmdp
->argv
; argv
[0]->len
!= 0; ++ap
, ++argv
) {
319 INT2CHAR(sp
, argv
[0]->bp
, argv
[0]->len
, np
, nlen
);
320 if ((*ap
= v_strdup(sp
, np
, nlen
)) == NULL
)