This commit was generated by cvs2svn to track changes on a CVS vendor
[unix-history] / usr.bin / vi / ex / ex_shift.c
CommitLineData
395c4480
AM
1/*-
2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char sccsid[] = "@(#)ex_shift.c 8.14 (Berkeley) 3/14/94";
36#endif /* not lint */
37
38#include <sys/types.h>
39#include <queue.h>
40#include <sys/time.h>
41
42#include <bitstring.h>
43#include <limits.h>
44#include <signal.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <termios.h>
49
50#include <db.h>
51#include <regex.h>
52
53#include "vi.h"
54#include "excmd.h"
55
56enum which {LEFT, RIGHT};
57static int shift __P((SCR *, EXF *, EXCMDARG *, enum which));
58
59int
60ex_shiftl(sp, ep, cmdp)
61 SCR *sp;
62 EXF *ep;
63 EXCMDARG *cmdp;
64{
65 return (shift(sp, ep, cmdp, LEFT));
66}
67
68int
69ex_shiftr(sp, ep, cmdp)
70 SCR *sp;
71 EXF *ep;
72 EXCMDARG *cmdp;
73{
74 return (shift(sp, ep, cmdp, RIGHT));
75}
76
77static int
78shift(sp, ep, cmdp, rl)
79 SCR *sp;
80 EXF *ep;
81 EXCMDARG *cmdp;
82 enum which rl;
83{
84 recno_t from, to;
85 size_t blen, len, newcol, newidx, oldcol, oldidx, sw;
86 int curset;
87 char *p, *bp, *tbp;
88
89 if (O_VAL(sp, O_SHIFTWIDTH) == 0) {
90 msgq(sp, M_INFO, "shiftwidth option set to 0");
91 return (0);
92 }
93
94 /*
95 * The historic version of vi permitted the user to string any number
96 * of '>' or '<' characters together, resulting in an indent of the
97 * appropriate levels. There's a special hack in ex_cmd() so that
98 * cmdp->argv[0] points to the string of '>' or '<' characters.
99 *
100 * Q: What's the difference between the people adding features
101 * to vi and the Girl Scouts?
102 * A: The Girl Scouts have mint cookies and adult supervision.
103 */
104 for (p = cmdp->argv[0]->bp, sw = 0; *p == '>' || *p == '<'; ++p)
105 sw += O_VAL(sp, O_SHIFTWIDTH);
106
107 GET_SPACE_RET(sp, bp, blen, 256);
108
109 curset = 0;
110 for (from = cmdp->addr1.lno, to = cmdp->addr2.lno; from <= to; ++from) {
111 if ((p = file_gline(sp, ep, from, &len)) == NULL)
112 goto err;
113 if (!len) {
114 if (sp->lno == from)
115 curset = 1;
116 continue;
117 }
118
119 /*
120 * Calculate the old indent amount and the number of
121 * characters it used.
122 */
123 for (oldidx = 0, oldcol = 0; oldidx < len; ++oldidx)
124 if (p[oldidx] == ' ')
125 ++oldcol;
126 else if (p[oldidx] == '\t')
127 oldcol += O_VAL(sp, O_TABSTOP) -
128 oldcol % O_VAL(sp, O_TABSTOP);
129 else
130 break;
131
132 /* Calculate the new indent amount. */
133 if (rl == RIGHT)
134 newcol = oldcol + sw;
135 else {
136 newcol = oldcol < sw ? 0 : oldcol - sw;
137 if (newcol == oldcol) {
138 if (sp->lno == from)
139 curset = 1;
140 continue;
141 }
142 }
143
144 /* Get a buffer that will hold the new line. */
145 ADD_SPACE_RET(sp, bp, blen, newcol + len);
146
147 /*
148 * Build a new indent string and count the number of
149 * characters it uses.
150 */
151 for (tbp = bp, newidx = 0;
152 newcol >= O_VAL(sp, O_TABSTOP); ++newidx) {
153 *tbp++ = '\t';
154 newcol -= O_VAL(sp, O_TABSTOP);
155 }
156 for (; newcol > 0; --newcol, ++newidx)
157 *tbp++ = ' ';
158
159 /* Add the original line. */
160 memmove(tbp, p + oldidx, len - oldidx);
161
162 /* Set the replacement line. */
163 if (file_sline(sp, ep, from, bp, (tbp + (len - oldidx)) - bp)) {
164err: FREE_SPACE(sp, bp, blen);
165 return (1);
166 }
167
168 /*
169 * !!!
170 * The shift command in historic vi had the usual bizarre
171 * collection of cursor semantics. If called from vi, the
172 * cursor was repositioned to the first non-blank character
173 * of the lowest numbered line shifted. If called from ex,
174 * the cursor was repositioned to the first non-blank of the
175 * highest numbered line shifted. Here, if the cursor isn't
176 * part of the set of lines that are moved, move it to the
177 * first non-blank of the last line shifted. (This makes
178 * ":3>>" in vi work reasonably.) If the cursor is part of
179 * the shifted lines, it doesn't get moved at all. This
180 * permits shifting of marked areas, i.e. ">'a." shifts the
181 * marked area twice, something that couldn't be done with
182 * historic vi.
183 */
184 if (sp->lno == from) {
185 curset = 1;
186 if (newidx > oldidx)
187 sp->cno += newidx - oldidx;
188 else if (sp->cno >= oldidx - newidx)
189 sp->cno -= oldidx - newidx;
190 }
191 }
192 if (!curset) {
193 sp->lno = to;
194 sp->cno = 0;
195 (void)nonblank(sp, ep, to, &sp->cno);
196 }
197
198 FREE_SPACE(sp, bp, blen);
199
200 sp->rptlines[rl == RIGHT ? L_RSHIFT : L_LSHIFT] +=
201 cmdp->addr2.lno - cmdp->addr1.lno + 1;
202 return (0);
203}