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

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
8
the Free Software Foundation; either version 2, or (at your option)
Jim Blandy's avatar
Jim Blandy committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
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>

27
#include "lisp.h"
Jim Blandy's avatar
Jim Blandy committed
28 29
#include "termchar.h"
#include "termopts.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
30
#include "termhooks.h"
Jim Blandy's avatar
Jim Blandy committed
31 32 33
#include "cm.h"
#include "dispextern.h"
#include "buffer.h"
Jim Blandy's avatar
Jim Blandy committed
34
#include "frame.h"
Jim Blandy's avatar
Jim Blandy committed
35 36 37 38 39
#include "window.h"
#include "commands.h"
#include "disptab.h"
#include "indent.h"

40
#include "systty.h"
Jim Blandy's avatar
Jim Blandy committed
41 42
#include "systime.h"

Jim Blandy's avatar
Jim Blandy committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56
#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
57
/* Nonzero upon entry to redisplay means do not assume anything about
Jim Blandy's avatar
Jim Blandy committed
58
   current contents of actual terminal frame; clear and redraw it.  */
Jim Blandy's avatar
Jim Blandy committed
59

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

/* 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
71
/* Invert the color of the whole frame, at a low level.  */
Jim Blandy's avatar
Jim Blandy committed
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 99

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
100 101 102
   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
103 104
int cursor_in_echo_area;

Jim Blandy's avatar
Jim Blandy committed
105 106
/* The currently selected frame.
   In a single-frame version, this variable always remains 0.  */
Jim Blandy's avatar
Jim Blandy committed
107

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

Jim Blandy's avatar
Jim Blandy committed
110 111 112 113
/* 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
114

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

   NOTE: the_only_frame is not checked for garbage collection; don't
   store collectable objects in any of its fields!

   You're not/The only frame in town/...  */
Jim Blandy's avatar
Jim Blandy committed
122

Jim Blandy's avatar
Jim Blandy committed
123 124
#ifndef MULTI_FRAME
struct frame the_only_frame;
Jim Blandy's avatar
Jim Blandy committed
125
#endif
Jim Blandy's avatar
Jim Blandy committed
126 127

/* This is a vector, made larger whenever it isn't large enough,
Jim Blandy's avatar
Jim Blandy committed
128 129 130
   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
131 132 133 134 135 136 137 138 139 140 141
/* 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
142
#ifdef MULTI_FRAME
Jim Blandy's avatar
Jim Blandy committed
143

Jim Blandy's avatar
Jim Blandy committed
144 145 146 147
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
148
{
Jim Blandy's avatar
Jim Blandy committed
149
  FRAME_PTR f;
Jim Blandy's avatar
Jim Blandy committed
150

Jim Blandy's avatar
Jim Blandy committed
151 152 153
  CHECK_LIVE_FRAME (frame, 0);
  f = XFRAME (frame);
  update_begin (f);
Jim Blandy's avatar
Jim Blandy committed
154
  /*  set_terminal_modes (); */
Jim Blandy's avatar
Jim Blandy committed
155
  clear_frame ();
156
  clear_frame_records (f);
Jim Blandy's avatar
Jim Blandy committed
157
  update_end (f);
Jim Blandy's avatar
Jim Blandy committed
158 159 160 161
  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
162 163
  mark_window_display_accurate (FRAME_ROOT_WINDOW (f), 0);
  f->garbaged = 0;
Jim Blandy's avatar
Jim Blandy committed
164 165 166
  return Qnil;
}

Jim Blandy's avatar
Jim Blandy committed
167 168
redraw_frame (f)
     FRAME_PTR f;
Jim Blandy's avatar
Jim Blandy committed
169
{
Jim Blandy's avatar
Jim Blandy committed
170 171 172
  Lisp_Object frame;
  XSET (frame, Lisp_Frame, f);
  Fredraw_frame (frame);
Jim Blandy's avatar
Jim Blandy committed
173 174
}

175
#else
Jim Blandy's avatar
Jim Blandy committed
176

177 178 179 180
DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 1, 1, "",
  "Clear frame FRAME and output again what is supposed to appear on it.")
  (frame)
     Lisp_Object frame;
