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