dispnew.c 199 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Updating of data structures for redisplay.
2

3
Copyright (C) 1985-1988, 1993-1995, 1997-2020 Free Software Foundation,
4
Inc.
Jim Blandy's avatar
Jim Blandy committed
5 6 7

This file is part of GNU Emacs.

8
GNU Emacs is free software: you can redistribute it and/or modify
Jim Blandy's avatar
Jim Blandy committed
9
it under the terms of the GNU General Public License as published by
10 11
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
Jim Blandy's avatar
Jim Blandy committed
12 13 14 15 16 17 18

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
19
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
20

21
#include <config.h>
22

23
#include "sysstdio.h"
24
#include <stdlib.h>
25 26
#include <unistd.h>

27
#include "lisp.h"
28
#include "ptr-bounds.h"
Jim Blandy's avatar
Jim Blandy committed
29
#include "termchar.h"
30
/* cm.h must come after dispextern.h on Windows.  */
31 32
#include "dispextern.h"
#include "cm.h"
33
#include "buffer.h"
34
#include "keyboard.h"
Jim Blandy's avatar
Jim Blandy committed
35
#include "frame.h"
36
#include "termhooks.h"
Jim Blandy's avatar
Jim Blandy committed
37 38 39
#include "window.h"
#include "commands.h"
#include "disptab.h"
40
#include "blockinput.h"
41
#include "syssignal.h"
42
#include "systime.h"
43
#include "tparam.h"
Paul Eggert's avatar
Paul Eggert committed
44
#include "xwidget.h"
Daniel Colascione's avatar
Daniel Colascione committed
45
#include "pdumper.h"
Jim Blandy's avatar
Jim Blandy committed
46

47 48 49
#ifdef HAVE_WINDOW_SYSTEM
#include TERM_HEADER
#endif /* HAVE_WINDOW_SYSTEM */
50

51 52
#include <errno.h>

53
#include <fpending.h>
54

55 56 57
#ifdef WINDOWSNT
#include "w32.h"
#endif
Gerd Moellmann's avatar
Gerd Moellmann committed
58 59 60 61 62 63 64 65 66 67 68 69 70

/* Structure to pass dimensions around.  Used for character bounding
   boxes, glyph matrix dimensions and alike.  */

struct dim
{
  int width;
  int height;
};


/* Function prototypes.  */

71
static void update_frame_line (struct frame *, int, bool);
72 73
static int required_matrix_height (struct window *);
static int required_matrix_width (struct window *);
74
static void increment_row_positions (struct glyph_row *, ptrdiff_t, ptrdiff_t);
75 76 77 78 79 80
static void build_frame_matrix_from_window_tree (struct glyph_matrix *,
                                                 struct window *);
static void build_frame_matrix_from_leaf_window (struct glyph_matrix *,
                                                 struct window *);
static void adjust_decode_mode_spec_buffer (struct frame *);
static void fill_up_glyph_row_with_spaces (struct glyph_row *);
81
static void clear_window_matrices (struct window *, bool);
82
static void fill_up_glyph_row_area_with_spaces (struct glyph_row *, int);
83
static int scrolling_window (struct window *, int);
84
static bool update_window_line (struct window *, int, bool *);
85
static void mirror_make_current (struct window *, int);
86
#ifdef GLYPH_DEBUG
87 88
static void check_matrix_pointers (struct glyph_matrix *,
                                   struct glyph_matrix *);
89
#endif
90
static void mirror_line_dance (struct window *, int, int, int *, char *);
91 92
static bool update_window_tree (struct window *, bool);
static bool update_window (struct window *, bool);
93
static bool update_frame_1 (struct frame *, bool, bool, bool, bool);
94
static bool scrolling (struct frame *);
95 96 97
static void set_window_cursor_after_update (struct window *);
static void adjust_frame_glyphs_for_window_redisplay (struct frame *);
static void adjust_frame_glyphs_for_frame_redisplay (struct frame *);
98
static void set_window_update_flags (struct window *w, bool on_p);
Jim Blandy's avatar
Jim Blandy committed
99

100
/* True means last display completed.  False means it was preempted.  */
Jim Blandy's avatar
Jim Blandy committed
101

102
bool display_completed;
Jim Blandy's avatar
Jim Blandy committed
103

104
/* True means SIGWINCH happened when not safe.  */
Jim Blandy's avatar
Jim Blandy committed
105

106
static bool delayed_size_change;
Jim Blandy's avatar
Jim Blandy committed
107

Gerd Moellmann's avatar
Gerd Moellmann committed
108
/* A glyph for a space.  */
Jim Blandy's avatar
Jim Blandy committed
109

Gerd Moellmann's avatar
Gerd Moellmann committed
110
struct glyph space_glyph;
Jim Blandy's avatar
Jim Blandy committed
111

112 113
#if defined GLYPH_DEBUG && defined ENABLE_CHECKING

Gerd Moellmann's avatar
Gerd Moellmann committed
114 115
/* Counts of allocated structures.  These counts serve to diagnose
   memory leaks and double frees.  */
Jim Blandy's avatar
Jim Blandy committed
116

117 118
static int glyph_matrix_count;
static int glyph_pool_count;
Jim Blandy's avatar
Jim Blandy committed
119

120 121
#endif /* GLYPH_DEBUG and ENABLE_CHECKING */

Gerd Moellmann's avatar
Gerd Moellmann committed
122 123
/* If non-null, the frame whose frame matrices are manipulated.  If
   null, window matrices are worked on.  */
Jim Blandy's avatar
Jim Blandy committed
124

Gerd Moellmann's avatar
Gerd Moellmann committed
125
static struct frame *frame_matrix_frame;
Jim Blandy's avatar
Jim Blandy committed
126

127
/* Convert vpos and hpos from frame to window and vice versa.
Gerd Moellmann's avatar
Gerd Moellmann committed
128
   This may only be used for terminal frames.  */
129

130
#ifdef GLYPH_DEBUG
Jim Blandy's avatar
Jim Blandy committed
131

132 133
static int window_to_frame_vpos (struct window *, int);
static int window_to_frame_hpos (struct window *, int);
Gerd Moellmann's avatar
Gerd Moellmann committed
134 135
#define WINDOW_TO_FRAME_VPOS(W, VPOS) window_to_frame_vpos ((W), (VPOS))
#define WINDOW_TO_FRAME_HPOS(W, HPOS) window_to_frame_hpos ((W), (HPOS))
Jim Blandy's avatar
Jim Blandy committed
136

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
/* One element of the ring buffer containing redisplay history
   information.  */

struct redisplay_history
{
  char trace[512 + 100];
};

/* The size of the history buffer.  */

#define REDISPLAY_HISTORY_SIZE	30

/* The redisplay history buffer.  */

static struct redisplay_history redisplay_history[REDISPLAY_HISTORY_SIZE];

/* Next free entry in redisplay_history.  */

static int history_idx;

/* A tick that's incremented each time something is added to the
   history.  */

Paul Eggert's avatar
Paul Eggert committed
160
static uintmax_t history_tick;
161

162 163
/* Add to the redisplay history how window W has been displayed.
   MSG is a trace containing the information how W's glyph matrix
164
   has been constructed.  PAUSED_P means that the update
165 166 167
   has been interrupted for pending input.  */

