dispnew.c 193 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-2012 Free Software Foundation, Inc.
Jim Blandy's avatar
Jim Blandy committed
4 5 6

This file is part of GNU Emacs.

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

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
18
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
Jim Blandy's avatar
Jim Blandy committed
19

20
#include <config.h>
21 22 23

#define DISPEXTERN_INLINE EXTERN_INLINE

24
#include <stdio.h>
Andreas Schwab's avatar
Andreas Schwab committed
25 26
#include <unistd.h>

27
#include "lisp.h"
Jim Blandy's avatar
Jim Blandy committed
28
#include "termchar.h"
29
/* cm.h must come after dispextern.h on Windows.  */
30 31
#include "dispextern.h"
#include "cm.h"
32
#include "character.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 40
#include "window.h"
#include "commands.h"
#include "disptab.h"
#include "indent.h"
41
#include "intervals.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
42
#include "blockinput.h"
Andreas Schwab's avatar
Andreas Schwab committed
43
#include "process.h"
Jim Blandy's avatar
Jim Blandy committed
44

Richard M. Stallman's avatar
Richard M. Stallman committed
45
#include "syssignal.h"
Jim Blandy's avatar
Jim Blandy committed
46

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

Gerd Moellmann's avatar
Gerd Moellmann committed
51
/* Include systime.h after xterm.h to avoid double inclusion of time.h.  */
52

Gerd Moellmann's avatar
Gerd Moellmann committed
53
#include "systime.h"
54 55
#include <errno.h>

56
#ifdef DISPNEW_NEEDS_STDIO_EXT
57
#include <stdio_ext.h>
Jim Blandy's avatar
Jim Blandy committed
58
#endif
59

60
#if defined (HAVE_TERM_H) && defined (GNU_LINUX)
Dave Love's avatar
Dave Love committed
61 62
#include <term.h>		/* for tgetent */
#endif
63 64 65 66

#ifdef WINDOWSNT
#include "w32.h"
#endif
Gerd Moellmann's avatar
Gerd Moellmann committed
67 68 69 70 71 72 73 74 75 76 77 78 79

/* Structure to pass dimensions around.  Used for character bounding
   boxes, glyph matrix dimensions and alike.  */

struct dim
{
  int width;
  int height;
};


/* Function prototypes.  */

80
static void update_frame_line (struct frame *, int);
Andreas Schwab's avatar
Andreas Schwab committed
81 82
static int required_matrix_height (struct window *);
static int required_matrix_width (struct window *);
83
static void adjust_frame_glyphs (struct frame *);
84
static void change_frame_size_1 (struct frame *, int, int, bool, bool, bool);
85
static void increment_row_positions (struct glyph_row *, ptrdiff_t, ptrdiff_t);
86 87 88 89 90 91 92 93
static void fill_up_frame_row_with_spaces (struct glyph_row *, int);
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_frame_message_buffer (struct frame *);
static void adjust_decode_mode_spec_buffer (struct frame *);
static void fill_up_glyph_row_with_spaces (struct glyph_row *);
94
static void clear_window_matrices (struct window *, bool);
95
static void fill_up_glyph_row_area_with_spaces (struct glyph_row *, int);
96 97
static int scrolling_window (struct window *, bool);
static bool update_window_line (struct window *, int, bool *);
98
static void mirror_make_current (struct window *, int);
99
#ifdef GLYPH_DEBUG
100 101
static void check_matrix_pointers (struct glyph_matrix *,
                                   struct glyph_matrix *);
102
#endif
103
static void mirror_line_dance (struct window *, int, int, int *, char *);
104 105 106 107
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 *);
108 109 110
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 *);
Gerd Moellmann's avatar
Gerd Moellmann committed
111 112


113 114 115 116 117
/* Redisplay preemption timers.  */

static EMACS_TIME preemption_period;
static EMACS_TIME preemption_next_check;

