xref: /openbsd/usr.bin/vi/ex/ex_shift.c (revision a6445c1d)
1 /*	$OpenBSD: ex_shift.c,v 1.7 2014/11/12 04:28:41 bentley 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 {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
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
45 ex_shiftr(SCR *sp, EXCMD *cmdp)
46 {
47 	return (shift(sp, cmdp, RIGHT));
48 }
49 
50 /*
51  * shift --
52  *	Ex shift support.
53  */
54 static int
55 shift(SCR *sp, EXCMD *cmdp, enum which rl)
56 {
57 	recno_t from, to;
58 	size_t blen, len, newcol, newidx, oldcol, oldidx, sw;
59 	int curset;
60 	char *p, *bp, *tbp;
61 
62 	NEEDFILE(sp, cmdp);
63 
64 	if (O_VAL(sp, O_SHIFTWIDTH) == 0) {
65 		msgq(sp, M_INFO, "152|shiftwidth option set to 0");
66 		return (0);
67 	}
68 
69 	/* Copy the lines being shifted into the unnamed buffer. */
70 	if (cut(sp, NULL, &cmdp->addr1, &cmdp->addr2, CUT_LINEMODE))
71 		return (1);
72 
73 	/*
74 	 * The historic version of vi permitted the user to string any number
75 	 * of '>' or '<' characters together, resulting in an indent of the
76 	 * appropriate levels.  There's a special hack in ex_cmd() so that
77 	 * cmdp->argv[0] points to the string of '>' or '<' characters.
78 	 *
79 	 * Q: What's the difference between the people adding features
80 	 *    to vi and the Girl Scouts?
81 	 * A: The Girl Scouts have mint cookies and adult supervision.
82 	 */
83 	for (p = cmdp->argv[0]->bp, sw = 0; *p == '>' || *p == '<'; ++p)
84 		sw += O_VAL(sp, O_SHIFTWIDTH);
85 
86 	GET_SPACE_RET(sp, bp, blen, 256);
87 
88 	curset = 0;
89 	for (from = cmdp->addr1.lno, to = cmdp->addr2.lno; from <= to; ++from) {
90 		if (db_get(sp, from, DBG_FATAL, &p, &len))
91 			goto err;
92 		if (!len) {
93 			if (sp->lno == from)
94 				curset = 1;
95 			continue;
96 		}
97 
98 		/*
99 		 * Calculate the old indent amount and the number of
100 		 * characters it used.
101 		 */
102 		for (oldidx = 0, oldcol = 0; oldidx < len; ++oldidx)
103 			if (p[oldidx] == ' ')
104 				++oldcol;
105 			else if (p[oldidx] == '\t')
106 				oldcol += O_VAL(sp, O_TABSTOP) -
107 				    oldcol % O_VAL(sp, O_TABSTOP);
108 			else
109 				break;
110 
111 		/* Calculate the new indent amount. */
112 		if (rl == RIGHT)
113 			newcol = oldcol + sw;
114 		else {
115 			newcol = oldcol < sw ? 0 : oldcol - sw;
116 			if (newcol == oldcol) {
117 				if (sp->lno == from)
118 					curset = 1;
119 				continue;
120 			}
121 		}
122 
123 		/* Get a buffer that will hold the new line. */
124 		ADD_SPACE_RET(sp, bp, blen, newcol + len);
125 
126 		/*
127 		 * Build a new indent string and count the number of
128 		 * characters it uses.
129 		 */
130 		for (tbp = bp, newidx = 0;
131 		    newcol >= O_VAL(sp, O_TABSTOP); ++newidx) {
132 			*tbp++ = '\t';
133 			newcol -= O_VAL(sp, O_TABSTOP);
134 		}
135 		for (; newcol > 0; --newcol, ++newidx)
136 			*tbp++ = ' ';
137 
138 		/* Add the original line. */
139 		memcpy(tbp, p + oldidx, len - oldidx);
140 
141 		/* Set the replacement line. */
142 		if (db_set(sp, from, bp, (tbp + (len - oldidx)) - bp)) {
143 err:			FREE_SPACE(sp, bp, blen);
144 			return (1);
145 		}
146 
147 		/*
148 		 * !!!
149 		 * The shift command in historic vi had the usual bizarre
150 		 * collection of cursor semantics.  If called from vi, the
151 		 * cursor was repositioned to the first non-blank character
152 		 * of the lowest numbered line shifted.  If called from ex,
153 		 * the cursor was repositioned to the first non-blank of the
154 		 * highest numbered line shifted.  Here, if the cursor isn't
155 		 * part of the set of lines that are moved, move it to the
156 		 * first non-blank of the last line shifted.  (This makes
157 		 * ":3>>" in vi work reasonably.)  If the cursor is part of
158 		 * the shifted lines, it doesn't get moved at all.  This
159 		 * permits shifting of marked areas, i.e. ">'a." shifts the
160 		 * marked area twice, something that couldn't be done with
161 		 * historic vi.
162 		 */
163 		if (sp->lno == from) {
164 			curset = 1;
165 			if (newidx > oldidx)
166 				sp->cno += newidx - oldidx;
167 			else if (sp->cno >= oldidx - newidx)
168 				sp->cno -= oldidx - newidx;
169 		}
170 	}
171 	if (!curset) {
172 		sp->lno = to;
173 		sp->cno = 0;
174 		(void)nonblank(sp, to, &sp->cno);
175 	}
176 
177 	FREE_SPACE(sp, bp, blen);
178 
179 	sp->rptlines[L_SHIFT] += cmdp->addr2.lno - cmdp->addr1.lno + 1;
180 	return (0);
181 }
182