static void
168
add_window_display_history (struct window *w, const char *msg, bool paused_p)
169 170
{
  char *buf;
Paul Eggert's avatar
Paul Eggert committed
171
  void *ptr = w;
172

173 174 175 176
  if (history_idx >= REDISPLAY_HISTORY_SIZE)
    history_idx = 0;
  buf = redisplay_history[history_idx].trace;
  ++history_idx;
Joakim Verona's avatar
Joakim Verona committed
177

178
  snprintf (buf, sizeof redisplay_history[0].trace,
Paul Eggert's avatar
Paul Eggert committed
179
	    "%"PRIuMAX": window %p (%s)%s\n%s",
180
	    history_tick++,
Paul Eggert's avatar
Paul Eggert committed
181
	    ptr,
182 183 184
	    ((BUFFERP (w->contents)
	      && STRINGP (BVAR (XBUFFER (w->contents), name)))
	     ? SSDATA (BVAR (XBUFFER (w->contents), name))
185 186 187
	     : "???"),
	    paused_p ? " ***paused***" : "",
	    msg);
Joakim Verona's avatar
Joakim Verona committed
188
}
189 190 191


/* Add to the redisplay history that frame F has been displayed.
192
   PAUSED_P means that the update has been interrupted for
193 194
   pending input.  */

195 196
static void
add_frame_display_history (struct frame *f, bool paused_p)
197 198
{
  char *buf;
Paul Eggert's avatar
Paul Eggert committed
199
  void *ptr = f;
200

201 202 203 204
  if (history_idx >= REDISPLAY_HISTORY_SIZE)
    history_idx = 0;
  buf = redisplay_history[history_idx].trace;
  ++history_idx;
205

Paul Eggert's avatar
Paul Eggert committed
206
  sprintf (buf, "%"PRIuMAX": update frame %p%s",
207
	   history_tick++,
Paul Eggert's avatar
Paul Eggert committed
208
	   ptr, paused_p ? " ***paused***" : "");
209 210 211 212 213
}


DEFUN ("dump-redisplay-history", Fdump_redisplay_history,
       Sdump_redisplay_history, 0, 0, "",
214
       doc: /* Dump redisplay history to stderr.  */)
215
  (void)
216 217 218 219 220 221 222 223 224 225 226 227 228 229
{
  int i;

  for (i = history_idx - 1; i != history_idx; --i)
    {
      if (i < 0)
	i = REDISPLAY_HISTORY_SIZE - 1;
      fprintf (stderr, "%s\n", redisplay_history[i].trace);
    }

  return Qnil;
}


230
#else /* not GLYPH_DEBUG */
Jim Blandy's avatar
Jim Blandy committed
231

232 233
#define WINDOW_TO_FRAME_VPOS(W, VPOS) ((VPOS) + WINDOW_TOP_EDGE_LINE (W))
#define WINDOW_TO_FRAME_HPOS(W, HPOS) ((HPOS) + WINDOW_LEFT_EDGE_COL (W))
Jim Blandy's avatar
Jim Blandy committed
234

235
#endif /* GLYPH_DEBUG */
Jim Blandy's avatar
Jim Blandy committed
236 237


238
#if defined PROFILING && !HAVE___EXECUTABLE_START
239 240
/* This function comes first in the Emacs executable and is used only
   to estimate the text start for profiling.  */
Jim Blandy's avatar
Jim Blandy committed
241
void
242
__executable_start (void)
Jim Blandy's avatar
Jim Blandy committed
243
{
244
  emacs_abort ();
245
}
246
#endif
Gerd Moellmann's avatar
Gerd Moellmann committed
247 248 249 250 251 252 253 254 255 256 257

/***********************************************************************
			    Glyph Matrices
 ***********************************************************************/

/* Allocate and return a glyph_matrix structure.  POOL is the glyph
   pool from which memory for the matrix should be allocated, or null
   for window-based redisplay where no glyph pools are used.  The
   member `pool' of the glyph matrix structure returned is set to
   POOL, the structure is otherwise zeroed.  */

258
static struct glyph_matrix *
259
new_glyph_matrix (struct glyph_pool *pool)
Jim Blandy's avatar
Jim Blandy committed
260
{
Dmitry Antipov's avatar
Dmitry Antipov committed
261
  struct glyph_matrix *result = xzalloc (sizeof *result);
Jim Blandy's avatar
Jim Blandy committed
262

263
#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
Gerd Moellmann's avatar
Gerd Moellmann committed
264 265 266
  /* Increment number of allocated matrices.  This count is used
     to detect memory leaks.  */
  ++glyph_matrix_count;
267
#endif
Jim Blandy's avatar
Jim Blandy committed
268

Gerd Moellmann's avatar
Gerd Moellmann committed
269 270 271
  /* Set pool and return.  */
  result->pool = pool;
  return result;
Jim Blandy's avatar
Jim Blandy committed
272 273 274
}


Gerd Moellmann's avatar
Gerd Moellmann committed
275 276
/* Free glyph matrix MATRIX.  Passing in a null MATRIX is allowed.

277 278 279 280
   If GLYPH_DEBUG and ENABLE_CHECKING are in effect, the global counter
   glyph_matrix_count is decremented when a matrix is freed.  If the count
   gets negative, more structures were freed than allocated, i.e. one matrix
   was freed more than once or a bogus pointer was passed to this function.
281

Gerd Moellmann's avatar
Gerd Moellmann committed
282 283 284 285 286
   If MATRIX->pool is null, this means that the matrix manages its own
   glyph memory---this is done for matrices on X frames.  Freeing the
   matrix also frees the glyph memory in this case.  */

static void
287
free_glyph_matrix (struct glyph_matrix *matrix)
Jim Blandy's avatar
Jim Blandy committed
288
{
Gerd Moellmann's avatar
Gerd Moellmann committed
289 290 291 292
  if (matrix)
    {
      int i;

293
#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
Gerd Moellmann's avatar
Gerd Moellmann committed
294 295
      /* Detect the case that more matrices are freed than were
	 allocated.  */
296 297 298
      --glyph_matrix_count;
      eassert (glyph_matrix_count >= 0);
#endif
Gerd Moellmann's avatar
Gerd Moellmann committed
299 300 301 302 303

      /* Free glyph memory if MATRIX owns it.  */
      if (matrix->pool == NULL)
	for (i = 0; i < matrix->rows_allocated; ++i)
	  xfree (matrix->rows[i].glyphs[LEFT_MARGIN_AREA]);
304

Gerd Moellmann's avatar
Gerd Moellmann committed
305 306 307 308 309
      /* Free row structures and the matrix itself.  */
      xfree (matrix->rows);
      xfree (matrix);
    }
}
Jim Blandy's avatar
Jim Blandy committed
310 311


Gerd Moellmann's avatar
Gerd Moellmann committed
312 313 314
/* Return the number of glyphs to reserve for a marginal area of
   window W.  TOTAL_GLYPHS is the number of glyphs in a complete
   display line of window W.  MARGIN gives the width of the marginal
315
   area in canonical character units.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
316 317

static int
318
margin_glyphs_to_reserve (struct window *w, int total_glyphs, int margin)
Gerd Moellmann's avatar
Gerd Moellmann committed
319
{
320
  if (margin > 0)
Jim Blandy's avatar
Jim Blandy committed
321
    {
322
      int width = w->total_cols;
323
      double d = max (0, margin);
Gerd Moellmann's avatar
Gerd Moellmann committed
324
      d = min (width / 2 - 1, d);
325 326 327
      /* Since MARGIN is positive, we cannot possibly have less than
	 one glyph for the marginal area.  */
      return max (1, (int) ((double) total_glyphs / width * d));