Jim Blandy's avatar
Jim Blandy committed
181 182 183
{
  update_begin (0);
  set_terminal_modes ();
Jim Blandy's avatar
Jim Blandy committed
184
  clear_frame ();
Jim Blandy's avatar
Jim Blandy committed
185 186
  update_end (0);
  fflush (stdout);
Jim Blandy's avatar
Jim Blandy committed
187
  clear_frame_records (0);
Jim Blandy's avatar
Jim Blandy committed
188 189 190
  windows_or_buffers_changed++;
  /* Mark all windows as INaccurate,
     so that every window will have its redisplay done.  */
191
  mark_window_display_accurate (FRAME_ROOT_WINDOW (0), 0);
Jim Blandy's avatar
Jim Blandy committed
192 193 194
  return Qnil;
}

195 196 197 198 199 200 201 202 203
#endif

DEFUN ("redraw-display", Fredraw_display, Sredraw_display, 0, 0, "",
  "Clear and redisplay all visible frames.")
  ()
{
  Lisp_Object tail, frame;

  FOR_EACH_FRAME (tail, frame)
204
    if (FRAME_VISIBLE_P (XFRAME (frame)))
205 206 207 208 209
      Fredraw_frame (frame);

  return Qnil;
}

210 211 212 213 214 215 216 217 218 219 220 221 222 223
/* This is used when frame_garbaged is set.
   Redraw the individual frames marked as garbaged.  */

void
redraw_garbaged_frames ()
{
  Lisp_Object tail, frame;

  FOR_EACH_FRAME (tail, frame)
    if (FRAME_VISIBLE_P (XFRAME (frame))
	&& FRAME_GARBAGED_P (XFRAME (frame)))
      Fredraw_frame (frame);
}

Jim Blandy's avatar
Jim Blandy committed
224

Jim Blandy's avatar
Jim Blandy committed
225 226 227
static struct frame_glyphs *
make_frame_glyphs (frame, empty)
     register FRAME_PTR frame;
Jim Blandy's avatar
Jim Blandy committed
228 229 230
     int empty;
{
  register int i;
Jim Blandy's avatar
Jim Blandy committed
231 232 233 234
  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
235

Jim Blandy's avatar
Jim Blandy committed
236
  SET_GLYPHS_FRAME (new, frame);
Jim Blandy's avatar
Jim Blandy committed
237 238 239 240 241 242 243 244 245 246
  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
247
  if (FRAME_X_P (frame))
Jim Blandy's avatar
Jim Blandy committed
248 249 250 251 252
    {
      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));
253
      new->max_ascent = (short *) xmalloc (height * sizeof (short));
Jim Blandy's avatar
Jim Blandy committed
254 255 256 257 258 259
    }
#endif

  if (empty)
    {
      /* Make the buffer used by decode_mode_spec.  This buffer is also
Jim Blandy's avatar
Jim Blandy committed
260
         used as temporary storage when updating the frame.  See scroll.c. */
Jim Blandy's avatar
Jim Blandy committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
      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
280 281 282
free_frame_glyphs (frame, glyphs)
     FRAME_PTR frame;
     struct frame_glyphs *glyphs;
Jim Blandy's avatar
Jim Blandy committed
283 284
{
  if (glyphs->total_contents)
285
    xfree (glyphs->total_contents);
Jim Blandy's avatar
Jim Blandy committed
286

287 288 289 290 291
  xfree (glyphs->used);
  xfree (glyphs->glyphs);
  xfree (glyphs->highlight);
  xfree (glyphs->enable);
  xfree (glyphs->bufp);
Jim Blandy's avatar
Jim Blandy committed
292 293

#ifdef HAVE_X_WINDOWS
294
  if (FRAME_X_P (frame))
Jim Blandy's avatar
Jim Blandy committed
295
    {
296 297 298 299 300
      xfree (glyphs->top_left_x);
      xfree (glyphs->top_left_y);
      xfree (glyphs->pix_width);
      xfree (glyphs->pix_height);
      xfree (glyphs->max_ascent);
Jim Blandy's avatar
Jim Blandy committed
301 302 303
    }
#endif

304
  xfree (glyphs);
Jim Blandy's avatar
Jim Blandy committed
305 306 307
}

static void
Jim Blandy's avatar
Jim Blandy committed
308 309
remake_frame_glyphs (frame)
     FRAME_PTR frame;
Jim Blandy's avatar
Jim Blandy committed
310
{
Jim Blandy's avatar
Jim Blandy committed
311 312 313 314 315 316 317 318
  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))
