1 /* $NetBSD: ex_args.c,v 1.2 2008/12/05 22:51:42 christos Exp $ */
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1991, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
15 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";
18 #include <sys/types.h>
19 #include <sys/queue.h>
22 #include <bitstring.h>
29 #include "../common/common.h"
32 static int ex_N_next
__P((SCR
*, EXCMD
*));
35 * ex_next -- :next [+cmd] [files]
36 * Edit the next file, optionally setting the list of files.
39 * The :next command behaved differently from the :rewind command in
40 * historic vi. See nvi/docs/autowrite for details, but the basic
41 * idea was that it ignored the force flag if the autowrite flag was
42 * set. This implementation handles them all identically.
44 * PUBLIC: int ex_next __P((SCR *, EXCMD *));
47 ex_next(SCR
*sp
, EXCMD
*cmdp
)
58 /* Check for file to move to. */
59 if (cmdp
->argc
== 0 && (sp
->cargv
== NULL
|| sp
->cargv
[1] == NULL
)) {
60 msgq(sp
, M_ERR
, "111|No more files to edit");
64 if (F_ISSET(cmdp
, E_NEWSCREEN
)) {
65 /* By default, edit the next file in the old argument list. */
66 if (cmdp
->argc
== 0) {
67 CHAR2INT(sp
, sp
->cargv
[1], strlen(sp
->cargv
[1]) + 1,
69 if (argv_exp0(sp
, cmdp
, wp
, wlen
- 1))
71 return (ex_edit(sp
, cmdp
));
73 return (ex_N_next(sp
, cmdp
));
76 /* Check modification. */
78 FL_ISSET(cmdp
->iflags
, E_C_FORCE
), FS_ALL
| FS_POSSIBLE
))
81 /* Any arguments are a replacement file list. */
83 /* Free the current list. */
84 if (!F_ISSET(sp
, SC_ARGNOFREE
) && sp
->argv
!= NULL
) {
85 for (ap
= sp
->argv
; *ap
!= NULL
; ++ap
)
89 F_CLR(sp
, SC_ARGNOFREE
| SC_ARGRECOVER
);
92 /* Create a new list. */
94 sp
->argv
, char **, cmdp
->argc
+ 1, sizeof(char *));
96 argv
= cmdp
->argv
; argv
[0]->len
!= 0; ++ap
, ++argv
) {
97 INT2CHAR(sp
, argv
[0]->bp
, argv
[0]->len
, np
, nlen
);
98 if ((*ap
= v_strdup(sp
, np
, nlen
)) == NULL
)
103 /* Switch to the first file. */
104 sp
->cargv
= sp
->argv
;
105 if ((frp
= file_add(sp
, *sp
->cargv
)) == NULL
)
109 /* Display a file count with the welcome message. */
110 F_SET(sp
, SC_STATUS_CNT
);
112 if ((frp
= file_add(sp
, sp
->cargv
[1])) == NULL
)
114 if (F_ISSET(sp
, SC_ARGRECOVER
))
115 F_SET(frp
, FR_RECOVER
);
119 if (file_init(sp
, frp
, NULL
, FS_SETALT
|
120 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0)))
125 F_SET(sp
, SC_FSWITCH
);
131 * New screen version of ex_next.
134 ex_N_next(SCR
*sp
, EXCMD
*cmdp
)
141 /* Get a new screen. */
142 if (screen_init(sp
->gp
, sp
, &new))
144 if (vs_split(sp
, new, 0)) {
145 (void)screen_end(new);
149 /* Get a backing file. */
150 INT2CHAR(sp
, cmdp
->argv
[0]->bp
, cmdp
->argv
[0]->len
+ 1, np
, nlen
);
151 if ((frp
= file_add(new, np
)) == NULL
||
152 file_init(new, frp
, NULL
,
153 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0))) {
154 (void)vs_discard(new, NULL
);
155 (void)screen_end(new);
159 /* The arguments are a replacement file list. */
160 new->cargv
= new->argv
= ex_buildargv(sp
, cmdp
, NULL
);
162 /* Display a file count with the welcome message. */
163 F_SET(new, SC_STATUS_CNT
);
165 /* Set up the switch. */
167 F_SET(sp
, SC_SSWITCH
);
174 * Edit the previous file.
176 * PUBLIC: int ex_prev __P((SCR *, EXCMD *));
179 ex_prev(SCR
*sp
, EXCMD
*cmdp
)
185 if (sp
->cargv
== sp
->argv
) {
186 msgq(sp
, M_ERR
, "112|No previous files to edit");
190 if (F_ISSET(cmdp
, E_NEWSCREEN
)) {
191 CHAR2INT(sp
, sp
->cargv
[-1], strlen(sp
->cargv
[-1]) + 1,
193 if (argv_exp0(sp
, cmdp
, wp
, wlen
- 1))
195 return (ex_edit(sp
, cmdp
));
199 FL_ISSET(cmdp
->iflags
, E_C_FORCE
), FS_ALL
| FS_POSSIBLE
))
202 if ((frp
= file_add(sp
, sp
->cargv
[-1])) == NULL
)
205 if (file_init(sp
, frp
, NULL
, FS_SETALT
|
206 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0)))
210 F_SET(sp
, SC_FSWITCH
);
216 * Re-edit the list of files.
219 * Historic practice was that all files would start editing at the beginning
220 * of the file. We don't get this right because we may have multiple screens
221 * and we can't clear the FR_CURSORSET bit for a single screen. I don't see
222 * anyone noticing, but if they do, we'll have to put information into the SCR
223 * structure so we can keep track of it.
225 * PUBLIC: int ex_rew __P((SCR *, EXCMD *));
228 ex_rew(SCR
*sp
, EXCMD
*cmdp
)
234 * Historic practice -- you can rewind to the current file.
236 if (sp
->argv
== NULL
) {
237 msgq(sp
, M_ERR
, "113|No previous files to rewind");
242 FL_ISSET(cmdp
->iflags
, E_C_FORCE
), FS_ALL
| FS_POSSIBLE
))
245 /* Switch to the first one. */
246 sp
->cargv
= sp
->argv
;
247 if ((frp
= file_add(sp
, *sp
->cargv
)) == NULL
)
249 if (file_init(sp
, frp
, NULL
, FS_SETALT
|
250 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0)))
253 /* Switch and display a file count with the welcome message. */
254 F_SET(sp
, SC_FSWITCH
| SC_STATUS_CNT
);
261 * Display the list of files.
263 * PUBLIC: int ex_args __P((SCR *, EXCMD *));
266 ex_args(SCR
*sp
, EXCMD
*cmdp
)
273 if (sp
->argv
== NULL
) {
274 (void)msgq(sp
, M_ERR
, "114|No file list to display");
280 for (cnt
= 1, ap
= sp
->argv
; *ap
!= NULL
; ++ap
) {
281 col
+= len
= strlen(*ap
) + sep
+ (ap
== sp
->cargv
? 2 : 0);
282 if (col
>= sp
->cols
- 1) {
285 (void)ex_puts(sp
, "\n");
286 } else if (cnt
!= 1) {
288 (void)ex_puts(sp
, " ");
292 (void)ex_printf(sp
, "%s%s%s", ap
== sp
->cargv
? "[" : "",
293 *ap
, ap
== sp
->cargv
? "]" : "");
297 (void)ex_puts(sp
, "\n");
303 * Build a new file argument list.
305 * PUBLIC: char **ex_buildargv __P((SCR *, EXCMD *, char *));
308 ex_buildargv(SCR
*sp
, EXCMD
*cmdp
, char *name
)
316 argc
= cmdp
== NULL
? 1 : cmdp
->argc
;
317 CALLOC(sp
, s_argv
, char **, argc
+ 1, sizeof(char *));
318 if ((ap
= s_argv
) == NULL
)
322 if ((*ap
= v_strdup(sp
, name
, strlen(name
))) == NULL
)
326 for (argv
= cmdp
->argv
; argv
[0]->len
!= 0; ++ap
, ++argv
) {
327 INT2CHAR(sp
, cmdp
->argv
[0]->bp
, cmdp
->argv
[0]->len
,
329 if ((*ap
= v_strdup(sp
, np
, nlen
)) == NULL
)