align CHAR_T string in log
[nvi.git] / ex / ex_edit.c
blob61036654bbba4efdd9aaa6fd8e45c1b2897c0a3c
1 /*-
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.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: ex_edit.c,v 10.12 2000/07/14 14:29:20 skimo Exp $ (Berkeley) $Date: 2000/07/14 14:29:20 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
20 #include <bitstring.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "../common/common.h"
28 #include "../vi/vi.h"
30 static int ex_N_edit __P((SCR *, EXCMD *, FREF *, int));
33 * ex_edit -- :e[dit][!] [+cmd] [file]
34 * :ex[!] [+cmd] [file]
35 * :vi[sual][!] [+cmd] [file]
37 * Edit a file; if none specified, re-edit the current file. The third
38 * form of the command can only be executed while in vi mode. See the
39 * hack in ex.c:ex_cmd().
41 * !!!
42 * Historic vi didn't permit the '+' command form without specifying
43 * a file name as well. This seems unreasonable, so we support it
44 * regardless.
46 * PUBLIC: int ex_edit __P((SCR *, EXCMD *));
48 int
49 ex_edit(sp, cmdp)
50 SCR *sp;
51 EXCMD *cmdp;
53 FREF *frp;
54 int attach, setalt;
55 char *np;
56 size_t nlen;
58 switch (cmdp->argc) {
59 case 0:
61 * If the name has been changed, we edit that file, not the
62 * original name. If the user was editing a temporary file
63 * (or wasn't editing any file), create another one. The
64 * reason for not reusing temporary files is that there is
65 * special exit processing of them, and reuse is tricky.
67 frp = sp->frp;
68 if (sp->ep == NULL || F_ISSET(frp, FR_TMPFILE)) {
69 if ((frp = file_add(sp, NULL)) == NULL)
70 return (1);
71 attach = 0;
72 } else
73 attach = 1;
74 setalt = 0;
75 break;
76 case 1:
77 INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1,
78 np, nlen);
79 if ((frp = file_add(sp, np)) == NULL)
80 return (1);
81 attach = 0;
82 setalt = 1;
83 set_alt_name(sp, np);
84 break;
85 default:
86 abort();
89 if (F_ISSET(cmdp, E_NEWSCREEN) || cmdp->cmd == &cmds[C_VSPLIT])
90 return (ex_N_edit(sp, cmdp, frp, attach));
93 * Check for modifications.
95 * !!!
96 * Contrary to POSIX 1003.2-1992, autowrite did not affect :edit.
98 if (file_m2(sp, FL_ISSET(cmdp->iflags, E_C_FORCE)))
99 return (1);
101 /* Switch files. */
102 if (file_init(sp, frp, NULL, (setalt ? FS_SETALT : 0) |
103 (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0)))
104 return (1);
106 F_SET(sp, SC_FSWITCH);
107 return (0);
111 * ex_N_edit --
112 * New screen version of ex_edit.
114 static int
115 ex_N_edit(sp, cmdp, frp, attach)
116 SCR *sp;
117 EXCMD *cmdp;
118 FREF *frp;
119 int attach;
121 SCR *new;
123 /* Get a new screen. */
124 if (screen_init(sp->gp, sp, &new))
125 return (1);
126 if (cmdp->cmd == &cmds[C_VSPLIT] && vs_vsplit(sp, new) ||
127 cmdp->cmd != &cmds[C_VSPLIT] && vs_split(sp, new, 0)) {
128 (void)screen_end(new);
129 return (1);
132 /* Get a backing file. */
133 if (attach) {
134 /* Copy file state, keep the screen and cursor the same. */
135 new->ep = sp->ep;
136 ++new->ep->refcnt;
138 new->frp = frp;
139 new->frp->flags = sp->frp->flags;
141 new->lno = sp->lno;
142 new->cno = sp->cno;
143 } else if (file_init(new, frp, NULL,
144 (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0))) {
145 (void)vs_discard(new, NULL);
146 (void)screen_end(new);
147 return (1);
150 /* Create the argument list. */
151 new->cargv = new->argv = ex_buildargv(sp, NULL, frp->name);
153 /* Set up the switch. */
154 sp->nextdisp = new;
155 F_SET(sp, SC_SSWITCH);
157 return (0);