319 320 321 322 323 324 325 326 327 328 329 330 331 332
    {
      /* Reallocate the frame's message buffer; remember that
	 echo_area_glyphs may be pointing here.  */
      char *old_message_buf = FRAME_MESSAGE_BUF (frame);

      FRAME_MESSAGE_BUF (frame)
	= (char *) xrealloc (FRAME_MESSAGE_BUF (frame),
			     FRAME_WIDTH (frame) + 1);

      if (echo_area_glyphs == old_message_buf)
	echo_area_glyphs = FRAME_MESSAGE_BUF (frame);
      if (previous_echo_glyphs == old_message_buf)
	previous_echo_glyphs = FRAME_MESSAGE_BUF (frame);
    }
Jim Blandy's avatar
Jim Blandy committed
333
  else
Jim Blandy's avatar
Jim Blandy committed
334 335
    FRAME_MESSAGE_BUF (frame)
      = (char *) xmalloc (FRAME_WIDTH (frame) + 1);
Jim Blandy's avatar
Jim Blandy committed
336

Jim Blandy's avatar
Jim Blandy committed
337 338 339 340
  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
341 342
}

Jim Blandy's avatar
Jim Blandy committed
343
/* Return the hash code of contents of line VPOS in frame-matrix M.  */
Jim Blandy's avatar
Jim Blandy committed
344 345 346

static int
line_hash_code (m, vpos)
Jim Blandy's avatar
Jim Blandy committed
347
     register struct frame_glyphs *m;
Jim Blandy's avatar
Jim Blandy committed
348 349 350 351 352 353 354 355
     int vpos;
{
  register GLYPH *body, *end;
  register int h = 0;

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

356
  /* Give all highlighted lines the same hash code
Jim Blandy's avatar
Jim Blandy committed
357 358 359 360 361 362 363 364 365 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
     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
393
     struct frame_glyphs *m;
Jim Blandy's avatar
Jim Blandy committed
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
     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
436 437
   FRAME_CURSOR_X (frame) and FRAME_CURSOR_Y (frame)
   and SET_FRAME_GARBAGED (frame).  */
Jim Blandy's avatar
Jim Blandy committed
438 439 440

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

Jim Blandy's avatar
Jim Blandy committed
441
cancel_line (vpos, frame)
Jim Blandy's avatar
Jim Blandy committed
442
     int vpos;
Jim Blandy's avatar
Jim Blandy committed
443
     register FRAME_PTR frame;
Jim Blandy's avatar
Jim Blandy committed
444
{
Jim Blandy's avatar
Jim Blandy committed
445
  FRAME_DESIRED_GLYPHS (frame)->enable[vpos] = 0;
Jim Blandy's avatar
Jim Blandy committed
446 447
}

Jim Blandy's avatar
Jim Blandy committed
448 449
clear_frame_records (frame)
     register FRAME_PTR frame;
Jim Blandy's avatar
Jim Blandy committed
450
{
Jim Blandy's avatar
Jim Blandy committed
451
  bzero (FRAME_CURRENT_GLYPHS (frame)->enable, FRAME_HEIGHT (frame));
Jim Blandy's avatar
Jim Blandy committed
452 453 454 455 456
}

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

void
Jim Blandy's avatar
Jim Blandy committed
457 458
get_display_line (frame, vpos, hpos)
     register FRAME_PTR frame;
Jim Blandy's avatar
Jim Blandy committed
459 460 461
     int vpos;
     register int hpos;
{
Jim Blandy's avatar
Jim Blandy committed
462 463
  register struct frame_glyphs *glyphs;
  register struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (frame);
Jim Blandy's avatar
Jim Blandy committed
464 465
  register GLYPH *p;

466
  if (vpos < 0)
Jim Blandy's avatar
Jim Blandy committed
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 496
    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;
{
497
  if (size <= 0 || from == to)
Jim Blandy's avatar
Jim Blandy committed
498 499
    return;

500 501 502 503 504 505 506 507
  /* 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
508
    {
509 510
      register char *endf = from + size;
      register char *endt = to + size;
Jim Blandy's avatar
Jim Blandy committed
511 512 513 514 515

      /* 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
516 517
	 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
518 519 520 521 522 523 524 525
      if (to - from < 64)
	{
	  do
	    *--endt = *--endf;
	  while (endf != from);
	}
      else
	{
526
	  for (;;)
Jim Blandy's avatar
Jim Blandy committed
527 528 529 530
	    {
	      endt -= (to - from);
	      endf -= (to - from);

531 532 533
	      if (endt < to)
		break;

Jim Blandy's avatar
Jim Blandy committed
534 535
	      bcopy (endf, endt, to - from);
	    }
536 537

	  /* If SIZE wasn't a multiple of TO - FROM, there will be a
Jim Blandy's avatar
Jim Blandy committed
538 539 540 541 542 543 544
	     little left over.  The amount left over is
	     (endt + (to - from)) - to, which is endt - from.  */
	  bcopy (from, to, endt - from);
	}
    }
}     

