dispnew.c 57.4 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Updating of data structures for redisplay.
Jim Blandy's avatar
Jim Blandy committed
2
   Copyright (C) 1985, 1986, 1987, 1988, 1990, 1992 Free Software Foundation, Inc.
Jim Blandy's avatar
Jim Blandy committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

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
the Free Software Foundation; either version 1, or (at your option)
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
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */


#include <signal.h>

#include "config.h"
#include <stdio.h>
#include <ctype.h>

#include "termchar.h"
#include "termopts.h"
#include "cm.h"
#include "lisp.h"
#include "dispextern.h"
#include "buffer.h"
Jim Blandy's avatar
Jim Blandy committed
33
#include "frame.h"
Jim Blandy's avatar
Jim Blandy committed
34 35 36 37 38
#include "window.h"
#include "commands.h"
#include "disptab.h"
#include "indent.h"

Jim Blandy's avatar
Jim Blandy committed
39 40 41
#include "systerm.h"
#include "systime.h"

Jim Blandy's avatar
Jim Blandy committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55
#ifdef HAVE_X_WINDOWS
#include "xterm.h"
#endif	/* HAVE_X_WINDOWS */

#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

#ifndef PENDING_OUTPUT_COUNT
/* Get number of chars of output now in the buffer of a stdio stream.
   This ought to be built in in stdio, but it isn't.
   Some s- files override this because their stdio internals differ.  */
#define PENDING_OUTPUT_COUNT(FILE) ((FILE)->_ptr - (FILE)->_base)
#endif

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

Jim Blandy's avatar
Jim Blandy committed
59
int frame_garbaged;
Jim Blandy's avatar
Jim Blandy committed
60 61 62 63 64 65 66 67 68 69

/* Nonzero means last display completed.  Zero means it was preempted. */

int display_completed;

/* Lisp variable visible-bell; enables use of screen-flash
   instead of audible bell.  */

int visible_bell;

Jim Blandy's avatar
Jim Blandy committed
70
/* Invert the color of the whole frame, at a low level.  */
Jim Blandy's avatar
Jim Blandy committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

int inverse_video;

/* Line speed of the terminal.  */

int baud_rate;

/* nil or a symbol naming the window system under which emacs is
   running ('x is the only current possibility).  */

Lisp_Object Vwindow_system;

/* Version number of X windows: 10, 11 or nil.  */
Lisp_Object Vwindow_system_version;

/* Vector of glyph definitions.  Indexed by glyph number,
   the contents are a string which is how to output the glyph.

   If Vglyph_table is nil, a glyph is output by using its low 8 bits
   as a character code.  */

Lisp_Object Vglyph_table;

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

Lisp_Object Vstandard_display_table;

/* Nonzero means reading single-character input with prompt
Jim Blandy's avatar
Jim Blandy committed
99 100 101
   so put cursor on minibuffer 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
102 103
int cursor_in_echo_area;

Jim Blandy's avatar
Jim Blandy committed
104 105
/* The currently selected frame.
   In a single-frame version, this variable always remains 0.  */
Jim Blandy's avatar
Jim Blandy committed
106

Jim Blandy's avatar
Jim Blandy committed
107
FRAME_PTR selected_frame;
Jim Blandy's avatar
Jim Blandy committed
108

Jim Blandy's avatar
Jim Blandy committed
109 110 111 112
/* A frame which is not just a minibuffer, or 0 if there are no such
   frames.  This is usually the most recent such frame that was
   selected.  In a single-frame version, this variable always remains 0.  */
FRAME_PTR last_nonminibuf_frame;
Jim Blandy's avatar
Jim Blandy committed
113

Jim Blandy's avatar
Jim Blandy committed
114 115
/* In a single-frame version, the information that would otherwise
   exist inside frame objects lives in the following structure instead.  */
Jim Blandy's avatar
Jim Blandy committed
116

Jim Blandy's avatar
Jim Blandy committed
117 118
#ifndef MULTI_FRAME
struct frame the_only_frame;
Jim Blandy's avatar
Jim Blandy committed
119
#endif
Jim Blandy's avatar
Jim Blandy committed
120 121

/* This is a vector, made larger whenever it isn't large enough,
Jim Blandy's avatar
Jim Blandy committed
122 123 124
   which is used inside `update_frame' to hold the old contents
   of the FRAME_PHYS_LINES of the frame being updated.  */
struct frame_glyphs **ophys_lines;
Jim Blandy's avatar
Jim Blandy committed
125 126 127 128 129 130 131 132 133 134 135
/* Length of vector currently allocated.  */
int ophys_lines_length;

FILE *termscript;	/* Stdio stream being used for copy of all output.  */

struct cm Wcm;		/* Structure for info on cursor positioning */

extern short ospeed;	/* Output speed (from sg_ospeed) */

int delayed_size_change;  /* 1 means SIGWINCH happened when not safe.  */

Jim Blandy's avatar
Jim Blandy committed
136
#ifdef MULTI_FRAME
Jim Blandy's avatar
Jim Blandy committed
137

Jim Blandy's avatar
Jim Blandy committed
138 139 140 141
DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 1, 1, 0,
  "Clear frame FRAME and output again what is supposed to appear on it.")
  (frame)
     Lisp_Object frame;
Jim Blandy's avatar
Jim Blandy committed
142
{
Jim Blandy's avatar
Jim Blandy committed
143
  FRAME_PTR f;
Jim Blandy's avatar
Jim Blandy committed
144

Jim Blandy's avatar
Jim Blandy committed
145 146 147
  CHECK_LIVE_FRAME (frame, 0);
  f = XFRAME (frame);
  update_begin (f);
Jim Blandy's avatar
Jim Blandy committed
148
  /*  set_terminal_modes (); */
Jim Blandy's avatar
Jim Blandy committed
149
  clear_frame ();
150
  clear_frame_records (f);
Jim Blandy's avatar
Jim Blandy committed
151
  update_end (f);
Jim Blandy's avatar
Jim Blandy committed
152 153 154 155
  fflush (stdout);
  windows_or_buffers_changed++;
  /* Mark all windows as INaccurate,
     so that every window will have its redisplay done.  */
Jim Blandy's avatar
Jim Blandy committed
156 157
  mark_window_display_accurate (FRAME_ROOT_WINDOW (f), 0);
  f->garbaged = 0;
Jim Blandy's avatar
Jim Blandy committed
158 159 160 161
  return Qnil;
}

DEFUN ("redraw-display", Fredraw_display, Sredraw_display, 0, 0, "",
Jim Blandy's avatar
Jim Blandy committed
162
  "Redraw all frames marked as having their images garbled.")
Jim Blandy's avatar
Jim Blandy committed
163 164
  ()
{
Jim Blandy's avatar
Jim Blandy committed
165
  Lisp_Object frame, tail;
Jim Blandy's avatar
Jim Blandy committed
166

Jim Blandy's avatar
Jim Blandy committed
167
  for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
Jim Blandy's avatar
Jim Blandy committed
168
    {
Jim Blandy's avatar
Jim Blandy committed
169 170 171
      frame = XCONS (tail)->car;
      if (XFRAME (frame)->garbaged && XFRAME (frame)->visible)
	Fredraw_frame (frame);
Jim Blandy's avatar
Jim Blandy committed
172 173 174 175
    }
  return Qnil;
}

