dispnew.c 177 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Updating of data structures for redisplay.
2
   Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 1999
3
       Free Software Foundation, Inc.
Jim Blandy's avatar
Jim Blandy committed
4 5 6 7 8

This file is part of GNU Emacs.

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

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
along with GNU Emacs; see the file COPYING.  If not, write to
19 20
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */
Jim Blandy's avatar
Jim Blandy committed
21

22
#include <config.h>
23
#include <signal.h>
24
#include <stdio.h>
Jim Blandy's avatar
Jim Blandy committed
25 26
#include <ctype.h>

Andreas Schwab's avatar
Andreas Schwab committed
27 28 29 30
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

31
#include "lisp.h"
Jim Blandy's avatar
Jim Blandy committed
32 33
#include "termchar.h"
#include "termopts.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
34
#include "termhooks.h"
35
/* cm.h must come after dispextern.h on Windows.  */
36 37
#include "dispextern.h"
#include "cm.h"
Jim Blandy's avatar
Jim Blandy committed
38
#include "buffer.h"
39
#include "charset.h"
Jim Blandy's avatar
Jim Blandy committed
40
#include "frame.h"
Jim Blandy's avatar
Jim Blandy committed
41 42 43 44
#include "window.h"
#include "commands.h"
#include "disptab.h"
#include "indent.h"
45
#include "intervals.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
46
#include "blockinput.h"
Andreas Schwab's avatar
Andreas Schwab committed
47 48
#include "process.h"
#include "keyboard.h"
Jim Blandy's avatar
Jim Blandy committed
49

50 51 52
/* I don't know why DEC Alpha OSF1 fail to compile this file if we
   include the following file.  */
/* #include "systty.h" */
Richard M. Stallman's avatar
Richard M. Stallman committed
53
#include "syssignal.h"
Jim Blandy's avatar
Jim Blandy committed
54

Jim Blandy's avatar
Jim Blandy committed
55 56
#ifdef HAVE_X_WINDOWS
#include "xterm.h"
Gerd Moellmann's avatar
Gerd Moellmann committed
57
#endif /* HAVE_X_WINDOWS */
Jim Blandy's avatar
Jim Blandy committed
58

59 60 61 62
#ifdef HAVE_NTGUI
#include "w32term.h"
#endif /* HAVE_NTGUI */

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

Gerd Moellmann's avatar
Gerd Moellmann committed
65
#include "systime.h"
66 67
#include <errno.h>

Gerd Moellmann's avatar
Gerd Moellmann committed
68 69 70 71 72 73
/* To get the prototype for `sleep'.  */

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

Jim Blandy's avatar
Jim Blandy committed
74 75 76 77
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

/* Get number of chars of output now in the buffer of a stdio stream.
Gerd Moellmann's avatar
Gerd Moellmann committed
78 79 80
   This ought to be built in in stdio, but it isn't.  Some s- files
   override this because their stdio internals differ.  */

81
#ifdef __GNU_LIBRARY__
Gerd Moellmann's avatar
Gerd Moellmann committed
82 83 84 85 86

/* The s- file might have overridden the definition with one that
   works for the system's C library.  But we are using the GNU C
   library, so this is the right definition for every system.  */

87 88 89
#ifdef GNU_LIBRARY_PENDING_OUTPUT_COUNT
#define PENDING_OUTPUT_COUNT GNU_LIBRARY_PENDING_OUTPUT_COUNT
#else
90
#undef	PENDING_OUTPUT_COUNT
91
#define	PENDING_OUTPUT_COUNT(FILE) ((FILE)->__bufp - (FILE)->__buffer)
92 93
#endif
#else /* not __GNU_LIBRARY__ */
94 95 96 97
#if !defined (PENDING_OUTPUT_COUNT) && HAVE_STDIO_EXT_H && HAVE___FPENDING
#include <stdio_ext.h>
#define PENDING_OUTPUT_COUNT(FILE) __fpending (FILE)
#endif
98
#ifndef PENDING_OUTPUT_COUNT
Jim Blandy's avatar
Jim Blandy committed
99 100
#define PENDING_OUTPUT_COUNT(FILE) ((FILE)->_ptr - (FILE)->_base)
#endif
Gerd Moellmann's avatar
Gerd Moellmann committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
#endif /* not __GNU_LIBRARY__ */


/* Structure to pass dimensions around.  Used for character bounding
   boxes, glyph matrix dimensions and alike.  */

struct dim
{
  int width;
  int height;
};


/* Function prototypes.  */

116 117
static void redraw_overlapping_rows P_ ((struct window *, int));
static void redraw_overlapped_rows P_ ((struct window *, int));
Gerd Moellmann's avatar
Gerd Moellmann committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
static int count_blanks P_ ((struct glyph *, int));
static int count_match P_ ((struct glyph *, struct glyph *,
			    struct glyph *, struct glyph *));
static unsigned line_draw_cost P_ ((struct glyph_matrix *, int));
static void update_frame_line P_ ((struct frame *, int));
static struct dim allocate_matrices_for_frame_redisplay
     P_ ((Lisp_Object, int, int, struct dim, int, int *));
static void allocate_matrices_for_window_redisplay P_ ((struct window *,
							struct dim));