Gerd Moellmann's avatar
Gerd Moellmann committed
328
    }
329
  return 0;
Gerd Moellmann's avatar
Gerd Moellmann committed
330 331
}

332
/* Return true if ROW's hash value is correct.
333 334
   Optimized away if ENABLE_CHECKING is not defined.  */

335
static bool
336 337
verify_row_hash (struct glyph_row *row)
{
338
  return row->hash == row_hash (row);
339
}
Jim Blandy's avatar
Jim Blandy committed
340

Gerd Moellmann's avatar
Gerd Moellmann committed
341 342
/* Adjust glyph matrix MATRIX on window W or on a frame to changed
   window sizes.
Jim Blandy's avatar
Jim Blandy committed
343

Gerd Moellmann's avatar
Gerd Moellmann committed
344 345 346 347 348
   W is null if the function is called for a frame glyph matrix.
   Otherwise it is the window MATRIX is a member of.  X and Y are the
   indices of the first column and row of MATRIX within the frame
   matrix, if such a matrix exists.  They are zero for purely
   window-based redisplay.  DIM is the needed size of the matrix.
349

Gerd Moellmann's avatar
Gerd Moellmann committed
350 351 352 353
   In window-based redisplay, where no frame matrices exist, glyph
   matrices manage their own glyph storage.  Otherwise, they allocate
   storage from a common frame glyph pool which can be found in
   MATRIX->pool.
354

Gerd Moellmann's avatar
Gerd Moellmann committed
355 356 357 358 359 360 361
   The reason for this memory management strategy is to avoid complete
   frame redraws if possible.  When we allocate from a common pool, a
   change of the location or size of a sub-matrix within the pool
   requires a complete redisplay of the frame because we cannot easily
   make sure that the current matrices of all windows still agree with
   what is displayed on the screen.  While this is usually fast, it
   leads to screen flickering.  */
362

Gerd Moellmann's avatar
Gerd Moellmann committed
363
static void
364
adjust_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int x, int y, struct dim dim)
Gerd Moellmann's avatar
Gerd Moellmann committed
365 366 367
{
  int i;
  int new_rows;
368
  bool marginal_areas_changed_p = 0;
369 370
  bool tab_line_changed_p = 0;
  bool tab_line_p = 0;
371 372
  bool header_line_changed_p = 0;
  bool header_line_p = 0;
Gerd Moellmann's avatar
Gerd Moellmann committed
373
  int left = -1, right = -1;
374
  int window_width = -1, window_height = -1;
Gerd Moellmann's avatar
Gerd Moellmann committed
375

Kenichi Handa's avatar
Kenichi Handa committed
376 377
  /* See if W had a header line that has disappeared now, or vice versa.
     Get W's size.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
378 379
  if (w)
    {
Joakim Verona's avatar
Joakim Verona committed
380
      window_box (w, ANY_AREA, 0, 0, &window_width, &window_height);
Kenichi Handa's avatar
Kenichi Handa committed
381

382 383 384
      tab_line_p = window_wants_tab_line (w);
      tab_line_changed_p = tab_line_p != matrix->tab_line_p;

385
      header_line_p = window_wants_header_line (w);
Gerd Moellmann's avatar
Gerd Moellmann committed
386
      header_line_changed_p = header_line_p != matrix->header_line_p;
Gerd Moellmann's avatar
Gerd Moellmann committed
387
    }
388
  matrix->tab_line_p = tab_line_p;
Gerd Moellmann's avatar
Gerd Moellmann committed
389
  matrix->header_line_p = header_line_p;
390

Kenichi Handa's avatar
Kenichi Handa committed
391 392
  /* If POOL is null, MATRIX is a window matrix for window-based redisplay.
     Do nothing if MATRIX' size, position, vscroll, and marginal areas
Gerd Moellmann's avatar
Gerd Moellmann committed
393 394
     haven't changed.  This optimization is important because preserving
     the matrix means preventing redisplay.  */
395
  eassume (w != NULL || matrix->pool != NULL);
Gerd Moellmann's avatar
Gerd Moellmann committed
396 397
  if (matrix->pool == NULL)
    {
398 399
      left = margin_glyphs_to_reserve (w, dim.width, w->left_margin_cols);
      right = margin_glyphs_to_reserve (w, dim.width, w->right_margin_cols);
400
      eassert (left >= 0 && right >= 0);
Gerd Moellmann's avatar
Gerd Moellmann committed
401 402 403 404
      marginal_areas_changed_p = (left != matrix->left_margin_glyphs
				  || right != matrix->right_margin_glyphs);

      if (!marginal_areas_changed_p
Joakim Verona's avatar
Joakim Verona committed
405
	  && !XFRAME (w->frame)->fonts_changed
406
	  && !tab_line_changed_p
Gerd Moellmann's avatar
Gerd Moellmann committed
407
	  && !header_line_changed_p
408 409
	  && matrix->window_pixel_left == WINDOW_LEFT_PIXEL_EDGE (w)
	  && matrix->window_pixel_top == WINDOW_TOP_PIXEL_EDGE (w)
Gerd Moellmann's avatar
Gerd Moellmann committed
410 411 412 413 414
	  && matrix->window_height == window_height
	  && matrix->window_vscroll == w->vscroll
	  && matrix->window_width == window_width)
	return;
    }
415

Gerd Moellmann's avatar
Gerd Moellmann committed
416 417 418
  /* Enlarge MATRIX->rows if necessary.  New rows are cleared.  */
  if (matrix->rows_allocated < dim.height)
    {
419
      int old_alloc = matrix->rows_allocated;
Gerd Moellmann's avatar
Gerd Moellmann committed
420
      new_rows = dim.height - matrix->rows_allocated;
421 422 423 424
      matrix->rows = xpalloc (matrix->rows, &matrix->rows_allocated,
			      new_rows, INT_MAX, sizeof *matrix->rows);
      memset (matrix->rows + old_alloc, 0,
	      (matrix->rows_allocated - old_alloc) * sizeof *matrix->rows);
Gerd Moellmann's avatar
Gerd Moellmann committed
425 426 427
    }
  else
    new_rows = 0;
428

Gerd Moellmann's avatar
Gerd Moellmann committed
429 430 431 432 433
  /* If POOL is not null, MATRIX is a frame matrix or a window matrix
     on a frame not using window-based redisplay.  Set up pointers for
     each row into the glyph pool.  */
  if (matrix->pool)
    {
434
      eassert (matrix->pool->glyphs);
435

Gerd Moellmann's avatar
Gerd Moellmann committed
436
      if (w)
437
	{
Gerd Moellmann's avatar
Gerd Moellmann committed
438
	  left = margin_glyphs_to_reserve (w, dim.width,
439
					   w->left_margin_cols);
Gerd Moellmann's avatar
Gerd Moellmann committed
440
	  right = margin_glyphs_to_reserve (w, dim.width,
441
					    w->right_margin_cols);
442
	}
Gerd Moellmann's avatar
Gerd Moellmann committed
443 444
      else
	left = right = 0;
445

Gerd Moellmann's avatar
Gerd Moellmann committed
446
      for (i = 0; i < dim.height; ++i)
447
	{
Gerd Moellmann's avatar
Gerd Moellmann committed
448
	  struct glyph_row *row = &matrix->rows[i];
449 450

	  row->glyphs[LEFT_MARGIN_AREA]
Gerd Moellmann's avatar
Gerd Moellmann committed
451 452 453
	    = (matrix->pool->glyphs
	       + (y + i) * matrix->pool->ncolumns
	       + x);
454

Gerd Moellmann's avatar
Gerd Moellmann committed
455
	  if (w == NULL
456
	      || (row == matrix->rows + dim.height - 1
457
		  && window_wants_mode_line (w))
458
	      || (row == matrix->rows && matrix->tab_line_p)
459 460 461 462
	      || (row == matrix->rows
		  && !matrix->tab_line_p && matrix->header_line_p)
	      || (row == (matrix->rows + 1)
		  && matrix->tab_line_p && matrix->header_line_p))
Gerd Moellmann's avatar
Gerd Moellmann committed
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
	    {
	      row->glyphs[TEXT_AREA]
		= row->glyphs[LEFT_MARGIN_AREA];
	      row->glyphs[RIGHT_MARGIN_AREA]
		= row->glyphs[TEXT_AREA] + dim.width;
	      row->glyphs[LAST_AREA]
		= row->glyphs[RIGHT_MARGIN_AREA];
	    }
	  else
	    {
	      row->glyphs[TEXT_AREA]
		= row->glyphs[LEFT_MARGIN_AREA] + left;
	      row->glyphs[RIGHT_MARGIN_AREA]
		= row->glyphs[TEXT_AREA] + dim.width - left - right;
	      row->glyphs[LAST_AREA]
		= row->glyphs[LEFT_MARGIN_AREA] + dim.width;
	    }
480
	}
481

Gerd Moellmann's avatar
Gerd Moellmann committed
482 483 484 485 486 487
      matrix->left_margin_glyphs = left;
      matrix->right_margin_glyphs = right;
    }
  else
    {
      /* If MATRIX->pool is null, MATRIX is responsible for managing
Kenichi Handa's avatar
Kenichi Handa committed
488 489
	 its own memory.  It is a window matrix for window-based redisplay.
	 Allocate glyph memory from the heap.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
490 491
      if (dim.width > matrix->matrix_w
	  || new_rows
492
	  || tab_line_changed_p
Gerd Moellmann's avatar
Gerd Moellmann committed
493
	  || header_line_changed_p
Gerd Moellmann's avatar
Gerd Moellmann committed
494
	  || marginal_areas_changed_p)
Jim Blandy's avatar
Jim Blandy committed
495
	{
Gerd Moellmann's avatar
Gerd Moellmann committed
496 497
	  struct glyph_row *row = matrix->rows;
	  struct glyph_row *end = row + matrix->rows_allocated;
498

Gerd Moellmann's avatar
Gerd Moellmann committed
499 500 501
	  while (row < end)
	    {
	      row->glyphs[LEFT_MARGIN_AREA]
502 503
		= xnrealloc (row->glyphs[LEFT_MARGIN_AREA],
			     dim.width, sizeof (struct glyph));
504

505 506
	      /* The mode line, if displayed, never has marginal areas.  */
	      if ((row == matrix->rows + dim.height - 1
507
		   && !(w && window_wants_mode_line (w)))
508
		  || (row == matrix->rows && matrix->tab_line_p)
509 510 511 512
		  || (row == matrix->rows
		      && !matrix->tab_line_p && matrix->header_line_p)
		  || (row == (matrix->rows + 1)
		      && matrix->tab_line_p && matrix->header_line_p))
Gerd Moellmann's avatar
Gerd Moellmann committed
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
		{
		  row->glyphs[TEXT_AREA]
		    = row->glyphs[LEFT_MARGIN_AREA];
		  row->glyphs[RIGHT_MARGIN_AREA]
		    = row->glyphs[TEXT_AREA] + dim.width;
		  row->glyphs[LAST_AREA]
		    = row->glyphs[RIGHT_MARGIN_AREA];
		}
	      else
		{
		  row->glyphs[TEXT_AREA]
		    = row->glyphs[LEFT_MARGIN_AREA] + left;
		  row->glyphs[RIGHT_MARGIN_AREA]
		    = row->glyphs[TEXT_AREA] + dim.width - left - right;
		  row->glyphs[LAST_AREA]
		    = row->glyphs[LEFT_MARGIN_AREA] + dim.width;
		}
	      ++row;
	    }
Jim Blandy's avatar
Jim Blandy committed
532 533
	}