Jim Blandy's avatar
Jim Blandy committed
176 177
redraw_frame (f)
     FRAME_PTR f;
Jim Blandy's avatar
Jim Blandy committed
178
{
Jim Blandy's avatar
Jim Blandy committed
179 180 181
  Lisp_Object frame;
  XSET (frame, Lisp_Frame, f);
  Fredraw_frame (frame);
Jim Blandy's avatar
Jim Blandy committed
182 183
}

Jim Blandy's avatar
Jim Blandy committed
184
#else /* not MULTI_FRAME */
Jim Blandy's avatar
Jim Blandy committed
185 186 187 188 189 190 191

DEFUN ("redraw-display", Fredraw_display, Sredraw_display, 0, 0, 0,
  "Clear screen and output again what is supposed to appear on it.")
  ()
{
  update_begin (0);
  set_terminal_modes ();
Jim Blandy's avatar
Jim Blandy committed
192
  clear_frame ();
Jim Blandy's avatar
Jim Blandy committed
193 194
  update_end (0);
  fflush (stdout);
Jim Blandy's avatar
Jim Blandy committed
195
  clear_frame_records (0);
Jim Blandy's avatar
Jim Blandy committed
196 197 198 199 200 201 202
  windows_or_buffers_changed++;
  /* Mark all windows as INaccurate,
     so that every window will have its redisplay done.  */
  mark_window_display_accurate (XWINDOW (minibuf_window)->prev, 0);
  return Qnil;
}

Jim Blandy's avatar
Jim Blandy committed
203
#endif /* not MULTI_FRAME */
Jim Blandy's avatar
Jim Blandy committed
204

Jim Blandy's avatar
Jim Blandy committed
205 206 207
static struct frame_glyphs *
make_frame_glyphs (frame, empty)
     register FRAME_PTR frame;
Jim Blandy's avatar
Jim Blandy committed
208 209 210
     int empty;
{
  register int i;
Jim Blandy's avatar
Jim Blandy committed
211 212 213 214
  register width = FRAME_WIDTH (frame);
  register height = FRAME_HEIGHT (frame);
  register struct frame_glyphs *new =
    (struct frame_glyphs *) xmalloc (sizeof (struct frame_glyphs));
Jim Blandy's avatar
Jim Blandy committed
215

Jim Blandy's avatar
Jim Blandy committed
216
  SET_GLYPHS_FRAME (new, frame);
Jim Blandy's avatar
Jim Blandy committed
217 218 219 220 221 222 223 224 225 226
  new->height = height;
  new->width = width;
  new->used = (int *) xmalloc (height * sizeof (int));
  new->glyphs = (GLYPH **) xmalloc (height * sizeof (GLYPH *));
  new->highlight = (char *) xmalloc (height * sizeof (char));
  new->enable = (char *) xmalloc (height * sizeof (char));
  bzero (new->enable, height * sizeof (char));
  new->bufp = (int *) xmalloc (height * sizeof (int));

#ifdef HAVE_X_WINDOWS
227
  if (FRAME_X_P (frame))
Jim Blandy's avatar
Jim Blandy committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241
    {
      new->nruns = (int *) xmalloc (height * sizeof (int));
      new->face_list
	= (struct run **) xmalloc (height * sizeof (struct run *));
      new->top_left_x = (short *) xmalloc (height * sizeof (short));
      new->top_left_y = (short *) xmalloc (height * sizeof (short));
      new->pix_width = (short *) xmalloc (height * sizeof (short));
      new->pix_height = (short *) xmalloc (height * sizeof (short));
    }
#endif

  if (empty)
    {
      /* Make the buffer used by decode_mode_spec.  This buffer is also
Jim Blandy's avatar
Jim Blandy committed
242
         used as temporary storage when updating the frame.  See scroll.c. */
Jim Blandy's avatar
Jim Blandy committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
      unsigned int total_glyphs = (width + 2) * sizeof (GLYPH);

      new->total_contents = (GLYPH *) xmalloc (total_glyphs);
      bzero (new->total_contents, total_glyphs);
    }
  else
    {
      unsigned int total_glyphs = height * (width + 2) * sizeof (GLYPH);

      new->total_contents = (GLYPH *) xmalloc (total_glyphs);
      bzero (new->total_contents, total_glyphs);
      for (i = 0; i < height; i++)
	new->glyphs[i] = new->total_contents + i * (width + 2) + 1;
    }

  return new;
}

static void
Jim Blandy's avatar
Jim Blandy committed
262 263 264
free_frame_glyphs (frame, glyphs)
     FRAME_PTR frame;
     struct frame_glyphs *glyphs;
Jim Blandy's avatar
Jim Blandy committed
265 266 267 268 269 270 271 272 273 274 275
{
  if (glyphs->total_contents)
    free (glyphs->total_contents);

  free (glyphs->used);
  free (glyphs->glyphs);
  free (glyphs->highlight);
  free (glyphs->enable);
  free (glyphs->bufp);

#ifdef HAVE_X_WINDOWS
276
  if (FRAME_X_P (frame))
Jim Blandy's avatar
Jim Blandy committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290
    {
      free (glyphs->nruns);
      free (glyphs->face_list);
      free (glyphs->top_left_x);
      free (glyphs->top_left_y);
      free (glyphs->pix_width);
      free (glyphs->pix_height);
    }
#endif

  free (glyphs);
}

static void
Jim Blandy's avatar
Jim Blandy committed
291 292
remake_frame_glyphs (frame)
     FRAME_PTR frame;
Jim Blandy's avatar
Jim Blandy committed
293
{
Jim Blandy's avatar
Jim Blandy committed
294 295 296 297 298 299 300 301 302 303 304
  if (FRAME_CURRENT_GLYPHS (frame))
    free_frame_glyphs (frame, FRAME_CURRENT_GLYPHS (frame));
  if (FRAME_DESIRED_GLYPHS (frame))
    free_frame_glyphs (frame, FRAME_DESIRED_GLYPHS (frame));
  if (FRAME_TEMP_GLYPHS (frame))
    free_frame_glyphs (frame, FRAME_TEMP_GLYPHS (frame));

  if (FRAME_MESSAGE_BUF (frame))
    FRAME_MESSAGE_BUF (frame)
      = (char *) xrealloc (FRAME_MESSAGE_BUF (frame),
			   FRAME_WIDTH (frame) + 1);
Jim Blandy's avatar
Jim Blandy committed
305
  else
Jim Blandy's avatar
Jim Blandy committed
306 307
    FRAME_MESSAGE_BUF (frame)
      = (char *) xmalloc (FRAME_WIDTH (frame) + 1);
Jim Blandy's avatar
Jim Blandy committed
308

Jim Blandy's avatar
Jim Blandy committed
309 310 311 312
  FRAME_CURRENT_GLYPHS (frame) = make_frame_glyphs (frame, 0);
  FRAME_DESIRED_GLYPHS (frame) = make_frame_glyphs (frame, 0);
  FRAME_TEMP_GLYPHS (frame) = make_frame_glyphs (frame, 1);
  SET_FRAME_GARBAGED (frame);
Jim Blandy's avatar
Jim Blandy committed
313 314
}

