1994-08-16 23:36:45 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1992, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1996-11-01 06:45:43 +00:00
|
|
|
* Copyright (c) 1992, 1993, 1994, 1995, 1996
|
|
|
|
* Keith Bostic. All rights reserved.
|
1994-08-16 23:36:45 +00:00
|
|
|
*
|
1996-11-01 06:45:43 +00:00
|
|
|
* See the LICENSE file for redistribution information.
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
1994-08-16 23:36:45 +00:00
|
|
|
#ifndef lint
|
2013-08-11 09:44:58 +00:00
|
|
|
static const char sccsid[] = "$Id: ex_shift.c,v 10.17 2001/06/25 15:19:20 skimo Exp $";
|
1994-08-16 23:36:45 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/queue.h>
|
2013-08-11 09:44:58 +00:00
|
|
|
#include <sys/time.h>
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
#include <bitstring.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
#include "../common/common.h"
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
enum which {LEFT, RIGHT};
|
1996-11-01 06:45:43 +00:00
|
|
|
static int shift __P((SCR *, EXCMD *, enum which));
|
1994-08-16 23:36:45 +00:00
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
/*
|
|
|
|
* ex_shiftl -- :<[<...]
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* PUBLIC: int ex_shiftl __P((SCR *, EXCMD *));
|
|
|
|
*/
|
1994-08-16 23:36:45 +00:00
|
|
|
int
|
2013-08-11 09:44:58 +00:00
|
|
|
ex_shiftl(SCR *sp, EXCMD *cmdp)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
1996-11-01 06:45:43 +00:00
|
|
|
return (shift(sp, cmdp, LEFT));
|
1994-08-16 23:36:45 +00:00
|
|
|
}
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
/*
|
|
|
|
* ex_shiftr -- :>[>...]
|
|
|
|
*
|
|
|
|
* PUBLIC: int ex_shiftr __P((SCR *, EXCMD *));
|
|
|
|
*/
|
1994-08-16 23:36:45 +00:00
|
|
|
int
|
2013-08-11 09:44:58 +00:00
|
|
|
ex_shiftr(SCR *sp, EXCMD *cmdp)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
1996-11-01 06:45:43 +00:00
|
|
|
return (shift(sp, cmdp, RIGHT));
|
1994-08-16 23:36:45 +00:00
|
|
|
}
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
/*
|
|
|
|
* shift --
|
|
|
|
* Ex shift support.
|
|
|
|
*/
|
1994-08-16 23:36:45 +00:00
|
|
|
static int
|
2013-08-11 09:44:58 +00:00
|
|
|
shift(SCR *sp, EXCMD *cmdp, enum which rl)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
|
|
|
recno_t from, to;
|
|
|
|
size_t blen, len, newcol, newidx, oldcol, oldidx, sw;
|
|
|
|
int curset;
|
2013-08-11 09:44:58 +00:00
|
|
|
CHAR_T *p;
|
|
|
|
CHAR_T *bp, *tbp;
|
1994-08-16 23:36:45 +00:00
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
NEEDFILE(sp, cmdp);
|
|
|
|
|
1994-08-16 23:36:45 +00:00
|
|
|
if (O_VAL(sp, O_SHIFTWIDTH) == 0) {
|
1996-11-01 06:45:43 +00:00
|
|
|
msgq(sp, M_INFO, "152|shiftwidth option set to 0");
|
1994-08-16 23:36:45 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
/* Copy the lines being shifted into the unnamed buffer. */
|
|
|
|
if (cut(sp, NULL, &cmdp->addr1, &cmdp->addr2, CUT_LINEMODE))
|
|
|
|
return (1);
|
|
|
|
|
1994-08-16 23:36:45 +00:00
|
|
|
/*
|
|
|
|
* The historic version of vi permitted the user to string any number
|
|
|
|
* of '>' or '<' characters together, resulting in an indent of the
|
|
|
|
* appropriate levels. There's a special hack in ex_cmd() so that
|
|
|
|
* cmdp->argv[0] points to the string of '>' or '<' characters.
|
|
|
|
*
|
|
|
|
* Q: What's the difference between the people adding features
|
|
|
|
* to vi and the Girl Scouts?
|
|
|
|
* A: The Girl Scouts have mint cookies and adult supervision.
|
|
|
|
*/
|
|
|
|
for (p = cmdp->argv[0]->bp, sw = 0; *p == '>' || *p == '<'; ++p)
|
|
|
|
sw += O_VAL(sp, O_SHIFTWIDTH);
|
|
|
|
|
2013-08-11 09:44:58 +00:00
|
|
|
GET_SPACE_RETW(sp, bp, blen, 256);
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
curset = 0;
|
|
|
|
for (from = cmdp->addr1.lno, to = cmdp->addr2.lno; from <= to; ++from) {
|
1996-11-01 06:45:43 +00:00
|
|
|
if (db_get(sp, from, DBG_FATAL, &p, &len))
|
1994-08-16 23:36:45 +00:00
|
|
|
goto err;
|
|
|
|
if (!len) {
|
|
|
|
if (sp->lno == from)
|
|
|
|
curset = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate the old indent amount and the number of
|
|
|
|
* characters it used.
|
|
|
|
*/
|
|
|
|
for (oldidx = 0, oldcol = 0; oldidx < len; ++oldidx)
|
|
|
|
if (p[oldidx] == ' ')
|
|
|
|
++oldcol;
|
|
|
|
else if (p[oldidx] == '\t')
|
|
|
|
oldcol += O_VAL(sp, O_TABSTOP) -
|
|
|
|
oldcol % O_VAL(sp, O_TABSTOP);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Calculate the new indent amount. */
|
|
|
|
if (rl == RIGHT)
|
|
|
|
newcol = oldcol + sw;
|
|
|
|
else {
|
|
|
|
newcol = oldcol < sw ? 0 : oldcol - sw;
|
|
|
|
if (newcol == oldcol) {
|
|
|
|
if (sp->lno == from)
|
|
|
|
curset = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a buffer that will hold the new line. */
|
2013-08-11 09:44:58 +00:00
|
|
|
ADD_SPACE_RETW(sp, bp, blen, newcol + len);
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Build a new indent string and count the number of
|
|
|
|
* characters it uses.
|
|
|
|
*/
|
|
|
|
for (tbp = bp, newidx = 0;
|
|
|
|
newcol >= O_VAL(sp, O_TABSTOP); ++newidx) {
|
|
|
|
*tbp++ = '\t';
|
|
|
|
newcol -= O_VAL(sp, O_TABSTOP);
|
|
|
|
}
|
|
|
|
for (; newcol > 0; --newcol, ++newidx)
|
|
|
|
*tbp++ = ' ';
|
|
|
|
|
|
|
|
/* Add the original line. */
|
2013-08-11 09:44:58 +00:00
|
|
|
MEMCPY(tbp, p + oldidx, len - oldidx);
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
/* Set the replacement line. */
|
1996-11-01 06:45:43 +00:00
|
|
|
if (db_set(sp, from, bp, (tbp + (len - oldidx)) - bp)) {
|
2013-08-11 09:44:58 +00:00
|
|
|
err: FREE_SPACEW(sp, bp, blen);
|
1994-08-16 23:36:45 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* !!!
|
|
|
|
* The shift command in historic vi had the usual bizarre
|
|
|
|
* collection of cursor semantics. If called from vi, the
|
|
|
|
* cursor was repositioned to the first non-blank character
|
|
|
|
* of the lowest numbered line shifted. If called from ex,
|
|
|
|
* the cursor was repositioned to the first non-blank of the
|
|
|
|
* highest numbered line shifted. Here, if the cursor isn't
|
|
|
|
* part of the set of lines that are moved, move it to the
|
|
|
|
* first non-blank of the last line shifted. (This makes
|
|
|
|
* ":3>>" in vi work reasonably.) If the cursor is part of
|
|
|
|
* the shifted lines, it doesn't get moved at all. This
|
|
|
|
* permits shifting of marked areas, i.e. ">'a." shifts the
|
|
|
|
* marked area twice, something that couldn't be done with
|
|
|
|
* historic vi.
|
|
|
|
*/
|
|
|
|
if (sp->lno == from) {
|
|
|
|
curset = 1;
|
|
|
|
if (newidx > oldidx)
|
|
|
|
sp->cno += newidx - oldidx;
|
|
|
|
else if (sp->cno >= oldidx - newidx)
|
|
|
|
sp->cno -= oldidx - newidx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!curset) {
|
|
|
|
sp->lno = to;
|
|
|
|
sp->cno = 0;
|
1996-11-01 06:45:43 +00:00
|
|
|
(void)nonblank(sp, to, &sp->cno);
|
1994-08-16 23:36:45 +00:00
|
|
|
}
|
|
|
|
|
2013-08-11 09:44:58 +00:00
|
|
|
FREE_SPACEW(sp, bp, blen);
|
1994-08-16 23:36:45 +00:00
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
sp->rptlines[L_SHIFT] += cmdp->addr2.lno - cmdp->addr1.lno + 1;
|
1994-08-16 23:36:45 +00:00
|
|
|
return (0);
|
|
|
|
}
|