static int realloc_glyph_pool P_ ((struct glyph_pool *, struct dim));
static void adjust_frame_glyphs P_ ((struct frame *));
struct glyph_matrix *new_glyph_matrix P_ ((struct glyph_pool *));
static void free_glyph_matrix P_ ((struct glyph_matrix *));
static void adjust_glyph_matrix P_ ((struct window *, struct glyph_matrix *,
				     int, int, struct dim));
133
static void change_frame_size_1 P_ ((struct frame *, int, int, int, int, int));
Gerd Moellmann's avatar
Gerd Moellmann committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
static void swap_glyphs_in_rows P_ ((struct glyph_row *, struct glyph_row *));
static void swap_glyph_pointers P_ ((struct glyph_row *, struct glyph_row *));
static int glyph_row_slice_p P_ ((struct glyph_row *, struct glyph_row *));
static void fill_up_frame_row_with_spaces P_ ((struct glyph_row *, int));
static void build_frame_matrix_from_window_tree P_ ((struct glyph_matrix *,
						     struct window *));
static void build_frame_matrix_from_leaf_window P_ ((struct glyph_matrix *,
						     struct window *));
static struct glyph_pool *new_glyph_pool P_ ((void));
static void free_glyph_pool P_ ((struct glyph_pool *));
static void adjust_frame_glyphs_initially P_ ((void));
static void adjust_frame_message_buffer P_ ((struct frame *));
static void adjust_decode_mode_spec_buffer P_ ((struct frame *));
static void fill_up_glyph_row_with_spaces P_ ((struct glyph_row *));
static void build_frame_matrix P_ ((struct frame *));
void clear_current_matrices P_ ((struct frame *));
void scroll_glyph_matrix_range P_ ((struct glyph_matrix *, int, int,
				    int, int));
static void clear_window_matrices P_ ((struct window *, int));
static void fill_up_glyph_row_area_with_spaces P_ ((struct glyph_row *, int));
static int scrolling_window P_ ((struct window *, int));
155
static int update_window_line P_ ((struct window *, int));
Gerd Moellmann's avatar
Gerd Moellmann committed
156
static void update_marginal_area P_ ((struct window *, int, int));
157
static int update_text_area P_ ((struct window *, int));
Gerd Moellmann's avatar
Gerd Moellmann committed
158 159 160 161
static void make_current P_ ((struct glyph_matrix *, struct glyph_matrix *,
			      int));
static void mirror_make_current P_ ((struct window *, int));
void check_window_matrix_pointers P_ ((struct window *));
162
#if GLYPH_DEBUG
Gerd Moellmann's avatar
Gerd Moellmann committed
163 164
static void check_matrix_pointers P_ ((struct glyph_matrix *,
				       struct glyph_matrix *));
165
#endif
Gerd Moellmann's avatar
Gerd Moellmann committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
static void mirror_line_dance P_ ((struct window *, int, int, int *, char *));
static int update_window_tree P_ ((struct window *, int));
static int update_window P_ ((struct window *, int));
static int update_frame_1 P_ ((struct frame *, int, int));
static void set_window_cursor_after_update P_ ((struct window *));
static int row_equal_p P_ ((struct window *, struct glyph_row *,
			    struct glyph_row *));
static void adjust_frame_glyphs_for_window_redisplay P_ ((struct frame *));
static void adjust_frame_glyphs_for_frame_redisplay P_ ((struct frame *));
static void reverse_rows P_ ((struct glyph_matrix *, int, int));
static int margin_glyphs_to_reserve P_ ((struct window *, int, Lisp_Object));



/* Non-zero means don't pause redisplay for pending input.  (This is
   for debugging and for a future implementation of EDT-like
   scrolling.  */
Jim Blandy's avatar
Jim Blandy committed
183

Gerd Moellmann's avatar
Gerd Moellmann committed
184
int redisplay_dont_pause;
185

Jim Blandy's avatar
Jim Blandy committed
186
/* Nonzero upon entry to redisplay means do not assume anything about
Jim Blandy's avatar
Jim Blandy committed
187
   current contents of actual terminal frame; clear and redraw it.  */
Jim Blandy's avatar
Jim Blandy committed
188

Jim Blandy's avatar
Jim Blandy committed
189
int frame_garbaged;
Jim Blandy's avatar
Jim Blandy committed
190

Gerd Moellmann's avatar
Gerd Moellmann committed
191
/* Nonzero means last display completed.  Zero means it was preempted.  */
Jim Blandy's avatar
Jim Blandy committed
192 193 194

int display_completed;

Gerd Moellmann's avatar
Gerd Moellmann committed
195 196
/* Lisp variable visible-bell; enables use of screen-flash instead of
   audible bell.  */
Jim Blandy's avatar
Jim Blandy committed
197 198 199

int visible_bell;

Jim Blandy's avatar
Jim Blandy committed
200
/* Invert the color of the whole frame, at a low level.  */
Jim Blandy's avatar
Jim Blandy committed
201 202 203 204 205 206 207

int inverse_video;

/* Line speed of the terminal.  */

int baud_rate;

Gerd Moellmann's avatar
Gerd Moellmann committed
208 209
/* Either nil or a symbol naming the window system under which Emacs
   is running.  */
Jim Blandy's avatar
Jim Blandy committed
210 211 212 213

Lisp_Object Vwindow_system;

