lint
[nvi.git] / ex / ex_shift.c
blobe0c5594c93b7f410da56b29f11dcc0da1458cc9d
1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
6 */
8 #ifndef lint
9 static char sccsid[] = "$Id: ex_shift.c,v 8.11 1993/12/09 19:42:45 bostic Exp $ (Berkeley) $Date: 1993/12/09 19:42:45 $";
10 #endif /* not lint */
12 #include <sys/types.h>
14 #include <stdlib.h>
15 #include <string.h>
17 #include "vi.h"
18 #include "excmd.h"
20 enum which {LEFT, RIGHT};
21 static int shift __P((SCR *, EXF *, EXCMDARG *, enum which));
23 int
24 ex_shiftl(sp, ep, cmdp)
25 SCR *sp;
26 EXF *ep;
27 EXCMDARG *cmdp;
29 return (shift(sp, ep, cmdp, LEFT));
32 int
33 ex_shiftr(sp, ep, cmdp)
34 SCR *sp;
35 EXF *ep;
36 EXCMDARG *cmdp;
38 return (shift(sp, ep, cmdp, RIGHT));
41 static int
42 shift(sp, ep, cmdp, rl)
43 SCR *sp;
44 EXF *ep;
45 EXCMDARG *cmdp;
46 enum which rl;
48 recno_t from, to;
49 size_t blen, len, newcol, newidx, oldcol, oldidx, sw;
50 int curset;
51 char *p, *bp, *tbp;
53 if (O_VAL(sp, O_SHIFTWIDTH) == 0) {
54 msgq(sp, M_INFO, "shiftwidth option set to 0");
55 return (0);
59 * The historic version of vi permitted the user to string any number
60 * of '>' or '<' characters together, resulting in an indent of the
61 * appropriate levels. There's a special hack in ex_cmd() so that
62 * cmdp->argv[0] points to the string of '>' or '<' characters.
64 * Q: What's the difference between the people adding features
65 * to vi and the Girl Scouts?
66 * A: The Girl Scouts have mint cookies and adult supervision.
68 for (p = cmdp->argv[0]->bp, sw = 0; *p == '>' || *p == '<'; ++p)
69 sw += O_VAL(sp, O_SHIFTWIDTH);
71 GET_SPACE_RET(sp, bp, blen, 256);
73 curset = 0;
74 for (from = cmdp->addr1.lno, to = cmdp->addr2.lno; from <= to; ++from) {
75 if ((p = file_gline(sp, ep, from, &len)) == NULL)
76 goto err;
77 if (!len) {
78 if (sp->lno == from)
79 curset = 1;
80 continue;
84 * Calculate the old indent amount and the number of
85 * characters it used.
87 for (oldidx = 0, oldcol = 0; oldidx < len; ++oldidx)
88 if (p[oldidx] == ' ')
89 ++oldcol;
90 else if (p[oldidx] == '\t')
91 oldcol += O_VAL(sp, O_TABSTOP) -
92 oldcol % O_VAL(sp, O_TABSTOP);
93 else
94 break;
96 /* Calculate the new indent amount. */
97 if (rl == RIGHT)
98 newcol = oldcol + sw;
99 else {
100 newcol = oldcol < sw ? 0 : oldcol - sw;
101 if (newcol == oldcol) {
102 if (sp->lno == from)
103 curset = 1;
104 continue;
108 /* Get a buffer that will hold the new line. */
109 ADD_SPACE_RET(sp, bp, blen, newcol + len);
112 * Build a new indent string and count the number of
113 * characters it uses.
115 for (tbp = bp, newidx = 0;
116 newcol >= O_VAL(sp, O_TABSTOP); ++newidx) {
117 *tbp++ = '\t';
118 newcol -= O_VAL(sp, O_TABSTOP);
120 for (; newcol > 0; --newcol, ++newidx)
121 *tbp++ = ' ';
123 /* Add the original line. */
124 memmove(tbp, p + oldidx, len - oldidx);
126 /* Set the replacement line. */
127 if (file_sline(sp, ep, from, bp, (tbp + (len - oldidx)) - bp)) {
128 err: FREE_SPACE(sp, bp, blen);
129 return (1);
133 * !!!
134 * The shift command in historic vi had the usual bizarre
135 * collection of cursor semantics. If called from vi, the
136 * cursor was repositioned to the first non-blank character
137 * of the lowest numbered line shifted. If called from ex,
138 * the cursor was repositioned to the first non-blank of the
139 * highest numbered line shifted. Here, if the cursor isn't
140 * part of the set of lines that are moved, move it to the
141 * first non-blank of the last line shifted. (This makes
142 * ":3>>" in vi work reasonably.) If the cursor is part of
143 * the shifted lines, it doesn't get moved at all. This
144 * permits shifting of marked areas, i.e. ">'a." shifts the
145 * marked area twice, something that couldn't be done with
146 * historic vi.
148 if (sp->lno == from) {
149 curset = 1;
150 if (newidx > oldidx)
151 sp->cno += newidx - oldidx;
152 else if (sp->cno >= oldidx - newidx)
153 sp->cno -= oldidx - newidx;
156 if (!curset) {
157 sp->lno = to;
158 sp->cno = 0;
159 (void)nonblank(sp, ep, to, &sp->cno);
162 FREE_SPACE(sp, bp, blen);
164 sp->rptlines[rl == RIGHT ? L_RSHIFT : L_LSHIFT] +=
165 cmdp->addr2.lno - cmdp->addr1.lno + 1;
167 F_SET(sp, S_AUTOPRINT);
168 return (0);