use three-argument AC_DEFINE instead of acconfig.h
[nvi.git] / ex / ex_append.c
blobee3a40e8f23afcd1363678ad847ea0eaf1d482c8
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_append.c,v 10.34 2001/06/25 15:19:14 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:14 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
25 #include "../common/common.h"
27 enum which {APPEND, CHANGE, INSERT};
29 static int ex_aci __P((SCR *, EXCMD *, enum which));
32 * ex_append -- :[line] a[ppend][!]
33 * Append one or more lines of new text after the specified line,
34 * or the current line if no address is specified.
36 * PUBLIC: int ex_append __P((SCR *, EXCMD *));
38 int
39 ex_append(SCR *sp, EXCMD *cmdp)
41 return (ex_aci(sp, cmdp, APPEND));
45 * ex_change -- :[line[,line]] c[hange][!] [count]
46 * Change one or more lines to the input text.
48 * PUBLIC: int ex_change __P((SCR *, EXCMD *));
50 int
51 ex_change(SCR *sp, EXCMD *cmdp)
53 return (ex_aci(sp, cmdp, CHANGE));
57 * ex_insert -- :[line] i[nsert][!]
58 * Insert one or more lines of new text before the specified line,
59 * or the current line if no address is specified.
61 * PUBLIC: int ex_insert __P((SCR *, EXCMD *));
63 int
64 ex_insert(SCR *sp, EXCMD *cmdp)
66 return (ex_aci(sp, cmdp, INSERT));
70 * ex_aci --
71 * Append, change, insert in ex.
73 static int
74 ex_aci(SCR *sp, EXCMD *cmdp, enum which cmd)
76 CHAR_T *p, *t;
77 GS *gp;
78 TEXT *tp;
79 TEXTH tiq;
80 db_recno_t cnt, lno;
81 size_t len;
82 u_int32_t flags;
83 int need_newline;
85 gp = sp->gp;
86 NEEDFILE(sp, cmdp);
89 * If doing a change, replace lines for as long as possible. Then,
90 * append more lines or delete remaining lines. Changes to an empty
91 * file are appends, inserts are the same as appends to the previous
92 * line.
94 * !!!
95 * Set the address to which we'll append. We set sp->lno to this
96 * address as well so that autoindent works correctly when get text
97 * from the user.
99 lno = cmdp->addr1.lno;
100 sp->lno = lno;
101 if ((cmd == CHANGE || cmd == INSERT) && lno != 0)
102 --lno;
105 * !!!
106 * If the file isn't empty, cut changes into the unnamed buffer.
108 if (cmd == CHANGE && cmdp->addr1.lno != 0 &&
109 (cut(sp, NULL, &cmdp->addr1, &cmdp->addr2, CUT_LINEMODE) ||
110 del(sp, &cmdp->addr1, &cmdp->addr2, 1)))
111 return (1);
114 * !!!
115 * Anything that was left after the command separator becomes part
116 * of the inserted text. Apparently, it was common usage to enter:
118 * :g/pattern/append|stuff1
120 * and append the line of text "stuff1" to the lines containing the
121 * pattern. It was also historically legal to enter:
123 * :append|stuff1
124 * stuff2
127 * and the text on the ex command line would be appended as well as
128 * the text inserted after it. There was an historic bug however,
129 * that the user had to enter *two* terminating lines (the '.' lines)
130 * to terminate text input mode, in this case. This whole thing
131 * could be taken too far, however. Entering:
133 * :append|stuff1\
134 * stuff2
135 * stuff3
138 * i.e. mixing and matching the forms confused the historic vi, and,
139 * not only did it take two terminating lines to terminate text input
140 * mode, but the trailing backslashes were retained on the input. We
141 * match historic practice except that we discard the backslashes.
143 * Input lines specified on the ex command line lines are separated by
144 * <newline>s. If there is a trailing delimiter an empty line was
145 * inserted. There may also be a leading delimiter, which is ignored
146 * unless it's also a trailing delimiter. It is possible to encounter
147 * a termination line, i.e. a single '.', in a global command, but not
148 * necessary if the text insert command was the last of the global
149 * commands.
151 if (cmdp->save_cmdlen != 0) {
152 for (p = cmdp->save_cmd,
153 len = cmdp->save_cmdlen; len > 0; p = t) {
154 for (t = p; len > 0 && t[0] != '\n'; ++t, --len);
155 if (t != p || len == 0) {
156 if (F_ISSET(sp, SC_EX_GLOBAL) &&
157 t - p == 1 && p[0] == '.') {
158 ++t;
159 if (len > 0)
160 --len;
161 break;
163 if (db_append(sp, 1, lno++, p, t - p))
164 return (1);
166 if (len != 0) {
167 ++t;
168 if (--len == 0 &&
169 db_append(sp, 1, lno++, NULL, 0))
170 return (1);
174 * If there's any remaining text, we're in a global, and
175 * there's more command to parse.
177 * !!!
178 * We depend on the fact that non-global commands will eat the
179 * rest of the command line as text input, and before getting
180 * any text input from the user. Otherwise, we'd have to save
181 * off the command text before or during the call to the text
182 * input function below.
184 if (len != 0)
185 cmdp->save_cmd = t;
186 cmdp->save_cmdlen = len;
189 if (F_ISSET(sp, SC_EX_GLOBAL)) {
190 if ((sp->lno = lno) == 0 && db_exist(sp, 1))
191 sp->lno = 1;
192 return (0);
196 * If not in a global command, read from the terminal.
198 * If this code is called by vi, we want to reset the terminal and use
199 * ex's line get routine. It actually works fine if we use vi's get
200 * routine, but it doesn't look as nice. Maybe if we had a separate
201 * window or something, but getting a line at a time looks awkward.
202 * However, depending on the screen that we're using, that may not
203 * be possible.
205 if (F_ISSET(sp, SC_VI)) {
206 if (gp->scr_screen(sp, SC_EX)) {
207 ex_wemsg(sp, cmdp->cmd->name, EXM_NOCANON);
208 return (1);
211 /* If we're still in the vi screen, move out explicitly. */
212 need_newline = !F_ISSET(sp, SC_SCR_EXWROTE);
213 F_SET(sp, SC_SCR_EX | SC_SCR_EXWROTE);
214 if (need_newline)
215 (void)ex_puts(sp, "\n");
218 * !!!
219 * Users of historical versions of vi sometimes get confused
220 * when they enter append mode, and can't seem to get out of
221 * it. Give them an informational message.
223 (void)ex_puts(sp,
224 msg_cat(sp, "273|Entering ex input mode.", NULL));
225 (void)ex_puts(sp, "\n");
226 (void)ex_fflush(sp);
230 * Set input flags; the ! flag turns off autoindent for append,
231 * change and insert.
233 LF_INIT(TXT_DOTTERM | TXT_NUMBER);
234 if (!FL_ISSET(cmdp->iflags, E_C_FORCE) && O_ISSET(sp, O_AUTOINDENT))
235 LF_SET(TXT_AUTOINDENT);
236 if (O_ISSET(sp, O_BEAUTIFY))
237 LF_SET(TXT_BEAUTIFY);
240 * This code can't use the common screen TEXTH structure (sp->tiq),
241 * as it may already be in use, e.g. ":append|s/abc/ABC/" would fail
242 * as we are only halfway through the text when the append code fires.
243 * Use a local structure instead. (The ex code would have to use a
244 * local structure except that we're guaranteed to finish remaining
245 * characters in the common TEXTH structure when they were inserted
246 * into the file, above.)
248 memset(&tiq, 0, sizeof(TEXTH));
249 CIRCLEQ_INIT(&tiq);
251 if (ex_txt(sp, &tiq, 0, flags))
252 return (1);
254 for (cnt = 0, tp = tiq.cqh_first;
255 tp != (TEXT *)&tiq; ++cnt, tp = tp->q.cqe_next)
256 if (db_append(sp, 1, lno++, tp->lb, tp->len))
257 return (1);
260 * Set sp->lno to the final line number value (correcting for a
261 * possible 0 value) as that's historically correct for the final
262 * line value, whether or not the user entered any text.
264 if ((sp->lno = lno) == 0 && db_exist(sp, 1))
265 sp->lno = 1;
267 return (0);