/* Version number of X windows: 10, 11 or nil.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
214

Jim Blandy's avatar
Jim Blandy committed
215 216
Lisp_Object Vwindow_system_version;

Gerd Moellmann's avatar
Gerd Moellmann committed
217 218
/* Vector of glyph definitions.  Indexed by glyph number, the contents
   are a string which is how to output the glyph.
Jim Blandy's avatar
Jim Blandy committed
219 220

   If Vglyph_table is nil, a glyph is output by using its low 8 bits
Gerd Moellmann's avatar
Gerd Moellmann committed
221 222 223 224
   as a character code.

   This is an obsolete feature that is no longer used.  The variable
   is retained for compatibility.  */
Jim Blandy's avatar
Jim Blandy committed
225 226 227 228 229 230 231

Lisp_Object Vglyph_table;

/* Display table to use for vectors that don't specify their own.  */

Lisp_Object Vstandard_display_table;

Gerd Moellmann's avatar
Gerd Moellmann committed
232 233 234 235
/* Nonzero means reading single-character input with prompt so put
   cursor on mini-buffer after the prompt.  positive means at end of
   text in echo area; negative means at beginning of line.  */

Jim Blandy's avatar
Jim Blandy committed
236
int cursor_in_echo_area;
237 238

Lisp_Object Qdisplay_table;
Gerd Moellmann's avatar
Gerd Moellmann committed
239

Jim Blandy's avatar
Jim Blandy committed
240

Gerd Moellmann's avatar
Gerd Moellmann committed
241
/* The currently selected frame.  In a single-frame version, this
242
   variable always equals the_only_frame.  */
Jim Blandy's avatar
Jim Blandy committed
243

244
Lisp_Object selected_frame;
Jim Blandy's avatar
Jim Blandy committed
245

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

Gerd Moellmann's avatar
Gerd Moellmann committed
251
struct frame *last_nonminibuf_frame;
252

Gerd Moellmann's avatar
Gerd Moellmann committed
253
/* Stdio stream being used for copy of all output.  */
Jim Blandy's avatar
Jim Blandy committed
254

Gerd Moellmann's avatar
Gerd Moellmann committed
255
FILE *termscript;
Jim Blandy's avatar
Jim Blandy committed
256

Gerd Moellmann's avatar
Gerd Moellmann committed
257
/* Structure for info on cursor positioning.  */
Jim Blandy's avatar
Jim Blandy committed
258

Gerd Moellmann's avatar
Gerd Moellmann committed
259
struct cm Wcm;
Jim Blandy's avatar
Jim Blandy committed
260

Gerd Moellmann's avatar
Gerd Moellmann committed
261
/* 1 means SIGWINCH happened when not safe.  */
Jim Blandy's avatar
Jim Blandy committed
262

Gerd Moellmann's avatar
Gerd Moellmann committed
263
int delayed_size_change;
Jim Blandy's avatar
Jim Blandy committed
264

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

Gerd Moellmann's avatar
Gerd Moellmann committed
267
static int glyphs_initialized_initially_p;
Jim Blandy's avatar
Jim Blandy committed
268

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

Gerd Moellmann's avatar
Gerd Moellmann committed
271
struct window *updated_window;
Jim Blandy's avatar
Jim Blandy committed
272

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

Gerd Moellmann's avatar
Gerd Moellmann committed
275 276
struct glyph_row *updated_row;
int updated_area;
Jim Blandy's avatar
Jim Blandy committed
277

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

Gerd Moellmann's avatar
Gerd Moellmann committed
280
struct glyph space_glyph;
Jim Blandy's avatar
Jim Blandy committed
281

Gerd Moellmann's avatar
Gerd Moellmann committed
282 283 284
/* Non-zero means update has been performed directly, so that there's
   no need for redisplay_internal to do much work.  Set by
   direct_output_for_insert.  */
Jim Blandy's avatar
Jim Blandy committed
285

Gerd Moellmann's avatar
Gerd Moellmann committed
286
int redisplay_performed_directly_p;
Jim Blandy's avatar
Jim Blandy committed
287

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

Gerd Moellmann's avatar
Gerd Moellmann committed
291 292
int glyph_matrix_count;
int glyph_pool_count;
Jim Blandy's avatar
Jim Blandy committed
293

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

Gerd Moellmann's avatar
Gerd Moellmann committed
297
static struct frame *frame_matrix_frame;
Jim Blandy's avatar
Jim Blandy committed
298

Gerd Moellmann's avatar
Gerd Moellmann committed
299 300
/* Current interface for window-based redisplay.  Set from init_xterm.
   A null value means we are not using window-based redisplay.  */
Jim Blandy's avatar
Jim Blandy committed
301

Gerd Moellmann's avatar
Gerd Moellmann committed
302
struct redisplay_interface *rif;
Jim Blandy's avatar
Jim Blandy committed
303

Gerd Moellmann's avatar
Gerd Moellmann committed
304 305 306 307 308
/* Non-zero means that fonts have been loaded since the last glyph
   matrix adjustments.  Redisplay must stop, and glyph matrices must
   be adjusted when this flag becomes non-zero during display.  The
   reason fonts can be loaded so late is that fonts of fontsets are
   loaded on demand.  */
309

Gerd Moellmann's avatar
Gerd Moellmann committed
310
int fonts_changed_p;
311

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

Gerd Moellmann's avatar
Gerd Moellmann committed
315
#if GLYPH_DEBUG
Jim Blandy's avatar
Jim Blandy committed
316

Gerd Moellmann's avatar
Gerd Moellmann committed
317 318 319 320
static int window_to_frame_vpos P_ ((struct window *, int));
static int window_to_frame_hpos P_ ((struct window *, int));
#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
321

