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