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