1 /* $NetBSD: ex_args.c,v 1.4 2014/01/26 21:43:45 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.
13 #include <sys/cdefs.h>
16 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 ";
19 __RCSID("$NetBSD: ex_args.c,v 1.4 2014/01/26 21:43:45 christos Exp $");
22 #include <sys/types.h>
23 #include <sys/queue.h>
26 #include <bitstring.h>
33 #include "../common/common.h"
36 static int ex_N_next
__P((SCR
*, EXCMD
*));
39 * ex_next -- :next [+cmd] [files]
40 * Edit the next file, optionally setting the list of files.
43 * The :next command behaved differently from the :rewind command in
44 * historic vi. See nvi/docs/autowrite for details, but the basic
45 * idea was that it ignored the force flag if the autowrite flag was
46 * set. This implementation handles them all identically.
48 * PUBLIC: int ex_next __P((SCR *, EXCMD *));
51 ex_next(SCR
*sp
, EXCMD
*cmdp
)
62 /* Check for file to move to. */
63 if (cmdp
->argc
== 0 && (sp
->cargv
== NULL
|| sp
->cargv
[1] == NULL
)) {
64 msgq(sp
, M_ERR
, "111|No more files to edit");
68 if (F_ISSET(cmdp
, E_NEWSCREEN
)) {
69 /* By default, edit the next file in the old argument list. */
70 if (cmdp
->argc
== 0) {
71 CHAR2INT(sp
, sp
->cargv
[1], strlen(sp
->cargv
[1]) + 1,
73 if (argv_exp0(sp
, cmdp
, wp
, wlen
- 1))
75 return (ex_edit(sp
, cmdp
));
77 return (ex_N_next(sp
, cmdp
));
80 /* Check modification. */
82 FL_ISSET(cmdp
->iflags
, E_C_FORCE
), FS_ALL
| FS_POSSIBLE
))
85 /* Any arguments are a replacement file list. */
87 /* Free the current list. */
88 if (!F_ISSET(sp
, SC_ARGNOFREE
) && sp
->argv
!= NULL
) {
89 for (ap
= sp
->argv
; *ap
!= NULL
; ++ap
)
93 F_CLR(sp
, SC_ARGNOFREE
| SC_ARGRECOVER
);
96 /* Create a new list. */
98 sp
->argv
, char **, cmdp
->argc
+ 1, sizeof(char *));
100 argv
= cmdp
->argv
; argv
[0]->len
!= 0; ++ap
, ++argv
) {
101 INT2CHAR(sp
, argv
[0]->bp
, argv
[0]->len
, np
, nlen
);
102 if ((*ap
= v_strdup(sp
, np
, nlen
)) == NULL
)
107 /* Switch to the first file. */
108 sp
->cargv
= sp
->argv
;
109 if ((frp
= file_add(sp
, *sp
->cargv
)) == NULL
)
113 /* Display a file count with the welcome message. */
114 F_SET(sp
, SC_STATUS_CNT
);
116 if ((frp
= file_add(sp
, sp
->cargv
[1])) == NULL
)
118 if (F_ISSET(sp
, SC_ARGRECOVER
))
119 F_SET(frp
, FR_RECOVER
);
123 if (file_init(sp
, frp
, NULL
, FS_SETALT
|
124 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0)))
129 F_SET(sp
, SC_FSWITCH
);
135 * New screen version of ex_next.
138 ex_N_next(SCR
*sp
, EXCMD
*cmdp
)
145 /* Get a new screen. */
146 if (screen_init(sp
->gp
, sp
, &new))
148 if (vs_split(sp
, new, 0)) {
149 (void)screen_fini(new);
153 /* Get a backing file. */
154 INT2CHAR(sp
, cmdp
->argv
[0]->bp
, cmdp
->argv
[0]->len
+ 1, np
, nlen
);
155 if ((frp
= file_add(new, np
)) == NULL
||
156 file_init(new, frp
, NULL
,
157 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0))) {
158 (void)vs_discard(new, NULL
);
159 (void)screen_end(new);
163 /* The arguments are a replacement file list. */
164 new->cargv
= new->argv
= ex_buildargv(sp
, cmdp
, NULL
);
166 /* Display a file count with the welcome message. */
167 F_SET(new, SC_STATUS_CNT
);
169 /* Set up the switch. */
171 F_SET(sp
, SC_SSWITCH
);
178 * Edit the previous file.
180 * PUBLIC: int ex_prev __P((SCR *, EXCMD *));
183 ex_prev(SCR
*sp
, EXCMD
*cmdp
)
189 if (sp
->cargv
== sp
->argv
) {
190 msgq(sp
, M_ERR
, "112|No previous files to edit");
194 if (F_ISSET(cmdp
, E_NEWSCREEN
)) {
195 CHAR2INT(sp
, sp
->cargv
[-1], strlen(sp
->cargv
[-1]) + 1,
197 if (argv_exp0(sp
, cmdp
, wp
, wlen
- 1))
199 return (ex_edit(sp
, cmdp
));
203 FL_ISSET(cmdp
->iflags
, E_C_FORCE
), FS_ALL
| FS_POSSIBLE
))
206 if ((frp
= file_add(sp
, sp
->cargv
[-1])) == NULL
)
209 if (file_init(sp
, frp
, NULL
, FS_SETALT
|
210 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0)))
214 F_SET(sp
, SC_FSWITCH
);
220 * Re-edit the list of files.
223 * Historic practice was that all files would start editing at the beginning
224 * of the file. We don't get this right because we may have multiple screens
225 * and we can't clear the FR_CURSORSET bit for a single screen. I don't see
226 * anyone noticing, but if they do, we'll have to put information into the SCR
227 * structure so we can keep track of it.
229 * PUBLIC: int ex_rew __P((SCR *, EXCMD *));
232 ex_rew(SCR
*sp
, EXCMD
*cmdp
)
238 * Historic practice -- you can rewind to the current file.
240 if (sp
->argv
== NULL
) {
241 msgq(sp
, M_ERR
, "113|No previous files to rewind");
246 FL_ISSET(cmdp
->iflags
, E_C_FORCE
), FS_ALL
| FS_POSSIBLE
))
249 /* Switch to the first one. */
250 sp
->cargv
= sp
->argv
;
251 if ((frp
= file_add(sp
, *sp
->cargv
)) == NULL
)
253 if (file_init(sp
, frp
, NULL
, FS_SETALT
|
254 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0)))
257 /* Switch and display a file count with the welcome message. */
258 F_SET(sp
, SC_FSWITCH
| SC_STATUS_CNT
);
265 * Display the list of files.
267 * PUBLIC: int ex_args __P((SCR *, EXCMD *));
270 ex_args(SCR
*sp
, EXCMD
*cmdp
)
276 if (sp
->argv
== NULL
) {
277 (void)msgq(sp
, M_ERR
, "114|No file list to display");
282 for (cnt
= 1, ap
= sp
->argv
; *ap
!= NULL
; ++ap
) {
283 col
+= len
= strlen(*ap
) + sep
+ (ap
== sp
->cargv
? 2 : 0);
284 if (col
>= sp
->cols
- 1) {
287 (void)ex_puts(sp
, "\n");
288 } else if (cnt
!= 1) {
290 (void)ex_puts(sp
, " ");
294 (void)ex_printf(sp
, "%s%s%s", ap
== sp
->cargv
? "[" : "",
295 *ap
, ap
== sp
->cargv
? "]" : "");
299 (void)ex_puts(sp
, "\n");
305 * Build a new file argument list.
307 * PUBLIC: char **ex_buildargv __P((SCR *, EXCMD *, char *));
310 ex_buildargv(SCR
*sp
, EXCMD
*cmdp
, char *name
)
318 argc
= cmdp
== NULL
? 1 : cmdp
->argc
;
319 CALLOC(sp
, s_argv
, char **, argc
+ 1, sizeof(char *));
320 if ((ap
= s_argv
) == NULL
)
324 if ((*ap
= v_strdup(sp
, name
, strlen(name
))) == NULL
)
328 for (argv
= cmdp
->argv
; argv
[0]->len
!= 0; ++ap
, ++argv
) {
329 INT2CHAR(sp
, cmdp
->argv
[0]->bp
, cmdp
->argv
[0]->len
,
331 if ((*ap
= v_strdup(sp
, np
, nlen
)) == NULL
)