]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/hardscroll.c
ncurses 4.2
[ncurses.git] / ncurses / hardscroll.c
1 /****************************************************************************
2  * Copyright (c) 1998 Free Software Foundation, Inc.                        *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  ****************************************************************************/
33
34
35 /******************************************************************************
36
37 NAME
38    hardscroll.c -- hardware-scrolling optimization for ncurses
39
40 SYNOPSIS
41    void _nc_scroll_optimize(void)
42
43 DESCRIPTION
44                         OVERVIEW
45
46 This algorithm for computes optimum hardware scrolling to transform an
47 old screen (curscr) into a new screen (newscr) via vertical line moves.
48
49 Because the screen has a `grain' (there are insert/delete/scroll line
50 operations but no insert/delete/scroll column operations), it is efficient
51 break the update algorithm into two pieces: a first stage that does only line
52 moves, optimizing the end product of user-invoked insertions, deletions, and
53 scrolls; and a second phase (corresponding to the present doupdate code in
54 ncurses) that does only line transformations.
55
56 The common case we want hardware scrolling for is to handle line insertions
57 and deletions in screen-oriented text-editors.  This two-stage approach will
58 accomplish that at a low computation and code-size cost.
59
60                         LINE-MOVE COMPUTATION
61
62 Now, to a discussion of the line-move computation.
63
64 For expository purposes, consider the screen lines to be represented by
65 integers 0..23 (with the understanding that the value of 23 may vary).
66 Let a new line introduced by insertion, scrolling, or at the bottom of
67 the screen following a line delete be given the index -1.
68
69 Assume that the real screen starts with lines 0..23.  Now, we have
70 the following possible line-oriented operations on the screen:
71
72 Insertion: inserts a line at a given screen row, forcing all lines below
73 to scroll forward.  The last screen line is lost.  For example, an insertion
74 at line 5 would produce: 0..4 -1 5..23.
75
76 Deletion: deletes a line at a given screen row, forcing all lines below
77 to scroll forward.  The last screen line is made new.  For example, a deletion
78 at line 7 would produce: 0..6 8..23 -1.
79
80 Scroll up: move a range of lines up 1.  The bottom line of the range
81 becomes new.  For example, scrolling up the region from 9 to 14 will
82 produce 0..8 10..14 -1 15..23.
83
84 Scroll down: move a range of lines down 1.  The top line of the range
85 becomes new.  For example, scrolling down the region from 12 to 16 will produce
86 0..11 -1 12..15 17..23.
87
88 Now, an obvious property of all these operations is that they preserve the
89 order of old lines, though not their position in the sequence.
90
91 The key trick of this algorithm is that the original line indices described
92 above are actually maintained as _line[].oldindex fields in the window
93 structure, and stick to each line through scroll and insert/delete operations.
94
95 Thus, it is possible at update time to look at the oldnum fields and compute
96 an optimal set of il/dl/scroll operations that will take the real screen
97 lines to the virtual screen lines.  Once these vertical moves have been done,
98 we can hand off to the second stage of the update algorithm, which does line
99 transformations.
100
101 Note that the move computation does not need to have the full generality
102 of a diff algorithm (which it superficially resembles) because lines cannot
103 be moved out of order.
104
105                         THE ALGORITHM
106
107 The scrolling is done in two passes. The first pass is from top to bottom
108 scroling hunks UP. The second one is from bottom to top scrolling hunks DOWN.
109 Obviously enough, no lines to be scrolled will be destroyed. (lav)
110
111 HOW TO TEST THIS:
112
113 Use the following production:
114
115 hardscroll: hardscroll.c
116         $(CC) -g -DSCROLLDEBUG hardscroll.c -o hardscroll
117
118 Then just type scramble vectors and watch.  The following test loads are
119 a representative sample of cases:
120
121 -----------------------------  CUT HERE ------------------------------------
122 # No lines moved
123  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
124 #
125 # A scroll up
126  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 -1
127 #
128 # A scroll down
129 -1  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22
130 #
131 # An insertion (after line 12)
132  0  1  2  3  4  5  6  7  8  9 10 11 12 -1 13 14 15 16 17 18 19 20 21 22
133 #
134 # A simple deletion (line 10)
135  0  1  2  3  4  5  6  7  8  9  11 12 13 14 15 16 17 18 19 20 21 22 23 -1
136 #
137 # A more complex case
138 -1 -1 -1 -1 -1  3  4  5  6  7  -1 -1  8  9 10 11 12 13 14 15 16 17 -1 -1
139 -----------------------------  CUT HERE ------------------------------------
140
141 AUTHOR
142     Eric S. Raymond <esr@snark.thyrsus.com>, November 1994
143     New algorithm by Alexander V. Lukyanov <lav@yars.free.net>, Aug 1997
144
145 *****************************************************************************/
146
147 #include <curses.priv.h>
148
149 MODULE_ID("$Id: hardscroll.c,v 1.29 1998/02/11 12:13:57 tom Exp $")
150
151 #if defined(SCROLLDEBUG) || defined(HASHDEBUG)
152 int oldnums[MAXLINES];
153 #define OLDNUM(n)       oldnums[n]
154 #undef T
155 #define T(x)            (void) printf x ; (void) putchar('\n');
156 #else
157 #include <curses.h>
158 #define OLDNUM(n)       newscr->_line[n].oldindex
159 #ifndef _NEWINDEX
160 #define _NEWINDEX       -1
161 #endif /* _NEWINDEX */
162 #endif /* defined(SCROLLDEBUG) || defined(HASHDEBUG) */
163
164
165 void _nc_scroll_optimize(void)
166 /* scroll optimization to transform curscr to newscr */
167 {
168     int i;
169     int start, end, shift;
170
171     TR(TRACE_ICALLS, ("_nc_scroll_optimize() begins"));
172
173 #ifdef TRACE
174     if (_nc_tracing & (TRACE_UPDATE | TRACE_MOVE))
175         _nc_linedump();
176 #endif /* TRACE */
177
178     /* pass 1 - from top to bottom scrolling up */
179     for (i = 0; i < screen_lines; )
180     {
181         while (i < screen_lines && (OLDNUM(i) == _NEWINDEX || OLDNUM(i) <= i))
182             i++;
183         if (i >= screen_lines)
184             break;
185             
186         shift = OLDNUM(i) - i; /* shift > 0 */
187         start = i;
188         
189         i++;
190         while (i < screen_lines && OLDNUM(i) != _NEWINDEX && OLDNUM(i) - i == shift)
191             i++;
192         end = i-1 + shift;
193
194         TR(TRACE_UPDATE | TRACE_MOVE, ("scroll [%d, %d] by %d", start, end, shift));
195 #if !defined(SCROLLDEBUG) && !defined(HASHDEBUG)
196         if (_nc_scrolln(shift, start, end, screen_lines - 1) == ERR)
197         {
198             TR(TRACE_UPDATE | TRACE_MOVE, ("unable to scroll"));
199             continue;
200         }
201 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */
202     }
203
204     /* pass 2 - from bottom to top scrolling down */
205     for (i = screen_lines-1; i >= 0; )
206     {
207         while (i >= 0 && (OLDNUM(i) == _NEWINDEX || OLDNUM(i) >= i))
208             i--;
209         if (i < 0)
210             break;
211             
212         shift = OLDNUM(i) - i; /* shift < 0 */
213         end = i;
214         
215         i--;
216         while (i >= 0 && OLDNUM(i) != _NEWINDEX && OLDNUM(i) - i == shift)
217             i--;
218         start = i+1 - (-shift);
219
220         TR(TRACE_UPDATE | TRACE_MOVE, ("scroll [%d, %d] by %d", start, end, shift));
221 #if !defined(SCROLLDEBUG) && !defined(HASHDEBUG)
222         if (_nc_scrolln(shift, start, end, screen_lines - 1) == ERR)
223         {
224             TR(TRACE_UPDATE | TRACE_MOVE, ("unable to scroll"));
225             continue;
226         }
227 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */
228     }
229 }
230
231 #if defined(TRACE) || defined(SCROLLDEBUG)
232 void _nc_linedump(void)
233 /* dump the state of the real and virtual oldnum fields */
234 {
235     static size_t have;
236     static char *buf;
237
238     int n;
239     size_t      want = (screen_lines + 1) * 4;
240
241     if (have < want)
242         buf = malloc(have = want);
243
244     (void) strcpy(buf, "virt");
245     for (n = 0; n < screen_lines; n++)
246         (void) sprintf(buf + strlen(buf), " %02d", OLDNUM(n));
247     TR(TRACE_UPDATE | TRACE_MOVE, (buf));
248 #if NO_LEAKS
249     free(buf);
250     have = 0;
251 #endif
252 }
253 #endif /* defined(TRACE) || defined(SCROLLDEBUG) */
254
255 #ifdef SCROLLDEBUG
256
257 int
258 main(int argc GCC_UNUSED, char *argv[] GCC_UNUSED)
259 {
260     char        line[BUFSIZ], *st;
261
262 #ifdef TRACE
263     _nc_tracing = TRACE_MOVE;
264 #endif
265     for (;;)
266     {
267         int     n;
268
269         for (n = 0; n < screen_lines; n++)
270             oldnums[n] = _NEWINDEX;
271
272         /* grab the test vector */
273         if (fgets(line, sizeof(line), stdin) == (char *)NULL)
274             exit(EXIT_SUCCESS);
275
276         /* parse it */
277         n = 0;
278         if (line[0] == '#')
279         {
280             (void) fputs(line, stderr);
281             continue;
282         }
283         st = strtok(line, " ");
284         do {
285             oldnums[n++] = atoi(st);
286         } while
287             ((st = strtok((char *)NULL, " ")) != 0);
288
289         /* display it */
290         (void) fputs("Initial input:\n", stderr);
291         _nc_linedump();
292
293         _nc_scroll_optimize();
294     }
295 }
296
297 #endif /* SCROLLDEBUG */
298
299 /* hardscroll.c ends here */