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

3 4
Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation,
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 <http://www.gnu.org/licenses/>.  */
Jim Blandy's avatar
Jim Blandy committed
20

21
#include <config.h>
22

23
#include "sysstdio.h"
Andreas Schwab's avatar
Andreas Schwab committed
24 25
#include <unistd.h>

26
#include "lisp.h"
Jim Blandy's avatar
Jim Blandy committed
27
#include "termchar.h"
28
/* cm.h must come after dispextern.h on Windows.  */
29 30
#include "dispextern.h"
#include "cm.h"
31
#include "character.h"
32
#include "buffer.h"
33
#include "keyboard.h"
Jim Blandy's avatar
Jim Blandy committed
34
#include "frame.h"
35
#include "termhooks.h"
Jim Blandy's avatar
Jim Blandy committed
36 37 38 39
#include "window.h"
#include "commands.h"
#include "disptab.h"
#include "indent.h"
40
#include "intervals.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
41
#include "blockinput.h"
Andreas Schwab's avatar
Andreas Schwab committed
42
#include "process.h"
Jim Blandy's avatar
Jim Blandy committed
43

Richard M. Stallman's avatar
Richard M. Stallman committed
44
#include "syssignal.h"
Paul Eggert's avatar
Paul Eggert committed
45
#include "tparam.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>

Paul Eggert's avatar
Paul Eggert committed
53
#include <fpending.h>
54
#include <timespec.h>
55

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

struct dim
{
  int width;
  int height;
};


/* Function prototypes.  */

72
static void update_frame_line (struct frame *, int);
Andreas Schwab's avatar
Andreas Schwab committed
73 74
static int required_matrix_height (struct window *);
static int required_matrix_width (struct window *);
75
static void change_frame_size_1 (struct frame *, int, int, bool, bool, bool);
76
static void increment_row_positions (struct glyph_row *, ptrdiff_t, ptrdiff_t);
77 78 79 80 81 82
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 *);
83
static void clear_window_matrices (struct window *, bool);
84
static void fill_up_glyph_row_area_with_spaces (struct glyph_row *, int);
85 86
static int scrolling_window (struct window *, bool);
static bool update_window_line (struct window *, int, bool *);
87
static void mirror_make_current (struct window *, int);
88
#ifdef GLYPH_DEBUG
89 90
static void check_matrix_pointers (struct glyph_matrix *,
                                   struct glyph_matrix *);
91
#endif
92
static void mirror_line_dance (struct window *, int, int, int *, char *);
93 94 95 96
static bool update_window_tree (struct window *, bool);
static bool update_window (struct window *, bool);
static bool update_frame_1 (struct frame *, bool, bool);
static bool scrolling (struct frame *);
97 98 99
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 *);
100
static void set_window_update_flags (struct window *w, bool on_p);
Gerd Moellmann's avatar
Gerd Moellmann committed
101

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

104
bool display_completed;
Jim Blandy's avatar
Jim Blandy committed
105

106
Lisp_Object Qdisplay_table, Qredisplay_dont_pause;
Gerd Moellmann's avatar
Gerd Moellmann committed
107

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

110
static bool delayed_size_change;
Jim Blandy's avatar
Jim Blandy committed
111

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

Gerd Moellmann's avatar
Gerd Moellmann committed
114
struct glyph space_glyph;
Jim Blandy's avatar
Jim Blandy committed
115

116 117
#if defined GLYPH_DEBUG && defined ENABLE_CHECKING

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

121 122
static int glyph_matrix_count;
static int glyph_pool_count;
Jim Blandy's avatar
Jim Blandy committed
123

124 125
#endif /* GLYPH_DEBUG and ENABLE_CHECKING */

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

Gerd Moellmann's avatar
Gerd Moellmann committed
129
static struct frame *frame_matrix_frame;
Jim Blandy's avatar
Jim Blandy committed
130

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

134
#ifdef GLYPH_DEBUG
Jim Blandy's avatar
Jim Blandy committed
135

136 137
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
138 139
#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
140

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
/* 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.  */

164
static uprintmax_t history_tick;
Richard M. Stallman's avatar
Richard M. Stallman committed
165

166 167
/* Add to the redisplay history how window W has been displayed.
   MSG is a trace containing the information how W's glyph matrix
168
   has been constructed.  PAUSED_P means that the update
169 170 171
   has been interrupted for pending input.  */

