#include "config.h"
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/time.h>
#include <bitstring.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../common/common.h"
#include "../vi/vi.h"
static int ex_N_edit(SCR *, EXCMD *, FREF *, int);
int
ex_edit(SCR *sp, EXCMD *cmdp)
{
FREF *frp;
int attach, setalt;
switch (cmdp->argc) {
case 0:
frp = sp->frp;
if (sp->ep == NULL || F_ISSET(frp, FR_TMPFILE)) {
if ((frp = file_add(sp, NULL)) == NULL)
return (1);
attach = 0;
} else
attach = 1;
setalt = 0;
break;
case 1:
if ((frp = file_add(sp, cmdp->argv[0]->bp)) == NULL)
return (1);
attach = 0;
setalt = 1;
set_alt_name(sp, cmdp->argv[0]->bp);
break;
default:
abort();
}
if (F_ISSET(cmdp, E_NEWSCREEN))
return (ex_N_edit(sp, cmdp, frp, attach));
if (file_m2(sp, FL_ISSET(cmdp->iflags, E_C_FORCE)))
return (1);
if (file_init(sp, frp, NULL, (setalt ? FS_SETALT : 0) |
(FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0)))
return (1);
F_SET(sp, SC_FSWITCH);
return (0);
}
static int
ex_N_edit(SCR *sp, EXCMD *cmdp, FREF *frp, int attach)
{
SCR *new;
if (screen_init(sp->gp, sp, &new))
return (1);
if (vs_split(sp, new, 0)) {
(void)screen_end(new);
return (1);
}
if (attach) {
new->ep = sp->ep;
++new->ep->refcnt;
new->frp = frp;
new->frp->flags = sp->frp->flags;
new->lno = sp->lno;
new->cno = sp->cno;
} else if (file_init(new, frp, NULL,
(FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0))) {
(void)vs_discard(new, NULL);
(void)screen_end(new);
return (1);
}
new->cargv = new->argv = ex_buildargv(sp, NULL, frp->name);
sp->nextdisp = new;
F_SET(sp, SC_SSWITCH);
return (0);
}