Jim Blandy's avatar
Jim Blandy committed
315
/* Return the hash code of contents of line VPOS in frame-matrix M.  */
Jim Blandy's avatar
Jim Blandy committed
316 317 318

static int
line_hash_code (m, vpos)
Jim Blandy's avatar
Jim Blandy committed
319
     register struct frame_glyphs *m;
Jim Blandy's avatar
Jim Blandy committed
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
     int vpos;
{
  register GLYPH *body, *end;
  register int h = 0;

  if (!m->enable[vpos])
    return 0;

  /* Give all lighlighted lines the same hash code
     so as to encourage scrolling to leave them in place.  */
  if (m->highlight[vpos])
    return -1;

  body = m->glyphs[vpos];

  if (must_write_spaces)
    while (1)
      {
	GLYPH g = *body++;

	if (g == 0)
	  break;
	h = (((h << 4) + (h >> 24)) & 0x0fffffff) + g - SPACEGLYPH;
      }
  else
    while (1)
      {
	GLYPH g = *body++;

	if (g == 0)
	  break;
	h = (((h << 4) + (h >> 24)) & 0x0fffffff) + g;
      }

  if (h)
    return h;
  return 1;
}

/* Return number of characters in line in M at vpos VPOS,
   except don't count leading and trailing spaces
   unless the terminal requires those to be explicitly output.  */

static unsigned int
line_draw_cost (m, vpos)
Jim Blandy's avatar
Jim Blandy committed
365
     struct frame_glyphs *m;
Jim Blandy's avatar
Jim Blandy committed
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
     int vpos;
{
  register GLYPH *beg = m->glyphs[vpos];
  register GLYPH *end = m->glyphs[vpos] + m->used[vpos];
  register int i;
  register int tlen = GLYPH_TABLE_LENGTH;
  register Lisp_Object *tbase = GLYPH_TABLE_BASE;

  /* Ignore trailing and leading spaces if we can.  */
  if (!must_write_spaces)
    {
      while ((end != beg) && (*end == SPACEGLYPH))
	--end;
      if (end == beg)
	return (0); /* All blank line. */

      while (*beg == SPACEGLYPH)
	++beg;
    }

  /* If we don't have a glyph-table, each glyph is one character,
     so return the number of glyphs.  */
  if (tbase == 0)
    return end - beg;

  /* Otherwise, scan the glyphs and accumulate their total size in I.  */
  i = 0;
  while ((beg <= end) && *beg)
    {
      register GLYPH g = *beg++;

      if (GLYPH_SIMPLE_P (tbase, tlen, g))
	i += 1;
      else
	i += GLYPH_LENGTH (tbase, g);
    }
  return i;
}

/* The functions on this page are the interface from xdisp.c to redisplay.

   The only other interface into redisplay is through setting
Jim Blandy's avatar
Jim Blandy committed
408 409
   FRAME_CURSOR_X (frame) and FRAME_CURSOR_Y (frame)
   and SET_FRAME_GARBAGED (frame).  */
Jim Blandy's avatar
Jim Blandy committed
410 411 412

/* cancel_line eliminates any request to display a line at position `vpos' */

Jim Blandy's avatar
Jim Blandy committed
413
cancel_line (vpos, frame)
Jim Blandy's avatar
Jim Blandy committed
414
     int vpos;
Jim Blandy's avatar
Jim Blandy committed
415
     register FRAME_PTR frame;
Jim Blandy's avatar
Jim Blandy committed
416
{
Jim Blandy's avatar
Jim Blandy committed
417
  FRAME_DESIRED_GLYPHS (frame)->enable[vpos] = 0;
Jim Blandy's avatar
Jim Blandy committed
418 419
}

Jim Blandy's avatar
Jim Blandy committed
420 421
clear_frame_records (frame)
     register FRAME_PTR frame;
Jim Blandy's avatar
Jim Blandy committed
422
{
Jim Blandy's avatar
Jim Blandy committed
423
  bzero (FRAME_CURRENT_GLYPHS (frame)->enable, FRAME_HEIGHT (frame));
Jim Blandy's avatar
Jim Blandy committed
424 425 426 427 428
}

/* Prepare to display on line VPOS starting at HPOS within it.  */

void
Jim Blandy's avatar
Jim Blandy committed
429 430
get_display_line (frame, vpos, hpos)
     register FRAME_PTR frame;
Jim Blandy's avatar
Jim Blandy committed
431 432 433
     int vpos;
     register int hpos;
{
Jim Blandy's avatar
Jim Blandy committed
434 435
  register struct frame_glyphs *glyphs;
  register struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (frame);
Jim Blandy's avatar
Jim Blandy committed
436 437
  register GLYPH *p;

Jim Blandy's avatar
Jim Blandy committed
438
  if (vpos < 0 || (! FRAME_VISIBLE_P (frame)))
Jim Blandy's avatar
Jim Blandy committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
    abort ();

  if ((desired_glyphs->enable[vpos]) && desired_glyphs->used[vpos] > hpos)
    abort ();

  if (! desired_glyphs->enable[vpos])
    {
      desired_glyphs->used[vpos] = 0;
      desired_glyphs->highlight[vpos] = 0;
      desired_glyphs->enable[vpos] = 1;
    }

  if (hpos > desired_glyphs->used[vpos])
    {
      GLYPH *g = desired_glyphs->glyphs[vpos] + desired_glyphs->used[vpos];
      GLYPH *end = desired_glyphs->glyphs[vpos] + hpos;

      desired_glyphs->used[vpos] = hpos;
      while (g != end)
	*g++ = SPACEGLYPH;
    }
}

/* Like bcopy except never gets confused by overlap.  */

void
safe_bcopy (from, to, size)
     char *from, *to;
     int size;
{
  register char *endf;
  register char *endt;

  if (size == 0)
    return;

  /* If destination is higher in memory, and overlaps source zone,
     copy from the end.  */
  if (from < to && from + size > to)
    {
      endf = from + size;
      endt = to + size;

      /* 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
	 anywhere.  Since I don't think the obvious copy loop is ever
	 too bad, I'm trying to err in its favor.  */
      if (to - from < 64)
	{
	  do
	    *--endt = *--endf;
	  while (endf != from);
	}
      else
	{
Jim Blandy's avatar
Jim Blandy committed
496 497
	  /* Since TO - FROM >= 64, the overlap is less than SIZE,
	     so we can always safely do this loop once.  */
Jim Blandy's avatar
Jim Blandy committed
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
	  while (endt > to)
	    {
	      endt -= (to - from);
	      endf -= (to - from);

	      bcopy (endf, endt, to - from);
	    }
	  
	  /* If TO - FROM wasn't a multiple of SIZE, there will be a
	     little left over.  The amount left over is
	     (endt + (to - from)) - to, which is endt - from.  */
	  bcopy (from, to, endt - from);
	}
    }
  else
    bcopy (from, to, size);
}     

