2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
23 #include "../common/common.h"
26 static int ex_N_edit(SCR *, EXCMD *, FREF *, int);
29 * ex_edit -- :e[dit][!] [+cmd] [file]
30 * :ex[!] [+cmd] [file]
31 * :vi[sual][!] [+cmd] [file]
33 * Edit a file; if none specified, re-edit the current file. The third
34 * form of the command can only be executed while in vi mode. See the
35 * hack in ex.c:ex_cmd().
38 * Historic vi didn't permit the '+' command form without specifying
39 * a file name as well. This seems unreasonable, so we support it
42 * PUBLIC: int ex_edit(SCR *, EXCMD *);
45 ex_edit(SCR *sp, EXCMD *cmdp)
55 * If the name has been changed, we edit that file, not the
56 * original name. If the user was editing a temporary file
57 * (or wasn't editing any file), create another one. The
58 * reason for not reusing temporary files is that there is
59 * special exit processing of them, and reuse is tricky.
62 if (sp->ep == NULL || F_ISSET(frp, FR_TMPFILE)) {
63 if ((frp = file_add(sp, NULL)) == NULL)
71 INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1,
73 if ((frp = file_add(sp, np)) == NULL)
83 if (F_ISSET(cmdp, E_NEWSCREEN) || cmdp->cmd == &cmds[C_VSPLIT])
84 return (ex_N_edit(sp, cmdp, frp, attach));
87 * Check for modifications.
90 * Contrary to POSIX 1003.2-1992, autowrite did not affect :edit.
92 if (file_m2(sp, FL_ISSET(cmdp->iflags, E_C_FORCE)))
96 if (file_init(sp, frp, NULL, (setalt ? FS_SETALT : 0) |
97 (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0)))
100 F_SET(sp, SC_FSWITCH);
106 * New screen version of ex_edit.
109 ex_N_edit(SCR *sp, EXCMD *cmdp, FREF *frp, int attach)
113 /* Get a new screen. */
114 if (screen_init(sp->gp, sp, &new))
116 if ((cmdp->cmd == &cmds[C_VSPLIT] && vs_vsplit(sp, new)) ||
117 (cmdp->cmd != &cmds[C_VSPLIT] && vs_split(sp, new, 0))) {
118 (void)screen_end(new);
122 /* Get a backing file. */
124 /* Copy file state, keep the screen and cursor the same. */
129 new->frp->flags = sp->frp->flags;
134 #if defined(USE_WIDECHAR) && defined(USE_ICONV)
135 /* Synchronize the iconv environments. */
136 o_set(new, O_FILEENCODING, OS_STRDUP,
137 O_STR(sp, O_FILEENCODING), 0);
138 conv_enc(new, O_FILEENCODING, 0);
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);