rework initial error handling -- since we check for any screens on
[nvi.git] / ex / ex_edit.c
blob10446b32081da6ccad701b9c9459554ea688d722
1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
6 */
8 #ifndef lint
9 static char sccsid[] = "$Id: ex_edit.c,v 8.14 1994/01/22 20:41:39 bostic Exp $ (Berkeley) $Date: 1994/01/22 20:41:39 $";
10 #endif /* not lint */
12 #include <sys/types.h>
14 #include <errno.h>
15 #include <stdlib.h>
16 #include <string.h>
18 #include "vi.h"
19 #include "excmd.h"
22 * ex_edit -- :e[dit][!] [+cmd] [file]
23 * :vi[sual][!] [+cmd] [file]
25 * Edit a file; if none specified, re-edit the current file. The second
26 * form of the command can only be executed while in vi mode. See the
27 * hack in ex.c:ex_cmd().
29 * !!!
30 * Historic vi didn't permit the '+' command form without specifying
31 * a file name as well.
33 int
34 ex_edit(sp, ep, cmdp)
35 SCR *sp;
36 EXF *ep;
37 EXCMDARG *cmdp;
39 ARGS *ap;
40 FREF *frp;
42 frp = sp->frp;
43 switch (cmdp->argc) {
44 case 0:
46 * If the name has been changed, we edit that file, not the
47 * original name. If the user was editing a temporary file,
48 * create another one. The reason for this is that we do
49 * special exit processing of temporary files, and reusing
50 * them is tricky.
52 if (frp->cname != NULL) {
53 if ((frp = file_add(sp, frp, frp->cname, 1)) == NULL)
54 return (1);
55 set_alt_name(sp, sp->frp->cname);
56 } else if (frp->name == NULL)
57 if ((frp = file_add(sp, frp, NULL, 1)) == NULL)
58 return (1);
59 break;
60 case 1:
61 ap = cmdp->argv[0];
62 if ((frp = file_add(sp, sp->frp, ap->bp, 1)) == NULL)
63 return (1);
64 set_alt_name(sp, ap->bp);
65 break;
66 default:
67 abort();
71 * Check for modifications.
73 * !!!
74 * Contrary to POSIX 1003.2-1992, autowrite did not affect :edit.
76 if (F_ISSET(ep, F_MODIFIED) &&
77 ep->refcnt <= 1 && !F_ISSET(cmdp, E_FORCE)) {
78 msgq(sp, M_ERR,
79 "Modified since last write; write or use ! to override.");
80 return (1);
83 /* Switch files. */
84 if (file_init(sp, frp, NULL, F_ISSET(cmdp, E_FORCE)))
85 return (1);
86 F_SET(sp, S_FSWITCH);
87 return (0);