#if 0
void
safe_bcopy (from, to, size)
     char *from, *to;
     int size;
{
  register char *endf;
  register char *endt;

  if (size == 0)
    return;

  /* If destination is higher in memory, and overlaps source zone,
     copy from the end. */
  if (from < to && from + size > to)
    {
      endf = from + size;
      endt = to + size;

      do
	*--endt = *--endf;
      while (endf != from);

      return;
    }

  bcopy (from, to, size);
}
#endif

Jim Blandy's avatar
Jim Blandy committed
546
/* Rotate a vector of SIZE bytes right, by DISTANCE bytes.
Jim Blandy's avatar
Jim Blandy committed
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
   DISTANCE may be negative.  */

static void
rotate_vector (vector, size, distance)
     char *vector;
     int size;
     int distance;
{
  char *temp = (char *) alloca (size);

  if (distance < 0)
    distance += size;

  bcopy (vector, temp + distance, size - distance);
  bcopy (vector + size - distance, temp, distance);
  bcopy (temp, vector, size);
}

/* Scroll lines from vpos FROM up to but not including vpos END
   down by AMOUNT lines (AMOUNT may be negative).
   Returns nonzero if done, zero if terminal cannot scroll them.  */

int
Jim Blandy's avatar
Jim Blandy committed
570 571
scroll_frame_lines (frame, from, end, amount)
     register FRAME_PTR frame;
Jim Blandy's avatar
Jim Blandy committed
572 573 574
     int from, end, amount;
{
  register int i;
Jim Blandy's avatar
Jim Blandy committed
575 576
  register struct frame_glyphs *current_frame
    = FRAME_CURRENT_GLYPHS (frame);
Jim Blandy's avatar
Jim Blandy committed
577 578 579 580 581 582 583 584 585

  if (!line_ins_del_ok)
    return 0;

  if (amount == 0)
    return 1;

  if (amount > 0)
    {
Jim Blandy's avatar
Jim Blandy committed
586
      update_begin (frame);
Jim Blandy's avatar
Jim Blandy committed
587 588 589 590 591 592
      set_terminal_window (end + amount);
      if (!scroll_region_ok)
	ins_del_lines (end, -amount);
      ins_del_lines (from, amount);
      set_terminal_window (0);

Jim Blandy's avatar
Jim Blandy committed
593
      rotate_vector (current_frame->glyphs + from,
Jim Blandy's avatar
Jim Blandy committed
594 595 596
		     sizeof (GLYPH *) * (end + amount - from),
		     amount * sizeof (GLYPH *));

Jim Blandy's avatar
Jim Blandy committed
597 598 599
      safe_bcopy (current_frame->used + from,
		  current_frame->used + from + amount,
		  (end - from) * sizeof current_frame->used[0]);
Jim Blandy's avatar
Jim Blandy committed
600

Jim Blandy's avatar
Jim Blandy committed
601 602 603
      safe_bcopy (current_frame->highlight + from,
		  current_frame->highlight + from + amount,
		  (end - from) * sizeof current_frame->highlight[0]);
Jim Blandy's avatar
Jim Blandy committed
604

Jim Blandy's avatar
Jim Blandy committed
605 606 607
      safe_bcopy (current_frame->enable + from,
		  current_frame->enable + from + amount,
		  (end - from) * sizeof current_frame->enable[0]);
Jim Blandy's avatar
Jim Blandy committed
608 609 610

      /* Mark the lines made empty by scrolling as enabled, empty and
	 normal video.  */
Jim Blandy's avatar
Jim Blandy committed
611 612 613 614
      bzero (current_frame->used + from,
	     amount * sizeof current_frame->used[0]);
      bzero (current_frame->highlight + from,
	     amount * sizeof current_frame->highlight[0]);
Jim Blandy's avatar
Jim Blandy committed
615 616
      for (i = from; i < from + amount; i++)
	{
Jim Blandy's avatar
Jim Blandy committed
617 618
	  current_frame->glyphs[i][0] = '\0';
	  current_frame->enable[i] = 1;
Jim Blandy's avatar
Jim Blandy committed
619 620
	}

Jim Blandy's avatar
Jim Blandy committed
621 622 623
      safe_bcopy (current_frame->bufp + from,
		  current_frame->bufp + from + amount,
		  (end - from) * sizeof current_frame->bufp[0]);
Jim Blandy's avatar
Jim Blandy committed
624 625

#ifdef HAVE_X_WINDOWS
626
      if (FRAME_X_P (frame))
Jim Blandy's avatar
Jim Blandy committed
627
	{
Jim Blandy's avatar
Jim Blandy committed
628 629 630
	  safe_bcopy (current_frame->nruns + from,
		      current_frame->nruns + from + amount,
		      (end - from) * sizeof current_frame->nruns[0]);
Jim Blandy's avatar
Jim Blandy committed
631

Jim Blandy's avatar
Jim Blandy committed
632 633 634
	  safe_bcopy (current_frame->face_list + from,
		      current_frame->face_list + from + amount,
		      (end - from) * sizeof current_frame->face_list[0]);
Jim Blandy's avatar
Jim Blandy committed
635

Jim Blandy's avatar
Jim Blandy committed
636 637 638
	  safe_bcopy (current_frame->top_left_x + from,
		      current_frame->top_left_x + from + amount,
		      (end - from) * sizeof current_frame->top_left_x[0]);
Jim Blandy's avatar
Jim Blandy committed
639

Jim Blandy's avatar
Jim Blandy committed
640 641 642
	  safe_bcopy (current_frame->top_left_y + from,
		      current_frame->top_left_y + from + amount,
		      (end - from) * sizeof current_frame->top_left_y[0]);
Jim Blandy's avatar
Jim Blandy committed
643

Jim Blandy's avatar
Jim Blandy committed
644 645 646
	  safe_bcopy (current_frame->pix_width + from,
		      current_frame->pix_width + from + amount,
		      (end - from) * sizeof current_frame->pix_width[0]);
Jim Blandy's avatar
Jim Blandy committed
647

Jim Blandy's avatar
Jim Blandy committed
648 649 650
	  safe_bcopy (current_frame->pix_height + from,
		      current_frame->pix_height + from + amount,
		      (end - from) * sizeof current_frame->pix_height[0]);
Jim Blandy's avatar
Jim Blandy committed
651 652 653
	}
#endif				/* HAVE_X_WINDOWS */

Jim Blandy's avatar
Jim Blandy committed
654
      update_end (frame);
Jim Blandy's avatar
Jim Blandy committed
655 656 657
    }
  if (amount < 0)
    {
Jim Blandy's avatar
Jim Blandy committed
658
      update_begin (frame);
Jim Blandy's avatar
Jim Blandy committed
659 660 661 662 663 664
      set_terminal_window (end);
      ins_del_lines (from + amount, amount);
      if (!scroll_region_ok)
	ins_del_lines (end + amount, -amount);
      set_terminal_window (0);

Jim Blandy's avatar
Jim Blandy committed
665
      rotate_vector (current_frame->glyphs + from + amount,
Jim Blandy's avatar
Jim Blandy committed
666 667 668
		     sizeof (GLYPH *) * (end - from - amount),
		     amount * sizeof (GLYPH *));

Jim Blandy's avatar
Jim Blandy committed
669 670 671
      safe_bcopy (current_frame->used + from,
		  current_frame->used + from + amount,
		  (end - from) * sizeof current_frame->used[0]);
Jim Blandy's avatar
Jim Blandy committed
672

Jim Blandy's avatar
Jim Blandy committed
673 674 675
      safe_bcopy (current_frame->highlight + from,
		  current_frame->highlight + from + amount,
		  (end - from) * sizeof current_frame->highlight[0]);
Jim Blandy's avatar
Jim Blandy committed
676

Jim Blandy's avatar
Jim Blandy committed
677 678 679
      safe_bcopy (current_frame->enable + from,
		  current_frame->enable + from + amount,
		  (end - from) * sizeof current_frame->enable[0]);
Jim Blandy's avatar
Jim Blandy committed
680 681 682

      /* Mark the lines made empty by scrolling as enabled, empty and
	 normal video.  */
Jim Blandy's avatar
Jim Blandy committed
683 684 685 686
      bzero (current_frame->used + end + amount,
	     - amount * sizeof current_frame->used[0]);
      bzero (current_frame->highlight + end + amount,
	     - amount * sizeof current_frame->highlight[0]);
Jim Blandy's avatar
Jim Blandy committed
687 688
      for (i = end + amount; i < end; i++)
	{
Jim Blandy's avatar
Jim Blandy committed
689 690
	  current_frame->glyphs[i][0] = '\0';
	  current_frame->enable[i] = 1;
Jim Blandy's avatar
Jim Blandy committed
691 692
	}

Jim Blandy's avatar
Jim Blandy committed
693 694 695
      safe_bcopy (current_frame->bufp + from,
		  current_frame->bufp + from + amount,
		  (end - from) * sizeof current_frame->bufp[0]);
Jim Blandy's avatar
Jim Blandy committed
696 697

#ifdef HAVE_X_WINDOWS
698
      if (FRAME_X_P (frame))
Jim Blandy's avatar
Jim Blandy committed
699
	{
Jim Blandy's avatar
Jim Blandy committed
700 701 702
	  safe_bcopy (current_frame->nruns + from,
		      current_frame->nruns + from + amount,
		      (end - from) * sizeof current_frame->nruns[0]);
Jim Blandy's avatar
Jim Blandy committed
703

Jim Blandy's avatar
Jim Blandy committed
704 705 706
	  safe_bcopy (current_frame->face_list + from,
		      current_frame->face_list + from + amount,
		      (end - from) * sizeof current_frame->face_list[0]);
Jim Blandy's avatar
Jim Blandy committed
707

Jim Blandy's avatar
Jim Blandy committed
708 709 710
	  safe_bcopy (current_frame->top_left_x + from,
		      current_frame->top_left_x + from + amount,
		      (end - from) * sizeof current_frame->top_left_x[0]);
Jim Blandy's avatar
Jim Blandy committed
711

Jim Blandy's avatar
Jim Blandy committed
712 713 714
	  safe_bcopy (current_frame->top_left_y + from,
		      current_frame->top_left_y + from + amount,
		      (end - from) * sizeof current_frame->top_left_y[0]);
Jim Blandy's avatar
Jim Blandy committed
715

Jim Blandy's avatar
Jim Blandy committed
716 717 718
	  safe_bcopy (current_frame->pix_width + from,
		      current_frame->pix_width + from + amount,
		      (end - from) * sizeof current_frame->pix_width[0]);
Jim Blandy's avatar
Jim Blandy committed
719

Jim Blandy's avatar
Jim Blandy committed
720 721 722
	  safe_bcopy (current_frame->pix_height + from,
		      current_frame->pix_height + from + amount,
		      (end - from) * sizeof current_frame->pix_height[0]);
Jim Blandy's avatar
Jim Blandy committed
723 724 725
	}
#endif				/* HAVE_X_WINDOWS */

Jim Blandy's avatar
Jim Blandy committed
726
      update_end (frame);
Jim Blandy's avatar
Jim Blandy committed
727 728 729 730
    }
  return 1;
}

