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