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