1 /* $NetBSD: ex_edit.c,v 1.7 2014/01/26 21:43:45 christos Exp $ */
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 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_edit.c,v 10.14 2001/08/28 13:29:15 skimo Exp (Berkeley) Date: 2001/08/28 13:29:15 ";
19 __RCSID("$NetBSD: ex_edit.c,v 1.7 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_edit
__P((SCR
*, EXCMD
*, FREF
*, int));
39 * ex_edit -- :e[dit][!] [+cmd] [file]
40 * :ex[!] [+cmd] [file]
41 * :vi[sual][!] [+cmd] [file]
43 * Edit a file; if none specified, re-edit the current file. The third
44 * form of the command can only be executed while in vi mode. See the
45 * hack in ex.c:ex_cmd().
48 * Historic vi didn't permit the '+' command form without specifying
49 * a file name as well. This seems unreasonable, so we support it
52 * PUBLIC: int ex_edit __P((SCR *, EXCMD *));
55 ex_edit(SCR
*sp
, EXCMD
*cmdp
)
65 * If the name has been changed, we edit that file, not the
66 * original name. If the user was editing a temporary file
67 * (or wasn't editing any file), create another one. The
68 * reason for not reusing temporary files is that there is
69 * special exit processing of them, and reuse is tricky.
72 if (sp
->ep
== NULL
|| F_ISSET(frp
, FR_TMPFILE
)) {
73 if ((frp
= file_add(sp
, NULL
)) == NULL
)
81 INT2CHAR(sp
, cmdp
->argv
[0]->bp
, cmdp
->argv
[0]->len
+ 1,
83 if ((frp
= file_add(sp
, np
)) == NULL
)
93 if (F_ISSET(cmdp
, E_NEWSCREEN
) || cmdp
->cmd
== &cmds
[C_VSPLIT
])
94 return (ex_N_edit(sp
, cmdp
, frp
, attach
));
97 * Check for modifications.
100 * Contrary to POSIX 1003.2-1992, autowrite did not affect :edit.
102 if (file_m2(sp
, FL_ISSET(cmdp
->iflags
, E_C_FORCE
)))
106 if (file_init(sp
, frp
, NULL
, (setalt
? FS_SETALT
: 0) |
107 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0)))
110 F_SET(sp
, SC_FSWITCH
);
116 * New screen version of ex_edit.
119 ex_N_edit(SCR
*sp
, EXCMD
*cmdp
, FREF
*frp
, int attach
)
123 /* Get a new screen. */
124 if (screen_init(sp
->gp
, sp
, &new))
126 if ((cmdp
->cmd
== &cmds
[C_VSPLIT
] && vs_vsplit(sp
, new)) ||
127 (cmdp
->cmd
!= &cmds
[C_VSPLIT
] && vs_split(sp
, new, 0))) {
128 (void)screen_fini(new);
132 /* Get a backing file. */
134 /* Copy file state, keep the screen and cursor the same. */
137 TAILQ_INSERT_HEAD(&new->ep
->scrq
, new, eq
);
140 new->frp
->flags
= sp
->frp
->flags
;
144 } else if (file_init(new, frp
, NULL
,
145 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0))) {
146 (void)vs_discard(new, NULL
);
147 (void)screen_end(new);
151 /* Create the argument list. */
152 new->cargv
= new->argv
= ex_buildargv(sp
, NULL
, frp
->name
);
154 /* Set up the switch. */
156 F_SET(sp
, SC_SSWITCH
);