1 /* $OpenBSD: ex_shift.c,v 1.10 2022/04/22 15:48:29 millert Exp $ */
2
3 /*-
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.
8 *
9 * See the LICENSE file for redistribution information.
10 */
11
12 #include "config.h"
13
14 #include <sys/queue.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 *p, *bp, *tbp;
73
74 NEEDFILE(sp, cmdp);
75
76 if (O_VAL(sp, O_SHIFTWIDTH) == 0) {
77 msgq(sp, M_INFO, "shiftwidth option set to 0");
78 return (0);
79 }
80
81 /* Copy the lines being shifted into the unnamed buffer. */
82 if (cut(sp, NULL, &cmdp->addr1, &cmdp->addr2, CUT_LINEMODE))
83 return (1);
84
85 /*
86 * The historic version of vi permitted the user to string any number
87 * of '>' or '<' characters together, resulting in an indent of the
88 * appropriate levels. There's a special hack in ex_cmd() so that
89 * cmdp->argv[0] points to the string of '>' or '<' characters.
90 *
91 * Q: What's the difference between the people adding features
92 * to vi and the Girl Scouts?
93 * A: The Girl Scouts have mint cookies and adult supervision.
94 */
95 for (p = cmdp->argv[0]->bp, sw = 0; *p == '>' || *p == '<'; ++p)
96 sw += O_VAL(sp, O_SHIFTWIDTH);
97
98 GET_SPACE_RET(sp, bp, blen, 256);
99
100 curset = 0;
101 for (from = cmdp->addr1.lno, to = cmdp->addr2.lno; from <= to; ++from) {
102 if (db_get(sp, from, DBG_FATAL, &p, &len))
103 goto err;
104 if (!len) {
105 if (sp->lno == from)
106 curset = 1;
107 continue;
108 }
109
110 /*
111 * Calculate the old indent amount and the number of
112 * characters it used.
113 */
114 for (oldidx = 0, oldcol = 0; oldidx < len; ++oldidx)
115 if (p[oldidx] == ' ')
116 ++oldcol;
117 else if (p[oldidx] == '\t')
118 oldcol += O_VAL(sp, O_TABSTOP) -
119 oldcol % O_VAL(sp, O_TABSTOP);
120 else
121 break;
122
123 /* Calculate the new indent amount. */
124 if (rl == RETAB)
125 newcol = oldcol;
126 else if (rl == RIGHT)
127 newcol = oldcol + sw;
128 else {
129 newcol = oldcol < sw ? 0 : oldcol - sw;
130 if (newcol == oldcol) {
131 if (sp->lno == from)
132 curset = 1;
133 continue;
134 }
135 }
136
137 /* Get a buffer that will hold the new line. */
138 ADD_SPACE_RET(sp, bp, blen, newcol + len);
139
140 /*
141 * Build a new indent string and count the number of
142 * characters it uses.
143 */
144 tbp = bp;
145 newidx = 0;
146 if (!O_ISSET(sp, O_EXPANDTAB)) {
147 for (; newcol >= O_VAL(sp, O_TABSTOP); ++newidx) {
148 *tbp++ = '\t';
149 newcol -= O_VAL(sp, O_TABSTOP);
150 }
151 }
152 for (; newcol > 0; --newcol, ++newidx)
153 *tbp++ = ' ';
154
155 /* Add the original line. */
156 memcpy(tbp, p + oldidx, len - oldidx);
157
158 /* Set the replacement line. */
159 if (db_set(sp, from, bp, (tbp + (len - oldidx)) - bp)) {
160 err: FREE_SPACE(sp, bp, blen);
161 return (1);
162 }
163
164 /*
165 * !!!
166 * The shift command in historic vi had the usual bizarre
167 * collection of cursor semantics. If called from vi, the
168 * cursor was repositioned to the first non-blank character
169 * of the lowest numbered line shifted. If called from ex,
170 * the cursor was repositioned to the first non-blank of the
171 * highest numbered line shifted. Here, if the cursor isn't
172 * part of the set of lines that are moved, move it to the
173 * first non-blank of the last line shifted. (This makes
174 * ":3>>" in vi work reasonably.) If the cursor is part of
175 * the shifted lines, it doesn't get moved at all. This
176 * permits shifting of marked areas, i.e. ">'a." shifts the
177 * marked area twice, something that couldn't be done with
178 * historic vi.
179 */
180 if (sp->lno == from) {
181 curset = 1;
182 if (newidx > oldidx)
183 sp->cno += newidx - oldidx;
184 else if (sp->cno >= oldidx - newidx)
185 sp->cno -= oldidx - newidx;
186 }
187 }
188 if (!curset) {
189 sp->lno = to;
190 sp->cno = 0;
191 (void)nonblank(sp, to, &sp->cno);
192 }
193
194 FREE_SPACE(sp, bp, blen);
195
196 sp->rptlines[L_SHIFT] += cmdp->addr2.lno - cmdp->addr1.lno + 1;
197 return (0);
198 }
199