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