Gerd Moellmann's avatar
Gerd Moellmann committed
322
#else /* GLYPH_DEBUG == 0 */
Jim Blandy's avatar
Jim Blandy committed
323

Gerd Moellmann's avatar
Gerd Moellmann committed
324 325
#define WINDOW_TO_FRAME_VPOS(W, VPOS) ((VPOS) + XFASTINT ((W)->top))
#define WINDOW_TO_FRAME_HPOS(W, HPOS) ((HPOS) + XFASTINT ((W)->left))
Jim Blandy's avatar
Jim Blandy committed
326

Gerd Moellmann's avatar
Gerd Moellmann committed
327
#endif /* GLYPH_DEBUG == 0 */
Jim Blandy's avatar
Jim Blandy committed
328 329


Gerd Moellmann's avatar
Gerd Moellmann committed
330 331 332
/* Like bcopy except never gets confused by overlap.  Let this be the
   first function defined in this file, or change emacs.c where the
   address of this function is used.  */
Jim Blandy's avatar
Jim Blandy committed
333 334 335 336 337 338

void
safe_bcopy (from, to, size)
     char *from, *to;
     int size;
{
339
  if (size <= 0 || from == to)
Jim Blandy's avatar
Jim Blandy committed
340 341
    return;

342 343 344 345 346 347 348 349
  /* If the source and destination don't overlap, then bcopy can
     handle it.  If they do overlap, but the destination is lower in
     memory than the source, we'll assume bcopy can handle that.  */
  if (to < from || from + size <= to)
    bcopy (from, to, size);

  /* Otherwise, we'll copy from the end.  */
  else
Jim Blandy's avatar
Jim Blandy committed
350
    {
351 352
      register char *endf = from + size;
      register char *endt = to + size;
Jim Blandy's avatar
Jim Blandy committed
353 354 355 356 357

      /* If TO - FROM is large, then we should break the copy into
	 nonoverlapping chunks of TO - FROM bytes each.  However, if
	 TO - FROM is small, then the bcopy function call overhead
	 makes this not worth it.  The crossover point could be about
358 359
	 anywhere.  Since I don't think the obvious copy loop is too
	 bad, I'm trying to err in its favor.  */
Jim Blandy's avatar
Jim Blandy committed
360 361 362 363 364 365 366 367
      if (to - from < 64)
	{
	  do
	    *--endt = *--endf;
	  while (endf != from);
	}
      else
	{
368
	  for (;;)
Jim Blandy's avatar
Jim Blandy committed
369 370 371 372
	    {
	      endt -= (to - from);
	      endf -= (to - from);

373 374 375
	      if (endt < to)
		break;

Jim Blandy's avatar
Jim Blandy committed
376 377
	      bcopy (endf, endt, to - from);
	    }
378 379

	  /* If SIZE wasn't a multiple of TO - FROM, there will be a
Gerd Moellmann's avatar
Gerd Moellmann committed
380 381
	     little left over.  The amount left over is (endt + (to -
	     from)) - to, which is endt - from.  */
Jim Blandy's avatar
Jim Blandy committed
382 383 384 385 386 387
	  bcopy (from, to, endt - from);
	}
    }
}     


Gerd Moellmann's avatar
Gerd Moellmann committed
388 389 390 391 392 393 394 395 396 397 398 399 400 401

/***********************************************************************
			    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.  */

struct glyph_matrix *
new_glyph_matrix (pool)
     struct glyph_pool *pool;
Jim Blandy's avatar
Jim Blandy committed
402
{
Gerd Moellmann's avatar
Gerd Moellmann committed
403 404 405 406 407
  struct glyph_matrix *result;

  /* Allocate and clear.  */
  result = (struct glyph_matrix *) xmalloc (sizeof *result);
  bzero (result, sizeof *result);
Jim Blandy's avatar
Jim Blandy committed
408

Gerd Moellmann's avatar
Gerd Moellmann committed
409 410 411
  /* Increment number of allocated matrices.  This count is used
     to detect memory leaks.  */
  ++glyph_matrix_count;
Jim Blandy's avatar
Jim Blandy committed
412

Gerd Moellmann's avatar
Gerd Moellmann committed
413 414 415
  /* Set pool and return.  */
  result->pool = pool;
  return result;
Jim Blandy's avatar
Jim Blandy committed
416 417 418
}


Gerd Moellmann's avatar
Gerd Moellmann committed
419 420 421 422 423 424 425 426 427 428 429 430 431 432
/* 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.
 
   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
free_glyph_matrix (matrix)
     struct glyph_matrix *matrix;
Jim Blandy's avatar
Jim Blandy committed
433
{
Gerd Moellmann's avatar
Gerd Moellmann committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
  if (matrix)
    {
      int i;

      /* Detect the case that more matrices are freed than were
	 allocated.  */
      if (--glyph_matrix_count < 0)
	abort ();

      /* 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]);
      
      /* Free row structures and the matrix itself.  */
      xfree (matrix->rows);
      xfree (matrix);
    }
}
Jim Blandy's avatar
Jim Blandy committed
453 454


