2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
9 static char sccsid
[] = "$Id: ex_shift.c,v 8.12 1993/12/29 09:50:54 bostic Exp $ (Berkeley) $Date: 1993/12/29 09:50:54 $";
12 #include <sys/types.h>
20 enum which
{LEFT
, RIGHT
};
21 static int shift
__P((SCR
*, EXF
*, EXCMDARG
*, enum which
));
24 ex_shiftl(sp
, ep
, cmdp
)
29 return (shift(sp
, ep
, cmdp
, LEFT
));
33 ex_shiftr(sp
, ep
, cmdp
)
38 return (shift(sp
, ep
, cmdp
, RIGHT
));
42 shift(sp
, ep
, cmdp
, rl
)
49 size_t blen
, len
, newcol
, newidx
, oldcol
, oldidx
, sw
;
53 if (O_VAL(sp
, O_SHIFTWIDTH
) == 0) {
54 msgq(sp
, M_INFO
, "shiftwidth option set to 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);
74 for (from
= cmdp
->addr1
.lno
, to
= cmdp
->addr2
.lno
; from
<= to
; ++from
) {
75 if ((p
= file_gline(sp
, ep
, from
, &len
)) == NULL
)
84 * Calculate the old indent amount and the number of
87 for (oldidx
= 0, oldcol
= 0; oldidx
< len
; ++oldidx
)
90 else if (p
[oldidx
] == '\t')
91 oldcol
+= O_VAL(sp
, O_TABSTOP
) -
92 oldcol
% O_VAL(sp
, O_TABSTOP
);
96 /* Calculate the new indent amount. */
100 newcol
= oldcol
< sw
? 0 : oldcol
- sw
;
101 if (newcol
== oldcol
) {
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
) {
118 newcol
-= O_VAL(sp
, O_TABSTOP
);
120 for (; newcol
> 0; --newcol
, ++newidx
)
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
);
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
148 if (sp
->lno
== from
) {
151 sp
->cno
+= newidx
- oldidx
;
152 else if (sp
->cno
>= oldidx
- newidx
)
153 sp
->cno
-= oldidx
- newidx
;
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;