1 /* $NetBSD: ex_shift.c,v 1.2 2008/12/12 22:55:56 lukem Exp $ */
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
15 static const char sccsid
[] = "Id: ex_shift.c,v 10.17 2001/06/25 15:19:20 skimo Exp (Berkeley) Date: 2001/06/25 15:19:20";
18 #include <sys/types.h>
19 #include <sys/queue.h>
21 #include <bitstring.h>
27 #include "../common/common.h"
29 enum which
{LEFT
, RIGHT
};
30 static int shift
__P((SCR
*, EXCMD
*, enum which
));
33 * ex_shiftl -- :<[<...]
36 * PUBLIC: int ex_shiftl __P((SCR *, EXCMD *));
39 ex_shiftl(SCR
*sp
, EXCMD
*cmdp
)
41 return (shift(sp
, cmdp
, LEFT
));
45 * ex_shiftr -- :>[>...]
47 * PUBLIC: int ex_shiftr __P((SCR *, EXCMD *));
50 ex_shiftr(SCR
*sp
, EXCMD
*cmdp
)
52 return (shift(sp
, cmdp
, RIGHT
));
60 shift(SCR
*sp
, EXCMD
*cmdp
, enum which rl
)
63 size_t blen
, len
, newcol
, newidx
, oldcol
, oldidx
, sw
;
70 if (O_VAL(sp
, O_SHIFTWIDTH
) == 0) {
71 msgq(sp
, M_INFO
, "152|shiftwidth option set to 0");
75 /* Copy the lines being shifted into the unnamed buffer. */
76 if (cut(sp
, NULL
, &cmdp
->addr1
, &cmdp
->addr2
, CUT_LINEMODE
))
80 * The historic version of vi permitted the user to string any number
81 * of '>' or '<' characters together, resulting in an indent of the
82 * appropriate levels. There's a special hack in ex_cmd() so that
83 * cmdp->argv[0] points to the string of '>' or '<' characters.
85 * Q: What's the difference between the people adding features
86 * to vi and the Girl Scouts?
87 * A: The Girl Scouts have mint cookies and adult supervision.
89 for (p
= cmdp
->argv
[0]->bp
, sw
= 0; *p
== '>' || *p
== '<'; ++p
)
90 sw
+= O_VAL(sp
, O_SHIFTWIDTH
);
92 GET_SPACE_RETW(sp
, bp
, blen
, 256);
95 for (from
= cmdp
->addr1
.lno
, to
= cmdp
->addr2
.lno
; from
<= to
; ++from
) {
96 if (db_get(sp
, from
, DBG_FATAL
, &p
, &len
))
105 * Calculate the old indent amount and the number of
106 * characters it used.
108 for (oldidx
= 0, oldcol
= 0; oldidx
< len
; ++oldidx
)
109 if (p
[oldidx
] == ' ')
111 else if (p
[oldidx
] == '\t')
112 oldcol
+= O_VAL(sp
, O_TABSTOP
) -
113 oldcol
% O_VAL(sp
, O_TABSTOP
);
117 /* Calculate the new indent amount. */
119 newcol
= oldcol
+ sw
;
121 newcol
= oldcol
< sw
? 0 : oldcol
- sw
;
122 if (newcol
== oldcol
) {
129 /* Get a buffer that will hold the new line. */
130 ADD_SPACE_RETW(sp
, bp
, blen
, newcol
+ len
);
133 * Build a new indent string and count the number of
134 * characters it uses.
138 if (!O_ISSET(sp
, O_EXPANDTAB
)) {
139 for (; newcol
>= O_VAL(sp
, O_TABSTOP
); ++newidx
) {
141 newcol
-= O_VAL(sp
, O_TABSTOP
);
144 for (; newcol
> 0; --newcol
, ++newidx
)
147 /* Add the original line. */
148 MEMCPYW(tbp
, p
+ oldidx
, len
- oldidx
);
150 /* Set the replacement line. */
151 if (db_set(sp
, from
, bp
, (tbp
+ (len
- oldidx
)) - bp
)) {
152 err
: FREE_SPACEW(sp
, bp
, blen
);
158 * The shift command in historic vi had the usual bizarre
159 * collection of cursor semantics. If called from vi, the
160 * cursor was repositioned to the first non-blank character
161 * of the lowest numbered line shifted. If called from ex,
162 * the cursor was repositioned to the first non-blank of the
163 * highest numbered line shifted. Here, if the cursor isn't
164 * part of the set of lines that are moved, move it to the
165 * first non-blank of the last line shifted. (This makes
166 * ":3>>" in vi work reasonably.) If the cursor is part of
167 * the shifted lines, it doesn't get moved at all. This
168 * permits shifting of marked areas, i.e. ">'a." shifts the
169 * marked area twice, something that couldn't be done with
172 if (sp
->lno
== from
) {
175 sp
->cno
+= newidx
- oldidx
;
176 else if (sp
->cno
>= oldidx
- newidx
)
177 sp
->cno
-= oldidx
- newidx
;
183 (void)nonblank(sp
, to
, &sp
->cno
);
186 FREE_SPACEW(sp
, bp
, blen
);
188 sp
->rptlines
[L_SHIFT
] += cmdp
->addr2
.lno
- cmdp
->addr1
.lno
+ 1;