dispnew.c 76.3 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Updating of data structures for redisplay.
Karl Heuer's avatar
Karl Heuer committed
2
   Copyright (C) 1985, 86, 87, 88, 93, 94, 95 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
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
18 19
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */
Jim Blandy's avatar
Jim Blandy committed
20 21 22 23


#include <signal.h>

24
#include <config.h>
25 26

#include <stdio.h>
Jim Blandy's avatar
Jim Blandy committed
27 28
#include <ctype.h>

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

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

Jim Blandy's avatar
Jim Blandy committed
51 52 53 54
#ifdef HAVE_X_WINDOWS
#include "xterm.h"
#endif	/* HAVE_X_WINDOWS */

55 56 57 58
#ifdef HAVE_NTGUI
#include "w32term.h"
#endif /* HAVE_NTGUI */

59 60 61
/* Include systime.h after xterm.h to avoid double inclusion of time.h. */
#include "systime.h"

62 63
#include <errno.h>

Jim Blandy's avatar
Jim Blandy committed
64 65 66 67 68 69
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

/* Get number of chars of output now in the buffer of a stdio stream.
   This ought to be built in in stdio, but it isn't.
   Some s- files override this because their stdio internals differ.  */
70
#ifdef __GNU_LIBRARY__
Roland McGrath's avatar
Roland McGrath committed
71 72 73
/* The s- file might have overridden the definition with one that works for
   the system's C library.  But we are using the GNU C library, so this is
   the right definition for every system.  */
74 75 76
#ifdef GNU_LIBRARY_PENDING_OUTPUT_COUNT
#define PENDING_OUTPUT_COUNT GNU_LIBRARY_PENDING_OUTPUT_COUNT
#else
77
#undef	PENDING_OUTPUT_COUNT
78
#define	PENDING_OUTPUT_COUNT(FILE) ((FILE)->__bufp - (FILE)->__buffer)
79 80
#endif
#else /* not __GNU_LIBRARY__ */
81
#ifndef PENDING_OUTPUT_COUNT
Jim Blandy's avatar
Jim Blandy committed
82 83
#define PENDING_OUTPUT_COUNT(FILE) ((FILE)->_ptr - (FILE)->_base)
#endif
84
#endif
Jim Blandy's avatar
Jim Blandy committed
85

86 87
static void change_frame_size_1 ();

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

Jim Blandy's avatar
Jim Blandy committed
91
int frame_garbaged;
Jim Blandy's avatar
Jim Blandy committed
92 93 94 95 96 97 98 99 100 101

/* 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
102
/* Invert the color of the whole frame, at a low level.  */
Jim Blandy's avatar
Jim Blandy committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

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
131 132 133
   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
134
int cursor_in_echo_area;
135 136

Lisp_Object Qdisplay_table;
Jim Blandy's avatar
Jim Blandy committed
137

Jim Blandy's avatar
Jim Blandy committed
138
/* The currently selected frame.
Morten Welinder's avatar
Morten Welinder committed
139 140
   In a single-frame version, this variable always holds the address of
   the_only_frame.  */
Jim Blandy's avatar
Jim Blandy committed
141

Jim Blandy's avatar
Jim Blandy committed
142
FRAME_PTR selected_frame;
Jim Blandy's avatar
Jim Blandy committed
143

Jim Blandy's avatar
Jim Blandy committed
144 145
/* 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
Morten Welinder's avatar
Morten Welinder committed
146 147
   selected.  In a single-frame version, this variable always holds
   the address of the_only_frame.  */
Jim Blandy's avatar
Jim Blandy committed
148
FRAME_PTR last_nonminibuf_frame;
Jim Blandy's avatar
Jim Blandy committed
149

Jim Blandy's avatar
Jim Blandy committed
150
/* This is a vector, made larger whenever it isn't large enough,
Jim Blandy's avatar
Jim Blandy committed
151 152 153
   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
154 155 156 157 158 159 160 161 162
/* 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 */

