use dynwind_begin and dynwind_end
[bpt/emacs.git] / src / dispnew.c
CommitLineData
4588ec20 1/* Updating of data structures for redisplay.
9311dcff 2
ba318903 3Copyright (C) 1985-1988, 1993-1995, 1997-2014 Free Software Foundation,
ab422c4d 4Inc.
4588ec20
JB
5
6This file is part of GNU Emacs.
7
9ec0b715 8GNU Emacs is free software: you can redistribute it and/or modify
4588ec20 9it under the terms of the GNU General Public License as published by
9ec0b715
GM
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
4588ec20
JB
12
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
9ec0b715 19along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
4588ec20 20
18160b98 21#include <config.h>
f162bcc3 22
406af475 23#include "sysstdio.h"
dfcf069d 24#include <unistd.h>
dfcf069d 25
47099d6f 26#include "lisp.h"
4588ec20 27#include "termchar.h"
a0879520 28/* cm.h must come after dispextern.h on Windows. */
fd2e066a
GV
29#include "dispextern.h"
30#include "cm.h"
83be827a 31#include "character.h"
e5560ff7 32#include "buffer.h"
d43721a2 33#include "keyboard.h"
502b9b64 34#include "frame.h"
428a555e 35#include "termhooks.h"
4588ec20
JB
36#include "window.h"
37#include "commands.h"
38#include "disptab.h"
39#include "indent.h"
d169fe39 40#include "intervals.h"
97cf50e7 41#include "blockinput.h"
dfcf069d 42#include "process.h"
4588ec20 43
58b2bb63 44#include "syssignal.h"
91f2d272 45#include "tparam.h"
a41f8bed 46
17a2cbbd
DC
47#ifdef HAVE_WINDOW_SYSTEM
48#include TERM_HEADER
49#endif /* HAVE_WINDOW_SYSTEM */
edfda783 50
3883a901
RS
51#include <errno.h>
52
8148369c 53#include <fpending.h>
43aac990 54#include <timespec.h>
c59592b3 55
a68089e4
EZ
56#ifdef WINDOWSNT
57#include "w32.h"
58#endif
5f5c8ee5
GM
59\f
60/* Structure to pass dimensions around. Used for character bounding
61 boxes, glyph matrix dimensions and alike. */
62
63struct dim
64{
65 int width;
66 int height;
67};
68
69\f
70/* Function prototypes. */
71
f57e2426 72static void update_frame_line (struct frame *, int);
2f7c71a1
AS
73static int required_matrix_height (struct window *);
74static int required_matrix_width (struct window *);
880e6158 75static void change_frame_size_1 (struct frame *, int, int, bool, bool, bool, bool);
d311d28c 76static void increment_row_positions (struct glyph_row *, ptrdiff_t, ptrdiff_t);
f57e2426
J
77static void build_frame_matrix_from_window_tree (struct glyph_matrix *,
78 struct window *);
79static void build_frame_matrix_from_leaf_window (struct glyph_matrix *,
80 struct window *);
f57e2426
J
81static void adjust_decode_mode_spec_buffer (struct frame *);
82static void fill_up_glyph_row_with_spaces (struct glyph_row *);
59ea14cd 83static void clear_window_matrices (struct window *, bool);
f57e2426 84static void fill_up_glyph_row_area_with_spaces (struct glyph_row *, int);
59ea14cd
PE
85static int scrolling_window (struct window *, bool);
86static bool update_window_line (struct window *, int, bool *);
f57e2426 87static void mirror_make_current (struct window *, int);
e509cfa6 88#ifdef GLYPH_DEBUG
f57e2426
J
89static void check_matrix_pointers (struct glyph_matrix *,
90 struct glyph_matrix *);
b96fd3e8 91#endif
f57e2426 92static void mirror_line_dance (struct window *, int, int, int *, char *);
59ea14cd
PE
93static bool update_window_tree (struct window *, bool);
94static bool update_window (struct window *, bool);
d13adf6d 95static bool update_frame_1 (struct frame *, bool, bool, bool);
59ea14cd 96static bool scrolling (struct frame *);
f57e2426 97static void set_window_cursor_after_update (struct window *);
f57e2426
J
98static void adjust_frame_glyphs_for_window_redisplay (struct frame *);
99static void adjust_frame_glyphs_for_frame_redisplay (struct frame *);
44ad1cf7 100static void set_window_update_flags (struct window *w, bool on_p);
5f5c8ee5 101
59ea14cd 102/* True means last display completed. False means it was preempted. */
4588ec20 103
59ea14cd 104bool display_completed;
4588ec20 105
e7067d00 106Lisp_Object Qdisplay_table, Qredisplay_dont_pause;
5f5c8ee5 107
59ea14cd 108/* True means SIGWINCH happened when not safe. */
4588ec20 109
59ea14cd 110static bool delayed_size_change;
4588ec20 111
5f5c8ee5 112/* A glyph for a space. */
4588ec20 113
5f5c8ee5 114struct glyph space_glyph;
4588ec20 115
f0b79313
DA
116#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
117
5f5c8ee5
GM
118/* Counts of allocated structures. These counts serve to diagnose
119 memory leaks and double frees. */
4588ec20 120
5bf46f05
PE
121static int glyph_matrix_count;
122static int glyph_pool_count;
4588ec20 123
f0b79313
DA
124#endif /* GLYPH_DEBUG and ENABLE_CHECKING */
125
5f5c8ee5
GM
126/* If non-null, the frame whose frame matrices are manipulated. If
127 null, window matrices are worked on. */
4588ec20 128
5f5c8ee5 129static struct frame *frame_matrix_frame;
4588ec20 130
177c0ea7 131/* Convert vpos and hpos from frame to window and vice versa.
5f5c8ee5 132 This may only be used for terminal frames. */
836d2cde 133
e509cfa6 134#ifdef GLYPH_DEBUG
4588ec20 135
f57e2426
J
136static int window_to_frame_vpos (struct window *, int);
137static int window_to_frame_hpos (struct window *, int);
5f5c8ee5
GM
138#define WINDOW_TO_FRAME_VPOS(W, VPOS) window_to_frame_vpos ((W), (VPOS))
139#define WINDOW_TO_FRAME_HPOS(W, HPOS) window_to_frame_hpos ((W), (HPOS))
4588ec20 140
9b0e97aa
GM
141/* One element of the ring buffer containing redisplay history
142 information. */
143
144struct redisplay_history
145{
146 char trace[512 + 100];
147};
148
149/* The size of the history buffer. */
150
151#define REDISPLAY_HISTORY_SIZE 30
152
153/* The redisplay history buffer. */
154
155static struct redisplay_history redisplay_history[REDISPLAY_HISTORY_SIZE];
156
157/* Next free entry in redisplay_history. */
158
159static int history_idx;
160
161/* A tick that's incremented each time something is added to the
162 history. */
163
dfd153ae 164static uprintmax_t history_tick;
6a8aec41 165\f
9b0e97aa
GM
166/* Add to the redisplay history how window W has been displayed.
167 MSG is a trace containing the information how W's glyph matrix
59ea14cd 168 has been constructed. PAUSED_P means that the update
9b0e97aa
GM
169 has been interrupted for pending input. */
170
171static void
59ea14cd 172add_window_display_history (struct window *w, const char *msg, bool paused_p)
9b0e97aa
GM
173{
174 char *buf;
29abe551 175 void *ptr = w;
177c0ea7 176
9b0e97aa
GM
177 if (history_idx >= REDISPLAY_HISTORY_SIZE)
178 history_idx = 0;
179 buf = redisplay_history[history_idx].trace;
180 ++history_idx;
177c0ea7 181
8666506e
PE
182 snprintf (buf, sizeof redisplay_history[0].trace,
183 "%"pMu": window %p (`%s')%s\n%s",
184 history_tick++,
29abe551 185 ptr,
e74aeda8
DA
186 ((BUFFERP (w->contents)
187 && STRINGP (BVAR (XBUFFER (w->contents), name)))
188 ? SSDATA (BVAR (XBUFFER (w->contents), name))
8666506e
PE
189 : "???"),
190 paused_p ? " ***paused***" : "",
191 msg);
9b0e97aa
GM
192}
193
194
195/* Add to the redisplay history that frame F has been displayed.
59ea14cd 196 PAUSED_P means that the update has been interrupted for
9b0e97aa
GM
197 pending input. */
198
199static void
59ea14cd 200add_frame_display_history (struct frame *f, bool paused_p)
9b0e97aa
GM
201{
202 char *buf;
29abe551 203 void *ptr = f;
177c0ea7 204
9b0e97aa
GM
205 if (history_idx >= REDISPLAY_HISTORY_SIZE)
206 history_idx = 0;
207 buf = redisplay_history[history_idx].trace;
208 ++history_idx;
177c0ea7 209
dfd153ae 210 sprintf (buf, "%"pMu": update frame %p%s",
9b0e97aa 211 history_tick++,
29abe551 212 ptr, paused_p ? " ***paused***" : "");
9b0e97aa
GM
213}
214
215
a7ca3326 216DEFUN ("dump-redisplay-history", Fdump_redisplay_history,
9b0e97aa 217 Sdump_redisplay_history, 0, 0, "",
7ee72033 218 doc: /* Dump redisplay history to stderr. */)
5842a27b 219 (void)
9b0e97aa
GM
220{
221 int i;
222
223 for (i = history_idx - 1; i != history_idx; --i)
224 {
225 if (i < 0)
226 i = REDISPLAY_HISTORY_SIZE - 1;
227 fprintf (stderr, "%s\n", redisplay_history[i].trace);
228 }
229
230 return Qnil;
231}
232
233
e509cfa6 234#else /* not GLYPH_DEBUG */
4588ec20 235
7df02016
KS
236#define WINDOW_TO_FRAME_VPOS(W, VPOS) ((VPOS) + WINDOW_TOP_EDGE_LINE (W))
237#define WINDOW_TO_FRAME_HPOS(W, HPOS) ((HPOS) + WINDOW_LEFT_EDGE_COL (W))
4588ec20 238
e509cfa6 239#endif /* GLYPH_DEBUG */
4588ec20 240
4588ec20 241
9e4bf381
PE
242#if (defined PROFILING \
243 && (defined __FreeBSD__ || defined GNU_LINUX || defined __MINGW32__) \
244 && !HAVE___EXECUTABLE_START)
245/* This function comes first in the Emacs executable and is used only
246 to estimate the text start for profiling. */
4588ec20 247void
9e4bf381 248__executable_start (void)
4588ec20 249{
1088b922 250 emacs_abort ();
177c0ea7 251}
72af86bd 252#endif
5f5c8ee5
GM
253\f
254/***********************************************************************
255 Glyph Matrices
256 ***********************************************************************/
257
258/* Allocate and return a glyph_matrix structure. POOL is the glyph
259 pool from which memory for the matrix should be allocated, or null
260 for window-based redisplay where no glyph pools are used. The
261 member `pool' of the glyph matrix structure returned is set to
262 POOL, the structure is otherwise zeroed. */
263
5bf46f05 264static struct glyph_matrix *
971de7fb 265new_glyph_matrix (struct glyph_pool *pool)
4588ec20 266{
23f86fce 267 struct glyph_matrix *result = xzalloc (sizeof *result);
4588ec20 268
f0b79313 269#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
5f5c8ee5
GM
270 /* Increment number of allocated matrices. This count is used
271 to detect memory leaks. */
272 ++glyph_matrix_count;
f0b79313 273#endif
4588ec20 274
5f5c8ee5
GM
275 /* Set pool and return. */
276 result->pool = pool;
277 return result;
4588ec20
JB
278}
279
4588ec20 280
5f5c8ee5
GM
281/* Free glyph matrix MATRIX. Passing in a null MATRIX is allowed.
282
f0b79313
DA
283 If GLYPH_DEBUG and ENABLE_CHECKING are in effect, the global counter
284 glyph_matrix_count is decremented when a matrix is freed. If the count
285 gets negative, more structures were freed than allocated, i.e. one matrix
286 was freed more than once or a bogus pointer was passed to this function.
177c0ea7 287
5f5c8ee5
GM
288 If MATRIX->pool is null, this means that the matrix manages its own
289 glyph memory---this is done for matrices on X frames. Freeing the
290 matrix also frees the glyph memory in this case. */
291
292static void
971de7fb 293free_glyph_matrix (struct glyph_matrix *matrix)
4588ec20 294{
5f5c8ee5
GM
295 if (matrix)
296 {
297 int i;
298
f0b79313 299#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
5f5c8ee5
GM
300 /* Detect the case that more matrices are freed than were
301 allocated. */
f0b79313
DA
302 --glyph_matrix_count;
303 eassert (glyph_matrix_count >= 0);
304#endif
5f5c8ee5
GM
305
306 /* Free glyph memory if MATRIX owns it. */
307 if (matrix->pool == NULL)
308 for (i = 0; i < matrix->rows_allocated; ++i)
309 xfree (matrix->rows[i].glyphs[LEFT_MARGIN_AREA]);
177c0ea7 310
5f5c8ee5
GM
311 /* Free row structures and the matrix itself. */
312 xfree (matrix->rows);
313 xfree (matrix);
314 }
315}
4588ec20 316
4588ec20 317
5f5c8ee5
GM
318/* Return the number of glyphs to reserve for a marginal area of
319 window W. TOTAL_GLYPHS is the number of glyphs in a complete
320 display line of window W. MARGIN gives the width of the marginal
eeaf9bf3 321 area in canonical character units. */
5f5c8ee5
GM
322
323static int
eeaf9bf3 324margin_glyphs_to_reserve (struct window *w, int total_glyphs, int margin)
5f5c8ee5 325{
eeaf9bf3 326 if (margin > 0)
4588ec20 327 {
5f24fa51 328 int width = w->total_cols;
eeaf9bf3 329 double d = max (0, margin);
5f5c8ee5 330 d = min (width / 2 - 1, d);
eeaf9bf3 331 return (int) ((double) total_glyphs / width * d);
5f5c8ee5 332 }
eeaf9bf3 333 return 0;
5f5c8ee5
GM
334}
335
59ea14cd 336/* Return true if ROW's hash value is correct.
e509cfa6
DA
337 Optimized away if ENABLE_CHECKING is not defined. */
338
59ea14cd 339static bool
7a7270dd
EZ
340verify_row_hash (struct glyph_row *row)
341{
015137db 342 return row->hash == row_hash (row);
7a7270dd 343}
4588ec20 344
5f5c8ee5
GM
345/* Adjust glyph matrix MATRIX on window W or on a frame to changed
346 window sizes.
4588ec20 347
5f5c8ee5
GM
348 W is null if the function is called for a frame glyph matrix.
349 Otherwise it is the window MATRIX is a member of. X and Y are the
350 indices of the first column and row of MATRIX within the frame
351 matrix, if such a matrix exists. They are zero for purely
352 window-based redisplay. DIM is the needed size of the matrix.
d52bad65 353
5f5c8ee5
GM
354 In window-based redisplay, where no frame matrices exist, glyph
355 matrices manage their own glyph storage. Otherwise, they allocate
356 storage from a common frame glyph pool which can be found in
357 MATRIX->pool.
23b0200c 358
5f5c8ee5
GM
359 The reason for this memory management strategy is to avoid complete
360 frame redraws if possible. When we allocate from a common pool, a
361 change of the location or size of a sub-matrix within the pool
362 requires a complete redisplay of the frame because we cannot easily
363 make sure that the current matrices of all windows still agree with
364 what is displayed on the screen. While this is usually fast, it
365 leads to screen flickering. */
23b0200c 366
5f5c8ee5 367static void
971de7fb 368adjust_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int x, int y, struct dim dim)
5f5c8ee5
GM
369{
370 int i;
371 int new_rows;
59ea14cd
PE
372 bool marginal_areas_changed_p = 0;
373 bool header_line_changed_p = 0;
374 bool header_line_p = 0;
5f5c8ee5 375 int left = -1, right = -1;
ca8a12a7 376 int window_width = -1, window_height = -1;
5f5c8ee5 377
6b61353c
KH
378 /* See if W had a header line that has disappeared now, or vice versa.
379 Get W's size. */
5f5c8ee5
GM
380 if (w)
381 {
c3c4768d 382 window_box (w, ANY_AREA, 0, 0, &window_width, &window_height);
6b61353c 383
045dee35
GM
384 header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
385 header_line_changed_p = header_line_p != matrix->header_line_p;
5f5c8ee5 386 }
045dee35 387 matrix->header_line_p = header_line_p;
23b0200c 388
6b61353c
KH
389 /* If POOL is null, MATRIX is a window matrix for window-based redisplay.
390 Do nothing if MATRIX' size, position, vscroll, and marginal areas
5f5c8ee5
GM
391 haven't changed. This optimization is important because preserving
392 the matrix means preventing redisplay. */
393 if (matrix->pool == NULL)
394 {
d3d50620
DA
395 left = margin_glyphs_to_reserve (w, dim.width, w->left_margin_cols);
396 right = margin_glyphs_to_reserve (w, dim.width, w->right_margin_cols);
a54e2c05 397 eassert (left >= 0 && right >= 0);
5f5c8ee5
GM
398 marginal_areas_changed_p = (left != matrix->left_margin_glyphs
399 || right != matrix->right_margin_glyphs);
400
401 if (!marginal_areas_changed_p
bd0443bb 402 && !XFRAME (w->frame)->fonts_changed
045dee35 403 && !header_line_changed_p
880e6158
MR
404 && matrix->window_pixel_left == WINDOW_LEFT_PIXEL_EDGE (w)
405 && matrix->window_pixel_top == WINDOW_TOP_PIXEL_EDGE (w)
5f5c8ee5
GM
406 && matrix->window_height == window_height
407 && matrix->window_vscroll == w->vscroll
408 && matrix->window_width == window_width)
409 return;
410 }
177c0ea7 411
5f5c8ee5
GM
412 /* Enlarge MATRIX->rows if necessary. New rows are cleared. */
413 if (matrix->rows_allocated < dim.height)
414 {
0065d054 415 int old_alloc = matrix->rows_allocated;
5f5c8ee5 416 new_rows = dim.height - matrix->rows_allocated;
0065d054
PE
417 matrix->rows = xpalloc (matrix->rows, &matrix->rows_allocated,
418 new_rows, INT_MAX, sizeof *matrix->rows);
419 memset (matrix->rows + old_alloc, 0,
420 (matrix->rows_allocated - old_alloc) * sizeof *matrix->rows);
5f5c8ee5
GM
421 }
422 else
423 new_rows = 0;
60a8948a 424
5f5c8ee5
GM
425 /* If POOL is not null, MATRIX is a frame matrix or a window matrix
426 on a frame not using window-based redisplay. Set up pointers for
427 each row into the glyph pool. */
428 if (matrix->pool)
429 {
a54e2c05 430 eassert (matrix->pool->glyphs);
177c0ea7 431
5f5c8ee5 432 if (w)
60a8948a 433 {
5f5c8ee5 434 left = margin_glyphs_to_reserve (w, dim.width,
d3d50620 435 w->left_margin_cols);
5f5c8ee5 436 right = margin_glyphs_to_reserve (w, dim.width,
d3d50620 437 w->right_margin_cols);
60a8948a 438 }
5f5c8ee5
GM
439 else
440 left = right = 0;
177c0ea7 441
5f5c8ee5 442 for (i = 0; i < dim.height; ++i)
60a8948a 443 {
5f5c8ee5 444 struct glyph_row *row = &matrix->rows[i];
177c0ea7
JB
445
446 row->glyphs[LEFT_MARGIN_AREA]
5f5c8ee5
GM
447 = (matrix->pool->glyphs
448 + (y + i) * matrix->pool->ncolumns
449 + x);
177c0ea7 450
5f5c8ee5
GM
451 if (w == NULL
452 || row == matrix->rows + dim.height - 1
045dee35 453 || (row == matrix->rows && matrix->header_line_p))
5f5c8ee5
GM
454 {
455 row->glyphs[TEXT_AREA]
456 = row->glyphs[LEFT_MARGIN_AREA];
457 row->glyphs[RIGHT_MARGIN_AREA]
458 = row->glyphs[TEXT_AREA] + dim.width;
459 row->glyphs[LAST_AREA]
460 = row->glyphs[RIGHT_MARGIN_AREA];
461 }
462 else
463 {
464 row->glyphs[TEXT_AREA]
465 = row->glyphs[LEFT_MARGIN_AREA] + left;
466 row->glyphs[RIGHT_MARGIN_AREA]
467 = row->glyphs[TEXT_AREA] + dim.width - left - right;
468 row->glyphs[LAST_AREA]
469 = row->glyphs[LEFT_MARGIN_AREA] + dim.width;
470 }
60a8948a 471 }
177c0ea7 472
5f5c8ee5
GM
473 matrix->left_margin_glyphs = left;
474 matrix->right_margin_glyphs = right;
475 }
476 else
477 {
478 /* If MATRIX->pool is null, MATRIX is responsible for managing
6b61353c
KH
479 its own memory. It is a window matrix for window-based redisplay.
480 Allocate glyph memory from the heap. */
5f5c8ee5
GM
481 if (dim.width > matrix->matrix_w
482 || new_rows
045dee35 483 || header_line_changed_p
5f5c8ee5 484 || marginal_areas_changed_p)
4588ec20 485 {
5f5c8ee5
GM
486 struct glyph_row *row = matrix->rows;
487 struct glyph_row *end = row + matrix->rows_allocated;
177c0ea7 488
5f5c8ee5
GM
489 while (row < end)
490 {
491 row->glyphs[LEFT_MARGIN_AREA]
0065d054
PE
492 = xnrealloc (row->glyphs[LEFT_MARGIN_AREA],
493 dim.width, sizeof (struct glyph));
177c0ea7 494
5f5c8ee5
GM
495 /* The mode line never has marginal areas. */
496 if (row == matrix->rows + dim.height - 1
045dee35 497 || (row == matrix->rows && matrix->header_line_p))
5f5c8ee5
GM
498 {
499 row->glyphs[TEXT_AREA]
500 = row->glyphs[LEFT_MARGIN_AREA];
501 row->glyphs[RIGHT_MARGIN_AREA]
502 = row->glyphs[TEXT_AREA] + dim.width;
503 row->glyphs[LAST_AREA]
504 = row->glyphs[RIGHT_MARGIN_AREA];
505 }
506 else
507 {
508 row->glyphs[TEXT_AREA]
509 = row->glyphs[LEFT_MARGIN_AREA] + left;
510 row->glyphs[RIGHT_MARGIN_AREA]
511 = row->glyphs[TEXT_AREA] + dim.width - left - right;
512 row->glyphs[LAST_AREA]
513 = row->glyphs[LEFT_MARGIN_AREA] + dim.width;
514 }
515 ++row;
516 }
4588ec20
JB
517 }
518
a54e2c05 519 eassert (left >= 0 && right >= 0);
5f5c8ee5
GM
520 matrix->left_margin_glyphs = left;
521 matrix->right_margin_glyphs = right;
522 }
177c0ea7 523
5f5c8ee5
GM
524 /* Number of rows to be used by MATRIX. */
525 matrix->nrows = dim.height;
a54e2c05 526 eassert (matrix->nrows >= 0);
5f5c8ee5 527
b7b20fbd 528 if (w)
5f5c8ee5 529 {
b7b20fbd 530 if (matrix == w->current_matrix)
4588ec20 531 {
b7b20fbd
GM
532 /* Mark rows in a current matrix of a window as not having
533 valid contents. It's important to not do this for
534 desired matrices. When Emacs starts, it may already be
535 building desired matrices when this function runs. */
536 if (window_width < 0)
537 window_width = window_box_width (w, -1);
177c0ea7 538
b7b20fbd
GM
539 /* Optimize the case that only the height has changed (C-x 2,
540 upper window). Invalidate all rows that are no longer part
541 of the window. */
542 if (!marginal_areas_changed_p
73d75a62
GM
543 && !header_line_changed_p
544 && new_rows == 0
545 && dim.width == matrix->matrix_w
880e6158
MR
546 && matrix->window_pixel_left == WINDOW_LEFT_PIXEL_EDGE (w)
547 && matrix->window_pixel_top == WINDOW_TOP_PIXEL_EDGE (w)
73d75a62 548 && matrix->window_width == window_width)
b7b20fbd 549 {
dd42b31a
GM
550 /* Find the last row in the window. */
551 for (i = 0; i < matrix->nrows && matrix->rows[i].enabled_p; ++i)
552 if (MATRIX_ROW_BOTTOM_Y (matrix->rows + i) >= window_height)
553 {
26447967 554 ++i;
dd42b31a
GM
555 break;
556 }
b7b20fbd
GM
557
558 /* Window end is invalid, if inside of the rows that
dd42b31a 559 are invalidated below. */
02455cb6 560 if (w->window_end_vpos >= i)
9d93ce29 561 w->window_end_valid = 0;
177c0ea7 562
b7b20fbd 563 while (i < matrix->nrows)
96c06863 564 matrix->rows[i++].enabled_p = false;
b7b20fbd
GM
565 }
566 else
567 {
568 for (i = 0; i < matrix->nrows; ++i)
96c06863 569 matrix->rows[i].enabled_p = false;
b7b20fbd 570 }
5f5c8ee5 571 }
b7b20fbd 572 else if (matrix == w->desired_matrix)
5f5c8ee5 573 {
b7b20fbd
GM
574 /* Rows in desired matrices always have to be cleared;
575 redisplay expects this is the case when it runs, so it
576 had better be the case when we adjust matrices between
577 redisplays. */
5f5c8ee5 578 for (i = 0; i < matrix->nrows; ++i)
96c06863 579 matrix->rows[i].enabled_p = false;
4588ec20 580 }
4588ec20 581 }
177c0ea7
JB
582
583
5f5c8ee5
GM
584 /* Remember last values to be able to optimize frame redraws. */
585 matrix->matrix_x = x;
586 matrix->matrix_y = y;
587 matrix->matrix_w = dim.width;
588 matrix->matrix_h = dim.height;
589
590 /* Record the top y location and height of W at the time the matrix
591 was last adjusted. This is used to optimize redisplay above. */
592 if (w)
4588ec20 593 {
880e6158
MR
594 matrix->window_pixel_left = WINDOW_LEFT_PIXEL_EDGE (w);
595 matrix->window_pixel_top = WINDOW_TOP_PIXEL_EDGE (w);
5f5c8ee5
GM
596 matrix->window_height = window_height;
597 matrix->window_width = window_width;
598 matrix->window_vscroll = w->vscroll;
599 }
600}
4588ec20 601
4588ec20 602
5f5c8ee5
GM
603/* Reverse the contents of rows in MATRIX between START and END. The
604 contents of the row at END - 1 end up at START, END - 2 at START +
605 1 etc. This is part of the implementation of rotate_matrix (see
606 below). */
d52bad65 607
5f5c8ee5 608static void
971de7fb 609reverse_rows (struct glyph_matrix *matrix, int start, int end)
5f5c8ee5
GM
610{
611 int i, j;
23b0200c 612
5f5c8ee5
GM
613 for (i = start, j = end - 1; i < j; ++i, --j)
614 {
615 /* Non-ISO HP/UX compiler doesn't like auto struct
616 initialization. */
617 struct glyph_row temp;
618 temp = matrix->rows[i];
619 matrix->rows[i] = matrix->rows[j];
620 matrix->rows[j] = temp;
621 }
622}
23b0200c 623
23b0200c 624
5f5c8ee5
GM
625/* Rotate the contents of rows in MATRIX in the range FIRST .. LAST -
626 1 by BY positions. BY < 0 means rotate left, i.e. towards lower
627 indices. (Note: this does not copy glyphs, only glyph pointers in
628 row structures are moved around).
60a8948a 629
5f5c8ee5
GM
630 The algorithm used for rotating the vector was, I believe, first
631 described by Kernighan. See the vector R as consisting of two
632 sub-vectors AB, where A has length BY for BY >= 0. The result
633 after rotating is then BA. Reverse both sub-vectors to get ArBr
634 and reverse the result to get (ArBr)r which is BA. Similar for
635 rotating right. */
636
637void
971de7fb 638rotate_matrix (struct glyph_matrix *matrix, int first, int last, int by)
5f5c8ee5
GM
639{
640 if (by < 0)
641 {
642 /* Up (rotate left, i.e. towards lower indices). */
643 by = -by;
644 reverse_rows (matrix, first, first + by);
645 reverse_rows (matrix, first + by, last);
646 reverse_rows (matrix, first, last);
647 }
648 else if (by > 0)
649 {
650 /* Down (rotate right, i.e. towards higher indices). */
651 reverse_rows (matrix, last - by, last);
652 reverse_rows (matrix, first, last - by);
653 reverse_rows (matrix, first, last);
654 }
655}
656
657
658/* Increment buffer positions in glyph rows of MATRIX. Do it for rows
659 with indices START <= index < END. Increment positions by DELTA/
660 DELTA_BYTES. */
661
662void
ace1712c 663increment_matrix_positions (struct glyph_matrix *matrix, int start, int end,
d311d28c 664 ptrdiff_t delta, ptrdiff_t delta_bytes)
5f5c8ee5
GM
665{
666 /* Check that START and END are reasonable values. */
a54e2c05
DA
667 eassert (start >= 0 && start <= matrix->nrows);
668 eassert (end >= 0 && end <= matrix->nrows);
669 eassert (start <= end);
5f5c8ee5
GM
670
671 for (; start < end; ++start)
86c11ba1 672 increment_row_positions (matrix->rows + start, delta, delta_bytes);
5f5c8ee5
GM
673}
674
675
59ea14cd
PE
676/* Clear the enable_p flags in a range of rows in glyph matrix MATRIX.
677 START and END are the row indices of the first and last + 1 row to clear. */
5f5c8ee5
GM
678
679void
59ea14cd 680clear_glyph_matrix_rows (struct glyph_matrix *matrix, int start, int end)
5f5c8ee5 681{
a54e2c05
DA
682 eassert (start <= end);
683 eassert (start >= 0 && start < matrix->nrows);
684 eassert (end >= 0 && end <= matrix->nrows);
177c0ea7 685
5f5c8ee5 686 for (; start < end; ++start)
96c06863 687 matrix->rows[start].enabled_p = false;
5f5c8ee5
GM
688}
689
690
691/* Clear MATRIX.
692
59ea14cd
PE
693 Empty all rows in MATRIX by clearing their enabled_p flags.
694 The function prepare_desired_row will eventually really clear a row
695 when it sees one with a false enabled_p flag.
5f5c8ee5 696
59ea14cd 697 Reset update hints to default values. The only update hint
5f5c8ee5
GM
698 currently present is the flag MATRIX->no_scrolling_p. */
699
700void
971de7fb 701clear_glyph_matrix (struct glyph_matrix *matrix)
5f5c8ee5
GM
702{
703 if (matrix)
704 {
59ea14cd 705 clear_glyph_matrix_rows (matrix, 0, matrix->nrows);
5f5c8ee5
GM
706 matrix->no_scrolling_p = 0;
707 }
708}
177c0ea7 709
5f5c8ee5
GM
710
711/* Shift part of the glyph matrix MATRIX of window W up or down.
712 Increment y-positions in glyph rows between START and END by DY,
713 and recompute their visible height. */
714
715void
971de7fb 716shift_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int start, int end, int dy)
5f5c8ee5
GM
717{
718 int min_y, max_y;
177c0ea7 719
a54e2c05
DA
720 eassert (start <= end);
721 eassert (start >= 0 && start < matrix->nrows);
722 eassert (end >= 0 && end <= matrix->nrows);
177c0ea7 723
7df02016
KS
724 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
725 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
177c0ea7 726
5f5c8ee5
GM
727 for (; start < end; ++start)
728 {
729 struct glyph_row *row = &matrix->rows[start];
177c0ea7 730
5f5c8ee5 731 row->y += dy;
d6c747a4 732 row->visible_height = row->height;
177c0ea7 733
5f5c8ee5 734 if (row->y < min_y)
d6c747a4
GM
735 row->visible_height -= min_y - row->y;
736 if (row->y + row->height > max_y)
737 row->visible_height -= row->y + row->height - max_y;
e61124cd
YM
738 if (row->fringe_bitmap_periodic_p)
739 row->redraw_fringe_bitmaps_p = 1;
5f5c8ee5
GM
740 }
741}
742
743
744/* Mark all rows in current matrices of frame F as invalid. Marking
745 invalid is done by setting enabled_p to zero for all rows in a
746 current matrix. */
747
748void
971de7fb 749clear_current_matrices (register struct frame *f)
5f5c8ee5
GM
750{
751 /* Clear frame current matrix, if we have one. */
752 if (f->current_matrix)
753 clear_glyph_matrix (f->current_matrix);
754
5a49b79c 755#if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
5f5c8ee5
GM
756 /* Clear the matrix of the menu bar window, if such a window exists.
757 The menu bar window is currently used to display menus on X when
758 no toolkit support is compiled in. */
e69b0960
DA
759 if (WINDOWP (f->menu_bar_window))
760 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
5a49b79c 761#endif
5f5c8ee5 762
742516e0 763#if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
9ea173e8 764 /* Clear the matrix of the tool-bar window, if any. */
e69b0960
DA
765 if (WINDOWP (f->tool_bar_window))
766 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
742516e0 767#endif
5f5c8ee5
GM
768
769 /* Clear current window matrices. */
a54e2c05 770 eassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
5f5c8ee5
GM
771 clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 0);
772}
773
774
775/* Clear out all display lines of F for a coming redisplay. */
776
777void
971de7fb 778clear_desired_matrices (register struct frame *f)
5f5c8ee5
GM
779{
780 if (f->desired_matrix)
781 clear_glyph_matrix (f->desired_matrix);
177c0ea7 782
5a49b79c 783#if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
e69b0960
DA
784 if (WINDOWP (f->menu_bar_window))
785 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->desired_matrix);
5a49b79c 786#endif
5f5c8ee5 787
742516e0 788#if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
e69b0960
DA
789 if (WINDOWP (f->tool_bar_window))
790 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->desired_matrix);
742516e0 791#endif
5f5c8ee5
GM
792
793 /* Do it for window matrices. */
a54e2c05 794 eassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
5f5c8ee5
GM
795 clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
796}
797
798
59ea14cd
PE
799/* Clear matrices in window tree rooted in W. If DESIRED_P,
800 clear desired matrices, otherwise clear current matrices. */
5f5c8ee5
GM
801
802static void
59ea14cd 803clear_window_matrices (struct window *w, bool desired_p)
5f5c8ee5
GM
804{
805 while (w)
806 {
e74aeda8
DA
807 if (WINDOWP (w->contents))
808 clear_window_matrices (XWINDOW (w->contents), desired_p);
5f5c8ee5
GM
809 else
810 {
811 if (desired_p)
812 clear_glyph_matrix (w->desired_matrix);
813 else
814 {
815 clear_glyph_matrix (w->current_matrix);
9d93ce29 816 w->window_end_valid = 0;
5f5c8ee5
GM
817 }
818 }
819
d3d50620 820 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5f5c8ee5
GM
821 }
822}
823
824
825\f
826/***********************************************************************
827 Glyph Rows
828
829 See dispextern.h for an overall explanation of glyph rows.
830 ***********************************************************************/
831
ec7bc82f
DA
832/* Clear glyph row ROW. NOTE: this code relies on the current
833 layout of `glyphs' and `used' fields of `struct glyph_row'. */
2cca872d 834
5f5c8ee5 835void
971de7fb 836clear_glyph_row (struct glyph_row *row)
5f5c8ee5 837{
cf647d93 838 enum { off = offsetof (struct glyph_row, used) };
ec7bc82f 839
ec7bc82f 840 /* Zero everything except pointers in `glyphs'. */
9da0f50e 841 memset (row->used, 0, sizeof *row - off);
5f5c8ee5
GM
842}
843
844
845/* Make ROW an empty, enabled row of canonical character height,
846 in window W starting at y-position Y. */
847
848void
971de7fb 849blank_row (struct window *w, struct glyph_row *row, int y)
5f5c8ee5
GM
850{
851 int min_y, max_y;
177c0ea7 852
7df02016
KS
853 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
854 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
177c0ea7 855
5f5c8ee5
GM
856 clear_glyph_row (row);
857 row->y = y;
408f5064 858 row->ascent = row->phys_ascent = 0;
d3d50620 859 row->height = row->phys_height = FRAME_LINE_HEIGHT (XFRAME (w->frame));
d6c747a4 860 row->visible_height = row->height;
177c0ea7 861
5f5c8ee5 862 if (row->y < min_y)
d6c747a4
GM
863 row->visible_height -= min_y - row->y;
864 if (row->y + row->height > max_y)
865 row->visible_height -= row->y + row->height - max_y;
5f5c8ee5 866
96c06863 867 row->enabled_p = true;
5f5c8ee5
GM
868}
869
870
871/* Increment buffer positions in glyph row ROW. DELTA and DELTA_BYTES
872 are the amounts by which to change positions. Note that the first
873 glyph of the text area of a row can have a buffer position even if
874 the used count of the text area is zero. Such rows display line
875 ends. */
876
5bf46f05 877static void
ace1712c 878increment_row_positions (struct glyph_row *row,
d311d28c 879 ptrdiff_t delta, ptrdiff_t delta_bytes)
5f5c8ee5
GM
880{
881 int area, i;
882
883 /* Increment start and end positions. */
884 MATRIX_ROW_START_CHARPOS (row) += delta;
885 MATRIX_ROW_START_BYTEPOS (row) += delta_bytes;
886 MATRIX_ROW_END_CHARPOS (row) += delta;
887 MATRIX_ROW_END_BYTEPOS (row) += delta_bytes;
d36fe237
EZ
888 CHARPOS (row->start.pos) += delta;
889 BYTEPOS (row->start.pos) += delta_bytes;
890 CHARPOS (row->end.pos) += delta;
891 BYTEPOS (row->end.pos) += delta_bytes;
5f5c8ee5 892
5cacdcfd
KS
893 if (!row->enabled_p)
894 return;
895
5f5c8ee5
GM
896 /* Increment positions in glyphs. */
897 for (area = 0; area < LAST_AREA; ++area)
898 for (i = 0; i < row->used[area]; ++i)
899 if (BUFFERP (row->glyphs[area][i].object)
900 && row->glyphs[area][i].charpos > 0)
901 row->glyphs[area][i].charpos += delta;
902
903 /* Capture the case of rows displaying a line end. */
904 if (row->used[TEXT_AREA] == 0
905 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
906 row->glyphs[TEXT_AREA]->charpos += delta;
907}
908
909
9af3e742 910#if 0
5f5c8ee5
GM
911/* Swap glyphs between two glyph rows A and B. This exchanges glyph
912 contents, i.e. glyph structure contents are exchanged between A and
913 B without changing glyph pointers in A and B. */
914
915static void
1dae0f0a 916swap_glyphs_in_rows (struct glyph_row *a, struct glyph_row *b)
5f5c8ee5
GM
917{
918 int area;
919
920 for (area = 0; area < LAST_AREA; ++area)
921 {
922 /* Number of glyphs to swap. */
923 int max_used = max (a->used[area], b->used[area]);
924
925 /* Start of glyphs in area of row A. */
926 struct glyph *glyph_a = a->glyphs[area];
927
928 /* End + 1 of glyphs in area of row A. */
929 struct glyph *glyph_a_end = a->glyphs[max_used];
930
931 /* Start of glyphs in area of row B. */
932 struct glyph *glyph_b = b->glyphs[area];
933
934 while (glyph_a < glyph_a_end)
935 {
936 /* Non-ISO HP/UX compiler doesn't like auto struct
937 initialization. */
938 struct glyph temp;
939 temp = *glyph_a;
940 *glyph_a = *glyph_b;
941 *glyph_b = temp;
942 ++glyph_a;
943 ++glyph_b;
944 }
945 }
946}
947
9af3e742 948#endif /* 0 */
5f5c8ee5 949
015137db
EZ
950/* Exchange pointers to glyph memory between glyph rows A and B. Also
951 exchange the used[] array and the hash values of the rows, because
952 these should all go together for the row's hash value to be
953 correct. */
5f5c8ee5 954
b0ab8123 955static void
971de7fb 956swap_glyph_pointers (struct glyph_row *a, struct glyph_row *b)
5f5c8ee5
GM
957{
958 int i;
015137db
EZ
959 unsigned hash_tem = a->hash;
960
5f5c8ee5
GM
961 for (i = 0; i < LAST_AREA + 1; ++i)
962 {
963 struct glyph *temp = a->glyphs[i];
015137db 964
5f5c8ee5
GM
965 a->glyphs[i] = b->glyphs[i];
966 b->glyphs[i] = temp;
73055685
EZ
967 if (i < LAST_AREA)
968 {
969 short used_tem = a->used[i];
970
971 a->used[i] = b->used[i];
972 b->used[i] = used_tem;
973 }
5f5c8ee5 974 }
015137db
EZ
975 a->hash = b->hash;
976 b->hash = hash_tem;
5f5c8ee5
GM
977}
978
979
ec7bc82f
DA
980/* Copy glyph row structure FROM to glyph row structure TO, except that
981 glyph pointers, the `used' counts, and the hash values in the structures
982 are left unchanged. NOTE: this code relies on the current layout of
983 `glyphs', `used', `hash' and `x' fields of `struct glyph_row'. */
5f5c8ee5 984
b0ab8123 985static void
971de7fb 986copy_row_except_pointers (struct glyph_row *to, struct glyph_row *from)
5f5c8ee5 987{
cf647d93 988 enum { off = offsetof (struct glyph_row, x) };
ec7bc82f 989
9da0f50e 990 memcpy (&to->x, &from->x, sizeof *to - off);
5f5c8ee5
GM
991}
992
993
5f5c8ee5
GM
994/* Assign glyph row FROM to glyph row TO. This works like a structure
995 assignment TO = FROM, except that glyph pointers are not copied but
996 exchanged between TO and FROM. Pointers must be exchanged to avoid
997 a memory leak. */
998
b0ab8123 999static void
971de7fb 1000assign_row (struct glyph_row *to, struct glyph_row *from)
5f5c8ee5
GM
1001{
1002 swap_glyph_pointers (to, from);
1003 copy_row_except_pointers (to, from);
1004}
1005
1006
1007/* Test whether the glyph memory of the glyph row WINDOW_ROW, which is
1008 a row in a window matrix, is a slice of the glyph memory of the
1009 glyph row FRAME_ROW which is a row in a frame glyph matrix. Value
59ea14cd 1010 is true if the glyph memory of WINDOW_ROW is part of the glyph
5f5c8ee5
GM
1011 memory of FRAME_ROW. */
1012
e509cfa6 1013#ifdef GLYPH_DEBUG
9339e87f 1014
59ea14cd 1015static bool
59fc5cf9 1016glyph_row_slice_p (struct glyph_row *window_row, struct glyph_row *frame_row)
5f5c8ee5
GM
1017{
1018 struct glyph *window_glyph_start = window_row->glyphs[0];
1019 struct glyph *frame_glyph_start = frame_row->glyphs[0];
1020 struct glyph *frame_glyph_end = frame_row->glyphs[LAST_AREA];
1021
1022 return (frame_glyph_start <= window_glyph_start
1023 && window_glyph_start < frame_glyph_end);
1024}
1025
9339e87f 1026#endif /* GLYPH_DEBUG */
5f5c8ee5 1027
9af3e742
GM
1028#if 0
1029
5f5c8ee5
GM
1030/* Find the row in the window glyph matrix WINDOW_MATRIX being a slice
1031 of ROW in the frame matrix FRAME_MATRIX. Value is null if no row
1032 in WINDOW_MATRIX is found satisfying the condition. */
1033
1034static struct glyph_row *
59fc5cf9
AS
1035find_glyph_row_slice (struct glyph_matrix *window_matrix,
1036 struct glyph_matrix *frame_matrix, int row)
5f5c8ee5
GM
1037{
1038 int i;
1039
a54e2c05 1040 eassert (row >= 0 && row < frame_matrix->nrows);
5f5c8ee5
GM
1041
1042 for (i = 0; i < window_matrix->nrows; ++i)
1043 if (glyph_row_slice_p (window_matrix->rows + i,
1044 frame_matrix->rows + row))
1045 break;
1046
1047 return i < window_matrix->nrows ? window_matrix->rows + i : 0;
1048}
1049
9af3e742 1050#endif /* 0 */
5f5c8ee5
GM
1051
1052/* Prepare ROW for display. Desired rows are cleared lazily,
1053 i.e. they are only marked as to be cleared by setting their
1054 enabled_p flag to zero. When a row is to be displayed, a prior
1055 call to this function really clears it. */
1056
1057void
971de7fb 1058prepare_desired_row (struct glyph_row *row)
5f5c8ee5
GM
1059{
1060 if (!row->enabled_p)
1061 {
59ea14cd 1062 bool rp = row->reversed_p;
5009f85e 1063
5f5c8ee5 1064 clear_glyph_row (row);
96c06863 1065 row->enabled_p = true;
5009f85e 1066 row->reversed_p = rp;
5f5c8ee5
GM
1067 }
1068}
1069
1070
77e3b1b7
DA
1071/* Return a hash code for glyph row ROW, which may
1072 be from current or desired matrix of frame F. */
5f5c8ee5 1073
2f7c71a1 1074static int
77e3b1b7 1075line_hash_code (struct frame *f, struct glyph_row *row)
5f5c8ee5
GM
1076{
1077 int hash = 0;
177c0ea7 1078
5f5c8ee5
GM
1079 if (row->enabled_p)
1080 {
4cecaab8
MB
1081 struct glyph *glyph = row->glyphs[TEXT_AREA];
1082 struct glyph *end = glyph + row->used[TEXT_AREA];
1083
1084 while (glyph < end)
1085 {
1086 int c = glyph->u.ch;
1087 int face_id = glyph->face_id;
77e3b1b7 1088 if (FRAME_MUST_WRITE_SPACES (f))
4cecaab8
MB
1089 c -= SPACEGLYPH;
1090 hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + c;
1091 hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + face_id;
1092 ++glyph;
1093 }
5f5c8ee5 1094
4cecaab8
MB
1095 if (hash == 0)
1096 hash = 1;
5f5c8ee5
GM
1097 }
1098
1099 return hash;
1100}
1101
1102
77e3b1b7
DA
1103/* Return the cost of drawing line VPOS in MATRIX, which may
1104 be current or desired matrix of frame F. The cost equals
1105 the number of characters in the line. If must_write_spaces
1106 is zero, leading and trailing spaces are ignored. */
5f5c8ee5 1107
dfd153ae 1108static int
77e3b1b7 1109line_draw_cost (struct frame *f, struct glyph_matrix *matrix, int vpos)
5f5c8ee5
GM
1110{
1111 struct glyph_row *row = matrix->rows + vpos;
1112 struct glyph *beg = row->glyphs[TEXT_AREA];
1113 struct glyph *end = beg + row->used[TEXT_AREA];
1114 int len;
1115 Lisp_Object *glyph_table_base = GLYPH_TABLE_BASE;
ca9ce8f2 1116 ptrdiff_t glyph_table_len = GLYPH_TABLE_LENGTH;
5f5c8ee5
GM
1117
1118 /* Ignore trailing and leading spaces if we can. */
77e3b1b7 1119 if (!FRAME_MUST_WRITE_SPACES (f))
5f5c8ee5
GM
1120 {
1121 /* Skip from the end over trailing spaces. */
2b4ec4d7 1122 while (end > beg && CHAR_GLYPH_SPACE_P (*(end - 1)))
5f5c8ee5
GM
1123 --end;
1124
177c0ea7 1125 /* All blank line. */
5f5c8ee5
GM
1126 if (end == beg)
1127 return 0;
1128
1129 /* Skip over leading spaces. */
1130 while (CHAR_GLYPH_SPACE_P (*beg))
1131 ++beg;
1132 }
1133
1134 /* If we don't have a glyph-table, each glyph is one character,
1135 so return the number of glyphs. */
1136 if (glyph_table_base == 0)
1137 len = end - beg;
1138 else
1139 {
1140 /* Otherwise, scan the glyphs and accumulate their total length
1141 in LEN. */
1142 len = 0;
1143 while (beg < end)
1144 {
3fc809e2 1145 GLYPH g;
177c0ea7 1146
3fc809e2
KS
1147 SET_GLYPH_FROM_CHAR_GLYPH (g, *beg);
1148
565b39a8 1149 if (GLYPH_INVALID_P (g)
42186983 1150 || GLYPH_SIMPLE_P (glyph_table_base, glyph_table_len, g))
5f5c8ee5
GM
1151 len += 1;
1152 else
1153 len += GLYPH_LENGTH (glyph_table_base, g);
177c0ea7 1154
5f5c8ee5
GM
1155 ++beg;
1156 }
1157 }
177c0ea7 1158
5f5c8ee5
GM
1159 return len;
1160}
1161
1162
59ea14cd
PE
1163/* Return true if the glyph rows A and B have equal contents.
1164 MOUSE_FACE_P means compare the mouse_face_p flags of A and B, too. */
5f5c8ee5 1165
b0ab8123 1166static bool
59ea14cd 1167row_equal_p (struct glyph_row *a, struct glyph_row *b, bool mouse_face_p)
5f5c8ee5 1168{
a54e2c05
DA
1169 eassert (verify_row_hash (a));
1170 eassert (verify_row_hash (b));
7a7270dd 1171
5f5c8ee5
GM
1172 if (a == b)
1173 return 1;
1174 else if (a->hash != b->hash)
1175 return 0;
1176 else
1177 {
1178 struct glyph *a_glyph, *b_glyph, *a_end;
1179 int area;
1180
e4e0bee9
GM
1181 if (mouse_face_p && a->mouse_face_p != b->mouse_face_p)
1182 return 0;
1183
5f5c8ee5
GM
1184 /* Compare glyphs. */
1185 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
1186 {
1187 if (a->used[area] != b->used[area])
1188 return 0;
177c0ea7 1189
5f5c8ee5
GM
1190 a_glyph = a->glyphs[area];
1191 a_end = a_glyph + a->used[area];
1192 b_glyph = b->glyphs[area];
177c0ea7 1193
5f5c8ee5
GM
1194 while (a_glyph < a_end
1195 && GLYPH_EQUAL_P (a_glyph, b_glyph))
1196 ++a_glyph, ++b_glyph;
177c0ea7 1197
5f5c8ee5
GM
1198 if (a_glyph != a_end)
1199 return 0;
1200 }
1201
6b61353c
KH
1202 if (a->fill_line_p != b->fill_line_p
1203 || a->cursor_in_fringe_p != b->cursor_in_fringe_p
1204 || a->left_fringe_bitmap != b->left_fringe_bitmap
1205 || a->left_fringe_face_id != b->left_fringe_face_id
e61124cd 1206 || a->left_fringe_offset != b->left_fringe_offset
6b61353c
KH
1207 || a->right_fringe_bitmap != b->right_fringe_bitmap
1208 || a->right_fringe_face_id != b->right_fringe_face_id
e61124cd
YM
1209 || a->right_fringe_offset != b->right_fringe_offset
1210 || a->fringe_bitmap_periodic_p != b->fringe_bitmap_periodic_p
3d4a365f 1211 || a->overlay_arrow_bitmap != b->overlay_arrow_bitmap
6b61353c 1212 || a->exact_window_width_line_p != b->exact_window_width_line_p
408f5064 1213 || a->overlapped_p != b->overlapped_p
5f5c8ee5
GM
1214 || (MATRIX_ROW_CONTINUATION_LINE_P (a)
1215 != MATRIX_ROW_CONTINUATION_LINE_P (b))
918f4159 1216 || a->reversed_p != b->reversed_p
5f5c8ee5
GM
1217 /* Different partially visible characters on left margin. */
1218 || a->x != b->x
1219 /* Different height. */
1220 || a->ascent != b->ascent
408f5064
GM
1221 || a->phys_ascent != b->phys_ascent
1222 || a->phys_height != b->phys_height
5f5c8ee5
GM
1223 || a->visible_height != b->visible_height)
1224 return 0;
1225 }
1226
1227 return 1;
1228}
1229
1230
1231\f
1232/***********************************************************************
1233 Glyph Pool
1234
1235 See dispextern.h for an overall explanation of glyph pools.
1236 ***********************************************************************/
1237
f0b79313
DA
1238/* Allocate a glyph_pool structure. The structure returned is initialized
1239 with zeros. If GLYPH_DEBUG and ENABLE_CHECKING are in effect, the global
1240 variable glyph_pool_count is incremented for each pool allocated. */
5f5c8ee5
GM
1241
1242static struct glyph_pool *
971de7fb 1243new_glyph_pool (void)
5f5c8ee5 1244{
23f86fce 1245 struct glyph_pool *result = xzalloc (sizeof *result);
177c0ea7 1246
f0b79313 1247#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
5f5c8ee5
GM
1248 /* For memory leak and double deletion checking. */
1249 ++glyph_pool_count;
f0b79313 1250#endif
177c0ea7 1251
5f5c8ee5
GM
1252 return result;
1253}
1254
1255
1256/* Free a glyph_pool structure POOL. The function may be called with
f0b79313
DA
1257 a null POOL pointer. If GLYPH_DEBUG and ENABLE_CHECKING are in effect,
1258 global variable glyph_pool_count is decremented with every pool structure
1259 freed. If this count gets negative, more structures were freed than
1260 allocated, i.e. one structure must have been freed more than once or
1261 a bogus pointer was passed to free_glyph_pool. */
5f5c8ee5
GM
1262
1263static void
971de7fb 1264free_glyph_pool (struct glyph_pool *pool)
5f5c8ee5
GM
1265{
1266 if (pool)
1267 {
f0b79313 1268#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
153a073c 1269 /* More freed than allocated? */
5f5c8ee5 1270 --glyph_pool_count;
a54e2c05 1271 eassert (glyph_pool_count >= 0);
f0b79313 1272#endif
5f5c8ee5
GM
1273 xfree (pool->glyphs);
1274 xfree (pool);
1275 }
1276}
1277
1278
1279/* Enlarge a glyph pool POOL. MATRIX_DIM gives the number of rows and
1280 columns we need. This function never shrinks a pool. The only
1281 case in which this would make sense, would be when a frame's size
1282 is changed from a large value to a smaller one. But, if someone
1283 does it once, we can expect that he will do it again.
1284
59ea14cd 1285 Return true if the pool changed in a way which makes
5f5c8ee5
GM
1286 re-adjusting window glyph matrices necessary. */
1287
59ea14cd 1288static bool
971de7fb 1289realloc_glyph_pool (struct glyph_pool *pool, struct dim matrix_dim)
5f5c8ee5 1290{
ca9ce8f2 1291 ptrdiff_t needed;
59ea14cd 1292 bool changed_p;
5f5c8ee5
GM
1293
1294 changed_p = (pool->glyphs == 0
1295 || matrix_dim.height != pool->nrows
1296 || matrix_dim.width != pool->ncolumns);
1297
1298 /* Enlarge the glyph pool. */
ca9ce8f2 1299 needed = matrix_dim.width;
0065d054
PE
1300 if (INT_MULTIPLY_OVERFLOW (needed, matrix_dim.height))
1301 memory_full (SIZE_MAX);
ca9ce8f2 1302 needed *= matrix_dim.height;
5f5c8ee5
GM
1303 if (needed > pool->nglyphs)
1304 {
0065d054
PE
1305 ptrdiff_t old_nglyphs = pool->nglyphs;
1306 pool->glyphs = xpalloc (pool->glyphs, &pool->nglyphs,
1307 needed - old_nglyphs, -1, sizeof *pool->glyphs);
1308 memset (pool->glyphs + old_nglyphs, 0,
1309 (pool->nglyphs - old_nglyphs) * sizeof *pool->glyphs);
5f5c8ee5
GM
1310 }
1311
153a073c 1312 /* Remember the number of rows and columns because (a) we use them
5f5c8ee5
GM
1313 to do sanity checks, and (b) the number of columns determines
1314 where rows in the frame matrix start---this must be available to
1315 determine pointers to rows of window sub-matrices. */
1316 pool->nrows = matrix_dim.height;
1317 pool->ncolumns = matrix_dim.width;
177c0ea7 1318
5f5c8ee5
GM
1319 return changed_p;
1320}
1321
1322
1323\f
1324/***********************************************************************
1325 Debug Code
1326 ***********************************************************************/
1327
e509cfa6 1328#ifdef GLYPH_DEBUG
5f5c8ee5 1329
a13396c9 1330
28d440ab
KL
1331/* Flush standard output. This is sometimes useful to call from the debugger.
1332 XXX Maybe this should be changed to flush the current terminal instead of
1333 stdout.
1334*/
a13396c9 1335
85fece3e
PE
1336void flush_stdout (void) EXTERNALLY_VISIBLE;
1337
a13396c9 1338void
59fc5cf9 1339flush_stdout (void)
a13396c9
GM
1340{
1341 fflush (stdout);
1342}
1343
1344
5f5c8ee5
GM
1345/* Check that no glyph pointers have been lost in MATRIX. If a
1346 pointer has been lost, e.g. by using a structure assignment between
1347 rows, at least one pointer must occur more than once in the rows of
1348 MATRIX. */
1349
1350void
59fc5cf9 1351check_matrix_pointer_lossage (struct glyph_matrix *matrix)
5f5c8ee5
GM
1352{
1353 int i, j;
177c0ea7 1354
5f5c8ee5
GM
1355 for (i = 0; i < matrix->nrows; ++i)
1356 for (j = 0; j < matrix->nrows; ++j)
a54e2c05 1357 eassert (i == j
5f5c8ee5
GM
1358 || (matrix->rows[i].glyphs[TEXT_AREA]
1359 != matrix->rows[j].glyphs[TEXT_AREA]));
1360}
1361
1362
1363/* Get a pointer to glyph row ROW in MATRIX, with bounds checks. */
1364
1365struct glyph_row *
59fc5cf9 1366matrix_row (struct glyph_matrix *matrix, int row)
5f5c8ee5 1367{
a54e2c05
DA
1368 eassert (matrix && matrix->rows);
1369 eassert (row >= 0 && row < matrix->nrows);
5f5c8ee5
GM
1370
1371 /* That's really too slow for normal testing because this function
1372 is called almost everywhere. Although---it's still astonishingly
1373 fast, so it is valuable to have for debugging purposes. */
1374#if 0
1375 check_matrix_pointer_lossage (matrix);
1376#endif
177c0ea7 1377
5f5c8ee5
GM
1378 return matrix->rows + row;
1379}
1380
1381
1382#if 0 /* This function makes invalid assumptions when text is
1383 partially invisible. But it might come handy for debugging
1384 nevertheless. */
1385
1386/* Check invariants that must hold for an up to date current matrix of
1387 window W. */
1388
1389static void
59fc5cf9 1390check_matrix_invariants (struct window *w)
5f5c8ee5
GM
1391{
1392 struct glyph_matrix *matrix = w->current_matrix;
1393 int yb = window_text_bottom_y (w);
1394 struct glyph_row *row = matrix->rows;
1395 struct glyph_row *last_text_row = NULL;
1396 struct buffer *saved = current_buffer;
e74aeda8 1397 struct buffer *buffer = XBUFFER (w->contents);
5f5c8ee5 1398 int c;
177c0ea7 1399
5f5c8ee5
GM
1400 /* This can sometimes happen for a fresh window. */
1401 if (matrix->nrows < 2)
1402 return;
1403
1404 set_buffer_temp (buffer);
1405
1406 /* Note: last row is always reserved for the mode line. */
1407 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
1408 && MATRIX_ROW_BOTTOM_Y (row) < yb)
1409 {
1410 struct glyph_row *next = row + 1;
1411
1412 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
1413 last_text_row = row;
1414
1415 /* Check that character and byte positions are in sync. */
a54e2c05 1416 eassert (MATRIX_ROW_START_BYTEPOS (row)
5f5c8ee5 1417 == CHAR_TO_BYTE (MATRIX_ROW_START_CHARPOS (row)));
a54e2c05 1418 eassert (BYTEPOS (row->start.pos)
d36fe237 1419 == CHAR_TO_BYTE (CHARPOS (row->start.pos)));
5f5c8ee5
GM
1420
1421 /* CHAR_TO_BYTE aborts when invoked for a position > Z. We can
1422 have such a position temporarily in case of a minibuffer
1423 displaying something like `[Sole completion]' at its end. */
1424 if (MATRIX_ROW_END_CHARPOS (row) < BUF_ZV (current_buffer))
d36fe237 1425 {
a54e2c05 1426 eassert (MATRIX_ROW_END_BYTEPOS (row)
d36fe237 1427 == CHAR_TO_BYTE (MATRIX_ROW_END_CHARPOS (row)));
a54e2c05 1428 eassert (BYTEPOS (row->end.pos)
d36fe237
EZ
1429 == CHAR_TO_BYTE (CHARPOS (row->end.pos)));
1430 }
5f5c8ee5
GM
1431
1432 /* Check that end position of `row' is equal to start position
1433 of next row. */
1434 if (next->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (next))
1435 {
a54e2c05 1436 eassert (MATRIX_ROW_END_CHARPOS (row)
5f5c8ee5 1437 == MATRIX_ROW_START_CHARPOS (next));
a54e2c05 1438 eassert (MATRIX_ROW_END_BYTEPOS (row)
5f5c8ee5 1439 == MATRIX_ROW_START_BYTEPOS (next));
a54e2c05
DA
1440 eassert (CHARPOS (row->end.pos) == CHARPOS (next->start.pos));
1441 eassert (BYTEPOS (row->end.pos) == BYTEPOS (next->start.pos));
5f5c8ee5
GM
1442 }
1443 row = next;
1444 }
1445
a54e2c05
DA
1446 eassert (w->current_matrix->nrows == w->desired_matrix->nrows);
1447 eassert (w->desired_matrix->rows != NULL);
5f5c8ee5
GM
1448 set_buffer_temp (saved);
1449}
1450
1451#endif /* 0 */
1452
e509cfa6 1453#endif /* GLYPH_DEBUG */
5f5c8ee5
GM
1454
1455
1456\f
1457/**********************************************************************
1458 Allocating/ Adjusting Glyph Matrices
1459 **********************************************************************/
1460
1461/* Allocate glyph matrices over a window tree for a frame-based
1462 redisplay
1463
1464 X and Y are column/row within the frame glyph matrix where
1465 sub-matrices for the window tree rooted at WINDOW must be
59ea14cd 1466 allocated. DIM_ONLY_P means that the caller of this
8ce2650d
YM
1467 function is only interested in the result matrix dimension, and
1468 matrix adjustments should not be performed.
5f5c8ee5
GM
1469
1470 The function returns the total width/height of the sub-matrices of
1471 the window tree. If called on a frame root window, the computation
1472 will take the mini-buffer window into account.
1473
1474 *WINDOW_CHANGE_FLAGS is set to a bit mask with bits
1475
1476 NEW_LEAF_MATRIX set if any window in the tree did not have a
1477 glyph matrices yet, and
1478
1479 CHANGED_LEAF_MATRIX set if the dimension or location of a matrix of
1480 any window in the tree will be changed or have been changed (see
153a073c 1481 DIM_ONLY_P)
5f5c8ee5
GM
1482
1483 *WINDOW_CHANGE_FLAGS must be initialized by the caller of this
1484 function.
1485
1486 Windows are arranged into chains of windows on the same level
1487 through the next fields of window structures. Such a level can be
1488 either a sequence of horizontally adjacent windows from left to
1489 right, or a sequence of vertically adjacent windows from top to
1490 bottom. Each window in a horizontal sequence can be either a leaf
1491 window or a vertical sequence; a window in a vertical sequence can
1492 be either a leaf or a horizontal sequence. All windows in a
1493 horizontal sequence have the same height, and all windows in a
1494 vertical sequence have the same width.
1495
1496 This function uses, for historical reasons, a more general
1497 algorithm to determine glyph matrix dimensions that would be
1498 necessary.
1499
1500 The matrix height of a horizontal sequence is determined by the
1501 maximum height of any matrix in the sequence. The matrix width of
1502 a horizontal sequence is computed by adding up matrix widths of
1503 windows in the sequence.
1504
1505 |<------- result width ------->|
1506 +---------+----------+---------+ ---
1507 | | | | |
1508 | | | |
1509 +---------+ | | result height
1510 | +---------+
1511 | | |
1512 +----------+ ---
1513
1514 The matrix width of a vertical sequence is the maximum matrix width
1515 of any window in the sequence. Its height is computed by adding up
1516 matrix heights of windows in the sequence.
1517
1518 |<---- result width -->|
1519 +---------+ ---
1520 | | |
1521 | | |
1522 +---------+--+ |
1523 | | |
1524 | | result height
1525 | |
1526 +------------+---------+ |
1527 | | |
1528 | | |
1529 +------------+---------+ --- */
1530
1531/* Bit indicating that a new matrix will be allocated or has been
1532 allocated. */
1533
1534#define NEW_LEAF_MATRIX (1 << 0)
1535
1536/* Bit indicating that a matrix will or has changed its location or
1537 size. */
1538
1539#define CHANGED_LEAF_MATRIX (1 << 1)
1540
1541static struct dim
6f704c76 1542allocate_matrices_for_frame_redisplay (Lisp_Object window, int x, int y,
59ea14cd 1543 bool dim_only_p, int *window_change_flags)
5f5c8ee5
GM
1544{
1545 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1546 int x0 = x, y0 = y;
1547 int wmax = 0, hmax = 0;
1548 struct dim total;
1549 struct dim dim;
1550 struct window *w;
59ea14cd 1551 bool in_horz_combination_p;
5f5c8ee5
GM
1552
1553 /* What combination is WINDOW part of? Compute this once since the
1554 result is the same for all windows in the `next' chain. The
1555 special case of a root window (parent equal to nil) is treated
1556 like a vertical combination because a root window's `next'
1557 points to the mini-buffer window, if any, which is arranged
1558 vertically below other windows. */
1559 in_horz_combination_p
d3d50620 1560 = (!NILP (XWINDOW (window)->parent)
e74aeda8 1561 && WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (XWINDOW (window)->parent)));
5f5c8ee5
GM
1562
1563 /* For WINDOW and all windows on the same level. */
177c0ea7 1564 do
5f5c8ee5
GM
1565 {
1566 w = XWINDOW (window);
1567
1568 /* Get the dimension of the window sub-matrix for W, depending
153a073c 1569 on whether this is a combination or a leaf window. */
e74aeda8
DA
1570 if (WINDOWP (w->contents))
1571 dim = allocate_matrices_for_frame_redisplay (w->contents, x, y,
5f5c8ee5
GM
1572 dim_only_p,
1573 window_change_flags);
1574 else
1575 {
1576 /* If not already done, allocate sub-matrix structures. */
1577 if (w->desired_matrix == NULL)
1578 {
1579 w->desired_matrix = new_glyph_matrix (f->desired_pool);
1580 w->current_matrix = new_glyph_matrix (f->current_pool);
1581 *window_change_flags |= NEW_LEAF_MATRIX;
1582 }
177c0ea7 1583
5f5c8ee5
GM
1584 /* Width and height MUST be chosen so that there are no
1585 holes in the frame matrix. */
5d3cc43c
GM
1586 dim.width = required_matrix_width (w);
1587 dim.height = required_matrix_height (w);
5f5c8ee5
GM
1588
1589 /* Will matrix be re-allocated? */
1590 if (x != w->desired_matrix->matrix_x
1591 || y != w->desired_matrix->matrix_y
1592 || dim.width != w->desired_matrix->matrix_w
1593 || dim.height != w->desired_matrix->matrix_h
1594 || (margin_glyphs_to_reserve (w, dim.width,
d3d50620 1595 w->left_margin_cols)
5f5c8ee5
GM
1596 != w->desired_matrix->left_margin_glyphs)
1597 || (margin_glyphs_to_reserve (w, dim.width,
d3d50620 1598 w->right_margin_cols)
5f5c8ee5
GM
1599 != w->desired_matrix->right_margin_glyphs))
1600 *window_change_flags |= CHANGED_LEAF_MATRIX;
1601
1602 /* Actually change matrices, if allowed. Do not consider
1603 CHANGED_LEAF_MATRIX computed above here because the pool
1604 may have been changed which we don't now here. We trust
59ea14cd 1605 that we only will be called with DIM_ONLY_P when
5f5c8ee5
GM
1606 necessary. */
1607 if (!dim_only_p)
1608 {
1609 adjust_glyph_matrix (w, w->desired_matrix, x, y, dim);
1610 adjust_glyph_matrix (w, w->current_matrix, x, y, dim);
1611 }
1612 }
1613
1614 /* If we are part of a horizontal combination, advance x for
1615 windows to the right of W; otherwise advance y for windows
1616 below W. */
1617 if (in_horz_combination_p)
1618 x += dim.width;
177c0ea7 1619 else
5f5c8ee5
GM
1620 y += dim.height;
1621
1622 /* Remember maximum glyph matrix dimensions. */
1623 wmax = max (wmax, dim.width);
1624 hmax = max (hmax, dim.height);
1625
1626 /* Next window on same level. */
d3d50620 1627 window = w->next;
5f5c8ee5
GM
1628 }
1629 while (!NILP (window));
1630
1631 /* Set `total' to the total glyph matrix dimension of this window
1632 level. In a vertical combination, the width is the width of the
1633 widest window; the height is the y we finally reached, corrected
1634 by the y we started with. In a horizontal combination, the total
1635 height is the height of the tallest window, and the width is the
1636 x we finally reached, corrected by the x we started with. */
1637 if (in_horz_combination_p)
1638 {
1639 total.width = x - x0;
1640 total.height = hmax;
1641 }
177c0ea7 1642 else
5f5c8ee5
GM
1643 {
1644 total.width = wmax;
1645 total.height = y - y0;
1646 }
1647
1648 return total;
1649}
1650
1651
5d3cc43c
GM
1652/* Return the required height of glyph matrices for window W. */
1653
2f7c71a1 1654static int
971de7fb 1655required_matrix_height (struct window *w)
5d3cc43c
GM
1656{
1657#ifdef HAVE_WINDOW_SYSTEM
d3d50620 1658 struct frame *f = XFRAME (w->frame);
177c0ea7 1659
5d3cc43c
GM
1660 if (FRAME_WINDOW_P (f))
1661 {
1662 int ch_height = FRAME_SMALLEST_FONT_HEIGHT (f);
1ea40aa2 1663 int window_pixel_height = window_box_height (w) + eabs (w->vscroll);
880e6158 1664
5d3cc43c 1665 return (((window_pixel_height + ch_height - 1)
cc834cf9 1666 / ch_height) * w->nrows_scale_factor
5d3cc43c
GM
1667 /* One partially visible line at the top and
1668 bottom of the window. */
1669 + 2
153a073c 1670 /* 2 for header and mode line. */
5d3cc43c
GM
1671 + 2);
1672 }
1673#endif /* HAVE_WINDOW_SYSTEM */
177c0ea7 1674
7df02016 1675 return WINDOW_TOTAL_LINES (w);
5d3cc43c
GM
1676}
1677
1678
1679/* Return the required width of glyph matrices for window W. */
1680
2f7c71a1 1681static int
971de7fb 1682required_matrix_width (struct window *w)
5d3cc43c
GM
1683{
1684#ifdef HAVE_WINDOW_SYSTEM
d3d50620 1685 struct frame *f = XFRAME (w->frame);
5d3cc43c
GM
1686 if (FRAME_WINDOW_P (f))
1687 {
1688 int ch_width = FRAME_SMALLEST_CHAR_WIDTH (f);
177c0ea7 1689
5d3cc43c 1690 /* Compute number of glyphs needed in a glyph row. */
880e6158 1691 return (((WINDOW_PIXEL_WIDTH (w) + ch_width - 1)
cc834cf9 1692 / ch_width) * w->ncols_scale_factor
5d3cc43c
GM
1693 /* 2 partially visible columns in the text area. */
1694 + 2
1695 /* One partially visible column at the right
1696 edge of each marginal area. */
1697 + 1 + 1);
1698 }
1699#endif /* HAVE_WINDOW_SYSTEM */
1700
5f24fa51 1701 return w->total_cols;
5d3cc43c
GM
1702}
1703
1704
5f5c8ee5 1705/* Allocate window matrices for window-based redisplay. W is the
8ce2650d 1706 window whose matrices must be allocated/reallocated. */
177c0ea7 1707
5f5c8ee5 1708static void
971de7fb 1709allocate_matrices_for_window_redisplay (struct window *w)
5f5c8ee5 1710{
5f5c8ee5
GM
1711 while (w)
1712 {
e74aeda8
DA
1713 if (WINDOWP (w->contents))
1714 allocate_matrices_for_window_redisplay (XWINDOW (w->contents));
5f5c8ee5
GM
1715 else
1716 {
1717 /* W is a leaf window. */
5f5c8ee5
GM
1718 struct dim dim;
1719
1720 /* If matrices are not yet allocated, allocate them now. */
1721 if (w->desired_matrix == NULL)
1722 {
1723 w->desired_matrix = new_glyph_matrix (NULL);
1724 w->current_matrix = new_glyph_matrix (NULL);
1725 }
1726
5d3cc43c
GM
1727 dim.width = required_matrix_width (w);
1728 dim.height = required_matrix_height (w);
5f5c8ee5
GM
1729 adjust_glyph_matrix (w, w->desired_matrix, 0, 0, dim);
1730 adjust_glyph_matrix (w, w->current_matrix, 0, 0, dim);
1731 }
177c0ea7 1732
d3d50620 1733 w = NILP (w->next) ? NULL : XWINDOW (w->next);
5f5c8ee5
GM
1734 }
1735}
1736
bd0443bb
DA
1737/* Allocate/reallocate glyph matrices of a single frame F.
1738 This function must be called when a new frame is created,
1739 its size changes, or its window configuration changes. */
5f5c8ee5
GM
1740
1741void
bd0443bb 1742adjust_frame_glyphs (struct frame *f)
5f5c8ee5 1743{
408f5064
GM
1744 /* Block input so that expose events and other events that access
1745 glyph matrices are not processed while we are changing them. */
4d7e6e51 1746 block_input ();
408f5064 1747
5f5c8ee5
GM
1748 if (FRAME_WINDOW_P (f))
1749 adjust_frame_glyphs_for_window_redisplay (f);
1750 else
1751 adjust_frame_glyphs_for_frame_redisplay (f);
177c0ea7 1752
b09cca6a 1753 /* Don't forget the buffer for decode_mode_spec. */
5f5c8ee5
GM
1754 adjust_decode_mode_spec_buffer (f);
1755
1756 f->glyphs_initialized_p = 1;
bd0443bb
DA
1757
1758 unblock_input ();
5f5c8ee5
GM
1759}
1760
59ea14cd 1761/* Return true if any window in the tree has nonzero window margins. See
075757cc 1762 the hack at the end of adjust_frame_glyphs_for_frame_redisplay. */
59ea14cd 1763static bool
971de7fb 1764showing_window_margins_p (struct window *w)
075757cc
CY
1765{
1766 while (w)
1767 {
e74aeda8 1768 if (WINDOWP (w->contents))
075757cc 1769 {
e74aeda8 1770 if (showing_window_margins_p (XWINDOW (w->contents)))
075757cc
CY
1771 return 1;
1772 }
eeaf9bf3 1773 else if (w->left_margin_cols > 0 || w->right_margin_cols > 0)
075757cc 1774 return 1;
3fc809e2 1775
d3d50620 1776 w = NILP (w->next) ? 0 : XWINDOW (w->next);
075757cc
CY
1777 }
1778 return 0;
1779}
1780
5f5c8ee5 1781
c9f00270
GM
1782/* In the window tree with root W, build current matrices of leaf
1783 windows from the frame's current matrix. */
1784
1785static void
971de7fb 1786fake_current_matrices (Lisp_Object window)
c9f00270
GM
1787{
1788 struct window *w;
177c0ea7 1789
d3d50620 1790 for (; !NILP (window); window = w->next)
c9f00270
GM
1791 {
1792 w = XWINDOW (window);
177c0ea7 1793
e74aeda8
DA
1794 if (WINDOWP (w->contents))
1795 fake_current_matrices (w->contents);
c9f00270
GM
1796 else
1797 {
1798 int i;
d3d50620 1799 struct frame *f = XFRAME (w->frame);
c9f00270
GM
1800 struct glyph_matrix *m = w->current_matrix;
1801 struct glyph_matrix *fm = f->current_matrix;
1802
a54e2c05
DA
1803 eassert (m->matrix_h == WINDOW_TOTAL_LINES (w));
1804 eassert (m->matrix_w == WINDOW_TOTAL_COLS (w));
177c0ea7 1805
c9f00270
GM
1806 for (i = 0; i < m->matrix_h; ++i)
1807 {
1808 struct glyph_row *r = m->rows + i;
7df02016 1809 struct glyph_row *fr = fm->rows + i + WINDOW_TOP_EDGE_LINE (w);
c9f00270 1810
a54e2c05 1811 eassert (r->glyphs[TEXT_AREA] >= fr->glyphs[TEXT_AREA]
c9f00270
GM
1812 && r->glyphs[LAST_AREA] <= fr->glyphs[LAST_AREA]);
1813
1814 r->enabled_p = fr->enabled_p;
1815 if (r->enabled_p)
1816 {
1817 r->used[LEFT_MARGIN_AREA] = m->left_margin_glyphs;
1818 r->used[RIGHT_MARGIN_AREA] = m->right_margin_glyphs;
1819 r->used[TEXT_AREA] = (m->matrix_w
1820 - r->used[LEFT_MARGIN_AREA]
1821 - r->used[RIGHT_MARGIN_AREA]);
1822 r->mode_line_p = 0;
c9f00270
GM
1823 }
1824 }
1825 }
1826 }
1827}
1828
1829
bccee4f2 1830/* Save away the contents of frame F's current frame matrix. Value is
153a073c 1831 a glyph matrix holding the contents of F's current frame matrix. */
c9f00270 1832
bccee4f2 1833static struct glyph_matrix *
971de7fb 1834save_current_matrix (struct frame *f)
c9f00270 1835{
bccee4f2 1836 int i;
38182d90 1837 struct glyph_matrix *saved = xzalloc (sizeof *saved);
bccee4f2 1838 saved->nrows = f->current_matrix->nrows;
23f86fce 1839 saved->rows = xzalloc (saved->nrows * sizeof *saved->rows);
bccee4f2
GM
1840
1841 for (i = 0; i < saved->nrows; ++i)
31798cfe 1842 {
bccee4f2
GM
1843 struct glyph_row *from = f->current_matrix->rows + i;
1844 struct glyph_row *to = saved->rows + i;
dfd153ae 1845 ptrdiff_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
141f1ff7 1846
23f86fce 1847 to->glyphs[TEXT_AREA] = xmalloc (nbytes);
72af86bd 1848 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
bccee4f2 1849 to->used[TEXT_AREA] = from->used[TEXT_AREA];
fa93733d
EZ
1850 to->enabled_p = from->enabled_p;
1851 to->hash = from->hash;
141f1ff7
EZ
1852 if (from->used[LEFT_MARGIN_AREA])
1853 {
1854 nbytes = from->used[LEFT_MARGIN_AREA] * sizeof (struct glyph);
3b158d11 1855 to->glyphs[LEFT_MARGIN_AREA] = xmalloc (nbytes);
141f1ff7
EZ
1856 memcpy (to->glyphs[LEFT_MARGIN_AREA],
1857 from->glyphs[LEFT_MARGIN_AREA], nbytes);
1858 to->used[LEFT_MARGIN_AREA] = from->used[LEFT_MARGIN_AREA];
1859 }
1860 if (from->used[RIGHT_MARGIN_AREA])
1861 {
1862 nbytes = from->used[RIGHT_MARGIN_AREA] * sizeof (struct glyph);
3b158d11 1863 to->glyphs[RIGHT_MARGIN_AREA] = xmalloc (nbytes);
141f1ff7
EZ
1864 memcpy (to->glyphs[RIGHT_MARGIN_AREA],
1865 from->glyphs[RIGHT_MARGIN_AREA], nbytes);
1866 to->used[RIGHT_MARGIN_AREA] = from->used[RIGHT_MARGIN_AREA];
1867 }
e797dea6 1868 }
bccee4f2
GM
1869
1870 return saved;
1871}
1872
1873
1874/* Restore the contents of frame F's current frame matrix from SAVED,
1875 and free memory associated with SAVED. */
1876
1877static void
971de7fb 1878restore_current_matrix (struct frame *f, struct glyph_matrix *saved)
bccee4f2
GM
1879{
1880 int i;
1881
1882 for (i = 0; i < saved->nrows; ++i)
e797dea6 1883 {
bccee4f2
GM
1884 struct glyph_row *from = saved->rows + i;
1885 struct glyph_row *to = f->current_matrix->rows + i;
dfd153ae 1886 ptrdiff_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
141f1ff7 1887
72af86bd 1888 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
e797dea6 1889 to->used[TEXT_AREA] = from->used[TEXT_AREA];
bccee4f2 1890 xfree (from->glyphs[TEXT_AREA]);
2fa833da 1891 nbytes = from->used[LEFT_MARGIN_AREA] * sizeof (struct glyph);
141f1ff7
EZ
1892 if (nbytes)
1893 {
1894 memcpy (to->glyphs[LEFT_MARGIN_AREA],
1895 from->glyphs[LEFT_MARGIN_AREA], nbytes);
1896 to->used[LEFT_MARGIN_AREA] = from->used[LEFT_MARGIN_AREA];
1897 xfree (from->glyphs[LEFT_MARGIN_AREA]);
1898 }
1899 else
1900 to->used[LEFT_MARGIN_AREA] = 0;
2fa833da 1901 nbytes = from->used[RIGHT_MARGIN_AREA] * sizeof (struct glyph);
141f1ff7
EZ
1902 if (nbytes)
1903 {
1904 memcpy (to->glyphs[RIGHT_MARGIN_AREA],
1905 from->glyphs[RIGHT_MARGIN_AREA], nbytes);
1906 to->used[RIGHT_MARGIN_AREA] = from->used[RIGHT_MARGIN_AREA];
1907 xfree (from->glyphs[RIGHT_MARGIN_AREA]);
1908 }
1909 else
1910 to->used[RIGHT_MARGIN_AREA] = 0;
e797dea6 1911 }
177c0ea7 1912
bccee4f2
GM
1913 xfree (saved->rows);
1914 xfree (saved);
c9f00270
GM
1915}
1916
1917
bccee4f2 1918
5f5c8ee5
GM
1919/* Allocate/reallocate glyph matrices of a single frame F for
1920 frame-based redisplay. */
1921
1922static void
971de7fb 1923adjust_frame_glyphs_for_frame_redisplay (struct frame *f)
5f5c8ee5 1924{
5f5c8ee5 1925 struct dim matrix_dim;
59ea14cd 1926 bool pool_changed_p;
5f5c8ee5
GM
1927 int window_change_flags;
1928 int top_window_y;
1929
1930 if (!FRAME_LIVE_P (f))
1931 return;
1932
5f5c8ee5
GM
1933 top_window_y = FRAME_TOP_MARGIN (f);
1934
1935 /* Allocate glyph pool structures if not already done. */
1936 if (f->desired_pool == NULL)
1937 {
1938 f->desired_pool = new_glyph_pool ();
1939 f->current_pool = new_glyph_pool ();
1940 }
1941
1942 /* Allocate frames matrix structures if needed. */
1943 if (f->desired_matrix == NULL)
1944 {
1945 f->desired_matrix = new_glyph_matrix (f->desired_pool);
1946 f->current_matrix = new_glyph_matrix (f->current_pool);
1947 }
177c0ea7 1948
5f5c8ee5
GM
1949 /* Compute window glyph matrices. (This takes the mini-buffer
1950 window into account). The result is the size of the frame glyph
1951 matrix needed. The variable window_change_flags is set to a bit
1952 mask indicating whether new matrices will be allocated or
1953 existing matrices change their size or location within the frame
1954 matrix. */
1955 window_change_flags = 0;
1956 matrix_dim
1957 = allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f),
1958 0, top_window_y,
5d3cc43c 1959 1,
5f5c8ee5
GM
1960 &window_change_flags);
1961
1962 /* Add in menu bar lines, if any. */
1963 matrix_dim.height += top_window_y;
1964
1965 /* Enlarge pools as necessary. */
1966 pool_changed_p = realloc_glyph_pool (f->desired_pool, matrix_dim);
1967 realloc_glyph_pool (f->current_pool, matrix_dim);
1968
177c0ea7 1969 /* Set up glyph pointers within window matrices. Do this only if
5f5c8ee5
GM
1970 absolutely necessary since it requires a frame redraw. */
1971 if (pool_changed_p || window_change_flags)
1972 {
1973 /* Do it for window matrices. */
1974 allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f),
5d3cc43c 1975 0, top_window_y, 0,
5f5c8ee5
GM
1976 &window_change_flags);
1977
1978 /* Size of frame matrices must equal size of frame. Note
1979 that we are called for X frames with window widths NOT equal
1980 to the frame width (from CHANGE_FRAME_SIZE_1). */
880e6158
MR
1981 if (matrix_dim.width != FRAME_COLS (f)
1982 || matrix_dim.height != FRAME_LINES (f))
1983 return;
1984
a54e2c05 1985 eassert (matrix_dim.width == FRAME_COLS (f)
7df02016 1986 && matrix_dim.height == FRAME_LINES (f));
177c0ea7 1987
e797dea6
GM
1988 /* Pointers to glyph memory in glyph rows are exchanged during
1989 the update phase of redisplay, which means in general that a
1990 frame's current matrix consists of pointers into both the
1991 desired and current glyph pool of the frame. Adjusting a
1992 matrix sets the frame matrix up so that pointers are all into
1993 the same pool. If we want to preserve glyph contents of the
1994 current matrix over a call to adjust_glyph_matrix, we must
1995 make a copy of the current glyphs, and restore the current
1996 matrix' contents from that copy. */
1997 if (display_completed
1998 && !FRAME_GARBAGED_P (f)
1999 && matrix_dim.width == f->current_matrix->matrix_w
075757cc
CY
2000 && matrix_dim.height == f->current_matrix->matrix_h
2001 /* For some reason, the frame glyph matrix gets corrupted if
2002 any of the windows contain margins. I haven't been able
2003 to hunt down the reason, but for the moment this prevents
2004 the problem from manifesting. -- cyd */
d5c20fe8 2005 && !showing_window_margins_p (XWINDOW (FRAME_ROOT_WINDOW (f))))
e797dea6 2006 {
bccee4f2
GM
2007 struct glyph_matrix *copy = save_current_matrix (f);
2008 adjust_glyph_matrix (NULL, f->desired_matrix, 0, 0, matrix_dim);
e797dea6 2009 adjust_glyph_matrix (NULL, f->current_matrix, 0, 0, matrix_dim);
bccee4f2 2010 restore_current_matrix (f, copy);
e797dea6
GM
2011 fake_current_matrices (FRAME_ROOT_WINDOW (f));
2012 }
c9f00270 2013 else
e797dea6 2014 {
bccee4f2 2015 adjust_glyph_matrix (NULL, f->desired_matrix, 0, 0, matrix_dim);
e797dea6
GM
2016 adjust_glyph_matrix (NULL, f->current_matrix, 0, 0, matrix_dim);
2017 SET_FRAME_GARBAGED (f);
2018 }
5f5c8ee5
GM
2019 }
2020}
2021
2022
2023/* Allocate/reallocate glyph matrices of a single frame F for
2024 window-based redisplay. */
2025
2026static void
971de7fb 2027adjust_frame_glyphs_for_window_redisplay (struct frame *f)
5f5c8ee5 2028{
a54e2c05 2029 eassert (FRAME_WINDOW_P (f) && FRAME_LIVE_P (f));
177c0ea7 2030
5f5c8ee5 2031 /* Allocate/reallocate window matrices. */
5d3cc43c 2032 allocate_matrices_for_window_redisplay (XWINDOW (FRAME_ROOT_WINDOW (f)));
5f5c8ee5 2033
742516e0 2034#if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
5f5c8ee5
GM
2035 /* Allocate/ reallocate matrices of the dummy window used to display
2036 the menu bar under X when no X toolkit support is available. */
5f5c8ee5
GM
2037 {
2038 /* Allocate a dummy window if not already done. */
d2d37ddc 2039 struct window *w;
e69b0960 2040 if (NILP (f->menu_bar_window))
5f5c8ee5 2041 {
077288cf 2042 Lisp_Object frame;
f00af5b1 2043 fset_menu_bar_window (f, make_window ());
e69b0960 2044 w = XWINDOW (f->menu_bar_window);
077288cf 2045 XSETFRAME (frame, f);
e8c17b81 2046 wset_frame (w, frame);
5f5c8ee5
GM
2047 w->pseudo_window_p = 1;
2048 }
2049 else
e69b0960 2050 w = XWINDOW (f->menu_bar_window);
5f5c8ee5
GM
2051
2052 /* Set window dimensions to frame dimensions and allocate or
2053 adjust glyph matrices of W. */
880e6158 2054 w->pixel_left = 0;
5f24fa51 2055 w->left_col = 0;
880e6158
MR
2056 w->pixel_top = 0;
2057 w->top_line = 0;
7d7ccb88
MR
2058 w->pixel_width = (FRAME_PIXEL_WIDTH (f)
2059 - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5f24fa51 2060 w->total_cols = FRAME_TOTAL_COLS (f);
880e6158
MR
2061 w->pixel_height = FRAME_MENU_BAR_HEIGHT (f);
2062 w->total_lines = FRAME_MENU_BAR_LINES (f);
5d3cc43c 2063 allocate_matrices_for_window_redisplay (w);
5f5c8ee5 2064 }
742516e0 2065#endif
5f5c8ee5 2066
742516e0 2067#if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
d2d37ddc
PE
2068 {
2069 /* Allocate/ reallocate matrices of the tool bar window. If we
2070 don't have a tool bar window yet, make one. */
2071 struct window *w;
e69b0960 2072 if (NILP (f->tool_bar_window))
d2d37ddc 2073 {
077288cf 2074 Lisp_Object frame;
f00af5b1 2075 fset_tool_bar_window (f, make_window ());
e69b0960 2076 w = XWINDOW (f->tool_bar_window);
077288cf 2077 XSETFRAME (frame, f);
e8c17b81 2078 wset_frame (w, frame);
d2d37ddc
PE
2079 w->pseudo_window_p = 1;
2080 }
2081 else
e69b0960 2082 w = XWINDOW (f->tool_bar_window);
5f5c8ee5 2083
880e6158 2084 w->pixel_left = 0;
5f24fa51 2085 w->left_col = 0;
880e6158
MR
2086 w->pixel_top = FRAME_MENU_BAR_HEIGHT (f);
2087 w->top_line = FRAME_MENU_BAR_LINES (f);
2088 w->pixel_width = (FRAME_PIXEL_WIDTH (f)
2089 - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5f24fa51 2090 w->total_cols = FRAME_TOTAL_COLS (f);
880e6158
MR
2091 w->pixel_height = FRAME_TOOL_BAR_HEIGHT (f);
2092 w->total_lines = FRAME_TOOL_BAR_LINES (f);
d2d37ddc
PE
2093 allocate_matrices_for_window_redisplay (w);
2094 }
488dd4c4 2095#endif
5f5c8ee5
GM
2096}
2097
2098
5f5c8ee5
GM
2099/* Re-allocate buffer for decode_mode_spec on frame F. */
2100
2101static void
971de7fb 2102adjust_decode_mode_spec_buffer (struct frame *f)
5f5c8ee5 2103{
38182d90
PE
2104 f->decode_mode_spec_buffer = xrealloc (f->decode_mode_spec_buffer,
2105 FRAME_MESSAGE_BUF_SIZE (f) + 1);
5f5c8ee5
GM
2106}
2107
2108
2109\f
2110/**********************************************************************
2111 Freeing Glyph Matrices
2112 **********************************************************************/
2113
2114/* Free glyph memory for a frame F. F may be null. This function can
2115 be called for the same frame more than once. The root window of
2116 F may be nil when this function is called. This is the case when
2117 the function is called when F is destroyed. */
2118
2119void
971de7fb 2120free_glyphs (struct frame *f)
5f5c8ee5
GM
2121{
2122 if (f && f->glyphs_initialized_p)
2123 {
42f55fe0
GM
2124 /* Block interrupt input so that we don't get surprised by an X
2125 event while we're in an inconsistent state. */
4d7e6e51 2126 block_input ();
5f5c8ee5 2127 f->glyphs_initialized_p = 0;
177c0ea7 2128
5f5c8ee5 2129 /* Release window sub-matrices. */
e69b0960
DA
2130 if (!NILP (f->root_window))
2131 free_window_matrices (XWINDOW (f->root_window));
5f5c8ee5 2132
5a49b79c 2133#if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
5f5c8ee5
GM
2134 /* Free the dummy window for menu bars without X toolkit and its
2135 glyph matrices. */
e69b0960 2136 if (!NILP (f->menu_bar_window))
5f5c8ee5 2137 {
e69b0960 2138 struct window *w = XWINDOW (f->menu_bar_window);
5f5c8ee5
GM
2139 free_glyph_matrix (w->desired_matrix);
2140 free_glyph_matrix (w->current_matrix);
2141 w->desired_matrix = w->current_matrix = NULL;
f00af5b1 2142 fset_menu_bar_window (f, Qnil);
5f5c8ee5 2143 }
5a49b79c 2144#endif
5f5c8ee5 2145
742516e0 2146#if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
9ea173e8 2147 /* Free the tool bar window and its glyph matrices. */
e69b0960 2148 if (!NILP (f->tool_bar_window))
5f5c8ee5 2149 {
e69b0960 2150 struct window *w = XWINDOW (f->tool_bar_window);
5f5c8ee5
GM
2151 free_glyph_matrix (w->desired_matrix);
2152 free_glyph_matrix (w->current_matrix);
2153 w->desired_matrix = w->current_matrix = NULL;
f00af5b1 2154 fset_tool_bar_window (f, Qnil);
5f5c8ee5 2155 }
742516e0 2156#endif
5f5c8ee5
GM
2157
2158 /* Release frame glyph matrices. Reset fields to zero in
2159 case we are called a second time. */
2160 if (f->desired_matrix)
2161 {
2162 free_glyph_matrix (f->desired_matrix);
2163 free_glyph_matrix (f->current_matrix);
2164 f->desired_matrix = f->current_matrix = NULL;
2165 }
2166
2167 /* Release glyph pools. */
2168 if (f->desired_pool)
2169 {
2170 free_glyph_pool (f->desired_pool);
2171 free_glyph_pool (f->current_pool);
2172 f->desired_pool = f->current_pool = NULL;
2173 }
177c0ea7 2174
4d7e6e51 2175 unblock_input ();
5f5c8ee5
GM
2176 }
2177}
2178
2179
2180/* Free glyph sub-matrices in the window tree rooted at W. This
2181 function may be called with a null pointer, and it may be called on
2182 the same tree more than once. */
2183
2184void
971de7fb 2185free_window_matrices (struct window *w)
5f5c8ee5
GM
2186{
2187 while (w)
2188 {
e74aeda8
DA
2189 if (WINDOWP (w->contents))
2190 free_window_matrices (XWINDOW (w->contents));
177c0ea7 2191 else
5f5c8ee5
GM
2192 {
2193 /* This is a leaf window. Free its memory and reset fields
2194 to zero in case this function is called a second time for
2195 W. */
2196 free_glyph_matrix (w->current_matrix);
2197 free_glyph_matrix (w->desired_matrix);
2198 w->current_matrix = w->desired_matrix = NULL;
2199 }
2200
2201 /* Next window on same level. */
d3d50620 2202 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5f5c8ee5
GM
2203 }
2204}
2205
2206
2207/* Check glyph memory leaks. This function is called from
2208 shut_down_emacs. Note that frames are not destroyed when Emacs
2209 exits. We therefore free all glyph memory for all active frames
2210 explicitly and check that nothing is left allocated. */
2211
2212void
971de7fb 2213check_glyph_memory (void)
5f5c8ee5
GM
2214{
2215 Lisp_Object tail, frame;
2216
2217 /* Free glyph memory for all frames. */
2218 FOR_EACH_FRAME (tail, frame)
2219 free_glyphs (XFRAME (frame));
2220
f0b79313 2221#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
5f5c8ee5 2222 /* Check that nothing is left allocated. */
f0b79313
DA
2223 eassert (glyph_matrix_count == 0);
2224 eassert (glyph_pool_count == 0);
2225#endif
5f5c8ee5
GM
2226}
2227
2228
2229\f
2230/**********************************************************************
2231 Building a Frame Matrix
2232 **********************************************************************/
2233
2234/* Most of the redisplay code works on glyph matrices attached to
2235 windows. This is a good solution most of the time, but it is not
2236 suitable for terminal code. Terminal output functions cannot rely
2237 on being able to set an arbitrary terminal window. Instead they
2238 must be provided with a view of the whole frame, i.e. the whole
2239 screen. We build such a view by constructing a frame matrix from
2240 window matrices in this section.
2241
65620264 2242 Windows that must be updated have their must_be_updated_p flag set.
5f5c8ee5
GM
2243 For all such windows, their desired matrix is made part of the
2244 desired frame matrix. For other windows, their current matrix is
2245 made part of the desired frame matrix.
2246
2247 +-----------------+----------------+
2248 | desired | desired |
2249 | | |
2250 +-----------------+----------------+
2251 | current |
2252 | |
2253 +----------------------------------+
2254
2255 Desired window matrices can be made part of the frame matrix in a
2256 cheap way: We exploit the fact that the desired frame matrix and
2257 desired window matrices share their glyph memory. This is not
2258 possible for current window matrices. Their glyphs are copied to
2259 the desired frame matrix. The latter is equivalent to
2260 preserve_other_columns in the old redisplay.
2261
2262 Used glyphs counters for frame matrix rows are the result of adding
2263 up glyph lengths of the window matrices. A line in the frame
2264 matrix is enabled, if a corresponding line in a window matrix is
2265 enabled.
177c0ea7 2266
5f5c8ee5
GM
2267 After building the desired frame matrix, it will be passed to
2268 terminal code, which will manipulate both the desired and current
2269 frame matrix. Changes applied to the frame's current matrix have
2270 to be visible in current window matrices afterwards, of course.
2271
2272 This problem is solved like this:
2273
2274 1. Window and frame matrices share glyphs. Window matrices are
2275 constructed in a way that their glyph contents ARE the glyph
2276 contents needed in a frame matrix. Thus, any modification of
2277 glyphs done in terminal code will be reflected in window matrices
2278 automatically.
177c0ea7 2279
5f5c8ee5
GM
2280 2. Exchanges of rows in a frame matrix done by terminal code are
2281 intercepted by hook functions so that corresponding row operations
2282 on window matrices can be performed. This is necessary because we
2283 use pointers to glyphs in glyph row structures. To satisfy the
2284 assumption of point 1 above that glyphs are updated implicitly in
2285 window matrices when they are manipulated via the frame matrix,
2286 window and frame matrix must of course agree where to find the
2287 glyphs for their rows. Possible manipulations that must be
2288 mirrored are assignments of rows of the desired frame matrix to the
2289 current frame matrix and scrolling the current frame matrix. */
2290
2291/* Build frame F's desired matrix from window matrices. Only windows
2292 which have the flag must_be_updated_p set have to be updated. Menu
2293 bar lines of a frame are not covered by window matrices, so make
2294 sure not to touch them in this function. */
2295
2296static void
971de7fb 2297build_frame_matrix (struct frame *f)
5f5c8ee5
GM
2298{
2299 int i;
2300
2301 /* F must have a frame matrix when this function is called. */
a54e2c05 2302 eassert (!FRAME_WINDOW_P (f));
177c0ea7 2303
5f5c8ee5
GM
2304 /* Clear all rows in the frame matrix covered by window matrices.
2305 Menu bar lines are not covered by windows. */
2306 for (i = FRAME_TOP_MARGIN (f); i < f->desired_matrix->nrows; ++i)
2307 clear_glyph_row (MATRIX_ROW (f->desired_matrix, i));
2308
2309 /* Build the matrix by walking the window tree. */
2310 build_frame_matrix_from_window_tree (f->desired_matrix,
2311 XWINDOW (FRAME_ROOT_WINDOW (f)));
2312}
2313
2314
2315/* Walk a window tree, building a frame matrix MATRIX from window
2316 matrices. W is the root of a window tree. */
2317
2318static void
971de7fb 2319build_frame_matrix_from_window_tree (struct glyph_matrix *matrix, struct window *w)
5f5c8ee5
GM
2320{
2321 while (w)
2322 {
e74aeda8
DA
2323 if (WINDOWP (w->contents))
2324 build_frame_matrix_from_window_tree (matrix, XWINDOW (w->contents));
5f5c8ee5
GM
2325 else
2326 build_frame_matrix_from_leaf_window (matrix, w);
2327
d3d50620 2328 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5f5c8ee5
GM
2329 }
2330}
2331
2332
2333/* Add a window's matrix to a frame matrix. FRAME_MATRIX is the
2334 desired frame matrix built. W is a leaf window whose desired or
2335 current matrix is to be added to FRAME_MATRIX. W's flag
2336 must_be_updated_p determines which matrix it contributes to
59ea14cd 2337 FRAME_MATRIX. If W->must_be_updated_p, W's desired matrix
5f5c8ee5
GM
2338 is added to FRAME_MATRIX, otherwise W's current matrix is added.
2339 Adding a desired matrix means setting up used counters and such in
2340 frame rows, while adding a current window matrix to FRAME_MATRIX
2341 means copying glyphs. The latter case corresponds to
2342 preserve_other_columns in the old redisplay. */
2343
2344static void
971de7fb 2345build_frame_matrix_from_leaf_window (struct glyph_matrix *frame_matrix, struct window *w)
5f5c8ee5
GM
2346{
2347 struct glyph_matrix *window_matrix;
2348 int window_y, frame_y;
2349 /* If non-zero, a glyph to insert at the right border of W. */
3fc809e2
KS
2350 GLYPH right_border_glyph;
2351
2352 SET_GLYPH_FROM_CHAR (right_border_glyph, 0);
5f5c8ee5
GM
2353
2354 /* Set window_matrix to the matrix we have to add to FRAME_MATRIX. */
2355 if (w->must_be_updated_p)
2356 {
2357 window_matrix = w->desired_matrix;
2358
2359 /* Decide whether we want to add a vertical border glyph. */
2360 if (!WINDOW_RIGHTMOST_P (w))
2361 {
2362 struct Lisp_Char_Table *dp = window_display_table (w);
3fc809e2 2363 Lisp_Object gc;
b4e49aee 2364
3fc809e2
KS
2365 SET_GLYPH_FROM_CHAR (right_border_glyph, '|');
2366 if (dp
d311d28c 2367 && (gc = DISP_BORDER_GLYPH (dp), GLYPH_CODE_P (gc)))
3fc809e2
KS
2368 {
2369 SET_GLYPH_FROM_GLYPH_CODE (right_border_glyph, gc);
2370 spec_glyph_lookup_face (w, &right_border_glyph);
2371 }
b4e49aee 2372
3fc809e2
KS
2373 if (GLYPH_FACE (right_border_glyph) <= 0)
2374 SET_GLYPH_FACE (right_border_glyph, VERTICAL_BORDER_FACE_ID);
5f5c8ee5
GM
2375 }
2376 }
2377 else
2378 window_matrix = w->current_matrix;
2379
2380 /* For all rows in the window matrix and corresponding rows in the
2381 frame matrix. */
2382 window_y = 0;
2383 frame_y = window_matrix->matrix_y;
2384 while (window_y < window_matrix->nrows)
2385 {
2386 struct glyph_row *frame_row = frame_matrix->rows + frame_y;
2387 struct glyph_row *window_row = window_matrix->rows + window_y;
59ea14cd 2388 bool current_row_p = window_matrix == w->current_matrix;
5f5c8ee5
GM
2389
2390 /* Fill up the frame row with spaces up to the left margin of the
2391 window row. */
2392 fill_up_frame_row_with_spaces (frame_row, window_matrix->matrix_x);
2393
2394 /* Fill up areas in the window matrix row with spaces. */
2395 fill_up_glyph_row_with_spaces (window_row);
2a8bd25f
GM
2396
2397 /* If only part of W's desired matrix has been built, and
2398 window_row wasn't displayed, use the corresponding current
2399 row instead. */
2400 if (window_matrix == w->desired_matrix
2401 && !window_row->enabled_p)
2402 {
2403 window_row = w->current_matrix->rows + window_y;
2404 current_row_p = 1;
2405 }
177c0ea7 2406
2a8bd25f 2407 if (current_row_p)
5f5c8ee5 2408 {
2a8bd25f 2409 /* Copy window row to frame row. */
72af86bd
AS
2410 memcpy (frame_row->glyphs[TEXT_AREA] + window_matrix->matrix_x,
2411 window_row->glyphs[0],
2412 window_matrix->matrix_w * sizeof (struct glyph));
5f5c8ee5
GM
2413 }
2414 else
2415 {
a54e2c05 2416 eassert (window_row->enabled_p);
177c0ea7 2417
2a8bd25f
GM
2418 /* Only when a desired row has been displayed, we want
2419 the corresponding frame row to be updated. */
96c06863 2420 frame_row->enabled_p = true;
177c0ea7 2421
5f5c8ee5
GM
2422 /* Maybe insert a vertical border between horizontally adjacent
2423 windows. */
3fc809e2 2424 if (GLYPH_CHAR (right_border_glyph) != 0)
5f5c8ee5
GM
2425 {
2426 struct glyph *border = window_row->glyphs[LAST_AREA] - 1;
2427 SET_CHAR_GLYPH_FROM_GLYPH (*border, right_border_glyph);
2428 }
2429
e509cfa6 2430#ifdef GLYPH_DEBUG
a13396c9
GM
2431 /* Window row window_y must be a slice of frame row
2432 frame_y. */
a54e2c05 2433 eassert (glyph_row_slice_p (window_row, frame_row));
177c0ea7 2434
5f5c8ee5
GM
2435 /* If rows are in sync, we don't have to copy glyphs because
2436 frame and window share glyphs. */
177c0ea7 2437
a38634ff 2438 strcpy (w->current_matrix->method, w->desired_matrix->method);
9b0e97aa 2439 add_window_display_history (w, w->current_matrix->method, 0);
a38634ff 2440#endif
5f5c8ee5
GM
2441 }
2442
2443 /* Set number of used glyphs in the frame matrix. Since we fill
2444 up with spaces, and visit leaf windows from left to right it
2445 can be done simply. */
177c0ea7 2446 frame_row->used[TEXT_AREA]
5f5c8ee5
GM
2447 = window_matrix->matrix_x + window_matrix->matrix_w;
2448
5f5c8ee5
GM
2449 /* Next row. */
2450 ++window_y;
2451 ++frame_y;
2452 }
2453}
2454
5f81871e
RS
2455/* Given a user-specified glyph, possibly including a Lisp-level face
2456 ID, return a glyph that has a realized face ID.
2457 This is used for glyphs displayed specially and not part of the text;
2458 for instance, vertical separators, truncation markers, etc. */
2459
3fc809e2 2460void
971de7fb 2461spec_glyph_lookup_face (struct window *w, GLYPH *glyph)
5f81871e 2462{
3fc809e2 2463 int lface_id = GLYPH_FACE (*glyph);
5f81871e
RS
2464 /* Convert the glyph's specified face to a realized (cache) face. */
2465 if (lface_id > 0)
2466 {
d3d50620 2467 int face_id = merge_faces (XFRAME (w->frame),
5f81871e 2468 Qt, lface_id, DEFAULT_FACE_ID);
3fc809e2 2469 SET_GLYPH_FACE (*glyph, face_id);
5f81871e 2470 }
5f81871e 2471}
5f5c8ee5
GM
2472
2473/* Add spaces to a glyph row ROW in a window matrix.
2474
2475 Each row has the form:
2476
2477 +---------+-----------------------------+------------+
2478 | left | text | right |
2479 +---------+-----------------------------+------------+
2480
2481 Left and right marginal areas are optional. This function adds
2482 spaces to areas so that there are no empty holes between areas.
2483 In other words: If the right area is not empty, the text area
2484 is filled up with spaces up to the right area. If the text area
2485 is not empty, the left area is filled up.
2486
2487 To be called for frame-based redisplay, only. */
2488
2489static void
971de7fb 2490fill_up_glyph_row_with_spaces (struct glyph_row *row)
5f5c8ee5
GM
2491{
2492 fill_up_glyph_row_area_with_spaces (row, LEFT_MARGIN_AREA);
2493 fill_up_glyph_row_area_with_spaces (row, TEXT_AREA);
2494 fill_up_glyph_row_area_with_spaces (row, RIGHT_MARGIN_AREA);
2495}
2496
2497
2498/* Fill area AREA of glyph row ROW with spaces. To be called for
2499 frame-based redisplay only. */
2500
2501static void
971de7fb 2502fill_up_glyph_row_area_with_spaces (struct glyph_row *row, int area)
5f5c8ee5
GM
2503{
2504 if (row->glyphs[area] < row->glyphs[area + 1])
2505 {
2506 struct glyph *end = row->glyphs[area + 1];
2507 struct glyph *text = row->glyphs[area] + row->used[area];
2508
2509 while (text < end)
2510 *text++ = space_glyph;
2511 row->used[area] = text - row->glyphs[area];
2512 }
2513}
2514
2515
2516/* Add spaces to the end of ROW in a frame matrix until index UPTO is
2517 reached. In frame matrices only one area, TEXT_AREA, is used. */
2518
abe02020 2519void
971de7fb 2520fill_up_frame_row_with_spaces (struct glyph_row *row, int upto)
5f5c8ee5
GM
2521{
2522 int i = row->used[TEXT_AREA];
2523 struct glyph *glyph = row->glyphs[TEXT_AREA];
177c0ea7 2524
5f5c8ee5
GM
2525 while (i < upto)
2526 glyph[i++] = space_glyph;
2527
2528 row->used[TEXT_AREA] = i;
2529}
2530
2531
2532\f
2533/**********************************************************************
2534 Mirroring operations on frame matrices in window matrices
2535 **********************************************************************/
2536
2537/* Set frame being updated via frame-based redisplay to F. This
2538 function must be called before updates to make explicit that we are
2539 working on frame matrices or not. */
2540
b0ab8123 2541static void
971de7fb 2542set_frame_matrix_frame (struct frame *f)
5f5c8ee5
GM
2543{
2544 frame_matrix_frame = f;
2545}
2546
2547
2548/* Make sure glyph row ROW in CURRENT_MATRIX is up to date.
2549 DESIRED_MATRIX is the desired matrix corresponding to
2550 CURRENT_MATRIX. The update is done by exchanging glyph pointers
2551 between rows in CURRENT_MATRIX and DESIRED_MATRIX. If
2552 frame_matrix_frame is non-null, this indicates that the exchange is
2553 done in frame matrices, and that we have to perform analogous
2554 operations in window matrices of frame_matrix_frame. */
2555
b0ab8123 2556static void
971de7fb 2557make_current (struct glyph_matrix *desired_matrix, struct glyph_matrix *current_matrix, int row)
5f5c8ee5
GM
2558{
2559 struct glyph_row *current_row = MATRIX_ROW (current_matrix, row);
2560 struct glyph_row *desired_row = MATRIX_ROW (desired_matrix, row);
59ea14cd 2561 bool mouse_face_p = current_row->mouse_face_p;
5f5c8ee5
GM
2562
2563 /* Do current_row = desired_row. This exchanges glyph pointers
2564 between both rows, and does a structure assignment otherwise. */
2565 assign_row (current_row, desired_row);
2566
2567 /* Enable current_row to mark it as valid. */
96c06863 2568 current_row->enabled_p = true;
e876ff42 2569 current_row->mouse_face_p = mouse_face_p;
5f5c8ee5
GM
2570
2571 /* If we are called on frame matrices, perform analogous operations
2572 for window matrices. */
2573 if (frame_matrix_frame)
e69b0960 2574 mirror_make_current (XWINDOW (frame_matrix_frame->root_window), row);
5f5c8ee5
GM
2575}
2576
2577
2578/* W is the root of a window tree. FRAME_ROW is the index of a row in
2579 W's frame which has been made current (by swapping pointers between
2580 current and desired matrix). Perform analogous operations in the
2581 matrices of leaf windows in the window tree rooted at W. */
2582
2583static void
971de7fb 2584mirror_make_current (struct window *w, int frame_row)
5f5c8ee5
GM
2585{
2586 while (w)
2587 {
e74aeda8
DA
2588 if (WINDOWP (w->contents))
2589 mirror_make_current (XWINDOW (w->contents), frame_row);
5f5c8ee5
GM
2590 else
2591 {
2592 /* Row relative to window W. Don't use FRAME_TO_WINDOW_VPOS
2593 here because the checks performed in debug mode there
2594 will not allow the conversion. */
2595 int row = frame_row - w->desired_matrix->matrix_y;
2596
2597 /* If FRAME_ROW is within W, assign the desired row to the
2598 current row (exchanging glyph pointers). */
2599 if (row >= 0 && row < w->desired_matrix->matrix_h)
2600 {
2601 struct glyph_row *current_row
2602 = MATRIX_ROW (w->current_matrix, row);
2603 struct glyph_row *desired_row
2604 = MATRIX_ROW (w->desired_matrix, row);
a38634ff
GM
2605
2606 if (desired_row->enabled_p)
2607 assign_row (current_row, desired_row);
2608 else
2609 swap_glyph_pointers (desired_row, current_row);
96c06863 2610 current_row->enabled_p = true;
d009ae66
EZ
2611
2612 /* Set the Y coordinate of the mode/header line's row.
2613 It is needed in draw_row_with_mouse_face to find the
2614 screen coordinates. (Window-based redisplay sets
2615 this in update_window, but no one seems to do that
2616 for frame-based redisplay.) */
2617 if (current_row->mode_line_p)
2618 current_row->y = row;
5f5c8ee5
GM
2619 }
2620 }
177c0ea7 2621
d3d50620 2622 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5f5c8ee5
GM
2623 }
2624}
2625
2626
2627/* Perform row dance after scrolling. We are working on the range of
2628 lines UNCHANGED_AT_TOP + 1 to UNCHANGED_AT_TOP + NLINES (not
2629 including) in MATRIX. COPY_FROM is a vector containing, for each
2630 row I in the range 0 <= I < NLINES, the index of the original line
2631 to move to I. This index is relative to the row range, i.e. 0 <=
2632 index < NLINES. RETAINED_P is a vector containing zero for each
2633 row 0 <= I < NLINES which is empty.
2634
2635 This function is called from do_scrolling and do_direct_scrolling. */
177c0ea7 2636
5f5c8ee5 2637void
6f704c76
DN
2638mirrored_line_dance (struct glyph_matrix *matrix, int unchanged_at_top, int nlines,
2639 int *copy_from, char *retained_p)
5f5c8ee5
GM
2640{
2641 /* A copy of original rows. */
2642 struct glyph_row *old_rows;
2643
2644 /* Rows to assign to. */
2645 struct glyph_row *new_rows = MATRIX_ROW (matrix, unchanged_at_top);
177c0ea7 2646
5f5c8ee5
GM
2647 int i;
2648
2649 /* Make a copy of the original rows. */
38182d90 2650 old_rows = alloca (nlines * sizeof *old_rows);
72af86bd 2651 memcpy (old_rows, new_rows, nlines * sizeof *old_rows);
5f5c8ee5
GM
2652
2653 /* Assign new rows, maybe clear lines. */
2654 for (i = 0; i < nlines; ++i)
2655 {
59ea14cd 2656 bool enabled_before_p = new_rows[i].enabled_p;
5f5c8ee5 2657
a54e2c05
DA
2658 eassert (i + unchanged_at_top < matrix->nrows);
2659 eassert (unchanged_at_top + copy_from[i] < matrix->nrows);
5f5c8ee5
GM
2660 new_rows[i] = old_rows[copy_from[i]];
2661 new_rows[i].enabled_p = enabled_before_p;
2662
2663 /* RETAINED_P is zero for empty lines. */
2664 if (!retained_p[copy_from[i]])
96c06863 2665 new_rows[i].enabled_p = false;
5f5c8ee5
GM
2666 }
2667
153a073c 2668 /* Do the same for window matrices, if MATRIX is a frame matrix. */
5f5c8ee5 2669 if (frame_matrix_frame)
e69b0960 2670 mirror_line_dance (XWINDOW (frame_matrix_frame->root_window),
5f5c8ee5
GM
2671 unchanged_at_top, nlines, copy_from, retained_p);
2672}
2673
2674
a13396c9 2675/* Synchronize glyph pointers in the current matrix of window W with
5cb88111 2676 the current frame matrix. */
a13396c9
GM
2677
2678static void
971de7fb 2679sync_window_with_frame_matrix_rows (struct window *w)
a13396c9 2680{
d3d50620 2681 struct frame *f = XFRAME (w->frame);
a13396c9 2682 struct glyph_row *window_row, *window_row_end, *frame_row;
075c507b 2683 int left, right, x, width;
a13396c9 2684
e74aeda8
DA
2685 /* Preconditions: W must be a live window on a tty frame. */
2686 eassert (BUFFERP (w->contents));
a54e2c05 2687 eassert (!FRAME_WINDOW_P (f));
a13396c9 2688
d3d50620
DA
2689 left = margin_glyphs_to_reserve (w, 1, w->left_margin_cols);
2690 right = margin_glyphs_to_reserve (w, 1, w->right_margin_cols);
5cb88111
GM
2691 x = w->current_matrix->matrix_x;
2692 width = w->current_matrix->matrix_w;
2693
a13396c9
GM
2694 window_row = w->current_matrix->rows;
2695 window_row_end = window_row + w->current_matrix->nrows;
7df02016 2696 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
177c0ea7 2697
5cb88111 2698 for (; window_row < window_row_end; ++window_row, ++frame_row)
a13396c9 2699 {
5cb88111
GM
2700 window_row->glyphs[LEFT_MARGIN_AREA]
2701 = frame_row->glyphs[0] + x;
2702 window_row->glyphs[TEXT_AREA]
2703 = window_row->glyphs[LEFT_MARGIN_AREA] + left;
2704 window_row->glyphs[LAST_AREA]
2705 = window_row->glyphs[LEFT_MARGIN_AREA] + width;
2706 window_row->glyphs[RIGHT_MARGIN_AREA]
2707 = window_row->glyphs[LAST_AREA] - right;
a13396c9
GM
2708 }
2709}
2710
2711
2712/* Return the window in the window tree rooted in W containing frame
2713 row ROW. Value is null if none is found. */
2714
09ad5a91 2715static struct window *
971de7fb 2716frame_row_to_window (struct window *w, int row)
a13396c9
GM
2717{
2718 struct window *found = NULL;
177c0ea7 2719
a13396c9
GM
2720 while (w && !found)
2721 {
e74aeda8
DA
2722 if (WINDOWP (w->contents))
2723 found = frame_row_to_window (XWINDOW (w->contents), row);
7df02016
KS
2724 else if (row >= WINDOW_TOP_EDGE_LINE (w)
2725 && row < WINDOW_BOTTOM_EDGE_LINE (w))
a13396c9 2726 found = w;
177c0ea7 2727
d3d50620 2728 w = NILP (w->next) ? 0 : XWINDOW (w->next);
a13396c9
GM
2729 }
2730
2731 return found;
2732}
2733
2734
5f5c8ee5
GM
2735/* Perform a line dance in the window tree rooted at W, after
2736 scrolling a frame matrix in mirrored_line_dance.
2737
2738 We are working on the range of lines UNCHANGED_AT_TOP + 1 to
2739 UNCHANGED_AT_TOP + NLINES (not including) in W's frame matrix.
2740 COPY_FROM is a vector containing, for each row I in the range 0 <=
2741 I < NLINES, the index of the original line to move to I. This
2742 index is relative to the row range, i.e. 0 <= index < NLINES.
2743 RETAINED_P is a vector containing zero for each row 0 <= I < NLINES
2744 which is empty. */
2745
2746static void
971de7fb 2747mirror_line_dance (struct window *w, int unchanged_at_top, int nlines, int *copy_from, char *retained_p)
5f5c8ee5
GM
2748{
2749 while (w)
2750 {
e74aeda8
DA
2751 if (WINDOWP (w->contents))
2752 mirror_line_dance (XWINDOW (w->contents), unchanged_at_top,
5f5c8ee5
GM
2753 nlines, copy_from, retained_p);
2754 else
2755 {
2756 /* W is a leaf window, and we are working on its current
2757 matrix m. */
2758 struct glyph_matrix *m = w->current_matrix;
59ea14cd
PE
2759 int i;
2760 bool sync_p = 0;
5f5c8ee5
GM
2761 struct glyph_row *old_rows;
2762
2763 /* Make a copy of the original rows of matrix m. */
38182d90 2764 old_rows = alloca (m->nrows * sizeof *old_rows);
72af86bd 2765 memcpy (old_rows, m->rows, m->nrows * sizeof *old_rows);
5f5c8ee5
GM
2766
2767 for (i = 0; i < nlines; ++i)
2768 {
2769 /* Frame relative line assigned to. */
2770 int frame_to = i + unchanged_at_top;
177c0ea7 2771
5f5c8ee5
GM
2772 /* Frame relative line assigned. */
2773 int frame_from = copy_from[i] + unchanged_at_top;
177c0ea7 2774
5f5c8ee5
GM
2775 /* Window relative line assigned to. */
2776 int window_to = frame_to - m->matrix_y;
177c0ea7 2777
5f5c8ee5
GM
2778 /* Window relative line assigned. */
2779 int window_from = frame_from - m->matrix_y;
177c0ea7 2780
5f5c8ee5 2781 /* Is assigned line inside window? */
59ea14cd 2782 bool from_inside_window_p
5f5c8ee5 2783 = window_from >= 0 && window_from < m->matrix_h;
177c0ea7 2784
a13396c9 2785 /* Is assigned to line inside window? */
59ea14cd 2786 bool to_inside_window_p
a13396c9 2787 = window_to >= 0 && window_to < m->matrix_h;
177c0ea7 2788
a13396c9 2789 if (from_inside_window_p && to_inside_window_p)
5f5c8ee5 2790 {
5f5c8ee5
GM
2791 /* Do the assignment. The enabled_p flag is saved
2792 over the assignment because the old redisplay did
2793 that. */
59ea14cd 2794 bool enabled_before_p = m->rows[window_to].enabled_p;
5f5c8ee5
GM
2795 m->rows[window_to] = old_rows[window_from];
2796 m->rows[window_to].enabled_p = enabled_before_p;
177c0ea7 2797
5f5c8ee5
GM
2798 /* If frame line is empty, window line is empty, too. */
2799 if (!retained_p[copy_from[i]])
96c06863 2800 m->rows[window_to].enabled_p = false;
5f5c8ee5 2801 }
a13396c9
GM
2802 else if (to_inside_window_p)
2803 {
2804 /* A copy between windows. This is an infrequent
2805 case not worth optimizing. */
d3d50620 2806 struct frame *f = XFRAME (w->frame);
a13396c9
GM
2807 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
2808 struct window *w2;
2809 struct glyph_matrix *m2;
2810 int m2_from;
2811
fe8b8401 2812 w2 = frame_row_to_window (root, frame_from);
6183d842
RS
2813 /* ttn@surf.glug.org: when enabling menu bar using `emacs
2814 -nw', FROM_FRAME sometimes has no associated window.
2815 This check avoids a segfault if W2 is null. */
2816 if (w2)
2817 {
2818 m2 = w2->current_matrix;
2819 m2_from = frame_from - m2->matrix_y;
2820 copy_row_except_pointers (m->rows + window_to,
2821 m2->rows + m2_from);
2822
2823 /* If frame line is empty, window line is empty, too. */
2824 if (!retained_p[copy_from[i]])
96c06863 2825 m->rows[window_to].enabled_p = false;
6183d842 2826 }
a13396c9
GM
2827 sync_p = 1;
2828 }
2829 else if (from_inside_window_p)
2830 sync_p = 1;
5f5c8ee5 2831 }
a13396c9
GM
2832
2833 /* If there was a copy between windows, make sure glyph
2834 pointers are in sync with the frame matrix. */
2835 if (sync_p)
2836 sync_window_with_frame_matrix_rows (w);
177c0ea7 2837
5f5c8ee5
GM
2838 /* Check that no pointers are lost. */
2839 CHECK_MATRIX (m);
2840 }
2841
2842 /* Next window on same level. */
d3d50620 2843 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5f5c8ee5
GM
2844 }
2845}
2846
2847
e509cfa6 2848#ifdef GLYPH_DEBUG
5f5c8ee5
GM
2849
2850/* Check that window and frame matrices agree about their
2851 understanding where glyphs of the rows are to find. For each
2852 window in the window tree rooted at W, check that rows in the
2853 matrices of leaf window agree with their frame matrices about
2854 glyph pointers. */
2855
85fece3e 2856static void
59fc5cf9 2857check_window_matrix_pointers (struct window *w)
5f5c8ee5
GM
2858{
2859 while (w)
2860 {
e74aeda8
DA
2861 if (WINDOWP (w->contents))
2862 check_window_matrix_pointers (XWINDOW (w->contents));
5f5c8ee5 2863 else
60a8948a 2864 {
d3d50620 2865 struct frame *f = XFRAME (w->frame);
5f5c8ee5
GM
2866 check_matrix_pointers (w->desired_matrix, f->desired_matrix);
2867 check_matrix_pointers (w->current_matrix, f->current_matrix);
60a8948a 2868 }
177c0ea7 2869
d3d50620 2870 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5f5c8ee5
GM
2871 }
2872}
2873
2874
2875/* Check that window rows are slices of frame rows. WINDOW_MATRIX is
2876 a window and FRAME_MATRIX is the corresponding frame matrix. For
2877 each row in WINDOW_MATRIX check that it's a slice of the
2878 corresponding frame row. If it isn't, abort. */
2879
2880static void
59fc5cf9
AS
2881check_matrix_pointers (struct glyph_matrix *window_matrix,
2882 struct glyph_matrix *frame_matrix)
5f5c8ee5
GM
2883{
2884 /* Row number in WINDOW_MATRIX. */
2885 int i = 0;
2886
2887 /* Row number corresponding to I in FRAME_MATRIX. */
2888 int j = window_matrix->matrix_y;
2889
177c0ea7 2890 /* For all rows check that the row in the window matrix is a
5f5c8ee5
GM
2891 slice of the row in the frame matrix. If it isn't we didn't
2892 mirror an operation on the frame matrix correctly. */
2893 while (i < window_matrix->nrows)
2894 {
2895 if (!glyph_row_slice_p (window_matrix->rows + i,
2896 frame_matrix->rows + j))
1088b922 2897 emacs_abort ();
5f5c8ee5
GM
2898 ++i, ++j;
2899 }
2900}
2901
e509cfa6 2902#endif /* GLYPH_DEBUG */
5f5c8ee5
GM
2903
2904
2905\f
2906/**********************************************************************
2907 VPOS and HPOS translations
2908 **********************************************************************/
2909
e509cfa6 2910#ifdef GLYPH_DEBUG
5f5c8ee5
GM
2911
2912/* Translate vertical position VPOS which is relative to window W to a
2913 vertical position relative to W's frame. */
2914
2915static int
59fc5cf9 2916window_to_frame_vpos (struct window *w, int vpos)
5f5c8ee5 2917{
d3d50620 2918 eassert (!FRAME_WINDOW_P (XFRAME (w->frame)));
a54e2c05 2919 eassert (vpos >= 0 && vpos <= w->desired_matrix->nrows);
7df02016 2920 vpos += WINDOW_TOP_EDGE_LINE (w);
d3d50620 2921 eassert (vpos >= 0 && vpos <= FRAME_LINES (XFRAME (w->frame)));
5f5c8ee5
GM
2922 return vpos;
2923}
2924
2925
2926/* Translate horizontal position HPOS which is relative to window W to
153a073c 2927 a horizontal position relative to W's frame. */
5f5c8ee5
GM
2928
2929static int
59fc5cf9 2930window_to_frame_hpos (struct window *w, int hpos)
5f5c8ee5 2931{
d3d50620 2932 eassert (!FRAME_WINDOW_P (XFRAME (w->frame)));
7df02016 2933 hpos += WINDOW_LEFT_EDGE_COL (w);
5f5c8ee5
GM
2934 return hpos;
2935}
177c0ea7 2936
5f5c8ee5
GM
2937#endif /* GLYPH_DEBUG */
2938
2939
2940\f
2941/**********************************************************************
2942 Redrawing Frames
2943 **********************************************************************/
2944
2a14f83b 2945/* Redraw frame F. */
5f5c8ee5 2946
2a14f83b
DA
2947void
2948redraw_frame (struct frame *f)
2949{
2950 /* Error if F has no glyphs. */
2951 eassert (f->glyphs_initialized_p);
5f5c8ee5 2952 update_begin (f);
f2adda5e 2953 if (FRAME_MSDOS_P (f))
79d39b69 2954 FRAME_TERMINAL (f)->set_terminal_modes_hook (FRAME_TERMINAL (f));
385ed61f 2955 clear_frame (f);
5f5c8ee5
GM
2956 clear_current_matrices (f);
2957 update_end (f);
94ae2ad4 2958 windows_or_buffers_changed = 13;
5f5c8ee5
GM
2959 /* Mark all windows as inaccurate, so that every window will have
2960 its redisplay done. */
2961 mark_window_display_accurate (FRAME_ROOT_WINDOW (f), 0);
44ad1cf7
SM
2962 set_window_update_flags (XWINDOW (FRAME_ROOT_WINDOW (f)), true);
2963 f->garbaged = false;
5f5c8ee5
GM
2964}
2965
2a14f83b
DA
2966DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 0, 1, 0,
2967 doc: /* Clear frame FRAME and output again what is supposed to appear on it.
2968If FRAME is omitted or nil, the selected frame is used. */)
2969 (Lisp_Object frame)
5f5c8ee5 2970{
2a14f83b
DA
2971 redraw_frame (decode_live_frame (frame));
2972 return Qnil;
5f5c8ee5
GM
2973}
2974
5f5c8ee5 2975DEFUN ("redraw-display", Fredraw_display, Sredraw_display, 0, 0, "",
7ee72033 2976 doc: /* Clear and redisplay all visible frames. */)
5842a27b 2977 (void)
5f5c8ee5
GM
2978{
2979 Lisp_Object tail, frame;
2980
2981 FOR_EACH_FRAME (tail, frame)
2982 if (FRAME_VISIBLE_P (XFRAME (frame)))
2a14f83b 2983 redraw_frame (XFRAME (frame));
5f5c8ee5
GM
2984
2985 return Qnil;
2986}
2987
2988
5f5c8ee5 2989\f
5f5c8ee5
GM
2990/***********************************************************************
2991 Frame Update
2992 ***********************************************************************/
4588ec20 2993
5f5c8ee5 2994/* Update frame F based on the data in desired matrices.
4588ec20 2995
59ea14cd
PE
2996 If FORCE_P, don't let redisplay be stopped by detecting pending input.
2997 If INHIBIT_HAIRY_ID_P, don't try scrolling.
177c0ea7 2998
59ea14cd 2999 Value is true if redisplay was stopped due to pending input. */
4588ec20 3000
59ea14cd
PE
3001bool
3002update_frame (struct frame *f, bool force_p, bool inhibit_hairy_id_p)
5f5c8ee5 3003{
59ea14cd
PE
3004 /* True means display has been paused because of pending input. */
3005 bool paused_p;
e69b0960 3006 struct window *root_window = XWINDOW (f->root_window);
5f5c8ee5 3007
a2130dbb
KS
3008 if (redisplay_dont_pause)
3009 force_p = 1;
6fd326b7 3010 else if (!force_p && detect_input_pending_ignore_squeezables ())
d012c62b 3011 {
6fd326b7
DA
3012 paused_p = 1;
3013 goto do_pause;
d012c62b 3014 }
d012c62b 3015
5f5c8ee5 3016 if (FRAME_WINDOW_P (f))
4588ec20 3017 {
5f5c8ee5
GM
3018 /* We are working on window matrix basis. All windows whose
3019 flag must_be_updated_p is set have to be updated. */
3020
3021 /* Record that we are not working on frame matrices. */
3022 set_frame_matrix_frame (NULL);
3023
3024 /* Update all windows in the window tree of F, maybe stopping
3025 when pending input is detected. */
3026 update_begin (f);
3027
5a49b79c 3028#if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
5f5c8ee5
GM
3029 /* Update the menu bar on X frames that don't have toolkit
3030 support. */
e69b0960
DA
3031 if (WINDOWP (f->menu_bar_window))
3032 update_window (XWINDOW (f->menu_bar_window), 1);
5a49b79c 3033#endif
5f5c8ee5 3034
742516e0 3035#if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
5f5c8ee5 3036 /* Update the tool-bar window, if present. */
e69b0960 3037 if (WINDOWP (f->tool_bar_window))
4588ec20 3038 {
e69b0960 3039 struct window *w = XWINDOW (f->tool_bar_window);
5f5c8ee5
GM
3040
3041 /* Update tool-bar window. */
3042 if (w->must_be_updated_p)
4588ec20 3043 {
153a073c
PJ
3044 Lisp_Object tem;
3045
5f5c8ee5 3046 update_window (w, 1);
44ad1cf7 3047 w->must_be_updated_p = false;
5f5c8ee5
GM
3048
3049 /* Swap tool-bar strings. We swap because we want to
3050 reuse strings. */
e69b0960 3051 tem = f->current_tool_bar_string;
f00af5b1
PE
3052 fset_current_tool_bar_string (f, f->desired_tool_bar_string);
3053 fset_desired_tool_bar_string (f, tem);
4588ec20
JB
3054 }
3055 }
742516e0 3056#endif
5f5c8ee5
GM
3057
3058 /* Update windows. */
3059 paused_p = update_window_tree (root_window, force_p);
3060 update_end (f);
4588ec20 3061 }
5f5c8ee5
GM
3062 else
3063 {
3064 /* We are working on frame matrix basis. Set the frame on whose
3065 frame matrix we operate. */
3066 set_frame_matrix_frame (f);
3067
868dd24b 3068 /* Build F's desired matrix from window matrices. */
5f5c8ee5 3069 build_frame_matrix (f);
177c0ea7 3070
868dd24b
GM
3071 /* Update the display */
3072 update_begin (f);
d13adf6d 3073 paused_p = update_frame_1 (f, force_p, inhibit_hairy_id_p, 1);
868dd24b
GM
3074 update_end (f);
3075
7d2f1216 3076 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
3224dac1 3077 {
0b0d3e0b
KL
3078 if (FRAME_TTY (f)->termscript)
3079 fflush (FRAME_TTY (f)->termscript);
7d2f1216
EZ
3080 if (FRAME_TERMCAP_P (f))
3081 fflush (FRAME_TTY (f)->output);
3224dac1 3082 }
868dd24b 3083
5f5c8ee5 3084 /* Check window matrices for lost pointers. */
e509cfa6 3085#ifdef GLYPH_DEBUG
9b0e97aa
GM
3086 check_window_matrix_pointers (root_window);
3087 add_frame_display_history (f, paused_p);
3088#endif
5f5c8ee5
GM
3089 }
3090
993d4ab6 3091 do_pause:
5f5c8ee5 3092 /* Reset flags indicating that a window should be updated. */
44ad1cf7 3093 set_window_update_flags (root_window, false);
177c0ea7 3094
868dd24b 3095 display_completed = !paused_p;
5f5c8ee5 3096 return paused_p;
4588ec20 3097}
5f5c8ee5 3098
b5e9cbb6
EZ
3099/* Update a TTY frame F that has a menu dropped down over some of its
3100 glyphs. This is like the second part of update_frame, but it
3101 doesn't call build_frame_matrix, because we already have the
3102 desired matrix prepared, and don't want it to be overwritten by the
d13adf6d
EZ
3103 text of the normal display.
3104
3105 ROW and COL, if non-negative, are the row and column of the TTY
3106 frame where to position the cursor after the frame update is
3107 complete. Negative values mean ask update_frame_1 to position the
3108 cursor "normally", i.e. at point in the selected window. */
b5e9cbb6 3109void
d13adf6d 3110update_frame_with_menu (struct frame *f, int row, int col)
b5e9cbb6
EZ
3111{
3112 struct window *root_window = XWINDOW (f->root_window);
d13adf6d 3113 bool paused_p, cursor_at_point_p;
b5e9cbb6 3114
e7873136 3115 eassert (FRAME_TERMCAP_P (f));
b5e9cbb6
EZ
3116
3117 /* We are working on frame matrix basis. Set the frame on whose
3118 frame matrix we operate. */
3119 set_frame_matrix_frame (f);
3120
44ad1cf7 3121 /* Update the display. */
b5e9cbb6 3122 update_begin (f);
d13adf6d 3123 cursor_at_point_p = !(row >= 0 && col >= 0);
141f1ff7
EZ
3124 /* Force update_frame_1 not to stop due to pending input, and not
3125 try scrolling. */
d13adf6d
EZ
3126 paused_p = update_frame_1 (f, 1, 1, cursor_at_point_p);
3127 /* ROW and COL tell us where in the menu to position the cursor, so
3128 that screen readers know the active region on the screen. */
3129 if (!cursor_at_point_p)
3130 cursor_to (f, row, col);
b5e9cbb6
EZ
3131 update_end (f);
3132
3133 if (FRAME_TTY (f)->termscript)
3134 fflush (FRAME_TTY (f)->termscript);
3135 fflush (FRAME_TTY (f)->output);
3136 /* Check window matrices for lost pointers. */
3137#if GLYPH_DEBUG
ffc3882f
EZ
3138#if 0
3139 /* We cannot possibly survive the matrix pointers check, since
3140 we have overwritten parts of the frame glyph matrix without
3141 making any updates to the window matrices. */
b5e9cbb6 3142 check_window_matrix_pointers (root_window);
ffc3882f 3143#endif
b5e9cbb6
EZ
3144 add_frame_display_history (f, paused_p);
3145#endif
3146
3147 /* Reset flags indicating that a window should be updated. */
44ad1cf7 3148 set_window_update_flags (root_window, false);
834dee20 3149 display_completed = !paused_p;
b5e9cbb6 3150}
5f5c8ee5 3151
4588ec20 3152\f
5f5c8ee5
GM
3153/************************************************************************
3154 Window-based updates
3155 ************************************************************************/
3156
59ea14cd
PE
3157/* Perform updates in window tree rooted at W.
3158 If FORCE_P, don't stop updating if input is pending. */
5f5c8ee5 3159
59ea14cd
PE
3160static bool
3161update_window_tree (struct window *w, bool force_p)
5f5c8ee5 3162{
59ea14cd 3163 bool paused_p = 0;
177c0ea7 3164
5f5c8ee5
GM
3165 while (w && !paused_p)
3166 {
e74aeda8
DA
3167 if (WINDOWP (w->contents))
3168 paused_p |= update_window_tree (XWINDOW (w->contents), force_p);
5f5c8ee5
GM
3169 else if (w->must_be_updated_p)
3170 paused_p |= update_window (w, force_p);
3171
d3d50620 3172 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5f5c8ee5
GM
3173 }
3174
3175 return paused_p;
3176}
3177
3178
59ea14cd
PE
3179/* Update window W if its flag must_be_updated_p is set.
3180 If FORCE_P, don't stop updating if input is pending. */
5f5c8ee5
GM
3181
3182void
59ea14cd 3183update_single_window (struct window *w, bool force_p)
5f5c8ee5
GM
3184{
3185 if (w->must_be_updated_p)
3186 {
3187 struct frame *f = XFRAME (WINDOW_FRAME (w));
3188
3189 /* Record that this is not a frame-based redisplay. */
3190 set_frame_matrix_frame (NULL);
3191
a2130dbb
KS
3192 if (redisplay_dont_pause)
3193 force_p = 1;
d012c62b 3194
5f5c8ee5
GM
3195 /* Update W. */
3196 update_begin (f);
3197 update_window (w, force_p);
3198 update_end (f);
4588ec20 3199
5f5c8ee5 3200 /* Reset flag in W. */
44ad1cf7 3201 w->must_be_updated_p = false;
5f5c8ee5
GM
3202 }
3203}
4588ec20 3204
93e023fc 3205#ifdef HAVE_WINDOW_SYSTEM
4588ec20 3206
408f5064
GM
3207/* Redraw lines from the current matrix of window W that are
3208 overlapped by other rows. YB is bottom-most y-position in W. */
3209
3210static void
971de7fb 3211redraw_overlapped_rows (struct window *w, int yb)
408f5064 3212{
d8ee7803 3213 int i;
fa971ac3 3214 struct frame *f = XFRAME (WINDOW_FRAME (w));
3fc809e2 3215
408f5064
GM
3216 /* If rows overlapping others have been changed, the rows being
3217 overlapped have to be redrawn. This won't draw lines that have
3218 already been drawn in update_window_line because overlapped_p in
3219 desired rows is 0, so after row assignment overlapped_p in
3220 current rows is 0. */
3221 for (i = 0; i < w->current_matrix->nrows; ++i)
3222 {
e876ff42 3223 struct glyph_row *row = w->current_matrix->rows + i;
408f5064
GM
3224
3225 if (!row->enabled_p)
3226 break;
3227 else if (row->mode_line_p)
3228 continue;
177c0ea7 3229
408f5064
GM
3230 if (row->overlapped_p)
3231 {
3232 enum glyph_row_area area;
177c0ea7 3233
408f5064
GM
3234 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
3235 {
f60e429a
DA
3236 output_cursor_to (w, i, 0, row->y,
3237 area == TEXT_AREA ? row->x : 0);
408f5064 3238 if (row->used[area])
7f780da6
DA
3239 FRAME_RIF (f)->write_glyphs (w, row, row->glyphs[area],
3240 area, row->used[area]);
3241 FRAME_RIF (f)->clear_end_of_line (w, row, area, -1);
408f5064 3242 }
177c0ea7 3243
408f5064
GM
3244 row->overlapped_p = 0;
3245 }
3246
e876ff42 3247 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
408f5064
GM
3248 break;
3249 }
3250}
3251
3252
3253/* Redraw lines from the current matrix of window W that overlap
3254 others. YB is bottom-most y-position in W. */
3255
3256static void
971de7fb 3257redraw_overlapping_rows (struct window *w, int yb)
408f5064
GM
3258{
3259 int i, bottom_y;
3260 struct glyph_row *row;
fa971ac3 3261 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3fc809e2 3262
408f5064
GM
3263 for (i = 0; i < w->current_matrix->nrows; ++i)
3264 {
3265 row = w->current_matrix->rows + i;
3266
3267 if (!row->enabled_p)
3268 break;
3269 else if (row->mode_line_p)
3270 continue;
177c0ea7 3271
408f5064
GM
3272 bottom_y = MATRIX_ROW_BOTTOM_Y (row);
3273
b4b2c2ca 3274 if (row->overlapping_p)
408f5064 3275 {
41124e06
YM
3276 int overlaps = 0;
3277
b4b2c2ca 3278 if (MATRIX_ROW_OVERLAPS_PRED_P (row) && i > 0
41124e06
YM
3279 && !MATRIX_ROW (w->current_matrix, i - 1)->overlapped_p)
3280 overlaps |= OVERLAPS_PRED;
b4b2c2ca 3281 if (MATRIX_ROW_OVERLAPS_SUCC_P (row) && bottom_y < yb
41124e06
YM
3282 && !MATRIX_ROW (w->current_matrix, i + 1)->overlapped_p)
3283 overlaps |= OVERLAPS_SUCC;
3284
3285 if (overlaps)
3286 {
3287 if (row->used[LEFT_MARGIN_AREA])
3288 rif->fix_overlapping_area (w, row, LEFT_MARGIN_AREA, overlaps);
3289
3290 if (row->used[TEXT_AREA])
3291 rif->fix_overlapping_area (w, row, TEXT_AREA, overlaps);
3292
3293 if (row->used[RIGHT_MARGIN_AREA])
3294 rif->fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, overlaps);
3295
c5e87d10 3296 /* Record in neighbor rows that ROW overwrites part of
41124e06
YM
3297 their display. */
3298 if (overlaps & OVERLAPS_PRED)
3299 MATRIX_ROW (w->current_matrix, i - 1)->overlapped_p = 1;
3300 if (overlaps & OVERLAPS_SUCC)
3301 MATRIX_ROW (w->current_matrix, i + 1)->overlapped_p = 1;
3302 }
408f5064
GM
3303 }
3304
3305 if (bottom_y >= yb)
3306 break;
3307 }
3308}
3309
93e023fc
KS
3310#endif /* HAVE_WINDOW_SYSTEM */
3311
408f5064 3312
09ad5a91 3313#if defined GLYPH_DEBUG && 0
93962bfa
GM
3314
3315/* Check that no row in the current matrix of window W is enabled
3316 which is below what's displayed in the window. */
3317
09ad5a91 3318static void
971de7fb 3319check_current_matrix_flags (struct window *w)
93962bfa 3320{
59ea14cd 3321 bool last_seen_p = 0;
93962bfa
GM
3322 int i, yb = window_text_bottom_y (w);
3323
3324 for (i = 0; i < w->current_matrix->nrows - 1; ++i)
3325 {
3326 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
3327 if (!last_seen_p && MATRIX_ROW_BOTTOM_Y (row) >= yb)
3328 last_seen_p = 1;
3329 else if (last_seen_p && row->enabled_p)
1088b922 3330 emacs_abort ();
93962bfa
GM
3331 }
3332}
3333
3334#endif /* GLYPH_DEBUG */
3335
3336
59ea14cd
PE
3337/* Update display of window W.
3338 If FORCE_P, don't stop updating when input is pending. */
5f5c8ee5 3339
59ea14cd
PE
3340static bool
3341update_window (struct window *w, bool force_p)
4588ec20 3342{
5f5c8ee5 3343 struct glyph_matrix *desired_matrix = w->desired_matrix;
59ea14cd 3344 bool paused_p;
5f5c8ee5 3345 int preempt_count = baud_rate / 2400 + 1;
597cfb3f 3346 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
e509cfa6 3347#ifdef GLYPH_DEBUG
5f5c8ee5 3348 /* Check that W's frame doesn't have glyph matrices. */
a54e2c05 3349 eassert (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))));
4a34b529 3350#endif
5f5c8ee5
GM
3351
3352 /* Check pending input the first time so that we can quickly return. */
a2130dbb 3353 if (!force_p)
a2d5fca0 3354 detect_input_pending_ignore_squeezables ();
4588ec20 3355
5f5c8ee5
GM
3356 /* If forced to complete the update, or if no input is pending, do
3357 the update. */
8fd9a666 3358 if (force_p || !input_pending || !NILP (do_mouse_tracking))
4588ec20 3359 {
5f5c8ee5
GM
3360 struct glyph_row *row, *end;
3361 struct glyph_row *mode_line_row;
56974e03 3362 struct glyph_row *header_line_row;
59ea14cd
PE
3363 int yb;
3364 bool changed_p = 0, mouse_face_overwritten_p = 0;
0bc32927 3365 int n_updated = 0;
5f5c8ee5
GM
3366
3367 rif->update_window_begin_hook (w);
3368 yb = window_text_bottom_y (w);
138c0ae8
EZ
3369 row = MATRIX_ROW (desired_matrix, 0);
3370 end = MATRIX_MODE_LINE_ROW (desired_matrix);
177c0ea7 3371
394305db
EZ
3372 /* Take note of the header line, if there is one. We will
3373 update it below, after updating all of the window's lines. */
5f5c8ee5 3374 if (row->mode_line_p)
56974e03
GM
3375 {
3376 header_line_row = row;
3377 ++row;
3378 }
3379 else
3380 header_line_row = NULL;
5f5c8ee5
GM
3381
3382 /* Update the mode line, if necessary. */
3383 mode_line_row = MATRIX_MODE_LINE_ROW (desired_matrix);
3384 if (mode_line_row->mode_line_p && mode_line_row->enabled_p)
3385 {
3386 mode_line_row->y = yb;
3387 update_window_line (w, MATRIX_ROW_VPOS (mode_line_row,
e876ff42
GM
3388 desired_matrix),
3389 &mouse_face_overwritten_p);
5f5c8ee5
GM
3390 }
3391
3392 /* Find first enabled row. Optimizations in redisplay_internal
3393 may lead to an update with only one row enabled. There may
3394 be also completely empty matrices. */
3395 while (row < end && !row->enabled_p)
3396 ++row;
177c0ea7 3397
bfdcafe1 3398 /* Try reusing part of the display by copying. */
5f5c8ee5 3399 if (row < end && !desired_matrix->no_scrolling_p)
56974e03
GM
3400 {
3401 int rc = scrolling_window (w, header_line_row != NULL);
3402 if (rc < 0)
3403 {
3404 /* All rows were found to be equal. */
3405 paused_p = 0;
3406 goto set_cursor;
3407 }
3408 else if (rc > 0)
6b61353c
KH
3409 {
3410 /* We've scrolled the display. */
3411 force_p = 1;
3412 changed_p = 1;
3413 }
5f5c8ee5
GM
3414 }
3415
3416 /* Update the rest of the lines. */
0bc32927 3417 for (; row < end && (force_p || !input_pending); ++row)
394305db
EZ
3418 /* scrolling_window resets the enabled_p flag of the rows it
3419 reuses from current_matrix. */
b7412313 3420 if (row->enabled_p)
5f5c8ee5
GM
3421 {
3422 int vpos = MATRIX_ROW_VPOS (row, desired_matrix);
3423 int i;
177c0ea7 3424
153a073c 3425 /* We'll have to play a little bit with when to
5f5c8ee5
GM
3426 detect_input_pending. If it's done too often,
3427 scrolling large windows with repeated scroll-up
3428 commands will too quickly pause redisplay. */
e87b8809 3429 if (!force_p && ++n_updated % preempt_count == 0)
a2d5fca0 3430 detect_input_pending_ignore_squeezables ();
e876ff42
GM
3431 changed_p |= update_window_line (w, vpos,
3432 &mouse_face_overwritten_p);
5f5c8ee5
GM
3433
3434 /* Mark all rows below the last visible one in the current
3435 matrix as invalid. This is necessary because of
3436 variable line heights. Consider the case of three
3437 successive redisplays, where the first displays 5
3438 lines, the second 3 lines, and the third 5 lines again.
3439 If the second redisplay wouldn't mark rows in the
3440 current matrix invalid, the third redisplay might be
3441 tempted to optimize redisplay based on lines displayed
3442 in the first redisplay. */
3443 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
3444 for (i = vpos + 1; i < w->current_matrix->nrows - 1; ++i)
96c06863 3445 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, i, false);
5f5c8ee5
GM
3446 }
3447
3448 /* Was display preempted? */
3449 paused_p = row < end;
177c0ea7 3450
56974e03 3451 set_cursor:
177c0ea7 3452
6b61353c
KH
3453 /* Update the header line after scrolling because a new header
3454 line would otherwise overwrite lines at the top of the window
3455 that can be scrolled. */
3456 if (header_line_row && header_line_row->enabled_p)
3457 {
3458 header_line_row->y = 0;
3459 update_window_line (w, 0, &mouse_face_overwritten_p);
6b61353c
KH
3460 }
3461
153a073c 3462 /* Fix the appearance of overlapping/overlapped rows. */
5f5c8ee5
GM
3463 if (!paused_p && !w->pseudo_window_p)
3464 {
93e023fc 3465#ifdef HAVE_WINDOW_SYSTEM
e876ff42
GM
3466 if (changed_p && rif->fix_overlapping_area)
3467 {
3468 redraw_overlapped_rows (w, yb);
3469 redraw_overlapping_rows (w, yb);
3470 }
93e023fc 3471#endif
177c0ea7 3472
5f5c8ee5
GM
3473 /* Make cursor visible at cursor position of W. */
3474 set_window_cursor_after_update (w);
3475
e876ff42
GM
3476#if 0 /* Check that current matrix invariants are satisfied. This is
3477 for debugging only. See the comment of check_matrix_invariants. */
5f5c8ee5
GM
3478 IF_DEBUG (check_matrix_invariants (w));
3479#endif
4588ec20 3480 }
5f5c8ee5 3481
e509cfa6 3482#ifdef GLYPH_DEBUG
5f5c8ee5
GM
3483 /* Remember the redisplay method used to display the matrix. */
3484 strcpy (w->current_matrix->method, w->desired_matrix->method);
3485#endif
408f5064 3486
6b61353c
KH
3487#ifdef HAVE_WINDOW_SYSTEM
3488 update_window_fringes (w, 0);
3489#endif
3490
56974e03
GM
3491 /* End the update of window W. Don't set the cursor if we
3492 paused updating the display because in this case,
3493 set_window_cursor_after_update hasn't been called, and
f60e429a 3494 W->output_cursor doesn't contain the cursor location. */
56974e03 3495 rif->update_window_end_hook (w, !paused_p, mouse_face_overwritten_p);
4588ec20 3496 }
5f5c8ee5
GM
3497 else
3498 paused_p = 1;
3499
e509cfa6 3500#ifdef GLYPH_DEBUG
0cffd9d8 3501 /* check_current_matrix_flags (w); */
9b0e97aa
GM
3502 add_window_display_history (w, w->current_matrix->method, paused_p);
3503#endif
177c0ea7 3504
5f5c8ee5 3505 clear_glyph_matrix (desired_matrix);
408f5064 3506
5f5c8ee5 3507 return paused_p;
4588ec20
JB
3508}
3509
b64b3980 3510
5f5c8ee5
GM
3511/* Update the display of area AREA in window W, row number VPOS.
3512 AREA can be either LEFT_MARGIN_AREA or RIGHT_MARGIN_AREA. */
3513
3514static void
7f780da6
DA
3515update_marginal_area (struct window *w, struct glyph_row *updated_row,
3516 enum glyph_row_area area, int vpos)
b64b3980 3517{
5f5c8ee5 3518 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
fa971ac3 3519 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
5f5c8ee5 3520
5f5c8ee5
GM
3521 /* Set cursor to start of glyphs, write them, and clear to the end
3522 of the area. I don't think that something more sophisticated is
3523 necessary here, since marginal areas will not be the default. */
f60e429a 3524 output_cursor_to (w, vpos, 0, desired_row->y, 0);
5f5c8ee5 3525 if (desired_row->used[area])
7f780da6
DA
3526 rif->write_glyphs (w, updated_row, desired_row->glyphs[area],
3527 area, desired_row->used[area]);
3528 rif->clear_end_of_line (w, updated_row, area, -1);
b64b3980 3529}
23b0200c 3530
352f1545 3531
408f5064 3532/* Update the display of the text area of row VPOS in window W.
59ea14cd 3533 Value is true if display has changed. */
5f5c8ee5 3534
59ea14cd 3535static bool
7f780da6 3536update_text_area (struct window *w, struct glyph_row *updated_row, int vpos)
23b0200c 3537{
5f5c8ee5
GM
3538 struct glyph_row *current_row = MATRIX_ROW (w->current_matrix, vpos);
3539 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
fa971ac3 3540 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
59ea14cd 3541 bool changed_p = 0;
5f5c8ee5 3542
5f5c8ee5
GM
3543 /* If rows are at different X or Y, or rows have different height,
3544 or the current row is marked invalid, write the entire line. */
3545 if (!current_row->enabled_p
3546 || desired_row->y != current_row->y
3547 || desired_row->ascent != current_row->ascent
408f5064
GM
3548 || desired_row->phys_ascent != current_row->phys_ascent
3549 || desired_row->phys_height != current_row->phys_height
5f5c8ee5 3550 || desired_row->visible_height != current_row->visible_height
408f5064 3551 || current_row->overlapped_p
fee8b690
CY
3552 /* This next line is necessary for correctly redrawing
3553 mouse-face areas after scrolling and other operations.
3554 However, it causes excessive flickering when mouse is moved
3555 across the mode line. Luckily, turning it off for the mode
a75b65c4
KS
3556 line doesn't seem to hurt anything. -- cyd.
3557 But it is still needed for the header line. -- kfs. */
3558 || (current_row->mouse_face_p
3559 && !(current_row->mode_line_p && vpos > 0))
5f5c8ee5
GM
3560 || current_row->x != desired_row->x)
3561 {
f60e429a 3562 output_cursor_to (w, vpos, 0, desired_row->y, desired_row->x);
177c0ea7 3563
5f5c8ee5 3564 if (desired_row->used[TEXT_AREA])
7f780da6
DA
3565 rif->write_glyphs (w, updated_row, desired_row->glyphs[TEXT_AREA],
3566 TEXT_AREA, desired_row->used[TEXT_AREA]);
177c0ea7 3567
5f5c8ee5 3568 /* Clear to end of window. */
7f780da6 3569 rif->clear_end_of_line (w, updated_row, TEXT_AREA, -1);
408f5064 3570 changed_p = 1;
60f2f3f1
GM
3571
3572 /* This erases the cursor. We do this here because
3573 notice_overwritten_cursor cannot easily check this, which
3574 might indicate that the whole functionality of
3575 notice_overwritten_cursor would better be implemented here.
3576 On the other hand, we need notice_overwritten_cursor as long
3577 as mouse highlighting is done asynchronously outside of
3578 redisplay. */
3579 if (vpos == w->phys_cursor.vpos)
3580 w->phys_cursor_on_p = 0;
5f5c8ee5
GM
3581 }
3582 else
3583 {
3584 int stop, i, x;
3585 struct glyph *current_glyph = current_row->glyphs[TEXT_AREA];
3586 struct glyph *desired_glyph = desired_row->glyphs[TEXT_AREA];
59ea14cd 3587 bool overlapping_glyphs_p = current_row->contains_overlapping_glyphs_p;
074b781a 3588 int desired_stop_pos = desired_row->used[TEXT_AREA];
59ea14cd 3589 bool abort_skipping = 0;
5f5c8ee5 3590
78897758
YM
3591 /* If the desired row extends its face to the text area end, and
3592 unless the current row also does so at the same position,
5f5c8ee5
GM
3593 make sure we write at least one glyph, so that the face
3594 extension actually takes place. */
78897758
YM
3595 if (MATRIX_ROW_EXTENDS_FACE_P (desired_row)
3596 && (desired_stop_pos < current_row->used[TEXT_AREA]
3597 || (desired_stop_pos == current_row->used[TEXT_AREA]
3598 && !MATRIX_ROW_EXTENDS_FACE_P (current_row))))
074b781a 3599 --desired_stop_pos;
177c0ea7 3600
5f5c8ee5
GM
3601 stop = min (current_row->used[TEXT_AREA], desired_stop_pos);
3602 i = 0;
3603 x = desired_row->x;
8399f8a0
GM
3604
3605 /* Loop over glyphs that current and desired row may have
3606 in common. */
5f5c8ee5 3607 while (i < stop)
352f1545 3608 {
59ea14cd 3609 bool can_skip_p = !abort_skipping;
177c0ea7 3610
5f5c8ee5 3611 /* Skip over glyphs that both rows have in common. These
e47306e6
GM
3612 don't have to be written. We can't skip if the last
3613 current glyph overlaps the glyph to its right. For
3614 example, consider a current row of `if ' with the `f' in
3615 Courier bold so that it overlaps the ` ' to its right.
3616 If the desired row is ` ', we would skip over the space
3617 after the `if' and there would remain a pixel from the
3618 `f' on the screen. */
3619 if (overlapping_glyphs_p && i > 0)
352f1545 3620 {
e47306e6
GM
3621 struct glyph *glyph = &current_row->glyphs[TEXT_AREA][i - 1];
3622 int left, right;
177c0ea7 3623
d3d50620 3624 rif->get_glyph_overhangs (glyph, XFRAME (w->frame),
e47306e6 3625 &left, &right);
91d227d9 3626 can_skip_p = (right == 0 && !abort_skipping);
352f1545 3627 }
177c0ea7 3628
6d950f4c 3629 if (can_skip_p)
352f1545 3630 {
91d227d9
CY
3631 int start_hpos = i;
3632
6d950f4c
GM
3633 while (i < stop
3634 && GLYPH_EQUAL_P (desired_glyph, current_glyph))
3635 {
3636 x += desired_glyph->pixel_width;
3637 ++desired_glyph, ++current_glyph, ++i;
3638 }
3639
3640 /* Consider the case that the current row contains "xxx
3641 ppp ggg" in italic Courier font, and the desired row
3642 is "xxx ggg". The character `p' has lbearing, `g'
3643 has not. The loop above will stop in front of the
3644 first `p' in the current row. If we would start
3645 writing glyphs there, we wouldn't erase the lbearing
3646 of the `p'. The rest of the lbearing problem is then
641bdbf3 3647 taken care of by draw_glyphs. */
6d950f4c
GM
3648 if (overlapping_glyphs_p
3649 && i > 0
3650 && i < current_row->used[TEXT_AREA]
3651 && (current_row->used[TEXT_AREA]
3652 != desired_row->used[TEXT_AREA]))
5f5c8ee5 3653 {
6d950f4c 3654 int left, right;
177c0ea7 3655
3a45383a 3656 rif->get_glyph_overhangs (current_glyph,
d3d50620 3657 XFRAME (w->frame),
6d950f4c
GM
3658 &left, &right);
3659 while (left > 0 && i > 0)
3660 {
3661 --i, --desired_glyph, --current_glyph;
3662 x -= desired_glyph->pixel_width;
3663 left -= desired_glyph->pixel_width;
3664 }
91d227d9
CY
3665
3666 /* Abort the skipping algorithm if we end up before
3667 our starting point, to avoid looping (bug#1070).
3668 This can happen when the lbearing is larger than
3669 the pixel width. */
3670 abort_skipping = (i < start_hpos);
5f5c8ee5 3671 }
352f1545 3672 }
177c0ea7 3673
5f5c8ee5
GM
3674 /* Try to avoid writing the entire rest of the desired row
3675 by looking for a resync point. This mainly prevents
3676 mode line flickering in the case the mode line is in
3677 fixed-pitch font, which it usually will be. */
3678 if (i < desired_row->used[TEXT_AREA])
3679 {
3680 int start_x = x, start_hpos = i;
3681 struct glyph *start = desired_glyph;
3682 int current_x = x;
59ea14cd 3683 bool skip_first_p = !can_skip_p;
6d950f4c 3684
5f5c8ee5
GM
3685 /* Find the next glyph that's equal again. */
3686 while (i < stop
6d950f4c
GM
3687 && (skip_first_p
3688 || !GLYPH_EQUAL_P (desired_glyph, current_glyph))
5f5c8ee5
GM
3689 && x == current_x)
3690 {
3691 x += desired_glyph->pixel_width;
3692 current_x += current_glyph->pixel_width;
3693 ++desired_glyph, ++current_glyph, ++i;
6d950f4c 3694 skip_first_p = 0;
5f5c8ee5 3695 }
23b0200c 3696
5f5c8ee5
GM
3697 if (i == start_hpos || x != current_x)
3698 {
3699 i = start_hpos;
3700 x = start_x;
3701 desired_glyph = start;
3702 break;
3703 }
e47306e6 3704
f60e429a 3705 output_cursor_to (w, vpos, start_hpos, desired_row->y, start_x);
7f780da6
DA
3706 rif->write_glyphs (w, updated_row, start,
3707 TEXT_AREA, i - start_hpos);
408f5064 3708 changed_p = 1;
5f5c8ee5
GM
3709 }
3710 }
177c0ea7 3711
5f5c8ee5
GM
3712 /* Write the rest. */
3713 if (i < desired_row->used[TEXT_AREA])
3714 {
f60e429a 3715 output_cursor_to (w, vpos, i, desired_row->y, x);
7f780da6
DA
3716 rif->write_glyphs (w, updated_row, desired_glyph,
3717 TEXT_AREA, desired_row->used[TEXT_AREA] - i);
408f5064 3718 changed_p = 1;
5f5c8ee5 3719 }
177c0ea7 3720
5f5c8ee5
GM
3721 /* Maybe clear to end of line. */
3722 if (MATRIX_ROW_EXTENDS_FACE_P (desired_row))
3723 {
3724 /* If new row extends to the end of the text area, nothing
3725 has to be cleared, if and only if we did a write_glyphs
3726 above. This is made sure by setting desired_stop_pos
3727 appropriately above. */
a54e2c05 3728 eassert (i < desired_row->used[TEXT_AREA]
78897758
YM
3729 || ((desired_row->used[TEXT_AREA]
3730 == current_row->used[TEXT_AREA])
3731 && MATRIX_ROW_EXTENDS_FACE_P (current_row)));
5f5c8ee5
GM
3732 }
3733 else if (MATRIX_ROW_EXTENDS_FACE_P (current_row))
3734 {
3735 /* If old row extends to the end of the text area, clear. */
3736 if (i >= desired_row->used[TEXT_AREA])
f60e429a
DA
3737 output_cursor_to (w, vpos, i, desired_row->y,
3738 desired_row->pixel_width);
7f780da6 3739 rif->clear_end_of_line (w, updated_row, TEXT_AREA, -1);
408f5064 3740 changed_p = 1;
5f5c8ee5
GM
3741 }
3742 else if (desired_row->pixel_width < current_row->pixel_width)
2e8907d3 3743 {
5f5c8ee5
GM
3744 /* Otherwise clear to the end of the old row. Everything
3745 after that position should be clear already. */
27786b2f 3746 int xlim;
177c0ea7 3747
5f5c8ee5 3748 if (i >= desired_row->used[TEXT_AREA])
f60e429a
DA
3749 output_cursor_to (w, vpos, i, desired_row->y,
3750 desired_row->pixel_width);
5f5c8ee5
GM
3751
3752 /* If cursor is displayed at the end of the line, make sure
3753 it's cleared. Nowadays we don't have a phys_cursor_glyph
3754 with which to erase the cursor (because this method
3755 doesn't work with lbearing/rbearing), so we must do it
3756 this way. */
3757 if (vpos == w->phys_cursor.vpos
f951a506
EZ
3758 && (desired_row->reversed_p
3759 ? (w->phys_cursor.hpos < 0)
3760 : (w->phys_cursor.hpos >= desired_row->used[TEXT_AREA])))
5f5c8ee5
GM
3761 {
3762 w->phys_cursor_on_p = 0;
27786b2f 3763 xlim = -1;
5f5c8ee5
GM
3764 }
3765 else
27786b2f 3766 xlim = current_row->pixel_width;
7f780da6 3767 rif->clear_end_of_line (w, updated_row, TEXT_AREA, xlim);
408f5064 3768 changed_p = 1;
2e8907d3 3769 }
23b0200c 3770 }
408f5064
GM
3771
3772 return changed_p;
23b0200c 3773}
4588ec20 3774
5f5c8ee5 3775
59ea14cd 3776/* Update row VPOS in window W. Value is true if display has been changed. */
5f5c8ee5 3777
59ea14cd
PE
3778static bool
3779update_window_line (struct window *w, int vpos, bool *mouse_face_overwritten_p)
5f5c8ee5
GM
3780{
3781 struct glyph_row *current_row = MATRIX_ROW (w->current_matrix, vpos);
3782 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
fa971ac3 3783 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
59ea14cd 3784 bool changed_p = 0;
5f5c8ee5 3785
177c0ea7
JB
3786 /* A row can be completely invisible in case a desired matrix was
3787 built with a vscroll and then make_cursor_line_fully_visible shifts
504805ac
GM
3788 the matrix. Make sure to make such rows current anyway, since
3789 we need the correct y-position, for example, in the current matrix. */
001f93f3
GM
3790 if (desired_row->mode_line_p
3791 || desired_row->visible_height > 0)
408f5064 3792 {
a54e2c05 3793 eassert (desired_row->enabled_p);
504805ac
GM
3794
3795 /* Update display of the left margin area, if there is one. */
eeaf9bf3 3796 if (!desired_row->full_width_p && w->left_margin_cols > 0)
504805ac
GM
3797 {
3798 changed_p = 1;
7f780da6 3799 update_marginal_area (w, desired_row, LEFT_MARGIN_AREA, vpos);
e9957956
EZ
3800 /* Setting this flag will ensure the vertical border, if
3801 any, between this window and the one on its left will be
3802 redrawn. This is necessary because updating the left
3803 margin area can potentially draw over the border. */
3804 current_row->redraw_fringe_bitmaps_p = 1;
504805ac 3805 }
177c0ea7 3806
504805ac 3807 /* Update the display of the text area. */
7f780da6 3808 if (update_text_area (w, desired_row, vpos))
504805ac
GM
3809 {
3810 changed_p = 1;
3811 if (current_row->mouse_face_p)
3812 *mouse_face_overwritten_p = 1;
3813 }
177c0ea7 3814
504805ac 3815 /* Update display of the right margin area, if there is one. */
eeaf9bf3 3816 if (!desired_row->full_width_p && w->right_margin_cols > 0)
504805ac
GM
3817 {
3818 changed_p = 1;
7f780da6 3819 update_marginal_area (w, desired_row, RIGHT_MARGIN_AREA, vpos);
504805ac 3820 }
177c0ea7 3821
504805ac
GM
3822 /* Draw truncation marks etc. */
3823 if (!current_row->enabled_p
3824 || desired_row->y != current_row->y
3825 || desired_row->visible_height != current_row->visible_height
6b61353c 3826 || desired_row->cursor_in_fringe_p != current_row->cursor_in_fringe_p
3d4a365f 3827 || desired_row->overlay_arrow_bitmap != current_row->overlay_arrow_bitmap
6b61353c 3828 || current_row->redraw_fringe_bitmaps_p
504805ac 3829 || desired_row->mode_line_p != current_row->mode_line_p
6b61353c 3830 || desired_row->exact_window_width_line_p != current_row->exact_window_width_line_p
504805ac
GM
3831 || (MATRIX_ROW_CONTINUATION_LINE_P (desired_row)
3832 != MATRIX_ROW_CONTINUATION_LINE_P (current_row)))
65620264 3833 rif->after_update_window_line_hook (w, desired_row);
408f5064 3834 }
177c0ea7 3835
5f5c8ee5
GM
3836 /* Update current_row from desired_row. */
3837 make_current (w->desired_matrix, w->current_matrix, vpos);
408f5064 3838 return changed_p;
5f5c8ee5
GM
3839}
3840
3841
3842/* Set the cursor after an update of window W. This function may only
3843 be called from update_window. */
3844
3845static void
971de7fb 3846set_window_cursor_after_update (struct window *w)
4588ec20 3847{
d3d50620 3848 struct frame *f = XFRAME (w->frame);
5f5c8ee5
GM
3849 int cx, cy, vpos, hpos;
3850
3851 /* Not intended for frame matrix updates. */
a54e2c05 3852 eassert (FRAME_WINDOW_P (f));
177c0ea7 3853
b96fd3e8
GM
3854 if (cursor_in_echo_area
3855 && !NILP (echo_area_buffer[0])
3856 /* If we are showing a message instead of the mini-buffer,
3857 show the cursor for the message instead. */
3858 && XWINDOW (minibuf_window) == w
3859 && EQ (minibuf_window, echo_area_window)
5f5c8ee5
GM
3860 /* These cases apply only to the frame that contains
3861 the active mini-buffer window. */
3862 && FRAME_HAS_MINIBUF_P (f)
3863 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
3864 {
3865 cx = cy = vpos = hpos = 0;
3866
3867 if (cursor_in_echo_area >= 0)
3868 {
3869 /* If the mini-buffer is several lines high, find the last
3870 line that has any text on it. Note: either all lines
3871 are enabled or none. Otherwise we wouldn't be able to
3872 determine Y. */
862b4790
GM
3873 struct glyph_row *row, *last_row;
3874 struct glyph *glyph;
3875 int yb = window_text_bottom_y (w);
5f5c8ee5 3876
862b4790 3877 last_row = NULL;
6206f8ce
GM
3878 row = w->current_matrix->rows;
3879 while (row->enabled_p
3880 && (last_row == NULL
3881 || MATRIX_ROW_BOTTOM_Y (row) <= yb))
5f5c8ee5 3882 {
862b4790
GM
3883 if (row->used[TEXT_AREA]
3884 && row->glyphs[TEXT_AREA][0].charpos >= 0)
3885 last_row = row;
6206f8ce 3886 ++row;
862b4790 3887 }
177c0ea7 3888
5f5c8ee5
GM
3889 if (last_row)
3890 {
6206f8ce
GM
3891 struct glyph *start = last_row->glyphs[TEXT_AREA];
3892 struct glyph *last = start + last_row->used[TEXT_AREA] - 1;
862b4790 3893
b96fd3e8 3894 while (last > start && last->charpos < 0)
862b4790 3895 --last;
177c0ea7 3896
862b4790
GM
3897 for (glyph = start; glyph < last; ++glyph)
3898 {
3899 cx += glyph->pixel_width;
3900 ++hpos;
3901 }
3902
5f5c8ee5 3903 cy = last_row->y;
862b4790 3904 vpos = MATRIX_ROW_VPOS (last_row, w->current_matrix);
5f5c8ee5
GM
3905 }
3906 }
3907 }
3908 else
3909 {
3910 cx = w->cursor.x;
3911 cy = w->cursor.y;
3912 hpos = w->cursor.hpos;
3913 vpos = w->cursor.vpos;
3914 }
4588ec20 3915
2dd61a9b 3916 /* Window cursor can be out of sync for horizontally split windows.
cccaebd2 3917 Horizontal position is -1 when cursor is on the left fringe. */
2dd61a9b
DA
3918 hpos = clip_to_bounds (-1, hpos, w->current_matrix->matrix_w - 1);
3919 vpos = clip_to_bounds (0, vpos, w->current_matrix->nrows - 1);
f60e429a 3920 output_cursor_to (w, vpos, hpos, cy, cx);
4588ec20 3921}
4588ec20 3922
5f5c8ee5 3923
44ad1cf7
SM
3924/* Set WINDOW->must_be_updated_p to ON_P for all windows in
3925 the window tree rooted at W. */
6df5d75f 3926
44ad1cf7
SM
3927static void
3928set_window_update_flags (struct window *w, bool on_p)
6df5d75f
GM
3929{
3930 while (w)
3931 {
e74aeda8 3932 if (WINDOWP (w->contents))
44ad1cf7
SM
3933 set_window_update_flags (XWINDOW (w->contents), on_p);
3934 else
6df5d75f
GM
3935 w->must_be_updated_p = on_p;
3936
d3d50620 3937 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6df5d75f 3938 }
177c0ea7 3939}
6df5d75f
GM
3940
3941
3942\f
3943/***********************************************************************
3944 Window-Based Scrolling
3945 ***********************************************************************/
3946
3947/* Structure describing rows in scrolling_window. */
3948
3949struct row_entry
3950{
3951 /* Number of occurrences of this row in desired and current matrix. */
3952 int old_uses, new_uses;
177c0ea7 3953
6df5d75f
GM
3954 /* Vpos of row in new matrix. */
3955 int new_line_number;
3956
3957 /* Bucket index of this row_entry in the hash table row_table. */
ca9ce8f2 3958 ptrdiff_t bucket;
177c0ea7 3959
6df5d75f
GM
3960 /* The row described by this entry. */
3961 struct glyph_row *row;
177c0ea7 3962
6df5d75f
GM
3963 /* Hash collision chain. */
3964 struct row_entry *next;
3965};
3966
3967/* A pool to allocate row_entry structures from, and the size of the
3968 pool. The pool is reallocated in scrolling_window when we find
3969 that we need a larger one. */
3970
3971static struct row_entry *row_entry_pool;
ca9ce8f2 3972static ptrdiff_t row_entry_pool_size;
6df5d75f
GM
3973
3974/* Index of next free entry in row_entry_pool. */
3975
ca9ce8f2 3976static ptrdiff_t row_entry_idx;
6df5d75f
GM
3977
3978/* The hash table used during scrolling, and the table's size. This
3979 table is used to quickly identify equal rows in the desired and
3980 current matrix. */
3981
3982static struct row_entry **row_table;
ca9ce8f2 3983static ptrdiff_t row_table_size;
6df5d75f
GM
3984
3985/* Vectors of pointers to row_entry structures belonging to the
3986 current and desired matrix, and the size of the vectors. */
3987
3988static struct row_entry **old_lines, **new_lines;
0065d054 3989static ptrdiff_t old_lines_size, new_lines_size;
6df5d75f
GM
3990
3991/* A pool to allocate run structures from, and its size. */
3992
3993static struct run *run_pool;
0065d054 3994static ptrdiff_t runs_size;
6df5d75f
GM
3995
3996/* A vector of runs of lines found during scrolling. */
3997
3998static struct run **runs;
3999
6b1f9ba4 4000/* Add glyph row ROW to the scrolling hash table. */
6df5d75f 4001
b0ab8123 4002static struct row_entry *
6b1f9ba4 4003add_row_entry (struct glyph_row *row)
6df5d75f
GM
4004{
4005 struct row_entry *entry;
ca9ce8f2 4006 ptrdiff_t i = row->hash % row_table_size;
177c0ea7 4007
6df5d75f 4008 entry = row_table[i];
a54e2c05 4009 eassert (entry || verify_row_hash (row));
6b1f9ba4 4010 while (entry && !row_equal_p (entry->row, row, 1))
6df5d75f 4011 entry = entry->next;
177c0ea7 4012
6df5d75f
GM
4013 if (entry == NULL)
4014 {
4015 entry = row_entry_pool + row_entry_idx++;
4016 entry->row = row;
4017 entry->old_uses = entry->new_uses = 0;
4018 entry->new_line_number = 0;
4019 entry->bucket = i;
4020 entry->next = row_table[i];
4021 row_table[i] = entry;
4022 }
4023
4024 return entry;
4025}
4026
4027
5f5c8ee5 4028/* Try to reuse part of the current display of W by scrolling lines.
59ea14cd 4029 HEADER_LINE_P means W has a header line.
5f5c8ee5
GM
4030
4031 The algorithm is taken from Communications of the ACM, Apr78 "A
4032 Technique for Isolating Differences Between Files." It should take
4033 O(N) time.
4034
4035 A short outline of the steps of the algorithm
4036
4037 1. Skip lines equal at the start and end of both matrices.
4038
4039 2. Enter rows in the current and desired matrix into a symbol
4040 table, counting how often they appear in both matrices.
4041
4042 3. Rows that appear exactly once in both matrices serve as anchors,
4043 i.e. we assume that such lines are likely to have been moved.
4044
4045 4. Starting from anchor lines, extend regions to be scrolled both
4046 forward and backward.
4047
4048 Value is
4049
4050 -1 if all rows were found to be equal.
4051 0 to indicate that we did not scroll the display, or
4052 1 if we did scroll. */
4053
4054static int
59ea14cd 4055scrolling_window (struct window *w, bool header_line_p)
4588ec20 4056{
5f5c8ee5
GM
4057 struct glyph_matrix *desired_matrix = w->desired_matrix;
4058 struct glyph_matrix *current_matrix = w->current_matrix;
4059 int yb = window_text_bottom_y (w);
ca9ce8f2
PE
4060 ptrdiff_t i;
4061 int j, first_old, first_new, last_old, last_new;
4062 int nruns, run_idx;
0065d054 4063 ptrdiff_t n;
6df5d75f 4064 struct row_entry *entry;
fa971ac3 4065 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
5f5c8ee5
GM
4066
4067 /* Skip over rows equal at the start. */
59ea14cd 4068 for (i = header_line_p; i < current_matrix->nrows - 1; ++i)
5f5c8ee5 4069 {
bfdcafe1
GM
4070 struct glyph_row *d = MATRIX_ROW (desired_matrix, i);
4071 struct glyph_row *c = MATRIX_ROW (current_matrix, i);
4072
4073 if (c->enabled_p
4074 && d->enabled_p
6b61353c 4075 && !d->redraw_fringe_bitmaps_p
bfdcafe1
GM
4076 && c->y == d->y
4077 && MATRIX_ROW_BOTTOM_Y (c) <= yb
4078 && MATRIX_ROW_BOTTOM_Y (d) <= yb
6b1f9ba4 4079 && row_equal_p (c, d, 1))
bfdcafe1
GM
4080 {
4081 assign_row (c, d);
96c06863 4082 d->enabled_p = false;
bfdcafe1
GM
4083 }
4084 else
4085 break;
5f5c8ee5 4086 }
4588ec20 4087
5f5c8ee5 4088 /* Give up if some rows in the desired matrix are not enabled. */
96c06863 4089 if (! MATRIX_ROW_ENABLED_P (desired_matrix, i))
5f5c8ee5 4090 return -1;
177c0ea7 4091
5f5c8ee5
GM
4092 first_old = first_new = i;
4093
4d8ade89
YM
4094 /* Set last_new to the index + 1 of the row that reaches the
4095 bottom boundary in the desired matrix. Give up if we find a
4096 disabled row before we reach the bottom boundary. */
5f5c8ee5 4097 i = first_new + 1;
4d8ade89
YM
4098 while (i < desired_matrix->nrows - 1)
4099 {
4100 int bottom;
5f5c8ee5 4101
96c06863 4102 if (! MATRIX_ROW_ENABLED_P (desired_matrix, i))
4d8ade89
YM
4103 return 0;
4104 bottom = MATRIX_ROW_BOTTOM_Y (MATRIX_ROW (desired_matrix, i));
4105 if (bottom <= yb)
4106 ++i;
4107 if (bottom >= yb)
4108 break;
4109 }
5f5c8ee5
GM
4110
4111 last_new = i;
4112
4d8ade89
YM
4113 /* Set last_old to the index + 1 of the row that reaches the bottom
4114 boundary in the current matrix. We don't look at the enabled
4115 flag here because we plan to reuse part of the display even if
4116 other parts are disabled. */
5f5c8ee5 4117 i = first_old + 1;
93962bfa
GM
4118 while (i < current_matrix->nrows - 1)
4119 {
4120 int bottom = MATRIX_ROW_BOTTOM_Y (MATRIX_ROW (current_matrix, i));
4121 if (bottom <= yb)
4122 ++i;
4123 if (bottom >= yb)
4124 break;
4125 }
4126
5f5c8ee5
GM
4127 last_old = i;
4128
4129 /* Skip over rows equal at the bottom. */
4130 i = last_new;
4131 j = last_old;
4132 while (i - 1 > first_new
4133 && j - 1 > first_old
96c06863 4134 && MATRIX_ROW_ENABLED_P (current_matrix, j - 1)
1e5b2111
EZ
4135 && (MATRIX_ROW (current_matrix, j - 1)->y
4136 == MATRIX_ROW (desired_matrix, i - 1)->y)
4137 && !MATRIX_ROW (desired_matrix, i - 1)->redraw_fringe_bitmaps_p
6b1f9ba4 4138 && row_equal_p (MATRIX_ROW (desired_matrix, i - 1),
e4e0bee9 4139 MATRIX_ROW (current_matrix, j - 1), 1))
5f5c8ee5
GM
4140 --i, --j;
4141 last_new = i;
4142 last_old = j;
4143
4144 /* Nothing to do if all rows are equal. */
4145 if (last_new == first_new)
4146 return 0;
4147
0065d054 4148 /* Check for integer overflow in size calculation.
ca9ce8f2
PE
4149
4150 If next_almost_prime checks (N) for divisibility by 2..10, then
4151 it can return at most N + 10, e.g., next_almost_prime (1) == 11.
4152 So, set next_almost_prime_increment_max to 10.
4153
4154 It's just a coincidence that next_almost_prime_increment_max ==
4155 NEXT_ALMOST_PRIME_LIMIT - 1. If NEXT_ALMOST_PRIME_LIMIT were
4156 13, then next_almost_prime_increment_max would be 14, e.g.,
4157 because next_almost_prime (113) would be 127. */
4158 {
4159 verify (NEXT_ALMOST_PRIME_LIMIT == 11);
4160 enum { next_almost_prime_increment_max = 10 };
ca9ce8f2 4161 ptrdiff_t row_table_max =
0065d054
PE
4162 (min (PTRDIFF_MAX, SIZE_MAX) / (3 * sizeof *row_table)
4163 - next_almost_prime_increment_max);
4164 ptrdiff_t current_nrows_max = row_table_max - desired_matrix->nrows;
4165 if (current_nrows_max < current_matrix->nrows)
ca9ce8f2
PE
4166 memory_full (SIZE_MAX);
4167 }
4168
6df5d75f 4169 /* Reallocate vectors, tables etc. if necessary. */
177c0ea7 4170
6df5d75f 4171 if (current_matrix->nrows > old_lines_size)
0065d054
PE
4172 old_lines = xpalloc (old_lines, &old_lines_size,
4173 current_matrix->nrows - old_lines_size,
4174 INT_MAX, sizeof *old_lines);
177c0ea7 4175
6df5d75f 4176 if (desired_matrix->nrows > new_lines_size)
0065d054
PE
4177 new_lines = xpalloc (new_lines, &new_lines_size,
4178 desired_matrix->nrows - new_lines_size,
4179 INT_MAX, sizeof *new_lines);
6df5d75f 4180
ca9ce8f2
PE
4181 n = desired_matrix->nrows;
4182 n += current_matrix->nrows;
0065d054 4183 if (row_table_size < 3 * n)
6df5d75f 4184 {
ca9ce8f2 4185 ptrdiff_t size = next_almost_prime (3 * n);
0065d054 4186 row_table = xnrealloc (row_table, size, sizeof *row_table);
ca9ce8f2 4187 row_table_size = size;
0065d054 4188 memset (row_table, 0, size * sizeof *row_table);
6df5d75f
GM
4189 }
4190
4191 if (n > row_entry_pool_size)
0065d054
PE
4192 row_entry_pool = xpalloc (row_entry_pool, &row_entry_pool_size,
4193 n - row_entry_pool_size,
4194 -1, sizeof *row_entry_pool);
6df5d75f
GM
4195
4196 if (desired_matrix->nrows > runs_size)
4197 {
0065d054
PE
4198 runs = xnrealloc (runs, desired_matrix->nrows, sizeof *runs);
4199 run_pool = xnrealloc (run_pool, desired_matrix->nrows, sizeof *run_pool);
ca9ce8f2 4200 runs_size = desired_matrix->nrows;
6df5d75f
GM
4201 }
4202
4203 nruns = run_idx = 0;
4204 row_entry_idx = 0;
4205
4206 /* Add rows from the current and desired matrix to the hash table
4207 row_hash_table to be able to find equal ones quickly. */
177c0ea7 4208
5f5c8ee5
GM
4209 for (i = first_old; i < last_old; ++i)
4210 {
96c06863 4211 if (MATRIX_ROW_ENABLED_P (current_matrix, i))
5f5c8ee5 4212 {
6b1f9ba4 4213 entry = add_row_entry (MATRIX_ROW (current_matrix, i));
6df5d75f
GM
4214 old_lines[i] = entry;
4215 ++entry->old_uses;
5f5c8ee5
GM
4216 }
4217 else
6df5d75f 4218 old_lines[i] = NULL;
5f5c8ee5
GM
4219 }
4220
5f5c8ee5
GM
4221 for (i = first_new; i < last_new; ++i)
4222 {
a54e2c05 4223 eassert (MATRIX_ROW_ENABLED_P (desired_matrix, i));
6b1f9ba4 4224 entry = add_row_entry (MATRIX_ROW (desired_matrix, i));
6df5d75f
GM
4225 ++entry->new_uses;
4226 entry->new_line_number = i;
4227 new_lines[i] = entry;
5f5c8ee5 4228 }
4588ec20 4229
5f5c8ee5
GM
4230 /* Identify moves based on lines that are unique and equal
4231 in both matrices. */
4232 for (i = first_old; i < last_old;)
6df5d75f
GM
4233 if (old_lines[i]
4234 && old_lines[i]->old_uses == 1
4235 && old_lines[i]->new_uses == 1)
5f5c8ee5 4236 {
27786b2f 4237 int p, q;
6df5d75f
GM
4238 int new_line = old_lines[i]->new_line_number;
4239 struct run *run = run_pool + run_idx++;
5f5c8ee5
GM
4240
4241 /* Record move. */
4242 run->current_vpos = i;
4243 run->current_y = MATRIX_ROW (current_matrix, i)->y;
4244 run->desired_vpos = new_line;
4245 run->desired_y = MATRIX_ROW (desired_matrix, new_line)->y;
4246 run->nrows = 1;
4247 run->height = MATRIX_ROW (current_matrix, i)->height;
4248
4249 /* Extend backward. */
27786b2f
PE
4250 p = i - 1;
4251 q = new_line - 1;
4252 while (p > first_old
4253 && q > first_new
4254 && old_lines[p] == new_lines[q])
5f5c8ee5 4255 {
27786b2f 4256 int h = MATRIX_ROW (current_matrix, p)->height;
177c0ea7
JB
4257 --run->current_vpos;
4258 --run->desired_vpos;
5f5c8ee5
GM
4259 ++run->nrows;
4260 run->height += h;
4261 run->desired_y -= h;
4262 run->current_y -= h;
27786b2f 4263 --p, --q;
5f5c8ee5 4264 }
4588ec20 4265
5f5c8ee5 4266 /* Extend forward. */
27786b2f
PE
4267 p = i + 1;
4268 q = new_line + 1;
4269 while (p < last_old
4270 && q < last_new
4271 && old_lines[p] == new_lines[q])
5f5c8ee5 4272 {
27786b2f 4273 int h = MATRIX_ROW (current_matrix, p)->height;
177c0ea7 4274 ++run->nrows;
5f5c8ee5 4275 run->height += h;
27786b2f 4276 ++p, ++q;
5f5c8ee5 4277 }
19dff8dc 4278
5f5c8ee5
GM
4279 /* Insert run into list of all runs. Order runs by copied
4280 pixel lines. Note that we record runs that don't have to
4281 be copied because they are already in place. This is done
4282 because we can avoid calling update_window_line in this
4283 case. */
27786b2f 4284 for (p = 0; p < nruns && runs[p]->height > run->height; ++p)
5f5c8ee5 4285 ;
27786b2f
PE
4286 for (q = nruns; q > p; --q)
4287 runs[q] = runs[q - 1];
4288 runs[p] = run;
5f5c8ee5
GM
4289 ++nruns;
4290
4291 i += run->nrows;
4292 }
4293 else
4294 ++i;
4588ec20 4295
5f5c8ee5
GM
4296 /* Do the moves. Do it in a way that we don't overwrite something
4297 we want to copy later on. This is not solvable in general
4298 because there is only one display and we don't have a way to
4299 exchange areas on this display. Example:
4588ec20 4300
5f5c8ee5
GM
4301 +-----------+ +-----------+
4302 | A | | B |
4303 +-----------+ --> +-----------+
4304 | B | | A |
4305 +-----------+ +-----------+
4588ec20 4306
5f5c8ee5
GM
4307 Instead, prefer bigger moves, and invalidate moves that would
4308 copy from where we copied to. */
ea0d86af 4309
5f5c8ee5
GM
4310 for (i = 0; i < nruns; ++i)
4311 if (runs[i]->nrows > 0)
4312 {
4313 struct run *r = runs[i];
24e86043 4314
5f5c8ee5
GM
4315 /* Copy on the display. */
4316 if (r->current_y != r->desired_y)
4317 {
989b42d2 4318 rif->clear_window_mouse_face (w);
5f5c8ee5 4319 rif->scroll_run_hook (w, r);
1305621b 4320 }
5f5c8ee5 4321
1305621b
YM
4322 /* Truncate runs that copy to where we copied to, and
4323 invalidate runs that copy from where we copied to. */
4324 for (j = nruns - 1; j > i; --j)
4325 {
4326 struct run *p = runs[j];
59ea14cd 4327 bool truncated_p = 0;
5f5c8ee5 4328
1305621b
YM
4329 if (p->nrows > 0
4330 && p->desired_y < r->desired_y + r->height
4331 && p->desired_y + p->height > r->desired_y)
5f5c8ee5 4332 {
1305621b
YM
4333 if (p->desired_y < r->desired_y)
4334 {
4335 p->nrows = r->desired_vpos - p->desired_vpos;
4336 p->height = r->desired_y - p->desired_y;
4337 truncated_p = 1;
4338 }
4339 else
4340 {
4341 int nrows_copied = (r->desired_vpos + r->nrows
4342 - p->desired_vpos);
4343
4344 if (p->nrows <= nrows_copied)
4345 p->nrows = 0;
4346 else
4347 {
4348 int height_copied = (r->desired_y + r->height
4349 - p->desired_y);
4350
4351 p->current_vpos += nrows_copied;
4352 p->desired_vpos += nrows_copied;
4353 p->nrows -= nrows_copied;
4354 p->current_y += height_copied;
4355 p->desired_y += height_copied;
4356 p->height -= height_copied;
4357 truncated_p = 1;
4358 }
4359 }
4360 }
177c0ea7 4361
1305621b
YM
4362 if (r->current_y != r->desired_y
4363 /* The condition below is equivalent to
4364 ((p->current_y >= r->desired_y
5f5c8ee5 4365 && p->current_y < r->desired_y + r->height)
1305621b 4366 || (p->current_y + p->height > r->desired_y
5f5c8ee5 4367 && (p->current_y + p->height
1305621b
YM
4368 <= r->desired_y + r->height)))
4369 because we have 0 < p->height <= r->height. */
4370 && p->current_y < r->desired_y + r->height
4371 && p->current_y + p->height > r->desired_y)
4372 p->nrows = 0;
4373
4374 /* Reorder runs by copied pixel lines if truncated. */
4375 if (truncated_p && p->nrows > 0)
4376 {
4377 int k = nruns - 1;
4378
4379 while (runs[k]->nrows == 0 || runs[k]->height < p->height)
4380 k--;
4381 memmove (runs + j, runs + j + 1, (k - j) * sizeof (*runs));
4382 runs[k] = p;
5f5c8ee5
GM
4383 }
4384 }
ea0d86af 4385
5f5c8ee5
GM
4386 /* Assign matrix rows. */
4387 for (j = 0; j < r->nrows; ++j)
4388 {
4389 struct glyph_row *from, *to;
59ea14cd 4390 bool to_overlapped_p;
e876ff42 4391
5f5c8ee5
GM
4392 to = MATRIX_ROW (current_matrix, r->desired_vpos + j);
4393 from = MATRIX_ROW (desired_matrix, r->desired_vpos + j);
e876ff42 4394 to_overlapped_p = to->overlapped_p;
e61124cd 4395 from->redraw_fringe_bitmaps_p = from->fringe_bitmap_periodic_p;
5f5c8ee5 4396 assign_row (to, from);
1305621b
YM
4397 /* The above `assign_row' actually does swap, so if we had
4398 an overlap in the copy destination of two runs, then
4399 the second run would assign a previously disabled bogus
4400 row. But thanks to the truncation code in the
4401 preceding for-loop, we no longer have such an overlap,
4402 and thus the assigned row should always be enabled. */
a54e2c05 4403 eassert (to->enabled_p);
96c06863 4404 from->enabled_p = false;
408f5064 4405 to->overlapped_p = to_overlapped_p;
5f5c8ee5
GM
4406 }
4407 }
15874c59 4408
6df5d75f
GM
4409 /* Clear the hash table, for the next time. */
4410 for (i = 0; i < row_entry_idx; ++i)
4411 row_table[row_entry_pool[i].bucket] = NULL;
4412
aec1708a 4413 /* Value is 1 to indicate that we scrolled the display. */
908589fd 4414 return nruns > 0;
5f5c8ee5 4415}
de83c314 4416
15874c59 4417
4588ec20 4418\f
5f5c8ee5
GM
4419/************************************************************************
4420 Frame-Based Updates
4421 ************************************************************************/
4588ec20 4422
5f5c8ee5 4423/* Update the desired frame matrix of frame F.
4588ec20 4424
59ea14cd 4425 FORCE_P means that the update should not be stopped by pending input.
d13adf6d
EZ
4426 INHIBIT_ID_P means that scrolling by insert/delete should not be tried.
4427 SET_CURSOR_P false means do not set cursor at point in selected window.
5f5c8ee5 4428
59ea14cd 4429 Value is true if update was stopped due to pending input. */
5f5c8ee5 4430
59ea14cd 4431static bool
d13adf6d
EZ
4432update_frame_1 (struct frame *f, bool force_p, bool inhibit_id_p,
4433 bool set_cursor_p)
4588ec20 4434{
5f5c8ee5
GM
4435 /* Frame matrices to work on. */
4436 struct glyph_matrix *current_matrix = f->current_matrix;
4437 struct glyph_matrix *desired_matrix = f->desired_matrix;
4438 int i;
59ea14cd 4439 bool pause_p;
4588ec20 4440 int preempt_count = baud_rate / 2400 + 1;
5f5c8ee5 4441
a54e2c05 4442 eassert (current_matrix && desired_matrix);
4588ec20 4443
c37e4889
RS
4444 if (baud_rate != FRAME_COST_BAUD_RATE (f))
4445 calculate_costs (f);
4446
d88c2b9e
RS
4447 if (preempt_count <= 0)
4448 preempt_count = 1;
4449
a2130dbb 4450 if (!force_p && detect_input_pending_ignore_squeezables ())
4588ec20 4451 {
27786b2f 4452 pause_p = 1;
4588ec20
JB
4453 goto do_pause;
4454 }
4455
5f5c8ee5 4456 /* If we cannot insert/delete lines, it's no use trying it. */
daf01701 4457 if (!FRAME_LINE_INS_DEL_OK (f))
5f5c8ee5 4458 inhibit_id_p = 1;
7098a0fa 4459
efb859b4 4460 /* See if any of the desired lines are enabled; don't compute for
5f5c8ee5
GM
4461 i/d line if just want cursor motion. */
4462 for (i = 0; i < desired_matrix->nrows; i++)
4463 if (MATRIX_ROW_ENABLED_P (desired_matrix, i))
4588ec20
JB
4464 break;
4465
4466 /* Try doing i/d line, if not yet inhibited. */
5f5c8ee5
GM
4467 if (!inhibit_id_p && i < desired_matrix->nrows)
4468 force_p |= scrolling (f);
4588ec20
JB
4469
4470 /* Update the individual lines as needed. Do bottom line first. */
5f5c8ee5
GM
4471 if (MATRIX_ROW_ENABLED_P (desired_matrix, desired_matrix->nrows - 1))
4472 update_frame_line (f, desired_matrix->nrows - 1);
4588ec20 4473
5f5c8ee5
GM
4474 /* Now update the rest of the lines. */
4475 for (i = 0; i < desired_matrix->nrows - 1 && (force_p || !input_pending); i++)
4588ec20 4476 {
5f5c8ee5 4477 if (MATRIX_ROW_ENABLED_P (desired_matrix, i))
4588ec20 4478 {
b6a65ac2 4479 if (FRAME_TERMCAP_P (f))
4588ec20
JB
4480 {
4481 /* Flush out every so many lines.
4482 Also flush out if likely to have more than 1k buffered
4483 otherwise. I'm told that some telnet connections get
4484 really screwed by more than 1k output at once. */
fced92f1
CY
4485 FILE *display_output = FRAME_TTY (f)->output;
4486 if (display_output)
4588ec20 4487 {
8148369c 4488 ptrdiff_t outq = __fpending (display_output);
fced92f1
CY
4489 if (outq > 900
4490 || (outq > 20 && ((i - 1) % preempt_count == 0)))
06b63c9b 4491 fflush (display_output);
4588ec20 4492 }
4588ec20
JB
4493 }
4494
d012c62b 4495 if (!force_p && (i - 1) % preempt_count == 0)
a2d5fca0 4496 detect_input_pending_ignore_squeezables ();
a2960116 4497
5f5c8ee5 4498 update_frame_line (f, i);
4588ec20 4499 }
4588ec20 4500 }
177c0ea7 4501
59ea14cd 4502 pause_p = 0 < i && i < FRAME_LINES (f) - 1;
4588ec20
JB
4503
4504 /* Now just clean up termcap drivers and set cursor, etc. */
d13adf6d 4505 if (!pause_p && set_cursor_p)
4588ec20 4506 {
48cf7030 4507 if ((cursor_in_echo_area
5f5c8ee5 4508 /* If we are showing a message instead of the mini-buffer,
2577053b 4509 show the cursor for the message instead of for the
5f5c8ee5 4510 (now hidden) mini-buffer contents. */
2577053b
RS
4511 || (EQ (minibuf_window, selected_window)
4512 && EQ (minibuf_window, echo_area_window)
b96fd3e8 4513 && !NILP (echo_area_buffer[0])))
2577053b 4514 /* These cases apply only to the frame that contains
5f5c8ee5 4515 the active mini-buffer window. */
2577053b 4516 && FRAME_HAS_MINIBUF_P (f)
140f8645 4517 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
1113d9db 4518 {
7df02016 4519 int top = WINDOW_TOP_EDGE_LINE (XWINDOW (FRAME_MINIBUF_WINDOW (f)));
648fa17d
JB
4520 int row, col;
4521
4522 if (cursor_in_echo_area < 0)
4523 {
5f5c8ee5
GM
4524 /* Negative value of cursor_in_echo_area means put
4525 cursor at beginning of line. */
648fa17d
JB
4526 row = top;
4527 col = 0;
4528 }
1113d9db 4529 else
648fa17d 4530 {
5f5c8ee5
GM
4531 /* Positive value of cursor_in_echo_area means put
4532 cursor at the end of the prompt. If the mini-buffer
4533 is several lines high, find the last line that has
4534 any text on it. */
7df02016 4535 row = FRAME_LINES (f);
177c0ea7 4536 do
648fa17d 4537 {
5f5c8ee5
GM
4538 --row;
4539 col = 0;
177c0ea7 4540
5f5c8ee5
GM
4541 if (MATRIX_ROW_ENABLED_P (current_matrix, row))
4542 {
4543 /* Frame rows are filled up with spaces that
4544 must be ignored here. */
5f5c8ee5
GM
4545 struct glyph_row *r = MATRIX_ROW (current_matrix,
4546 row);
4547 struct glyph *start = r->glyphs[TEXT_AREA];
4548 struct glyph *last = start + r->used[TEXT_AREA];
4549
4550 while (last > start
4551 && (last - 1)->charpos < 0)
4552 --last;
177c0ea7 4553
5f5c8ee5
GM
4554 col = last - start;
4555 }
648fa17d
JB
4556 }
4557 while (row > top && col == 0);
4558
6395da5c 4559 /* Make sure COL is not out of range. */
868e640e 4560 if (col >= FRAME_CURSOR_X_LIMIT (f))
648fa17d 4561 {
6395da5c 4562 /* If we have another row, advance cursor into it. */
7df02016 4563 if (row < FRAME_LINES (f) - 1)
6395da5c 4564 {
7df02016 4565 col = FRAME_LEFT_SCROLL_BAR_COLS (f);
6395da5c
RS
4566 row++;
4567 }
4568 /* Otherwise move it back in range. */
4569 else
868e640e 4570 col = FRAME_CURSOR_X_LIMIT (f) - 1;
648fa17d
JB
4571 }
4572 }
4573
385ed61f 4574 cursor_to (f, row, col);
1113d9db 4575 }
4588ec20 4576 else
5f5c8ee5
GM
4577 {
4578 /* We have only one cursor on terminal frames. Use it to
4579 display the cursor of the selected window. */
4580 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
a871441d
GM
4581 if (w->cursor.vpos >= 0
4582 /* The cursor vpos may be temporarily out of bounds
4583 in the following situation: There is one window,
4584 with the cursor in the lower half of it. The window
4585 is split, and a message causes a redisplay before
4586 a new cursor position has been computed. */
7df02016 4587 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
5f5c8ee5
GM
4588 {
4589 int x = WINDOW_TO_FRAME_HPOS (w, w->cursor.hpos);
4590 int y = WINDOW_TO_FRAME_VPOS (w, w->cursor.vpos);
4591
eeaf9bf3 4592 x += max (0, w->left_margin_cols);
385ed61f 4593 cursor_to (f, y, x);
5f5c8ee5
GM
4594 }
4595 }
4588ec20
JB
4596 }
4597
4588ec20
JB
4598 do_pause:
4599
5f5c8ee5 4600 clear_desired_matrices (f);
27786b2f 4601 return pause_p;
4588ec20
JB
4602}
4603
4588ec20 4604
5f5c8ee5 4605/* Do line insertions/deletions on frame F for frame-based redisplay. */
4588ec20 4606
59ea14cd 4607static bool
971de7fb 4608scrolling (struct frame *frame)
4588ec20
JB
4609{
4610 int unchanged_at_top, unchanged_at_bottom;
4611 int window_size;
4612 int changed_lines;
38182d90
PE
4613 int *old_hash = alloca (FRAME_LINES (frame) * sizeof (int));
4614 int *new_hash = alloca (FRAME_LINES (frame) * sizeof (int));
4615 int *draw_cost = alloca (FRAME_LINES (frame) * sizeof (int));
4616 int *old_draw_cost = alloca (FRAME_LINES (frame) * sizeof (int));
4588ec20 4617 register int i;
7df02016 4618 int free_at_end_vpos = FRAME_LINES (frame);
5f5c8ee5
GM
4619 struct glyph_matrix *current_matrix = frame->current_matrix;
4620 struct glyph_matrix *desired_matrix = frame->desired_matrix;
4588ec20 4621
77e3b1b7 4622 eassert (current_matrix);
4588ec20 4623
5f5c8ee5
GM
4624 /* Compute hash codes of all the lines. Also calculate number of
4625 changed lines, number of unchanged lines at the beginning, and
4626 number of unchanged lines at the end. */
4588ec20
JB
4627 changed_lines = 0;
4628 unchanged_at_top = 0;
7df02016
KS
4629 unchanged_at_bottom = FRAME_LINES (frame);
4630 for (i = 0; i < FRAME_LINES (frame); i++)
4588ec20
JB
4631 {
4632 /* Give up on this scrolling if some old lines are not enabled. */
5f5c8ee5 4633 if (!MATRIX_ROW_ENABLED_P (current_matrix, i))
4588ec20 4634 return 0;
77e3b1b7 4635 old_hash[i] = line_hash_code (frame, MATRIX_ROW (current_matrix, i));
5f5c8ee5 4636 if (! MATRIX_ROW_ENABLED_P (desired_matrix, i))
f188b3c4
RS
4637 {
4638 /* This line cannot be redrawn, so don't let scrolling mess it. */
4639 new_hash[i] = old_hash[i];
4640#define INFINITY 1000000 /* Taken from scroll.c */
4641 draw_cost[i] = INFINITY;
4642 }
4588ec20 4643 else
f188b3c4 4644 {
77e3b1b7
DA
4645 new_hash[i] = line_hash_code (frame, MATRIX_ROW (desired_matrix, i));
4646 draw_cost[i] = line_draw_cost (frame, desired_matrix, i);
f188b3c4 4647 }
4588ec20
JB
4648
4649 if (old_hash[i] != new_hash[i])
4650 {
4651 changed_lines++;
7df02016 4652 unchanged_at_bottom = FRAME_LINES (frame) - i - 1;
4588ec20
JB
4653 }
4654 else if (i == unchanged_at_top)
4655 unchanged_at_top++;
77e3b1b7 4656 old_draw_cost[i] = line_draw_cost (frame, current_matrix, i);
4588ec20
JB
4657 }
4658
4659 /* If changed lines are few, don't allow preemption, don't scroll. */
daf01701 4660 if ((!FRAME_SCROLL_REGION_OK (frame)
8a56675d 4661 && changed_lines < baud_rate / 2400)
7df02016 4662 || unchanged_at_bottom == FRAME_LINES (frame))
4588ec20
JB
4663 return 1;
4664
7df02016 4665 window_size = (FRAME_LINES (frame) - unchanged_at_top
4588ec20
JB
4666 - unchanged_at_bottom);
4667
daf01701 4668 if (FRAME_SCROLL_REGION_OK (frame))
4588ec20 4669 free_at_end_vpos -= unchanged_at_bottom;
daf01701 4670 else if (FRAME_MEMORY_BELOW_FRAME (frame))
4588ec20
JB
4671 free_at_end_vpos = -1;
4672
4673 /* If large window, fast terminal and few lines in common between
5f5c8ee5 4674 current frame and desired frame, don't bother with i/d calc. */
daf01701 4675 if (!FRAME_SCROLL_REGION_OK (frame)
8a56675d 4676 && window_size >= 18 && baud_rate > 2400
4588ec20
JB
4677 && (window_size >=
4678 10 * scrolling_max_lines_saved (unchanged_at_top,
7df02016 4679 FRAME_LINES (frame) - unchanged_at_bottom,
4588ec20
JB
4680 old_hash, new_hash, draw_cost)))
4681 return 0;
4682
5f5c8ee5
GM
4683 if (window_size < 2)
4684 return 0;
4685
502b9b64 4686 scrolling_1 (frame, window_size, unchanged_at_top, unchanged_at_bottom,
4588ec20 4687 draw_cost + unchanged_at_top - 1,
190bb91a 4688 old_draw_cost + unchanged_at_top - 1,
4588ec20
JB
4689 old_hash + unchanged_at_top - 1,
4690 new_hash + unchanged_at_top - 1,
4691 free_at_end_vpos - unchanged_at_top);
4692
4693 return 0;
4694}
4588ec20 4695
4588ec20 4696
5f5c8ee5
GM
4697/* Count the number of blanks at the start of the vector of glyphs R
4698 which is LEN glyphs long. */
4588ec20 4699
4588ec20 4700static int
971de7fb 4701count_blanks (struct glyph *r, int len)
4588ec20 4702{
5f5c8ee5 4703 int i;
177c0ea7 4704
5f5c8ee5
GM
4705 for (i = 0; i < len; ++i)
4706 if (!CHAR_GLYPH_SPACE_P (r[i]))
4707 break;
4708
4709 return i;
4588ec20
JB
4710}
4711
5f5c8ee5
GM
4712
4713/* Count the number of glyphs in common at the start of the glyph
4714 vectors STR1 and STR2. END1 is the end of STR1 and END2 is the end
4715 of STR2. Value is the number of equal glyphs equal at the start. */
4716
4588ec20 4717static int
971de7fb 4718count_match (struct glyph *str1, struct glyph *end1, struct glyph *str2, struct glyph *end2)
4588ec20 4719{
5f5c8ee5
GM
4720 struct glyph *p1 = str1;
4721 struct glyph *p2 = str2;
177c0ea7 4722
5f5c8ee5
GM
4723 while (p1 < end1
4724 && p2 < end2
42186983 4725 && GLYPH_CHAR_AND_FACE_EQUAL_P (p1, p2))
5f5c8ee5 4726 ++p1, ++p2;
177c0ea7 4727
5f5c8ee5 4728 return p1 - str1;
4588ec20
JB
4729}
4730
5f5c8ee5 4731
4588ec20 4732/* Char insertion/deletion cost vector, from term.c */
4588ec20 4733
5e617bc2 4734#define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_TOTAL_COLS ((f))])
4588ec20 4735
5f5c8ee5
GM
4736
4737/* Perform a frame-based update on line VPOS in frame FRAME. */
4738
4588ec20 4739static void
971de7fb 4740update_frame_line (struct frame *f, int vpos)
4588ec20 4741{
5f5c8ee5 4742 struct glyph *obody, *nbody, *op1, *op2, *np1, *nend;
4588ec20
JB
4743 int tem;
4744 int osp, nsp, begmatch, endmatch, olen, nlen;
c363a1d6
GM
4745 struct glyph_matrix *current_matrix = f->current_matrix;
4746 struct glyph_matrix *desired_matrix = f->desired_matrix;
5f5c8ee5
GM
4747 struct glyph_row *current_row = MATRIX_ROW (current_matrix, vpos);
4748 struct glyph_row *desired_row = MATRIX_ROW (desired_matrix, vpos);
59ea14cd
PE
4749 bool must_write_whole_line_p;
4750 bool write_spaces_p = FRAME_MUST_WRITE_SPACES (f);
4751 bool colored_spaces_p = (FACE_FROM_ID (f, DEFAULT_FACE_ID)->background
4752 != FACE_TTY_DEFAULT_BG_COLOR);
0a894bad
GM
4753
4754 if (colored_spaces_p)
4755 write_spaces_p = 1;
5f5c8ee5 4756
26c63686
GM
4757 /* Current row not enabled means it has unknown contents. We must
4758 write the whole desired line in that case. */
5f5c8ee5
GM
4759 must_write_whole_line_p = !current_row->enabled_p;
4760 if (must_write_whole_line_p)
4588ec20 4761 {
5f5c8ee5 4762 obody = 0;
4588ec20
JB
4763 olen = 0;
4764 }
4765 else
4766 {
5f5c8ee5
GM
4767 obody = MATRIX_ROW_GLYPH_START (current_matrix, vpos);
4768 olen = current_row->used[TEXT_AREA];
177c0ea7 4769
4cecaab8
MB
4770 /* Ignore trailing spaces, if we can. */
4771 if (!write_spaces_p)
4772 while (olen > 0 && CHAR_GLYPH_SPACE_P (obody[olen-1]))
4773 olen--;
4588ec20
JB
4774 }
4775
96c06863 4776 current_row->enabled_p = true;
5f5c8ee5 4777 current_row->used[TEXT_AREA] = desired_row->used[TEXT_AREA];
4588ec20 4778
5f5c8ee5
GM
4779 /* If desired line is empty, just clear the line. */
4780 if (!desired_row->enabled_p)
4588ec20
JB
4781 {
4782 nlen = 0;
4783 goto just_erase;
4784 }
4785
5f5c8ee5
GM
4786 nbody = desired_row->glyphs[TEXT_AREA];
4787 nlen = desired_row->used[TEXT_AREA];
4788 nend = nbody + nlen;
4789
4790 /* If display line has unknown contents, write the whole line. */
4791 if (must_write_whole_line_p)
4792 {
26c63686 4793 /* Ignore spaces at the end, if we can. */
0a894bad 4794 if (!write_spaces_p)
74ca462f
GM
4795 while (nlen > 0 && CHAR_GLYPH_SPACE_P (nbody[nlen - 1]))
4796 --nlen;
4797
26c63686 4798 /* Write the contents of the desired line. */
74ca462f 4799 if (nlen)
26c63686 4800 {
385ed61f
KL
4801 cursor_to (f, vpos, 0);
4802 write_glyphs (f, nbody, nlen);
26c63686 4803 }
177c0ea7 4804
26c63686
GM
4805 /* Don't call clear_end_of_line if we already wrote the whole
4806 line. The cursor will not be at the right margin in that
4807 case but in the line below. */
7df02016 4808 if (nlen < FRAME_TOTAL_COLS (f))
26c63686 4809 {
385ed61f
KL
4810 cursor_to (f, vpos, nlen);
4811 clear_end_of_line (f, FRAME_TOTAL_COLS (f));
26c63686 4812 }
58827478
GM
4813 else
4814 /* Make sure we are in the right row, otherwise cursor movement
4815 with cmgoto might use `ch' in the wrong row. */
385ed61f 4816 cursor_to (f, vpos, 0);
177c0ea7 4817
5f5c8ee5
GM
4818 make_current (desired_matrix, current_matrix, vpos);
4819 return;
4820 }
4588ec20
JB
4821
4822 /* Pretend trailing spaces are not there at all,
4823 unless for one reason or another we must write all spaces. */
4cecaab8
MB
4824 if (!write_spaces_p)
4825 while (nlen > 0 && CHAR_GLYPH_SPACE_P (nbody[nlen - 1]))
4826 nlen--;
4588ec20
JB
4827
4828 /* If there's no i/d char, quickly do the best we can without it. */
daf01701 4829 if (!FRAME_CHAR_INS_DEL_OK (f))
4588ec20 4830 {
5f5c8ee5 4831 int i, j;
4588ec20 4832
5f5c8ee5
GM
4833 /* Find the first glyph in desired row that doesn't agree with
4834 a glyph in the current row, and write the rest from there on. */
4588ec20
JB
4835 for (i = 0; i < nlen; i++)
4836 {
5f5c8ee5 4837 if (i >= olen || !GLYPH_EQUAL_P (nbody + i, obody + i))
4588ec20 4838 {
5f5c8ee5
GM
4839 /* Find the end of the run of different glyphs. */
4840 j = i + 1;
4841 while (j < nlen
4842 && (j >= olen
4843 || !GLYPH_EQUAL_P (nbody + j, obody + j)
4844 || CHAR_GLYPH_PADDING_P (nbody[j])))
4845 ++j;
177c0ea7
JB
4846
4847 /* Output this run of non-matching chars. */
385ed61f
KL
4848 cursor_to (f, vpos, i);
4849 write_glyphs (f, nbody + i, j - i);
5f5c8ee5 4850 i = j - 1;
4588ec20
JB
4851
4852 /* Now find the next non-match. */
4853 }
4854 }
4855
4856 /* Clear the rest of the line, or the non-clear part of it. */
4857 if (olen > nlen)
4858 {
385ed61f
KL
4859 cursor_to (f, vpos, nlen);
4860 clear_end_of_line (f, olen);
4588ec20
JB
4861 }
4862
5f5c8ee5
GM
4863 /* Make current row = desired row. */
4864 make_current (desired_matrix, current_matrix, vpos);
4588ec20
JB
4865 return;
4866 }
4867
5f5c8ee5
GM
4868 /* Here when CHAR_INS_DEL_OK != 0, i.e. we can insert or delete
4869 characters in a row. */
4870
4588ec20
JB
4871 if (!olen)
4872 {
5f5c8ee5
GM
4873 /* If current line is blank, skip over initial spaces, if
4874 possible, and write the rest. */
4cecaab8 4875 if (write_spaces_p)
5f5c8ee5
GM
4876 nsp = 0;
4877 else
4878 nsp = count_blanks (nbody, nlen);
4879
4588ec20
JB
4880 if (nlen > nsp)
4881 {
385ed61f
KL
4882 cursor_to (f, vpos, nsp);
4883 write_glyphs (f, nbody + nsp, nlen - nsp);
4588ec20
JB
4884 }
4885
502b9b64 4886 /* Exchange contents between current_frame and new_frame. */
5f5c8ee5 4887 make_current (desired_matrix, current_matrix, vpos);
4588ec20
JB
4888 return;
4889 }
4890
4588ec20 4891 /* Compute number of leading blanks in old and new contents. */
5f5c8ee5 4892 osp = count_blanks (obody, olen);
4cecaab8 4893 nsp = (colored_spaces_p ? 0 : count_blanks (nbody, nlen));
4588ec20 4894
5f5c8ee5
GM
4895 /* Compute number of matching chars starting with first non-blank. */
4896 begmatch = count_match (obody + osp, obody + olen,
4897 nbody + nsp, nbody + nlen);
4588ec20
JB
4898
4899 /* Spaces in new match implicit space past the end of old. */
4900 /* A bug causing this to be a no-op was fixed in 18.29. */
0a894bad 4901 if (!write_spaces_p && osp + begmatch == olen)
4588ec20
JB
4902 {
4903 np1 = nbody + nsp;
5f5c8ee5
GM
4904 while (np1 + begmatch < nend && CHAR_GLYPH_SPACE_P (np1[begmatch]))
4905 ++begmatch;
4588ec20
JB
4906 }
4907
4908 /* Avoid doing insert/delete char
4909 just cause number of leading spaces differs
5f5c8ee5 4910 when the following text does not match. */
4588ec20
JB
4911 if (begmatch == 0 && osp != nsp)
4912 osp = nsp = min (osp, nsp);
4913
4914 /* Find matching characters at end of line */
4915 op1 = obody + olen;
4916 np1 = nbody + nlen;
4917 op2 = op1 + begmatch - min (olen - osp, nlen - nsp);
5f5c8ee5
GM
4918 while (op1 > op2
4919 && GLYPH_EQUAL_P (op1 - 1, np1 - 1))
4588ec20
JB
4920 {
4921 op1--;
4922 np1--;
4923 }
4924 endmatch = obody + olen - op1;
4925
4588ec20
JB
4926 /* tem gets the distance to insert or delete.
4927 endmatch is how many characters we save by doing so.
4928 Is it worth it? */
4929
4930 tem = (nlen - nsp) - (olen - osp);
4931 if (endmatch && tem
daf01701 4932 && (!FRAME_CHAR_INS_DEL_OK (f)
8a56675d 4933 || endmatch <= char_ins_del_cost (f)[tem]))
4588ec20
JB
4934 endmatch = 0;
4935
4936 /* nsp - osp is the distance to insert or delete.
4937 If that is nonzero, begmatch is known to be nonzero also.
4938 begmatch + endmatch is how much we save by doing the ins/del.
4939 Is it worth it? */
4940
4941 if (nsp != osp
daf01701 4942 && (!FRAME_CHAR_INS_DEL_OK (f)
c363a1d6 4943 || begmatch + endmatch <= char_ins_del_cost (f)[nsp - osp]))
4588ec20
JB
4944 {
4945 begmatch = 0;
4946 endmatch = 0;
4947 osp = nsp = min (osp, nsp);
4948 }
4949
4950 /* Now go through the line, inserting, writing and
4951 deleting as appropriate. */
4952
4953 if (osp > nsp)
4954 {
385ed61f
KL
4955 cursor_to (f, vpos, nsp);
4956 delete_glyphs (f, osp - nsp);
4588ec20
JB
4957 }
4958 else if (nsp > osp)
4959 {
4960 /* If going to delete chars later in line
4961 and insert earlier in the line,
4962 must delete first to avoid losing data in the insert */
4963 if (endmatch && nlen < olen + nsp - osp)
4964 {
385ed61f
KL
4965 cursor_to (f, vpos, nlen - endmatch + osp - nsp);
4966 delete_glyphs (f, olen + nsp - osp - nlen);
4588ec20
JB
4967 olen = nlen - (nsp - osp);
4968 }
385ed61f
KL
4969 cursor_to (f, vpos, osp);
4970 insert_glyphs (f, 0, nsp - osp);
4588ec20
JB
4971 }
4972 olen += nsp - osp;
4973
4974 tem = nsp + begmatch + endmatch;
4975 if (nlen != tem || olen != tem)
4976 {
4588ec20
JB
4977 if (!endmatch || nlen == olen)
4978 {
c363a1d6
GM
4979 /* If new text being written reaches right margin, there is
4980 no need to do clear-to-eol at the end of this function
4981 (and it would not be safe, since cursor is not going to
4982 be "at the margin" after the text is done). */
7df02016 4983 if (nlen == FRAME_TOTAL_COLS (f))
4588ec20 4984 olen = 0;
c363a1d6
GM
4985
4986 /* Function write_glyphs is prepared to do nothing
4987 if passed a length <= 0. Check it here to avoid
4988 unnecessary cursor movement. */
4989 if (nlen - tem > 0)
4990 {
385ed61f
KL
4991 cursor_to (f, vpos, nsp + begmatch);
4992 write_glyphs (f, nbody + nsp + begmatch, nlen - tem);
c363a1d6 4993 }
4588ec20
JB
4994 }
4995 else if (nlen > olen)
4996 {
24e86043
KH
4997 /* Here, we used to have the following simple code:
4998 ----------------------------------------
4999 write_glyphs (nbody + nsp + begmatch, olen - tem);
5000 insert_glyphs (nbody + nsp + begmatch + olen - tem, nlen - olen);
5001 ----------------------------------------
5002 but it doesn't work if nbody[nsp + begmatch + olen - tem]
5003 is a padding glyph. */
5004 int out = olen - tem; /* Columns to be overwritten originally. */
5005 int del;
5f5c8ee5 5006
385ed61f 5007 cursor_to (f, vpos, nsp + begmatch);
177c0ea7 5008
5f5c8ee5 5009 /* Calculate columns we can actually overwrite. */
c363a1d6
GM
5010 while (CHAR_GLYPH_PADDING_P (nbody[nsp + begmatch + out]))
5011 out--;
385ed61f 5012 write_glyphs (f, nbody + nsp + begmatch, out);
177c0ea7 5013
5f5c8ee5
GM
5014 /* If we left columns to be overwritten, we must delete them. */
5015 del = olen - tem - out;
c363a1d6 5016 if (del > 0)
385ed61f 5017 delete_glyphs (f, del);
177c0ea7 5018
5f5c8ee5 5019 /* At last, we insert columns not yet written out. */
385ed61f 5020 insert_glyphs (f, nbody + nsp + begmatch + out, nlen - olen + del);
5f5c8ee5
GM
5021 olen = nlen;
5022 }
5023 else if (olen > nlen)
5024 {
385ed61f
KL
5025 cursor_to (f, vpos, nsp + begmatch);
5026 write_glyphs (f, nbody + nsp + begmatch, nlen - tem);
5027 delete_glyphs (f, olen - nlen);
5f5c8ee5
GM
5028 olen = nlen;
5029 }
bd9e3e75 5030 }
5f5c8ee5
GM
5031
5032 just_erase:
5033 /* If any unerased characters remain after the new line, erase them. */
5034 if (olen > nlen)
078b3696 5035 {
385ed61f
KL
5036 cursor_to (f, vpos, nlen);
5037 clear_end_of_line (f, olen);
078b3696 5038 }
5f5c8ee5
GM
5039
5040 /* Exchange contents between current_frame and new_frame. */
5041 make_current (desired_matrix, current_matrix, vpos);
078b3696 5042}
5f5c8ee5
GM
5043
5044
078b3696 5045\f
5f5c8ee5
GM
5046/***********************************************************************
5047 X/Y Position -> Buffer Position
5048 ***********************************************************************/
5049
a1b0a6db 5050/* Determine what's under window-relative pixel position (*X, *Y).
2396cbba 5051 Return the OBJECT (string or buffer) that's there.
6b61353c 5052 Return in *POS the position in that object.
2396cbba
EZ
5053 Adjust *X and *Y to character positions.
5054 Return in *DX and *DY the pixel coordinates of the click,
5055 relative to the top left corner of OBJECT, or relative to
5056 the top left corner of the character glyph at (*X, *Y)
5057 if OBJECT is nil.
5058 Return WIDTH and HEIGHT of the object at (*X, *Y), or zero
5059 if the coordinates point to an empty area of the display. */
5f5c8ee5 5060
6b61353c 5061Lisp_Object
971de7fb 5062buffer_posn_from_coords (struct window *w, int *x, int *y, struct display_pos *pos, Lisp_Object *object, int *dx, int *dy, int *width, int *height)
4588ec20 5063{
5f5c8ee5 5064 struct it it;
707ea971 5065 Lisp_Object old_current_buffer = Fcurrent_buffer ();
5f5c8ee5 5066 struct text_pos startp;
6b61353c
KH
5067 Lisp_Object string;
5068 struct glyph_row *row;
cedd7cd9
KS
5069#ifdef HAVE_WINDOW_SYSTEM
5070 struct image *img = 0;
5071#endif
2396cbba 5072 int x0, x1, to_x;
57b3e30b 5073 void *itdata = NULL;
5f5c8ee5 5074
707ea971
CY
5075 /* We used to set current_buffer directly here, but that does the
5076 wrong thing with `face-remapping-alist' (bug#2044). */
e74aeda8 5077 Fset_buffer (w->contents);
57b3e30b 5078 itdata = bidi_shelve_cache ();
cf91889a 5079 CLIP_TEXT_POS_FROM_MARKER (startp, w->start);
5f5c8ee5 5080 start_display (&it, w, startp);
352ec8ff
EZ
5081 /* start_display takes into account the header-line row, but IT's
5082 vpos still counts from the glyph row that includes the window's
5083 start position. Adjust for a possible header-line row. */
59ea14cd 5084 it.vpos += WINDOW_WANTS_HEADER_LINE_P (w);
177c0ea7 5085
9173a8fb 5086 x0 = *x;
2396cbba
EZ
5087
5088 /* First, move to the beginning of the row corresponding to *Y. We
5089 need to be in that row to get the correct value of base paragraph
40629f17 5090 direction for the text at (*X, *Y). */
2396cbba
EZ
5091 move_it_to (&it, -1, 0, *y, -1, MOVE_TO_X | MOVE_TO_Y);
5092
5093 /* TO_X is the pixel position that the iterator will compute for the
40629f17
EZ
5094 glyph at *X. We add it.first_visible_x because iterator
5095 positions include the hscroll. */
2396cbba
EZ
5096 to_x = x0 + it.first_visible_x;
5097 if (it.bidi_it.paragraph_dir == R2L)
5098 /* For lines in an R2L paragraph, we need to mirror TO_X wrt the
5099 text area. This is because the iterator, even in R2L
5100 paragraphs, delivers glyphs as if they started at the left
5101 margin of the window. (When we actually produce glyphs for
5102 display, we reverse their order in PRODUCE_GLYPHS, but the
5103 iterator doesn't know about that.) The following line adjusts
5104 the pixel position to the iterator geometry, which is what
40629f17
EZ
5105 move_it_* routines use. (The -1 is because in a window whose
5106 text-area width is W, the rightmost pixel position is W-1, and
5107 it should be mirrored into zero pixel position.) */
5108 to_x = window_box_width (w, TEXT_AREA) - to_x - 1;
5109
5110 /* Now move horizontally in the row to the glyph under *X. Second
5111 argument is ZV to prevent move_it_in_display_line from matching
5112 based on buffer positions. */
2396cbba 5113 move_it_in_display_line (&it, ZV, to_x, MOVE_TO_X);
35928349 5114 bidi_unshelve_cache (itdata, 0);
177c0ea7 5115
707ea971 5116 Fset_buffer (old_current_buffer);
a1b0a6db 5117
6b61353c
KH
5118 *dx = x0 + it.first_visible_x - it.current_x;
5119 *dy = *y - it.current_y;
5120
e74aeda8 5121 string = w->contents;
6b61353c
KH
5122 if (STRINGP (it.string))
5123 string = it.string;
a1b0a6db 5124 *pos = it.current;
86282aab
KH
5125 if (it.what == IT_COMPOSITION
5126 && it.cmp_it.nchars > 1
5127 && it.cmp_it.reversed_p)
5128 {
5129 /* The current display element is a grapheme cluster in a
5130 composition. In that case, we need the position of the first
5131 character of the cluster. But, as it.cmp_it.reversed_p is 1,
5132 it.current points to the last character of the cluster, thus
5133 we must move back to the first character of the same
5134 cluster. */
5135 CHARPOS (pos->pos) -= it.cmp_it.nchars - 1;
5136 if (STRINGP (it.string))
5137 BYTEPOS (pos->pos) = string_char_to_byte (string, CHARPOS (pos->pos));
5138 else
e74aeda8 5139 BYTEPOS (pos->pos) = buf_charpos_to_bytepos (XBUFFER (w->contents),
425cc014 5140 CHARPOS (pos->pos));
86282aab 5141 }
6b61353c
KH
5142
5143#ifdef HAVE_WINDOW_SYSTEM
5144 if (it.what == IT_IMAGE)
5145 {
6b61353c
KH
5146 if ((img = IMAGE_FROM_ID (it.f, it.image_id)) != NULL
5147 && !NILP (img->spec))
5148 *object = img->spec;
5149 }
5150#endif
5151
b70d98e1
KS
5152 if (it.vpos < w->current_matrix->nrows
5153 && (row = MATRIX_ROW (w->current_matrix, it.vpos),
5154 row->enabled_p))
6b61353c
KH
5155 {
5156 if (it.hpos < row->used[TEXT_AREA])
5157 {
5158 struct glyph *glyph = row->glyphs[TEXT_AREA] + it.hpos;
6b61353c 5159#ifdef HAVE_WINDOW_SYSTEM
cedd7cd9
KS
5160 if (img)
5161 {
5162 *dy -= row->ascent - glyph->ascent;
4be9765d
KH
5163 *dx += glyph->slice.img.x;
5164 *dy += glyph->slice.img.y;
cedd7cd9
KS
5165 /* Image slices positions are still relative to the entire image */
5166 *width = img->width;
5167 *height = img->height;
5168 }
5169 else
6b61353c 5170#endif
cedd7cd9
KS
5171 {
5172 *width = glyph->pixel_width;
5173 *height = glyph->ascent + glyph->descent;
5174 }
6b61353c
KH
5175 }
5176 else
5177 {
5178 *width = 0;
5179 *height = row->height;
5180 }
5181 }
5182 else
5183 {
5184 *width = *height = 0;
5185 }
5186
5187 /* Add extra (default width) columns if clicked after EOL. */
5e617bc2 5188 x1 = max (0, it.current_x + it.pixel_width - it.first_visible_x);
6b61353c
KH
5189 if (x0 > x1)
5190 it.hpos += (x0 - x1) / WINDOW_FRAME_COLUMN_WIDTH (w);
5191
5192 *x = it.hpos;
5193 *y = it.vpos;
5194
5195 return string;
5f5c8ee5 5196}
4588ec20 5197
5f5c8ee5
GM
5198
5199/* Value is the string under window-relative coordinates X/Y in the
6b61353c
KH
5200 mode line or header line (PART says which) of window W, or nil if none.
5201 *CHARPOS is set to the position in the string returned. */
5f5c8ee5
GM
5202
5203Lisp_Object
ace1712c 5204mode_line_string (struct window *w, enum window_part part,
d311d28c 5205 int *x, int *y, ptrdiff_t *charpos, Lisp_Object *object,
ace1712c 5206 int *dx, int *dy, int *width, int *height)
5f5c8ee5
GM
5207{
5208 struct glyph_row *row;
5209 struct glyph *glyph, *end;
6b61353c 5210 int x0, y0;
5f5c8ee5
GM
5211 Lisp_Object string = Qnil;
5212
641bdbf3 5213 if (part == ON_MODE_LINE)
5f5c8ee5
GM
5214 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
5215 else
045dee35 5216 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6b61353c
KH
5217 y0 = *y - row->y;
5218 *y = row - MATRIX_FIRST_TEXT_ROW (w->current_matrix);
5f5c8ee5
GM
5219
5220 if (row->mode_line_p && row->enabled_p)
4588ec20 5221 {
5f5c8ee5
GM
5222 /* Find the glyph under X. If we find one with a string object,
5223 it's the one we were looking for. */
5224 glyph = row->glyphs[TEXT_AREA];
5225 end = glyph + row->used[TEXT_AREA];
cfde341f 5226 for (x0 = *x; glyph < end && x0 >= glyph->pixel_width; ++glyph)
6b61353c
KH
5227 x0 -= glyph->pixel_width;
5228 *x = glyph - row->glyphs[TEXT_AREA];
5229 if (glyph < end)
5230 {
5231 string = glyph->object;
5232 *charpos = glyph->charpos;
5233 *width = glyph->pixel_width;
5234 *height = glyph->ascent + glyph->descent;
5235#ifdef HAVE_WINDOW_SYSTEM
5236 if (glyph->type == IMAGE_GLYPH)
5237 {
5238 struct image *img;
5239 img = IMAGE_FROM_ID (WINDOW_XFRAME (w), glyph->u.img_id);
5240 if (img != NULL)
5241 *object = img->spec;
5242 y0 -= row->ascent - glyph->ascent;
5243 }
5244#endif
5245 }
5246 else
5247 {
5248 /* Add extra (default width) columns if clicked after EOL. */
5249 *x += x0 / WINDOW_FRAME_COLUMN_WIDTH (w);
5250 *width = 0;
5251 *height = row->height;
5252 }
5253 }
5254 else
5255 {
5256 *x = 0;
5257 x0 = 0;
5258 *width = *height = 0;
4588ec20 5259 }
5f5c8ee5 5260
6b61353c
KH
5261 *dx = x0;
5262 *dy = y0;
5263
5f5c8ee5 5264 return string;
4588ec20 5265}
5f5c8ee5
GM
5266
5267
db1db309
GM
5268/* Value is the string under window-relative coordinates X/Y in either
5269 marginal area, or nil if none. *CHARPOS is set to the position in
5270 the string returned. */
5271
5272Lisp_Object
ace1712c 5273marginal_area_string (struct window *w, enum window_part part,
d311d28c 5274 int *x, int *y, ptrdiff_t *charpos, Lisp_Object *object,
ace1712c 5275 int *dx, int *dy, int *width, int *height)
db1db309
GM
5276{
5277 struct glyph_row *row = w->current_matrix->rows;
5278 struct glyph *glyph, *end;
6b61353c 5279 int x0, y0, i, wy = *y;
641bdbf3 5280 int area;
db1db309
GM
5281 Lisp_Object string = Qnil;
5282
641bdbf3 5283 if (part == ON_LEFT_MARGIN)
db1db309 5284 area = LEFT_MARGIN_AREA;
641bdbf3 5285 else if (part == ON_RIGHT_MARGIN)
db1db309
GM
5286 area = RIGHT_MARGIN_AREA;
5287 else
1088b922 5288 emacs_abort ();
db1db309
GM
5289
5290 for (i = 0; row->enabled_p && i < w->current_matrix->nrows; ++i, ++row)
5291 if (wy >= row->y && wy < MATRIX_ROW_BOTTOM_Y (row))
5292 break;
6b61353c
KH
5293 y0 = *y - row->y;
5294 *y = row - MATRIX_FIRST_TEXT_ROW (w->current_matrix);
db1db309
GM
5295
5296 if (row->enabled_p)
5297 {
5298 /* Find the glyph under X. If we find one with a string object,
5299 it's the one we were looking for. */
db1db309 5300 if (area == RIGHT_MARGIN_AREA)
7df02016 5301 x0 = ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
6b61353c 5302 ? WINDOW_LEFT_FRINGE_WIDTH (w)
880e6158 5303 : WINDOW_FRINGES_WIDTH (w))
7df02016
KS
5304 + window_box_width (w, LEFT_MARGIN_AREA)
5305 + window_box_width (w, TEXT_AREA));
db1db309 5306 else
7df02016 5307 x0 = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
6b61353c 5308 ? WINDOW_LEFT_FRINGE_WIDTH (w)
7df02016
KS
5309 : 0);
5310
6b61353c
KH
5311 glyph = row->glyphs[area];
5312 end = glyph + row->used[area];
cfde341f 5313 for (x0 = *x - x0; glyph < end && x0 >= glyph->pixel_width; ++glyph)
6b61353c
KH
5314 x0 -= glyph->pixel_width;
5315 *x = glyph - row->glyphs[area];
5316 if (glyph < end)
5317 {
5318 string = glyph->object;
5319 *charpos = glyph->charpos;
5320 *width = glyph->pixel_width;
5321 *height = glyph->ascent + glyph->descent;
5322#ifdef HAVE_WINDOW_SYSTEM
5323 if (glyph->type == IMAGE_GLYPH)
5324 {
5325 struct image *img;
5326 img = IMAGE_FROM_ID (WINDOW_XFRAME (w), glyph->u.img_id);
5327 if (img != NULL)
5328 *object = img->spec;
5329 y0 -= row->ascent - glyph->ascent;
4be9765d
KH
5330 x0 += glyph->slice.img.x;
5331 y0 += glyph->slice.img.y;
6b61353c
KH
5332 }
5333#endif
5334 }
5335 else
5336 {
5337 /* Add extra (default width) columns if clicked after EOL. */
5338 *x += x0 / WINDOW_FRAME_COLUMN_WIDTH (w);
5339 *width = 0;
5340 *height = row->height;
5341 }
db1db309 5342 }
6b61353c
KH
5343 else
5344 {
5345 x0 = 0;
5346 *x = 0;
5347 *width = *height = 0;
5348 }
5349
5350 *dx = x0;
5351 *dy = y0;
db1db309
GM
5352
5353 return string;
5354}
5355
5356
5f5c8ee5
GM
5357/***********************************************************************
5358 Changing Frame Sizes
5359 ***********************************************************************/
4588ec20
JB
5360
5361#ifdef SIGWINCH
5f5c8ee5 5362
20ef56db
PE
5363static void deliver_window_change_signal (int);
5364
9af30bdf 5365static void
20ef56db 5366handle_window_change_signal (int sig)
4588ec20
JB
5367{
5368 int width, height;
28d7d09f 5369 struct tty_display_info *tty;
4d553a13 5370
9628b887
KL
5371 /* The frame size change obviously applies to a single
5372 termcap-controlled terminal, but we can't decide which.
5373 Therefore, we resize the frames corresponding to each tty.
9628b887
KL
5374 */
5375 for (tty = tty_list; tty; tty = tty->next) {
4588ec20 5376
0a125897
KL
5377 if (! tty->term_initted)
5378 continue;
4588ec20 5379
db878925
DN
5380 /* Suspended tty frames have tty->input == NULL avoid trying to
5381 use it. */
5382 if (!tty->input)
5383 continue;
5384
0b0d3e0b 5385 get_tty_size (fileno (tty->input), &width, &height);
3fc809e2 5386
bedeffcf 5387 if (width > 5 && height > 2) {
9628b887 5388 Lisp_Object tail, frame;
3fc809e2 5389
9628b887 5390 FOR_EACH_FRAME (tail, frame)
4d553a13
KL
5391 if (FRAME_TERMCAP_P (XFRAME (frame)) && FRAME_TTY (XFRAME (frame)) == tty)
5392 /* Record the new sizes, but don't reallocate the data
5393 structures now. Let that be done later outside of the
5394 signal handler. */
880e6158 5395 change_frame_size (XFRAME (frame), width, height, 0, 1, 0, 0);
9628b887 5396 }
4588ec20 5397 }
20ef56db 5398}
3fc809e2 5399
20ef56db
PE
5400static void
5401deliver_window_change_signal (int sig)
5402{
4d7e6e51 5403 deliver_process_signal (sig, handle_window_change_signal);
4588ec20
JB
5404}
5405#endif /* SIGWINCH */
5406
5407
59ea14cd
PE
5408/* Do any change in frame size that was requested by a signal.
5409 SAFE means this function is called from a place where it is
5410 safe to change frame sizes while a redisplay is in progress. */
4588ec20 5411
dfcf069d 5412void
59ea14cd 5413do_pending_window_change (bool safe)
4588ec20 5414{
20ef56db 5415 /* If window change signal handler should have run before, run it now. */
b96fd3e8
GM
5416 if (redisplaying_p && !safe)
5417 return;
177c0ea7 5418
4588ec20
JB
5419 while (delayed_size_change)
5420 {
35f56f96 5421 Lisp_Object tail, frame;
4588ec20
JB
5422
5423 delayed_size_change = 0;
5424
35f56f96 5425 FOR_EACH_FRAME (tail, frame)
4588ec20 5426 {
5f5c8ee5 5427 struct frame *f = XFRAME (frame);
35f56f96 5428
880e6158
MR
5429 if (f->new_height != 0 || f->new_width != 0)
5430 change_frame_size (f, f->new_width, f->new_height,
5431 0, 0, safe, f->new_pixelwise);
4588ec20
JB
5432 }
5433 }
5434}
5435
502b9b64 5436/* Change the frame height and/or width. Values may be given as zero to
177c0ea7 5437 indicate no change is to take place.
4588ec20 5438
880e6158
MR
5439 new_height and new_width refer to the text portion of the frame. It
5440 doesn't matter for new_height, since text and total portion are the
5441 same in that case. But new_width must be enlarged to get the total
5442 width of the frame.
5443
59ea14cd
PE
5444 If DELAY, assume we're being called from a signal handler, and
5445 queue the change for later - perhaps the next redisplay.
5446 Since this tries to resize windows, we can't call it
b96fd3e8
GM
5447 from a signal handler.
5448
59ea14cd 5449 SAFE means this function is called from a place where it's
b96fd3e8 5450 safe to change frame sizes while a redisplay is in progress. */
b6a65ac2 5451
dfcf069d 5452void
880e6158
MR
5453change_frame_size (struct frame *f, int new_width, int new_height,
5454 bool pretend, bool delay, bool safe, bool pixelwise)
45140e01
RS
5455{
5456 Lisp_Object tail, frame;
3826ea1a 5457
9628b887 5458 if (FRAME_MSDOS_P (f))
45140e01 5459 {
9628b887
KL
5460 /* On MS-DOS, all frames use the same screen, so a change in
5461 size affects all frames. Termcap now supports multiple
5462 ttys. */
45140e01 5463 FOR_EACH_FRAME (tail, frame)
8a376b3b 5464 if (! FRAME_WINDOW_P (XFRAME (frame)))
880e6158
MR
5465 change_frame_size_1 (XFRAME (frame), new_width, new_height,
5466 pretend, delay, safe, pixelwise);
45140e01
RS
5467 }
5468 else
880e6158
MR
5469 change_frame_size_1 (f, new_width, new_height, pretend, delay, safe,
5470 pixelwise);
45140e01
RS
5471}
5472
5473static void
880e6158
MR
5474change_frame_size_1 (struct frame *f, int new_width, int new_height,
5475 bool pretend, bool delay, bool safe, bool pixelwise)
4588ec20 5476{
880e6158 5477 int new_text_width, new_text_height, new_root_width;
564eb533 5478 int old_root_width = WINDOW_PIXEL_WIDTH (XWINDOW (FRAME_ROOT_WINDOW (f)));
880e6158 5479 int new_cols, new_lines;
2bfa3d3e 5480 dynwind_begin ();
3826ea1a 5481
4588ec20 5482 /* If we can't deal with the change now, queue it for later. */
b96fd3e8 5483 if (delay || (redisplaying_p && !safe))
4588ec20 5484 {
880e6158
MR
5485 f->new_width = new_width;
5486 f->new_height = new_height;
5487 f->new_pixelwise = pixelwise;
4588ec20 5488 delayed_size_change = 1;
2bfa3d3e 5489 dynwind_end ();
4588ec20
JB
5490 return;
5491 }
5492
502b9b64 5493 /* This size-change overrides any pending one for this frame. */
880e6158
MR
5494 f->new_height = 0;
5495 f->new_width = 0;
5496 f->new_pixelwise = 0;
b6a65ac2 5497
08f7aa3e 5498 /* If an argument is zero, set it to the current value. */
880e6158
MR
5499 if (pixelwise)
5500 {
5501 new_text_width = (new_width == 0) ? FRAME_TEXT_WIDTH (f) : new_width;
5502 new_text_height = (new_height == 0) ? FRAME_TEXT_HEIGHT (f) : new_height;
c44de18d
MR
5503 /* Consider rounding here: Currently, the root window can be
5504 larger than the frame in terms of columns/lines. */
880e6158
MR
5505 new_cols = new_text_width / FRAME_COLUMN_WIDTH (f);
5506 new_lines = new_text_height / FRAME_LINE_HEIGHT (f);
5507 }
5508 else
5509 {
5510 new_cols = (new_width == 0) ? FRAME_COLS (f) : new_width;
5511 new_lines = (new_height == 0) ? FRAME_LINES (f) : new_height;
5512 new_text_width = new_cols * FRAME_COLUMN_WIDTH (f);
5513 new_text_height = new_lines * FRAME_LINE_HEIGHT (f);
5514 }
3826ea1a 5515
3b95a6f9 5516 /* Compute width of windows in F. */
b6a65ac2 5517 /* Round up to the smallest acceptable size. */
880e6158
MR
5518 check_frame_size (f, &new_text_width, &new_text_height, 1);
5519
5520 /* This is the width of the frame without vertical scroll bars and
5521 fringe columns. Do this after rounding - see discussion of
5522 bug#9723. */
5523 new_root_width = (new_text_width
880e6158
MR
5524 + FRAME_SCROLL_BAR_AREA_WIDTH (f)
5525 + FRAME_TOTAL_FRINGE_WIDTH (f));
b6a65ac2 5526 /* If we're not changing the frame size, quit now. */
3b95a6f9
MR
5527 /* Frame width may be unchanged but the text portion may change, for
5528 example, fullscreen and remove/add scroll bar. */
880e6158
MR
5529 if (new_text_height == FRAME_TEXT_HEIGHT (f)
5530 && new_text_width == FRAME_TEXT_WIDTH (f)
7d7ccb88
MR
5531 && new_root_width == old_root_width
5532 && (FRAME_PIXEL_HEIGHT (f) ==
5533 FRAME_TEXT_TO_PIXEL_HEIGHT (f, new_text_height))
5534 && (FRAME_PIXEL_WIDTH (f) ==
5535 FRAME_TEXT_TO_PIXEL_WIDTH (f, new_text_width)))
2bfa3d3e
BT
5536 {
5537 dynwind_end ();
5538 return;
5539 }
4588ec20 5540
4d7e6e51 5541 block_input ();
cbb95688 5542
886a8a6c
KH
5543#ifdef MSDOS
5544 /* We only can set screen dimensions to certain values supported
5545 by our video hardware. Try to find the smallest size greater
5546 or equal to the requested dimensions. */
880e6158 5547 dos_set_window_size (&new_lines, &new_cols);
886a8a6c
KH
5548#endif
5549
880e6158 5550 if (new_text_height != FRAME_TEXT_HEIGHT (f))
4588ec20 5551 {
880e6158 5552 resize_frame_windows (f, new_text_height, 0, 1);
b6a65ac2 5553
48e482e6
EZ
5554 /* MSDOS frames cannot PRETEND, as they change frame size by
5555 manipulating video hardware. */
5556 if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
e1a2cb1c 5557 FrameRows (FRAME_TTY (f)) = new_lines;
4588ec20
JB
5558 }
5559
880e6158
MR
5560 if (new_text_width != FRAME_TEXT_WIDTH (f)
5561 || new_root_width != old_root_width)
4588ec20 5562 {
880e6158 5563 resize_frame_windows (f, new_root_width, 1, 1);
4588ec20 5564
48e482e6
EZ
5565 /* MSDOS frames cannot PRETEND, as they change frame size by
5566 manipulating video hardware. */
5567 if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
880e6158 5568 FrameCols (FRAME_TTY (f)) = new_cols;
5f5c8ee5 5569
742516e0 5570#if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
e69b0960 5571 if (WINDOWP (f->tool_bar_window))
880e6158
MR
5572 {
5573 XWINDOW (f->tool_bar_window)->total_cols = new_cols;
5574 XWINDOW (f->tool_bar_window)->pixel_width = new_root_width;
5575 }
742516e0 5576#endif
4588ec20
JB
5577 }
5578
880e6158
MR
5579 SET_FRAME_COLS (f, new_cols);
5580 FRAME_LINES (f) = new_lines;
5581 FRAME_TEXT_WIDTH (f) = new_text_width;
5582 FRAME_TEXT_HEIGHT (f) = new_text_height;
5583 FRAME_PIXEL_WIDTH (f) = FRAME_TEXT_TO_PIXEL_WIDTH (f, new_text_width);
5584 FRAME_PIXEL_HEIGHT (f) = FRAME_TEXT_TO_PIXEL_HEIGHT (f, new_text_height);
986e61b8 5585
5f5c8ee5
GM
5586 {
5587 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
5588 int text_area_x, text_area_y, text_area_width, text_area_height;
177c0ea7 5589
5f5c8ee5
GM
5590 window_box (w, TEXT_AREA, &text_area_x, &text_area_y, &text_area_width,
5591 &text_area_height);
5592 if (w->cursor.x >= text_area_x + text_area_width)
5593 w->cursor.hpos = w->cursor.x = 0;
5594 if (w->cursor.y >= text_area_y + text_area_height)
5595 w->cursor.vpos = w->cursor.y = 0;
5596 }
986e61b8 5597
bd0443bb 5598 adjust_frame_glyphs (f);
5f5c8ee5 5599 calculate_costs (f);
cb02fa39
GM
5600 SET_FRAME_GARBAGED (f);
5601 f->resized_p = 1;
97cf50e7 5602
4d7e6e51 5603 unblock_input ();
61730a69 5604
66322887 5605 record_unwind_current_buffer ();
e523f7e5 5606
ef264c42 5607 run_window_configuration_change_hook (f);
e523f7e5 5608
2bfa3d3e 5609 dynwind_end ();
4588ec20
JB
5610}
5611\f
5f5c8ee5
GM
5612/***********************************************************************
5613 Terminal Related Lisp Functions
5614 ***********************************************************************/
5615
5616DEFUN ("open-termscript", Fopen_termscript, Sopen_termscript,
228299fa 5617 1, 1, "FOpen termscript file: ",
7ee72033
MB
5618 doc: /* Start writing all terminal output to FILE as well as the terminal.
5619FILE = nil means just close any termscript file currently open. */)
5842a27b 5620 (Lisp_Object file)
5f5c8ee5 5621{
0b0d3e0b
KL
5622 struct tty_display_info *tty;
5623
7d2f1216
EZ
5624 if (! FRAME_TERMCAP_P (SELECTED_FRAME ())
5625 && ! FRAME_MSDOS_P (SELECTED_FRAME ()))
0b0d3e0b
KL
5626 error ("Current frame is not on a tty device");
5627
5628 tty = CURTTY ();
5629
5630 if (tty->termscript != 0)
406af475
PE
5631 {
5632 block_input ();
5633 fclose (tty->termscript);
5634 tty->termscript = 0;
5635 unblock_input ();
5636 }
5f5c8ee5
GM
5637
5638 if (! NILP (file))
5639 {
5640 file = Fexpand_file_name (file, Qnil);
406af475 5641 tty->termscript = emacs_fopen (SSDATA (file), "w");
0b0d3e0b 5642 if (tty->termscript == 0)
a9757f6a 5643 report_file_error ("Opening termscript", file);
5f5c8ee5
GM
5644 }
5645 return Qnil;
5646}
5647
5648
4588ec20 5649DEFUN ("send-string-to-terminal", Fsend_string_to_terminal,
62af879c 5650 Ssend_string_to_terminal, 1, 2, 0,
7ee72033 5651 doc: /* Send STRING to the terminal without alteration.
62af879c
KL
5652Control characters in STRING will have terminal-dependent effects.
5653
6ed8eeff 5654Optional parameter TERMINAL specifies the tty terminal device to use.
558759e4
AM
5655It may be a terminal object, a frame, or nil for the terminal used by
5656the currently selected frame. In batch mode, STRING is sent to stdout
5657when TERMINAL is nil. */)
5842a27b 5658 (Lisp_Object string, Lisp_Object terminal)
4588ec20 5659{
7cef7ce3
SM
5660 struct terminal *t = get_terminal (terminal, 1);
5661 FILE *out;
0b0d3e0b 5662
94f3db62 5663 /* ??? Perhaps we should do something special for multibyte strings here. */
b7826503 5664 CHECK_STRING (string);
4d7e6e51 5665 block_input ();
0b0d3e0b 5666
6ed8eeff
KL
5667 if (!t)
5668 error ("Unknown terminal device");
0b0d3e0b 5669
7cef7ce3
SM
5670 if (t->type == output_initial)
5671 out = stdout;
5672 else if (t->type != output_termcap && t->type != output_msdos_raw)
5673 error ("Device %d is not a termcap terminal device", t->id);
5674 else
5675 {
5676 struct tty_display_info *tty = t->display_info.tty;
3fc809e2 5677
7cef7ce3
SM
5678 if (! tty->output)
5679 error ("Terminal is currently suspended");
cecfdea6 5680
7cef7ce3
SM
5681 if (tty->termscript)
5682 {
5683 fwrite (SDATA (string), 1, SBYTES (string), tty->termscript);
5684 fflush (tty->termscript);
5685 }
5686 out = tty->output;
4588ec20 5687 }
7cef7ce3
SM
5688 fwrite (SDATA (string), 1, SBYTES (string), out);
5689 fflush (out);
4d7e6e51 5690 unblock_input ();
4588ec20
JB
5691 return Qnil;
5692}
5693
5f5c8ee5 5694
a7ca3326 5695DEFUN ("ding", Fding, Sding, 0, 1, 0,
7ee72033 5696 doc: /* Beep, or flash the screen.
228299fa 5697Also, unless an argument is given,
7ee72033 5698terminate any keyboard macro currently executing. */)
5842a27b 5699 (Lisp_Object arg)
4588ec20 5700{
efb859b4 5701 if (!NILP (arg))
4588ec20 5702 {
7fa788da
RS
5703 if (noninteractive)
5704 putchar (07);
5705 else
385ed61f 5706 ring_bell (XFRAME (selected_frame));
4588ec20
JB
5707 }
5708 else
5709 bitch_at_user ();
5710
5711 return Qnil;
5712}
5713
dfcf069d 5714void
971de7fb 5715bitch_at_user (void)
4588ec20
JB
5716{
5717 if (noninteractive)
5718 putchar (07);
5f5c8ee5 5719 else if (!INTERACTIVE) /* Stop executing a keyboard macro. */
81519fd2
SM
5720 {
5721 const char *msg
5722 = "Keyboard macro terminated by a command ringing the bell";
6c6f1994 5723 Fsignal (Quser_error, list1 (build_string (msg)));
81519fd2 5724 }
4588ec20 5725 else
385ed61f 5726 ring_bell (XFRAME (selected_frame));
4588ec20
JB
5727}
5728
5f5c8ee5
GM
5729
5730\f
5731/***********************************************************************
5732 Sleeping, Waiting
5733 ***********************************************************************/
5734
a7ca3326 5735DEFUN ("sleep-for", Fsleep_for, Ssleep_for, 1, 2, 0,
7ee72033 5736 doc: /* Pause, without updating display, for SECONDS seconds.
228299fa
GM
5737SECONDS may be a floating-point value, meaning that you can wait for a
5738fraction of a second. Optional second arg MILLISECONDS specifies an
d35af63c 5739additional wait period, in milliseconds; this is for backwards compatibility.
7ee72033 5740\(Not all operating systems support waiting for a fraction of a second.) */)
5842a27b 5741 (Lisp_Object seconds, Lisp_Object milliseconds)
4588ec20 5742{
d311d28c 5743 double duration = extract_float (seconds);
4588ec20 5744
d311d28c
PE
5745 if (!NILP (milliseconds))
5746 {
5747 CHECK_NUMBER (milliseconds);
5748 duration += XINT (milliseconds) / 1000.0;
5749 }
b07646f5 5750
908589fd 5751 if (duration > 0)
d35af63c 5752 {
43aac990
PE
5753 struct timespec t = dtotimespec (duration);
5754 wait_reading_process_output (min (t.tv_sec, WAIT_READING_MAX),
5755 t.tv_nsec, 0, 0, Qnil, NULL, 0);
d35af63c 5756 }
4588ec20
JB
5757
5758 return Qnil;
5759}
5760
5f5c8ee5 5761
dcd1ca45 5762/* This is just like wait_reading_process_output, except that
902b9b6d 5763 it does redisplay.
f76475ad 5764
2bcac766
CY
5765 TIMEOUT is number of seconds to wait (float or integer),
5766 or t to wait forever.
59ea14cd 5767 READING is true if reading input.
9fa1de30
PE
5768 If DISPLAY_OPTION is >0 display process output while waiting.
5769 If DISPLAY_OPTION is >1 perform an initial redisplay before waiting.
902b9b6d 5770*/
4588ec20 5771
f76475ad 5772Lisp_Object
9fa1de30 5773sit_for (Lisp_Object timeout, bool reading, int display_option)
f76475ad 5774{
d35af63c
PE
5775 intmax_t sec;
5776 int nsec;
9fa1de30 5777 bool do_display = display_option > 0;
f222369e 5778
902b9b6d 5779 swallow_events (do_display);
ccddf474 5780
902b9b6d 5781 if ((detect_input_pending_run_timers (do_display))
29bcbe54 5782 || !NILP (Vexecuting_kbd_macro))
4588ec20 5783 return Qnil;
4588ec20 5784
9fa1de30 5785 if (display_option > 1)
902b9b6d
KS
5786 redisplay_preserve_echo_area (2);
5787
d35af63c 5788 if (INTEGERP (timeout))
2bcac766 5789 {
d35af63c 5790 sec = XINT (timeout);
908589fd 5791 if (sec <= 0)
d35af63c
PE
5792 return Qt;
5793 nsec = 0;
2bcac766 5794 }
d35af63c 5795 else if (FLOATP (timeout))
d311d28c 5796 {
d35af63c 5797 double seconds = XFLOAT_DATA (timeout);
7216e43b 5798 if (! (0 < seconds))
d311d28c 5799 return Qt;
d35af63c
PE
5800 else
5801 {
43aac990
PE
5802 struct timespec t = dtotimespec (seconds);
5803 sec = min (t.tv_sec, WAIT_READING_MAX);
5804 nsec = t.tv_nsec;
d35af63c
PE
5805 }
5806 }
5807 else if (EQ (timeout, Qt))
5808 {
5809 sec = 0;
5810 nsec = 0;
d311d28c 5811 }
d35af63c
PE
5812 else
5813 wrong_type_argument (Qnumberp, timeout);
5814
dfdb645c 5815
4a4bbad2 5816#ifdef USABLE_SIGIO
f75beb47 5817 gobble_input ();
f76475ad
JB
5818#endif
5819
d35af63c 5820 wait_reading_process_output (sec, nsec, reading ? -1 : 1, do_display,
dcd1ca45 5821 Qnil, NULL, 0);
f76475ad 5822
4588ec20
JB
5823 return detect_input_pending () ? Qnil : Qt;
5824}
5825
5f5c8ee5 5826
a7ca3326 5827DEFUN ("redisplay", Fredisplay, Sredisplay, 0, 1, 0,
025de85b
CY
5828 doc: /* Perform redisplay.
5829Optional arg FORCE, if non-nil, prevents redisplay from being
5830preempted by arriving input, even if `redisplay-dont-pause' is nil.
5831If `redisplay-dont-pause' is non-nil (the default), redisplay is never
5832preempted by arriving input, so FORCE does nothing.
5833
5834Return t if redisplay was performed, nil if redisplay was preempted
5835immediately by pending input. */)
5842a27b 5836 (Lisp_Object force)
f76475ad 5837{
d311d28c 5838 ptrdiff_t count;
659d9874 5839
902b9b6d
KS
5840 swallow_events (1);
5841 if ((detect_input_pending_run_timers (1)
01327edf 5842 && NILP (force) && !redisplay_dont_pause)
7ba07a1a
CY
5843 || !NILP (Vexecuting_kbd_macro))
5844 return Qnil;
f76475ad 5845
2bfa3d3e 5846 dynwind_begin ();
01327edf
KS
5847 if (!NILP (force) && !redisplay_dont_pause)
5848 specbind (Qredisplay_dont_pause, Qt);
7ba07a1a 5849 redisplay_preserve_echo_area (2);
2bfa3d3e 5850 dynwind_end ();
7ba07a1a 5851 return Qt;
f76475ad 5852}
5f5c8ee5
GM
5853
5854
5855\f
5856/***********************************************************************
5857 Other Lisp Functions
5858 ***********************************************************************/
5859
5860/* A vector of size >= 2 * NFRAMES + 3 * NBUFFERS + 1, containing the
5861 session's frames, frame names, buffers, buffer-read-only flags, and
44edd521 5862 buffer-modified-flags. */
5f5c8ee5
GM
5863
5864static Lisp_Object frame_and_buffer_state;
5865
5866
5867DEFUN ("frame-or-buffer-changed-p", Fframe_or_buffer_changed_p,
44edd521 5868 Sframe_or_buffer_changed_p, 0, 1, 0,
bacf09b4 5869 doc: /* Return non-nil if the frame and buffer state appears to have changed.
44edd521
RS
5870VARIABLE is a variable name whose value is either nil or a state vector
5871that will be updated to contain all frames and buffers,
228299fa 5872aside from buffers whose names start with space,
44edd521
RS
5873along with the buffers' read-only and modified flags. This allows a fast
5874check to see whether buffer menus might need to be recomputed.
228299fa 5875If this function returns non-nil, it updates the internal vector to reflect
44edd521
RS
5876the current state.
5877
5878If VARIABLE is nil, an internal variable is used. Users should not
5879pass nil for VARIABLE. */)
5842a27b 5880 (Lisp_Object variable)
5f5c8ee5 5881{
44edd521 5882 Lisp_Object state, tail, frame, buf;
086ca913 5883 ptrdiff_t n, idx;
5f5c8ee5 5884
44edd521
RS
5885 if (! NILP (variable))
5886 {
5887 CHECK_SYMBOL (variable);
5888 state = Fsymbol_value (variable);
5889 if (! VECTORP (state))
5890 goto changed;
5891 }
5892 else
5893 state = frame_and_buffer_state;
5894
086ca913 5895 idx = 0;
5f5c8ee5
GM
5896 FOR_EACH_FRAME (tail, frame)
5897 {
086ca913 5898 if (idx == ASIZE (state))
44edd521 5899 goto changed;
086ca913 5900 if (!EQ (AREF (state, idx++), frame))
5f5c8ee5 5901 goto changed;
086ca913 5902 if (idx == ASIZE (state))
44edd521 5903 goto changed;
086ca913 5904 if (!EQ (AREF (state, idx++), XFRAME (frame)->name))
5f5c8ee5
GM
5905 goto changed;
5906 }
44edd521 5907 /* Check that the buffer info matches. */
8f3a2c26 5908 FOR_EACH_LIVE_BUFFER (tail, buf)
5f5c8ee5 5909 {
5f5c8ee5 5910 /* Ignore buffers that aren't included in buffer lists. */
4b4deea2 5911 if (SREF (BVAR (XBUFFER (buf), name), 0) == ' ')
5f5c8ee5 5912 continue;
086ca913 5913 if (idx == ASIZE (state))
44edd521 5914 goto changed;
086ca913 5915 if (!EQ (AREF (state, idx++), buf))
5f5c8ee5 5916 goto changed;
086ca913 5917 if (idx == ASIZE (state))
44edd521 5918 goto changed;
086ca913 5919 if (!EQ (AREF (state, idx++), BVAR (XBUFFER (buf), read_only)))
5f5c8ee5 5920 goto changed;
086ca913 5921 if (idx == ASIZE (state))
44edd521 5922 goto changed;
086ca913 5923 if (!EQ (AREF (state, idx++), Fbuffer_modified_p (buf)))
5f5c8ee5
GM
5924 goto changed;
5925 }
086ca913 5926 if (idx == ASIZE (state))
44edd521 5927 goto changed;
5f5c8ee5 5928 /* Detect deletion of a buffer at the end of the list. */
086ca913 5929 if (EQ (AREF (state, idx), Qlambda))
5f5c8ee5 5930 return Qnil;
44edd521
RS
5931
5932 /* Come here if we decide the data has changed. */
5f5c8ee5 5933 changed:
44edd521
RS
5934 /* Count the size we will need.
5935 Start with 1 so there is room for at least one lambda at the end. */
5f5c8ee5
GM
5936 n = 1;
5937 FOR_EACH_FRAME (tail, frame)
5938 n += 2;
8f3a2c26 5939 FOR_EACH_LIVE_BUFFER (tail, buf)
5f5c8ee5 5940 n += 3;
44edd521
RS
5941 /* Reallocate the vector if data has grown to need it,
5942 or if it has shrunk a lot. */
5943 if (! VECTORP (state)
77b37c05
PE
5944 || n > ASIZE (state)
5945 || n + 20 < ASIZE (state) / 2)
5f5c8ee5 5946 /* Add 20 extra so we grow it less often. */
44edd521
RS
5947 {
5948 state = Fmake_vector (make_number (n + 20), Qlambda);
5949 if (! NILP (variable))
5950 Fset (variable, state);
5951 else
5952 frame_and_buffer_state = state;
5953 }
5954
5955 /* Record the new data in the (possibly reallocated) vector. */
086ca913 5956 idx = 0;
5f5c8ee5
GM
5957 FOR_EACH_FRAME (tail, frame)
5958 {
086ca913
DA
5959 ASET (state, idx, frame);
5960 idx++;
5961 ASET (state, idx, XFRAME (frame)->name);
5962 idx++;
5f5c8ee5 5963 }
8f3a2c26 5964 FOR_EACH_LIVE_BUFFER (tail, buf)
5f5c8ee5 5965 {
5f5c8ee5 5966 /* Ignore buffers that aren't included in buffer lists. */
4b4deea2 5967 if (SREF (BVAR (XBUFFER (buf), name), 0) == ' ')
5f5c8ee5 5968 continue;
086ca913
DA
5969 ASET (state, idx, buf);
5970 idx++;
5971 ASET (state, idx, BVAR (XBUFFER (buf), read_only));
5972 idx++;
5973 ASET (state, idx, Fbuffer_modified_p (buf));
5974 idx++;
5f5c8ee5
GM
5975 }
5976 /* Fill up the vector with lambdas (always at least one). */
086ca913
DA
5977 ASET (state, idx, Qlambda);
5978 idx++;
5979 while (idx < ASIZE (state))
5980 {
5981 ASET (state, idx, Qlambda);
5982 idx++;
5983 }
5f5c8ee5 5984 /* Make sure we didn't overflow the vector. */
086ca913 5985 eassert (idx <= ASIZE (state));
5f5c8ee5
GM
5986 return Qt;
5987}
5988
5989
4588ec20 5990\f
5f5c8ee5
GM
5991/***********************************************************************
5992 Initialization
5993***********************************************************************/
5994
5f5c8ee5
GM
5995/* Initialization done when Emacs fork is started, before doing stty.
5996 Determine terminal type and set terminal_driver. Then invoke its
5997 decoding routine to set up variables in the terminal package. */
4588ec20 5998
dfcf069d 5999void
971de7fb 6000init_display (void)
4588ec20 6001{
28d440ab
KL
6002 char *terminal_type;
6003
5f5c8ee5
GM
6004 /* Construct the space glyph. */
6005 space_glyph.type = CHAR_GLYPH;
3fc809e2 6006 SET_CHAR_GLYPH (space_glyph, ' ', DEFAULT_FACE_ID, 0);
5f5c8ee5
GM
6007 space_glyph.charpos = -1;
6008
4588ec20
JB
6009 inverse_video = 0;
6010 cursor_in_echo_area = 0;
4588ec20 6011
1315c181
JB
6012 /* Now is the time to initialize this; it's used by init_sys_modes
6013 during startup. */
2246281f 6014 Vinitial_window_system = Qnil;
4588ec20 6015
4181ad9c
KL
6016 /* SIGWINCH needs to be handled no matter what display we start
6017 with. Otherwise newly opened tty frames will not resize
6018 automatically. */
6019#ifdef SIGWINCH
6020#ifndef CANNOT_DUMP
6021 if (initialized)
6022#endif /* CANNOT_DUMP */
2fe28299
PE
6023 {
6024 struct sigaction action;
6025 emacs_sigaction_init (&action, deliver_window_change_signal);
6026 sigaction (SIGWINCH, &action, 0);
6027 }
4181ad9c 6028#endif /* SIGWINCH */
4588ec20 6029
d6ef42b9 6030 /* If running as a daemon, no need to initialize any frames/terminal. */
ff808935
DN
6031 if (IS_DAEMON)
6032 return;
d6ef42b9 6033
1315c181
JB
6034 /* If the user wants to use a window system, we shouldn't bother
6035 initializing the terminal. This is especially important when the
6036 terminal is so dumb that emacs gives up before and doesn't bother
6037 using the window system.
4588ec20 6038
36bbad1d
KH
6039 If the DISPLAY environment variable is set and nonempty,
6040 try to use X, and die with an error message if that doesn't work. */
4588ec20
JB
6041
6042#ifdef HAVE_X_WINDOWS
00d70d57 6043 if (! inhibit_window_system && ! display_arg)
d460af17 6044 {
36bbad1d 6045 char *display;
36bbad1d 6046 display = getenv ("DISPLAY");
36bbad1d 6047 display_arg = (display != 0 && *display != 0);
c1fc674b
AS
6048
6049 if (display_arg && !x_display_ok (display))
6050 {
6051 fprintf (stderr, "Display %s unavailable, simulating -nw\n",
6052 display);
6053 inhibit_window_system = 1;
6054 }
f040093a 6055 }
d460af17 6056
4fcc2638 6057 if (!inhibit_window_system && display_arg)
4588ec20 6058 {
111d9af3 6059 Vinitial_window_system = Qx;
4588ec20
JB
6060#ifdef HAVE_X11
6061 Vwindow_system_version = make_number (11);
039e5d71 6062#endif
a29c3e6d 6063#ifdef USE_NCURSES
039e5d71 6064 /* In some versions of ncurses,
6a428f77 6065 tputs crashes if we have not called tgetent.
039e5d71
KH
6066 So call tgetent. */
6067 { char b[2044]; tgetent (b, "xterm");}
4588ec20
JB
6068#endif
6069 return;
6070 }
6071#endif /* HAVE_X_WINDOWS */
6072
fd2e066a 6073#ifdef HAVE_NTGUI
177c0ea7 6074 if (!inhibit_window_system)
fd2e066a 6075 {
111d9af3 6076 Vinitial_window_system = Qw32;
fd2e066a
GV
6077 Vwindow_system_version = make_number (1);
6078 return;
6079 }
6080#endif /* HAVE_NTGUI */
6081
edfda783
AR
6082#ifdef HAVE_NS
6083 if (!inhibit_window_system
6084#ifndef CANNOT_DUMP
6085 && initialized
6086#endif
6087 )
6088 {
111d9af3 6089 Vinitial_window_system = Qns;
5e617bc2 6090 Vwindow_system_version = make_number (10);
edfda783
AR
6091 return;
6092 }
6093#endif
6094
4588ec20
JB
6095 /* If no window system has been specified, try to use the terminal. */
6096 if (! isatty (0))
d9df6f40 6097 fatal ("standard input is not a tty");
4588ec20 6098
6152361f
JR
6099#ifdef WINDOWSNT
6100 terminal_type = "w32console";
6101#else
83be8524 6102 terminal_type = getenv ("TERM");
6152361f 6103#endif
4588ec20
JB
6104 if (!terminal_type)
6105 {
5d12ff0b 6106#ifdef HAVE_WINDOW_SYSTEM
b9e590fc
RS
6107 if (! inhibit_window_system)
6108 fprintf (stderr, "Please set the environment variable DISPLAY or TERM (see `tset').\n");
5d12ff0b
RS
6109 else
6110#endif /* HAVE_WINDOW_SYSTEM */
b9e590fc 6111 fprintf (stderr, "Please set the environment variable TERM; see `tset'.\n");
4588ec20
JB
6112 exit (1);
6113 }
6114
0a125897 6115 {
6ed8eeff 6116 struct terminal *t;
3224dac1
KL
6117 struct frame *f = XFRAME (selected_frame);
6118
322aea6d
PE
6119 init_foreground_group ();
6120
3224dac1 6121 /* Open a display on the controlling tty. */
6ed8eeff 6122 t = init_tty (0, terminal_type, 1); /* Errors are fatal. */
3224dac1
KL
6123
6124 /* Convert the initial frame to use the new display. */
b04f5ad2 6125 if (f->output_method != output_initial)
1088b922 6126 emacs_abort ();
6ed8eeff
KL
6127 f->output_method = t->type;
6128 f->terminal = t;
3224dac1 6129
6ed8eeff 6130 t->reference_count++;
7ac5dac9
EZ
6131#ifdef MSDOS
6132 f->output_data.tty->display_info = &the_only_display_info;
6133#else
6134 if (f->output_method == output_termcap)
6135 create_tty_output (f);
6136#endif
6ed8eeff 6137 t->display_info.tty->top_frame = selected_frame;
5b65d888 6138 change_frame_size (XFRAME (selected_frame),
880e6158
MR
6139 FrameCols (t->display_info.tty),
6140 FrameRows (t->display_info.tty), 0, 0, 1, 0);
3224dac1 6141
6ed8eeff
KL
6142 /* Delete the initial terminal. */
6143 if (--initial_terminal->reference_count == 0
6144 && initial_terminal->delete_terminal_hook)
6145 (*initial_terminal->delete_terminal_hook) (initial_terminal);
2666355c
KL
6146
6147 /* Update frame parameters to reflect the new type. */
6148 Fmodify_frame_parameters
6c6f1994
PE
6149 (selected_frame, list1 (Fcons (Qtty_type,
6150 Ftty_type (selected_frame))));
657ceaa0 6151 if (t->display_info.tty->name)
6c6f1994
PE
6152 Fmodify_frame_parameters
6153 (selected_frame,
6154 list1 (Fcons (Qtty, build_string (t->display_info.tty->name))));
657ceaa0 6155 else
6c6f1994 6156 Fmodify_frame_parameters (selected_frame, list1 (Fcons (Qtty, Qnil)));
0a125897 6157 }
3fc809e2 6158
d86c299a 6159 {
91fb7e1b 6160 struct frame *sf = SELECTED_FRAME ();
7df02016
KS
6161 int width = FRAME_TOTAL_COLS (sf);
6162 int height = FRAME_LINES (sf);
d86c299a 6163
5f5c8ee5 6164 /* If these sizes are so big they cause overflow, just ignore the
dfd153ae
PE
6165 change. It's not clear what better we could do. The rest of
6166 the code assumes that (width + 2) * height * sizeof (struct glyph)
6167 does not overflow and does not exceed PTRDIFF_MAX or SIZE_MAX. */
e3c25c68
PE
6168 if (INT_ADD_RANGE_OVERFLOW (width, 2, INT_MIN, INT_MAX)
6169 || INT_MULTIPLY_RANGE_OVERFLOW (width + 2, height, INT_MIN, INT_MAX)
dfd153ae
PE
6170 || (min (PTRDIFF_MAX, SIZE_MAX) / sizeof (struct glyph)
6171 < (width + 2) * height))
1559a86d 6172 fatal ("screen size %dx%d too big", width, height);
d86c299a
RS
6173 }
6174
91fb7e1b 6175 calculate_costs (XFRAME (selected_frame));
4588ec20 6176
5f5c8ee5
GM
6177 /* Set up faces of the initial terminal frame of a dumped Emacs. */
6178 if (initialized
6179 && !noninteractive
2246281f 6180 && NILP (Vinitial_window_system))
2d764c78
EZ
6181 {
6182 /* For the initial frame, we don't have any way of knowing what
6183 are the foreground and background colors of the terminal. */
5e617bc2 6184 struct frame *sf = SELECTED_FRAME ();
2d764c78 6185
f9d2fdc4
EZ
6186 FRAME_FOREGROUND_PIXEL (sf) = FACE_TTY_DEFAULT_FG_COLOR;
6187 FRAME_BACKGROUND_PIXEL (sf) = FACE_TTY_DEFAULT_BG_COLOR;
2d764c78
EZ
6188 call0 (intern ("tty-set-up-initial-frame-faces"));
6189 }
5f5c8ee5
GM
6190}
6191
6192
6193\f
6194/***********************************************************************
6195 Blinking cursor
6196 ***********************************************************************/
6197
c3f13540 6198DEFUN ("internal-show-cursor", Finternal_show_cursor,
5d7791b3 6199 Sinternal_show_cursor, 2, 2, 0,
7ee72033 6200 doc: /* Set the cursor-visibility flag of WINDOW to SHOW.
228299fa
GM
6201WINDOW nil means use the selected window. SHOW non-nil means
6202show a cursor in WINDOW in the next redisplay. SHOW nil means
7ee72033 6203don't show a cursor. */)
5842a27b 6204 (Lisp_Object window, Lisp_Object show)
5f5c8ee5 6205{
5f5c8ee5
GM
6206 /* Don't change cursor state while redisplaying. This could confuse
6207 output routines. */
6208 if (!redisplaying_p)
b9e9df47 6209 decode_any_window (window)->cursor_off_p = NILP (show);
5f5c8ee5 6210 return Qnil;
4588ec20 6211}
5f5c8ee5
GM
6212
6213
5d7791b3
GM
6214DEFUN ("internal-show-cursor-p", Finternal_show_cursor_p,
6215 Sinternal_show_cursor_p, 0, 1, 0,
bacf09b4 6216 doc: /* Value is non-nil if next redisplay will display a cursor in WINDOW.
7ee72033 6217WINDOW nil or omitted means report on the selected window. */)
5842a27b 6218 (Lisp_Object window)
5d7791b3 6219{
b9e9df47 6220 return decode_any_window (window)->cursor_off_p ? Qnil : Qt;
5d7791b3 6221}
4588ec20 6222\f
5f5c8ee5
GM
6223/***********************************************************************
6224 Initialization
6225 ***********************************************************************/
6226
dfcf069d 6227void
971de7fb 6228syms_of_display (void)
4588ec20 6229{
fe6aa7a1 6230#include "dispnew.x"
9b0e97aa 6231
d1dad759 6232 frame_and_buffer_state = Fmake_vector (make_number (20), Qlambda);
078b3696
KH
6233 staticpro (&frame_and_buffer_state);
6234
cd3520a4
JB
6235 DEFSYM (Qdisplay_table, "display-table");
6236 DEFSYM (Qredisplay_dont_pause, "redisplay-dont-pause");
9cda4f7c 6237
29208e82 6238 DEFVAR_INT ("baud-rate", baud_rate,
025de85b 6239 doc: /* The output baud rate of the terminal.
228299fa
GM
6240On most systems, changing this value will affect the amount of padding
6241and the other strategic decisions made during redisplay. */);
177c0ea7 6242
29208e82 6243 DEFVAR_BOOL ("inverse-video", inverse_video,
025de85b 6244 doc: /* Non-nil means invert the entire frame display.
228299fa 6245This means everything is in inverse video which otherwise would not be. */);
177c0ea7 6246
29208e82 6247 DEFVAR_BOOL ("visible-bell", visible_bell,
025de85b 6248 doc: /* Non-nil means try to flash the frame to represent a bell.
552b1b01
EZ
6249
6250See also `ring-bell-function'. */);
177c0ea7 6251
29208e82 6252 DEFVAR_BOOL ("no-redraw-on-reenter", no_redraw_on_reenter,
025de85b 6253 doc: /* Non-nil means no need to redraw entire frame after suspending.
228299fa
GM
6254A non-nil value is useful if the terminal can automatically preserve
6255Emacs's frame display when you reenter Emacs.
6256It is up to you to set this variable if your terminal can do that. */);
177c0ea7 6257
29208e82 6258 DEFVAR_LISP ("initial-window-system", Vinitial_window_system,
2246281f 6259 doc: /* Name of the window system that Emacs uses for the first frame.
89baa1df
EZ
6260The value is a symbol:
6261 nil for a termcap frame (a character-only terminal),
6262 'x' for an Emacs frame that is really an X window,
6263 'w32' for an Emacs frame that is a window on MS-Windows display,
6264 'ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display,
6265 'pc' for a direct-write MS-DOS frame.
6266
6267Use of this variable as a boolean is deprecated. Instead,
6268use `display-graphic-p' or any of the other `display-*-p'
6269predicates which report frame's specific UI-related capabilities. */);
177c0ea7 6270
70b8d0a4
SM
6271 DEFVAR_KBOARD ("window-system", Vwindow_system,
6272 doc: /* Name of window system through which the selected frame is displayed.
89baa1df
EZ
6273The value is a symbol:
6274 nil for a termcap frame (a character-only terminal),
6275 'x' for an Emacs frame that is really an X window,
6276 'w32' for an Emacs frame that is a window on MS-Windows display,
6277 'ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display,
6278 'pc' for a direct-write MS-DOS frame.
6279
6280Use of this variable as a boolean is deprecated. Instead,
6281use `display-graphic-p' or any of the other `display-*-p'
6282predicates which report frame's specific UI-related capabilities. */);
70b8d0a4 6283
29208e82 6284 DEFVAR_LISP ("window-system-version", Vwindow_system_version,
bacf09b4 6285 doc: /* The version number of the window system in use.
c22140f6 6286For X windows, this is 11. */);
177c0ea7 6287
29208e82 6288 DEFVAR_BOOL ("cursor-in-echo-area", cursor_in_echo_area,
bacf09b4 6289 doc: /* Non-nil means put cursor in minibuffer, at end of any message there. */);
177c0ea7 6290
29208e82 6291 DEFVAR_LISP ("glyph-table", Vglyph_table,
bacf09b4 6292 doc: /* Table defining how to output a glyph code to the frame.
228299fa
GM
6293If not nil, this is a vector indexed by glyph code to define the glyph.
6294Each element can be:
6295 integer: a glyph code which this glyph is an alias for.
6296 string: output this glyph using that string (not impl. in X windows).
6297 nil: this glyph mod 524288 is the code of a character to output,
6298 and this glyph / 524288 is the face number (see `face-id') to use
6299 while outputting it. */);
4588ec20
JB
6300 Vglyph_table = Qnil;
6301
29208e82 6302 DEFVAR_LISP ("standard-display-table", Vstandard_display_table,
bacf09b4 6303 doc: /* Display table to use for buffers that specify none.
228299fa 6304See `buffer-display-table' for more information. */);
4588ec20
JB
6305 Vstandard_display_table = Qnil;
6306
29208e82 6307 DEFVAR_BOOL ("redisplay-dont-pause", redisplay_dont_pause,
025de85b 6308 doc: /* Non-nil means display update isn't paused when input is detected. */);
6bf7006f 6309 redisplay_dont_pause = 1;
5f5c8ee5 6310
4588ec20
JB
6311#ifdef CANNOT_DUMP
6312 if (noninteractive)
6313#endif
6314 {
2246281f 6315 Vinitial_window_system = Qnil;
4588ec20
JB
6316 Vwindow_system_version = Qnil;
6317 }
6318}