534
      eassert (left >= 0 && right >= 0);
Gerd Moellmann's avatar
Gerd Moellmann committed
535 536
      matrix->left_margin_glyphs = left;
      matrix->right_margin_glyphs = right;
537 538 539 540

      /* If we are resizing a window, make sure the previous mode-line
	 row of the window's current matrix is no longer marked as such.  */
      if (w && matrix == w->current_matrix
541
	  && matrix->nrows > 0
542 543 544
	  && dim.height != matrix->nrows
	  && matrix->nrows <= matrix->rows_allocated)
	MATRIX_MODE_LINE_ROW (matrix)->mode_line_p = false;
Gerd Moellmann's avatar
Gerd Moellmann committed
545
    }
546

Gerd Moellmann's avatar
Gerd Moellmann committed
547 548
  /* Number of rows to be used by MATRIX.  */
  matrix->nrows = dim.height;
549
  eassert (matrix->nrows >= 0);
Gerd Moellmann's avatar
Gerd Moellmann committed
550

551
  if (w)
Gerd Moellmann's avatar
Gerd Moellmann committed
552
    {
553
      if (matrix == w->current_matrix)
Jim Blandy's avatar
Jim Blandy committed
554
	{
555 556 557 558 559 560
	  /* Mark rows in a current matrix of a window as not having
	     valid contents.  It's important to not do this for
	     desired matrices.  When Emacs starts, it may already be
	     building desired matrices when this function runs.  */
	  if (window_width < 0)
	    window_width = window_box_width (w, -1);
561

562 563 564 565
	  /* Optimize the case that only the height has changed (C-x 2,
	     upper window).  Invalidate all rows that are no longer part
	     of the window.  */
	  if (!marginal_areas_changed_p
566
	      && !tab_line_changed_p
567 568 569
	      && !header_line_changed_p
	      && new_rows == 0
	      && dim.width == matrix->matrix_w
570 571
	      && matrix->window_pixel_left == WINDOW_LEFT_PIXEL_EDGE (w)
	      && matrix->window_pixel_top == WINDOW_TOP_PIXEL_EDGE (w)
572
	      && matrix->window_width == window_width)
573
	    {
574 575 576 577
	      /* Find the last row in the window.  */
	      for (i = 0; i < matrix->nrows && matrix->rows[i].enabled_p; ++i)
		if (MATRIX_ROW_BOTTOM_Y (matrix->rows + i) >= window_height)
		  {
578
		    ++i;
579 580
		    break;
		  }
581 582

	      /* Window end is invalid, if inside of the rows that
583
		 are invalidated below.  */
584
	      if (w->window_end_vpos >= i)
585
		w->window_end_valid = 0;
586

587
	      while (i < matrix->nrows)
588
		matrix->rows[i++].enabled_p = false;
589 590 591 592
	    }
	  else
	    {
	      for (i = 0; i < matrix->nrows; ++i)
593
		matrix->rows[i].enabled_p = false;
594
	    }
595 596 597 598
	  /* We've disabled the mode-line row, so force redrawing of
	     the mode line, if any, since otherwise it will remain
	     disabled in the current matrix, and expose events won't
	     redraw it.  */
599
	  if (window_wants_mode_line (w))
600
	    w->update_mode_line = 1;
Gerd Moellmann's avatar
Gerd Moellmann committed
601
	}
602
      else if (matrix == w->desired_matrix)
Gerd Moellmann's avatar
Gerd Moellmann committed
603
	{
604 605 606 607
	  /* Rows in desired matrices always have to be cleared;
	     redisplay expects this is the case when it runs, so it
	     had better be the case when we adjust matrices between
	     redisplays.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
608
	  for (i = 0; i < matrix->nrows; ++i)
609
	    matrix->rows[i].enabled_p = false;
Jim Blandy's avatar
Jim Blandy committed
610 611
	}
    }
612 613


Gerd Moellmann's avatar
Gerd Moellmann committed
614 615 616 617 618 619 620 621 622
  /* Remember last values to be able to optimize frame redraws.  */
  matrix->matrix_x = x;
  matrix->matrix_y = y;
  matrix->matrix_w = dim.width;
  matrix->matrix_h = dim.height;

  /* Record the top y location and height of W at the time the matrix
     was last adjusted.  This is used to optimize redisplay above.  */
  if (w)
Jim Blandy's avatar
Jim Blandy committed
623
    {
624 625
      matrix->window_pixel_left = WINDOW_LEFT_PIXEL_EDGE (w);
      matrix->window_pixel_top = WINDOW_TOP_PIXEL_EDGE (w);
Gerd Moellmann's avatar
Gerd Moellmann committed
626 627 628 629 630
      matrix->window_height = window_height;
      matrix->window_width = window_width;
      matrix->window_vscroll = w->vscroll;
    }
}
Jim Blandy's avatar
Jim Blandy committed
631 632


Gerd Moellmann's avatar
Gerd Moellmann committed
633 634 635 636
/* Reverse the contents of rows in MATRIX between START and END.  The
   contents of the row at END - 1 end up at START, END - 2 at START +
   1 etc.  This is part of the implementation of rotate_matrix (see
   below).  */
637

Gerd Moellmann's avatar
Gerd Moellmann committed
638
static void
639
reverse_rows (struct glyph_matrix *matrix, int start, int end)
Gerd Moellmann's avatar
Gerd Moellmann committed
640 641
{
  int i, j;
642

Gerd Moellmann's avatar
Gerd Moellmann committed
643 644 645 646 647 648 649 650 651 652
  for (i = start, j = end - 1; i < j; ++i, --j)
    {
      /* Non-ISO HP/UX compiler doesn't like auto struct
	 initialization.  */
      struct glyph_row temp;
      temp = matrix->rows[i];
      matrix->rows[i] = matrix->rows[j];
      matrix->rows[j] = temp;
    }
}
653 654


Gerd Moellmann's avatar
Gerd Moellmann committed
655 656 657 658
/* Rotate the contents of rows in MATRIX in the range FIRST .. LAST -
   1 by BY positions.  BY < 0 means rotate left, i.e. towards lower
   indices.  (Note: this does not copy glyphs, only glyph pointers in
   row structures are moved around).
659

Gerd Moellmann's avatar
Gerd Moellmann committed
660 661 662 663 664 665 666 667
   The algorithm used for rotating the vector was, I believe, first
   described by Kernighan.  See the vector R as consisting of two
   sub-vectors AB, where A has length BY for BY >= 0.  The result
   after rotating is then BA.  Reverse both sub-vectors to get ArBr
   and reverse the result to get (ArBr)r which is BA.  Similar for
   rotating right.  */

void
668
rotate_matrix (struct glyph_matrix *matrix, int first, int last, int by)
Gerd Moellmann's avatar
Gerd Moellmann committed
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
{
  if (by < 0)
    {
      /* Up (rotate left, i.e. towards lower indices).  */
      by = -by;
      reverse_rows (matrix, first, first + by);
      reverse_rows (matrix, first + by, last);
      reverse_rows (matrix, first, last);
    }
  else if (by > 0)
    {
      /* Down (rotate right, i.e. towards higher indices).  */
      reverse_rows (matrix, last - by, last);
      reverse_rows (matrix, first, last - by);
      reverse_rows (matrix, first, last);
    }
}


/* Increment buffer positions in glyph rows of MATRIX.  Do it for rows
   with indices START <= index < END.  Increment positions by DELTA/
   DELTA_BYTES.  */

void
693
increment_matrix_positions (struct glyph_matrix *matrix, int start, int end,
694
			    ptrdiff_t delta, ptrdiff_t delta_bytes)
Gerd Moellmann's avatar
Gerd Moellmann committed
695 696
{
  /* Check that START and END are reasonable values.  */
697 698 699
  eassert (start >= 0 && start <= matrix->nrows);
  eassert (end >= 0 && end <= matrix->nrows);
  eassert (start <= end);
Gerd Moellmann's avatar
Gerd Moellmann committed
700 701

  for (; start < end; ++start)
702
    increment_row_positions (matrix->rows + start, delta, delta_bytes);
Gerd Moellmann's avatar
Gerd Moellmann committed
703 704 705
}


706 707
/* Clear the enable_p flags in a range of rows in glyph matrix MATRIX.
   START and END are the row indices of the first and last + 1 row to clear.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
708 709

void
710
clear_glyph_matrix_rows (struct glyph_matrix *matrix, int start, int end)
Gerd Moellmann's avatar
Gerd Moellmann committed
711
{
712
  eassert (start <= end);
Chris Feng's avatar
Chris Feng committed
713 714 715
  eassert (start >= 0 && (start < matrix->nrows
			  /* matrix->nrows can be 0 for the initial frame.  */
			  || (matrix->nrows == 0)));