118
/* True upon entry to redisplay means do not assume anything about
Jim Blandy's avatar
Jim Blandy committed
119
   current contents of actual terminal frame; clear and redraw it.  */
Jim Blandy's avatar
Jim Blandy committed
120

121
bool frame_garbaged;
Jim Blandy's avatar
Jim Blandy committed
122

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

125
bool display_completed;
Jim Blandy's avatar
Jim Blandy committed
126

127
Lisp_Object Qdisplay_table, Qredisplay_dont_pause;
Gerd Moellmann's avatar
Gerd Moellmann committed
128

Jim Blandy's avatar
Jim Blandy committed
129

Gerd Moellmann's avatar
Gerd Moellmann committed
130
/* The currently selected frame.  In a single-frame version, this
131
   variable always equals the_only_frame.  */
Jim Blandy's avatar
Jim Blandy committed
132

133
Lisp_Object selected_frame;
Jim Blandy's avatar
Jim Blandy committed
134

Gerd Moellmann's avatar
Gerd Moellmann committed
135
/* A frame which is not just a mini-buffer, or 0 if there are no such
Jim Blandy's avatar
Jim Blandy committed
136
   frames.  This is usually the most recent such frame that was
Morten Welinder's avatar
Morten Welinder committed
137 138
   selected.  In a single-frame version, this variable always holds
   the address of the_only_frame.  */
Jim Blandy's avatar
Jim Blandy committed
139

Gerd Moellmann's avatar
Gerd Moellmann committed
140
struct frame *last_nonminibuf_frame;
141

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

144
static bool delayed_size_change;
Jim Blandy's avatar
Jim Blandy committed
145

Gerd Moellmann's avatar
Gerd Moellmann committed
146
/* 1 means glyph initialization has been completed at startup.  */
Jim Blandy's avatar
Jim Blandy committed
147

148
static bool glyphs_initialized_initially_p;
Jim Blandy's avatar
Jim Blandy committed
149

Gerd Moellmann's avatar
Gerd Moellmann committed
150
/* Updated window if != 0.  Set by update_window.  */
Jim Blandy's avatar
Jim Blandy committed
151

Gerd Moellmann's avatar
Gerd Moellmann committed
152
struct window *updated_window;
Jim Blandy's avatar
Jim Blandy committed
153

Gerd Moellmann's avatar
Gerd Moellmann committed
154
/* Glyph row updated in update_window_line, and area that is updated.  */
Jim Blandy's avatar
Jim Blandy committed
155

Gerd Moellmann's avatar
Gerd Moellmann committed
156 157
struct glyph_row *updated_row;
int updated_area;
Jim Blandy's avatar
Jim Blandy committed
158

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

Gerd Moellmann's avatar
Gerd Moellmann committed
161
struct glyph space_glyph;
Jim Blandy's avatar
Jim Blandy committed
162

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

166 167
static int glyph_matrix_count;
static int glyph_pool_count;
Jim Blandy's avatar
Jim Blandy committed
168

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

Gerd Moellmann's avatar
Gerd Moellmann committed
172
static struct frame *frame_matrix_frame;
Jim Blandy's avatar
Jim Blandy committed
173

174
/* True means that fonts have been loaded since the last glyph
Gerd Moellmann's avatar
Gerd Moellmann committed
175
   matrix adjustments.  Redisplay must stop, and glyph matrices must
176
   be adjusted when this flag becomes true during display.  The
Gerd Moellmann's avatar
Gerd Moellmann committed
177
   reason fonts can be loaded so late is that fonts of fontsets are
178 179 180
   loaded on demand.  Another reason is that a line contains many
   characters displayed by zero width or very narrow glyphs of
   variable-width fonts.  */
181

182
bool fonts_changed_p;
183

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

187
#ifdef GLYPH_DEBUG
Jim Blandy's avatar
Jim Blandy committed
188

189 190
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
191 192
#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
193

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
/* 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.  */

