Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / window / wwscroll.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Edward Wang at The University of California, Berkeley.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 #ifndef lint
38 static char sccsid[] = "@(#)wwscroll.c  8.1 (Berkeley) 6/6/93";
39 static char rcsid[] =
40   "$FreeBSD: src/usr.bin/window/wwscroll.c,v 1.1.1.1.14.1 2001/05/17 09:45:01 obrien Exp $";
41 #endif /* not lint */
42
43 #include "ww.h"
44 #include "tt.h"
45
46 wwscroll(w, n)
47 register struct ww *w;
48 int n;
49 {
50         register dir;
51         register top;
52
53         if (n == 0)
54                 return;
55         dir = n < 0 ? -1 : 1;
56         top = w->ww_b.t - n;
57         if (top > w->ww_w.t)
58                 top = w->ww_w.t;
59         else if (top + w->ww_b.nr < w->ww_w.b)
60                 top = w->ww_w.b - w->ww_b.nr;
61         n = abs(top - w->ww_b.t);
62         if (n < w->ww_i.nr) {
63                 while (--n >= 0) {
64                         (void) wwscroll1(w, w->ww_i.t, w->ww_i.b, dir, 0);
65                         w->ww_buf += dir;
66                         w->ww_b.t -= dir;
67                         w->ww_b.b -= dir;
68                 }
69         } else {
70                 w->ww_buf -= top - w->ww_b.t;
71                 w->ww_b.t = top;
72                 w->ww_b.b = top + w->ww_b.nr;
73                 wwredrawwin(w);
74         }
75 }
76
77 /*
78  * Scroll one line, between 'row1' and 'row2', in direction 'dir'.
79  * Don't adjust ww_scroll.
80  * And don't redraw 'leaveit' lines.
81  */
82 wwscroll1(w, row1, row2, dir, leaveit)
83 register struct ww *w;
84 int row1, row2, dir;
85 int leaveit;
86 {
87         register i;
88         int row1x, row2x;
89         int nvis;
90         int nvismax;
91         int scrolled = 0;
92
93         /*
94          * See how many lines on the screen are affected.
95          * And calculate row1x, row2x, and left at the same time.
96          */
97         for (i = row1; i < row2 && w->ww_nvis[i] == 0; i++)
98                 ;
99         if (i >= row2)                  /* can't do any fancy stuff */
100                 goto out;
101         row1x = i;
102         for (i = row2 - 1; i >= row1 && w->ww_nvis[i] == 0; i--)
103                 ;
104         if (i <= row1x)
105                 goto out;               /* just one line is easy */
106         row2x = i + 1;
107
108         /*
109          * See how much of this window is visible.
110          */
111         nvismax = wwncol * (row2x - row1x);
112         nvis = 0;
113         for (i = row1x; i < row2x; i++)
114                 nvis += w->ww_nvis[i];
115
116         /*
117          * If it's a good idea to scroll and the terminal can, then do it.
118          */
119         if (nvis < nvismax / 2)
120                 goto no_scroll;         /* not worth it */
121         if ((dir > 0 ? tt.tt_scroll_down == 0 : tt.tt_scroll_up == 0) ||
122             (tt.tt_scroll_top != row1x || tt.tt_scroll_bot != row2x - 1) &&
123             tt.tt_setscroll == 0)
124                 if (tt.tt_delline == 0 || tt.tt_insline == 0)
125                         goto no_scroll;
126         xxscroll(dir, row1x, row2x);
127         scrolled = 1;
128         /*
129          * Fix up the old screen.
130          */
131         {
132                 register union ww_char *tmp;
133                 register union ww_char **cpp, **cqq;
134
135                 if (dir > 0) {
136                         cpp = &wwos[row1x];
137                         cqq = cpp + 1;
138                         tmp = *cpp;
139                         for (i = row2x - row1x; --i > 0;)
140                                 *cpp++ = *cqq++;
141                         *cpp = tmp;
142                 } else {
143                         cpp = &wwos[row2x];
144                         cqq = cpp - 1;
145                         tmp = *cqq;
146                         for (i = row2x - row1x; --i > 0;)
147                                 *--cpp = *--cqq;
148                         *cqq = tmp;
149                 }
150                 for (i = wwncol; --i >= 0;)
151                         tmp++->c_w = ' ';
152         }
153
154 no_scroll:
155         /*
156          * Fix the new screen.
157          */
158         if (nvis == nvismax) {
159                 /*
160                  * Can shift whole lines.
161                  */
162                 if (dir > 0) {
163                         {
164                                 register union ww_char *tmp;
165                                 register union ww_char **cpp, **cqq;
166
167                                 cpp = &wwns[row1x];
168                                 cqq = cpp + 1;
169                                 tmp = *cpp;
170                                 for (i = row2x - row1x; --i > 0;)
171                                         *cpp++ = *cqq++;
172                                 *cpp = tmp;
173                         }
174                         if (scrolled) {
175                                 register char *p, *q;
176
177                                 p = &wwtouched[row1x];
178                                 q = p + 1;
179                                 for (i = row2x - row1x; --i > 0;)
180                                         *p++ = *q++;
181                                 *p |= WWU_TOUCHED;
182                         } else {
183                                 register char *p;
184
185                                 p = &wwtouched[row1x];
186                                 for (i = row2x - row1x; --i >= 0;)
187                                         *p++ |= WWU_TOUCHED;
188                         }
189                         wwredrawwin1(w, row1, row1x, dir);
190                         wwredrawwin1(w, row2x - 1, row2 - leaveit, dir);
191                 } else {
192                         {
193                                 register union ww_char *tmp;
194                                 register union ww_char **cpp, **cqq;
195
196                                 cpp = &wwns[row2x];
197                                 cqq = cpp - 1;
198                                 tmp = *cqq;
199                                 for (i = row2x - row1x; --i > 0;)
200                                         *--cpp = *--cqq;
201                                 *cqq = tmp;
202                         }
203                         if (scrolled) {
204                                 register char *p, *q;
205
206                                 p = &wwtouched[row2x];
207                                 q = p - 1;
208                                 for (i = row2x - row1x; --i > 0;)
209                                         *--p = *--q;
210                                 *q |= WWU_TOUCHED;
211                         } else {
212                                 register char *p;
213
214                                 p = &wwtouched[row1x];
215                                 for (i = row2x - row1x; --i >= 0;)
216                                         *p++ |= WWU_TOUCHED;
217                         }
218                         wwredrawwin1(w, row1 + leaveit, row1x + 1, dir);
219                         wwredrawwin1(w, row2x, row2, dir);
220                 }
221         } else {
222                 if (scrolled) {
223                         register char *p;
224
225                         p = &wwtouched[row1x];
226                         for (i = row2x - row1x; --i >= 0;)
227                                 *p++ |= WWU_TOUCHED;
228                 }
229 out:
230                 if (dir > 0)
231                         wwredrawwin1(w, row1, row2 - leaveit, dir);
232                 else
233                         wwredrawwin1(w, row1 + leaveit, row2, dir);
234         }
235         return scrolled;
236 }