716
  eassert (end >= 0 && end <= matrix->nrows);
717

Gerd Moellmann's avatar
Gerd Moellmann committed
718
  for (; start < end; ++start)
719
    matrix->rows[start].enabled_p = false;
Gerd Moellmann's avatar
Gerd Moellmann committed
720 721 722 723 724
}


/* Clear MATRIX.

725 726 727
   Empty all rows in MATRIX by clearing their enabled_p flags.
   The function prepare_desired_row will eventually really clear a row
   when it sees one with a false enabled_p flag.
Gerd Moellmann's avatar
Gerd Moellmann committed
728

729
   Reset update hints to default values.  The only update hint
Gerd Moellmann's avatar
Gerd Moellmann committed
730 731 732
   currently present is the flag MATRIX->no_scrolling_p.  */

void
733
clear_glyph_matrix (struct glyph_matrix *matrix)
Gerd Moellmann's avatar
Gerd Moellmann committed
734 735 736
{
  if (matrix)
    {
737
      clear_glyph_matrix_rows (matrix, 0, matrix->nrows);
Gerd Moellmann's avatar
Gerd Moellmann committed
738 739 740
      matrix->no_scrolling_p = 0;
    }
}
741

Gerd Moellmann's avatar
Gerd Moellmann committed
742 743 744 745 746 747

/* Shift part of the glyph matrix MATRIX of window W up or down.
   Increment y-positions in glyph rows between START and END by DY,
   and recompute their visible height.  */

