text input: fix problem with autoindenting and ^^D
[nvi.git] / ex / ex_join.c
blob5420b310a9db61908f725a64d7f4f5e713e1e69d
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.17 2004/03/16 14:14:04 skimo Exp $ (Berkeley) $Date: 2004/03/16 14:14:04 $";
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 (F_ISSET(cmdp, E_ADDR_DEF) || cmdp->addrcnt == 1)
61 ++cmdp->addr2.lno;
63 clen = tlen = 0;
64 for (first = 1,
65 from = cmdp->addr1.lno, to = cmdp->addr2.lno; from <= to; ++from) {
67 * Get next line. Historic versions of vi allowed "10J" while
68 * less than 10 lines from the end-of-file, so we do too.
70 if (db_get(sp, from, 0, &p, &len)) {
71 cmdp->addr2.lno = from - 1;
72 break;
75 /* Empty lines just go away. */
76 if (len == 0)
77 continue;
80 * Get more space if necessary. Note, tlen isn't the length
81 * of the new line, it's roughly the amount of space needed.
82 * tbp - bp is the length of the new line.
84 tlen += len + 2;
85 ADD_SPACE_RETW(sp, bp, blen, tlen);
86 tbp = bp + clen;
89 * Historic practice:
91 * If force specified, join without modification.
92 * If the current line ends with whitespace, strip leading
93 * whitespace from the joined line.
94 * If the next line starts with a ), do nothing.
95 * If the current line ends with ., insert two spaces.
96 * Else, insert one space.
98 * One change -- add ? and ! to the list of characters for
99 * which we insert two spaces. I expect that POSIX 1003.2
100 * will require this as well.
102 * Echar is the last character in the last line joined.
104 extra = 0;
105 if (!first && !FL_ISSET(cmdp->iflags, E_C_FORCE)) {
106 if (isblank(echar))
107 for (; len && isblank(*p); --len, ++p);
108 else if (p[0] != ')') {
109 if (strchr(".?!", echar)) {
110 *tbp++ = ' ';
111 ++clen;
112 extra = 1;
114 *tbp++ = ' ';
115 ++clen;
116 for (; len && isblank(*p); --len, ++p);
120 if (len != 0) {
121 MEMCPYW(tbp, p, len);
122 tbp += len;
123 clen += len;
124 echar = p[len - 1];
125 } else
126 echar = ' ';
129 * Historic practice for vi was to put the cursor at the first
130 * inserted whitespace character, if there was one, or the
131 * first character of the joined line, if there wasn't, or the
132 * last character of the line if joined to an empty line. If
133 * a count was specified, the cursor was moved as described
134 * for the first line joined, ignoring subsequent lines. If
135 * the join was a ':' command, the cursor was placed at the
136 * first non-blank character of the line unless the cursor was
137 * "attracted" to the end of line when the command was executed
138 * in which case it moved to the new end of line. There are
139 * probably several more special cases, but frankly, my dear,
140 * I don't give a damn. This implementation puts the cursor
141 * on the first inserted whitespace character, the first
142 * character of the joined line, or the last character of the
143 * line regardless. Note, if the cursor isn't on the joined
144 * line (possible with : commands), it is reset to the starting
145 * line.
147 if (first) {
148 sp->cno = (tbp - bp) - (1 + extra);
149 first = 0;
150 } else
151 sp->cno = (tbp - bp) - len - (1 + extra);
153 sp->lno = cmdp->addr1.lno;
155 /* Delete the joined lines. */
156 for (from = cmdp->addr1.lno, to = cmdp->addr2.lno; to > from; --to)
157 if (db_delete(sp, to))
158 goto err;
160 /* If the original line changed, reset it. */
161 if (!first && db_set(sp, from, bp, tbp - bp)) {
162 err: FREE_SPACEW(sp, bp, blen);
163 return (1);
165 FREE_SPACEW(sp, bp, blen);
167 sp->rptlines[L_JOINED] += (cmdp->addr2.lno - cmdp->addr1.lno) + 1;
168 return (0);