dispnew.c 203 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Updating of data structures for redisplay.
2 3
   Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
                 1997, 1998, 1999, 2000, 2001, 2002, 2003,
Glenn Morris's avatar
Glenn Morris committed
4
                 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
Jim Blandy's avatar
Jim Blandy committed
5 6 7 8 9

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
10
the Free Software Foundation; either version 2, or (at your option)
Jim Blandy's avatar
Jim Blandy committed
11 12 13 14 15 16 17 18 19
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
Lute Kamstra's avatar
Lute Kamstra committed
20 21
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.  */
Jim Blandy's avatar
Jim Blandy committed
22

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

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

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

51 52 53
/* 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
54
#include "syssignal.h"
Jim Blandy's avatar
Jim Blandy committed
55

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

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

64
#ifdef MAC_OS
65
#include "macterm.h"
66
#endif /* MAC_OS */
67

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

Gerd Moellmann's avatar
Gerd Moellmann committed
70
#include "systime.h"
71 72
#include <errno.h>

Gerd Moellmann's avatar
Gerd Moellmann committed
73 74 75 76 77 78
/* To get the prototype for `sleep'.  */

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

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

83
#ifdef __GNU_LIBRARY__
Gerd Moellmann's avatar
Gerd Moellmann committed
84 85 86 87 88

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

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

105
#if defined(HAVE_TERM_H) && defined (GNU_LINUX) && defined (HAVE_LIBNCURSES)
Dave Love's avatar
Dave Love committed
106 107
#include <term.h>		/* for tgetent */
#endif
Gerd Moellmann's avatar
Gerd Moellmann committed
108 109 110 111 112 113 114 115 116 117 118 119 120

/* Structure to pass dimensions around.  Used for character bounding
   boxes, glyph matrix dimensions and alike.  */

struct dim
{
  int width;
  int height;
};


/* Function prototypes.  */

121 122
static struct glyph_matrix *save_current_matrix P_ ((struct frame *));
static void restore_current_matrix P_ ((struct frame *, struct glyph_matrix *));
123
static void fake_current_matrices P_ ((Lisp_Object));
124 125
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
126 127 128 129 130 131
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
132 133
     P_ ((Lisp_Object, int, int, int, int *));
static void allocate_matrices_for_window_redisplay P_ ((struct window *));
Gerd Moellmann's avatar
Gerd Moellmann committed
134 135 136 137 138 139
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));
140
static void change_frame_size_1 P_ ((struct frame *, int, int, int, int, int));
Gerd Moellmann's avatar
Gerd Moellmann committed
141
static void swap_glyph_pointers P_ ((struct glyph_row *, struct glyph_row *));
142
#if GLYPH_DEBUG
Gerd Moellmann's avatar
Gerd Moellmann committed
143
static int glyph_row_slice_p P_ ((struct glyph_row *, struct glyph_row *));
144
#endif
Gerd Moellmann's avatar
Gerd Moellmann committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
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));
163
static int update_window_line P_ ((struct window *, int, int *));
Gerd Moellmann's avatar
Gerd Moellmann committed
164
static void update_marginal_area P_ ((struct window *, int, int));
165
static int update_text_area P_ ((struct window *, int));
Gerd Moellmann's avatar
Gerd Moellmann committed
166 167 168 169
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 *));
170
#if GLYPH_DEBUG
Gerd Moellmann's avatar
Gerd Moellmann committed
171 172
static void check_matrix_pointers P_ ((struct glyph_matrix *,
				       struct glyph_matrix *));
173
#endif
Gerd Moellmann's avatar
Gerd Moellmann committed
174 175 176 177 178 179
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 *,
180
			    struct glyph_row *, int));
Gerd Moellmann's avatar
Gerd Moellmann committed
181 182 183 184
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));
185 186
static void sync_window_with_frame_matrix_rows P_ ((struct window *));
struct window *frame_row_to_window P_ ((struct window *, int));
Gerd Moellmann's avatar
Gerd Moellmann committed
187 188 189 190 191

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

Gerd Moellmann's avatar
Gerd Moellmann committed
193
int redisplay_dont_pause;
194

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
/* Define PERIODIC_PREEMPTION_CHECKING to 1, if micro-second timers
   are supported, so we can check for input during redisplay at
   regular intervals.  */
#ifdef EMACS_HAS_USECS
#define PERIODIC_PREEMPTION_CHECKING 1
#else
#define PERIODIC_PREEMPTION_CHECKING 0
#endif