void
748
shift_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int start, int end, int dy)
Gerd Moellmann's avatar
Gerd Moellmann committed
749 750
{
  int min_y, max_y;
751

752 753 754
  eassert (start <= end);
  eassert (start >= 0 && start < matrix->nrows);
  eassert (end >= 0 && end <= matrix->nrows);
755

756
  min_y = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
757
  max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
758

Gerd Moellmann's avatar
Gerd Moellmann committed
759 760 761
  for (; start < end; ++start)
    {
      struct glyph_row *row = &matrix->rows[start];
762

Gerd Moellmann's avatar
Gerd Moellmann committed
763
      row->y += dy;
764
      row->visible_height = row->height;
765

Gerd Moellmann's avatar
Gerd Moellmann committed
766
      if (row->y < min_y)
767 768 769
	row->visible_height -= min_y - row->y;
      if (row->y + row->height > max_y)
	row->visible_height -= row->y + row->height - max_y;
770 771
      if (row->fringe_bitmap_periodic_p)
	row->redraw_fringe_bitmaps_p = 1;
Gerd Moellmann's avatar
Gerd Moellmann committed
772 773 774 775 776 777 778 779 780
    }
}


/* Mark all rows in current matrices of frame F as invalid.  Marking
   invalid is done by setting enabled_p to zero for all rows in a
   current matrix.  */

void
781
clear_current_matrices (register struct frame *f)
Gerd Moellmann's avatar
Gerd Moellmann committed
782 783 784 785 786
{
  /* Clear frame current matrix, if we have one.  */
  if (f->current_matrix)
    clear_glyph_matrix (f->current_matrix);

787
#if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
Gerd Moellmann's avatar
Gerd Moellmann committed
788 789 790
  /* Clear the matrix of the menu bar window, if such a window exists.
     The menu bar window is currently used to display menus on X when
     no toolkit support is compiled in.  */
791 792
  if (WINDOWP (f->menu_bar_window))
    clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
793
#endif
Gerd Moellmann's avatar
Gerd Moellmann committed
794

795 796 797 798 799 800
#if defined (HAVE_WINDOW_SYSTEM)
  /* Clear the matrix of the tab-bar window, if any.  */
  if (WINDOWP (f->tab_bar_window))
    clear_glyph_matrix (XWINDOW (f->tab_bar_window)->current_matrix);
#endif

801
#if defined (HAVE_WINDOW_SYSTEM) && ! defined (HAVE_EXT_TOOL_BAR)
802
  /* Clear the matrix of the tool-bar window, if any.  */
803 804
  if (WINDOWP (f->tool_bar_window))
    clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
805
#endif
Gerd Moellmann's avatar
Gerd Moellmann committed
806 807

  /* Clear current window matrices.  */
808
  eassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
Gerd Moellmann's avatar
Gerd Moellmann committed
809 810 811 812 813 814 815
  clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 0);
}


/* Clear out all display lines of F for a coming redisplay.  */

void
816
clear_desired_matrices (register struct frame *f)
Gerd Moellmann's avatar
Gerd Moellmann committed
817 818 819
{
  if (f->desired_matrix)
    clear_glyph_matrix (f->desired_matrix);
820

821
#if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
822 823
  if (WINDOWP (f->menu_bar_window))
    clear_glyph_matrix (XWINDOW (f->menu_bar_window)->desired_matrix);
824
#endif
Gerd Moellmann's avatar
Gerd Moellmann committed
825

826 827 828 829 830
#if defined (HAVE_WINDOW_SYSTEM)
  if (WINDOWP (f->tab_bar_window))
    clear_glyph_matrix (XWINDOW (f->tab_bar_window)->desired_matrix);
#endif

831
#if defined (HAVE_WINDOW_SYSTEM) && ! defined (HAVE_EXT_TOOL_BAR)
832 833
  if (WINDOWP (f->tool_bar_window))
    clear_glyph_matrix (XWINDOW (f->tool_bar_window)->desired_matrix);
834
#endif
Gerd Moellmann's avatar
Gerd Moellmann committed
835 836

  /* Do it for window matrices.  */
837
  eassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
Gerd Moellmann's avatar
Gerd Moellmann committed
838 839 840 841
  clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
}


842 843
/* Clear matrices in window tree rooted in W.  If DESIRED_P,
   clear desired matrices, otherwise clear current matrices.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
844 845

static void
846
clear_window_matrices (struct window *w, bool desired_p)
Gerd Moellmann's avatar
Gerd Moellmann committed
847 848 849
{
  while (w)
    {
850 851
      if (WINDOWP (w->contents))
	clear_window_matrices (XWINDOW (w->contents), desired_p);
Gerd Moellmann's avatar
Gerd Moellmann committed
852 853 854 855 856 857 858
      else
	{
	  if (desired_p)
	    clear_glyph_matrix (w->desired_matrix);
	  else
	    {
	      clear_glyph_matrix (w->current_matrix);
859
	      w->window_end_valid = 0;
Gerd Moellmann's avatar
Gerd Moellmann committed
860 861 862
	    }
	}

863
      w = NILP (w->next) ? 0 : XWINDOW (w->next);
Gerd Moellmann's avatar
Gerd Moellmann committed
864 865 866 867 868 869 870 871 872 873 874
    }
}



/***********************************************************************
			      Glyph Rows

      See dispextern.h for an overall explanation of glyph rows.
 ***********************************************************************/

875 876
/* Clear glyph row ROW.  NOTE: this code relies on the current
   layout of `glyphs' and `used' fields of `struct glyph_row'.  */
877

Gerd Moellmann's avatar
Gerd Moellmann committed
878
void
879
clear_glyph_row (struct glyph_row *row)
Gerd Moellmann's avatar
Gerd Moellmann committed
880
{
881
  enum { off = offsetof (struct glyph_row, used) };
882 883

  /* Zero everything except pointers in `glyphs'.  */
884
  memset (row->used, 0, sizeof *row - off);
Gerd Moellmann's avatar
Gerd Moellmann committed
885 886 887 888 889 890 891
}


/* Make ROW an empty, enabled row of canonical character height,
   in window W starting at y-position Y.  */

void
892
blank_row (struct window *w, struct glyph_row *row, int y)
Gerd Moellmann's avatar
Gerd Moellmann committed
893 894
{
  int min_y, max_y;
895

896
  min_y = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
897
  max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
898

Gerd Moellmann's avatar
Gerd Moellmann committed
899 900
  clear_glyph_row (row);
  row->y = y;
901
  row->ascent = row->phys_ascent = 0;
902
  row->height = row->phys_height = FRAME_LINE_HEIGHT (XFRAME (w->frame));
903
  row->visible_height = row->height;
904

Gerd Moellmann's avatar
Gerd Moellmann committed
905
  if (row->y < min_y)
906 907 908
    row->visible_height -= min_y - row->y;
  if (row->y + row->height > max_y)
    row->visible_height -= row->y + row->height - max_y;
Gerd Moellmann's avatar
Gerd Moellmann committed
909

910
  row->enabled_p = true;
Gerd Moellmann's avatar
Gerd Moellmann committed
911 912 913 914 915 916 917 918 919
}


/* Increment buffer positions in glyph row ROW.  DELTA and DELTA_BYTES
   are the amounts by which to change positions.  Note that the first
   glyph of the text area of a row can have a buffer position even if
   the used count of the text area is zero.  Such rows display line
   ends.  */

920
static void
921
increment_row_positions (struct glyph_row *row,
922
			 ptrdiff_t delta, ptrdiff_t delta_bytes)
