*** empty log message ***
[nvi.git] / ex / ex_join.c
blobe46708a0841897346299adc6c92ace901d142d4a
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_join.c,v 10.12 2000/06/27 17:19:06 skimo Exp $ (Berkeley) $Date: 2000/06/27 17:19:06 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
20 #include <ctype.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include "../common/common.h"
29 * ex_join -- :[line [,line]] j[oin][!] [count] [flags]
30 * Join lines.
32 * PUBLIC: int ex_join __P((SCR *, EXCMD *));
34 int
35 ex_join(sp, cmdp)
36 SCR *sp;
37 EXCMD *cmdp;
39 db_recno_t from, to;
40 size_t blen, clen, len, tlen;
41 int echar, extra, first;
42 char *bp, *tbp;
43 CHAR_T *p;
45 NEEDFILE(sp, cmdp);
47 from = cmdp->addr1.lno;
48 to = cmdp->addr2.lno;
50 /* Check for no lines to join. */
51 if (!db_exist(sp, from + 1)) {
52 msgq(sp, M_ERR, "131|No following lines to join");
53 return (1);
56 GET_SPACE_RET(sp, bp, blen, 256);
59 * The count for the join command was off-by-one,
60 * historically, to other counts for other commands.
62 if (FL_ISSET(cmdp->iflags, E_C_COUNT))
63 ++cmdp->addr2.lno;
66 * If only a single address specified, or, the same address
67 * specified twice, the from/two addresses will be the same.
69 if (cmdp->addr1.lno == cmdp->addr2.lno)
70 ++cmdp->addr2.lno;
72 clen = tlen = 0;
73 for (first = 1,
74 from = cmdp->addr1.lno, to = cmdp->addr2.lno; from <= to; ++from) {
76 * Get next line. Historic versions of vi allowed "10J" while
77 * less than 10 lines from the end-of-file, so we do too.
79 if (db_get(sp, from, 0, &p, &len)) {
80 cmdp->addr2.lno = from - 1;
81 break;
84 /* Empty lines just go away. */
85 if (len == 0)
86 continue;
89 * Get more space if necessary. Note, tlen isn't the length
90 * of the new line, it's roughly the amount of space needed.
91 * tbp - bp is the length of the new line.
93 tlen += len + 2;
94 ADD_SPACE_RET(sp, bp, blen, tlen);
95 tbp = bp + clen;
98 * Historic practice:
100 * If force specified, join without modification.
101 * If the current line ends with whitespace, strip leading
102 * whitespace from the joined line.
103 * If the next line starts with a ), do nothing.
104 * If the current line ends with ., insert two spaces.
105 * Else, insert one space.
107 * One change -- add ? and ! to the list of characters for
108 * which we insert two spaces. I expect that POSIX 1003.2
109 * will require this as well.
111 * Echar is the last character in the last line joined.
113 extra = 0;
114 if (!first && !FL_ISSET(cmdp->iflags, E_C_FORCE)) {
115 if (isblank(echar))
116 for (; len && isblank(*p); --len, ++p);
117 else if (p[0] != ')') {
118 if (strchr(".?!", echar)) {
119 *tbp++ = ' ';
120 ++clen;
121 extra = 1;
123 *tbp++ = ' ';
124 ++clen;
125 for (; len && isblank(*p); --len, ++p);
129 if (len != 0) {
130 memcpy(tbp, p, len);
131 tbp += len;
132 clen += len;
133 echar = p[len - 1];
134 } else
135 echar = ' ';
138 * Historic practice for vi was to put the cursor at the first
139 * inserted whitespace character, if there was one, or the
140 * first character of the joined line, if there wasn't, or the
141 * last character of the line if joined to an empty line. If
142 * a count was specified, the cursor was moved as described
143 * for the first line joined, ignoring subsequent lines. If
144 * the join was a ':' command, the cursor was placed at the
145 * first non-blank character of the line unless the cursor was
146 * "attracted" to the end of line when the command was executed
147 * in which case it moved to the new end of line. There are
148 * probably several more special cases, but frankly, my dear,
149 * I don't give a damn. This implementation puts the cursor
150 * on the first inserted whitespace character, the first
151 * character of the joined line, or the last character of the
152 * line regardless. Note, if the cursor isn't on the joined
153 * line (possible with : commands), it is reset to the starting
154 * line.
156 if (first) {
157 sp->cno = (tbp - bp) - (1 + extra);
158 first = 0;
159 } else
160 sp->cno = (tbp - bp) - len - (1 + extra);
162 sp->lno = cmdp->addr1.lno;
164 /* Delete the joined lines. */
165 for (from = cmdp->addr1.lno, to = cmdp->addr2.lno; to > from; --to)
166 if (db_delete(sp, to))
167 goto err;
169 /* If the original line changed, reset it. */
170 if (!first && db_set(sp, from, bp, tbp - bp)) {
171 err: FREE_SPACE(sp, bp, blen);
172 return (1);
174 FREE_SPACE(sp, bp, blen);
176 sp->rptlines[L_JOINED] += (cmdp->addr2.lno - cmdp->addr1.lno) + 1;
177 return (0);