Jim Blandy's avatar
Jim Blandy committed
545
/* Rotate a vector of SIZE bytes right, by DISTANCE bytes.
Jim Blandy's avatar
Jim Blandy committed
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
   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
569 570
scroll_frame_lines (frame, from, end, amount)
     register FRAME_PTR frame;
Jim Blandy's avatar
Jim Blandy committed
571 572 573
     int from, end, amount;
{
  register int i;
Jim Blandy's avatar
Jim Blandy committed
574 575
  register struct frame_glyphs *current_frame
    = FRAME_CURRENT_GLYPHS (frame);
Jim Blandy's avatar
Jim Blandy committed
576 577 578 579 580 581 582 583 584

  if (!line_ins_del_ok)
    return 0;

  if (amount == 0)
    return 1;

  if (amount > 0)
    {
Jim Blandy's avatar
Jim Blandy committed
585
      update_begin (frame);
Jim Blandy's avatar
Jim Blandy committed
586 587 588 589 590 591
      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
592
      rotate_vector (current_frame->glyphs + from,
Jim Blandy's avatar
Jim Blandy committed
593 594 595
		     sizeof (GLYPH *) * (end + amount - from),
		     amount * sizeof (GLYPH *));

Jim Blandy's avatar
Jim Blandy committed
596 597 598
      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
599

Jim Blandy's avatar
Jim Blandy committed
600 601 602
      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
603

Jim Blandy's avatar
Jim Blandy committed
604 605 606
      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
607 608 609

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

Jim Blandy's avatar
Jim Blandy committed
620 621 622
      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
623 624

#ifdef HAVE_X_WINDOWS
625
      if (FRAME_X_P (frame))
Jim Blandy's avatar
Jim Blandy committed
626
	{
Jim Blandy's avatar
Jim Blandy committed
627 628 629
	  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
630

Jim Blandy's avatar
Jim Blandy committed
631 632 633
	  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
634

Jim Blandy's avatar
Jim Blandy committed
635 636 637
	  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
638

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

	  safe_bcopy (current_frame->max_ascent + from,
		      current_frame->max_ascent + from + amount,
		      (end - from) * sizeof current_frame->max_ascent[0]);
Jim Blandy's avatar
Jim Blandy committed
646 647 648
	}
#endif				/* HAVE_X_WINDOWS */

Jim Blandy's avatar
Jim Blandy committed
649
      update_end (frame);
