1 /* $NetBSD: ex_edit.c,v 1.5 2013/12/01 02:34:54 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.
14 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 ";
17 #include <sys/types.h>
18 #include <sys/queue.h>
21 #include <bitstring.h>
28 #include "../common/common.h"
31 static int ex_N_edit
__P((SCR
*, EXCMD
*, FREF
*, int));
34 * ex_edit -- :e[dit][!] [+cmd] [file]
35 * :ex[!] [+cmd] [file]
36 * :vi[sual][!] [+cmd] [file]
38 * Edit a file; if none specified, re-edit the current file. The third
39 * form of the command can only be executed while in vi mode. See the
40 * hack in ex.c:ex_cmd().
43 * Historic vi didn't permit the '+' command form without specifying
44 * a file name as well. This seems unreasonable, so we support it
47 * PUBLIC: int ex_edit __P((SCR *, EXCMD *));
50 ex_edit(SCR
*sp
, EXCMD
*cmdp
)
60 * If the name has been changed, we edit that file, not the
61 * original name. If the user was editing a temporary file
62 * (or wasn't editing any file), create another one. The
63 * reason for not reusing temporary files is that there is
64 * special exit processing of them, and reuse is tricky.
67 if (sp
->ep
== NULL
|| F_ISSET(frp
, FR_TMPFILE
)) {
68 if ((frp
= file_add(sp
, NULL
)) == NULL
)
76 INT2CHAR(sp
, cmdp
->argv
[0]->bp
, cmdp
->argv
[0]->len
+ 1,
78 if ((frp
= file_add(sp
, np
)) == NULL
)
88 if (F_ISSET(cmdp
, E_NEWSCREEN
) || cmdp
->cmd
== &cmds
[C_VSPLIT
])
89 return (ex_N_edit(sp
, cmdp
, frp
, attach
));
92 * Check for modifications.
95 * Contrary to POSIX 1003.2-1992, autowrite did not affect :edit.
97 if (file_m2(sp
, FL_ISSET(cmdp
->iflags
, E_C_FORCE
)))
101 if (file_init(sp
, frp
, NULL
, (setalt
? FS_SETALT
: 0) |
102 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0)))
105 F_SET(sp
, SC_FSWITCH
);
111 * New screen version of ex_edit.
114 ex_N_edit(SCR
*sp
, EXCMD
*cmdp
, FREF
*frp
, int attach
)
118 /* Get a new screen. */
119 if (screen_init(sp
->gp
, sp
, &new))
121 if ((cmdp
->cmd
== &cmds
[C_VSPLIT
] && vs_vsplit(sp
, new)) ||
122 (cmdp
->cmd
!= &cmds
[C_VSPLIT
] && vs_split(sp
, new, 0))) {
123 (void)screen_end(new);
127 /* Get a backing file. */
129 /* Copy file state, keep the screen and cursor the same. */
132 TAILQ_INSERT_HEAD(&new->ep
->scrq
, new, eq
);
135 new->frp
->flags
= sp
->frp
->flags
;
139 } else if (file_init(new, frp
, NULL
,
140 (FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? FS_FORCE
: 0))) {
141 (void)vs_discard(new, NULL
);
142 (void)screen_end(new);
146 /* Create the argument list. */
147 new->cargv
= new->argv
= ex_buildargv(sp
, NULL
, frp
->name
);
149 /* Set up the switch. */
151 F_SET(sp
, SC_SSWITCH
);