217
static uprintmax_t history_tick;
Richard M. Stallman's avatar
Richard M. Stallman committed
218

219 220
/* Add to the redisplay history how window W has been displayed.
   MSG is a trace containing the information how W's glyph matrix
221
   has been constructed.  PAUSED_P means that the update
222 223 224
   has been interrupted for pending input.  */

static void
225
add_window_display_history (struct window *w, const char *msg, bool paused_p)
226 227
{
  char *buf;
228

229 230 231 232
  if (history_idx >= REDISPLAY_HISTORY_SIZE)
    history_idx = 0;
  buf = redisplay_history[history_idx].trace;
  ++history_idx;
233

234 235 236 237
  snprintf (buf, sizeof redisplay_history[0].trace,
	    "%"pMu": window %p (`%s')%s\n%s",
	    history_tick++,
	    w,
238 239 240
	    ((BUFFERP (w->buffer)
	      && STRINGP (BVAR (XBUFFER (w->buffer), name)))
	     ? SSDATA (BVAR (XBUFFER (w->buffer), name))
241 242 243
	     : "???"),
	    paused_p ? " ***paused***" : "",
	    msg);
244 245 246 247
}


/* Add to the redisplay history that frame F has been displayed.
248
   PAUSED_P means that the update has been interrupted for
249 250 251
   pending input.  */

static void
252
add_frame_display_history (struct frame *f, bool paused_p)
253 254
{
  char *buf;
255

256 257 258 259
  if (history_idx >= REDISPLAY_HISTORY_SIZE)
    history_idx = 0;
  buf = redisplay_history[history_idx].trace;
  ++history_idx;
260

261
  sprintf (buf, "%"pMu": update frame %p%s",
262 263 264 265 266
	   history_tick++,
	   f, paused_p ? " ***paused***" : "");
}


Paul Eggert's avatar
Paul Eggert committed
267
DEFUN ("dump-redisplay-history", Fdump_redisplay_history,
268
       Sdump_redisplay_history, 0, 0, "",
269
       doc: /* Dump redisplay history to stderr.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
270
  (void)
271 272 273 274 275 276 277 278 279 280 281 282 283 284
{
  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;
}


285
#else /* not GLYPH_DEBUG */
Jim Blandy's avatar
Jim Blandy committed
286

287 288
#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
289

290
#endif /* GLYPH_DEBUG */
Jim Blandy's avatar
Jim Blandy committed
291 292


293 294 295 296 297
#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
298
void
299
__executable_start (void)
Jim Blandy's avatar
Jim Blandy committed
300
{
301
  emacs_abort ();
302
}
303
#endif
Gerd Moellmann's avatar
Gerd Moellmann committed
304 305 306 307 308 309 310 311 312 313 314

/***********************************************************************
			    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.  */

315
static struct glyph_matrix *
316
new_glyph_matrix (struct glyph_pool *pool)
Jim Blandy's avatar
Jim Blandy committed
317
{
Dmitry Antipov's avatar
Dmitry Antipov committed
318
  struct glyph_matrix *result = xzalloc (sizeof *result);
Jim Blandy's avatar
Jim Blandy committed
319

Gerd Moellmann's avatar
Gerd Moellmann committed
320 321 322
  /* Increment number of allocated matrices.  This count is used
     to detect memory leaks.  */
  ++glyph_matrix_count;
Jim Blandy's avatar
Jim Blandy committed
323

Gerd Moellmann's avatar
Gerd Moellmann committed
324 325 326
  /* Set pool and return.  */
  result->pool = pool;
  return result;
Jim Blandy's avatar
Jim Blandy committed
327 328 329
}


Gerd Moellmann's avatar
Gerd Moellmann committed
330 331 332 333 334 335
/* Free glyph matrix MATRIX.  Passing in a null MATRIX is allowed.

   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.
336

Gerd Moellmann's avatar
Gerd Moellmann committed
337 338 339 340 341
   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
342
free_glyph_matrix (struct glyph_matrix *matrix)
Jim Blandy's avatar
Jim Blandy committed
343
{
Gerd Moellmann's avatar
Gerd Moellmann committed
344 345 346 347 348 349 350
  if (matrix)
    {
      int i;

      /* Detect the case that more matrices are freed than were
	 allocated.  */
      if (--glyph_matrix_count < 0)
351
	emacs_abort ();
Gerd Moellmann's avatar
Gerd Moellmann committed
352 353 354 355 356

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

Gerd Moellmann's avatar
Gerd Moellmann committed
358 359 360 361 362
      /* Free row structures and the matrix itself.  */
      xfree (matrix->rows);
      xfree (matrix);
    }
}
Jim Blandy's avatar
Jim Blandy committed
363 364


