protoize most of the function prototypes
[nvi.git] / ex / ex_join.c
blob1fad853a229712f38f69bc85c81907ff87f0576b
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.16 2001/06/25 15:19:16 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:16 $";
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(SCR *sp, EXCMD *cmdp)
37 db_recno_t from, to;
38 size_t blen, clen, len, tlen;
39 int echar, extra, first;
40 CHAR_T *bp, *tbp;
41 CHAR_T *p;
43 NEEDFILE(sp, cmdp);
45 from = cmdp->addr1.lno;
46 to = cmdp->addr2.lno;
48 /* Check for no lines to join. */
49 if (!db_exist(sp, from + 1)) {
50 msgq(sp, M_ERR, "131|No following lines to join");
51 return (1);
54 GET_SPACE_RETW(sp, bp, blen, 256);
57 * The count for the join command was off-by-one,
58 * historically, to other counts for other commands.
60 if (FL_ISSET(cmdp->iflags, E_C_COUNT))
61 ++cmdp->addr2.lno;
64 * If only a single address specified, or, the same address
65 * specified twice, the from/two addresses will be the same.
67 if (cmdp->addr1.lno == cmdp->addr2.lno)
68 ++cmdp->addr2.lno;
70 clen = tlen = 0;
71 for (first = 1,
72 from = cmdp->addr1.lno, to = cmdp->addr2.lno; from <= to; ++from) {
74 * Get next line. Historic versions of vi allowed "10J" while
75 * less than 10 lines from the end-of-file, so we do too.
77 if (db_get(sp, from, 0, &p, &len)) {
78 cmdp->addr2.lno = from - 1;
79 break;
82 /* Empty lines just go away. */
83 if (len == 0)
84 continue;
87 * Get more space if necessary. Note, tlen isn't the length
88 * of the new line, it's roughly the amount of space needed.
89 * tbp - bp is the length of the new line.
91 tlen += len + 2;
92 ADD_SPACE_RETW(sp, bp, blen, tlen);
93 tbp = bp + clen;
96 * Historic practice:
98 * If force specified, join without modification.
99 * If the current line ends with whitespace, strip leading
100 * whitespace from the joined line.
101 * If the next line starts with a ), do nothing.
102 * If the current line ends with ., insert two spaces.
103 * Else, insert one space.
105 * One change -- add ? and ! to the list of characters for
106 * which we insert two spaces. I expect that POSIX 1003.2
107 * will require this as well.
109 * Echar is the last character in the last line joined.
111 extra = 0;
112 if (!first && !FL_ISSET(cmdp->iflags, E_C_FORCE)) {
113 if (isblank(echar))
114 for (; len && isblank(*p); --len, ++p);
115 else if (p[0] != ')') {
116 if (strchr(".?!", echar)) {
117 *tbp++ = ' ';
118 ++clen;
119 extra = 1;
121 *tbp++ = ' ';
122 ++clen;
123 for (; len && isblank(*p); --len, ++p);
127 if (len != 0) {
128 MEMCPYW(tbp, p, len);
129 tbp += len;
130 clen += len;
131 echar = p[len - 1];
132 } else
133 echar = ' ';
136 * Historic practice for vi was to put the cursor at the first
137 * inserted whitespace character, if there was one, or the
138 * first character of the joined line, if there wasn't, or the
139 * last character of the line if joined to an empty line. If
140 * a count was specified, the cursor was moved as described
141 * for the first line joined, ignoring subsequent lines. If
142 * the join was a ':' command, the cursor was placed at the
143 * first non-blank character of the line unless the cursor was
144 * "attracted" to the end of line when the command was executed
145 * in which case it moved to the new end of line. There are
146 * probably several more special cases, but frankly, my dear,
147 * I don't give a damn. This implementation puts the cursor
148 * on the first inserted whitespace character, the first
149 * character of the joined line, or the last character of the
150 * line regardless. Note, if the cursor isn't on the joined
151 * line (possible with : commands), it is reset to the starting
152 * line.
154 if (first) {
155 sp->cno = (tbp - bp) - (1 + extra);
156 first = 0;
157 } else
158 sp->cno = (tbp - bp) - len - (1 + extra);
160 sp->lno = cmdp->addr1.lno;
162 /* Delete the joined lines. */
163 for (from = cmdp->addr1.lno, to = cmdp->addr2.lno; to > from; --to)
164 if (db_delete(sp, to))
165 goto err;
167 /* If the original line changed, reset it. */
168 if (!first && db_set(sp, from, bp, tbp - bp)) {
169 err: FREE_SPACEW(sp, bp, blen);
170 return (1);
172 FREE_SPACEW(sp, bp, blen);
174 sp->rptlines[L_JOINED] += (cmdp->addr2.lno - cmdp->addr1.lno) + 1;
175 return (0);