Jim Blandy's avatar
Jim Blandy committed
731
/* After updating a window W that isn't the full frame wide,
Jim Blandy's avatar
Jim Blandy committed
732
   copy all the columns that W does not occupy
Jim Blandy's avatar
Jim Blandy committed
733 734
   into the FRAME_DESIRED_GLYPHS (frame) from the FRAME_PHYS_GLYPHS (frame)
   so that update_frame will not change those columns.  */
Jim Blandy's avatar
Jim Blandy committed
735 736 737 738 739

preserve_other_columns (w)
     struct window *w;
{
  register int vpos;
Jim Blandy's avatar
Jim Blandy committed
740 741
  register struct frame_glyphs *current_frame, *desired_frame;
  register FRAME_PTR frame = XFRAME (w->frame);
Jim Blandy's avatar
Jim Blandy committed
742 743 744 745
  int start = XFASTINT (w->left);
  int end = XFASTINT (w->left) + XFASTINT (w->width);
  int bot = XFASTINT (w->top) + XFASTINT (w->height);

Jim Blandy's avatar
Jim Blandy committed
746 747
  current_frame = FRAME_CURRENT_GLYPHS (frame);
  desired_frame = FRAME_DESIRED_GLYPHS (frame);
Jim Blandy's avatar
Jim Blandy committed
748 749 750

  for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
    {
Jim Blandy's avatar
Jim Blandy committed
751
      if (current_frame->enable[vpos] && desired_frame->enable[vpos])
Jim Blandy's avatar
Jim Blandy committed
752 753 754 755 756
	{
	  if (start > 0)
	    {
	      int len;

Jim Blandy's avatar
Jim Blandy committed
757 758 759 760 761
	      bcopy (current_frame->glyphs[vpos],
		     desired_frame->glyphs[vpos], start);
	      len = min (start, current_frame->used[vpos]);
	      if (desired_frame->used[vpos] < len)
		desired_frame->used[vpos] = len;
Jim Blandy's avatar
Jim Blandy committed
762
	    }
Jim Blandy's avatar
Jim Blandy committed
763 764
	  if (current_frame->used[vpos] > end
	      && desired_frame->used[vpos] < current_frame->used[vpos])
Jim Blandy's avatar
Jim Blandy committed
765
	    {
Jim Blandy's avatar
Jim Blandy committed
766 767
	      while (desired_frame->used[vpos] < end)
		desired_frame->glyphs[vpos][desired_frame->used[vpos]++]
Jim Blandy's avatar
Jim Blandy committed
768
		  = SPACEGLYPH;
Jim Blandy's avatar
Jim Blandy committed
769 770 771 772
	      bcopy (current_frame->glyphs[vpos] + end,
		     desired_frame->glyphs[vpos] + end,
		     current_frame->used[vpos] - end);
	      desired_frame->used[vpos] = current_frame->used[vpos];
Jim Blandy's avatar
Jim Blandy committed
773 774 775 776 777 778 779
	    }
	}
    }
}

#if 0