static void
172
add_window_display_history (struct window *w, const char *msg, bool paused_p)
173 174
{
  char *buf;
Paul Eggert's avatar
Paul Eggert committed
175
  void *ptr = w;
176

177 178 179 180
  if (history_idx >= REDISPLAY_HISTORY_SIZE)
    history_idx = 0;
  buf = redisplay_history[history_idx].trace;
  ++history_idx;
181

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


/* Add to the redisplay history that frame F has been displayed.
196
   PAUSED_P means that the update has been interrupted for
197 198 199
   pending input.  */

static void
200
add_frame_display_history (struct frame *f, bool paused_p)
201 202
{
  char *buf;
Paul Eggert's avatar
Paul Eggert committed
203
  void *ptr = f;
204

205 206 207 208
  if (history_idx >= REDISPLAY_HISTORY_SIZE)
    history_idx = 0;
  buf = redisplay_history[history_idx].trace;
  ++history_idx;
209

210
  sprintf (buf, "%"pMu": update frame %p%s",
211
	   history_tick++,
Paul Eggert's avatar
Paul Eggert committed
212
	   ptr, paused_p ? " ***paused***" : "");
213 214 215
}


Paul Eggert's avatar
Paul Eggert committed
216
DEFUN ("dump-redisplay-history", Fdump_redisplay_history,
217
       Sdump_redisplay_history, 0, 0, "",
218
       doc: /* Dump redisplay history to stderr.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
219
  (void)
220 221 222 223 224 225 226 227 228 229 230 231 232 233
{
  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;
}


234
#else /* not GLYPH_DEBUG */
Jim Blandy's avatar
Jim Blandy committed
235

236 237
#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
238

239
#endif /* GLYPH_DEBUG */
Jim Blandy's avatar
Jim Blandy committed
240 241


242 243 244 245 246
#if (defined PROFILING \
     && (defined __FreeBSD__ || defined GNU_LINUX || defined __MINGW32__) \
     && !HAVE___EXECUTABLE_START)
/* 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
247
void
248
__executable_start (void)
Jim Blandy's avatar
Jim Blandy committed
249
{
250
  emacs_abort ();
251
}
252
#endif
Gerd Moellmann's avatar
Gerd Moellmann committed
253 254 255 256 257 258 259 260 261 262 263

/***********************************************************************
			    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.  */

264
static struct glyph_matrix *
265
new_glyph_matrix (struct glyph_pool *pool)
Jim Blandy's avatar
Jim Blandy committed
266
{
Dmitry Antipov's avatar
Dmitry Antipov committed
267
  struct glyph_matrix *result = xzalloc (sizeof *result);
Jim Blandy's avatar
Jim Blandy committed
268

269
#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
Gerd Moellmann's avatar
Gerd Moellmann committed
270 271 272
  /* Increment number of allocated matrices.  This count is used
     to detect memory leaks.  */
  ++glyph_matrix_count;
273
#endif
Jim Blandy's avatar
Jim Blandy committed
274

Gerd Moellmann's avatar
Gerd Moellmann committed
275 276 277
  /* Set pool and return.  */
  result->pool = pool;
  return result;
Jim Blandy's avatar
Jim Blandy committed
278 279 280
}


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

283 284 285 286
   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.
287

Gerd Moellmann's avatar
Gerd Moellmann committed
288 289 290 291 292
   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
293
free_glyph_matrix (struct glyph_matrix *matrix)
Jim Blandy's avatar
Jim Blandy committed
294
{
Gerd Moellmann's avatar
Gerd Moellmann committed
295 296 297 298
  if (matrix)
    {
      int i;

299
#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
Gerd Moellmann's avatar
Gerd Moellmann committed
300 301
      /* Detect the case that more matrices are freed than were
	 allocated.  */
302 303 304
      --glyph_matrix_count;
      eassert (glyph_matrix_count >= 0);
#endif
Gerd Moellmann's avatar
Gerd Moellmann committed
305 306 307 308 309

      /* 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]);
310

Gerd Moellmann's avatar
Gerd Moellmann committed
311 312 313 314 315
      /* Free row structures and the matrix itself.  */
      xfree (matrix->rows);
      xfree (matrix);
    }
}
Jim Blandy's avatar
Jim Blandy committed
316 317


Gerd Moellmann's avatar
Gerd Moellmann committed
318 319 320
/* 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
321
   area in canonical character units.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
322 323

static int
324
margin_glyphs_to_reserve (struct window *w, int total_glyphs, int margin)
Gerd Moellmann's avatar
Gerd Moellmann committed
325
{
326
  if (margin > 0)
Jim Blandy's avatar
Jim Blandy committed
327
    {
328
      int width = w->total_cols;
329
      double d = max (0, margin);
Gerd Moellmann's avatar
Gerd Moellmann committed
330
      d = min (width / 2 - 1, d);
331
      return (int) ((double) total_glyphs / width * d);
Gerd Moellmann's avatar
Gerd Moellmann committed
332
    }
333
  return 0;
Gerd Moellmann's avatar
Gerd Moellmann committed
334 335
}

336
/* Return true if ROW's hash value is correct.
337 338
   Optimized away if ENABLE_CHECKING is not defined.  */

339
static bool
340 341
verify_row_hash (struct glyph_row *row)
{
342
  return row->hash == row_hash (row);
343
}
Jim Blandy's avatar
Jim Blandy committed
344

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

Gerd Moellmann's avatar
Gerd Moellmann committed
348 349 350 351 352
   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.
353

Gerd Moellmann's avatar
Gerd Moellmann committed
354 355 356 357
   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.
358

Gerd Moellmann's avatar
Gerd Moellmann committed
359 360 361 362 363 364 365
   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.  */
366

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

Kenichi Handa's avatar
Kenichi Handa committed
378 379
  /* 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
380 381
  if (w)
    {
382
      window_box (w, ANY_AREA, 0, 0, &window_width, &window_height);
Kenichi Handa's avatar
Kenichi Handa committed
383

Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
384 385
      header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
      header_line_changed_p = header_line_p != matrix->header_line_p;
Gerd Moellmann's avatar
Gerd Moellmann committed
386
    }
Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
387
  matrix->header_line_p = header_line_p;
388

Kenichi Handa's avatar
Kenichi Handa committed
389 390
  /* 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
391 392 393 394
     haven't changed.  This optimization is important because preserving
     the matrix means preventing redisplay.  */
  if (matrix->pool == NULL)
    {
395 396
      left = margin_glyphs_to_reserve (w, dim.width, w->left_margin_cols);
      right = margin_glyphs_to_reserve (w, dim.width, w->right_margin_cols);
397
      eassert (left >= 0 && right >= 0);
Gerd Moellmann's avatar
Gerd Moellmann committed
398 399 400 401
      marginal_areas_changed_p = (left != matrix->left_margin_glyphs
				  || right != matrix->right_margin_glyphs);

      if (!marginal_areas_changed_p
402
	  && !XFRAME (w->frame)->fonts_changed
Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
403
	  && !header_line_changed_p
404 405
	  && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
	  && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
Gerd Moellmann's avatar
Gerd Moellmann committed
406 407 408 409 410
	  && matrix->window_height == window_height
	  && matrix->window_vscroll == w->vscroll
	  && matrix->window_width == window_width)
	return;
    }
411

Gerd Moellmann's avatar
Gerd Moellmann committed
412 413 414
  /* Enlarge MATRIX->rows if necessary.  New rows are cleared.  */
  if (matrix->rows_allocated < dim.height)
    {
415
      int old_alloc = matrix->rows_allocated;
Gerd Moellmann's avatar
Gerd Moellmann committed
416
      new_rows = dim.height - matrix->rows_allocated;
417 418 419 420
      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
421 422 423
    }
  else
    new_rows = 0;
424

Gerd Moellmann's avatar
Gerd Moellmann committed
425 426 427 428 429
  /* 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)
    {
430
      eassert (matrix->pool->glyphs);
431

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

Gerd Moellmann's avatar
Gerd Moellmann committed
442
      for (i = 0; i < dim.height; ++i)
443
	{
Gerd Moellmann's avatar
Gerd Moellmann committed
444
	  struct glyph_row *row = &matrix->rows[i];
445 446

	  row->glyphs[LEFT_MARGIN_AREA]
Gerd Moellmann's avatar
Gerd Moellmann committed
447 448 449
	    = (matrix->pool->glyphs
	       + (y + i) * matrix->pool->ncolumns
	       + x);
450

Gerd Moellmann's avatar
Gerd Moellmann committed
451 452
	  if (w == NULL
	      || row == matrix->rows + dim.height - 1
Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
453
	      || (row == matrix->rows && matrix->header_line_p))
Gerd Moellmann's avatar
Gerd Moellmann committed
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
	    {
	      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;
	    }
471
	}
472

Gerd Moellmann's avatar
Gerd Moellmann committed
473 474 475 476 477 478
      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
479 480
	 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
481 482
      if (dim.width > matrix->matrix_w
	  || new_rows
Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
483
	  || header_line_changed_p
Gerd Moellmann's avatar
Gerd Moellmann committed
484
	  || marginal_areas_changed_p)
Jim Blandy's avatar
Jim Blandy committed
485
	{
Gerd Moellmann's avatar
Gerd Moellmann committed
486 487
	  struct glyph_row *row = matrix->rows;
	  struct glyph_row *end = row + matrix->rows_allocated;
488

Gerd Moellmann's avatar
Gerd Moellmann committed
489 490 491
	  while (row < end)
	    {
	      row->glyphs[LEFT_MARGIN_AREA]
492 493
		= xnrealloc (row->glyphs[LEFT_MARGIN_AREA],
			     dim.width, sizeof (struct glyph));
494

Gerd Moellmann's avatar
Gerd Moellmann committed
495 496
	      /* The mode line never has marginal areas.  */
	      if (row == matrix->rows + dim.height - 1
Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
497
		  || (row == matrix->rows && matrix->header_line_p))
Gerd Moellmann's avatar
Gerd Moellmann committed
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
		{
		  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
517 518
	}

519
      eassert (left >= 0 && right >= 0);
Gerd Moellmann's avatar
Gerd Moellmann committed
520 521 522
      matrix->left_margin_glyphs = left;
      matrix->right_margin_glyphs = right;
    }