Gerd Moellmann's avatar
Gerd Moellmann committed
365 366 367 368 369 370 371
/* 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
   area in canonical character units.  MARGIN should be an integer
   or a float.  */

static int
372
margin_glyphs_to_reserve (struct window *w, int total_glyphs, Lisp_Object margin)
Gerd Moellmann's avatar
Gerd Moellmann committed
373 374
{
  int n;
Jim Blandy's avatar
Jim Blandy committed
375

Gerd Moellmann's avatar
Gerd Moellmann committed
376
  if (NUMBERP (margin))
Jim Blandy's avatar
Jim Blandy committed
377
    {
378
      int width = XFASTINT (w->total_cols);
Gerd Moellmann's avatar
Gerd Moellmann committed
379 380
      double d = max (0, XFLOATINT (margin));
      d = min (width / 2 - 1, d);
381
      n = (int) ((double) total_glyphs / width * d);
Gerd Moellmann's avatar
Gerd Moellmann committed
382 383 384 385 386 387 388
    }
  else
    n = 0;

  return n;
}

389
/* Return true if ROW's hash value is correct.
390 391
   Optimized away if ENABLE_CHECKING is not defined.  */

392
static bool
393 394
verify_row_hash (struct glyph_row *row)
{
395
  return row->hash == row_hash (row);
396
}
Jim Blandy's avatar
Jim Blandy committed
397

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

Gerd Moellmann's avatar
Gerd Moellmann committed
401 402 403 404 405
   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.
406

Gerd Moellmann's avatar
Gerd Moellmann committed
407 408 409 410
   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.
411

Gerd Moellmann's avatar
Gerd Moellmann committed
412 413 414 415 416 417 418
   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.  */
419

