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