1 /* $NetBSD: ex_edit.c,v 1.1.1.2 2008/05/18 14:31:14 aymeric Exp $ */
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 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_edit.c,v 10.14 2001/08/28 13:29:15 skimo Exp (Berkeley) Date: 2001/08/28 13:29:15";
18 #include <sys/types.h>
19 #include <sys/queue.h>
22 #include <bitstring.h>
29 #include "../common/common.h"
32 static int ex_N_edit
__P((SCR
*, EXCMD
*, FREF
*, int));
35 * ex_edit -- :e[dit][!] [+cmd] [file]
36 * :ex[!] [+cmd] [file]
37 * :vi[sual][!] [+cmd] [file]
39 * Edit a file; if none specified, re-edit the current file. The third
40 * form of the command can only be executed while in vi mode. See the
41 * hack in ex.c:ex_cmd().
44 * Historic vi didn't permit the '+' command form without specifying
45 * a file name as well. This seems unreasonable, so we support it
48 * PUBLIC: int ex_edit __P((SCR *, EXCMD *));
51 ex_edit(SCR
*sp
, EXCMD
*cmdp
)
61 * If the name has been changed, we edit that file, not the
62 * original name. If the user was editing a temporary file
63 * (or wasn't editing any file), create another one. The
64 * reason for not reusing temporary files is that there is
65 * special exit processing of them, and reuse is tricky.
68 if (sp
->ep
== NULL
|| F_ISSET(frp
, FR_TMPFILE
)) {
69 if ((frp
= file_add(sp
, NULL
)) == NULL
)
77 INT2CHAR(sp
, cmdp
->argv
[0]->bp
, cmdp
->argv
[0]->len
+ 1,
79 if ((frp
= file_add(sp
, np
)) == NULL
)
89 if (F_ISSET(cmdp
, E_NEWSCREEN
) || cmdp
->cmd
== &cmds
[C_VSPLIT
])
90 return (ex_N_edit(sp
, cmdp
, frp
, attach
));
93 * Check for modifications.
96 * Contrary to POSIX 1003.2-1992, autowrite did not affect :edit.
98 if (file_m2(sp
, FL_ISSET(cmdp
->iflags
, E_C_FORCE
)))
102 if (file_init(sp
, frp
, NULL
, (setalt
? FS_SETALT
: 0) |
103 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0)))
106 F_SET(sp
, SC_FSWITCH
);
112 * New screen version of ex_edit.
115 ex_N_edit(SCR
*sp
, EXCMD
*cmdp
, FREF
*frp
, int attach
)
119 /* Get a new screen. */
120 if (screen_init(sp
->gp
, sp
, &new))
122 if ((cmdp
->cmd
== &cmds
[C_VSPLIT
] && vs_vsplit(sp
, new)) ||
123 (cmdp
->cmd
!= &cmds
[C_VSPLIT
] && vs_split(sp
, new, 0))) {
124 (void)screen_end(new);
128 /* Get a backing file. */
130 /* Copy file state, keep the screen and cursor the same. */
133 CIRCLEQ_INSERT_HEAD(&new->ep
->scrq
, new, eq
);
136 new->frp
->flags
= sp
->frp
->flags
;
140 } else if (file_init(new, frp
, NULL
,
141 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0))) {
142 (void)vs_discard(new, NULL
);
143 (void)screen_end(new);
147 /* Create the argument list. */
148 new->cargv
= new->argv
= ex_buildargv(sp
, NULL
, frp
->name
);
150 /* Set up the switch. */
152 F_SET(sp
, SC_SSWITCH
);