Gerd Moellmann's avatar
Gerd Moellmann committed
923 924 925 926 927 928 929 930
{
  int area, i;

  /* Increment start and end positions.  */
  MATRIX_ROW_START_CHARPOS (row) += delta;
  MATRIX_ROW_START_BYTEPOS (row) += delta_bytes;
  MATRIX_ROW_END_CHARPOS (row) += delta;
  MATRIX_ROW_END_BYTEPOS (row) += delta_bytes;
931 932 933 934
  CHARPOS (row->start.pos) += delta;
  BYTEPOS (row->start.pos) += delta_bytes;
  CHARPOS (row->end.pos) += delta;
  BYTEPOS (row->end.pos) += delta_bytes;
Gerd Moellmann's avatar
Gerd Moellmann committed
935

936 937 938
  if (!row->enabled_p)
    return;

Gerd Moellmann's avatar
Gerd Moellmann committed
939 940 941 942 943 944 945 946 947 948 949 950 951 952
  /* Increment positions in glyphs.  */
  for (area = 0; area < LAST_AREA; ++area)
    for (i = 0; i < row->used[area]; ++i)
      if (BUFFERP (row->glyphs[area][i].object)
	  && row->glyphs[area][i].charpos > 0)
	row->glyphs[area][i].charpos += delta;

  /* Capture the case of rows displaying a line end.  */
  if (row->used[TEXT_AREA] == 0
      && MATRIX_ROW_DISPLAYS_TEXT_P (row))
    row->glyphs[TEXT_AREA]->charpos += delta;
}


953
#if 0
Gerd Moellmann's avatar
Gerd Moellmann committed
954 955 956 957 958
/* Swap glyphs between two glyph rows A and B.  This exchanges glyph
   contents, i.e. glyph structure contents are exchanged between A and
   B without changing glyph pointers in A and B.  */

static void
Andreas Schwab's avatar
Andreas Schwab committed
959
swap_glyphs_in_rows (struct glyph_row *a, struct glyph_row *b)
Gerd Moellmann's avatar
Gerd Moellmann committed
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
{
  int area;

  for (area = 0; area < LAST_AREA; ++area)
    {
      /* Number of glyphs to swap.  */
      int max_used = max (a->used[area], b->used[area]);

      /* Start of glyphs in area of row A.  */
      struct glyph *glyph_a = a->glyphs[area];

      /* End + 1 of glyphs in area of row A.  */
      struct glyph *glyph_a_end = a->glyphs[max_used];

      /* Start of glyphs in area of row B.  */
      struct glyph *glyph_b = b->glyphs[area];

      while (glyph_a < glyph_a_end)
	{
	  /* Non-ISO HP/UX compiler doesn't like auto struct
             initialization.  */
	  struct glyph temp;
	  temp = *glyph_a;
	  *glyph_a = *glyph_b;
	  *glyph_b = temp;
	  ++glyph_a;
	  ++glyph_b;
	}
    }
}

991
#endif /* 0 */
Gerd Moellmann's avatar
Gerd Moellmann committed
992

993 994 995 996
/* Exchange pointers to glyph memory between glyph rows A and B.  Also
   exchange the used[] array and the hash values of the rows, because
   these should all go together for the row's hash value to be
   correct.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
997

998
static void
999
swap_glyph_pointers (struct glyph_row *a, struct glyph_row *b)
Gerd Moellmann's avatar
Gerd Moellmann committed
1000 1001
{
  int i;
1002 1003
  unsigned hash_tem = a->hash;

Gerd Moellmann's avatar
Gerd Moellmann committed
1004 1005 1006
  for (i = 0; i < LAST_AREA + 1; ++i)
    {
      struct glyph *temp = a->glyphs[i];
1007

Gerd Moellmann's avatar
Gerd Moellmann committed
1008 1009
      a->glyphs[i] = b->glyphs[i];
      b->glyphs[i] = temp;
1010 1011 1012 1013 1014 1015 1016
      if (i < LAST_AREA)
	{
	  short used_tem = a->used[i];

	  a->used[i] = b->used[i];
	  b->used[i] = used_tem;
	}
Gerd Moellmann's avatar
Gerd Moellmann committed
1017
    }
1018 1019
  a->hash = b->hash;
  b->hash = hash_tem;
Gerd Moellmann's avatar
Gerd Moellmann committed
1020 1021 1022
}


1023 1024 1025 1026
/* Copy glyph row structure FROM to glyph row structure TO, except that
   glyph pointers, the `used' counts, and the hash values in the structures
   are left unchanged.  NOTE: this code relies on the current layout of
   `glyphs', `used', `hash' and `x' fields of `struct glyph_row'.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
1027

1028
static void
1029
copy_row_except_pointers (struct glyph_row *to, struct glyph_row *from)
Gerd Moellmann's avatar
Gerd Moellmann committed
1030
{
1031
  enum { off = offsetof (struct glyph_row, x) };
1032

1033
  memcpy (&to->x, &from->x, sizeof *to - off);
Gerd Moellmann's avatar
Gerd Moellmann committed
1034 1035 1036 1037 1038 1039 1040 1041
}


/* Assign glyph row FROM to glyph row TO.  This works like a structure
   assignment TO = FROM, except that glyph pointers are not copied but
   exchanged between TO and FROM.  Pointers must be exchanged to avoid
   a memory leak.  */

1042
static void
1043
assign_row (struct glyph_row *to, struct glyph_row *from)
Gerd Moellmann's avatar
Gerd Moellmann committed
1044 1045 1046 1047 1048 1049 1050 1051 1052
{
  swap_glyph_pointers (to, from);
  copy_row_except_pointers (to, from);
}


/* Test whether the glyph memory of the glyph row WINDOW_ROW, which is
   a row in a window matrix, is a slice of the glyph memory of the
   glyph row FRAME_ROW which is a row in a frame glyph matrix.  Value
1053
   is true if the glyph memory of WINDOW_ROW is part of the glyph
Gerd Moellmann's avatar
Gerd Moellmann committed
1054 1055
   memory of FRAME_ROW.  */

1056
#ifdef GLYPH_DEBUG
1057

1058
static bool
Andreas Schwab's avatar
Andreas Schwab committed
1059
glyph_row_slice_p (struct glyph_row *window_row, struct glyph_row *frame_row)
Gerd Moellmann's avatar
Gerd Moellmann committed
1060 1061 1062 1063 1064 1065 1066 1067 1068
{
  struct glyph *window_glyph_start = window_row->glyphs[0];
  struct glyph *frame_glyph_start = frame_row->glyphs[0];
  struct glyph *frame_glyph_end = frame_row->glyphs[LAST_AREA];

  return (frame_glyph_start <= window_glyph_start
	  && window_glyph_start < frame_glyph_end);
}

1069
#endif /* GLYPH_DEBUG */
Gerd Moellmann's avatar
Gerd Moellmann committed
1070

1071 1072
#if 0

Gerd Moellmann's avatar
Gerd Moellmann committed
1073 1074 1075 1076 1077
/* Find the row in the window glyph matrix WINDOW_MATRIX being a slice
   of ROW in the frame matrix FRAME_MATRIX.  Value is null if no row
   in WINDOW_MATRIX is found satisfying the condition.  */

static struct glyph_row *
Andreas Schwab's avatar
Andreas Schwab committed
1078 1079
find_glyph_row_slice (struct glyph_matrix *window_matrix,
		      struct glyph_matrix *frame_matrix, int row)
Gerd Moellmann's avatar
Gerd Moellmann committed
1080 1081 1082
{
  int i;

1083
  eassert (row >= 0 && row < frame_matrix->nrows);
Gerd Moellmann's avatar
Gerd Moellmann committed
1084 1085 1086 1087 1088 1089 1090 1091 1092

  for (i = 0; i < window_matrix->nrows; ++i)
    if (glyph_row_slice_p (window_matrix->rows + i,
			   frame_matrix->rows + row))
      break;

  return i < window_matrix->nrows ? window_matrix->rows + i : 0;
}

