1 /* $NetBSD: ex_args.c,v 1.2 2013/11/22 15:52:05 christos Exp $ */
3 * Copyright (c) 1991, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1991, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
14 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 ";
17 #include <sys/types.h>
18 #include <sys/queue.h>
21 #include <bitstring.h>
28 #include "../common/common.h"
31 static int ex_N_next
__P((SCR
*, EXCMD
*));
34 * ex_next -- :next [+cmd] [files]
35 * Edit the next file, optionally setting the list of files.
38 * The :next command behaved differently from the :rewind command in
39 * historic vi. See nvi/docs/autowrite for details, but the basic
40 * idea was that it ignored the force flag if the autowrite flag was
41 * set. This implementation handles them all identically.
43 * PUBLIC: int ex_next __P((SCR *, EXCMD *));
46 ex_next(SCR
*sp
, EXCMD
*cmdp
)
57 /* Check for file to move to. */
58 if (cmdp
->argc
== 0 && (sp
->cargv
== NULL
|| sp
->cargv
[1] == NULL
)) {
59 msgq(sp
, M_ERR
, "111|No more files to edit");
63 if (F_ISSET(cmdp
, E_NEWSCREEN
)) {
64 /* By default, edit the next file in the old argument list. */
65 if (cmdp
->argc
== 0) {
66 CHAR2INT(sp
, sp
->cargv
[1], strlen(sp
->cargv
[1]) + 1,
68 if (argv_exp0(sp
, cmdp
, wp
, wlen
- 1))
70 return (ex_edit(sp
, cmdp
));
72 return (ex_N_next(sp
, cmdp
));
75 /* Check modification. */
77 FL_ISSET(cmdp
->iflags
, E_C_FORCE
), FS_ALL
| FS_POSSIBLE
))
80 /* Any arguments are a replacement file list. */
82 /* Free the current list. */
83 if (!F_ISSET(sp
, SC_ARGNOFREE
) && sp
->argv
!= NULL
) {
84 for (ap
= sp
->argv
; *ap
!= NULL
; ++ap
)
88 F_CLR(sp
, SC_ARGNOFREE
| SC_ARGRECOVER
);
91 /* Create a new list. */
93 sp
->argv
, char **, cmdp
->argc
+ 1, sizeof(char *));
95 argv
= cmdp
->argv
; argv
[0]->len
!= 0; ++ap
, ++argv
) {
96 INT2CHAR(sp
, argv
[0]->bp
, argv
[0]->len
, np
, nlen
);
97 if ((*ap
= v_strdup(sp
, np
, nlen
)) == NULL
)
102 /* Switch to the first file. */
103 sp
->cargv
= sp
->argv
;
104 if ((frp
= file_add(sp
, *sp
->cargv
)) == NULL
)
108 /* Display a file count with the welcome message. */
109 F_SET(sp
, SC_STATUS_CNT
);
111 if ((frp
= file_add(sp
, sp
->cargv
[1])) == NULL
)
113 if (F_ISSET(sp
, SC_ARGRECOVER
))
114 F_SET(frp
, FR_RECOVER
);
118 if (file_init(sp
, frp
, NULL
, FS_SETALT
|
119 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0)))
124 F_SET(sp
, SC_FSWITCH
);
130 * New screen version of ex_next.
133 ex_N_next(SCR
*sp
, EXCMD
*cmdp
)
140 /* Get a new screen. */
141 if (screen_init(sp
->gp
, sp
, &new))
143 if (vs_split(sp
, new, 0)) {
144 (void)screen_end(new);
148 /* Get a backing file. */
149 INT2CHAR(sp
, cmdp
->argv
[0]->bp
, cmdp
->argv
[0]->len
+ 1, np
, nlen
);
150 if ((frp
= file_add(new, np
)) == NULL
||
151 file_init(new, frp
, NULL
,
152 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0))) {
153 (void)vs_discard(new, NULL
);
154 (void)screen_end(new);
158 /* The arguments are a replacement file list. */
159 new->cargv
= new->argv
= ex_buildargv(sp
, cmdp
, NULL
);
161 /* Display a file count with the welcome message. */
162 F_SET(new, SC_STATUS_CNT
);
164 /* Set up the switch. */
166 F_SET(sp
, SC_SSWITCH
);
173 * Edit the previous file.
175 * PUBLIC: int ex_prev __P((SCR *, EXCMD *));
178 ex_prev(SCR
*sp
, EXCMD
*cmdp
)
184 if (sp
->cargv
== sp
->argv
) {
185 msgq(sp
, M_ERR
, "112|No previous files to edit");
189 if (F_ISSET(cmdp
, E_NEWSCREEN
)) {
190 CHAR2INT(sp
, sp
->cargv
[-1], strlen(sp
->cargv
[-1]) + 1,
192 if (argv_exp0(sp
, cmdp
, wp
, wlen
- 1))
194 return (ex_edit(sp
, cmdp
));
198 FL_ISSET(cmdp
->iflags
, E_C_FORCE
), FS_ALL
| FS_POSSIBLE
))
201 if ((frp
= file_add(sp
, sp
->cargv
[-1])) == NULL
)
204 if (file_init(sp
, frp
, NULL
, FS_SETALT
|
205 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0)))
209 F_SET(sp
, SC_FSWITCH
);
215 * Re-edit the list of files.
218 * Historic practice was that all files would start editing at the beginning
219 * of the file. We don't get this right because we may have multiple screens
220 * and we can't clear the FR_CURSORSET bit for a single screen. I don't see
221 * anyone noticing, but if they do, we'll have to put information into the SCR
222 * structure so we can keep track of it.
224 * PUBLIC: int ex_rew __P((SCR *, EXCMD *));
227 ex_rew(SCR
*sp
, EXCMD
*cmdp
)
233 * Historic practice -- you can rewind to the current file.
235 if (sp
->argv
== NULL
) {
236 msgq(sp
, M_ERR
, "113|No previous files to rewind");
241 FL_ISSET(cmdp
->iflags
, E_C_FORCE
), FS_ALL
| FS_POSSIBLE
))
244 /* Switch to the first one. */
245 sp
->cargv
= sp
->argv
;
246 if ((frp
= file_add(sp
, *sp
->cargv
)) == NULL
)
248 if (file_init(sp
, frp
, NULL
, FS_SETALT
|
249 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0)))
252 /* Switch and display a file count with the welcome message. */
253 F_SET(sp
, SC_FSWITCH
| SC_STATUS_CNT
);
260 * Display the list of files.
262 * PUBLIC: int ex_args __P((SCR *, EXCMD *));
265 ex_args(SCR
*sp
, EXCMD
*cmdp
)
271 if (sp
->argv
== NULL
) {
272 (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
)