Replace bcopy, bzero, bcmp by memcpy, memmove, memset, memcmp
[bpt/emacs.git] / src / scroll.c
1 /* Calculate what line insertion or deletion to do, and do it,
2 Copyright (C) 1985, 1986, 1990, 1993, 1994, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <setjmp.h>
25 #include "lisp.h"
26 #include "termchar.h"
27 #include "dispextern.h"
28 #include "keyboard.h"
29 #include "frame.h"
30 #include "window.h"
31 #include "termhooks.h"
32
33 /* All costs measured in characters.
34 So no cost can exceed the area of a frame, measured in characters.
35 Let's hope this is never more than 1000000 characters. */
36
37 #define INFINITY 1000000
38
39 struct matrix_elt
40 {
41 /* Cost of outputting through this line
42 if no insert/delete is done just above it. */
43 int writecost;
44 /* Cost of outputting through this line
45 if an insert is done just above it. */
46 int insertcost;
47 /* Cost of outputting through this line
48 if a delete is done just above it. */
49 int deletecost;
50 /* Number of inserts so far in this run of inserts,
51 for the cost in insertcost. */
52 unsigned char insertcount;
53 /* Number of deletes so far in this run of deletes,
54 for the cost in deletecost. */
55 unsigned char deletecount;
56 /* Number of writes so far since the last insert
57 or delete for the cost in writecost. */
58 unsigned char writecount;
59 };
60
61 static void do_direct_scrolling (struct frame *,
62 struct glyph_matrix *,
63 struct matrix_elt *,
64 int, int);
65 static void do_scrolling (struct frame *,
66 struct glyph_matrix *,
67 struct matrix_elt *,
68 int, int);
69
70 \f
71 /* Determine, in matrix[i,j], the cost of updating the first j old
72 lines into the first i new lines using the general scrolling method.
73 This involves using insert or delete somewhere if i != j.
74 For each matrix elements, three kinds of costs are recorded:
75 the smallest cost that ends with an insert, the smallest
76 cost that ends with a delete, and the smallest cost that
77 ends with neither one. These are kept separate because
78 on some terminals the cost of doing an insert varies
79 depending on whether one was just done, etc. */
80
81 /* draw_cost[VPOS] is the cost of outputting new line at VPOS.
82 old_hash[VPOS] is the hash code of the old line at VPOS.
83 new_hash[VPOS] is the hash code of the new line at VPOS.
84 Note that these are not true frame vpos's, but relative
85 to the place at which the first mismatch between old and
86 new contents appears. */
87
88 static void
89 calculate_scrolling (FRAME_PTR frame,
90 /* matrix is of size window_size + 1 on each side. */
91 struct matrix_elt *matrix,
92 int window_size, int lines_below,
93 int *draw_cost, int *old_hash, int *new_hash,
94 int free_at_end)
95 {
96 register int i, j;
97 int frame_lines = FRAME_LINES (frame);
98 register struct matrix_elt *p, *p1;
99 register int cost, cost1;
100
101 int lines_moved = window_size
102 + (FRAME_SCROLL_REGION_OK (frame) ? 0 : lines_below);
103 /* first_insert_cost[I] is the cost of doing the first insert-line
104 at the i'th line of the lines we are considering,
105 where I is origin 1 (as it is below). */
106 int *first_insert_cost
107 = &FRAME_INSERT_COST (frame)[frame_lines - 1 - lines_moved];
108 int *first_delete_cost
109 = &FRAME_DELETE_COST (frame)[frame_lines - 1 - lines_moved];
110 int *next_insert_cost
111 = &FRAME_INSERTN_COST (frame)[frame_lines - 1 - lines_moved];
112 int *next_delete_cost
113 = &FRAME_DELETEN_COST (frame)[frame_lines - 1 - lines_moved];
114
115 /* Discourage long scrolls on fast lines.
116 Don't scroll nearly a full frame height unless it saves
117 at least 1/4 second. */
118 int extra_cost = baud_rate / (10 * 4 * FRAME_LINES (frame));
119
120 if (baud_rate <= 0)
121 extra_cost = 1;
122
123 /* initialize the top left corner of the matrix */
124 matrix->writecost = 0;
125 matrix->insertcost = INFINITY;
126 matrix->deletecost = INFINITY;
127 matrix->insertcount = 0;
128 matrix->deletecount = 0;
129
130 /* initialize the left edge of the matrix */
131 cost = first_insert_cost[1] - next_insert_cost[1];
132 for (i = 1; i <= window_size; i++)
133 {
134 p = matrix + i * (window_size + 1);
135 cost += draw_cost[i] + next_insert_cost[i] + extra_cost;
136 p->insertcost = cost;
137 p->writecost = INFINITY;
138 p->deletecost = INFINITY;
139 p->insertcount = i;
140 p->deletecount = 0;
141 }
142
143 /* initialize the top edge of the matrix */
144 cost = first_delete_cost[1] - next_delete_cost[1];
145 for (j = 1; j <= window_size; j++)
146 {
147 cost += next_delete_cost[j];
148 matrix[j].deletecost = cost;
149 matrix[j].writecost = INFINITY;
150 matrix[j].insertcost = INFINITY;
151 matrix[j].deletecount = j;
152 matrix[j].insertcount = 0;
153 }
154
155 /* `i' represents the vpos among new frame contents.
156 `j' represents the vpos among the old frame contents. */
157 p = matrix + window_size + 2; /* matrix [1, 1] */
158 for (i = 1; i <= window_size; i++, p++)
159 for (j = 1; j <= window_size; j++, p++)
160 {
161 /* p contains the address of matrix [i, j] */
162
163 /* First calculate the cost assuming we do
164 not insert or delete above this line.
165 That is, if we update through line i-1
166 based on old lines through j-1,
167 and then just change old line j to new line i. */
168 p1 = p - window_size - 2; /* matrix [i-1, j-1] */
169 cost = p1->writecost;
170 if (cost > p1->insertcost)
171 cost = p1->insertcost;
172 if (cost > p1->deletecost)
173 cost = p1->deletecost;
174 if (old_hash[j] != new_hash[i])
175 cost += draw_cost[i];
176 p->writecost = cost;
177
178 /* Calculate the cost if we do an insert-line
179 before outputting this line.
180 That is, we update through line i-1
181 based on old lines through j,
182 do an insert-line on line i,
183 and then output line i from scratch,
184 leaving old lines starting from j for reuse below. */
185 p1 = p - window_size - 1; /* matrix [i-1, j] */
186 /* No need to think about doing a delete followed
187 immediately by an insert. It cannot be as good
188 as not doing either of them. */
189 if (free_at_end == i)
190 {
191 cost = p1->writecost;
192 cost1 = p1->insertcost;
193 }
194 else
195 {
196 cost = p1->writecost + first_insert_cost[i];
197 if ((int) p1->insertcount > i)
198 abort ();
199 cost1 = p1->insertcost + next_insert_cost[i - p1->insertcount];
200 }
201 p->insertcost = min (cost, cost1) + draw_cost[i] + extra_cost;
202 p->insertcount = (cost < cost1) ? 1 : p1->insertcount + 1;
203 if ((int) p->insertcount > i)
204 abort ();
205
206 /* Calculate the cost if we do a delete line after
207 outputting this line.
208 That is, we update through line i
209 based on old lines through j-1,
210 and throw away old line j. */
211 p1 = p - 1; /* matrix [i, j-1] */
212 /* No need to think about doing an insert followed
213 immediately by a delete. */
214 if (free_at_end == i)
215 {
216 cost = p1->writecost;
217 cost1 = p1->deletecost;
218 }
219 else
220 {
221 cost = p1->writecost + first_delete_cost[i];
222 cost1 = p1->deletecost + next_delete_cost[i];
223 }
224 p->deletecost = min (cost, cost1);
225 p->deletecount = (cost < cost1) ? 1 : p1->deletecount + 1;
226 }
227 }
228
229
230 \f
231 /* Perform insert-lines and delete-lines operations on CURRENT_MATRIX
232 according to the costs in MATRIX, using the general scrolling
233 method that is used if the terminal does not support the setting of
234 scroll windows (scroll_region_ok == 0).
235
236 WINDOW_SIZE is the number of lines being considered for scrolling
237 and UNCHANGED_AT_TOP is the vpos of the first line being
238 considered. These two arguments can specify any contiguous range
239 of lines. */
240
241 static void
242 do_scrolling (struct frame *frame, struct glyph_matrix *current_matrix, struct matrix_elt *matrix, int window_size, int unchanged_at_top)
243 {
244 struct matrix_elt *p;
245 int i, j, k;
246
247 /* Set to 1 if we have set a terminal window with
248 set_terminal_window. */
249 int terminal_window_p = 0;
250
251 /* A queue for line insertions to be done. */
252 struct queue { int count, pos; };
253 struct queue *queue_start
254 = (struct queue *) alloca (current_matrix->nrows * sizeof (struct queue));
255 struct queue *queue = queue_start;
256
257 char *retained_p = (char *) alloca (window_size * sizeof (char));
258 int *copy_from = (int *) alloca (window_size * sizeof (int));
259
260 /* Zero means line is empty. */
261 memset (retained_p, 0, window_size * sizeof (char));
262 for (k = 0; k < window_size; ++k)
263 copy_from[k] = -1;
264
265 #define CHECK_BOUNDS \
266 do \
267 { \
268 int k; \
269 for (k = 0; k < window_size; ++k) \
270 xassert (copy_from[k] == -1 \
271 || (copy_from[k] >= 0 && copy_from[k] < window_size)); \
272 } \
273 while (0);
274
275 /* When j is advanced, this corresponds to deleted lines.
276 When i is advanced, this corresponds to inserted lines. */
277 i = j = window_size;
278 while (i > 0 || j > 0)
279 {
280 p = matrix + i * (window_size + 1) + j;
281
282 if (p->insertcost < p->writecost && p->insertcost < p->deletecost)
283 {
284 /* Insert should be done at vpos i-1, plus maybe some before.
285 Queue the screen operation to be performed. */
286 queue->count = p->insertcount;
287 queue->pos = i + unchanged_at_top - p->insertcount;
288 ++queue;
289
290 /* By incrementing I, we leave room in the result rows
291 for the empty rows opened up. */
292 i -= p->insertcount;
293 }
294 else if (p->deletecost < p->writecost)
295 {
296 /* Old line at vpos j-1, and maybe some before it, should be
297 deleted. By decrementing J, we skip some lines in the
298 temp_rows which is equivalent to omitting these lines in
299 the result rows, thus deleting them. */
300 j -= p->deletecount;
301
302 /* Set the terminal window, if not done already. */
303 if (! terminal_window_p)
304 {
305 set_terminal_window (frame, window_size + unchanged_at_top);
306 terminal_window_p = 1;
307 }
308
309 /* Delete lines on the terminal. */
310 ins_del_lines (frame, j + unchanged_at_top, - p->deletecount);
311 }
312 else
313 {
314 /* Best thing done here is no insert or delete, i.e. a write. */
315 --i, --j;
316 xassert (i >= 0 && i < window_size);
317 xassert (j >= 0 && j < window_size);
318 copy_from[i] = j;
319 retained_p[j] = 1;
320
321 #if GLYPH_DEBUG
322 CHECK_BOUNDS;
323 #endif
324 }
325 }
326
327 /* Now do all insertions queued above. */
328 if (queue > queue_start)
329 {
330 int next = -1;
331
332 /* Set the terminal window if not yet done. */
333 if (!terminal_window_p)
334 {
335 set_terminal_window (frame, window_size + unchanged_at_top);
336 terminal_window_p = 1;
337 }
338
339 do
340 {
341 --queue;
342
343 /* Do the deletion on the terminal. */
344 ins_del_lines (frame, queue->pos, queue->count);
345
346 /* All lines in the range deleted become empty in the glyph
347 matrix. Assign to them glyph rows that are not retained.
348 K is the starting position of the deleted range relative
349 to the window we are working in. */
350 k = queue->pos - unchanged_at_top;
351 for (j = 0; j < queue->count; ++j)
352 {
353 /* Find the next row not retained. */
354 while (retained_p[++next])
355 ;
356
357 /* Record that this row is to be used for the empty
358 glyph row j. */
359 copy_from[k + j] = next;
360 }
361 }
362 while (queue > queue_start);
363
364 }
365
366 for (k = 0; k < window_size; ++k)
367 xassert (copy_from[k] >= 0 && copy_from[k] < window_size);
368
369 /* Perform the row swizzling. */
370 mirrored_line_dance (current_matrix, unchanged_at_top, window_size,
371 copy_from, retained_p);
372
373 /* Some sanity checks if GLYPH_DEBUG != 0. */
374 CHECK_MATRIX (current_matrix);
375
376 if (terminal_window_p)
377 set_terminal_window (frame, 0);
378 }
379
380 \f
381 /* Determine, in matrix[i,j], the cost of updating the first j
382 old lines into the first i new lines using the direct
383 scrolling method. When the old line and the new line have
384 different hash codes, the calculated cost of updating old
385 line j into new line i includes the cost of outputting new
386 line i, and if i != j, the cost of outputting the old line j
387 is also included, as a penalty for moving the line and then
388 erasing it. In addition, the cost of updating a sequence of
389 lines with constant i - j includes the cost of scrolling the
390 old lines into their new positions, unless i == j. Scrolling
391 is achieved by setting the screen window to avoid affecting
392 other lines below, and inserting or deleting lines at the top
393 of the scrolled region. The cost of scrolling a sequence of
394 lines includes the fixed cost of specifying a scroll region,
395 plus a variable cost which can depend upon the number of lines
396 involved and the distance by which they are scrolled, and an
397 extra cost to discourage long scrolls.
398
399 As reflected in the matrix, an insert or delete does not
400 correspond directly to the insertion or deletion which is
401 used in scrolling lines. An insert means that the value of i
402 has increased without a corresponding increase in the value
403 of j. A delete means that the value of j has increased
404 without a corresponding increase in the value of i. A write
405 means that i and j are both increased by the same amount, and
406 that the old lines will be moved to their new positions.
407
408 An insert following a delete is allowed only if i > j.
409 A delete following an insert is allowed only if i < j.
410 These restrictions ensure that the new lines in an insert
411 will always be blank as an effect of the neighboring writes.
412 Thus the calculated cost of an insert is simply the cost of
413 outputting the new line contents. The direct cost of a
414 delete is zero. Inserts and deletes indirectly affect the
415 total cost through their influence on subsequent writes. */
416
417 /* The vectors draw_cost, old_hash, and new_hash have the same
418 meanings here as in calculate_scrolling, and old_draw_cost
419 is the equivalent of draw_cost for the old line contents */
420
421 static void
422 calculate_direct_scrolling (FRAME_PTR frame,
423 /* matrix is of size window_size + 1 on each side. */
424 struct matrix_elt *matrix,
425 int window_size, int lines_below,
426 int *draw_cost, int *old_draw_cost,
427 int *old_hash, int *new_hash,
428 int free_at_end)
429 {
430 register int i, j;
431 int frame_lines = FRAME_LINES (frame);
432 register struct matrix_elt *p, *p1;
433 register int cost, cost1, delta;
434
435 /* first_insert_cost[-I] is the cost of doing the first insert-line
436 at a position I lines above the bottom line in the scroll window. */
437 int *first_insert_cost
438 = &FRAME_INSERT_COST (frame)[frame_lines - 1];
439 int *first_delete_cost
440 = &FRAME_DELETE_COST (frame)[frame_lines - 1];
441 int *next_insert_cost
442 = &FRAME_INSERTN_COST (frame)[frame_lines - 1];
443 int *next_delete_cost
444 = &FRAME_DELETEN_COST (frame)[frame_lines - 1];
445
446 int scroll_overhead;
447
448 /* Discourage long scrolls on fast lines.
449 Don't scroll nearly a full frame height unless it saves
450 at least 1/4 second. */
451 int extra_cost = baud_rate / (10 * 4 * FRAME_LINES (frame));
452
453 if (baud_rate <= 0)
454 extra_cost = 1;
455
456 /* Overhead of setting the scroll window, plus the extra cost
457 cost of scrolling by a distance of one. The extra cost is
458 added once for consistency with the cost vectors */
459 scroll_overhead
460 = FRAME_SCROLL_REGION_COST (frame) + extra_cost;
461
462 /* initialize the top left corner of the matrix */
463 matrix->writecost = 0;
464 matrix->insertcost = INFINITY;
465 matrix->deletecost = INFINITY;
466 matrix->writecount = 0;
467 matrix->insertcount = 0;
468 matrix->deletecount = 0;
469
470 /* initialize the left edge of the matrix */
471 cost = 0;
472 for (i = 1; i <= window_size; i++)
473 {
474 p = matrix + i * (window_size + 1);
475 cost += draw_cost[i];
476 p->insertcost = cost;
477 p->writecost = INFINITY;
478 p->deletecost = INFINITY;
479 p->insertcount = i;
480 p->writecount = 0;
481 p->deletecount = 0;
482 }
483
484 /* initialize the top edge of the matrix */
485 for (j = 1; j <= window_size; j++)
486 {
487 matrix[j].deletecost = 0;
488 matrix[j].writecost = INFINITY;
489 matrix[j].insertcost = INFINITY;
490 matrix[j].deletecount = j;
491 matrix[j].writecount = 0;
492 matrix[j].insertcount = 0;
493 }
494
495 /* `i' represents the vpos among new frame contents.
496 `j' represents the vpos among the old frame contents. */
497 p = matrix + window_size + 2; /* matrix [1, 1] */
498
499 for (i = 1; i <= window_size; i++, p++)
500 for (j = 1; j <= window_size; j++, p++)
501 {
502 /* p contains the address of matrix [i, j] */
503
504 /* First calculate the cost assuming we do
505 not insert or delete above this line.
506 That is, if we update through line i-1
507 based on old lines through j-1,
508 and then just change old line j to new line i.
509
510 Depending on which choice gives the lower cost,
511 this usually involves either scrolling a single line
512 or extending a sequence of scrolled lines, but
513 when i == j, no scrolling is required. */
514 p1 = p - window_size - 2; /* matrix [i-1, j-1] */
515 cost = p1->insertcost;
516 if (cost > p1->deletecost)
517 cost = p1->deletecost;
518 cost1 = p1->writecost;
519 if (i == j)
520 {
521 if (cost > cost1)
522 {
523 cost = cost1;
524 p->writecount = p1->writecount + 1;
525 }
526 else
527 p->writecount = 1;
528 if (old_hash[j] != new_hash[i])
529 {
530 cost += draw_cost[i];
531 }
532 }
533 else
534 {
535 if (i > j)
536 {
537 delta = i - j;
538
539 /* The cost added here for scrolling the first line by
540 a distance N includes the overhead of setting the
541 scroll window, the cost of inserting N lines at a
542 position N lines above the bottom line of the window,
543 and an extra cost which is proportional to N. */
544 cost += scroll_overhead + first_insert_cost[-delta] +
545 (delta-1) * (next_insert_cost[-delta] + extra_cost);
546
547 /* In the most general case, the insertion overhead and
548 the multiply factor can grow linearly as the distance
549 from the bottom of the window increases. The incremental
550 cost of scrolling an additional line depends upon the
551 rate of change of these two parameters. Each of these
552 growth rates can be determined by a simple difference.
553 To reduce the cumulative effects of rounding error, we
554 vary the position at which the difference is computed. */
555 cost1 += first_insert_cost[-j] - first_insert_cost[1-j] +
556 (delta-1) * (next_insert_cost[-j] - next_insert_cost[1-j]);
557 }
558 else
559 {
560 delta = j - i;
561 cost += scroll_overhead + first_delete_cost[-delta] +
562 (delta-1) * (next_delete_cost[-delta] + extra_cost);
563 cost1 += first_delete_cost[-i] - first_delete_cost[1-i] +
564 (delta-1) * ( next_delete_cost[-i] - next_delete_cost[1-i]);
565 }
566 if (cost1 < cost)
567 {
568 cost = cost1;
569 p->writecount = p1->writecount + 1;
570 }
571 else
572 p->writecount = 1;
573 if (old_hash[j] != new_hash[i])
574 {
575 cost += draw_cost[i] + old_draw_cost[j];
576 }
577 }
578 p->writecost = cost;
579
580 /* Calculate the cost if we do an insert-line
581 before outputting this line.
582 That is, we update through line i-1
583 based on old lines through j,
584 do an insert-line on line i,
585 and then output line i from scratch,
586 leaving old lines starting from j for reuse below. */
587 p1 = p - window_size - 1; /* matrix [i-1, j] */
588 cost = p1->writecost;
589 /* If i > j, an insert is allowed after a delete. */
590 if (i > j && p1->deletecost < cost)
591 cost = p1->deletecost;
592 if (p1->insertcost <= cost)
593 {
594 cost = p1->insertcost;
595 p->insertcount = p1->insertcount + 1;
596 }
597 else
598 p->insertcount = 1;
599 cost += draw_cost[i];
600 p->insertcost = cost;
601
602 /* Calculate the cost if we do a delete line after
603 outputting this line.
604 That is, we update through line i
605 based on old lines through j-1,
606 and throw away old line j. */
607 p1 = p - 1; /* matrix [i, j-1] */
608 cost = p1->writecost;
609 /* If i < j, a delete is allowed after an insert. */
610 if (i < j && p1->insertcost < cost)
611 cost = p1->insertcost;
612 cost1 = p1->deletecost;
613 if (p1->deletecost <= cost)
614 {
615 cost = p1->deletecost;
616 p->deletecount = p1->deletecount + 1;
617 }
618 else
619 p->deletecount = 1;
620 p->deletecost = cost;
621 }
622 }
623
624
625 \f
626 /* Perform insert-lines and delete-lines operations on CURRENT_MATRIX
627 according to the costs in MATRIX, using the direct scrolling method
628 which is used when the terminal supports setting a scroll window
629 (scroll_region_ok).
630
631 WINDOW_SIZE is the number of lines being considered for scrolling
632 and UNCHANGED_AT_TOP is the vpos of the first line being
633 considered. These two arguments can specify any contiguous range
634 of lines.
635
636 In the direct scrolling method, a new scroll window is selected
637 before each insertion or deletion, so that groups of lines can be
638 scrolled directly to their final vertical positions. This method
639 is described in more detail in calculate_direct_scrolling, where
640 the cost matrix for this approach is constructed. */
641
642 static void
643 do_direct_scrolling (frame, current_matrix, cost_matrix,
644 window_size, unchanged_at_top)
645 struct frame *frame;
646 struct glyph_matrix *current_matrix;
647 struct matrix_elt *cost_matrix;
648 int window_size;
649 int unchanged_at_top;
650 {
651 struct matrix_elt *p;
652 int i, j;
653
654 /* A queue of deletions and insertions to be performed. */
655 struct alt_queue { int count, pos, window; };
656 struct alt_queue *queue_start = (struct alt_queue *)
657 alloca (window_size * sizeof *queue_start);
658 struct alt_queue *queue = queue_start;
659
660 /* Set to 1 if a terminal window has been set with
661 set_terminal_window: */
662 int terminal_window_p = 0;
663
664 /* A nonzero value of write_follows indicates that a write has been
665 selected, allowing either an insert or a delete to be selected
666 next. When write_follows is zero, a delete cannot be selected
667 unless j < i, and an insert cannot be selected unless i < j.
668 This corresponds to a similar restriction (with the ordering
669 reversed) in calculate_direct_scrolling, which is intended to
670 ensure that lines marked as inserted will be blank. */
671 int write_follows_p = 1;
672
673 /* For each row in the new matrix what row of the old matrix it is. */
674 int *copy_from = (int *) alloca (window_size * sizeof (int));
675
676 /* Non-zero for each row in the new matrix that is retained from the
677 old matrix. Lines not retained are empty. */
678 char *retained_p = (char *) alloca (window_size * sizeof (char));
679
680 memset (retained_p, 0, window_size * sizeof (char));
681
682 /* Perform some sanity checks when GLYPH_DEBUG is on. */
683 CHECK_MATRIX (current_matrix);
684
685 /* We are working on the line range UNCHANGED_AT_TOP ...
686 UNCHANGED_AT_TOP + WINDOW_SIZE (not including) in CURRENT_MATRIX.
687 We step through lines in this range from the end to the start. I
688 is an index into new lines, j an index into old lines. The cost
689 matrix determines what to do for ranges of indices.
690
691 If i is decremented without also decrementing j, this corresponds
692 to inserting empty lines in the result. If j is decremented
693 without also decrementing i, this corresponds to omitting these
694 lines in the new rows, i.e. rows are deleted. */
695 i = j = window_size;
696
697 while (i > 0 || j > 0)
698 {
699 p = cost_matrix + i * (window_size + 1) + j;
700
701 if (p->insertcost < p->writecost
702 && p->insertcost < p->deletecost
703 && (write_follows_p || i < j))
704 {
705 /* Insert is cheaper than deleting or writing lines. Leave
706 a hole in the result display that will be filled with
707 empty lines when the queue is emptied. */
708 queue->count = 0;
709 queue->window = i;
710 queue->pos = i - p->insertcount;
711 ++queue;
712
713 i -= p->insertcount;
714 write_follows_p = 0;
715 }
716 else if (p->deletecost < p->writecost
717 && (write_follows_p || i > j))
718 {
719 /* Deleting lines is cheaper. By decrementing J, omit
720 deletecount lines from the original. */
721 write_follows_p = 0;
722 j -= p->deletecount;
723 }
724 else
725 {
726 /* One or more lines should be written. In the direct
727 scrolling method we do this by scrolling the lines to the
728 place they belong. */
729 int n_to_write = p->writecount;
730 write_follows_p = 1;
731 xassert (n_to_write > 0);
732
733 if (i > j)
734 {
735 /* Immediately insert lines */
736 set_terminal_window (frame, i + unchanged_at_top);
737 terminal_window_p = 1;
738 ins_del_lines (frame, j - n_to_write + unchanged_at_top, i - j);
739 }
740 else if (i < j)
741 {
742 /* Queue the deletion of a group of lines */
743 queue->pos = i - n_to_write + unchanged_at_top;
744 queue->window = j + unchanged_at_top;
745 queue->count = i - j;
746 ++queue;
747 }
748
749 while (n_to_write > 0)
750 {
751 --i, --j, --n_to_write;
752 copy_from[i] = j;
753 retained_p[j] = 1;
754 }
755 }
756 }
757
758 /* Do queued operations. */
759 if (queue > queue_start)
760 {
761 int next = -1;
762
763 do
764 {
765 --queue;
766 if (queue->count)
767 {
768 set_terminal_window (frame, queue->window);
769 terminal_window_p = 1;
770 ins_del_lines (frame, queue->pos, queue->count);
771 }
772 else
773 {
774 for (j = queue->window - 1; j >= queue->pos; --j)
775 {
776 while (retained_p[++next])
777 ;
778 copy_from[j] = next;
779 }
780 }
781 }
782 while (queue > queue_start);
783 }
784
785 /* Now, for each row I in the range of rows we are working on,
786 copy_from[i] gives the original line to copy to I, and
787 retained_p[copy_from[i]] is zero if line I in the new display is
788 empty. */
789 mirrored_line_dance (current_matrix, unchanged_at_top, window_size,
790 copy_from, retained_p);
791
792 if (terminal_window_p)
793 set_terminal_window (frame, 0);
794 }
795
796
797 \f
798 void
799 scrolling_1 (frame, window_size, unchanged_at_top, unchanged_at_bottom,
800 draw_cost, old_draw_cost, old_hash, new_hash, free_at_end)
801 FRAME_PTR frame;
802 int window_size, unchanged_at_top, unchanged_at_bottom;
803 int *draw_cost;
804 int *old_draw_cost;
805 int *old_hash;
806 int *new_hash;
807 int free_at_end;
808 {
809 struct matrix_elt *matrix;
810 matrix = ((struct matrix_elt *)
811 alloca ((window_size + 1) * (window_size + 1) * sizeof *matrix));
812
813 if (FRAME_SCROLL_REGION_OK (frame))
814 {
815 calculate_direct_scrolling (frame, matrix, window_size,
816 unchanged_at_bottom,
817 draw_cost, old_draw_cost,
818 old_hash, new_hash, free_at_end);
819 do_direct_scrolling (frame, frame->current_matrix,
820 matrix, window_size, unchanged_at_top);
821 }
822 else
823 {
824 calculate_scrolling (frame, matrix, window_size, unchanged_at_bottom,
825 draw_cost, old_hash, new_hash,
826 free_at_end);
827 do_scrolling (frame,
828 frame->current_matrix, matrix, window_size,
829 unchanged_at_top);
830 }
831 }
832
833
834 \f
835 /* Return number of lines in common between current and desired frame
836 contents described to us only as vectors of hash codes OLDHASH and
837 NEWHASH. Consider only vpos range START to END (not including
838 END). Ignore short lines on the assumption that avoiding redrawing
839 such a line will have little weight. */
840
841 int
842 scrolling_max_lines_saved (int start, int end, int *oldhash, int *newhash, int *cost)
843 {
844 struct { int hash; int count; } lines[01000];
845 register int i, h;
846 register int matchcount = 0;
847 int avg_length = 0;
848 int threshold;
849
850 /* Compute a threshold which is 1/4 of average length of these lines. */
851
852 for (i = start; i < end; i++)
853 avg_length += cost[i];
854
855 avg_length /= end - start;
856 threshold = avg_length / 4;
857
858 memset (lines, 0, sizeof lines);
859
860 /* Put new lines' hash codes in hash table. Ignore lines shorter
861 than the threshold. Thus, if the lines that are in common are
862 mainly the ones that are short, they won't count. */
863 for (i = start; i < end; i++)
864 {
865 if (cost[i] > threshold)
866 {
867 h = newhash[i] & 0777;
868 lines[h].hash = newhash[i];
869 lines[h].count++;
870 }
871 }
872
873 /* Look up old line hash codes in the hash table. Count number of
874 matches between old lines and new. */
875 for (i = start; i < end; i++)
876 {
877 h = oldhash[i] & 0777;
878 if (oldhash[i] == lines[h].hash)
879 {
880 matchcount++;
881 if (--lines[h].count == 0)
882 lines[h].hash = 0;
883 }
884 }
885
886 return matchcount;
887 }
888 \f
889 /* Return a measure of the cost of moving the lines starting with vpos
890 FROM, up to but not including vpos TO, down by AMOUNT lines (AMOUNT
891 may be negative). These are the same arguments that might be given
892 to scroll_frame_lines to perform this scrolling. */
893
894 int
895 scroll_cost (FRAME_PTR frame, int from, int to, int amount)
896 {
897 /* Compute how many lines, at bottom of frame,
898 will not be involved in actual motion. */
899 int limit = to;
900 int offset;
901 int height = FRAME_LINES (frame);
902
903 if (amount == 0)
904 return 0;
905
906 if (! FRAME_SCROLL_REGION_OK (frame))
907 limit = height;
908 else if (amount > 0)
909 limit += amount;
910
911 if (amount < 0)
912 {
913 int temp = to;
914 to = from + amount;
915 from = temp + amount;
916 amount = - amount;
917 }
918
919 offset = height - limit;
920
921 return
922 (FRAME_INSERT_COST (frame)[offset + from]
923 + (amount - 1) * FRAME_INSERTN_COST (frame)[offset + from]
924 + FRAME_DELETE_COST (frame)[offset + to]
925 + (amount - 1) * FRAME_DELETEN_COST (frame)[offset + to]);
926 }
927 \f
928 /* Calculate the line insertion/deletion
929 overhead and multiply factor values */
930
931 static void
932 line_ins_del (FRAME_PTR frame, int ov1, int pf1, int ovn, int pfn, register int *ov, register int *mf)
933 {
934 register int i;
935 register int frame_lines = FRAME_LINES (frame);
936 register int insert_overhead = ov1 * 10;
937 register int next_insert_cost = ovn * 10;
938
939 for (i = frame_lines-1; i >= 0; i--)
940 {
941 mf[i] = next_insert_cost / 10;
942 next_insert_cost += pfn;
943 ov[i] = (insert_overhead + next_insert_cost) / 10;
944 insert_overhead += pf1;
945 }
946 }
947
948 static void
949 ins_del_costs (FRAME_PTR frame,
950 char *one_line_string, char *multi_string,
951 char *setup_string, char *cleanup_string,
952 int *costvec, int *ncostvec,
953 int coefficient)
954 {
955 if (multi_string)
956 line_ins_del (frame,
957 string_cost (multi_string) * coefficient,
958 per_line_cost (multi_string) * coefficient,
959 0, 0, costvec, ncostvec);
960 else if (one_line_string)
961 line_ins_del (frame,
962 string_cost (setup_string) + string_cost (cleanup_string), 0,
963 string_cost (one_line_string),
964 per_line_cost (one_line_string),
965 costvec, ncostvec);
966 else
967 line_ins_del (frame,
968 9999, 0, 9999, 0,
969 costvec, ncostvec);
970 }
971
972 /* Calculate the insert and delete line costs.
973 Note that this is done even when running with a window system
974 because we want to know how long scrolling takes (and avoid it).
975 This must be redone whenever the frame height changes.
976
977 We keep the ID costs in a precomputed array based on the position
978 at which the I or D is performed. Also, there are two kinds of ID
979 costs: the "once-only" and the "repeated". This is to handle both
980 those terminals that are able to insert N lines at a time (once-
981 only) and those that must repeatedly insert one line.
982
983 The cost to insert N lines at line L is
984 [tt.t_ILov + (frame_lines + 1 - L) * tt.t_ILpf] +
985 N * [tt.t_ILnov + (frame_lines + 1 - L) * tt.t_ILnpf]
986
987 ILov represents the basic insert line overhead. ILpf is the padding
988 required to allow the terminal time to move a line: insertion at line
989 L changes (frame_lines + 1 - L) lines.
990
991 The first bracketed expression above is the overhead; the second is
992 the multiply factor. Both are dependent only on the position at
993 which the insert is performed. We store the overhead in
994 FRAME_INSERT_COST (frame) and the multiply factor in
995 FRAME_INSERTN_COST (frame). Note however that any insertion
996 must include at least one multiply factor. Rather than compute this
997 as FRAME_INSERT_COST (frame)[line]+FRAME_INSERTN_COST (frame)[line],
998 we add FRAME_INSERTN_COST (frame) into FRAME_INSERT_COST (frame).
999 This is reasonable because of the particular algorithm used in calcM.
1000
1001 Deletion is essentially the same as insertion.
1002 */
1003
1004 void
1005 do_line_insertion_deletion_costs (frame,
1006 ins_line_string, multi_ins_string,
1007 del_line_string, multi_del_string,
1008 setup_string, cleanup_string, coefficient)
1009 FRAME_PTR frame;
1010 char *ins_line_string, *multi_ins_string;
1011 char *del_line_string, *multi_del_string;
1012 char *setup_string, *cleanup_string;
1013 int coefficient;
1014 {
1015 if (FRAME_INSERT_COST (frame) != 0)
1016 {
1017 FRAME_INSERT_COST (frame) =
1018 (int *) xrealloc (FRAME_INSERT_COST (frame),
1019 FRAME_LINES (frame) * sizeof (int));
1020 FRAME_DELETEN_COST (frame) =
1021 (int *) xrealloc (FRAME_DELETEN_COST (frame),
1022 FRAME_LINES (frame) * sizeof (int));
1023 FRAME_INSERTN_COST (frame) =
1024 (int *) xrealloc (FRAME_INSERTN_COST (frame),
1025 FRAME_LINES (frame) * sizeof (int));
1026 FRAME_DELETE_COST (frame) =
1027 (int *) xrealloc (FRAME_DELETE_COST (frame),
1028 FRAME_LINES (frame) * sizeof (int));
1029 }
1030 else
1031 {
1032 FRAME_INSERT_COST (frame) =
1033 (int *) xmalloc (FRAME_LINES (frame) * sizeof (int));
1034 FRAME_DELETEN_COST (frame) =
1035 (int *) xmalloc (FRAME_LINES (frame) * sizeof (int));
1036 FRAME_INSERTN_COST (frame) =
1037 (int *) xmalloc (FRAME_LINES (frame) * sizeof (int));
1038 FRAME_DELETE_COST (frame) =
1039 (int *) xmalloc (FRAME_LINES (frame) * sizeof (int));
1040 }
1041
1042 ins_del_costs (frame,
1043 ins_line_string, multi_ins_string,
1044 setup_string, cleanup_string,
1045 FRAME_INSERT_COST (frame), FRAME_INSERTN_COST (frame),
1046 coefficient);
1047 ins_del_costs (frame,
1048 del_line_string, multi_del_string,
1049 setup_string, cleanup_string,
1050 FRAME_DELETE_COST (frame), FRAME_DELETEN_COST (frame),
1051 coefficient);
1052 }
1053
1054 /* arch-tag: cdb7149c-48e7-4793-a948-2786c8e45485
1055 (do not change this comment) */