523

Gerd Moellmann's avatar
Gerd Moellmann committed
524 525
  /* Number of rows to be used by MATRIX.  */
  matrix->nrows = dim.height;
526
  eassert (matrix->nrows >= 0);
Gerd Moellmann's avatar
Gerd Moellmann committed
527

528
  if (w)
Gerd Moellmann's avatar
Gerd Moellmann committed
529
    {
530
      if (matrix == w->current_matrix)
Jim Blandy's avatar
Jim Blandy committed
531
	{
532 533 534 535 536 537
	  /* 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);
538

539 540 541 542
	  /* 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
543 544 545
	      && !header_line_changed_p
	      && new_rows == 0
	      && dim.width == matrix->matrix_w
546 547
	      && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
	      && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
548
	      && matrix->window_width == window_width)
549
	    {
550 551 552 553
	      /* 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)
		  {
554
		    ++i;
555 556
		    break;
		  }
557 558

	      /* Window end is invalid, if inside of the rows that
559
		 are invalidated below.  */
560
	      if (w->window_end_vpos >= i)
561
		w->window_end_valid = 0;
562

563 564 565 566 567 568 569 570
	      while (i < matrix->nrows)
		matrix->rows[i++].enabled_p = 0;
	    }
	  else
	    {
	      for (i = 0; i < matrix->nrows; ++i)
		matrix->rows[i].enabled_p = 0;
	    }
Gerd Moellmann's avatar
Gerd Moellmann committed
571
	}
