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