Gerd Moellmann's avatar
Gerd Moellmann committed
455 456 457 458 459 460 461 462 463 464 465 466 467
/* 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
margin_glyphs_to_reserve (w, total_glyphs, margin)
     struct window *w;
     int total_glyphs;
     Lisp_Object margin;
{
  int n;
Jim Blandy's avatar
Jim Blandy committed
468

Gerd Moellmann's avatar
Gerd Moellmann committed
469
  if (NUMBERP (margin))
Jim Blandy's avatar
Jim Blandy committed
470
    {
Gerd Moellmann's avatar
Gerd Moellmann committed
471 472 473 474 475 476 477 478 479 480 481
      int width = XFASTINT (w->width);
      double d = max (0, XFLOATINT (margin));
      d = min (width / 2 - 1, d);
      n = (int) ((double) total_glyphs / width * d);
    }
  else
    n = 0;

  return n;
}

Jim Blandy's avatar
Jim Blandy committed
482

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

Gerd Moellmann's avatar
Gerd Moellmann committed
486 487 488 489 490
   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.
491

Gerd Moellmann's avatar
Gerd Moellmann committed
492 493 494 495
   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.
496

Gerd Moellmann's avatar
Gerd Moellmann committed
497 498 499 500 501 502 503
   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.  */
504

Gerd Moellmann's avatar
Gerd Moellmann committed
505 506 507 508 509 510 511 512 513 514
static void
adjust_glyph_matrix (w, matrix, x, y, dim)
     struct window *w;
     struct glyph_matrix *matrix;
     int x, y;
     struct dim dim;
{
  int i;
  int new_rows;
  int marginal_areas_changed_p = 0;
Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
515 516
  int header_line_changed_p = 0;
  int header_line_p = 0;
Gerd Moellmann's avatar
Gerd Moellmann committed
517 518 519 520 521 522
  int left = -1, right = -1;
  int window_x, window_y, window_width, window_height;

  /* See if W had a top line that has disappeared now, or vice versa.  */
  if (w)
    {
Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
523 524
      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
525
    }
Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
526
  matrix->header_line_p = header_line_p;
527

Gerd Moellmann's avatar
Gerd Moellmann committed
528 529 530 531 532 533 534 535 536 537 538 539 540 541
  /* Do nothing if MATRIX' size, position, vscroll, and marginal areas
     haven't changed.  This optimization is important because preserving
     the matrix means preventing redisplay.  */
  if (matrix->pool == NULL)
    {
      window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
      left = margin_glyphs_to_reserve (w, dim.width, w->left_margin_width);
      right = margin_glyphs_to_reserve (w, dim.width, w->right_margin_width);
      xassert (left >= 0 && right >= 0);
      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
542
	  && !header_line_changed_p
Gerd Moellmann's avatar
Gerd Moellmann committed
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
	  && matrix->window_top_y == XFASTINT (w->top)
	  && matrix->window_height == window_height
	  && matrix->window_vscroll == w->vscroll
	  && matrix->window_width == window_width)
	return;
    }
  
  /* Enlarge MATRIX->rows if necessary.  New rows are cleared.  */
  if (matrix->rows_allocated < dim.height)
    {
      int size = dim.height * sizeof (struct glyph_row);
      new_rows = dim.height - matrix->rows_allocated;
      matrix->rows = (struct glyph_row *) xrealloc (matrix->rows, size);
      bzero (matrix->rows + matrix->rows_allocated,
	     new_rows * sizeof *matrix->rows);
      matrix->rows_allocated = dim.height;
    }
  else
    new_rows = 0;
562

Gerd Moellmann's avatar
Gerd Moellmann committed
563 564 565 566 567 568 569 570
  /* 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)
    {
      xassert (matrix->pool->glyphs);
      
      if (w)
571
	{
Gerd Moellmann's avatar
Gerd Moellmann committed
572 573 574 575
	  left = margin_glyphs_to_reserve (w, dim.width,
					   w->left_margin_width);
	  right = margin_glyphs_to_reserve (w, dim.width,
					    w->right_margin_width);
576
	}
Gerd Moellmann's avatar
Gerd Moellmann committed
577 578 579 580
      else
	left = right = 0;
      
      for (i = 0; i < dim.height; ++i)
581
	{
Gerd Moellmann's avatar
Gerd Moellmann committed
582 583 584 585 586 587 588 589 590
	  struct glyph_row *row = &matrix->rows[i];
	  
	  row->glyphs[LEFT_MARGIN_AREA] 
	    = (matrix->pool->glyphs
	       + (y + i) * matrix->pool->ncolumns
	       + x);
	  
	  if (w == NULL
	      || row == matrix->rows + dim.height - 1
Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
591
	      || (row == matrix->rows && matrix->header_line_p))
Gerd Moellmann's avatar
Gerd Moellmann committed
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
	    {
	      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;
	    }
609
	}
Gerd Moellmann's avatar
Gerd Moellmann committed
610 611 612 613 614 615 616 617 618 619
      
      matrix->left_margin_glyphs = left;
      matrix->right_margin_glyphs = right;
    }
  else
    {
      /* If MATRIX->pool is null, MATRIX is responsible for managing
	 its own memory.  Allocate glyph memory from the heap.  */
      if (dim.width > matrix->matrix_w
	  || new_rows
Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
620
	  || header_line_changed_p
Gerd Moellmann's avatar
Gerd Moellmann committed
621
	  || marginal_areas_changed_p)
