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