Jim Blandy's avatar
Jim Blandy committed
650 651 652
    }
  if (amount < 0)
    {
Jim Blandy's avatar
Jim Blandy committed
653
      update_begin (frame);
Jim Blandy's avatar
Jim Blandy committed
654 655 656 657 658 659
      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
660
      rotate_vector (current_frame->glyphs + from + amount,
Jim Blandy's avatar
Jim Blandy committed
661 662 663
		     sizeof (GLYPH *) * (end - from - amount),
		     amount * sizeof (GLYPH *));

Jim Blandy's avatar
Jim Blandy committed
664 665 666
      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
667

Jim Blandy's avatar
Jim Blandy committed
668 669 670
      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
671

Jim Blandy's avatar
Jim Blandy committed
672 673 674
      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
675 676 677

      /* Mark the lines made empty by scrolling as enabled, empty and
	 normal video.  */
Jim Blandy's avatar
Jim Blandy committed
678 679 680 681
      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
682 683
      for (i = end + amount; i < end; i++)
	{
Jim Blandy's avatar
Jim Blandy committed
684 685
	  current_frame->glyphs[i][0] = '\0';
	  current_frame->enable[i] = 1;
Jim Blandy's avatar
Jim Blandy committed
686 687
	}

Jim Blandy's avatar
Jim Blandy committed
688 689 690
      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
691 692

#ifdef HAVE_X_WINDOWS
693
      if (FRAME_X_P (frame))
Jim Blandy's avatar
Jim Blandy committed
694
	{
Jim Blandy's avatar
Jim Blandy committed
695 696 697
	  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
698

Jim Blandy's avatar
Jim Blandy committed
699 700 701
	  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
702

Jim Blandy's avatar
Jim Blandy committed
703 704 705
	  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
706

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

	  safe_bcopy (current_frame->max_ascent + from,
		      current_frame->max_ascent + from + amount,
		      (end - from) * sizeof current_frame->max_ascent[0]);
Jim Blandy's avatar
Jim Blandy committed
714 715 716
	}
#endif				/* HAVE_X_WINDOWS */

Jim Blandy's avatar
Jim Blandy committed
717
      update_end (frame);
Jim Blandy's avatar
Jim Blandy committed
718 719 720 721
    }
  return 1;
}

Jim Blandy's avatar
Jim Blandy committed
722
/* After updating a window W that isn't the full frame wide,
Jim Blandy's avatar
Jim Blandy committed
723
   copy all the columns that W does not occupy
Jim Blandy's avatar
Jim Blandy committed
724 725
   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
726 727 728 729 730

preserve_other_columns (w)
     struct window *w;
{
  register int vpos;
Jim Blandy's avatar
Jim Blandy committed
731 732
  register struct frame_glyphs *current_frame, *desired_frame;
  register FRAME_PTR frame = XFRAME (w->frame);
Jim Blandy's avatar
Jim Blandy committed
733 734 735 736
  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
737 738
  current_frame = FRAME_CURRENT_GLYPHS (frame);
  desired_frame = FRAME_DESIRED_GLYPHS (frame);
Jim Blandy's avatar
Jim Blandy committed
739 740 741

  for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
    {
Jim Blandy's avatar
Jim Blandy committed
742
      if (current_frame->enable[vpos] && desired_frame->enable[vpos])
Jim Blandy's avatar
Jim Blandy committed
743 744 745 746 747
	{
	  if (start > 0)
	    {
	      int len;

Jim Blandy's avatar
Jim Blandy committed
748
	      bcopy (current_frame->glyphs[vpos],
749 750
		     desired_frame->glyphs[vpos],
		     start * sizeof (current_frame->glyphs[vpos]));
Jim Blandy's avatar
Jim Blandy committed
751 752 753
	      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
754
	    }
Jim Blandy's avatar
Jim Blandy committed
755 756
	  if (current_frame->used[vpos] > end
	      && desired_frame->used[vpos] < current_frame->used[vpos])
Jim Blandy's avatar
Jim Blandy committed
757
	    {
Jim Blandy's avatar
Jim Blandy committed
758 759
	      while (desired_frame->used[vpos] < end)
		desired_frame->glyphs[vpos][desired_frame->used[vpos]++]
Jim Blandy's avatar
Jim Blandy committed
760
		  = SPACEGLYPH;
Jim Blandy's avatar
Jim Blandy committed
761 762
	      bcopy (current_frame->glyphs[vpos] + end,
		     desired_frame->glyphs[vpos] + end,
763 764
		     ((current_frame->used[vpos] - end)
		      * sizeof (current_frame->glyphs[vpos])));
Jim Blandy's avatar
Jim Blandy committed
765
	      desired_frame->used[vpos] = current_frame->used[vpos];
Jim Blandy's avatar
Jim Blandy committed
766 767 768 769 770 771 772
	    }
	}
    }
}

#if 0

Jim Blandy's avatar
Jim Blandy committed
773
/* If window w does not need to be updated and isn't the full frame wide,
Jim Blandy's avatar
Jim Blandy committed
774
 copy all the columns that w does occupy
Jim Blandy's avatar
Jim Blandy committed
775 776
 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
777 778 779 780 781 782 783

 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
784 785
  register struct frame_glyphs *l1, *l2;
  register FRAME_PTR frame = XFRAME (w->frame);
Jim Blandy's avatar
Jim Blandy committed
786 787 788 789 790 791
  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
792 793
      if ((l1 = FRAME_DESIRED_GLYPHS (frame)->glyphs[vpos + 1])
	  && (l2 = FRAME_PHYS_GLYPHS (frame)->glyphs[vpos + 1]))
Jim Blandy's avatar
Jim Blandy committed
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
	{
	  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
818 819
  register struct frame_glyphs *desired_glyphs =
    FRAME_DESIRED_GLYPHS (XFRAME (w->frame));
Jim Blandy's avatar
Jim Blandy committed
820 821 822 823 824 825 826 827 828
  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
829
/* These functions try to perform directly and immediately on the frame
Jim Blandy's avatar
Jim Blandy committed
830 831 832
   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
833
   They assume that the frame was up to date before the buffer
834
   change being displayed.  They make various other assumptions too;
Jim Blandy's avatar
Jim Blandy committed
835 836 837 838 839 840
   see command_loop_1 where these are called.  */