Jim Blandy's avatar
Jim Blandy committed
780
/* If window w does not need to be updated and isn't the full frame wide,
Jim Blandy's avatar
Jim Blandy committed
781
 copy all the columns that w does occupy
Jim Blandy's avatar
Jim Blandy committed
782 783
 into the FRAME_DESIRED_LINES (frame) from the FRAME_PHYS_LINES (frame)
 so that update_frame will not change those columns.
Jim Blandy's avatar
Jim Blandy committed
784 785 786 787 788 789 790

 Have not been able to figure out how to use this correctly.  */

preserve_my_columns (w)
     struct window *w;
{
  register int vpos, fin;
Jim Blandy's avatar
Jim Blandy committed
791 792
  register struct frame_glyphs *l1, *l2;
  register FRAME_PTR frame = XFRAME (w->frame);
Jim Blandy's avatar
Jim Blandy committed
793 794 795 796 797 798
  int start = XFASTINT (w->left);
  int end = XFASTINT (w->left) + XFASTINT (w->width);
  int bot = XFASTINT (w->top) + XFASTINT (w->height);

  for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
    {
Jim Blandy's avatar
Jim Blandy committed
799 800
      if ((l1 = FRAME_DESIRED_GLYPHS (frame)->glyphs[vpos + 1])
	  && (l2 = FRAME_PHYS_GLYPHS (frame)->glyphs[vpos + 1]))
Jim Blandy's avatar
Jim Blandy committed
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
	{
	  if (l2->length > start && l1->length < l2->length)
	    {
	      fin = l2->length;
	      if (fin > end) fin = end;
	      while (l1->length < start)
		l1->body[l1->length++] = ' ';
	      bcopy (l2->body + start, l1->body + start, fin - start);
	      l1->length = fin;
	    }
	}
    }
}

#endif

/* On discovering that the redisplay for a window was no good,
   cancel the columns of that window, so that when the window is
   displayed over again get_display_line will not complain.  */

cancel_my_columns (w)
     struct window *w;
{
  register int vpos;
Jim Blandy's avatar
Jim Blandy committed
825 826
  register struct frame_glyphs *desired_glyphs =
    FRAME_DESIRED_GLYPHS (XFRAME (w->frame));
Jim Blandy's avatar
Jim Blandy committed
827 828 829 830 831 832 833 834 835
  register int start = XFASTINT (w->left);
  register int bot = XFASTINT (w->top) + XFASTINT (w->height);

  for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
    if (desired_glyphs->enable[vpos]
	&& desired_glyphs->used[vpos] >= start)
      desired_glyphs->used[vpos] = start;
}

Jim Blandy's avatar
Jim Blandy committed
836
/* These functions try to perform directly and immediately on the frame
Jim Blandy's avatar
Jim Blandy committed
837 838 839
   the necessary output for one change in the buffer.
   They may return 0 meaning nothing was done if anything is difficult,
   or 1 meaning the output was performed properly.
Jim Blandy's avatar
Jim Blandy committed
840
   They assume that the frame was up to date before the buffer
Jim Blandy's avatar
Jim Blandy committed
841 842 843 844 845 846 847
   change being displayed.  THey make various other assumptions too;
   see command_loop_1 where these are called.  */

int
direct_output_for_insert (g)
     int g;
{
Jim Blandy's avatar
Jim Blandy committed
848 849 850
  register FRAME_PTR frame = selected_frame;
  register struct frame_glyphs *current_frame
    = FRAME_CURRENT_GLYPHS (frame);
Jim Blandy's avatar
Jim Blandy committed
851 852 853 854 855 856 857 858

#ifndef COMPILER_REGISTER_BUG
  register
#endif /* COMPILER_REGISTER_BUG */
    struct window *w = XWINDOW (selected_window);
#ifndef COMPILER_REGISTER_BUG
  register
#endif /* COMPILER_REGISTER_BUG */
Jim Blandy's avatar
Jim Blandy committed
859
    int hpos = FRAME_CURSOR_X (frame);
Jim Blandy's avatar
Jim Blandy committed
860 861 862
#ifndef COMPILER_REGISTER_BUG
  register
#endif /* COMPILER_REGISTER_BUG */
Jim Blandy's avatar
Jim Blandy committed
863
    int vpos = FRAME_CURSOR_Y (frame);
Jim Blandy's avatar
Jim Blandy committed
864 865 866 867 868 869 870 871

  /* Give up if about to continue line */
  if (hpos - XFASTINT (w->left) + 1 + 1 >= XFASTINT (w->width)

  /* Avoid losing if cursor is in invisible text off left margin */
      || (XINT (w->hscroll) && hpos == XFASTINT (w->left))
    
  /* Give up if cursor outside window (in minibuf, probably) */
Jim Blandy's avatar
Jim Blandy committed
872 873
      || FRAME_CURSOR_Y (frame) < XFASTINT (w->top)
      || FRAME_CURSOR_Y (frame) >= XFASTINT (w->top) + XFASTINT (w->height)
Jim Blandy's avatar
Jim Blandy committed
874

Jim Blandy's avatar
Jim Blandy committed
875
  /* Give up if cursor not really at FRAME_CURSOR_X, FRAME_CURSOR_Y */
Jim Blandy's avatar
Jim Blandy committed
876 877 878 879 880 881 882 883 884
      || !display_completed

  /* Give up if buffer appears in two places.  */
      || buffer_shared > 1

  /* Give up if w is minibuffer and a message is being displayed there */
      || (MINI_WINDOW_P (w) && echo_area_glyphs))
    return 0;

Jim Blandy's avatar
Jim Blandy committed
885
  current_frame->glyphs[vpos][hpos] = g;
Jim Blandy's avatar
Jim Blandy committed
886 887 888 889 890 891 892
  unchanged_modified = MODIFF;
  beg_unchanged = GPT - BEG;
  XFASTINT (w->last_point) = point;
  XFASTINT (w->last_point_x) = hpos;
  XFASTINT (w->last_modified) = MODIFF;

  reassert_line_highlight (0, vpos);
Jim Blandy's avatar
Jim Blandy committed
893
  write_glyphs (&current_frame->glyphs[vpos][hpos], 1);
Jim Blandy's avatar
Jim Blandy committed
894
  fflush (stdout);
Jim Blandy's avatar
Jim Blandy committed
895 896
  ++FRAME_CURSOR_X (frame);
  if (hpos == current_frame->used[vpos])
Jim Blandy's avatar
Jim Blandy committed
897
    {
Jim Blandy's avatar
Jim Blandy committed
898 899
      current_frame->used[vpos] = hpos + 1;
      current_frame->glyphs[vpos][hpos + 1] = 0;
Jim Blandy's avatar
Jim Blandy committed
900 901 902 903 904 905 906 907 908
    }

  return 1;
}