1093
#endif /* 0 */
Gerd Moellmann's avatar
Gerd Moellmann committed
1094

1095 1096
/* Prepare ROW for display in windows W.  Desired rows are cleared
   lazily, i.e. they are only marked as to be cleared by setting their
Gerd Moellmann's avatar
Gerd Moellmann committed
1097
   enabled_p flag to zero.  When a row is to be displayed, a prior
1098 1099 1100
   call to this function really clears it.  In addition, this function
   makes sure the marginal areas of ROW are in sync with the window's
   display margins.  MODE_LINE_P non-zero means we are preparing a
1101
   glyph row for tab/header line or mode line.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
1102 1103

void
1104
prepare_desired_row (struct window *w, struct glyph_row *row, bool mode_line_p)
Gerd Moellmann's avatar
Gerd Moellmann committed
1105 1106 1107
{
  if (!row->enabled_p)
    {
1108
      bool rp = row->reversed_p;
1109

Gerd Moellmann's avatar
Gerd Moellmann committed
1110
      clear_glyph_row (row);
1111
      row->enabled_p = true;
1112
      row->reversed_p = rp;
Gerd Moellmann's avatar
Gerd Moellmann committed
1113
    }
1114 1115
  if (mode_line_p)
    {
1116
      /* Mode and header/tab lines, if displayed, never have marginal
1117
	 areas.  If we are called with MODE_LINE_P non-zero, we are
1118
	 displaying the mode/header/tab line in this window, and so the
1119
	 marginal areas of this glyph row should be eliminated.  This
1120
	 is needed when the mode/header/tab line is switched on in a
1121 1122 1123 1124 1125 1126
	 window that has display margins.  */
      if (w->left_margin_cols > 0)
	row->glyphs[TEXT_AREA] = row->glyphs[LEFT_MARGIN_AREA];
      if (w->right_margin_cols > 0)
	row->glyphs[RIGHT_MARGIN_AREA] = row->glyphs[LAST_AREA];
    }
1127
  else
1128 1129 1130 1131 1132 1133 1134 1135 1136
    {
      /* The real number of glyphs reserved for the margins is
	 recorded in the glyph matrix, and can be different from
	 window's left_margin_cols and right_margin_cols; see
	 margin_glyphs_to_reserve for when that happens.  */
      int left = w->desired_matrix->left_margin_glyphs;
      int right = w->desired_matrix->right_margin_glyphs;

      /* Make sure the marginal areas of this row are in sync with
1137
	 what the window wants, when the row actually displays text
1138
	 and not tab/header/mode line.  */
1139 1140 1141 1142 1143 1144 1145
      if (w->left_margin_cols > 0
	  && (left != row->glyphs[TEXT_AREA] - row->glyphs[LEFT_MARGIN_AREA]))
	row->glyphs[TEXT_AREA] = row->glyphs[LEFT_MARGIN_AREA] + left;
      if (w->right_margin_cols > 0
	  && (right != row->glyphs[LAST_AREA] - row->glyphs[RIGHT_MARGIN_AREA]))
	row->glyphs[RIGHT_MARGIN_AREA] = row->glyphs[LAST_AREA] - right;
    }
Gerd Moellmann's avatar
Gerd Moellmann committed
1146 1147 1148
}


1149 1150
/* Return a hash code for glyph row ROW, which may
   be from current or desired matrix of frame F.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
1151

1152
static unsigned
1153
line_hash_code (struct frame *f, struct glyph_row *row)
Gerd Moellmann's avatar
Gerd Moellmann committed
1154
{
1155
  unsigned hash = 0;
1156

Gerd Moellmann's avatar
Gerd Moellmann committed
1157 1158
  if (row->enabled_p)
    {
1159 1160 1161 1162 1163 1164 1165
      struct glyph *glyph = row->glyphs[TEXT_AREA];
      struct glyph *end = glyph + row->used[TEXT_AREA];

      while (glyph < end)
	{
	  int c = glyph->u.ch;
	  int face_id = glyph->face_id;
1166
	  if (FRAME_MUST_WRITE_SPACES (f))
1167 1168 1169 1170 1171
	    c -= SPACEGLYPH;
	  hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + c;
	  hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + face_id;
	  ++glyph;
	}
Gerd Moellmann's avatar
Gerd Moellmann committed
1172

1173 1174
      if (hash == 0)
	hash = 1;
Gerd Moellmann's avatar
Gerd Moellmann committed
1175 1176 1177 1178 1179 1180
    }

  return hash;
}


1181 1182 1183 1184
/* Return the cost of drawing line VPOS in MATRIX, which may
   be current or desired matrix of frame F.  The cost equals
   the number of characters in the line.  If must_write_spaces
   is zero, leading and trailing spaces are ignored.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
1185

1186
static int
1187
line_draw_cost (struct frame *f, struct glyph_matrix *matrix, int vpos)
Gerd Moellmann's avatar
Gerd Moellmann committed
1188 1189 1190 1191 1192 1193
{
  struct glyph_row *row = matrix->rows + vpos;
  struct glyph *beg = row->glyphs[TEXT_AREA];
  struct glyph *end = beg + row->used[TEXT_AREA];
  int len;
  Lisp_Object *glyph_table_base = GLYPH_TABLE_BASE;
1194
  ptrdiff_t glyph_table_len = GLYPH_TABLE_LENGTH;
Gerd Moellmann's avatar
Gerd Moellmann committed
1195 1196

  /* Ignore trailing and leading spaces if we can.  */
1197
  if (!FRAME_MUST_WRITE_SPACES (f))
Gerd Moellmann's avatar
Gerd Moellmann committed
1198 1199
    {
      /* Skip from the end over trailing spaces.  */
1200
      while (end > beg && CHAR_GLYPH_SPACE_P (*(end - 1)))
Gerd Moellmann's avatar
Gerd Moellmann committed
1201 1202
	--end;

1203
      /* All blank line.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
      if (end == beg)
	return 0;

      /* Skip over leading spaces.  */
      while (CHAR_GLYPH_SPACE_P (*beg))
	++beg;
    }

  /* If we don't have a glyph-table, each glyph is one character,
     so return the number of glyphs.  */
  if (glyph_table_base == 0)
    len = end - beg;
  else
    {
      /* Otherwise, scan the glyphs and accumulate their total length
	 in LEN.  */
      len = 0;
      while (beg < end)
	{
1223
	  GLYPH g;
1224

1225 1226
	  SET_GLYPH_FROM_CHAR_GLYPH (g, *beg);

1227
	  if (GLYPH_INVALID_P (g)
1228
	      || GLYPH_SIMPLE_P (glyph_table_base, glyph_table_len, g))
Gerd Moellmann's avatar
Gerd Moellmann committed
1229 1230 1231
	    len += 1;
	  else
	    len += GLYPH_LENGTH (glyph_table_base, g);
1232

Gerd Moellmann's avatar
Gerd Moellmann committed
1233 1234 1235
	  ++beg;
	}
    }
1236

Gerd Moellmann's avatar
Gerd Moellmann committed
1237 1238 1239 1240
  return len;
}


1241 1242
/* Return true if the glyph rows A and B have equal contents.
   MOUSE_FACE_P means compare the mouse_face_p flags of A and B, too.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
1243

Paul Eggert's avatar