int delayed_size_change;  /* 1 means SIGWINCH happened when not safe.  */

Jim Blandy's avatar
Jim Blandy committed
163 164 165 166
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
167
{
Jim Blandy's avatar
Jim Blandy committed
168
  FRAME_PTR f;
Jim Blandy's avatar
Jim Blandy committed
169

Jim Blandy's avatar
Jim Blandy committed
170 171 172
  CHECK_LIVE_FRAME (frame, 0);
  f = XFRAME (frame);
  update_begin (f);
173 174
  if (FRAME_MSDOS_P (f))
    set_terminal_modes ();
Jim Blandy's avatar
Jim Blandy committed
175
  clear_frame ();
176
  clear_frame_records (f);
Jim Blandy's avatar
Jim Blandy committed
177
  update_end (f);
Jim Blandy's avatar
Jim Blandy committed
178 179 180 181
  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
182 183
  mark_window_display_accurate (FRAME_ROOT_WINDOW (f), 0);
  f->garbaged = 0;
Jim Blandy's avatar
Jim Blandy committed
184 185 186
  return Qnil;
}

Jim Blandy's avatar
Jim Blandy committed
187 188
redraw_frame (f)
     FRAME_PTR f;
Jim Blandy's avatar
Jim Blandy committed
189
{
Jim Blandy's avatar
Jim Blandy committed
190
  Lisp_Object frame;
191
  XSETFRAME (frame, f);
Jim Blandy's avatar
Jim Blandy committed
192
  Fredraw_frame (frame);
Jim Blandy's avatar
Jim Blandy committed
193 194
}

195 196 197 198 199 200 201
DEFUN ("redraw-display", Fredraw_display, Sredraw_display, 0, 0, "",
  "Clear and redisplay all visible frames.")
  ()
{
  Lisp_Object tail, frame;

  FOR_EACH_FRAME (tail, frame)
202
    if (FRAME_VISIBLE_P (XFRAME (frame)))
203 204 205 206 207
      Fredraw_frame (frame);

  return Qnil;
}

208 209 210 211 212 213 214 215 216 217 218 219 220 221
/* 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
222

Jim Blandy's avatar
Jim Blandy committed
223 224 225
static struct frame_glyphs *
make_frame_glyphs (frame, empty)
     register FRAME_PTR frame;
Jim Blandy's avatar
Jim Blandy committed
226 227 228
     int empty;
{
  register int i;
229
  register width = FRAME_WINDOW_WIDTH (frame);
Jim Blandy's avatar
Jim Blandy committed
230
  register height = FRAME_HEIGHT (frame);
231 232
  register struct frame_glyphs *new
    = (struct frame_glyphs *) xmalloc (sizeof (struct frame_glyphs));
Jim Blandy's avatar
Jim Blandy committed
233

Jim Blandy's avatar
Jim Blandy committed
234
  SET_GLYPHS_FRAME (new, frame);
Jim Blandy's avatar
Jim Blandy committed
235 236 237 238
  new->height = height;
  new->width = width;
  new->used = (int *) xmalloc (height * sizeof (int));
  new->glyphs = (GLYPH **) xmalloc (height * sizeof (GLYPH *));
239
  new->charstarts = (int **) xmalloc (height * sizeof (int *));
Jim Blandy's avatar
Jim Blandy committed
240 241 242 243 244
  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));

245 246
#ifdef HAVE_WINDOW_SYSTEM
  if (FRAME_WINDOW_P (frame))
Jim Blandy's avatar
Jim Blandy committed
247 248 249 250 251
    {
      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));
252
      new->max_ascent = (short *) xmalloc (height * sizeof (short));
Jim Blandy's avatar
Jim Blandy committed
253
    }
254
#endif /* HAVE_WINDOW_SYSTEM */
Jim Blandy's avatar
Jim Blandy committed
255 256 257 258

  if (empty)
    {
      /* Make the buffer used by decode_mode_spec.  This buffer is also
Jim Blandy's avatar
Jim Blandy committed
259
         used as temporary storage when updating the frame.  See scroll.c. */
Jim Blandy's avatar
Jim Blandy committed
260
      unsigned int total_glyphs = (width + 2) * sizeof (GLYPH);
261
      unsigned int total_charstarts = (width + 2) * sizeof (int);
Jim Blandy's avatar
Jim Blandy committed
262 263 264

      new->total_contents = (GLYPH *) xmalloc (total_glyphs);
      bzero (new->total_contents, total_glyphs);
265 266

      new->total_charstarts = (int *) xmalloc (total_charstarts);
267
      bzero (new->total_charstarts, total_charstarts);
Jim Blandy's avatar
Jim Blandy committed
268 269 270 271 272 273 274 275 276
    }
  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;
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295

      if (!FRAME_TERMCAP_P (frame))
	{
	  unsigned int total_charstarts = height * (width + 2) * sizeof (int);

	  new->total_charstarts = (int *) xmalloc (total_charstarts);
	  bzero (new->total_charstarts, total_charstarts);
	  for (i = 0; i < height; i++)
	    new->charstarts[i] = new->total_charstarts + i * (width + 2) + 1;
	}
      else
	{
	  /* Without a window system, we don't really need charstarts.
	     So use a small amount of space to make enough data structure
	     to prevent crashes in display_text_line.  */
	  new->total_charstarts = (int *) xmalloc ((width + 2) * sizeof (int));
	  for (i = 0; i < height; i++)
	    new->charstarts[i] = new->total_charstarts;
	}