Gerd Moellmann's avatar
Gerd Moellmann committed
420
static void
421
adjust_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int x, int y, struct dim dim)
Gerd Moellmann's avatar
Gerd Moellmann committed
422 423 424
{
  int i;
  int new_rows;
425 426 427
  bool marginal_areas_changed_p = 0;
  bool header_line_changed_p = 0;
  bool header_line_p = 0;
Gerd Moellmann's avatar
Gerd Moellmann committed
428
  int left = -1, right = -1;
429
  int window_width = -1, window_height = -1;
Gerd Moellmann's avatar
Gerd Moellmann committed
430

Kenichi Handa's avatar
Kenichi Handa committed
431 432
  /* 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
433 434
  if (w)
    {
Kenichi Handa's avatar
Kenichi Handa committed
435 436
      window_box (w, -1, 0, 0, &window_width, &window_height);

Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
437 438
      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
439
    }
Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
440
  matrix->header_line_p = header_line_p;
441

Kenichi Handa's avatar
Kenichi Handa committed
442 443
  /* 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
444 445 446 447
     haven't changed.  This optimization is important because preserving
     the matrix means preventing redisplay.  */
  if (matrix->pool == NULL)
    {
448 449
      left = margin_glyphs_to_reserve (w, dim.width, w->left_margin_cols);
      right = margin_glyphs_to_reserve (w, dim.width, w->right_margin_cols);
450
      eassert (left >= 0 && right >= 0);
Gerd Moellmann's avatar
Gerd Moellmann committed
451 452 453 454 455
      marginal_areas_changed_p = (left != matrix->left_margin_glyphs
				  || right != matrix->right_margin_glyphs);

      if (!marginal_areas_changed_p
	  && !fonts_changed_p
Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
456
	  && !header_line_changed_p
457 458
	  && 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
459 460 461 462 463
	  && matrix->window_height == window_height
	  && matrix->window_vscroll == w->vscroll
	  && matrix->window_width == window_width)
	return;
    }
464

Gerd Moellmann's avatar
Gerd Moellmann committed
465 466 467
  /* Enlarge MATRIX->rows if necessary.  New rows are cleared.  */
  if (matrix->rows_allocated < dim.height)
    {
468
      int old_alloc = matrix->rows_allocated;
Gerd Moellmann's avatar
Gerd Moellmann committed
469
      new_rows = dim.height - matrix->rows_allocated;
470 471 472 473
      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
474 475 476
    }
  else
    new_rows = 0;
477

Gerd Moellmann's avatar
Gerd Moellmann committed
478 479 480 481 482
  /* 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)
    {
483
      eassert (matrix->pool->glyphs);
484

Gerd Moellmann's avatar
Gerd Moellmann committed
485
      if (w)
486
	{
Gerd Moellmann's avatar
Gerd Moellmann committed
487
	  left = margin_glyphs_to_reserve (w, dim.width,
488
					   w->left_margin_cols);
Gerd Moellmann's avatar
Gerd Moellmann committed
489
	  right = margin_glyphs_to_reserve (w, dim.width,
490
					    w->right_margin_cols);
491
	}
Gerd Moellmann's avatar
Gerd Moellmann committed
492 493
      else
	left = right = 0;
494

Gerd Moellmann's avatar
Gerd Moellmann committed
495
      for (i = 0; i < dim.height; ++i)
496
	{
Gerd Moellmann's avatar
Gerd Moellmann committed
497
	  struct glyph_row *row = &matrix->rows[i];
498 499

	  row->glyphs[LEFT_MARGIN_AREA]
Gerd Moellmann's avatar
Gerd Moellmann committed
500 501 502
	    = (matrix->pool->glyphs
	       + (y + i) * matrix->pool->ncolumns
	       + x);
503

Gerd Moellmann's avatar
Gerd Moellmann committed
504 505
	  if (w == NULL
	      || row == matrix->rows + dim.height - 1
Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
506
	      || (row == matrix->rows && matrix->header_line_p))
Gerd Moellmann's avatar
Gerd Moellmann committed
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
	    {
	      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;
	    }
524
	}
525

Gerd Moellmann's avatar
Gerd Moellmann committed
526 527 528 529 530 531
      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
532 533
	 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
534 535
      if (dim.width > matrix->matrix_w
	  || new_rows
Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
536
	  || header_line_changed_p
Gerd Moellmann's avatar
Gerd Moellmann committed
537
	  || marginal_areas_changed_p)
Jim Blandy's avatar
Jim Blandy committed
538
	{
Gerd Moellmann's avatar
Gerd Moellmann committed
539 540
	  struct glyph_row *row = matrix->rows;
	  struct glyph_row *end = row + matrix->rows_allocated;
541

Gerd Moellmann's avatar
Gerd Moellmann committed
542 543 544
	  while (row < end)
	    {
	      row->glyphs[LEFT_MARGIN_AREA]
545 546
		= xnrealloc (row->glyphs[LEFT_MARGIN_AREA],
			     dim.width, sizeof (struct glyph));
547

Gerd Moellmann's avatar
Gerd Moellmann committed
548 549
	      /* The mode line never has marginal areas.  */
	      if (row == matrix->rows + dim.height - 1
Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
550
		  || (row == matrix->rows && matrix->header_line_p))
Gerd Moellmann's avatar
Gerd Moellmann committed
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
		{
		  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
570 571
	}

572
      eassert (left >= 0 && right >= 0);
Gerd Moellmann's avatar
Gerd Moellmann committed
573 574 575
      matrix->left_margin_glyphs = left;
      matrix->right_margin_glyphs = right;
    }
