update to 1.01
[nvi.git] / ex / ex_edit.c
blobd65bf72b2b32d507cacba7b7f236f24f7e6795f7
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.13 1993/12/03 15:40:48 bostic Exp $ (Berkeley) $Date: 1993/12/03 15:40:48 $";
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
47 * the original name.
49 if (frp->cname != NULL) {
50 if ((frp = file_add(sp, frp, frp->cname, 1)) == NULL)
51 return (1);
52 set_alt_name(sp, sp->frp->cname);
54 break;
55 case 1:
56 ap = cmdp->argv[0];
57 if ((frp = file_add(sp, sp->frp, ap->bp, 1)) == NULL)
58 return (1);
59 set_alt_name(sp, ap->bp);
60 break;
61 default:
62 abort();
66 * Check for modifications.
68 * !!!
69 * Contrary to POSIX 1003.2-1992, autowrite did not affect :edit.
71 if (F_ISSET(ep, F_MODIFIED) &&
72 ep->refcnt <= 1 && !F_ISSET(cmdp, E_FORCE)) {
73 msgq(sp, M_ERR,
74 "Modified since last write; write or use ! to override.");
75 return (1);
78 /* Switch files. */
79 if (file_init(sp, frp, NULL, F_ISSET(cmdp, E_FORCE)))
80 return (1);
81 F_SET(sp, S_FSWITCH);
82 return (0);