Jim Blandy's avatar
Jim Blandy committed
296 297 298 299 300
    }

  return new;
}

301
void
Jim Blandy's avatar
Jim Blandy committed
302 303 304
free_frame_glyphs (frame, glyphs)
     FRAME_PTR frame;
     struct frame_glyphs *glyphs;
Jim Blandy's avatar
Jim Blandy committed
305 306
{
  if (glyphs->total_contents)
307
    xfree (glyphs->total_contents);
308 309
  if (glyphs->total_charstarts)
    xfree (glyphs->total_charstarts);
Jim Blandy's avatar
Jim Blandy committed
310

311 312 313 314 315
  xfree (glyphs->used);
  xfree (glyphs->glyphs);
  xfree (glyphs->highlight);
  xfree (glyphs->enable);
  xfree (glyphs->bufp);
316 317
  if (glyphs->charstarts)
    xfree (glyphs->charstarts);
Jim Blandy's avatar
Jim Blandy committed
318

319 320
#ifdef HAVE_WINDOW_SYSTEM
  if (FRAME_WINDOW_P (frame))
Jim Blandy's avatar
Jim Blandy committed
321
    {
322 323 324 325 326
      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
327
    }
328
#endif /* HAVE_WINDOW_SYSTEM */
Jim Blandy's avatar
Jim Blandy committed
329

330
  xfree (glyphs);
Jim Blandy's avatar
Jim Blandy committed
331 332
}

333
void
Jim Blandy's avatar
Jim Blandy committed
334 335
remake_frame_glyphs (frame)
     FRAME_PTR frame;
Jim Blandy's avatar
Jim Blandy committed
336
{
Jim Blandy's avatar
Jim Blandy committed
337 338 339 340 341 342 343 344
  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))
