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