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.8 1993/11/30 16:30:50 bostic Exp $ (Berkeley) $Date: 1993/11/30 16:30:50 $";
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, 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
)
70 sw
+= O_VAL(sp
, O_SHIFTWIDTH
);
72 GET_SPACE(sp
, bp
, blen
, 256);
75 for (from
= cmdp
->addr1
.lno
, to
= cmdp
->addr2
.lno
; from
<= to
; ++from
) {
76 if ((p
= file_gline(sp
, ep
, from
, &len
)) == NULL
)
85 * Calculate the old indent amount and the number of
88 for (oldidx
= 0, oldcol
= 0; oldidx
< len
; ++oldidx
)
91 else if (p
[oldidx
] == '\t')
92 oldcol
+= O_VAL(sp
, O_TABSTOP
) -
93 oldcol
% O_VAL(sp
, O_TABSTOP
);
97 /* Calculate the new indent amount. */
101 newcol
= oldcol
< sw
? 0 : oldcol
- sw
;
102 if (newcol
== oldcol
) {
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
) {
119 newcol
-= O_VAL(sp
, O_TABSTOP
);
121 for (; newcol
> 0; --newcol
, ++newidx
)
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
);
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
149 if (sp
->lno
== from
) {
152 sp
->cno
+= newidx
- oldidx
;
153 else if (sp
->cno
>= oldidx
- newidx
)
154 sp
->cno
-= oldidx
- newidx
;
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
);