#if PERIODIC_PREEMPTION_CHECKING

/* If a number (float), check for user input every N seconds.  */

Lisp_Object Vredisplay_preemption_period;

/* Redisplay preemption timers.  */

static EMACS_TIME preemption_period;
static EMACS_TIME preemption_next_check;

#endif

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

Jim Blandy's avatar
Jim Blandy committed
220
int frame_garbaged;
Jim Blandy's avatar
Jim Blandy committed
221

Gerd Moellmann's avatar
Gerd Moellmann committed
222
/* Nonzero means last display completed.  Zero means it was preempted.  */
Jim Blandy's avatar
Jim Blandy committed
223 224 225

int display_completed;

Gerd Moellmann's avatar
Gerd Moellmann committed
226 227
/* Lisp variable visible-bell; enables use of screen-flash instead of
   audible bell.  */
Jim Blandy's avatar
Jim Blandy committed
228 229 230

int visible_bell;

Jim Blandy's avatar
Jim Blandy committed
231
/* Invert the color of the whole frame, at a low level.  */
Jim Blandy's avatar
Jim Blandy committed
232 233 234 235 236

int inverse_video;

/* Line speed of the terminal.  */

237
EMACS_INT baud_rate;
Jim Blandy's avatar
Jim Blandy committed
238

Gerd Moellmann's avatar
Gerd Moellmann committed
239 240
/* Either nil or a symbol naming the window system under which Emacs
   is running.  */
Jim Blandy's avatar
Jim Blandy committed
241 242 243 244

Lisp_Object Vwindow_system;

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

Jim Blandy's avatar
Jim Blandy committed
246 247
Lisp_Object Vwindow_system_version;

Gerd Moellmann's avatar
Gerd Moellmann committed
248 249
/* 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
250 251

   If Vglyph_table is nil, a glyph is output by using its low 8 bits
Gerd Moellmann's avatar
Gerd Moellmann committed
252 253 254 255
   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
256 257 258 259 260 261 262

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
263
/* Nonzero means reading single-character input with prompt so put
264
   cursor on mini-buffer after the prompt.  Positive means at end of
Gerd Moellmann's avatar
Gerd Moellmann committed
265 266
   text in echo area; negative means at beginning of line.  */

Jim Blandy's avatar
Jim Blandy committed
267
int cursor_in_echo_area;
268

269
Lisp_Object Qdisplay_table, Qredisplay_dont_pause;
Gerd Moellmann's avatar
Gerd Moellmann committed
270

Jim Blandy's avatar
Jim Blandy committed
271

Gerd Moellmann's avatar
Gerd Moellmann committed
272
/* The currently selected frame.  In a single-frame version, this
273
   variable always equals the_only_frame.  */
Jim Blandy's avatar
Jim Blandy committed
274

275
Lisp_Object selected_frame;
Jim Blandy's avatar
Jim Blandy committed
276

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

Gerd Moellmann's avatar
Gerd Moellmann committed
282
struct frame *last_nonminibuf_frame;
283

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

Gerd Moellmann's avatar
Gerd Moellmann committed
286
FILE *termscript;
Jim Blandy's avatar
Jim Blandy committed
287

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

Gerd Moellmann's avatar
Gerd Moellmann committed
290
struct cm Wcm;
Jim Blandy's avatar
Jim Blandy committed
291

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

Gerd Moellmann's avatar
Gerd Moellmann committed
294
int delayed_size_change;
Jim Blandy's avatar
Jim Blandy committed
295

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

Gerd Moellmann's avatar
Gerd Moellmann committed
298
static int glyphs_initialized_initially_p;
Jim Blandy's avatar
Jim Blandy committed
299

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

Gerd Moellmann's avatar
Gerd Moellmann committed
302
struct window *updated_window;
Jim Blandy's avatar
Jim Blandy committed
303

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

Gerd Moellmann's avatar
Gerd Moellmann committed
306 307
struct glyph_row *updated_row;
int updated_area;
Jim Blandy's avatar
Jim Blandy committed
308

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

Gerd Moellmann's avatar
Gerd Moellmann committed
311
struct glyph space_glyph;
Jim Blandy's avatar
Jim Blandy committed
312

