add EX_AUTOPRINT flag to ex private structure, replaces S_AUTOPRINT
[nvi.git] / ex / ex_join.c
blob7d8caa3bfd553c09b3d8141293035c7e5da9352a
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_join.c,v 8.8 1993/12/29 09:50:50 bostic Exp $ (Berkeley) $Date: 1993/12/29 09:50:50 $";
10 #endif /* not lint */
12 #include <sys/types.h>
14 #include <ctype.h>
15 #include <stdlib.h>
16 #include <string.h>
18 #include "vi.h"
19 #include "excmd.h"
22 * ex_join -- :[line [,line]] j[oin][!] [count] [flags]
23 * Join lines.
25 int
26 ex_join(sp, ep, cmdp)
27 SCR *sp;
28 EXF *ep;
29 EXCMDARG *cmdp;
31 recno_t from, to;
32 size_t blen, clen, len, tlen;
33 int echar, extra, first;
34 char *bp, *p, *tbp;
36 from = cmdp->addr1.lno;
37 to = cmdp->addr2.lno;
39 /* Check for no lines to join. */
40 if ((p = file_gline(sp, ep, from + 1, &len)) == NULL) {
41 msgq(sp, M_ERR, "No following lines to join.");
42 return (1);
45 GET_SPACE_RET(sp, bp, blen, 256);
48 * The count for the join command was off-by-one,
49 * historically, to other counts for other commands.
51 if (F_ISSET(cmdp, E_COUNT))
52 ++cmdp->addr2.lno;
55 * If only a single address specified, or, the same address
56 * specified twice, the from/two addresses will be the same.
58 if (cmdp->addr1.lno == cmdp->addr2.lno)
59 ++cmdp->addr2.lno;
61 clen = tlen = 0;
62 for (first = 1, from = cmdp->addr1.lno,
63 to = cmdp->addr2.lno; from <= to; ++from) {
65 * Get next line. Historic versions of vi allowed "10J" while
66 * less than 10 lines from the end-of-file, so we do too.
68 if ((p = file_gline(sp, ep, from, &len)) == NULL) {
69 cmdp->addr2.lno = from - 1;
70 break;
73 /* Empty lines just go away. */
74 if (len == 0)
75 continue;
78 * Get more space if necessary. Note, tlen isn't the length
79 * of the new line, it's roughly the amount of space needed.
80 * tbp - bp is the length of the new line.
82 tlen += len + 2;
83 ADD_SPACE_RET(sp, bp, blen, tlen);
84 tbp = bp + clen;
87 * Historic practice:
89 * If force specified, join without modification.
90 * If the current line ends with whitespace, strip leading
91 * whitespace from the joined line.
92 * If the next line starts with a ), do nothing.
93 * If the current line ends with ., ? or !, insert two spaces.
94 * Else, insert one space.
96 * Echar is the last character in the last line joined.
98 extra = 0;
99 if (!first && !F_ISSET(cmdp, E_FORCE)) {
100 if (isblank(echar))
101 for (; len && isblank(*p); --len, ++p);
102 else if (p[0] != ')') {
103 if (strchr(".?!", echar)) {
104 *tbp++ = ' ';
105 ++clen;
106 extra = 1;
108 *tbp++ = ' ';
109 ++clen;
110 for (; len && isblank(*p); --len, ++p);
114 if (len != 0) {
115 memmove(tbp, p, len);
116 tbp += len;
117 clen += len;
118 echar = p[len - 1];
119 } else
120 echar = ' ';
123 * Historic practice for vi was to put the cursor at the first
124 * inserted whitespace character, if there was one, or the
125 * first character of the joined line, if there wasn't, or the
126 * last character of the line if joined to an empty line. If
127 * a count was specified, the cursor was moved as described
128 * for the first line joined, ignoring subsequent lines. If
129 * the join was a ':' command, the cursor was placed at the
130 * first non-blank character of the line unless the cursor was
131 * "attracted" to the end of line when the command was executed
132 * in which case it moved to the new end of line. There are
133 * probably several more special cases, but frankly, my dear,
134 * I don't give a damn. This implementation puts the cursor
135 * on the first inserted whitespace character, the first
136 * character of the joined line, or the last character of the
137 * line regardless. Note, if the cursor isn't on the joined
138 * line (possible with : commands), it is reset to the starting
139 * line.
141 if (first) {
142 sp->cno = (tbp - bp) - (1 + extra);
143 first = 0;
144 } else
145 sp->cno = (tbp - bp) - len - (1 + extra);
147 sp->lno = cmdp->addr1.lno;
149 /* Delete the joined lines. */
150 for (from = cmdp->addr1.lno, to = cmdp->addr2.lno; to > from; --to)
151 if (file_dline(sp, ep, to))
152 goto err;
154 /* Reset the original line. */
155 if (file_sline(sp, ep, from, bp, tbp - bp)) {
156 err: FREE_SPACE(sp, bp, blen);
157 return (1);
159 FREE_SPACE(sp, bp, blen);
161 sp->rptlines[L_JOINED] += (cmdp->addr2.lno - cmdp->addr1.lno) + 1;
162 return (0);