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.
6 *
7 * See the LICENSE file for redistribution information.
8 */
9
10 #include "config.h"
11
12 #include <sys/types.h>
13 #include <sys/queue.h>
14 #include <sys/time.h>
15
16 #include <bitstring.h>
17 #include <limits.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include "../common/common.h"
23
24 enum which {RETAB, LEFT, RIGHT};
25 static int shift(SCR *, EXCMD *, enum which);
26
27 /*
28 * ex_shiftl -- :<[<...]
29 *
30 *
31 * PUBLIC: int ex_shiftl(SCR *, EXCMD *);
32 */
33 int
ex_shiftl(SCR * sp,EXCMD * cmdp)34 ex_shiftl(SCR *sp, EXCMD *cmdp)
35 {
36 return (shift(sp, cmdp, LEFT));
37 }
38
39 /*
40 * ex_shiftr -- :>[>...]
41 *
42 * PUBLIC: int ex_shiftr(SCR *, EXCMD *);
43 */
44 int
ex_shiftr(SCR * sp,EXCMD * cmdp)45 ex_shiftr(SCR *sp, EXCMD *cmdp)
46 {
47 return (shift(sp, cmdp, RIGHT));
48 }
49
50 /*
51 * ex_retab -- Expand tabs (if enabled)
52 *
53 *
54 * PUBLIC: int ex_retab(SCR *, EXCMD *);
55 */
56 int
ex_retab(SCR * sp,EXCMD * cmdp)57 ex_retab(SCR *sp, EXCMD *cmdp)
58 {
59 return (shift(sp, cmdp, RETAB));
60 }
61
62 /*
63 * shift --
64 * Ex shift support.
65 */
66 static int
shift(SCR * sp,EXCMD * cmdp,enum which rl)67 shift(SCR *sp, EXCMD *cmdp, enum which rl)
68 {
69 recno_t from, to;
70 size_t blen, len, newcol, newidx, oldcol, oldidx, sw;
71 int curset;
72 CHAR_T *p;
73 CHAR_T *bp, *tbp;
74
75 NEEDFILE(sp, cmdp);
76
77 if (O_VAL(sp, O_SHIFTWIDTH) == 0) {
78 msgq(sp, M_INFO, "152|shiftwidth option set to 0");
79 return (0);
80 }
81
82 /*
83 * When not doing re-expand tabs, copy the lines being shifted into
84 * the unnamed buffer.
85 */
86 if (rl != RETAB &&
87 cut(sp, NULL, &cmdp->addr1, &cmdp->addr2, CUT_LINEMODE))
88 return (1);
89
90 /*
91 * The historic version of vi permitted the user to string any number
92 * of '>' or '<' characters together, resulting in an indent of the
93 * appropriate levels. There's a special hack in ex_cmd() so that
94 * cmdp->argv[0] points to the string of '>' or '<' characters.
95 *
96 * Q: What's the difference between the people adding features
97 * to vi and the Girl Scouts?
98 * A: The Girl Scouts have mint cookies and adult supervision.
99 */
100 for (p = cmdp->argv[0]->bp, sw = 0; *p == '>' || *p == '<'; ++p)
101 sw += O_VAL(sp, O_SHIFTWIDTH);
102
103 GET_SPACE_RETW(sp, bp, blen, 256);
104
105 curset = 0;
106 for (from = cmdp->addr1.lno, to = cmdp->addr2.lno; from <= to; ++from) {
107 if (db_get(sp, from, DBG_FATAL, &p, &len))
108 goto err;
109 if (!len) {
110 if (sp->lno == from)
111 curset = 1;
112 continue;
113 }
114
115 /*
116 * Calculate the old indent amount and the number of
117 * characters it used.
118 */
119 for (oldidx = 0, oldcol = 0; oldidx < len; ++oldidx)
120 if (p[oldidx] == ' ')
121 ++oldcol;
122 else if (p[oldidx] == '\t')
123 oldcol += O_VAL(sp, O_TABSTOP) -
124 oldcol % O_VAL(sp, O_TABSTOP);
125 else
126 break;
127
128 /* Calculate the new indent amount. */
129 if (rl == RETAB)
130 newcol = oldcol;
131 else if (rl == RIGHT)
132 newcol = oldcol + sw;
133 else {
134 newcol = oldcol < sw ? 0 : oldcol - sw;
135 if (newcol == oldcol) {
136 if (sp->lno == from)
137 curset = 1;
138 continue;
139 }
140 }
141
142 /* Get a buffer that will hold the new line. */
143 ADD_SPACE_RETW(sp, bp, blen, newcol + len);
144
145 /*
146 * Build a new indent string and count the number of
147 * characters it uses.
148 */
149 tbp = bp;
150 newidx = 0;
151 if (!O_ISSET(sp, O_EXPANDTAB)) {
152 for (; newcol >= O_VAL(sp, O_TABSTOP); ++newidx) {
153 *tbp++ = '\t';
154 newcol -= O_VAL(sp, O_TABSTOP);
155 }
156 }
157 for (; newcol > 0; --newcol, ++newidx)
158 *tbp++ = ' ';
159
160 /* Add the original line. */
161 MEMCPY(tbp, p + oldidx, len - oldidx);
162
163 /* Set the replacement line. */
164 if (db_set(sp, from, bp, (tbp + (len - oldidx)) - bp)) {
165 err: FREE_SPACEW(sp, bp, blen);
166 return (1);
167 }
168
169 /*
170 * !!!
171 * The shift command in historic vi had the usual bizarre
172 * collection of cursor semantics. If called from vi, the
173 * cursor was repositioned to the first non-blank character
174 * of the lowest numbered line shifted. If called from ex,
175 * the cursor was repositioned to the first non-blank of the
176 * highest numbered line shifted. Here, if the cursor isn't
177 * part of the set of lines that are moved, move it to the
178 * first non-blank of the last line shifted. (This makes
179 * ":3>>" in vi work reasonably.) If the cursor is part of
180 * the shifted lines, it doesn't get moved at all. This
181 * permits shifting of marked areas, i.e. ">'a." shifts the
182 * marked area twice, something that couldn't be done with
183 * historic vi.
184 */
185 if (sp->lno == from) {
186 curset = 1;
187 if (newidx > oldidx)
188 sp->cno += newidx - oldidx;
189 else if (sp->cno >= oldidx - newidx)
190 sp->cno -= oldidx - newidx;
191 }
192 }
193 if (!curset) {
194 sp->lno = to;
195 sp->cno = 0;
196 (void)nonblank(sp, to, &sp->cno);
197 }
198
199 FREE_SPACEW(sp, bp, blen);
200
201 sp->rptlines[L_SHIFT] += cmdp->addr2.lno - cmdp->addr1.lno + 1;
202 return (0);
203 }
204