Jim Blandy's avatar
Jim Blandy committed
622
	{
Gerd Moellmann's avatar
Gerd Moellmann committed
623 624 625 626 627 628 629 630 631 632 633 634
	  struct glyph_row *row = matrix->rows;
	  struct glyph_row *end = row + matrix->rows_allocated;
	  
	  while (row < end)
	    {
	      row->glyphs[LEFT_MARGIN_AREA]
		= (struct glyph *) xrealloc (row->glyphs[LEFT_MARGIN_AREA],
					     (dim.width
					      * sizeof (struct glyph)));
	      
	      /* The mode line never has marginal areas.  */
	      if (row == matrix->rows + dim.height - 1
Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
635
		  || (row == matrix->rows && matrix->header_line_p))
Gerd Moellmann's avatar
Gerd Moellmann committed
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
		{
		  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
655 656
	}

Gerd Moellmann's avatar
Gerd Moellmann committed
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
      xassert (left >= 0 && right >= 0);
      matrix->left_margin_glyphs = left;
      matrix->right_margin_glyphs = right;
    }
  
  /* Number of rows to be used by MATRIX.  */
  matrix->nrows = dim.height;

  /* 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 (w && matrix == w->current_matrix)
    {
      /* 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
	  && matrix->window_top_y == XFASTINT (w->top)
	  && matrix->window_width == window_width)
Jim Blandy's avatar
Jim Blandy committed
677
	{
Gerd Moellmann's avatar
Gerd Moellmann committed
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
	  i = 0;
	  while (matrix->rows[i].enabled_p
		 && (MATRIX_ROW_BOTTOM_Y (matrix->rows + i)
		     < matrix->window_height))
	    ++i;

	  /* Window end is invalid, if inside of the rows that
	     are invalidated.  */
	  if (INTEGERP (w->window_end_vpos)
	      && XFASTINT (w->window_end_vpos) >= i)
	    w->window_end_valid = Qnil;
	  
	  while (i < matrix->nrows)
	    matrix->rows[i++].enabled_p = 0;
	}
      else
	{
	  for (i = 0; i < matrix->nrows; ++i)
	    matrix->rows[i].enabled_p = 0;
Jim Blandy's avatar
Jim Blandy committed
697 698
	}
    }
Gerd Moellmann's avatar
Gerd Moellmann committed
699 700 701 702 703 704 705 706 707 708
  
  /* 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
709
    {
Gerd Moellmann's avatar
Gerd Moellmann committed
710 711 712 713 714 715
      matrix->window_top_y = XFASTINT (w->top);
      matrix->window_height = window_height;
      matrix->window_width = window_width;
      matrix->window_vscroll = w->vscroll;
    }
}
Jim Blandy's avatar
Jim Blandy committed
716 717


Gerd Moellmann's avatar
Gerd Moellmann committed
718 719 720 721
/* 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).  */
722

Gerd Moellmann's avatar
Gerd Moellmann committed
723 724 725 726 727 728
static void
reverse_rows (matrix, start, end)
     struct glyph_matrix *matrix;
     int start, end;
{
  int i, j;
729

Gerd Moellmann's avatar
Gerd Moellmann committed
730 731 732 733 734 735 736 737 738 739
  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;
    }
}
740 741


Gerd Moellmann's avatar
Gerd Moellmann committed
742 743 744 745
/* 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).
746

Gerd Moellmann's avatar
Gerd Moellmann committed
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
   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
rotate_matrix (matrix, first, last, by)
     struct glyph_matrix *matrix;
     int first, last, by;
{
  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
increment_glyph_matrix_buffer_positions (matrix, start, end, delta,
					 delta_bytes)
     struct glyph_matrix *matrix;
     int start, end, delta, delta_bytes;
{
  /* Check that START and END are reasonable values.  */
  xassert (start >= 0 && start <= matrix->nrows);
  xassert (end >= 0 && end <= matrix->nrows);
  xassert (start <= end);

  for (; start < end; ++start)
    increment_glyph_row_buffer_positions (matrix->rows + start,
					  delta, delta_bytes);
}


/* Enable a range of rows in glyph matrix MATRIX.  START and END are
   the row indices of the first and last + 1 row to enable.  If
   ENABLED_P is non-zero, enabled_p flags in rows will be set to 1.  */

void
enable_glyph_matrix_rows (matrix, start, end, enabled_p)
     struct glyph_matrix *matrix;
     int start, end;
     int enabled_p;
{
  xassert (start <= end);
  xassert (start >= 0 && start < matrix->nrows);
  xassert (end >= 0 && end <= matrix->nrows);
  
  for (; start < end; ++start)
    matrix->rows[start].enabled_p = enabled_p != 0;
}


/* Clear MATRIX.

   This empties all rows in MATRIX by setting the enabled_p flag for
   all rows of the matrix to zero.  The function prepare_desired_row
   will eventually really clear a row when it sees one with a zero
   enabled_p flag.

   Resets update hints to defaults value.  The only update hint
   currently present is the flag MATRIX->no_scrolling_p.  */

void
clear_glyph_matrix (matrix)
     struct glyph_matrix *matrix;
{
  if (matrix)
    {
      enable_glyph_matrix_rows (matrix, 0, matrix->nrows, 0);
      matrix->no_scrolling_p = 0;
    }
}
  

