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