Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / ex / ex_join.c
blob5a773a164284970b9fc96e07b7111cc8182a5d64
1 /* $NetBSD: ex_join.c,v 1.1.1.2 2008/05/18 14:31:16 aymeric Exp $ */
3 /*-
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 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";
16 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/queue.h>
21 #include <bitstring.h>
22 #include <ctype.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include "../common/common.h"
31 * ex_join -- :[line [,line]] j[oin][!] [count] [flags]
32 * Join lines.
34 * PUBLIC: int ex_join __P((SCR *, EXCMD *));
36 int
37 ex_join(SCR *sp, EXCMD *cmdp)
39 db_recno_t from, to;
40 size_t blen, clen, len, tlen;
41 int echar = 0, extra, first;
42 CHAR_T *bp, *tbp = NULL;
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_RETW(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 (F_ISSET(cmdp, E_ADDR_DEF) || cmdp->addrcnt == 1)
63 ++cmdp->addr2.lno;
65 clen = tlen = 0;
66 for (first = 1,
67 from = cmdp->addr1.lno, to = cmdp->addr2.lno; from <= to; ++from) {
69 * Get next line. Historic versions of vi allowed "10J" while
70 * less than 10 lines from the end-of-file, so we do too.
72 if (db_get(sp, from, 0, &p, &len)) {
73 cmdp->addr2.lno = from - 1;
74 break;
77 /* Empty lines just go away. */
78 if (len == 0)
79 continue;
82 * Get more space if necessary. Note, tlen isn't the length
83 * of the new line, it's roughly the amount of space needed.
84 * tbp - bp is the length of the new line.
86 tlen += len + 2;
87 ADD_SPACE_RETW(sp, bp, blen, tlen);
88 tbp = bp + clen;
91 * Historic practice:
93 * If force specified, join without modification.
94 * If the current line ends with whitespace, strip leading
95 * whitespace from the joined line.
96 * If the next line starts with a ), do nothing.
97 * If the current line ends with ., insert two spaces.
98 * Else, insert one space.
100 * One change -- add ? and ! to the list of characters for
101 * which we insert two spaces. I expect that POSIX 1003.2
102 * will require this as well.
104 * Echar is the last character in the last line joined.
106 extra = 0;
107 if (!first && !FL_ISSET(cmdp->iflags, E_C_FORCE)) {
108 if (isblank(echar))
109 for (; len && isblank(*p); --len, ++p);
110 else if (p[0] != ')') {
111 if (strchr(".?!", echar)) {
112 *tbp++ = ' ';
113 ++clen;
114 extra = 1;
116 *tbp++ = ' ';
117 ++clen;
118 for (; len && isblank(*p); --len, ++p);
122 if (len != 0) {
123 MEMCPYW(tbp, p, len);
124 tbp += len;
125 clen += len;
126 echar = p[len - 1];
127 } else
128 echar = ' ';
131 * Historic practice for vi was to put the cursor at the first
132 * inserted whitespace character, if there was one, or the
133 * first character of the joined line, if there wasn't, or the
134 * last character of the line if joined to an empty line. If
135 * a count was specified, the cursor was moved as described
136 * for the first line joined, ignoring subsequent lines. If
137 * the join was a ':' command, the cursor was placed at the
138 * first non-blank character of the line unless the cursor was
139 * "attracted" to the end of line when the command was executed
140 * in which case it moved to the new end of line. There are
141 * probably several more special cases, but frankly, my dear,
142 * I don't give a damn. This implementation puts the cursor
143 * on the first inserted whitespace character, the first
144 * character of the joined line, or the last character of the
145 * line regardless. Note, if the cursor isn't on the joined
146 * line (possible with : commands), it is reset to the starting
147 * line.
149 if (first) {
150 sp->cno = (tbp - bp) - (1 + extra);
151 first = 0;
152 } else
153 sp->cno = (tbp - bp) - len - (1 + extra);
155 sp->lno = cmdp->addr1.lno;
157 /* Delete the joined lines. */
158 for (from = cmdp->addr1.lno, to = cmdp->addr2.lno; to > from; --to)
159 if (db_delete(sp, to))
160 goto err;
162 /* If the original line changed, reset it. */
163 if (!first && db_set(sp, from, bp, tbp - bp)) {
164 err: FREE_SPACEW(sp, bp, blen);
165 return (1);
167 FREE_SPACEW(sp, bp, blen);
169 sp->rptlines[L_JOINED] += (cmdp->addr2.lno - cmdp->addr1.lno) + 1;
170 return (0);