/* 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
shift_glyph_matrix (w, matrix, start, end, dy)
     struct window *w;
     struct glyph_matrix *matrix;
     int start, end, dy;
{
  int min_y, max_y;
  
  xassert (start <= end);
  xassert (start >= 0 && start < matrix->nrows);
  xassert (end >= 0 && end <= matrix->nrows);
  
Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
855
  min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
Gerd Moellmann's avatar
Gerd Moellmann committed
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
  max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w);
  
  for (; start < end; ++start)
    {
      struct glyph_row *row = &matrix->rows[start];
      
      row->y += dy;
      
      if (row->y < min_y)
	row->visible_height = row->height - (min_y - row->y);
      else if (row->y + row->height > max_y)
	row->visible_height = row->height - (row->y + row->height - max_y);
      else
	row->visible_height = row->height;
    }
}


/* 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
clear_current_matrices (f)
     register struct frame *f;
{
  /* 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.  */
  if (WINDOWP (f->menu_bar_window))
    clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);

892 893 894
  /* Clear the matrix of the tool-bar window, if any.  */
  if (WINDOWP (f->tool_bar_window))
    clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
Gerd Moellmann's avatar
Gerd Moellmann committed
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913

  /* Clear current window matrices.  */
  xassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
  clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 0);
}


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

void
clear_desired_matrices (f)
     register struct frame *f;
{
  if (f->desired_matrix)
    clear_glyph_matrix (f->desired_matrix);
  
  if (WINDOWP (f->menu_bar_window))
    clear_glyph_matrix (XWINDOW (f->menu_bar_window)->desired_matrix);

914 915
  if (WINDOWP (f->tool_bar_window))
    clear_glyph_matrix (XWINDOW (f->tool_bar_window)->desired_matrix);
Gerd Moellmann's avatar
Gerd Moellmann committed
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 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 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004

  /* Do it for window matrices.  */
  xassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
  clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
}


/* Clear matrices in window tree rooted in W.  If DESIRED_P is
   non-zero clear desired matrices, otherwise clear current matrices.  */

static void
clear_window_matrices (w, desired_p)
     struct window *w;
     int desired_p;
{
  while (w)
    {
      if (!NILP (w->hchild))
	{
	  xassert (WINDOWP (w->hchild));
	  clear_window_matrices (XWINDOW (w->hchild), desired_p);
	}
      else if (!NILP (w->vchild))
	{
	  xassert (WINDOWP (w->vchild));
	  clear_window_matrices (XWINDOW (w->vchild), desired_p);
	}
      else
	{
	  if (desired_p)
	    clear_glyph_matrix (w->desired_matrix);
	  else
	    {
	      clear_glyph_matrix (w->current_matrix);
	      w->window_end_valid = Qnil;
	    }
	}

      w = NILP (w->next) ? 0 : XWINDOW (w->next);
    }
}


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

void
clear_glyph_row (row)
     struct glyph_row *row;
{
  struct glyph *p[1 + LAST_AREA];
  static struct glyph_row null_row;

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


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

void
blank_row (w, row, y)
     struct window *w;
     struct glyph_row *row;
     int y;
{
  int min_y, max_y;
  
Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
1005
  min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
Gerd Moellmann's avatar
Gerd Moellmann committed
1006 1007 1008 1009
  max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w);
  
  clear_glyph_row (row);
  row->y = y;
1010 1011
  row->ascent = row->phys_ascent = 0;
  row->height = row->phys_height = CANON_Y_UNIT (XFRAME (w->frame));
Gerd Moellmann's avatar
Gerd Moellmann committed
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
  
