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