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