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