576

Gerd Moellmann's avatar
Gerd Moellmann committed
577 578
  /* Number of rows to be used by MATRIX.  */
  matrix->nrows = dim.height;
579
  eassert (matrix->nrows >= 0);
Gerd Moellmann's avatar
Gerd Moellmann committed
580

581
  if (w)
Gerd Moellmann's avatar
Gerd Moellmann committed
582
    {
583
      if (matrix == w->current_matrix)
Jim Blandy's avatar
Jim Blandy committed
584
	{
585 586 587 588 589 590
	  /* 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);
591

592 593 594 595
	  /* 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
596 597 598
	      && !header_line_changed_p
	      && new_rows == 0
	      && dim.width == matrix->matrix_w
599 600
	      && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
	      && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
601
	      && matrix->window_width == window_width)
602
	    {
603 604 605 606
	      /* 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)
		  {
607
		    ++i;
608 609
		    break;
		  }
610 611

	      /* Window end is invalid, if inside of the rows that
612
		 are invalidated below.  */
613 614
	      if (INTEGERP (w->window_end_vpos)
		  && XFASTINT (w->window_end_vpos) >= i)
Paul Eggert's avatar
Paul Eggert committed
615
		wset_window_end_valid (w, Qnil);
616

617 618 619 620 621 622 623 624
	      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
625
	}
626
      else if (matrix == w->desired_matrix)
Gerd Moellmann's avatar
Gerd Moellmann committed
627
	{
628 629 630 631
	  /* 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
632 633
	  for (i = 0; i < matrix->nrows; ++i)
	    matrix->rows[i].enabled_p = 0;
Jim Blandy's avatar
Jim Blandy committed
634 635
	}
    }
636 637


Gerd Moellmann's avatar
Gerd Moellmann committed
638 639 640 641 642 643 644 645 646
  /* 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
647
    {
648 649
      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
650 651 652 653 654
      matrix->window_height = window_height;
      matrix->window_width = window_width;
      matrix->window_vscroll = w->vscroll;
    }
}
Jim Blandy's avatar
Jim Blandy committed
655 656


Gerd Moellmann's avatar
Gerd Moellmann committed
657 658 659 660
/* 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).  */
661

Gerd Moellmann's avatar
Gerd Moellmann committed
662
static void
663
reverse_rows (struct glyph_matrix *matrix, int start, int end)
Gerd Moellmann's avatar
Gerd Moellmann committed
664 665
{
  int i, j;
666

Gerd Moellmann's avatar
Gerd Moellmann committed
667 668 669 670 671 672 673 674 675 676
  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;
    }
}
677 678


Gerd Moellmann's avatar
Gerd Moellmann committed
679 680 681 682
/* 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).
683

Gerd Moellmann's avatar
Gerd Moellmann committed
684 685 686 687 688 689 690 691
   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
692
rotate_matrix (struct glyph_matrix *matrix, int first, int last, int by)
Gerd Moellmann's avatar
Gerd Moellmann committed
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
{
  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
717
increment_matrix_positions (struct glyph_matrix *matrix, int start, int end,
718
			    ptrdiff_t delta, ptrdiff_t delta_bytes)
Gerd Moellmann's avatar
Gerd Moellmann committed
719 720
{
  /* Check that START and END are reasonable values.  */
721 722 723
  eassert (start >= 0 && start <= matrix->nrows);
  eassert (end >= 0 && end <= matrix->nrows);
  eassert (start <= end);
Gerd Moellmann's avatar
Gerd Moellmann committed
724 725

  for (; start < end; ++start)
726
    increment_row_positions (matrix->rows + start, delta, delta_bytes);
Gerd Moellmann's avatar
Gerd Moellmann committed
727 728 729
}


