go to 0.81
[nvi.git] / ex / ex_append.c
blob05c07920e9ebf607d00e3cd8d9b6b4e53e00303e
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.4 1993/11/05 11:08:33 bostic Exp $ (Berkeley) $Date: 1993/11/05 11:08:33 $";
10 #endif /* not lint */
12 #include <sys/types.h>
14 #include "vi.h"
15 #include "excmd.h"
17 enum which {APPEND, CHANGE};
19 static int ac __P((SCR *, EXF *, EXCMDARG *, enum which));
22 * ex_append -- :address append[!]
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 (ac(sp, ep, cmdp, APPEND));
36 * ex_change -- :range change[!] [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 (ac(sp, ep, cmdp, CHANGE));
48 static int
49 ac(sp, ep, cmdp, cmd)
50 SCR *sp;
51 EXF *ep;
52 EXCMDARG *cmdp;
53 enum which cmd;
55 MARK m;
56 TEXT *tp;
57 recno_t cnt;
58 int rval, aiset;
60 /* The ! flag turns off autoindent for change and append. */
61 if (F_ISSET(cmdp, E_FORCE)) {
62 aiset = O_ISSET(sp, O_AUTOINDENT);
63 O_CLR(sp, O_AUTOINDENT);
64 } else
65 aiset = 0;
67 rval = 0;
70 * If doing a change, replace lines as long as possible.
71 * Then, append more lines, or delete remaining lines.
73 m = cmdp->addr1;
74 if (m.lno == 0)
75 cmd = APPEND;
76 if (cmd == CHANGE)
77 for (;; ++m.lno) {
78 if (m.lno > cmdp->addr2.lno) {
79 cmd = APPEND;
80 --m.lno;
81 break;
83 switch (sp->s_get(sp, ep, &sp->txthdr, 0,
84 TXT_BEAUTIFY | TXT_CR | TXT_NLECHO)) {
85 case INP_OK:
86 break;
87 case INP_EOF:
88 case INP_ERR:
89 rval = 1;
90 goto done;
92 tp = sp->txthdr.next;
93 if (tp->len == 1 && tp->lb[0] == '.') {
94 cnt = cmdp->addr2.lno - m.lno;
95 while (cnt--)
96 if (file_dline(sp, ep, m.lno)) {
97 rval = 1;
98 goto done;
100 goto done;
102 if (file_sline(sp, ep, m.lno, tp->lb, tp->len)) {
103 rval = 1;
104 goto done;
108 if (cmd == APPEND)
109 for (;; ++m.lno) {
110 switch (sp->s_get(sp, ep, &sp->txthdr, 0,
111 TXT_BEAUTIFY | TXT_CR | TXT_NLECHO)) {
112 case INP_OK:
113 break;
114 case INP_EOF:
115 case INP_ERR:
116 rval = 1;
117 goto done;
119 tp = sp->txthdr.next;
120 if (tp->len == 1 && tp->lb[0] == '.')
121 break;
122 if (file_aline(sp, ep, 1, m.lno, tp->lb, tp->len)) {
123 rval = 1;
124 goto done;
128 done: if (rval == 0)
129 sp->lno = m.lno;
131 if (aiset)
132 O_SET(sp, O_AUTOINDENT);
134 return (rval);