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.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
22 #include "../common/common.h"
24 enum which {RETAB, LEFT, RIGHT};
25 static int shift(SCR *, EXCMD *, enum which);
28 * ex_shiftl -- :<[<...]
31 * PUBLIC: int ex_shiftl(SCR *, EXCMD *);
34 ex_shiftl(SCR *sp, EXCMD *cmdp)
36 return (shift(sp, cmdp, LEFT));
40 * ex_shiftr -- :>[>...]
42 * PUBLIC: int ex_shiftr(SCR *, EXCMD *);
45 ex_shiftr(SCR *sp, EXCMD *cmdp)
47 return (shift(sp, cmdp, RIGHT));
51 * ex_retab -- Expand tabs (if enabled)
54 * PUBLIC: int ex_retab(SCR *, EXCMD *);
57 ex_retab(SCR *sp, EXCMD *cmdp)
59 return (shift(sp, cmdp, RETAB));
67 shift(SCR *sp, EXCMD *cmdp, enum which rl)
70 size_t blen, len, newcol, newidx, oldcol, oldidx, sw;
77 if (O_VAL(sp, O_SHIFTWIDTH) == 0) {
78 msgq(sp, M_INFO, "152|shiftwidth option set to 0");
82 /* Copy the lines being shifted into the unnamed buffer. */
83 if (cut(sp, NULL, &cmdp->addr1, &cmdp->addr2, CUT_LINEMODE))
87 * The historic version of vi permitted the user to string any number
88 * of '>' or '<' characters together, resulting in an indent of the
89 * appropriate levels. There's a special hack in ex_cmd() so that
90 * cmdp->argv[0] points to the string of '>' or '<' characters.
92 * Q: What's the difference between the people adding features
93 * to vi and the Girl Scouts?
94 * A: The Girl Scouts have mint cookies and adult supervision.
96 for (p = cmdp->argv[0]->bp, sw = 0; *p == '>' || *p == '<'; ++p)
97 sw += O_VAL(sp, O_SHIFTWIDTH);
99 GET_SPACE_RETW(sp, bp, blen, 256);
102 for (from = cmdp->addr1.lno, to = cmdp->addr2.lno; from <= to; ++from) {
103 if (db_get(sp, from, DBG_FATAL, &p, &len))
112 * Calculate the old indent amount and the number of
113 * characters it used.
115 for (oldidx = 0, oldcol = 0; oldidx < len; ++oldidx)
116 if (p[oldidx] == ' ')
118 else if (p[oldidx] == '\t')
119 oldcol += O_VAL(sp, O_TABSTOP) -
120 oldcol % O_VAL(sp, O_TABSTOP);
124 /* Calculate the new indent amount. */
127 else if (rl == RIGHT)
128 newcol = oldcol + sw;
130 newcol = oldcol < sw ? 0 : oldcol - sw;
131 if (newcol == oldcol) {
138 /* Get a buffer that will hold the new line. */
139 ADD_SPACE_RETW(sp, bp, blen, newcol + len);
142 * Build a new indent string and count the number of
143 * characters it uses.
147 if (!O_ISSET(sp, O_EXPANDTAB)) {
148 for (; newcol >= O_VAL(sp, O_TABSTOP); ++newidx) {
150 newcol -= O_VAL(sp, O_TABSTOP);
153 for (; newcol > 0; --newcol, ++newidx)
156 /* Add the original line. */
157 MEMCPY(tbp, p + oldidx, len - oldidx);
159 /* Set the replacement line. */
160 if (db_set(sp, from, bp, (tbp + (len - oldidx)) - bp)) {
161 err: FREE_SPACEW(sp, bp, blen);
167 * The shift command in historic vi had the usual bizarre
168 * collection of cursor semantics. If called from vi, the
169 * cursor was repositioned to the first non-blank character
170 * of the lowest numbered line shifted. If called from ex,
171 * the cursor was repositioned to the first non-blank of the
172 * highest numbered line shifted. Here, if the cursor isn't
173 * part of the set of lines that are moved, move it to the
174 * first non-blank of the last line shifted. (This makes
175 * ":3>>" in vi work reasonably.) If the cursor is part of
176 * the shifted lines, it doesn't get moved at all. This
177 * permits shifting of marked areas, i.e. ">'a." shifts the
178 * marked area twice, something that couldn't be done with
181 if (sp->lno == from) {
184 sp->cno += newidx - oldidx;
185 else if (sp->cno >= oldidx - newidx)
186 sp->cno -= oldidx - newidx;
192 (void)nonblank(sp, to, &sp->cno);
195 FREE_SPACEW(sp, bp, blen);
197 sp->rptlines[L_SHIFT] += cmdp->addr2.lno - cmdp->addr1.lno + 1;