Gerd Moellmann's avatar
Gerd Moellmann committed
313 314 315
/* 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
316

Gerd Moellmann's avatar
Gerd Moellmann committed
317
int redisplay_performed_directly_p;
Jim Blandy's avatar
Jim Blandy committed
318

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

Gerd Moellmann's avatar
Gerd Moellmann committed
322 323
int glyph_matrix_count;
int glyph_pool_count;
Jim Blandy's avatar
Jim Blandy committed
324

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

Gerd Moellmann's avatar
Gerd Moellmann committed
328
static struct frame *frame_matrix_frame;
Jim Blandy's avatar
Jim Blandy committed
329

Gerd Moellmann's avatar
Gerd Moellmann committed
330 331
/* 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
332

Gerd Moellmann's avatar
Gerd Moellmann committed
333
struct redisplay_interface *rif;
Jim Blandy's avatar
Jim Blandy committed
334

Gerd Moellmann's avatar
Gerd Moellmann committed
335 336 337 338 339
/* 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.  */
340

Gerd Moellmann's avatar
Gerd Moellmann committed
341
int fonts_changed_p;
342

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

Gerd Moellmann's avatar
Gerd Moellmann committed
346
#if GLYPH_DEBUG
Jim Blandy's avatar
Jim Blandy committed
347

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

353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
/* 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.  */

static unsigned history_tick;

static void add_frame_display_history P_ ((struct frame *, int));
static void add_window_display_history P_ ((struct window *, char *, int));
Richard M. Stallman's avatar
Richard M. Stallman committed
380

381 382
/* Add to the redisplay history how window W has been displayed.
   MSG is a trace containing the information how W's glyph matrix
383
   has been constructed.  PAUSED_P non-zero means that the update
384 385 386 387 388 389 390 391 392
   has been interrupted for pending input.  */

static void
add_window_display_history (w, msg, paused_p)
     struct window *w;
     char *msg;
     int paused_p;
{
  char *buf;
393

394 395 396 397
  if (history_idx >= REDISPLAY_HISTORY_SIZE)
    history_idx = 0;
  buf = redisplay_history[history_idx].trace;
  ++history_idx;
398

399 400 401 402 403
  sprintf (buf, "%d: window %p (`%s')%s\n",
	   history_tick++,
	   w,
	   ((BUFFERP (w->buffer)
	     && STRINGP (XBUFFER (w->buffer)->name))
404
	    ? (char *) SDATA (XBUFFER (w->buffer)->name)
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
	    : "???"),
	   paused_p ? " ***paused***" : "");
  strcat (buf, msg);
}


/* Add to the redisplay history that frame F has been displayed.
   PAUSED_P non-zero means that the update has been interrupted for
   pending input.  */

static void
add_frame_display_history (f, paused_p)
     struct frame *f;
     int paused_p;
{
  char *buf;
421

422 423 424 425
  if (history_idx >= REDISPLAY_HISTORY_SIZE)
    history_idx = 0;
  buf = redisplay_history[history_idx].trace;
  ++history_idx;
426

427 428 429 430 431 432 433 434
  sprintf (buf, "%d: update frame %p%s",
	   history_tick++,
	   f, paused_p ? " ***paused***" : "");
}


DEFUN ("dump-redisplay-history", Fdump_redisplay_history,
       Sdump_redisplay_history, 0, 0, "",
435 436
       doc: /* Dump redisplay history to stderr.  */)
     ()
437 438 439 440 441 442 443 444 445 446 447 448 449 450
{
  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;
}


Gerd Moellmann's avatar
Gerd Moellmann committed
451
#else /* GLYPH_DEBUG == 0 */
Jim Blandy's avatar
Jim Blandy committed
452

453 454
#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
455

Gerd Moellmann's avatar
Gerd Moellmann committed
456
#endif /* GLYPH_DEBUG == 0 */
Jim Blandy's avatar
Jim Blandy committed
457 458


Gerd Moellmann's avatar
Gerd Moellmann committed
459 460 461
/* 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
462 463 464

void
safe_bcopy (from, to, size)
465 466
     const char *from;
     char *to;
Jim Blandy's avatar
Jim Blandy committed
467 468
     int size;
{
469
  if (size <= 0 || from == to)
Jim Blandy's avatar
Jim Blandy committed
470 471
    return;

472 473 474 475 476 477 478 479
  /* 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
480
    {
481
      register const char *endf = from + size;
482
      register char *endt = to + size;
Jim Blandy's avatar
Jim Blandy committed
483 484 485 486 487

      /* 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
488 489
	 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
490 491 492 493 494 495 496 497
      if (to - from < 64)
	{
	  do
	    *--endt = *--endf;
	  while (endf != from);
	}
      else
	{
498
	  for (;;)
Jim Blandy's avatar
Jim Blandy committed
499 500 501 502
	    {
	      endt -= (to - from);
	      endf -= (to - from);

503 504 505
	      if (endt < to)
		break;

Jim Blandy's avatar
Jim Blandy committed
506 507
	      bcopy (endf, endt, to - from);
	    }
508 509

	  /* If SIZE wasn't a multiple of TO - FROM, there will be a
Gerd Moellmann's avatar
Gerd Moellmann committed
510 511
	     little left over.  The amount left over is (endt + (to -
	     from)) - to, which is endt - from.  */
