only clean out gs when last window goes
[nvi.git] / ex / ex_shift.c
blob5b7d75885daed5f5f758f7a9325a706395c5e1a5
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.13 2000/06/27 17:19:06 skimo Exp $ (Berkeley) $Date: 2000/06/27 17:19:06 $";
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 db_recno_t from, to;
68 size_t blen, len, newcol, newidx, oldcol, oldidx, sw;
69 int curset;
70 CHAR_T *p;
71 char *bp, *tbp;
73 NEEDFILE(sp, cmdp);
75 if (O_VAL(sp, O_SHIFTWIDTH) == 0) {
76 msgq(sp, M_INFO, "152|shiftwidth option set to 0");
77 return (0);
80 /* Copy the lines being shifted into the unnamed buffer. */
81 if (cut(sp, NULL, &cmdp->addr1, &cmdp->addr2, CUT_LINEMODE))
82 return (1);
85 * The historic version of vi permitted the user to string any number
86 * of '>' or '<' characters together, resulting in an indent of the
87 * appropriate levels. There's a special hack in ex_cmd() so that
88 * cmdp->argv[0] points to the string of '>' or '<' characters.
90 * Q: What's the difference between the people adding features
91 * to vi and the Girl Scouts?
92 * A: The Girl Scouts have mint cookies and adult supervision.
94 for (p = cmdp->argv[0]->bp, sw = 0; *p == '>' || *p == '<'; ++p)
95 sw += O_VAL(sp, O_SHIFTWIDTH);
97 GET_SPACE_RET(sp, bp, blen, 256);
99 curset = 0;
100 for (from = cmdp->addr1.lno, to = cmdp->addr2.lno; from <= to; ++from) {
101 if (db_get(sp, from, DBG_FATAL, &p, &len))
102 goto err;
103 if (!len) {
104 if (sp->lno == from)
105 curset = 1;
106 continue;
110 * Calculate the old indent amount and the number of
111 * characters it used.
113 for (oldidx = 0, oldcol = 0; oldidx < len; ++oldidx)
114 if (p[oldidx] == ' ')
115 ++oldcol;
116 else if (p[oldidx] == '\t')
117 oldcol += O_VAL(sp, O_TABSTOP) -
118 oldcol % O_VAL(sp, O_TABSTOP);
119 else
120 break;
122 /* Calculate the new indent amount. */
123 if (rl == RIGHT)
124 newcol = oldcol + sw;
125 else {
126 newcol = oldcol < sw ? 0 : oldcol - sw;
127 if (newcol == oldcol) {
128 if (sp->lno == from)
129 curset = 1;
130 continue;
134 /* Get a buffer that will hold the new line. */
135 ADD_SPACE_RET(sp, bp, blen, newcol + len);
138 * Build a new indent string and count the number of
139 * characters it uses.
141 for (tbp = bp, newidx = 0;
142 newcol >= O_VAL(sp, O_TABSTOP); ++newidx) {
143 *tbp++ = '\t';
144 newcol -= O_VAL(sp, O_TABSTOP);
146 for (; newcol > 0; --newcol, ++newidx)
147 *tbp++ = ' ';
149 /* Add the original line. */
150 memcpy(tbp, p + oldidx, len - oldidx);
152 /* Set the replacement line. */
153 if (db_set(sp, from, bp, (tbp + (len - oldidx)) - bp)) {
154 err: FREE_SPACE(sp, bp, blen);
155 return (1);
159 * !!!
160 * The shift command in historic vi had the usual bizarre
161 * collection of cursor semantics. If called from vi, the
162 * cursor was repositioned to the first non-blank character
163 * of the lowest numbered line shifted. If called from ex,
164 * the cursor was repositioned to the first non-blank of the
165 * highest numbered line shifted. Here, if the cursor isn't
166 * part of the set of lines that are moved, move it to the
167 * first non-blank of the last line shifted. (This makes
168 * ":3>>" in vi work reasonably.) If the cursor is part of
169 * the shifted lines, it doesn't get moved at all. This
170 * permits shifting of marked areas, i.e. ">'a." shifts the
171 * marked area twice, something that couldn't be done with
172 * historic vi.
174 if (sp->lno == from) {
175 curset = 1;
176 if (newidx > oldidx)
177 sp->cno += newidx - oldidx;
178 else if (sp->cno >= oldidx - newidx)
179 sp->cno -= oldidx - newidx;
182 if (!curset) {
183 sp->lno = to;
184 sp->cno = 0;
185 (void)nonblank(sp, to, &sp->cno);
188 FREE_SPACE(sp, bp, blen);
190 sp->rptlines[L_SHIFT] += cmdp->addr2.lno - cmdp->addr1.lno + 1;
191 return (0);