345 346 347 348 349 350 351
    {
      /* 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),
352
			     FRAME_MESSAGE_BUF_SIZE (frame) + 1);
353 354 355 356 357 358

      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
359
  else
Jim Blandy's avatar
Jim Blandy committed
360
    FRAME_MESSAGE_BUF (frame)
361
      = (char *) xmalloc (FRAME_MESSAGE_BUF_SIZE (frame) + 1);
Jim Blandy's avatar
Jim Blandy committed
362

Jim Blandy's avatar
Jim Blandy committed
363 364 365
  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);
366
  if (FRAME_WINDOW_P (frame) || frame == selected_frame)
367
    SET_FRAME_GARBAGED (frame);
Jim Blandy's avatar
Jim Blandy committed
368 369
}

Jim Blandy's avatar
Jim Blandy committed
370
/* Return the hash code of contents of line VPOS in frame-matrix M.  */
Jim Blandy's avatar
Jim Blandy committed
371 372 373

static int
line_hash_code (m, vpos)
Jim Blandy's avatar
Jim Blandy committed
374
     register struct frame_glyphs *m;
Jim Blandy's avatar
Jim Blandy committed
375 376 377 378 379 380 381 382
     int vpos;
{
  register GLYPH *body, *end;
  register int h = 0;

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

383
  /* Give all highlighted lines the same hash code
Jim Blandy's avatar
Jim Blandy committed
384 385 386 387 388 389 390 391 392 393 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
     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
420
     struct frame_glyphs *m;
Jim Blandy's avatar
Jim Blandy committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
     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
463 464
   FRAME_CURSOR_X (frame) and FRAME_CURSOR_Y (frame)
   and SET_FRAME_GARBAGED (frame).  */
Jim Blandy's avatar
Jim Blandy committed
465 466 467

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

Jim Blandy's avatar
Jim Blandy committed
468
cancel_line (vpos, frame)
Jim Blandy's avatar
Jim Blandy committed
469
     int vpos;
Jim Blandy's avatar
Jim Blandy committed
470
     register FRAME_PTR frame;
Jim Blandy's avatar
Jim Blandy committed
471
{
Jim Blandy's avatar
Jim Blandy committed
472
  FRAME_DESIRED_GLYPHS (frame)->enable[vpos] = 0;
Jim Blandy's avatar
Jim Blandy committed
473 474
}

Jim Blandy's avatar
Jim Blandy committed
475 476
clear_frame_records (frame)
     register FRAME_PTR frame;
Jim Blandy's avatar
Jim Blandy committed
477
{
Jim Blandy's avatar
Jim Blandy committed
478
  bzero (FRAME_CURRENT_GLYPHS (frame)->enable, FRAME_HEIGHT (frame));
Jim Blandy's avatar
Jim Blandy committed
479 480
}

481 482 483 484 485 486 487 488 489 490 491 492 493 494
/* Clear out all display lines for a coming redisplay.  */

void
init_desired_glyphs (frame)
     register FRAME_PTR frame;
{
  register struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (frame);
  int vpos;
  int height = FRAME_HEIGHT (frame);

  for (vpos = 0; vpos < height; vpos++)
    desired_glyphs->enable[vpos] = 0;
}

Jim Blandy's avatar
Jim Blandy committed
495 496 497
/* Prepare to display on line VPOS starting at HPOS within it.  */

void
Jim Blandy's avatar
Jim Blandy committed
498 499
get_display_line (frame, vpos, hpos)
     register FRAME_PTR frame;
Jim Blandy's avatar
Jim Blandy committed
500 501 502
     int vpos;
     register int hpos;
{
Jim Blandy's avatar
Jim Blandy committed
503 504
  register struct frame_glyphs *glyphs;
  register struct frame_glyphs *desired_glyphs = FRAME_DESIRED_GLYPHS (frame);
Jim Blandy's avatar
Jim Blandy committed
505 506
  register GLYPH *p;

507
  if (vpos < 0)
Jim Blandy's avatar
Jim Blandy committed
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
    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;
{
535
  if (size <= 0 || from == to)
Jim Blandy's avatar
Jim Blandy committed
536 537
    return;

538 539 540 541 542 543 544 545
  /* 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
546
    {
547 548
      register char *endf = from + size;
      register char *endt = to + size;
Jim Blandy's avatar
Jim Blandy committed
549 550 551 552 553

      /* 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
554 555
	 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
556 557 558 559 560 561 562 563
      if (to - from < 64)
	{
	  do
	    *--endt = *--endf;
	  while (endf != from);
	}
      else
	{
564
	  for (;;)
Jim Blandy's avatar
Jim Blandy committed
565 566 567 568
	    {
	      endt -= (to - from);
	      endf -= (to - from);

569 570 571
	      if (endt < to)
		break;

Jim Blandy's avatar
Jim Blandy committed
572 573
	      bcopy (endf, endt, to - from);
	    }
574 575

	  /* If SIZE wasn't a multiple of TO - FROM, there will be a
Jim Blandy's avatar
Jim Blandy committed
576 577 578 579 580 581 582
	     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
583
/* Rotate a vector of SIZE bytes right, by DISTANCE bytes.
Jim Blandy's avatar
Jim Blandy committed
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
   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
607
scroll_frame_lines (frame, from, end, amount, newpos)
Jim Blandy's avatar
Jim Blandy committed
608
     register FRAME_PTR frame;
609
     int from, end, amount, newpos;
Jim Blandy's avatar
Jim Blandy committed
610 611
{
  register int i;
Jim Blandy's avatar
Jim Blandy committed
612 613
  register struct frame_glyphs *current_frame
    = FRAME_CURRENT_GLYPHS (frame);
614
  int pos_adjust;
615
  int width = FRAME_WINDOW_WIDTH (frame);
Jim Blandy's avatar
Jim Blandy committed
616 617 618 619 620 621 622 623 624

  if (!line_ins_del_ok)
    return 0;

  if (amount == 0)
    return 1;

  if (amount > 0)
    {
Jim Blandy's avatar
Jim Blandy committed
625
      update_begin (frame);
Jim Blandy's avatar
Jim Blandy committed
626 627 628 629 630 631
      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
632
      rotate_vector (current_frame->glyphs + from,
Jim Blandy's avatar
Jim Blandy committed
633 634 635
		     sizeof (GLYPH *) * (end + amount - from),
		     amount * sizeof (GLYPH *));

636
      rotate_vector (current_frame->charstarts + from,
637 638 639
		     sizeof (int *) * (end + amount - from),
		     amount * sizeof (int *));

640 641 642 643 644 645 646 647 648 649 650 651
      safe_bcopy (current_frame->used + from,
		  current_frame->used + from + amount,
		  (end - from) * sizeof current_frame->used[0]);

      safe_bcopy (current_frame->highlight + from,
		  current_frame->highlight + from + amount,
		  (end - from) * sizeof current_frame->highlight[0]);

      safe_bcopy (current_frame->enable + from,
		  current_frame->enable + from + amount,
		  (end - from) * sizeof current_frame->enable[0]);

652 653
      /* Adjust the lines by an amount
	 that puts the first of them at NEWPOS.  */
654
      pos_adjust = newpos - current_frame->charstarts[from + amount][0];
655 656 657

      /* Offset each char position in the charstarts lines we moved
	 by pos_adjust.  */
658
      for (i = from + amount; i < end + amount; i++)
659
	{
660
	  int *line = current_frame->charstarts[i];
661
	  int col;
662 663 664
	  for (col = 0; col < width; col++)
	    if (line[col] > 0)
	      line[col] += pos_adjust;
665
	}
666
      for (i = from; i < from + amount; i++)
667
	{
668
	  int *line = current_frame->charstarts[i];
669 670
	  int col;
	  line[0] = -1;
671
	  for (col = 0; col < width; col++)
672 673 674
	    line[col] = 0;
	}

Jim Blandy's avatar
Jim Blandy committed
675 676
      /* Mark the lines made empty by scrolling as enabled, empty and
	 normal video.  */
Jim Blandy's avatar
Jim Blandy committed
677 678 679 680
      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
681 682
      for (i = from; i < from + amount; i++)
	{
Jim Blandy's avatar
Jim Blandy committed
683
	  current_frame->glyphs[i][0] = '\0';
684
	  current_frame->charstarts[i][0] = -1;
Jim Blandy's avatar
Jim Blandy committed
685
	  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 693
#ifdef HAVE_WINDOW_SYSTEM
      if (FRAME_WINDOW_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
#endif /* HAVE_WINDOW_SYSTEM */
Jim Blandy's avatar
Jim Blandy committed
716

Jim Blandy's avatar
Jim Blandy committed
717
      update_end (frame);
Jim Blandy's avatar
Jim Blandy committed
718 719 720
    }
  if (amount < 0)
    {
Jim Blandy's avatar
Jim Blandy committed
721
      update_begin (frame);
Jim Blandy's avatar
Jim Blandy committed
722 723 724 725 726 727
      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
728
      rotate_vector (current_frame->glyphs + from + amount,
Jim Blandy's avatar
Jim Blandy committed
729 730 731
		     sizeof (GLYPH *) * (end - from - amount),
		     amount * sizeof (GLYPH *));

732
      rotate_vector (current_frame->charstarts + from + amount,
733 734 735
		     sizeof (int *) * (end - from - amount),
		     amount * sizeof (int *));

736 737 738 739 740 741 742 743 744 745 746 747
      safe_bcopy (current_frame->used + from,
		  current_frame->used + from + amount,
		  (end - from) * sizeof current_frame->used[0]);

      safe_bcopy (current_frame->highlight + from,
		  current_frame->highlight + from + amount,
		  (end - from) * sizeof current_frame->highlight[0]);

      safe_bcopy (current_frame->enable + from,
		  current_frame->enable + from + amount,
		  (end - from) * sizeof current_frame->enable[0]);

748 749
      /* Adjust the lines by an amount
	 that puts the first of them at NEWPOS.  */
750
      pos_adjust = newpos - current_frame->charstarts[from + amount][0];
751 752 753 754 755

      /* Offset each char position in the charstarts lines we moved
	 by pos_adjust.  */
      for (i = from + amount; i < end + amount; i++)
	{
756
	  int *line = current_frame->charstarts[i];
757
	  int col;
758 759 760
	  for (col = 0; col < width; col++)
	    if (line[col] > 0)
	      line[col] += pos_adjust;
761
	}
762
      for (i = end + amount; i < end; i++)
763
	{
764
	  int *line = current_frame->charstarts[i];
765 766
	  int col;
	  line[0] = -1;
767
	  for (col = 0; col < width; col++)
768 769 770
	    line[col] = 0;
	}

Jim Blandy's avatar
Jim Blandy committed
771 772
      /* Mark the lines made empty by scrolling as enabled, empty and
	 normal video.  */
Jim Blandy's avatar
Jim Blandy committed
773 774 775 776
      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
777 778
      for (i = end + amount; i < end; i++)
	{
Jim Blandy's avatar
Jim Blandy committed
779
	  current_frame->glyphs[i][0] = '\0';
780
	  current_frame->charstarts[i][0] = 0;
Jim Blandy's avatar
Jim Blandy committed
781
	  current_frame->enable[i] = 1;
Jim Blandy's avatar
Jim Blandy committed
782 783
	}

Jim Blandy's avatar
Jim Blandy committed
784 785 786
      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
787

788 789
#ifdef HAVE_WINDOW_SYSTEM
      if (FRAME_WINDOW_P (frame))
Jim Blandy's avatar
Jim Blandy committed
790
	{
Jim Blandy's avatar
Jim Blandy committed
791 792 793
	  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
794

Jim Blandy's avatar
Jim Blandy committed
795 796 797
	  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
798

Jim Blandy's avatar
Jim Blandy committed
799 800 801
	  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
802

Jim Blandy's avatar
Jim Blandy committed
803 804 805
	  safe_bcopy (current_frame->pix_height + from,
		      current_frame->pix_height + from + amount,
		      (end - from) * sizeof current_frame->pix_height[0]);
806 807 808 809

	  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
810
	}
811
#endif /* HAVE_WINDOW_SYSTEM */
Jim Blandy's avatar
Jim Blandy committed
812

Jim Blandy's avatar
Jim Blandy committed
813
      update_end (frame);
Jim Blandy's avatar
Jim Blandy committed
814 815 816 817
    }
  return 1;
}

Jim Blandy's avatar
Jim Blandy committed
818
/* After updating a window W that isn't the full frame wide,
Jim Blandy's avatar
Jim Blandy committed
819
   copy all the columns that W does not occupy
Jim Blandy's avatar
Jim Blandy committed
820 821
   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
822 823 824 825 826

preserve_other_columns (w)
     struct window *w;
{
  register int vpos;
Jim Blandy's avatar
Jim Blandy committed
827 828
  register struct frame_glyphs *current_frame, *desired_frame;
  register FRAME_PTR frame = XFRAME (w->frame);
829 830
  int start = WINDOW_LEFT_MARGIN (w);
  int end = WINDOW_RIGHT_EDGE (w);
Jim Blandy's avatar
Jim Blandy committed
831 832
  int bot = XFASTINT (w->top) + XFASTINT (w->height);

Jim Blandy's avatar
Jim Blandy committed
833 834
  current_frame = FRAME_CURRENT_GLYPHS (frame);
  desired_frame = FRAME_DESIRED_GLYPHS (frame);
Jim Blandy's avatar
Jim Blandy committed
835 836 837

  for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
    {
Jim Blandy's avatar
Jim Blandy committed
838
      if (current_frame->enable[vpos] && desired_frame->enable[vpos])
Jim Blandy's avatar
Jim Blandy committed
839 840 841 842 843
	{
	  if (start > 0)
	    {
	      int len;

Jim Blandy's avatar
Jim Blandy committed
844
	      bcopy (current_frame->glyphs[vpos],
845
		     desired_frame->glyphs[vpos],
846
		     start * sizeof (current_frame->glyphs[vpos][0]));
847 848
	      bcopy (current_frame->charstarts[vpos],
		     desired_frame->charstarts[vpos],
849
		     start * sizeof (current_frame->charstarts[vpos][0]));
Jim Blandy's avatar
Jim Blandy committed
850 851 852
	      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
853
	    }
Jim Blandy's avatar
Jim Blandy committed
854 855
	  if (current_frame->used[vpos] > end
	      && desired_frame->used[vpos] < current_frame->used[vpos])
Jim Blandy's avatar
Jim Blandy committed
856
	    {
Jim Blandy's avatar
Jim Blandy committed
857
	      while (desired_frame->used[vpos] < end)
858 859 860 861 862
		{
		  int used = desired_frame->used[vpos]++;
		  desired_frame->glyphs[vpos][used] = SPACEGLYPH;
		  desired_frame->glyphs[vpos][used] = 0;
		}
Jim Blandy's avatar
Jim Blandy committed
863 864
	      bcopy (current_frame->glyphs[vpos] + end,
		     desired_frame->glyphs[vpos] + end,
865
		     ((current_frame->used[vpos] - end)
866
		      * sizeof (current_frame->glyphs[vpos][0])));
867 868 869
	      bcopy (current_frame->charstarts[vpos] + end,
		     desired_frame->charstarts[vpos] + end,
		     ((current_frame->used[vpos] - end)
870
		      * sizeof (current_frame->charstarts[vpos][0])));
Jim Blandy's avatar
Jim Blandy committed
871
	      desired_frame->used[vpos] = current_frame->used[vpos];
Jim Blandy's avatar
Jim Blandy committed
872 873 874 875 876 877 878
	    }
	}
    }
}

#if 0

Jim Blandy's avatar
Jim Blandy committed
879
/* If window w does not need to be updated and isn't the full frame wide,
Jim Blandy's avatar
Jim Blandy committed
880
 copy all the columns that w does occupy
Jim Blandy's avatar
Jim Blandy committed
881 882
 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
883 884 885 886 887 888 889

 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
890 891
  register struct frame_glyphs *l1, *l2;
  register FRAME_PTR frame = XFRAME (w->frame);
892 893
  int start = WINDOW_LEFT_MARGIN (w);
  int end = WINDOW_RIGHT_EDGE (w);
Jim Blandy's avatar
Jim Blandy committed
894 895 896 897
  int bot = XFASTINT (w->top) + XFASTINT (w->height);

  for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
    {
Jim Blandy's avatar
Jim Blandy committed
898 899
      if ((l1 = FRAME_DESIRED_GLYPHS (frame)->glyphs[vpos + 1])
	  && (l2 = FRAME_PHYS_GLYPHS (frame)->glyphs[vpos + 1]))
Jim Blandy's avatar
Jim Blandy committed
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
	{
	  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

916
/* Adjust by ADJUST the charstart values in window W
917
   after vpos VPOS, which counts relative to the frame
918 919 920 921 922 923 924 925
   (not relative to W itself).  */

void
adjust_window_charstarts (w, vpos, adjust)
     struct window *w;
     int vpos;
     int adjust;
{
926
  int left = WINDOW_LEFT_MARGIN (w);
927
  int top = XFASTINT (w->top);
928 929
  int right = left + window_internal_width (w);
  int bottom = top + window_internal_height (w);
930 931
  int i;

932
  for (i = vpos + 1; i < bottom; i++)
933 934 935 936 937 938 939 940 941
    {
      int *charstart
	= FRAME_CURRENT_GLYPHS (XFRAME (WINDOW_FRAME (w)))->charstarts[i];
      int j;
      for (j = left; j < right; j++)
	if (charstart[j] > 0)
	  charstart[j] += adjust;
    }
}
942

943 944 945 946
/* Check the charstarts values in the area of window W
   for internal consistency.  We cannot check that they are "right";
   we can only look for something nonsensical.  */

947 948 949 950 951 952 953
verify_charstarts (w)
     struct window *w;
{
  FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
  int i;
  int top = XFASTINT (w->top);
  int bottom = top + window_internal_height (w);
954
  int left = WINDOW_LEFT_MARGIN (w);
955 956
  int right = left + window_internal_width (w);
  int next_line;
957 958
  int truncate = (XINT (w->hscroll)
		  || (truncate_partial_width_windows
959
		      && !WINDOW_FULL_WIDTH_P (w))
960
		  || !NILP (XBUFFER (w->buffer)->truncate_lines));
961 962 963 964 965

  for (i = top; i < bottom; i++)
    {
      int j;
      int last;
966
      int *charstart = FRAME_CURRENT_GLYPHS (f)->charstarts[i];
967 968

      if (i != top)
969 970 971 972 973 974 975 976 977 978 979 980 981 982
	{
	  if (truncate)
	    {
	      /* If we are truncating lines, allow a jump
		 in charstarts from one line to the next.  */
	      if (charstart[left] < next_line)
		abort ();
	    }
	  else
	    {
	      if (charstart[left] != next_line)
		abort ();
	    }
	}
983 984 985 986

      for (j = left; j < right; j++)
	if (charstart[j] > 0)
	  last = charstart[j];
987 988 989 990 991 992 993 994 995
      /* Record where the next line should start.  */
      next_line = last;
      if (BUF_ZV (XBUFFER (w->buffer)) != last)
	{
	  /* If there's a newline between the two lines, count that.  */
	  int endchar = *BUF_CHAR_ADDRESS (XBUFFER (w->buffer), last);
	  if (endchar == '\n')
	    next_line++;
	}
996 997
    }
}
998

Jim Blandy's avatar
Jim Blandy committed
999 1000 1001 1002 1003 1004 1005 1006
/* 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;
1007 1008
  register struct frame_glyphs *desired_glyphs
    = FRAME_DESIRED_GLYPHS (XFRAME (w->frame));
1009
  register int start = WINDOW_LEFT_MARGIN (w);
Jim Blandy's avatar
Jim Blandy committed
1010 1011 1012 1013 1014 1015 1016 1017
  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
1018
/* These functions try to perform directly and immediately on the frame
Jim Blandy's avatar
Jim Blandy committed
1019 1020 1021
   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
1022
   They assume that the frame was up to date before the buffer
1023
   change being displayed.  They make various other assumptions too;