2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
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 $";
12 #include <sys/types.h>
22 * ex_join -- :[line [,line]] j[oin][!] [count] [flags]
32 size_t blen
, clen
, len
, tlen
;
33 int echar
, extra
, first
;
36 from
= cmdp
->addr1
.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.");
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
))
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
)
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;
73 /* Empty lines just go away. */
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.
83 ADD_SPACE_RET(sp
, bp
, blen
, tlen
);
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.
99 if (!first
&& !F_ISSET(cmdp
, E_FORCE
)) {
101 for (; len
&& isblank(*p
); --len
, ++p
);
102 else if (p
[0] != ')') {
103 if (strchr(".?!", echar
)) {
110 for (; len
&& isblank(*p
); --len
, ++p
);
115 memmove(tbp
, p
, len
);
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
142 sp
->cno
= (tbp
- bp
) - (1 + extra
);
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
))
154 /* Reset the original line. */
155 if (file_sline(sp
, ep
, from
, bp
, tbp
- bp
)) {
156 err
: FREE_SPACE(sp
, bp
, blen
);
159 FREE_SPACE(sp
, bp
, blen
);
161 sp
->rptlines
[L_JOINED
] += (cmdp
->addr2
.lno
- cmdp
->addr1
.lno
) + 1;