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