730 731
/* 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
732 733

void
734
clear_glyph_matrix_rows (struct glyph_matrix *matrix, int start, int end)
Gerd Moellmann's avatar
Gerd Moellmann committed
735
{
736 737 738
  eassert (start <= end);
  eassert (start >= 0 && start < matrix->nrows);
  eassert (end >= 0 && end <= matrix->nrows);
739

Gerd Moellmann's avatar
Gerd Moellmann committed
740
  for (; start < end; ++start)
741
    matrix->rows[start].enabled_p = 0;
Gerd Moellmann's avatar
Gerd Moellmann committed
742 743 744 745 746
}


/* Clear MATRIX.

747 748 749
   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
750

751
   Reset update hints to default values.  The only update hint
Gerd Moellmann's avatar
Gerd Moellmann committed
752 753 754
   currently present is the flag MATRIX->no_scrolling_p.  */

void
755
clear_glyph_matrix (struct glyph_matrix *matrix)
Gerd Moellmann's avatar
Gerd Moellmann committed
756 757 758
{
  if (matrix)
    {
759
      clear_glyph_matrix_rows (matrix, 0, matrix->nrows);
Gerd Moellmann's avatar
Gerd Moellmann committed
760 761 762
      matrix->no_scrolling_p = 0;
    }
}
763

Gerd Moellmann's avatar
Gerd Moellmann committed
764 765 766 767 768 769

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

774 775 776
  eassert (start <= end);
  eassert (start >= 0 && start < matrix->nrows);
  eassert (end >= 0 && end <= matrix->nrows);
777

778 779
  min_y = WINDOW_HEADER_LINE_HEIGHT (w);
  max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
780

Gerd Moellmann's avatar
Gerd Moellmann committed
781 782 783
  for (; start < end; ++start)
    {
      struct glyph_row *row = &matrix->rows[start];
784

Gerd Moellmann's avatar
Gerd Moellmann committed
785
      row->y += dy;
786
      row->visible_height = row->height;
787

Gerd Moellmann's avatar
Gerd Moellmann committed
788
      if (row->y < min_y)
789 790 791
	row->visible_height -= min_y - row->y;
      if (row->y + row->height > max_y)
	row->visible_height -= row->y + row->height - max_y;
792 793
      if (row->fringe_bitmap_periodic_p)
	row->redraw_fringe_bitmaps_p = 1;
Gerd Moellmann's avatar
Gerd Moellmann committed
794 795 796 797 798 799 800 801 802
    }
}


/* 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
803
clear_current_matrices (register struct frame *f)
Gerd Moellmann's avatar
Gerd Moellmann committed
804 805 806 807 808 809 810 811
{
  /* Clear frame current matrix, if we have one.  */
  if (f->current_matrix)
    clear_glyph_matrix (f->current_matrix);

  /* 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.  */
812 813
  if (WINDOWP (f->menu_bar_window))
    clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
Gerd Moellmann's avatar
Gerd Moellmann committed
814

815
  /* Clear the matrix of the tool-bar window, if any.  */
816 817
  if (WINDOWP (f->tool_bar_window))
    clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
Gerd Moellmann's avatar
Gerd Moellmann committed
818 819

  /* Clear current window matrices.  */
820
  eassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
Gerd Moellmann's avatar
Gerd Moellmann committed
821 822 823 824 825 826 827
  clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 0);
}


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