  if (row->y < min_y)
    row->visible_height = row->height - (min_y - row->y);
  else if (row->y + row->height > max_y)
    row->visible_height = row->height - (row->y + row->height - max_y);
  else
    row->visible_height = row->height;

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

void
increment_glyph_row_buffer_positions (row, delta, delta_bytes)
     struct glyph_row *row;
     int delta, delta_bytes;
{
  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;

  /* 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;
}


/* 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
swap_glyphs_in_rows (a, b)
     struct glyph_row *a, *b;
{
  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;
	}
    }
}


/* Exchange pointers to glyph memory between glyph rows A and B.  */

static INLINE void
swap_glyph_pointers (a, b)
     struct glyph_row *a, *b;
{
  int i;
  for (i = 0; i < LAST_AREA + 1; ++i)
    {
      struct glyph *temp = a->glyphs[i];
      a->glyphs[i] = b->glyphs[i];
      b->glyphs[i] = temp;
    }
}


/* Copy glyph row structure FROM to glyph row structure TO, except
   that glyph pointers in the structures are left unchanged.  */

INLINE void
copy_row_except_pointers (to, from)
     struct glyph_row *to, *from;
{
  struct glyph *pointers[1 + LAST_AREA];

  /* Save glyph pointers of TO.  */
  bcopy (to->glyphs, pointers, sizeof to->glyphs);

  /* Do a structure assignment.  */
  *to = *from;

  /* Restore original pointers of TO.  */
  bcopy (pointers, to->glyphs, sizeof to->glyphs);
}


/* Copy contents of glyph row FROM to glyph row TO.  Glyph pointers in
   TO and FROM are left unchanged.  Glyph contents are copied from the
   glyph memory of FROM to the glyph memory of TO.  Increment buffer
   positions in row TO by DELTA/ DELTA_BYTES.  */

void
copy_glyph_row_contents (to, from, delta, delta_bytes)
     struct glyph_row *to, *from;
     int delta, delta_bytes;
{
  int area;

  /* This is like a structure assignment TO = FROM, except that
     glyph pointers in the rows are left unchanged.  */
  copy_row_except_pointers (to, from);

  /* Copy glyphs from FROM to TO.  */
  for (area = 0; area < LAST_AREA; ++area)
    if (from->used[area])
      bcopy (from->glyphs[area], to->glyphs[area], 
	     from->used[area] * sizeof (struct glyph));

  /* Increment buffer positions in TO by DELTA.  */
  increment_glyph_row_buffer_positions (to, delta, delta_bytes);
}


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

static INLINE void
assign_row (to, from)
     struct glyph_row *to, *from;
{
  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
   is non-zero if the glyph memory of WINDOW_ROW is part of the glyph
   memory of FRAME_ROW.  */

static int
glyph_row_slice_p (window_row, frame_row)
     struct glyph_row *window_row, *frame_row;
{
  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);
}


/* 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 *
find_glyph_row_slice (window_matrix, frame_matrix, row)
     struct glyph_matrix *window_matrix, *frame_matrix;
     int row;
{
  int i;

  xassert (row >= 0 && row < frame_matrix->nrows);

  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;
}


/* Prepare ROW for display.  Desired rows are cleared lazily,
   i.e. they are only marked as to be cleared by setting their
   enabled_p flag to zero.  When a row is to be displayed, a prior
   call to this function really clears it.  */

void
prepare_desired_row (row)
     struct glyph_row *row;
{
  if (!row->enabled_p)
    {
      clear_glyph_row (row);
      row->enabled_p = 1;
    }
}


/* Return a hash code for glyph row ROW.  */

int
line_hash_code (row)
     struct glyph_row *row;
{
  int hash = 0;
  
  if (row->enabled_p)
    {
      if (row->inverse_p)
        {
          /* Give all highlighted lines the same hash code
	     so as to encourage scrolling to leave them in place.  */
          hash = -1;
        }
      else
        {
          struct glyph *glyph = row->glyphs[TEXT_AREA];
	  struct glyph *end = glyph + row->used[TEXT_AREA];

          while (glyph < end)
            {
	      GLYPH g = GLYPH_FROM_CHAR_GLYPH (*glyph);
	      if (must_write_spaces)
	        g -= SPACEGLYPH;
	      hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + g;
	      ++glyph;
	    }

          if (hash == 0)
	    hash = 1;
        }
    }

  return hash;
}


/* Return the cost of drawing line VPOS In MATRIX.  The cost equals
   the number of characters in the line.  If must_write_spaces is
   zero, leading and trailing spaces are ignored.  */

static unsigned int
line_draw_cost (matrix, vpos)
     struct glyph_matrix *matrix;
     int vpos;
{
  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;
  int glyph_table_len = GLYPH_TABLE_LENGTH;

  /* Ignore trailing and leading spaces if we can.  */
  if (!must_write_spaces)
    {
      /* Skip from the end over trailing spaces.  */
      while (end != beg && CHAR_GLYPH_SPACE_P (*end))
	--end;

      /* All blank line.  */      
      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)
	{
	  GLYPH g = GLYPH_FROM_CHAR_GLYPH (*beg);
	  
	  if (GLYPH_SIMPLE_P (glyph_table_base, glyph_table_len, g))
	    len += 1;
	  else
	    len += GLYPH_LENGTH (glyph_table_base, g);
	  
	  ++beg;
	}
    }
  
  return len;
}


/* Test two glyph rows A and B for equality.  Value is non-zero if A
   and B have equal contents.  W is the window to which the glyphs
   rows A and B belong.  It is needed here to test for partial row
   visibility.  */

static INLINE int 
row_equal_p (w, a, b)
     struct window *w;
     struct glyph_row *a, *b;
{
  if (a == b)
    return 1;
  else if (a->hash != b->hash)
    return 0;
  else
    {
      struct glyph *a_glyph, *b_glyph, *a_end;
      int area;

      /* Compare glyphs.  */
      for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
	{
	  if (a->used[area] != b->used[area])
	    return 0;
	  
	  a_glyph = a->glyphs[area];
	  a_end = a_glyph + a->used[area];
	  b_glyph = b->glyphs[area];
	  
	  while (a_glyph < a_end
		 && GLYPH_EQUAL_P (a_glyph, b_glyph))
	    ++a_glyph, ++b_glyph;
	  
	  if (a_glyph != a_end)
	    return 0;
	}

      if (a->truncated_on_left_p != b->truncated_on_left_p
	  || a->inverse_p != b->inverse_p
	  || a->fill_line_p != b->fill_line_p
	  || a->truncated_on_right_p != b->truncated_on_right_p
	  || a->overlay_arrow_p != b->overlay_arrow_p
	  || a->continued_p != b->continued_p
	  || a->indicate_empty_line_p != b->indicate_empty_line_p
1372
	  || a->overlapped_p != b->overlapped_p
Gerd Moellmann's avatar
Gerd Moellmann committed
1373 1374 1375 1376 1377 1378
	  || (MATRIX_ROW_CONTINUATION_LINE_P (a)
	      != MATRIX_ROW_CONTINUATION_LINE_P (b))
	  /* Different partially visible characters on left margin.  */
	  || a->x != b->x
	  /* Different height.  */
	  || a->ascent != b->ascent
1379 1380
	  || a->phys_ascent != b->phys_ascent
	  || a->phys_height != b->phys_height
Gerd Moellmann's avatar
Gerd Moellmann committed
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730