update to 1.01
[nvi.git] / ex / ex_append.c
blobb3cbf94e597aef20448f023e4ec165c7bbb83965
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_append.c,v 8.6 1993/11/23 11:18:06 bostic Exp $ (Berkeley) $Date: 1993/11/23 11:18:06 $";
10 #endif /* not lint */
12 #include <sys/types.h>
14 #include "vi.h"
15 #include "excmd.h"
17 enum which {APPEND, CHANGE, INSERT};
19 static int aci __P((SCR *, EXF *, EXCMDARG *, enum which));
22 * ex_append -- :[line] a[ppend][!]
23 * Append one or more lines of new text after the specified line,
24 * or the current line if no address is specified.
26 int
27 ex_append(sp, ep, cmdp)
28 SCR *sp;
29 EXF *ep;
30 EXCMDARG *cmdp;
32 return (aci(sp, ep, cmdp, APPEND));
36 * ex_change -- :[line[,line]] c[hange][!] [count]
37 * Change one or more lines to the input text.
39 int
40 ex_change(sp, ep, cmdp)
41 SCR *sp;
42 EXF *ep;
43 EXCMDARG *cmdp;
45 return (aci(sp, ep, cmdp, CHANGE));
49 * ex_insert -- :[line] i[nsert][!]
50 * Insert one or more lines of new text before the specified line,
51 * or the current line if no address is specified.
53 int
54 ex_insert(sp, ep, cmdp)
55 SCR *sp;
56 EXF *ep;
57 EXCMDARG *cmdp;
59 return (aci(sp, ep, cmdp, INSERT));
62 static int
63 aci(sp, ep, cmdp, cmd)
64 SCR *sp;
65 EXF *ep;
66 EXCMDARG *cmdp;
67 enum which cmd;
69 MARK m;
70 TEXT *tp;
71 recno_t cnt;
72 int rval, aiset;
75 * The ! flag turns off autoindent for append, change and
76 * insert.
78 if (F_ISSET(cmdp, E_FORCE)) {
79 aiset = O_ISSET(sp, O_AUTOINDENT);
80 O_CLR(sp, O_AUTOINDENT);
81 } else
82 aiset = 0;
85 * If doing a change, replace lines as long as possible.
86 * Then, append more lines or delete remaining lines.
87 * Inserts are the same as appends to the previous line.
89 rval = 0;
90 m = cmdp->addr1;
91 if (cmd == INSERT) {
92 --m.lno;
93 cmd = APPEND;
95 if (cmd == CHANGE)
96 for (;; ++m.lno) {
97 if (m.lno > cmdp->addr2.lno) {
98 cmd = APPEND;
99 --m.lno;
100 break;
102 switch (sp->s_get(sp, ep, &sp->tiq, 0,
103 TXT_BEAUTIFY | TXT_CR | TXT_NLECHO)) {
104 case INP_OK:
105 break;
106 case INP_EOF:
107 case INP_ERR:
108 rval = 1;
109 goto done;
111 tp = sp->tiq.cqh_first;
112 if (tp->len == 1 && tp->lb[0] == '.') {
113 for (cnt =
114 (cmdp->addr2.lno - m.lno) + 1; cnt--;)
115 if (file_dline(sp, ep, m.lno)) {
116 rval = 1;
117 goto done;
119 goto done;
121 if (file_sline(sp, ep, m.lno, tp->lb, tp->len)) {
122 rval = 1;
123 goto done;
127 if (cmd == APPEND)
128 for (;; ++m.lno) {
129 switch (sp->s_get(sp, ep, &sp->tiq, 0,
130 TXT_BEAUTIFY | TXT_CR | TXT_NLECHO)) {
131 case INP_OK:
132 break;
133 case INP_EOF:
134 case INP_ERR:
135 rval = 1;
136 goto done;
138 tp = sp->tiq.cqh_first;
139 if (tp->len == 1 && tp->lb[0] == '.')
140 break;
141 if (file_aline(sp, ep, 1, m.lno, tp->lb, tp->len)) {
142 rval = 1;
143 goto done;
147 done: if (rval == 0)
148 sp->lno = m.lno;
150 if (aiset)
151 O_SET(sp, O_AUTOINDENT);
153 return (rval);