572
      else if (matrix == w->desired_matrix)
Gerd Moellmann's avatar
Gerd Moellmann committed
573
	{
574 575 576 577
	  /* 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
578 579
	  for (i = 0; i < matrix->nrows; ++i)
	    matrix->rows[i].enabled_p = 0;
Jim Blandy's avatar
Jim Blandy committed
580 581
	}
    }
582 583


Gerd Moellmann's avatar
Gerd Moellmann committed
584 585 586 587 588 589 590 591 592
  /* 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
593
    {
594 595
      matrix->window_left_col = WINDOW_LEFT_EDGE_COL (w);
      matrix->window_top_line = WINDOW_TOP_EDGE_LINE (w);
Gerd Moellmann's avatar
Gerd Moellmann committed
596 597 598 599 600
      matrix->window_height = window_height;
      matrix->window_width = window_width;
      matrix->window_vscroll = w->vscroll;
    }
}
Jim Blandy's avatar
Jim Blandy committed
601 602


Gerd Moellmann's avatar
Gerd Moellmann committed
603 604 605 606
/* 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).  */
607

Gerd Moellmann's avatar
Gerd Moellmann committed
608
static void
609
reverse_rows (struct glyph_matrix *matrix, int start, int end)
Gerd Moellmann's avatar
Gerd Moellmann committed
610 611
{
  int i, j;
612

Gerd Moellmann's avatar
Gerd Moellmann committed
613 614 615 616 617 618 619 620 621 622
  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;
    }
}
623 624


