I've added a 'description' field.
[nvi.git] / ex / ex_shift.c
blob124d07e6489ce6e18fbc320d688c8137b061ab44
1 /*-
2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: ex_shift.c,v 10.11 1996/09/15 15:59:45 bostic Exp $ (Berkeley) $Date: 1996/09/15 15:59:45 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include "../common/common.h"
27 enum which {LEFT, RIGHT};
28 static int shift __P((SCR *, EXCMD *, enum which));
31 * ex_shiftl -- :<[<...]
34 * PUBLIC: int ex_shiftl __P((SCR *, EXCMD *));
36 int
37 ex_shiftl(sp, cmdp)
38 SCR *sp;
39 EXCMD *cmdp;
41 return (shift(sp, cmdp, LEFT));
45 * ex_shiftr -- :>[>...]
47 * PUBLIC: int ex_shiftr __P((SCR *, EXCMD *));
49 int
50 ex_shiftr(sp, cmdp)
51 SCR *sp;
52 EXCMD *cmdp;
54 return (shift(sp, cmdp, RIGHT));
58 * shift --
59 * Ex shift support.
61 static int
62 shift(sp, cmdp, rl)
63 SCR *sp;
64 EXCMD *cmdp;
65 enum which rl;
67 recno_t from, to;
68 size_t blen, len, newcol, newidx, oldcol, oldidx, sw;
69 int curset;
70 char *p, *bp, *tbp;
72 NEEDFILE(sp, cmdp);
74 if (O_VAL(sp, O_SHIFTWIDTH) == 0) {
75 msgq(sp, M_INFO, "152|shiftwidth option set to 0");
76 return (0);
79 /* Copy the lines being shifted into the unnamed buffer. */
80 if (cut(sp, NULL, &cmdp->addr1, &cmdp->addr2, CUT_LINEMODE))
81 return (1);
84 * The historic version of vi permitted the user to string any number
85 * of '>' or '<' characters together, resulting in an indent of the
86 * appropriate levels. There's a special hack in ex_cmd() so that
87 * cmdp->argv[0] points to the string of '>' or '<' characters.
89 * Q: What's the difference between the people adding features
90 * to vi and the Girl Scouts?
91 * A: The Girl Scouts have mint cookies and adult supervision.
93 for (p = cmdp->argv[0]->bp, sw = 0; *p == '>' || *p == '<'; ++p)
94 sw += O_VAL(sp, O_SHIFTWIDTH);
96 GET_SPACE_RET(sp, bp, blen, 256);
98 curset = 0;
99 for (from = cmdp->addr1.lno, to = cmdp->addr2.lno; from <= to; ++from) {
100 if (db_get(sp, from, DBG_FATAL, &p, &len))
101 goto err;
102 if (!len) {
103 if (sp->lno == from)
104 curset = 1;
105 continue;
109 * Calculate the old indent amount and the number of
110 * characters it used.
112 for (oldidx = 0, oldcol = 0; oldidx < len; ++oldidx)
113 if (p[oldidx] == ' ')
114 ++oldcol;
115 else if (p[oldidx] == '\t')
116 oldcol += O_VAL(sp, O_TABSTOP) -
117 oldcol % O_VAL(sp, O_TABSTOP);
118 else
119 break;
121 /* Calculate the new indent amount. */
122 if (rl == RIGHT)
123 newcol = oldcol + sw;
124 else {
125 newcol = oldcol < sw ? 0 : oldcol - sw;
126 if (newcol == oldcol) {
127 if (sp->lno == from)
128 curset = 1;
129 continue;
133 /* Get a buffer that will hold the new line. */
134 ADD_SPACE_RET(sp, bp, blen, newcol + len);
137 * Build a new indent string and count the number of
138 * characters it uses.
140 for (tbp = bp, newidx = 0;
141 newcol >= O_VAL(sp, O_TABSTOP); ++newidx) {
142 *tbp++ = '\t';
143 newcol -= O_VAL(sp, O_TABSTOP);
145 for (; newcol > 0; --newcol, ++newidx)
146 *tbp++ = ' ';
148 /* Add the original line. */
149 memcpy(tbp, p + oldidx, len - oldidx);
151 /* Set the replacement line. */
152 if (db_set(sp, from, bp, (tbp + (len - oldidx)) - bp)) {
153 err: FREE_SPACE(sp, bp, blen);
154 return (1);
158 * !!!
159 * The shift command in historic vi had the usual bizarre
160 * collection of cursor semantics. If called from vi, the
161 * cursor was repositioned to the first non-blank character
162 * of the lowest numbered line shifted. If called from ex,
163 * the cursor was repositioned to the first non-blank of the
164 * highest numbered line shifted. Here, if the cursor isn't
165 * part of the set of lines that are moved, move it to the
166 * first non-blank of the last line shifted. (This makes
167 * ":3>>" in vi work reasonably.) If the cursor is part of
168 * the shifted lines, it doesn't get moved at all. This
169 * permits shifting of marked areas, i.e. ">'a." shifts the
170 * marked area twice, something that couldn't be done with
171 * historic vi.
173 if (sp->lno == from) {
174 curset = 1;
175 if (newidx > oldidx)
176 sp->cno += newidx - oldidx;
177 else if (sp->cno >= oldidx - newidx)
178 sp->cno -= oldidx - newidx;
181 if (!curset) {
182 sp->lno = to;
183 sp->cno = 0;
184 (void)nonblank(sp, to, &sp->cno);
187 FREE_SPACE(sp, bp, blen);
189 sp->rptlines[L_SHIFT] += cmdp->addr2.lno - cmdp->addr1.lno + 1;
190 return (0);