int
direct_output_for_insert (g)
     int g;
{
Jim Blandy's avatar
Jim Blandy committed
841 842 843
  register FRAME_PTR frame = selected_frame;
  register struct frame_glyphs *current_frame
    = FRAME_CURRENT_GLYPHS (frame);
Jim Blandy's avatar
Jim Blandy committed
844 845 846 847 848 849 850 851

#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
852
    int hpos = FRAME_CURSOR_X (frame);
Jim Blandy's avatar
Jim Blandy committed
853 854 855
#ifndef COMPILER_REGISTER_BUG
  register
#endif /* COMPILER_REGISTER_BUG */
Jim Blandy's avatar
Jim Blandy committed
856
    int vpos = FRAME_CURSOR_Y (frame);
Jim Blandy's avatar
Jim Blandy committed
857

858 859 860
  /* Give up if about to continue line.  */
  if (hpos >= XFASTINT (w->left) + window_internal_width (w) - 1
    
Jim Blandy's avatar
Jim Blandy committed
861 862 863 864
  /* 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) */
865
      || cursor_in_echo_area
Jim Blandy's avatar
Jim Blandy committed
866 867
      || FRAME_CURSOR_Y (frame) < XFASTINT (w->top)
      || FRAME_CURSOR_Y (frame) >= XFASTINT (w->top) + XFASTINT (w->height)
Jim Blandy's avatar
Jim Blandy committed
868

Jim Blandy's avatar
Jim Blandy committed
869
  /* Give up if cursor not really at FRAME_CURSOR_X, FRAME_CURSOR_Y */
Jim Blandy's avatar
Jim Blandy committed
870 871 872 873 874 875 876 877 878
      || !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;

879 880 881
  {
#ifdef HAVE_X_WINDOWS
    int dummy;
882
    int face = compute_char_face (frame, w, point, -1, -1, &dummy);
883 884 885 886 887 888
#else
    int face = 0;
#endif
				  
    current_frame->glyphs[vpos][hpos] = MAKE_GLYPH (g, face);
  }
Jim Blandy's avatar
Jim Blandy committed
889 890 891 892 893 894 895
  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
896
  write_glyphs (&current_frame->glyphs[vpos][hpos], 1);
Jim Blandy's avatar
Jim Blandy committed
897
  fflush (stdout);
Jim Blandy's avatar
Jim Blandy committed
898 899
  ++FRAME_CURSOR_X (frame);
  if (hpos == current_frame->used[vpos])
Jim Blandy's avatar
Jim Blandy committed
900
    {
Jim Blandy's avatar
Jim Blandy committed
901 902
      current_frame->used[vpos] = hpos + 1;
      current_frame->glyphs[vpos][hpos + 1] = 0;
Jim Blandy's avatar
Jim Blandy committed
903 904 905 906 907 908 909 910 911
    }

  return 1;
}

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

Jim Blandy's avatar
Jim Blandy committed
915 916
  /* 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
917
  if ((FRAME_CURSOR_X (frame) == XFASTINT (w->left)
Jim Blandy's avatar
Jim Blandy committed
918 919
       && (XINT (w->hscroll) || n < 0))
      || (n > 0
920
	  && (FRAME_CURSOR_X (frame) + 1 >= window_internal_width (w) - 1))
921
      || cursor_in_echo_area)
Jim Blandy's avatar
Jim Blandy committed
922 923
    return 0;

924 925 926 927
  /* Can't use direct output if highlighting a region.  */
  if (!NILP (Vtransient_mark_mode) && !NILP (current_buffer->mark_active))
    return 0;

Jim Blandy's avatar
Jim Blandy committed
928 929
  FRAME_CURSOR_X (frame) += n;
  XFASTINT (w->last_point_x) = FRAME_CURSOR_X (frame);
Jim Blandy's avatar
Jim Blandy committed
930
  XFASTINT (w->last_point) = point;
Jim Blandy's avatar
Jim Blandy committed
931
  cursor_to (FRAME_CURSOR_Y (frame), FRAME_CURSOR_X (frame));
Jim Blandy's avatar
Jim Blandy committed
932 933 934 935 936 937
  fflush (stdout);
  return 1;
}