int
direct_output_forward_char (n)
     int n;
{
Jim Blandy's avatar
Jim Blandy committed
909
  register FRAME_PTR frame = selected_frame;
Jim Blandy's avatar
Jim Blandy committed
910 911
  register struct window *w = XWINDOW (selected_window);

Jim Blandy's avatar
Jim Blandy committed
912 913
  /* Avoid losing if cursor is in invisible text off left margin
     or about to go off either side of window.  */
Jim Blandy's avatar
Jim Blandy committed
914
  if ((FRAME_CURSOR_X (frame) == XFASTINT (w->left)
Jim Blandy's avatar
Jim Blandy committed
915 916
       && (XINT (w->hscroll) || n < 0))
      || (n > 0
Jim Blandy's avatar
Jim Blandy committed
917
	  && (FRAME_CURSOR_X (frame) + 1
Jim Blandy's avatar
Jim Blandy committed
918
	      >= (XFASTINT (w->left) + XFASTINT (w->width)
Jim Blandy's avatar
Jim Blandy committed
919
		  - (XFASTINT (w->width) < FRAME_WIDTH (frame))
Jim Blandy's avatar
Jim Blandy committed
920
		  - 1))))
Jim Blandy's avatar
Jim Blandy committed
921 922
    return 0;

Jim Blandy's avatar
Jim Blandy committed
923 924
  FRAME_CURSOR_X (frame) += n;
  XFASTINT (w->last_point_x) = FRAME_CURSOR_X (frame);
Jim Blandy's avatar
Jim Blandy committed
925
  XFASTINT (w->last_point) = point;
Jim Blandy's avatar
Jim Blandy committed
926
  cursor_to (FRAME_CURSOR_Y (frame), FRAME_CURSOR_X (frame));
Jim Blandy's avatar
Jim Blandy committed
927 928 929 930 931 932
  fflush (stdout);
  return 1;
}

static void update_line ();

Jim Blandy's avatar
Jim Blandy committed
933
/* Update frame F based on the data in FRAME_DESIRED_GLYPHS.
Jim Blandy's avatar
Jim Blandy committed
934 935 936 937
   Value is nonzero if redisplay stopped due to pending input.
   FORCE nonzero means do not stop for pending input.  */

int
Jim Blandy's avatar
Jim Blandy committed
938 939
update_frame (f, force, inhibit_hairy_id)
     FRAME_PTR f;
Jim Blandy's avatar
Jim Blandy committed
940 941 942
     int force;
     int inhibit_hairy_id;
{
Jim Blandy's avatar
Jim Blandy committed
943 944
  register struct frame_glyphs *current_frame = FRAME_CURRENT_GLYPHS (f);
  register struct frame_glyphs *desired_frame = FRAME_DESIRED_GLYPHS (f);
Jim Blandy's avatar
Jim Blandy committed
945 946 947 948 949 950 951 952
  register int i;
  int pause;
  int preempt_count = baud_rate / 2400 + 1;
  extern input_pending;
#ifdef HAVE_X_WINDOWS
  register int downto, leftmost;
#endif

Jim Blandy's avatar
Jim Blandy committed
953
  if (FRAME_HEIGHT (f) == 0) abort (); /* Some bug zeros some core */
Jim Blandy's avatar
Jim Blandy committed
954 955 956 957 958 959 960 961

  detect_input_pending ();
  if (input_pending && !force)
    {
      pause = 1;
      goto do_pause;
    }

Jim Blandy's avatar
Jim Blandy committed
962
  update_begin (f);
Jim Blandy's avatar
Jim Blandy committed
963 964 965 966

  if (!line_ins_del_ok)
    inhibit_hairy_id = 1;

Jim Blandy's avatar
Jim Blandy committed
967 968
  /* See if any of the desired lines are enabled; don't compute for
     i/d line if just want cursor motion. */
Jim Blandy's avatar
Jim Blandy committed
969 970
  for (i = 0; i < FRAME_HEIGHT (f); i++)
    if (desired_frame->enable[i])
Jim Blandy's avatar
Jim Blandy committed
971 972 973
      break;

  /* Try doing i/d line, if not yet inhibited.  */
Jim Blandy's avatar
Jim Blandy committed
974 975
  if (!inhibit_hairy_id && i < FRAME_HEIGHT (f))
    force |= scrolling (f);
Jim Blandy's avatar
Jim Blandy committed
976 977 978

  /* Update the individual lines as needed.  Do bottom line first.  */

Jim Blandy's avatar
Jim Blandy committed
979 980
  if (desired_frame->enable[FRAME_HEIGHT (f) - 1])
    update_line (f, FRAME_HEIGHT (f) - 1);
Jim Blandy's avatar
Jim Blandy committed
981 982

#ifdef HAVE_X_WINDOWS
983
  if (FRAME_X_P (f))
Jim Blandy's avatar
Jim Blandy committed
984
    {
Jim Blandy's avatar
Jim Blandy committed
985 986
      leftmost = downto = f->display.x->internal_border_width;
      if (desired_frame->enable[0])
Jim Blandy's avatar
Jim Blandy committed
987
	{
Jim Blandy's avatar
Jim Blandy committed
988 989 990 991 992 993
	  current_frame->top_left_x[FRAME_HEIGHT (f) - 1] = leftmost;
	  current_frame->top_left_y[FRAME_HEIGHT (f) - 1]
	    = PIXEL_HEIGHT (f) - f->display.x->internal_border_width
	      - LINE_HEIGHT(f, FRAME_HEIGHT (f) - 1);
	  current_frame->top_left_x[0] = leftmost;
	  current_frame->top_left_y[0] = downto;
Jim Blandy's avatar
Jim Blandy committed
994 995 996 997 998
	}
    }
#endif /* HAVE_X_WINDOWS */

  /* Now update the rest of the lines. */
Jim Blandy's avatar
Jim Blandy committed
999
  for (i = 0; i < FRAME_HEIGHT (f) - 1 && (force || !input_pending); i++)
Jim Blandy's avatar
Jim Blandy committed
1000
    {
Jim Blandy's avatar
Jim Blandy committed
1001
      if (desired_frame->enable[i])
Jim Blandy's avatar
Jim Blandy committed
1002
	{
1003
	  if (FRAME_TERMCAP_P (f))
Jim Blandy's avatar
Jim Blandy committed
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
	    {
	      /* Flush out every so many lines.
		 Also flush out if likely to have more than 1k buffered
		 otherwise.   I'm told that some telnet connections get
		 really screwed by more than 1k output at once.  */
	      int outq = PENDING_OUTPUT_COUNT (stdout);
	      if (outq > 900
		  || (outq > 20 && ((i - 1) % preempt_count == 0)))
		{
		  fflush (stdout);
		  if (preempt_count == 1)
		    {
Jim Blandy's avatar
Jim Blandy committed
1016 1017
#ifdef EMACS_OUTQSIZE
		      if (EMACS_OUTQSIZE (0, &outq) < 0)
Jim Blandy's avatar
Jim Blandy committed
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
			/* Probably not a tty.  Ignore the error and reset
			 * the outq count. */
			outq = PENDING_OUTPUT_COUNT (stdout);
#endif
		      outq *= 10;
		      sleep (outq / baud_rate);
		    }
		}
	      if ((i - 1) % preempt_count == 0)
		detect_input_pending ();
	    }

Jim Blandy's avatar
Jim Blandy committed
1030
	  update_line (f, i);
Jim Blandy's avatar
Jim Blandy committed
1031
#ifdef HAVE_X_WINDOWS
1032
	  if (FRAME_X_P (f))
Jim Blandy's avatar
Jim Blandy committed
1033
	    {
Jim Blandy's avatar
Jim Blandy committed
1034 1035
	      current_frame->top_left_y[i] = downto;
	      current_frame->top_left_x[i] = leftmost;
Jim Blandy's avatar
Jim Blandy committed
1036 1037 1038 1039
	    }
#endif /* HAVE_X_WINDOWS */
	}

Jim Blandy's avatar
Jim Blandy committed
1040
#ifdef HAVE_X_WINDOWS
1041
      if (FRAME_X_P (f))
Jim Blandy's avatar
Jim Blandy committed
1042
	downto += LINE_HEIGHT(f, i);
Jim Blandy's avatar
Jim Blandy committed
1043
#endif
Jim Blandy's avatar
Jim Blandy committed
1044
    }
Jim Blandy's avatar
Jim Blandy committed
1045
  pause = (i < FRAME_HEIGHT (f) - 1) ? i : 0;
Jim Blandy's avatar
Jim Blandy committed
1046 1047 1048 1049

  /* Now just clean up termcap drivers and set cursor, etc.  */
  if (!pause)
    {
Jim Blandy's avatar
Jim Blandy committed
1050 1051
      if (cursor_in_echo_area)
	{
Jim Blandy's avatar
Jim Blandy committed
1052
	  if (f == selected_frame
Jim Blandy's avatar
Jim Blandy committed
1053
	      && cursor_in_echo_area < 0)
Jim Blandy's avatar
Jim Blandy committed
1054 1055 1056 1057
	    cursor_to (FRAME_HEIGHT (f) - 1, 0);
	  else if (f == selected_frame
		   && ! current_frame->enable[FRAME_HEIGHT (f) - 1])
	    cursor_to (FRAME_HEIGHT (f) - 1, 0);
Jim Blandy's avatar
Jim Blandy committed
1058
	  else
Jim Blandy's avatar
Jim Blandy committed
1059 1060 1061
	    cursor_to (FRAME_HEIGHT (f) - 1,
		       min (FRAME_WIDTH (f) - 1,
			    current_frame->used[FRAME_HEIGHT (f) - 1]));
Jim Blandy's avatar
Jim Blandy committed
1062
	}
Jim Blandy's avatar
Jim Blandy committed
1063
      else
Jim Blandy's avatar
Jim Blandy committed
1064 1065
	cursor_to (FRAME_CURSOR_Y (f), max (min (FRAME_CURSOR_X (f),
						  FRAME_WIDTH (f) - 1), 0));
Jim Blandy's avatar
Jim Blandy committed
1066 1067
    }

Jim Blandy's avatar
Jim Blandy committed
1068
  update_end (f);
Jim Blandy's avatar
Jim Blandy committed
1069 1070 1071 1072 1073 1074 1075 1076

  if (termscript)
    fflush (termscript);
  fflush (stdout);

  /* Here if output is preempted because input is detected.  */
 do_pause:

Jim Blandy's avatar
Jim Blandy committed
1077
  if (FRAME_HEIGHT (f) == 0) abort (); /* Some bug zeros some core */
Jim Blandy's avatar
Jim Blandy committed
1078 1079
  display_completed = !pause;

Jim Blandy's avatar
Jim Blandy committed
1080
  bzero (desired_frame->enable, FRAME_HEIGHT (f));
Jim Blandy's avatar
Jim Blandy committed
1081 1082 1083 1084 1085 1086 1087 1088 1089
  return pause;
}