Jim Blandy's avatar
Jim Blandy committed
512 513 514
	  bcopy (from, to, endt - from);
	}
    }
515
}
Jim Blandy's avatar
Jim Blandy committed
516 517


Gerd Moellmann's avatar
Gerd Moellmann committed
518 519 520 521 522 523 524 525 526 527 528 529 530 531

/***********************************************************************
			    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
532
{
Gerd Moellmann's avatar
Gerd Moellmann committed
533 534 535 536 537
  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
538

Gerd Moellmann's avatar
Gerd Moellmann committed
539 540 541
  /* Increment number of allocated matrices.  This count is used
     to detect memory leaks.  */
  ++glyph_matrix_count;
Jim Blandy's avatar
Jim Blandy committed
542

Gerd Moellmann's avatar
Gerd Moellmann committed
543 544 545
  /* Set pool and return.  */
  result->pool = pool;
  return result;
Jim Blandy's avatar
Jim Blandy committed
546 547 548
}


Gerd Moellmann's avatar
Gerd Moellmann committed
549 550 551 552 553 554
/* 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.
555

Gerd Moellmann's avatar
Gerd Moellmann committed
556 557 558 559 560 561 562
   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
563
{
Gerd Moellmann's avatar
Gerd Moellmann committed
564 565 566 567 568 569 570 571 572 573 574 575 576
  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]);
577

Gerd Moellmann's avatar
Gerd Moellmann committed
578 579 580 581 582
      /* Free row structures and the matrix itself.  */
      xfree (matrix->rows);
      xfree (matrix);
    }
}
Jim Blandy's avatar
Jim Blandy committed
583 584


Gerd Moellmann's avatar
Gerd Moellmann committed
585 586 587 588 589 590 591 592 593 594 595 596 597
/* 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
598

Gerd Moellmann's avatar
Gerd Moellmann committed
599
  if (NUMBERP (margin))
Jim Blandy's avatar
Jim Blandy committed
600
    {
601
      int width = XFASTINT (w->total_cols);
Gerd Moellmann's avatar
Gerd Moellmann committed
602 603
      double d = max (0, XFLOATINT (margin));
      d = min (width / 2 - 1, d);
604
      n = (int) ((double) total_glyphs / width * d);
Gerd Moellmann's avatar
Gerd Moellmann committed
605 606 607 608 609 610 611
    }
  else
    n = 0;

  return n;
}

Jim Blandy's avatar
Jim Blandy committed
612

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

Gerd Moellmann's avatar
Gerd Moellmann committed
616 617 618 619 620
   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.
621

Gerd Moellmann's avatar
Gerd Moellmann committed
622 623 624 625
   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.
626

Gerd Moellmann's avatar
Gerd Moellmann committed
627 628 629 630 631 632 633
   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.  */
634

Gerd Moellmann's avatar
Gerd Moellmann committed
635 636 637 638 639 640 641 642 643 644
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
645 646
  int header_line_changed_p = 0;
  int header_line_p = 0;
Gerd Moellmann's avatar
Gerd Moellmann committed
647
  int left = -1, right = -1;
648
  int window_width = -1, window_height;
Gerd Moellmann's avatar
Gerd Moellmann committed
649

650 651
  /* 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
652 653
  if (w)
    {
654 655
      window_box (w, -1, 0, 0, &window_width, &window_height);

Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
656 657
      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
658
    }
Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
659
  matrix->header_line_p = header_line_p;
660

661 662
  /* 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
663 664 665 666
     haven't changed.  This optimization is important because preserving
     the matrix means preventing redisplay.  */
  if (matrix->pool == NULL)
    {
667 668
      left = margin_glyphs_to_reserve (w, dim.width, w->left_margin_cols);
      right = margin_glyphs_to_reserve (w, dim.width, w->right_margin_cols);
Gerd Moellmann's avatar
Gerd Moellmann committed
669 670 671 672 673 674
      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
675
	  && !header_line_changed_p
676 677
	  && 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
678 679 680 681 682
	  && matrix->window_height == window_height
	  && matrix->window_vscroll == w->vscroll
	  && matrix->window_width == window_width)
	return;
    }
