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