Gerd Moellmann's avatar
Gerd Moellmann committed
625 626 627 628
/* 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).
629

Gerd Moellmann's avatar
Gerd Moellmann committed
630 631 632 633 634 635 636 637
   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
638
rotate_matrix (struct glyph_matrix *matrix, int first, int last, int by)
Gerd Moellmann's avatar
Gerd Moellmann committed
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
{
  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
663
increment_matrix_positions (struct glyph_matrix *matrix, int start, int end,
664
			    ptrdiff_t delta, ptrdiff_t delta_bytes)
Gerd Moellmann's avatar
Gerd Moellmann committed
665 666
{
  /* Check that START and END are reasonable values.  */
667 668 669
  eassert (start >= 0 && start <= matrix->nrows);
  eassert (end >= 0 && end <= matrix->nrows);
  eassert (start <= end);
Gerd Moellmann's avatar
Gerd Moellmann committed
670 671

  for (; start < end; ++start)
672
    increment_row_positions (matrix->rows + start, delta, delta_bytes);
Gerd Moellmann's avatar
Gerd Moellmann committed
673 674 675
}


676 677
/* 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
678 679

void
680
clear_glyph_matrix_rows (struct glyph_matrix *matrix, int start, int end)
Gerd Moellmann's avatar
Gerd Moellmann committed
681
{
682 683 684
  eassert (start <= end);
  eassert (start >= 0 && start < matrix->nrows);
  eassert (end >= 0 && end <= matrix->nrows);
685

Gerd Moellmann's avatar
Gerd Moellmann committed
686
  for (; start < end; ++start)
687
    matrix->rows[start].enabled_p = 0;
Gerd Moellmann's avatar
Gerd Moellmann committed
688 689 690 691 692
}


/* Clear MATRIX.

693 694 695
   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
696

697
   Reset update hints to default values.  The only update hint
Gerd Moellmann's avatar
Gerd Moellmann committed
698 699 700
   currently present is the flag MATRIX->no_scrolling_p.  */

void
701
clear_glyph_matrix (struct glyph_matrix *matrix)
Gerd Moellmann's avatar
Gerd Moellmann committed
702 703 704
{
  if (matrix)
    {
705
      clear_glyph_matrix_rows (matrix, 0, matrix->nrows);
Gerd Moellmann's avatar
Gerd Moellmann committed
706 707 708
      matrix->no_scrolling_p = 0;
    }
}
709

Gerd Moellmann's avatar
Gerd Moellmann committed
710 711 712 713 714 715

/* 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
716
shift_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int start, int end, int dy)
Gerd Moellmann's avatar
Gerd Moellmann committed
717 718
{
  int min_y, max_y;
719

720 721 722
  eassert (start <= end);
  eassert (start >= 0 && start < matrix->nrows);
  eassert (end >= 0 && end <= matrix->nrows);
723

724 725
  min_y = WINDOW_HEADER_LINE_HEIGHT (w);
  max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
726

Gerd Moellmann's avatar
Gerd Moellmann committed
727 728 729
  for (; start < end; ++start)
    {
      struct glyph_row *row = &matrix->rows[start];
730

Gerd Moellmann's avatar
Gerd Moellmann committed
731
      row->y += dy;
732
      row->visible_height = row->height;
733

Gerd Moellmann's avatar
Gerd Moellmann committed
734
      if (row->y < min_y)
735 736 737
	row->visible_height -= min_y - row->y;
      if (row->y + row->height > max_y)
	row->visible_height -= row->y + row->height - max_y;
738 739
      if (row->fringe_bitmap_periodic_p)
	row->redraw_fringe_bitmaps_p = 1;
Gerd Moellmann's avatar
Gerd Moellmann committed
740 741 742 743 744 745 746 747 748
    }
}


/* 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
749
clear_current_matrices (register struct frame *f)
Gerd Moellmann's avatar
Gerd Moellmann committed
750 751 752 753 754
{
  /* Clear frame current matrix, if we have one.  */
  if (f->current_matrix)
    clear_glyph_matrix (f->current_matrix);

755
#if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
Gerd Moellmann's avatar
Gerd Moellmann committed
756 757 758
  /* 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.  */
759 760
  if (WINDOWP (f->menu_bar_window))
    clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
