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