ex_shift.c revision 254225
138032Speter/*-
2261363Sgshapiro * Copyright (c) 1992, 1993, 1994
364565Sgshapiro *	The Regents of the University of California.  All rights reserved.
438032Speter * Copyright (c) 1992, 1993, 1994, 1995, 1996
538032Speter *	Keith Bostic.  All rights reserved.
638032Speter *
738032Speter * See the LICENSE file for redistribution information.
838032Speter */
938032Speter
1038032Speter#include "config.h"
1138032Speter
1238032Speter#ifndef lint
1338032Speterstatic const char sccsid[] = "$Id: ex_shift.c,v 10.17 2001/06/25 15:19:20 skimo Exp $";
1464565Sgshapiro#endif /* not lint */
1564565Sgshapiro
16266692Sgshapiro#include <sys/types.h>
1764565Sgshapiro#include <sys/queue.h>
1864565Sgshapiro#include <sys/time.h>
1964565Sgshapiro
2064565Sgshapiro#include <bitstring.h>
2164565Sgshapiro#include <limits.h>
2238032Speter#include <stdio.h>
2338032Speter#include <stdlib.h>
2464565Sgshapiro#include <string.h>
2564565Sgshapiro
2664565Sgshapiro#include "../common/common.h"
2764565Sgshapiro
2864565Sgshapiroenum which {LEFT, RIGHT};
2990795Sgshapirostatic int shift __P((SCR *, EXCMD *, enum which));
3064565Sgshapiro
3138032Speter/*
3238032Speter * ex_shiftl -- :<[<...]
3338032Speter *
3438032Speter *
3538032Speter * PUBLIC: int ex_shiftl __P((SCR *, EXCMD *));
3638032Speter */
3738032Speterint
3838032Speterex_shiftl(SCR *sp, EXCMD *cmdp)
3938032Speter{
4038032Speter	return (shift(sp, cmdp, LEFT));
4138032Speter}
4238032Speter
4338032Speter/*
4438032Speter * ex_shiftr -- :>[>...]
4538032Speter *
4638032Speter * PUBLIC: int ex_shiftr __P((SCR *, EXCMD *));
4738032Speter */
4838032Speterint
4938032Speterex_shiftr(SCR *sp, EXCMD *cmdp)
50157006Sgshapiro{
51157006Sgshapiro	return (shift(sp, cmdp, RIGHT));
52157006Sgshapiro}
5338032Speter
5438032Speter/*
5538032Speter * shift --
5638032Speter *	Ex shift support.
5738032Speter */
5838032Speterstatic int
5938032Spetershift(SCR *sp, EXCMD *cmdp, enum which rl)
6064565Sgshapiro{
6138032Speter	recno_t from, to;
6290795Sgshapiro	size_t blen, len, newcol, newidx, oldcol, oldidx, sw;
6338032Speter	int curset;
6438032Speter	CHAR_T *p;
6538032Speter	CHAR_T *bp, *tbp;
6638032Speter
6738032Speter	NEEDFILE(sp, cmdp);
6838032Speter
6938032Speter	if (O_VAL(sp, O_SHIFTWIDTH) == 0) {
7038032Speter		msgq(sp, M_INFO, "152|shiftwidth option set to 0");
7138032Speter		return (0);
7238032Speter	}
7338032Speter
7438032Speter	/* Copy the lines being shifted into the unnamed buffer. */
7538032Speter	if (cut(sp, NULL, &cmdp->addr1, &cmdp->addr2, CUT_LINEMODE))
7638032Speter		return (1);
7738032Speter
7838032Speter	/*
7938032Speter	 * The historic version of vi permitted the user to string any number
8038032Speter	 * of '>' or '<' characters together, resulting in an indent of the
8138032Speter	 * appropriate levels.  There's a special hack in ex_cmd() so that
8238032Speter	 * cmdp->argv[0] points to the string of '>' or '<' characters.
8338032Speter	 *
8438032Speter	 * Q: What's the difference between the people adding features
8538032Speter	 *    to vi and the Girl Scouts?
8638032Speter	 * A: The Girl Scouts have mint cookies and adult supervision.
8738032Speter	 */
8838032Speter	for (p = cmdp->argv[0]->bp, sw = 0; *p == '>' || *p == '<'; ++p)
8938032Speter		sw += O_VAL(sp, O_SHIFTWIDTH);
9038032Speter
9138032Speter	GET_SPACE_RETW(sp, bp, blen, 256);
9238032Speter
9338032Speter	curset = 0;
9438032Speter	for (from = cmdp->addr1.lno, to = cmdp->addr2.lno; from <= to; ++from) {
9538032Speter		if (db_get(sp, from, DBG_FATAL, &p, &len))
9638032Speter			goto err;
9738032Speter		if (!len) {
9838032Speter			if (sp->lno == from)
9938032Speter				curset = 1;
10038032Speter			continue;
10190795Sgshapiro		}
10238032Speter
10338032Speter		/*
10490795Sgshapiro		 * Calculate the old indent amount and the number of
10590795Sgshapiro		 * characters it used.
10638032Speter		 */
10738032Speter		for (oldidx = 0, oldcol = 0; oldidx < len; ++oldidx)
10864565Sgshapiro			if (p[oldidx] == ' ')
10990795Sgshapiro				++oldcol;
11090795Sgshapiro			else if (p[oldidx] == '\t')
11138032Speter				oldcol += O_VAL(sp, O_TABSTOP) -
11238032Speter				    oldcol % O_VAL(sp, O_TABSTOP);
11338032Speter			else
11438032Speter				break;
11590795Sgshapiro
11638032Speter		/* Calculate the new indent amount. */
11738032Speter		if (rl == RIGHT)
11838032Speter			newcol = oldcol + sw;
11938032Speter		else {
12038032Speter			newcol = oldcol < sw ? 0 : oldcol - sw;
12138032Speter			if (newcol == oldcol) {
12238032Speter				if (sp->lno == from)
12338032Speter					curset = 1;
12438032Speter				continue;
12538032Speter			}
12638032Speter		}
12738032Speter
12838032Speter		/* Get a buffer that will hold the new line. */
12938032Speter		ADD_SPACE_RETW(sp, bp, blen, newcol + len);
13038032Speter
13138032Speter		/*
13238032Speter		 * Build a new indent string and count the number of
13338032Speter		 * characters it uses.
13438032Speter		 */
13538032Speter		for (tbp = bp, newidx = 0;
13638032Speter		    newcol >= O_VAL(sp, O_TABSTOP); ++newidx) {
13738032Speter			*tbp++ = '\t';
13838032Speter			newcol -= O_VAL(sp, O_TABSTOP);
13938032Speter		}
14038032Speter		for (; newcol > 0; --newcol, ++newidx)
14138032Speter			*tbp++ = ' ';
14238032Speter
143168520Sgshapiro		/* Add the original line. */
144168520Sgshapiro		MEMCPY(tbp, p + oldidx, len - oldidx);
14564565Sgshapiro
14638032Speter		/* Set the replacement line. */
14738032Speter		if (db_set(sp, from, bp, (tbp + (len - oldidx)) - bp)) {
14838032Spetererr:			FREE_SPACEW(sp, bp, blen);
14938032Speter			return (1);
15038032Speter		}
15138032Speter
15238032Speter		/*
15338032Speter		 * !!!
15438032Speter		 * The shift command in historic vi had the usual bizarre
15538032Speter		 * collection of cursor semantics.  If called from vi, the
15638032Speter		 * cursor was repositioned to the first non-blank character
15738032Speter		 * of the lowest numbered line shifted.  If called from ex,
15894337Sgshapiro		 * the cursor was repositioned to the first non-blank of the
15964565Sgshapiro		 * highest numbered line shifted.  Here, if the cursor isn't
16064565Sgshapiro		 * part of the set of lines that are moved, move it to the
16164565Sgshapiro		 * first non-blank of the last line shifted.  (This makes
16264565Sgshapiro		 * ":3>>" in vi work reasonably.)  If the cursor is part of
16338032Speter		 * the shifted lines, it doesn't get moved at all.  This
16464565Sgshapiro		 * permits shifting of marked areas, i.e. ">'a." shifts the
16538032Speter		 * marked area twice, something that couldn't be done with
16664565Sgshapiro		 * historic vi.
16764565Sgshapiro		 */
16890795Sgshapiro		if (sp->lno == from) {
16938032Speter			curset = 1;
17038032Speter			if (newidx > oldidx)
17138032Speter				sp->cno += newidx - oldidx;
17238032Speter			else if (sp->cno >= oldidx - newidx)
17338032Speter				sp->cno -= oldidx - newidx;
17438032Speter		}
17538032Speter	}
17638032Speter	if (!curset) {
17738032Speter		sp->lno = to;
17890795Sgshapiro		sp->cno = 0;
17938032Speter		(void)nonblank(sp, to, &sp->cno);
18038032Speter	}
18138032Speter
18238032Speter	FREE_SPACEW(sp, bp, blen);
18338032Speter
18438032Speter	sp->rptlines[L_SHIFT] += cmdp->addr2.lno - cmdp->addr1.lno + 1;
18590795Sgshapiro	return (0);
18690795Sgshapiro}
18738032Speter