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