1 /* Calculate what line insertion or deletion to do, and do it,
2 Copyright (C) 1985, 1986, 1990 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #include "dispextern.h"
27 extern struct display_line
**ophys_lines
;
29 #define max(a, b) ((a) > (b) ? (a) : (b))
30 #define min(a, b) ((a) < (b) ? (a) : (b))
32 /* All costs measured in characters.
33 So no cost can exceed the area of a screen, measured in characters.
34 Let's hope this is never more than 15000 characters. */
36 #define INFINITY 15000
40 /* Cost of outputting through this line
41 if no insert/delete is done just above it. */
43 /* Cost of outputting through this line
44 if an insert is done just above it. */
46 /* Cost of outputting through this line
47 if a delete is done just above it. */
49 /* Number of inserts so far in this run of inserts,
50 for the cost in insertcost. */
52 /* Number of deletes so far in this run of deletes,
53 for the cost in deletecost. */
57 /* See do_line_insertion_deletion_costs for info on these arrays. */
60 static int *insert_line_cost
;
61 static int *delete_line_cost
;
62 static int *insert_n_lines_cost
;
63 static int *delete_n_lines_cost
;
67 /* Determine, in matrix[i,j], the cost of updating the first j old lines
68 into the first i new lines.
69 This involves using insert or delete somewhere if i != j.
70 For each matrix elements, three kinds of costs are recorded:
71 the smallest cost that ends with an insert, the smallest
72 cost that ends with a delete, and the smallest cost that
73 ends with neither one. These are kept separate because
74 on some terminals the cost of doing an insert varies
75 depending on whether one was just done, etc. */
77 /* draw_cost[VPOS] is the cost of outputting new line at VPOS.
78 old_hash[VPOS] is the hash code of the old line at VPOS.
79 new_hash[VPOS] is the hash code of the new line at VPOS.
80 Note that these are not true screen vpos's, but relative
81 to the place at which the first mismatch between old and
82 new contents appears. */
85 calculate_scrolling (screen
, matrix
, window_size
, lines_below
,
86 draw_cost
, old_hash
, new_hash
,
89 /* matrix is of size window_size + 1 on each side. */
90 struct matrix_elt
*matrix
;
98 int screen_height
= SCREEN_HEIGHT (screen
);
99 register struct matrix_elt
*p
, *p1
;
100 register int cost
, cost1
;
102 int lines_moved
= window_size
+ (scroll_region_ok
? 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 = &SCREEN_INSERT_COST (screen
)[screen_height
- 1 - lines_moved
];
108 int *first_delete_cost
109 = &SCREEN_DELETE_COST (screen
)[screen_height
- 1 - lines_moved
];
110 int *next_insert_cost
111 = &SCREEN_INSERTN_COST (screen
)[screen_height
- 1 - lines_moved
];
112 int *next_delete_cost
113 = &SCREEN_DELETEN_COST (screen
)[screen_height
- 1 - lines_moved
];
115 /* Discourage long scrolls on fast lines.
116 Don't scroll nearly a full screen height unless it saves
117 at least 1/4 second. */
118 int extra_cost
= baud_rate
/ (10 * 4 * SCREEN_HEIGHT (screen
));
120 /* initialize the top left corner of the matrix */
121 matrix
->writecost
= 0;
122 matrix
->insertcost
= INFINITY
;
123 matrix
->deletecost
= INFINITY
;
124 matrix
->insertcount
= 0;
125 matrix
->deletecount
= 0;
127 /* initialize the left edge of the matrix */
128 cost
= first_insert_cost
[1] - next_insert_cost
[1];
129 for (i
= 1; i
<= window_size
; i
++)
131 p
= matrix
+ i
* (window_size
+ 1);
132 cost
+= draw_cost
[i
] + next_insert_cost
[i
] + extra_cost
;
133 p
->insertcost
= cost
;
134 p
->writecost
= INFINITY
;
135 p
->deletecost
= INFINITY
;
140 /* initialize the top edge of the matrix */
141 cost
= first_delete_cost
[1] - next_delete_cost
[1];
142 for (j
= 1; j
<= window_size
; j
++)
144 cost
+= next_delete_cost
[j
];
145 matrix
[j
].deletecost
= cost
;
146 matrix
[j
].writecost
= INFINITY
;
147 matrix
[j
].insertcost
= INFINITY
;
148 matrix
[j
].deletecount
= j
;
149 matrix
[j
].insertcount
= 0;
152 /* `i' represents the vpos among new screen contents.
153 `j' represents the vpos among the old screen contents. */
154 p
= matrix
+ window_size
+ 2; /* matrix [1, 1] */
155 for (i
= 1; i
<= window_size
; i
++, p
++)
156 for (j
= 1; j
<= window_size
; j
++, p
++)
158 /* p contains the address of matrix [i, j] */
160 /* First calculate the cost assuming we do
161 not insert or delete above this line.
162 That is, if we update through line i-1
163 based on old lines through j-1,
164 and then just change old line j to new line i. */
165 p1
= p
- window_size
- 2; /* matrix [i-1, j-1] */
166 cost
= p1
->writecost
;
167 if (cost
> p1
->insertcost
)
168 cost
= p1
->insertcost
;
169 if (cost
> p1
->deletecost
)
170 cost
= p1
->deletecost
;
171 if (old_hash
[j
] != new_hash
[i
])
172 cost
+= draw_cost
[i
];
175 /* Calculate the cost if we do an insert-line
176 before outputting this line.
177 That is, we update through line i-1
178 based on old lines through j,
179 do an insert-line on line i,
180 and then output line i from scratch,
181 leaving old lines starting from j for reuse below. */
182 p1
= p
- window_size
- 1; /* matrix [i-1, j] */
183 /* No need to think about doing a delete followed
184 immediately by an insert. It cannot be as good
185 as not doing either of them. */
186 if (free_at_end
== i
)
188 cost
= p1
->writecost
;
189 cost1
= p1
->insertcost
;
193 cost
= p1
->writecost
+ first_insert_cost
[i
];
194 if (p1
->insertcount
> i
)
196 cost1
= p1
->insertcost
+ next_insert_cost
[i
- p1
->insertcount
];
198 p
->insertcost
= min (cost
, cost1
) + draw_cost
[i
] + extra_cost
;
199 p
->insertcount
= (cost
< cost1
) ? 1 : p1
->insertcount
+ 1;
200 if (p
->insertcount
> i
)
203 /* Calculate the cost if we do a delete line after
204 outputting this line.
205 That is, we update through line i
206 based on old lines through j-1,
207 and throw away old line j. */
208 p1
= p
- 1; /* matrix [i, j-1] */
209 /* No need to think about doing an insert followed
210 immediately by a delete. */
211 if (free_at_end
== i
)
213 cost
= p1
->writecost
;
214 cost1
= p1
->deletecost
;
218 cost
= p1
->writecost
+ first_delete_cost
[i
];
219 cost1
= p1
->deletecost
+ next_delete_cost
[i
];
221 p
->deletecost
= min (cost
, cost1
);
222 p
->deletecount
= (cost
< cost1
) ? 1 : p1
->deletecount
+ 1;
226 /* Perform insert-lines and delete-lines operations
227 according to the costs in the matrix.
228 Updates the contents of the screen to record what was done. */
231 do_scrolling (screen
, matrix
, window_size
, unchanged_at_top
)
233 struct matrix_elt
*matrix
;
235 int unchanged_at_top
;
237 register struct matrix_elt
*p
;
239 register struct screen_glyphs
*current_screen
;
240 /* temp_screen->enable[i] means line i has been moved to current_screen. */
241 register struct screen_glyphs
*temp_screen
;
242 struct queue
{ int count
, pos
; } *queue
;
243 int offset
= unchanged_at_top
;
249 queue
= (struct queue
*) alloca (SCREEN_HEIGHT (screen
)
250 * sizeof (struct queue
));
252 current_screen
= SCREEN_CURRENT_GLYPHS (screen
);
253 temp_screen
= SCREEN_TEMP_GLYPHS (screen
);
255 bcopy (current_screen
->glyphs
, temp_screen
->glyphs
,
256 current_screen
->height
* sizeof (GLYPH
*));
257 bcopy (current_screen
->used
, temp_screen
->used
,
258 current_screen
->height
* sizeof (int));
259 bcopy (current_screen
->highlight
, temp_screen
->highlight
,
260 current_screen
->height
* sizeof (char));
261 bzero (temp_screen
->enable
, temp_screen
->height
* sizeof (char));
262 bcopy (current_screen
->bufp
, temp_screen
->bufp
,
263 current_screen
->height
* sizeof (int));
265 #ifdef HAVE_X_WINDOWS
266 if (SCREEN_IS_X (screen
))
268 bcopy (current_screen
->nruns
, temp_screen
->nruns
,
269 current_screen
->height
* sizeof (int));
270 bcopy (current_screen
->face_list
, temp_screen
->face_list
,
271 current_screen
->height
* sizeof (struct run
*));
272 bcopy (current_screen
->top_left_x
, temp_screen
->top_left_x
,
273 current_screen
->height
* sizeof (short));
274 bcopy (current_screen
->top_left_y
, temp_screen
->top_left_y
,
275 current_screen
->height
* sizeof (short));
276 bcopy (current_screen
->pix_width
, temp_screen
->pix_width
,
277 current_screen
->height
* sizeof (short));
278 bcopy (current_screen
->pix_height
, temp_screen
->pix_height
,
279 current_screen
->height
* sizeof (short));
285 while (i
> 0 || j
> 0)
287 p
= matrix
+ i
* (window_size
+ 1) + j
;
289 if (tem
< p
->writecost
&& tem
< p
->deletecost
)
291 /* Insert should be done at vpos i-1, plus maybe some before */
292 queue
[qi
].count
= p
->insertcount
;
294 queue
[qi
++].pos
= i
+ unchanged_at_top
;
296 else if (p
->deletecost
< p
->writecost
)
298 /* Old line at vpos j-1, and maybe some before it,
303 set_terminal_window (window_size
+ unchanged_at_top
);
306 ins_del_lines (j
+ unchanged_at_top
, - p
->deletecount
);
310 /* Best thing done here is no insert or delete */
311 /* Old line at vpos j-1 ends up at vpos i-1 */
312 current_screen
->glyphs
[i
+ offset
- 1]
313 = temp_screen
->glyphs
[j
+ offset
- 1];
314 current_screen
->used
[i
+ offset
- 1]
315 = temp_screen
->used
[j
+ offset
- 1];
316 current_screen
->highlight
[i
+ offset
- 1]
317 = temp_screen
->highlight
[j
+ offset
- 1];
319 temp_screen
->enable
[j
+ offset
- 1] = 1;
327 set_terminal_window (window_size
+ unchanged_at_top
);
331 /* Now do all insertions */
333 next
= unchanged_at_top
;
334 for (i
= qi
- 1; i
>= 0; i
--)
336 ins_del_lines (queue
[i
].pos
, queue
[i
].count
);
338 /* Mark the inserted lines as clear,
339 and put into them the line-contents strings
340 that were discarded during the deletions.
341 Those are the ones for which temp_screen->enable was not set. */
343 for (j
= tem
+ queue
[i
].count
- 1; j
>= tem
; j
--)
345 current_screen
->enable
[j
] = 0;
346 while (temp_screen
->enable
[next
])
348 current_screen
->glyphs
[j
] = temp_screen
->glyphs
[next
++];
353 set_terminal_window (0);
357 scrolling_1 (screen
, window_size
, unchanged_at_top
, unchanged_at_bottom
,
358 draw_cost
, old_hash
, new_hash
, free_at_end
)
360 int window_size
, unchanged_at_top
, unchanged_at_bottom
;
366 struct matrix_elt
*matrix
;
367 matrix
= ((struct matrix_elt
*)
368 alloca ((window_size
+ 1) * (window_size
+ 1) * sizeof *matrix
));
370 calculate_scrolling (screen
, matrix
, window_size
, unchanged_at_bottom
,
371 draw_cost
, old_hash
, new_hash
,
373 do_scrolling (screen
, matrix
, window_size
, unchanged_at_top
);
376 /* Return number of lines in common between current and desired screen contents
377 described to us only as vectors of hash codes OLDHASH and NEWHASH.
378 Consider only vpos range START to END (not including END).
379 Ignore short lines on the assumption that
380 avoiding redrawing such a line will have little weight. */
383 scrolling_max_lines_saved (start
, end
, oldhash
, newhash
, cost
)
385 int *oldhash
, *newhash
, *cost
;
387 struct { int hash
; int count
; } lines
[01000];
389 register int matchcount
= 0;
393 /* Compute a threshold which is 1/4 of average length of these lines. */
395 for (i
= start
; i
< end
; i
++)
396 avg_length
+= cost
[i
];
398 avg_length
/= end
- start
;
399 threshold
= avg_length
/ 4;
401 bzero (lines
, sizeof lines
);
403 /* Put new lines' hash codes in hash table.
404 Ignore lines shorter than the threshold.
405 Thus, if the lines that are in common
406 are mainly the ones that are short,
408 for (i
= start
; i
< end
; i
++)
410 if (cost
[i
] > threshold
)
412 h
= newhash
[i
] & 0777;
413 lines
[h
].hash
= newhash
[i
];
418 /* Look up old line hash codes in the hash table.
419 Count number of matches between old lines and new. */
421 for (i
= start
; i
< end
; i
++)
423 h
= oldhash
[i
] & 0777;
424 if (oldhash
[i
] == lines
[h
].hash
)
427 if (--lines
[h
].count
== 0)
435 /* Return a measure of the cost of moving the lines
436 starting with vpos FROM, up to but not including vpos TO,
437 down by AMOUNT lines (AMOUNT may be negative).
438 These are the same arguments that might be given to
439 scroll_screen_lines to perform this scrolling. */
441 scroll_cost (screen
, from
, to
, amount
)
443 int from
, to
, amount
;
445 /* Compute how many lines, at bottom of screen,
446 will not be involved in actual motion. */
449 int height
= SCREEN_HEIGHT (screen
);
454 if (! scroll_region_ok
)
463 from
= temp
+ amount
;
467 offset
= height
- limit
;
470 (SCREEN_INSERT_COST (screen
)[offset
+ from
]
471 + (amount
- 1) * SCREEN_INSERTN_COST (screen
)[offset
+ from
]
472 + SCREEN_DELETEN_COST (screen
)[offset
+ to
]
473 + (amount
- 1) * SCREEN_DELETE_COST (screen
)[offset
+ to
]);
476 /* Calculate the line insertion/deletion
477 overhead and multiply factor values */
480 line_ins_del (screen
, ov1
, pf1
, ovn
, pfn
, ov
, mf
)
484 register int *ov
, *mf
;
487 register int screen_height
= SCREEN_HEIGHT (screen
);
488 register int insert_overhead
= ov1
* 10;
489 register int next_insert_cost
= ovn
* 10;
491 for (i
= screen_height
-1; i
>= 0; i
--)
493 mf
[i
] = next_insert_cost
/ 10;
494 next_insert_cost
+= pfn
;
495 ov
[i
] = (insert_overhead
+ next_insert_cost
) / 10;
496 insert_overhead
+= pf1
;
501 ins_del_costs (screen
,
502 one_line_string
, multi_string
,
503 setup_string
, cleanup_string
,
504 costvec
, ncostvec
, coefficient
)
506 char *one_line_string
, *multi_string
;
507 char *setup_string
, *cleanup_string
;
508 int *costvec
, *ncostvec
;
512 line_ins_del (screen
,
513 string_cost (multi_string
) * coefficient
,
514 per_line_cost (multi_string
) * coefficient
,
515 0, 0, costvec
, ncostvec
);
516 else if (one_line_string
)
517 line_ins_del (screen
,
518 string_cost (setup_string
) + string_cost (cleanup_string
), 0,
519 string_cost (one_line_string
),
520 per_line_cost (one_line_string
),
523 line_ins_del (screen
,
528 /* Calculate the insert and delete line costs.
529 Note that this is done even when running with a window system
530 because we want to know how long scrolling takes (and avoid it).
531 This must be redone whenever the screen height changes.
533 We keep the ID costs in a precomputed array based on the position
534 at which the I or D is performed. Also, there are two kinds of ID
535 costs: the "once-only" and the "repeated". This is to handle both
536 those terminals that are able to insert N lines at a time (once-
537 only) and those that must repeatedly insert one line.
539 The cost to insert N lines at line L is
540 [tt.t_ILov + (screen_height + 1 - L) * tt.t_ILpf] +
541 N * [tt.t_ILnov + (screen_height + 1 - L) * tt.t_ILnpf]
543 ILov represents the basic insert line overhead. ILpf is the padding
544 required to allow the terminal time to move a line: insertion at line
545 L changes (screen_height + 1 - L) lines.
547 The first bracketed expression above is the overhead; the second is
548 the multiply factor. Both are dependent only on the position at
549 which the insert is performed. We store the overhead in
550 SCREEN_INSERT_COST (screen) and the multiply factor in
551 SCREEN_INSERTN_COST (screen). Note however that any insertion
552 must include at least one multiply factor. Rather than compute this
553 as SCREEN_INSERT_COST (screen)[line]+SCREEN_INSERTN_COST (screen)[line],
554 we add SCREEN_INSERTN_COST (screen) into SCREEN_INSERT_COST (screen).
555 This is reasonable because of the particular algorithm used in calcM.
557 Deletion is essentially the same as insertion.
560 do_line_insertion_deletion_costs (screen
,
561 ins_line_string
, multi_ins_string
,
562 del_line_string
, multi_del_string
,
563 setup_string
, cleanup_string
, coefficient
)
565 char *ins_line_string
, *multi_ins_string
;
566 char *del_line_string
, *multi_del_string
;
567 char *setup_string
, *cleanup_string
;
570 if (SCREEN_INSERT_COST (screen
) != 0)
572 SCREEN_INSERT_COST (screen
) =
573 (int *) xrealloc (SCREEN_INSERT_COST (screen
),
574 SCREEN_HEIGHT (screen
) * sizeof (int));
575 SCREEN_DELETEN_COST (screen
) =
576 (int *) xrealloc (SCREEN_DELETEN_COST (screen
),
577 SCREEN_HEIGHT (screen
) * sizeof (int));
578 SCREEN_INSERTN_COST (screen
) =
579 (int *) xrealloc (SCREEN_INSERTN_COST (screen
),
580 SCREEN_HEIGHT (screen
) * sizeof (int));
581 SCREEN_DELETE_COST (screen
) =
582 (int *) xrealloc (SCREEN_DELETE_COST (screen
),
583 SCREEN_HEIGHT (screen
) * sizeof (int));
587 SCREEN_INSERT_COST (screen
) =
588 (int *) xmalloc (SCREEN_HEIGHT (screen
) * sizeof (int));
589 SCREEN_DELETEN_COST (screen
) =
590 (int *) xmalloc (SCREEN_HEIGHT (screen
) * sizeof (int));
591 SCREEN_INSERTN_COST (screen
) =
592 (int *) xmalloc (SCREEN_HEIGHT (screen
) * sizeof (int));
593 SCREEN_DELETE_COST (screen
) =
594 (int *) xmalloc (SCREEN_HEIGHT (screen
) * sizeof (int));
597 ins_del_costs (screen
,
598 ins_line_string
, multi_ins_string
,
599 setup_string
, cleanup_string
,
600 SCREEN_INSERT_COST (screen
), SCREEN_INSERTN_COST (screen
),
602 ins_del_costs (screen
,
603 del_line_string
, multi_del_string
,
604 setup_string
, cleanup_string
,
605 SCREEN_DELETEN_COST (screen
), SCREEN_DELETE_COST (screen
),