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