/* Called when about to quit, to check for doing so
   at an improper time.  */

void
quit_error_check ()
{
Jim Blandy's avatar
Jim Blandy committed
1090
  if (FRAME_DESIRED_GLYPHS (selected_frame) == 0)
Jim Blandy's avatar
Jim Blandy committed
1091
    return;
Jim Blandy's avatar
Jim Blandy committed
1092
  if (FRAME_DESIRED_GLYPHS (selected_frame)->enable[0])
Jim Blandy's avatar
Jim Blandy committed
1093
    abort ();
Jim Blandy's avatar
Jim Blandy committed
1094
  if (FRAME_DESIRED_GLYPHS (selected_frame)->enable[FRAME_HEIGHT (selected_frame) - 1])
Jim Blandy's avatar
Jim Blandy committed
1095 1096 1097 1098 1099 1100 1101
    abort ();
}

/* Decide what insert/delete line to do, and do it */

extern void scrolling_1 ();

Jim Blandy's avatar
Jim Blandy committed
1102 1103
scrolling (frame)
     FRAME_PTR frame;
Jim Blandy's avatar
Jim Blandy committed
1104 1105 1106 1107
{
  int unchanged_at_top, unchanged_at_bottom;
  int window_size;
  int changed_lines;
Jim Blandy's avatar
Jim Blandy committed
1108 1109 1110
  int *old_hash = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
  int *new_hash = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
  int *draw_cost = (int *) alloca (FRAME_HEIGHT (frame) * sizeof (int));
Jim Blandy's avatar
Jim Blandy committed
1111
  register int i;
Jim Blandy's avatar
Jim Blandy committed
1112 1113 1114
  int free_at_end_vpos = FRAME_HEIGHT (frame);
  register struct frame_glyphs *current_frame = FRAME_CURRENT_GLYPHS (frame);
  register struct frame_glyphs *desired_frame = FRAME_DESIRED_GLYPHS (frame);
Jim Blandy's avatar
Jim Blandy committed
1115 1116 1117 1118 1119 1120 1121 1122

  /* Compute hash codes of all the lines.
     Also calculate number of changed lines,
     number of unchanged lines at the beginning,
     and number of unchanged lines at the end.  */

  changed_lines = 0;
  unchanged_at_top = 0;
Jim Blandy's avatar
Jim Blandy committed
1123 1124
  unchanged_at_bottom = FRAME_HEIGHT (frame);
  for (i = 0; i < FRAME_HEIGHT (frame); i++)
Jim Blandy's avatar
Jim Blandy committed
1125 1126
    {
      /* Give up on this scrolling if some old lines are not enabled.  */
Jim Blandy's avatar
Jim Blandy committed
1127
      if (!current_frame->enable[i])
Jim Blandy's avatar
Jim Blandy committed
1128
	return 0;
Jim Blandy's avatar
Jim Blandy committed
1129 1130
      old_hash[i] = line_hash_code (current_frame, i);
      if (! desired_frame->enable[i])
Jim Blandy's avatar
Jim Blandy committed
1131 1132
	new_hash[i] = old_hash[i];
      else
Jim Blandy's avatar
Jim Blandy committed
1133
	new_hash[i] = line_hash_code (desired_frame, i);
Jim Blandy's avatar
Jim Blandy committed
1134 1135 1136 1137

      if (old_hash[i] != new_hash[i])
	{
	  changed_lines++;
Jim Blandy's avatar
Jim Blandy committed
1138
	  unchanged_at_bottom = FRAME_HEIGHT (frame) - i - 1;
Jim Blandy's avatar
Jim Blandy committed
1139 1140 1141
	}
      else if (i == unchanged_at_top)
	unchanged_at_top++;
Jim Blandy's avatar
Jim Blandy committed
1142
      draw_cost[i] = line_draw_cost (desired_frame, i);
Jim Blandy's avatar
Jim Blandy committed
1143 1144 1145 1146
    }

  /* If changed lines are