683

Gerd Moellmann's avatar
Gerd Moellmann committed
684 685 686 687 688 689 690 691 692 693 694 695
  /* 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;
696

Gerd Moellmann's avatar
Gerd Moellmann committed
697 698 699 700 701 702
  /* 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);
703

Gerd Moellmann's avatar
Gerd Moellmann committed
704
      if (w)
705
	{
Gerd Moellmann's avatar
Gerd Moellmann committed
706
	  left = margin_glyphs_to_reserve (w, dim.width,
707
					   w->left_margin_cols);
Gerd Moellmann's avatar
Gerd Moellmann committed
708
	  right = margin_glyphs_to_reserve (w, dim.width,
709
					    w->right_margin_cols);
710
	}
Gerd Moellmann's avatar
Gerd Moellmann committed
711 712
      else
	left = right = 0;
713

Gerd Moellmann's avatar
Gerd Moellmann committed
714
      for (i = 0; i < dim.height; ++i)
715
	{
Gerd Moellmann's avatar
Gerd Moellmann committed
716
	  struct glyph_row *row = &matrix->rows[i];
717 718

	  row->glyphs[LEFT_MARGIN_AREA]
Gerd Moellmann's avatar
Gerd Moellmann committed
719 720 721
	    = (matrix->pool->glyphs
	       + (y + i) * matrix->pool->ncolumns
	       + x);
722

Gerd Moellmann's avatar
Gerd Moellmann committed
723 724
	  if (w == NULL
	      || row == matrix->rows + dim.height - 1
Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
725
	      || (row == matrix->rows && matrix->header_line_p))
Gerd Moellmann's avatar
Gerd Moellmann committed
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
	    {
	      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;
	    }
743
	}
744

Gerd Moellmann's avatar
Gerd Moellmann committed
745 746 747 748 749 750
      matrix->left_margin_glyphs = left;
      matrix->right_margin_glyphs = right;
    }
  else
    {
      /* If MATRIX->pool is null, MATRIX is responsible for managing
751 752
	 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
753 754
      if (dim.width > matrix->matrix_w
	  || new_rows
Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
755
	  || header_line_changed_p
Gerd Moellmann's avatar
Gerd Moellmann committed
756
	  || marginal_areas_changed_p)
Jim Blandy's avatar
Jim Blandy committed
757
	{
Gerd Moellmann's avatar
Gerd Moellmann committed
758 759
	  struct glyph_row *row = matrix->rows;
	  struct glyph_row *end = row + matrix->rows_allocated;
760

Gerd Moellmann's avatar
Gerd Moellmann committed
761 762 763 764 765 766
	  while (row < end)
	    {
	      row->glyphs[LEFT_MARGIN_AREA]
		= (struct glyph *) xrealloc (row->glyphs[LEFT_MARGIN_AREA],
					     (dim.width
					      * sizeof (struct glyph)));
767

Gerd Moellmann's avatar
Gerd Moellmann committed
768 769
	      /* The mode line never has marginal areas.  */
	      if (row == matrix->rows + dim.height - 1
Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
770
		  || (row == matrix->rows && matrix->header_line_p))
Gerd Moellmann's avatar
Gerd Moellmann committed
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
		{
		  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
790 791
	}

Gerd Moellmann's avatar
Gerd Moellmann committed
792 793 794 795
      xassert (left >= 0 && right >= 0);
      matrix->left_margin_glyphs = left;
      matrix->right_margin_glyphs = right;
    }
796

Gerd Moellmann's avatar
Gerd Moellmann committed
797 798
  /* Number of rows to be used by MATRIX.  */
  matrix->nrows = dim.height;
799
  xassert (matrix->nrows >= 0);
Gerd Moellmann's avatar
Gerd Moellmann committed
800

801
  if (w)
