1 /* $NetBSD: ex_join.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
13 #include <sys/cdefs.h>
16 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 ";
19 __RCSID("$NetBSD: ex_join.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
22 #include <sys/types.h>
23 #include <sys/queue.h>
25 #include <bitstring.h>
32 #include "../common/common.h"
35 * ex_join -- :[line [,line]] j[oin][!] [count] [flags]
38 * PUBLIC: int ex_join __P((SCR *, EXCMD *));
41 ex_join(SCR
*sp
, EXCMD
*cmdp
)
44 size_t blen
, clen
, len
, tlen
;
47 CHAR_T
*p
, *bp
, *tbp
= NULL
;
51 from
= cmdp
->addr1
.lno
;
54 /* Check for no lines to join. */
55 if (!db_exist(sp
, from
+ 1)) {
56 msgq(sp
, M_ERR
, "131|No following lines to join");
60 GET_SPACE_RETW(sp
, bp
, blen
, 256);
63 * The count for the join command was off-by-one,
64 * historically, to other counts for other commands.
66 if (F_ISSET(cmdp
, E_ADDR_DEF
) || cmdp
->addrcnt
== 1)
71 from
= cmdp
->addr1
.lno
, to
= cmdp
->addr2
.lno
; from
<= to
; ++from
) {
73 * Get next line. Historic versions of vi allowed "10J" while
74 * less than 10 lines from the end-of-file, so we do too.
76 if (db_get(sp
, from
, 0, &p
, &len
)) {
77 cmdp
->addr2
.lno
= from
- 1;
81 /* Empty lines just go away. */
86 * Get more space if necessary. Note, tlen isn't the length
87 * of the new line, it's roughly the amount of space needed.
88 * tbp - bp is the length of the new line.
91 ADD_SPACE_RETW(sp
, bp
, blen
, tlen
);
97 * If force specified, join without modification.
98 * If the current line ends with whitespace, strip leading
99 * whitespace from the joined line.
100 * If the next line starts with a ), do nothing.
101 * If the current line ends with ., insert two spaces.
102 * Else, insert one space.
104 * One change -- add ? and ! to the list of characters for
105 * which we insert two spaces. I expect that POSIX 1003.2
106 * will require this as well.
108 * Echar is the last character in the last line joined.
111 if (!first
&& !FL_ISSET(cmdp
->iflags
, E_C_FORCE
)) {
113 for (; len
&& ISBLANK((UCHAR_T
)*p
); --len
, ++p
);
114 else if (p
[0] != ')') {
115 if (STRCHR(L(".?!"), echar
)) {
122 for (; len
&& ISBLANK((UCHAR_T
)*p
); --len
, ++p
);
127 MEMCPYW(tbp
, p
, len
);
135 * Historic practice for vi was to put the cursor at the first
136 * inserted whitespace character, if there was one, or the
137 * first character of the joined line, if there wasn't, or the
138 * last character of the line if joined to an empty line. If
139 * a count was specified, the cursor was moved as described
140 * for the first line joined, ignoring subsequent lines. If
141 * the join was a ':' command, the cursor was placed at the
142 * first non-blank character of the line unless the cursor was
143 * "attracted" to the end of line when the command was executed
144 * in which case it moved to the new end of line. There are
145 * probably several more special cases, but frankly, my dear,
146 * I don't give a damn. This implementation puts the cursor
147 * on the first inserted whitespace character, the first
148 * character of the joined line, or the last character of the
149 * line regardless. Note, if the cursor isn't on the joined
150 * line (possible with : commands), it is reset to the starting
154 sp
->cno
= (tbp
- bp
) - (1 + extra
);
157 sp
->cno
= (tbp
- bp
) - len
- (1 + extra
);
159 sp
->lno
= cmdp
->addr1
.lno
;
161 /* Delete the joined lines. */
162 for (from
= cmdp
->addr1
.lno
, to
= cmdp
->addr2
.lno
; to
> from
; --to
)
163 if (db_delete(sp
, to
))
166 /* If the original line changed, reset it. */
167 if (!first
&& db_set(sp
, from
, bp
, tbp
- bp
)) {
168 err
: FREE_SPACEW(sp
, bp
, blen
);
171 FREE_SPACEW(sp
, bp
, blen
);
173 sp
->rptlines
[L_JOINED
] += (cmdp
->addr2
.lno
- cmdp
->addr1
.lno
) + 1;