add S_EXSILENT flag, for ex batch scripts
[nvi.git] / ex / ex_shift.c
blobf24a7b7a524477c127fc18000d985d57d00fb789
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.7 1993/11/04 12:37:26 bostic Exp $ (Berkeley) $Date: 1993/11/04 12:37:26 $";
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, but it
63 * may not be nul terminated.
65 * Q: What's the difference between the people adding features
66 * to vi and the Girl Scouts?
67 * A: The Girl Scouts have mint cookies and adult supervision.
69 for (p = cmdp->argv[0], sw = 0; *p && (*p == '>' || *p == '<'); ++p)
70 sw += O_VAL(sp, O_SHIFTWIDTH);
72 GET_SPACE(sp, bp, blen, 256);
74 curset = 0;
75 for (from = cmdp->addr1.lno, to = cmdp->addr2.lno; from <= to; ++from) {
76 if ((p = file_gline(sp, ep, from, &len)) == NULL)
77 goto err;
78 if (!len) {
79 if (sp->lno == from)
80 curset = 1;
81 continue;
85 * Calculate the old indent amount and the number of
86 * characters it used.
88 for (oldidx = 0, oldcol = 0; oldidx < len; ++oldidx)
89 if (p[oldidx] == ' ')
90 ++oldcol;
91 else if (p[oldidx] == '\t')
92 oldcol += O_VAL(sp, O_TABSTOP) -
93 oldcol % O_VAL(sp, O_TABSTOP);
94 else
95 break;
97 /* Calculate the new indent amount. */
98 if (rl == RIGHT)
99 newcol = oldcol + sw;
100 else {
101 newcol = oldcol < sw ? 0 : oldcol - sw;
102 if (newcol == oldcol) {
103 if (sp->lno == from)
104 curset = 1;
105 continue;
109 /* Get a buffer that will hold the new line. */
110 ADD_SPACE(sp, bp, blen, newcol + len);
113 * Build a new indent string and count the number of
114 * characters it uses.
116 for (tbp = bp, newidx = 0;
117 newcol >= O_VAL(sp, O_TABSTOP); ++newidx) {
118 *tbp++ = '\t';
119 newcol -= O_VAL(sp, O_TABSTOP);
121 for (; newcol > 0; --newcol, ++newidx)
122 *tbp++ = ' ';
124 /* Add the original line. */
125 memmove(tbp, p + oldidx, len - oldidx);
127 /* Set the replacement line. */
128 if (file_sline(sp, ep, from, bp, (tbp + (len - oldidx)) - bp)) {
129 err: FREE_SPACE(sp, bp, blen);
130 return (1);
134 * !!!
135 * The shift command in historic vi had the usual bizarre
136 * collection of cursor semantics. If called from vi, the
137 * cursor was repositioned to the first non-blank character
138 * of the lowest numbered line shifted. If called from ex,
139 * the cursor was repositioned to the first non-blank of the
140 * highest numbered line shifted. Here, if the cursor isn't
141 * part of the set of lines that are moved, move it to the
142 * first non-blank of the last line shifted. (This makes
143 * ":3>>" in vi work reasonably.) If the cursor is part of
144 * the shifted lines, it doesn't get moved at all. This
145 * permits shifting of marked areas, i.e. ">'a." shifts the
146 * marked area twice, something that couldn't be done with
147 * historic vi.
149 if (sp->lno == from) {
150 curset = 1;
151 if (newidx > oldidx)
152 sp->cno += newidx - oldidx;
153 else if (sp->cno >= oldidx - newidx)
154 sp->cno -= oldidx - newidx;
157 if (!curset) {
158 sp->lno = to;
159 sp->cno = 0;
160 (void)nonblank(sp, ep, to, &sp->cno);
163 FREE_SPACE(sp, bp, blen);
165 sp->rptlines[rl == RIGHT ? L_RSHIFT : L_LSHIFT] +=
166 cmdp->addr2.lno - cmdp->addr1.lno + 1;
168 F_SET(sp, S_AUTOPRINT);
169 return (0);