Gerd Moellmann's avatar
Gerd Moellmann committed
802
    {
803
      if (matrix == w->current_matrix)
Jim Blandy's avatar
Jim Blandy committed
804
	{
805 806 807 808 809 810
	  /* 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);
811

812 813 814 815
	  /* 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
816 817 818
	      && !header_line_changed_p
	      && new_rows == 0
	      && dim.width == matrix->matrix_w
819 820
	      && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
	      && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
821
	      && matrix->window_width == window_width)
822
	    {
823 824 825 826
	      /* 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)
		  {
827
		    ++i;
828 829
		    break;
		  }
830 831

	      /* Window end is invalid, if inside of the rows that
832
		 are invalidated below.  */
833 834 835
	      if (INTEGERP (w->window_end_vpos)
		  && XFASTINT (w->window_end_vpos) >= i)
		w->window_end_valid = Qnil;
836

837 838 839 840 841 842 843 844
	      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
845
	}
846
      else if (matrix == w->desired_matrix)
Gerd Moellmann's avatar
Gerd Moellmann committed
847
	{
848 849 850 851
	  /* 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
852 853
	  for (i = 0; i < matrix->nrows; ++i)
	    matrix->rows[i].enabled_p = 0;
Jim Blandy's avatar
Jim Blandy committed
854 855
	}
    }
856 857


Gerd Moellmann's avatar
Gerd Moellmann committed
858 859 860 861 862 863 864 865 866
  /* 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
867
    {
868 869
      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
870 871 872 873 874
      matrix->window_height = window_height;
      matrix->window_width = window_width;
      matrix->window_vscroll = w->vscroll;
    }
}
Jim Blandy's avatar
Jim Blandy committed
875 876


Gerd Moellmann's avatar
Gerd Moellmann committed
877 878 879 880
/* 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).  */
881

Gerd Moellmann's avatar
Gerd Moellmann committed
882 883 884 885 886 887
static void
reverse_rows (matrix, start, end)
     struct glyph_matrix *matrix;
     int start, end;
{
  int i, j;
888

Gerd Moellmann's avatar
Gerd Moellmann committed
889 890 891 892 893 894 895 896 897 898
  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;
    }
}
899 900


Gerd Moellmann's avatar
Gerd Moellmann committed
901 902 903 904
/* 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).
905

Gerd Moellmann's avatar
Gerd Moellmann committed
906 907 908 909 910 911 912 913 914 915 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
   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
941
increment_matrix_positions (matrix, start, end, delta, delta_bytes)
Gerd Moellmann's avatar
Gerd Moellmann committed
942 943 944 945 946 947 948 949 950
     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)
951
    increment_row_positions (matrix->rows + start, delta, delta_bytes);
Gerd Moellmann's avatar
Gerd Moellmann committed
952 953 954 955 956 957 958 959 960 961 962 963 964
}


/* 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;
{
965 966 967
  xassert (start <= end);
  xassert (start >= 0 && start < matrix->nrows);
  xassert (end >= 0 && end <= matrix->nrows);
968

Gerd Moellmann's avatar
Gerd Moellmann committed
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
  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;
    }
}
994

Gerd Moellmann's avatar
Gerd Moellmann committed
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006

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

Gerd Moellmann's avatar
Gerd Moellmann committed
1008 1009 1010
  xassert (start <= end);
  xassert (start >= 0 && start < matrix->nrows);
  xassert (end >= 0 && end <= matrix->nrows);
1011

1012 1013
  min_y = WINDOW_HEADER_LINE_HEIGHT (w);
  max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
1014

Gerd Moellmann's avatar
Gerd Moellmann committed
1015 1016 1017
  for (; start < end; ++start)
    {
      struct glyph_row *row = &matrix->rows[start];
1018

Gerd Moellmann's avatar
Gerd Moellmann committed
1019
      row->y += dy;
1020
      row->visible_height = row->height;
1021

Gerd Moellmann's avatar
Gerd Moellmann committed
1022
      if (row->y < min_y)
1023 1024 1025
	row->visible_height -= min_y - row->y;
      if (row->y + row->height > max_y)
	row->visible_height -= row->y + row->height - max_y;
Gerd Moellmann's avatar
Gerd Moellmann committed
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
    }
}


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

1048 1049 1050
  /* 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
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065

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

Gerd Moellmann's avatar
Gerd Moellmann committed
1067 1068 1069
  if (WINDOWP (f->menu_bar_window))
    clear_glyph_matrix (XWINDOW (f->menu_bar_window)->desired_matrix);

1070 1071
  if (WINDOWP (f->tool_bar_window))
    clear_glyph_matrix (XWINDOW (f->tool_bar_window)->desired_matrix);
Gerd Moellmann's avatar
Gerd Moellmann committed
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

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

1126 1127
static struct glyph_row null_row;

Gerd Moellmann's avatar
Gerd Moellmann committed
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
void
clear_glyph_row (row)
     struct glyph_row *row;
{
  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];
1148 1149 1150 1151 1152 1153

#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
1154
	 the code below.  If the flickering is gone with that, chances