void
828
clear_desired_matrices (register struct frame *f)
Gerd Moellmann's avatar
Gerd Moellmann committed
829 830 831
{
  if (f->desired_matrix)
    clear_glyph_matrix (f->desired_matrix);
832

833 834
  if (WINDOWP (f->menu_bar_window))
    clear_glyph_matrix (XWINDOW (f->menu_bar_window)->desired_matrix);
Gerd Moellmann's avatar
Gerd Moellmann committed
835

836 837
  if (WINDOWP (f->tool_bar_window))
    clear_glyph_matrix (XWINDOW (f->tool_bar_window)->desired_matrix);
Gerd Moellmann's avatar
Gerd Moellmann committed
838 839

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


845 846
/* 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
847 848

static void
849
clear_window_matrices (struct window *w, bool desired_p)
Gerd Moellmann's avatar
Gerd Moellmann committed
850 851 852
{
  while (w)
    {
853
      if (!NILP (w->hchild))
Gerd Moellmann's avatar
Gerd Moellmann committed
854
	{
855 856
	  eassert (WINDOWP (w->hchild));
	  clear_window_matrices (XWINDOW (w->hchild), desired_p);
Gerd Moellmann's avatar
Gerd Moellmann committed
857
	}
858
      else if (!NILP (w->vchild))
Gerd Moellmann's avatar
Gerd Moellmann committed
859
	{
860 861
	  eassert (WINDOWP (w->vchild));
	  clear_window_matrices (XWINDOW (w->vchild), desired_p);
Gerd Moellmann's avatar
Gerd Moellmann committed
862 863 864 865 866 867 868 869
	}
      else
	{
	  if (desired_p)
	    clear_glyph_matrix (w->desired_matrix);
	  else
	    {
	      clear_glyph_matrix (w->current_matrix);
Paul Eggert's avatar
Paul Eggert committed
870
	      wset_window_end_valid (w, Qnil);
Gerd Moellmann's avatar
Gerd Moellmann committed
871 872 873
	    }
	}

874
      w = NILP (w->next) ? 0 : XWINDOW (w->next);
Gerd Moellmann's avatar
Gerd Moellmann committed
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
    }
}


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

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

/* Clear glyph row ROW.  Do it in a way that makes it robust against
   changes in the glyph_row structure, i.e. addition or removal of
   structure members.  */

890 891
static struct glyph_row null_row;

Gerd Moellmann's avatar
Gerd Moellmann committed
892
void
893
clear_glyph_row (struct glyph_row *row)
Gerd Moellmann's avatar
Gerd Moellmann committed
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
{
  struct glyph *p[1 + LAST_AREA];

  /* Save pointers.  */
  p[LEFT_MARGIN_AREA] = row->glyphs[LEFT_MARGIN_AREA];
  p[TEXT_AREA] = row->glyphs[TEXT_AREA];
  p[RIGHT_MARGIN_AREA] = row->glyphs[RIGHT_MARGIN_AREA];
  p[LAST_AREA] = row->glyphs[LAST_AREA];

  /* Clear.  */
  *row = null_row;

  /* Restore pointers.  */
  row->glyphs[LEFT_MARGIN_AREA] = p[LEFT_MARGIN_AREA];
  row->glyphs[TEXT_AREA] = p[TEXT_AREA];
  row->glyphs[RIGHT_MARGIN_AREA] = p[RIGHT_MARGIN_AREA];
  row->glyphs[LAST_AREA] = p[LAST_AREA];
911 912 913 914 915 916

#if 0 /* At some point, some bit-fields of struct glyph were not set,
	 which made glyphs unequal when compared with GLYPH_EQUAL_P.
	 Redisplay outputs such glyphs, and flickering effects were
	 the result.  This also depended on the contents of memory
	 returned by xmalloc.  If flickering happens again, activate
917
	 the code below.  If the flickering is gone with that, chances
918
	 are that the flickering has the same reason as here.  */
919
  memset (p[0], 0, (char *) p[LAST_AREA] - (char *) p[0]);
920
#endif
Gerd Moellmann's avatar
Gerd Moellmann committed
921 922 923 924 925 926 927
}


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

void
928
blank_row (struct window *w, struct glyph_row *row, int y)
Gerd Moellmann's avatar
Gerd Moellmann committed
929 930
{</