761
#endif
Gerd Moellmann's avatar
Gerd Moellmann committed
762

763
#if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
764
  /* Clear the matrix of the tool-bar window, if any.  */
765 766
  if (WINDOWP (f->tool_bar_window))
    clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
767
#endif
Gerd Moellmann's avatar
Gerd Moellmann committed
768 769

  /* Clear current window matrices.  */
770
  eassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
Gerd Moellmann's avatar
Gerd Moellmann committed
771 772 773 774 775 776 777
  clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 0);
}


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

void
778
clear_desired_matrices (register struct frame *f)
Gerd Moellmann's avatar
Gerd Moellmann committed
779 780 781
{
  if (f->desired_matrix)
    clear_glyph_matrix (f->desired_matrix);
782

783
#if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
784 785
  if (WINDOWP (f->menu_bar_window))
    clear_glyph_matrix (XWINDOW (f->menu_bar_window)->desired_matrix);
786
#endif
Gerd Moellmann's avatar
Gerd Moellmann committed
787

788
#if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
789 790
  if (WINDOWP (f->tool_bar_window))
    clear_glyph_matrix (XWINDOW (f->tool_bar_window)->desired_matrix);
791
#endif
Gerd Moellmann's avatar
Gerd Moellmann committed
792 793

  /* Do it for window matrices.  */
794
  eassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
Gerd Moellmann's avatar
Gerd Moellmann committed
795 796 797 798
  clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
}


799 800
/* 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
801 802

static void
803
clear_window_matrices (struct window *w, bool desired_p)
Gerd Moellmann's avatar
Gerd Moellmann committed
804 805 806
{
  while (w)
    {
807 808
      if (WINDOWP (w->contents))
	clear_window_matrices (XWINDOW (w->contents), desired_p);
Gerd Moellmann's avatar
Gerd Moellmann committed
809 810 811 812 813 814 815
      else
	{
	  if (desired_p)
	    clear_glyph_matrix (w->desired_matrix);
	  else
	    {
	      clear_glyph_matrix (w->current_matrix);
816
	      w->window_end_valid = 0;
Gerd Moellmann's avatar
Gerd Moellmann committed
817 818 819
	    }
	}

820
      w = NILP (w->next) ? 0 : XWINDOW (w->next);
Gerd Moellmann's avatar
Gerd Moellmann committed
821 822 823 824 825 826 827 828 829 830 831
    }
}


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

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

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

Gerd Moellmann's avatar
Gerd Moellmann committed
835
void
836
clear_glyph_row (struct glyph_row *row)
Gerd Moellmann's avatar
Gerd Moellmann committed
837
{
838
  enum { off = offsetof (struct glyph_row, used) };
839 840

  /* Zero everything except pointers in `glyphs'.  */
841
  memset (row->used, 0, sizeof *row - off);
Gerd Moellmann's avatar
Gerd Moellmann committed
842 843 844 845 846 847 848
}


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

void
849
blank_row (struct window *w, struct glyph_row *row, int y)
Gerd Moellmann's avatar
Gerd Moellmann committed
850 851
{
  int min_y, max_y;
852

853 854
  min_y = WINDOW_HEADER_LINE_HEIGHT (w);
  max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
855

Gerd Moellmann's avatar
Gerd Moellmann committed
856 857
  clear_glyph_row (row);
  row->y = y;
858
  row->ascent = row->phys_ascent = 0;
859
  row->height = row->phys_height = FRAME_LINE_HEIGHT (XFRAME (w->frame));
860
  row->visible_height = row->height;
861

Gerd Moellmann's avatar
Gerd Moellmann committed
862
  if (row->y < min_y)
863 864 865
    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
866 867 868 869 870 871 872 873 874 875 876

  row->enabled_p = 1;
}


/* 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.  */

877
static void
878
increment_row_positions (struct glyph_row *row,
879
			 ptrdiff_t delta, ptrdiff_t delta_bytes)
Gerd Moellmann's avatar
Gerd Moellmann committed
880 881 882 883 884 885 886 887
{
  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;
888 889 890 891
  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
892

893 894 895
  if (!row->enabled_p)
    return;

Gerd Moellmann's avatar
Gerd Moellmann committed
896 897 898 899 900 901 902 903 904 905 906 907 908 909
  /* 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;
}


910
#if 0
Gerd Moellmann's avatar
Gerd Moellmann committed
911 912 913 914 915
/* 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