static void update_line ();

Jim Blandy's avatar
Jim Blandy committed
938
/* Update frame F based on the data in FRAME_DESIRED_GLYPHS.
Jim Blandy's avatar
Jim Blandy committed
939 940 941 942
   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
943 944
update_frame (f, force, inhibit_hairy_id)
     FRAME_PTR f;
Jim Blandy's avatar
Jim Blandy committed
945 946 947
     int force;
     int inhibit_hairy_id;
{
Jim Blandy's avatar
Jim Blandy committed
948 949
  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
950 951 952 953 954 955 956 957
  register int i;
  int pause;
  int preempt_count = baud_rate / 2400 + 1;
  extern input_pending;
#ifdef HAVE_X_WINDOWS
  register int downto, leftmost;
#endif

958 959 960
  if (preempt_count <= 0)
    preempt_count = 1;

Jim Blandy's avatar
Jim Blandy committed
961
  if (FRAME_HEIGHT (f) == 0) abort (); /* Some bug zeros some core */
Jim Blandy's avatar
Jim Blandy committed
962 963 964 965 966 967 968 969

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

Jim Blandy's avatar
Jim Blandy committed
970
  update_begin (f);
Jim Blandy's avatar
Jim Blandy committed
971 972 973 974

  if (!line_ins_del_ok)
    inhibit_hairy_id = 1;

Jim Blandy's avatar
Jim Blandy committed
975 976
  /* 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
977 978
  for (i = 0; i < FRAME_HEIGHT (f); i++)
    if (desired_frame->enable[i])
Jim Blandy's avatar
Jim Blandy committed
979 980 981
      break;

  /* Try doing i/d line, if not yet inhibited.  */
Jim Blandy's avatar
Jim Blandy committed
982 983
  if (!inhibit_hairy_id && i < FRAME_HEIGHT (f))
    force |= scrolling (f);
Jim Blandy's avatar
Jim Blandy committed
984 985 986

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

Jim Blandy's avatar
Jim Blandy committed
987 988
  if (desired_frame->enable[FRAME_HEIGHT (f) - 1])
    update_line (f, FRAME_HEIGHT (f) - 1);
Jim Blandy's avatar
Jim Blandy committed
989 990

#ifdef HAVE_X_WINDOWS
991
  if (FRAME_X_P (f))
Jim Blandy's avatar
Jim Blandy committed
992
    {
Jim Blandy's avatar
Jim Blandy committed
993 994
      leftmost = downto = f->display.x->internal_border_width;
      if (desired_frame->enable[0])
Jim Blandy's avatar
Jim Blandy committed
995
	{
Jim Blandy's avatar
Jim Blandy committed
996 997 998
	  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
999
	      - current_frame->pix_height[FRAME_HEIGHT (f) - 1];
Jim Blandy's avatar
Jim Blandy committed
1000 1001
	  current_frame->top_left_x[0] = leftmost;
	  current_frame->top_left_y[0] = downto;
Jim Blandy's avatar
Jim Blandy committed
1002 1003 1004 1005 1006
	}
    }
#endif /* HAVE_X_WINDOWS */

  /* Now update the rest of the lines. */
Jim Blandy's avatar
Jim Blandy committed
1007
  for (i = 0; i < FRAME_HEIGHT (f) - 1 && (force || !input_pending); i++)
Jim Blandy's avatar
Jim Blandy committed
1008
    {
Jim Blandy's avatar
Jim Blandy committed
1009
      if (desired_frame->enable[i])
Jim Blandy's avatar
Jim Blandy committed
1010
	{
1011
	  if (FRAME_TERMCAP_P (f))
Jim Blandy's avatar
Jim Blandy committed
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
	    {
	      /* 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
1024 1025
#ifdef EMACS_OUTQSIZE
		      if (EMACS_OUTQSIZE (0, &outq) < 0)
Jim Blandy's avatar
Jim Blandy committed
1026 1027 1028 1029 1030
			/* Probably not a tty.  Ignore the error and reset
			 * the outq count. */
			outq = PENDING_OUTPUT_COUNT (stdout);
#endif
		      outq *= 10;
1031 1032
		      if (baud_rate > 0)
			sleep (outq / baud_rate);
Jim Blandy's avatar
Jim Blandy committed
1033 1034 1035 1036 1037 1038
		    }
		}
	      if ((i - 1) % preempt_count == 0)
		detect_input_pending ();
	    }

Jim Blandy's avatar
Jim Blandy committed
1039
	  update_line (f, i);
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
	    {
Jim Blandy's avatar
Jim Blandy committed
1043 1044
	      current_frame->top_left_y[i] = downto;
	      current_frame->top_left_x[i] = leftmost;
Jim Blandy's avatar
Jim Blandy committed
1045 1046 1047 1048
	    }
#endif /* HAVE_X_WINDOWS */
	}

Jim Blandy's avatar
Jim Blandy committed
1049
#ifdef HAVE_X_WINDOWS
1050
      if (FRAME_X_P (f))
1051
	downto += current_frame->pix_height[i];
Jim Blandy's avatar
Jim Blandy committed
1052
#endif
Jim Blandy's avatar
Jim Blandy committed
1053
    }
Jim Blandy's avatar
Jim Blandy committed
1054
  pause = (i < FRAME_HEIGHT (f) - 1) ? i : 0;
Jim Blandy's avatar
Jim Blandy committed
1055 1056 1057 1058

  /* Now just clean up termcap drivers and set cursor, etc.  */
  if (!pause)
    {
1059 1060
      if (cursor_in_echo_area
	  && FRAME_HAS_MINIBUF_P (f))
Jim Blandy's avatar
Jim Blandy committed
1061
	{
1062 1063 1064 1065 1066 1067 1068 1069
	  int top = XINT (XWINDOW (FRAME_MINIBUF_WINDOW (f))->top);
	  int row, col;

	  if (cursor_in_echo_area < 0)
	    {
	      row = top;
	      col = 0;
	    }
Jim Blandy's avatar
Jim Blandy committed
1070
	  else
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
	    {
	      /* If the minibuffer is several lines high, find the last
		 line that has any text on it.  */
	      row = FRAME_HEIGHT (f);
	      do 
		{
		  row--;
		  if (current_frame->enable[row])
		    col = current_frame->used[row];
		  else
		    col = 0;
		}
	      while (row > top && col == 0);

	      if (col >= FRAME_WIDTH (f))
		{
		  col = 0;
		  if (row < FRAME_HEIGHT (f) - 1)
		    row++;
		}
	    }

	  cursor_to (row, col);
Jim Blandy's avatar
Jim Blandy committed
1094
	}
Jim Blandy's avatar
Jim Blandy committed
1095
      else
Jim Blandy's avatar
Jim Blandy committed
1096 1097
	cursor_to (FRAME_CURSOR_Y (f), max (min (FRAME_CURSOR_X (f),
						  FRAME_WIDTH (f) - 1), 0));
Jim Blandy's avatar
Jim Blandy committed
1098 1099
    }

Jim Blandy's avatar
Jim Blandy committed
1100
  update_end (f);
Jim Blandy's avatar
Jim Blandy committed
1101 1102 1103 1104 1105 1106 1107 1108

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

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