/* Calculate what ins/del line to do, and do it, for Emacs redisplay.
   Copyright (C) 1985, 1986 Free Software Foundation, Inc.

This file is part of GNU Emacs.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.  No author or distributor
accepts responsibility to anyone for the consequences of using it
or for whether it serves any particular purpose or works at all,
unless he says so in writing.  Refer to the GNU Emacs General Public
License for full details.

Everyone is granted permission to copy, modify and redistribute
GNU Emacs, but only under the conditions described in the
GNU Emacs General Public License.   A copy of this license is
supposed to have been given to you along with GNU Emacs so you
can know your rights and responsibilities.  It should be in a
file named COPYING.  Among other things, the copyright notice
and this notice must be preserved on all copies.  */


#include "config.h"
#include "termchar.h"
#include "dispextern.h"

#include "screen.h"  /* [amc] */
#include "screenW.h"/* [amc] */

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

/* All costs measured in characters.  Therefore, no cost
   can exceed MScreenLength * MScreenWidth (or so).
   That is about 10000, which fits in a short.  */

#define INFINITY 15000

struct matrix_elt
  {
    /* Cost of outputting through this line
       if no insert/delete is done just above it.  */
    short writecost;
    /* Cost of outputting through this line
       if an insert is done just above it.  */
    short insertcost;
    /* Cost of outputting through this line
       if a delete is done just above it.  */
    short deletecost;
    /* Number of inserts so far in this run of inserts,
       for the cost in insertcost.  */
    char insertcount;
    /* Number of deletes so far in this run of deletes,
       for the cost in deletecost.  */
    char deletecount;
  };

/* See CalcIDCosts for on the arrays below */
int ILcost[MScreenLength];/* ov(n) + 1*mf(n) */
int DLcost[MScreenLength];/* ov(n) + 1*mf(n) */
int ILncost[MScreenLength];/* mf(n) */
int DLncost[MScreenLength];/* mf(n) */

Cscrolling_1 (window_size, unchanged_at_top, unchanged_at_bottom,
	     draw_cost, old_hash, new_hash, free_at_end, rb)
     int window_size, unchanged_at_top, unchanged_at_bottom;
     int *draw_cost;
     int *old_hash;
     int *new_hash;
     int free_at_end;
	struct Root_Block *rb;
{
  struct matrix_elt *matrix;
  matrix = ((struct matrix_elt *)
	    alloca ((window_size + 1) * (window_size + 1) * sizeof *matrix));

  Ccalculate_scrolling (matrix, window_size, unchanged_at_bottom,
		       draw_cost, old_hash, new_hash,
		       free_at_end, rb);
  Cdo_scrolling (matrix, window_size, unchanged_at_top, rb);
}

/* Determine, in matrix[i,j], the cost of updating the first j old lines
   into the first i new lines.
   This involves using insert or delete somewhere if i != j.
   For each matrix elements, three kinds of costs are recorded:
   the smallest cost that ends with an insert, the smallest
   cost that ends with a delete, and the smallest cost that
   ends with neither one.  These are kept separate because
   on some terminals the cost of doing an insert varies
   depending on whether one was just done, etc.  */

/* draw_cost[VPOS] is the cost of outputting new line at VPOS.
   old_hash[VPOS] is the hash code of the old line at VPOS.
   new_hash[VPOS] is the hash code of the new line at VPOS.
   Note that these are not true screen vpos's, but relative
   to the place at which the first mismatch between old and
   new contents appears.  */

Ccalculate_scrolling (matrix, window_size, lines_below,
		     draw_cost, old_hash, new_hash,
		     free_at_end, rb)
     /* matrix is of size window_size + 1 on each side.  */
     struct matrix_elt *matrix;
     int window_size;
     int *draw_cost;
     int *old_hash;
     int *new_hash;
     int free_at_end;
	struct Root_Block *rb;
{
  register int i, j;
  register struct matrix_elt *p, *p1;
  register int cost, cost1;
  WS_DEF;

  int lines_moved = window_size + (scroll_region_ok ? 0 : lines_below);
  int *first_insert_cost = &ILcost[ws->height - lines_moved];
  int *first_delete_cost = &DLcost[ws->height - lines_moved];
  int *next_insert_cost = &ILncost[ws->height - lines_moved];
  int *next_delete_cost = &DLncost[ws->height - lines_moved];

  /* initialize the top left corner of the matrix */
  matrix->writecost = 0;
  matrix->insertcost = INFINITY;
  matrix->deletecost = INFINITY;
  matrix->insertcount = 0;
  matrix->deletecount = 0;

  /* initialize the left edge of the matrix */
  cost = first_insert_cost[1] - next_insert_cost[1];
  for (i = 1; i <= window_size; i++)
    {
      p = matrix + i * (window_size + 1);
      cost += draw_cost[i] + next_insert_cost[i];
      p->insertcost = cost;
      p->writecost = INFINITY;
      p->deletecost = INFINITY;
      p->insertcount = i;
      p->deletecount = 0;
    }

  /* initialize the top edge of the matrix */
  cost = first_delete_cost[1] - next_delete_cost[1];
  for (j = 1; j <= window_size; j++)
    {
      cost += next_delete_cost[j];
      matrix[j].deletecost = cost;
      matrix[j].writecost = INFINITY;
      matrix[j].insertcost = INFINITY;
      matrix[j].deletecount = j;
      matrix[j].insertcount = 0;
    }

  /* `i' represents the vpos among new screen contents.
     `j' represents the vpos among the old screen contents.  */
  p = matrix + window_size + 2;	/* matrix [1, 1] */
  for (i = 1; i <= window_size; i++, p++)
    for (j = 1; j <= window_size; j++, p++)
      {
	/* p contains the address of matrix [i, j] */

	/* First calculate the cost assuming we do
	   not insert or delete above this line.
	   That is, if we update through line i-1
	   based on old lines through j-1,
	   and then just change old line j to new line i.  */
	p1 = p - window_size - 2; /* matrix [i-1, j-1] */
	cost = p1->writecost;
	if (cost > p1->insertcost)
	  cost = p1->insertcost;
	if (cost > p1->deletecost)
	  cost = p1->deletecost;
	if (old_hash[j] != new_hash[i])
	  cost += draw_cost[i];
	p->writecost = cost;

	/* Calculate the cost if we do an insert-line
	   before outputting this line.
	   That is, we update through line i-1
	   based on old lines through j,
	   do an insert-line on line i,
	   and then output line i from scratch,
	   leaving old lines starting from j for reuse below.  */
	p1 = p - window_size - 1; /* matrix [i-1, j] */
	/* No need to think about doing a delete followed
	   immediately by an insert.  It cannot be as good
	   as not doing either of them.  */
	if (free_at_end == i)
	  {
	    cost = p1->writecost;
	    cost1 = p1->insertcost;
	  }
	else
	  {
	    cost = p1->writecost + first_insert_cost[i];
	    if (p1->insertcount > i)
	      abort ();
	    cost1 = p1->insertcost + next_insert_cost[i - p1->insertcount];
	  }
	p->insertcost = min (cost, cost1) + draw_cost[i];
	p->insertcount = (cost < cost1) ? 1 : p1->insertcount + 1;
	if (p->insertcount > i)
	  abort ();

	/* Calculate the cost if we do a delete line after
	   outputting this line.
	   That is, we update through line i
	   based on old lines through j-1,
	   and throw away old line j.  */
	p1 = p - 1;		/* matrix [i, j-1] */
	/* No need to think about doing an insert followed
	   immediately by a delete.  */
	if (free_at_end == i)
	  {
	    cost = p1->writecost;
	    cost1 = p1->deletecost;
	  }
	else
	  {
	    cost = p1->writecost + first_delete_cost[i];
	    cost1 = p1->deletecost + next_delete_cost[i];
	  }
	p->deletecost = min (cost, cost1);
	p->deletecount = (cost < cost1) ? 1 : p1->deletecount + 1;
      }
}

/* Perform insert-lines and delete-lines operations
 according to the costs in the matrix.
 Updates the contents of PhysScreen to record what was done. */

Cdo_scrolling (matrix, window_size, unchanged_at_top, rb)
     struct matrix_elt *matrix;
     int window_size;
     int unchanged_at_top;
	struct Root_Block *rb;
{
  register struct matrix_elt *p;
  register int i, j;
  struct { int count, pos; } queue[MScreenLength];
  int offset = unchanged_at_top;
  int qi = 0;
  int window = 0;
  register int tem;
  WS_DEF;
#ifdef NO_COED
  extern struct display_line *PhysScreen[MScreenLength + 1];
#endif
  extern struct display_line *OPhysScreen[MScreenLength + 1];

/* First do all deletions of lines; queue up insertions.
  Also move lines to correct slots in PhysScreen */

  i = j = window_size;

  while (i > 0 || j > 0)
    {
      p = matrix + i * (window_size + 1) + j;
      tem = p->insertcost;
      if (tem < p->writecost && tem < p->deletecost)
	{
	  /* Insert should be done at vpos i-1, plus maybe some before */
	  queue[qi].count = p->insertcount;
	  i -= p->insertcount;
	  queue[qi++].pos = i + unchanged_at_top;
	}
      else if (p->deletecost < p->writecost)
	{
	  /* Old line at vpos j-1, and maybe some before it,
	     should be deleted */
	  j -= p->deletecount;
 	  if (!window)
	    {
	      CXTset_terminal_window (window_size + unchanged_at_top, rb);
	      window = 1;
	    }
	  CXTins_del_lines (j + unchanged_at_top, - p->deletecount,rb);
	}
      else
	{
	  /* Best thing done here is no insert or delete */
	  /* Old line at vpos j-1 ends up at vpos i-1 */
	  ws->phys_line[i + offset] = OPhysScreen[j + offset];
	  i--;
	  j--;
	}
    }

  if (!window && qi)
    {
      CXTset_terminal_window (window_size + unchanged_at_top, rb);
      window = 1;
    }

  /* Now do all insertions */

  for (i = qi - 1; i >= 0; i--)
    {
      CXTins_del_lines (queue[i].pos, queue[i].count, rb);
      /* mark the inserted lines as clear */
      tem = queue[i].pos;
      for (j = tem + queue[i].count; j > tem; j--)
	ws->phys_line[j] = 0;
    }

  if (window)
    CXTset_terminal_window (0, rb);
}

/* Return number of lines in common between PhysScreen and DesiredScreen,
   considering only vpos range START to END (not including END).
   Ignores short lines (length < 20) on the assumption that
   avoiding redrawing such a line will have little weight.  */

int
scrolling_max_lines_saved (start, end, oldhash, newhash, cost)
     int start, end;
     int *oldhash, *newhash, *cost;
{
  struct { int hash; int count; } lines[01000];
  register int i, h;
  register int matchcount = 0;

  bzero (lines, sizeof lines);

  /* Put new lines' hash codes in hash table.  */
  for (i = start; i < end; i++)
    {
      if (cost[i] > 20)
	{
	  h = newhash[i] & 0777;
	  lines[h].hash = newhash[i];
	  lines[h].count++;
	}
    }

  /* Look up old line hash codes in the hash table.
     Count number of matches between old lines and new.  */

  for (i = start; i < end; i++)
    {
      h = oldhash[i] & 0777;
      if (oldhash[i] == lines[h].hash)
	{
	  matchcount++;
	  if (--lines[h].count == 0)
	    lines[h].hash = 0;
	}
    }

  return matchcount;
}

/* Return a measure of the cost of moving the lines
   starting with vpos FROM, up to but not including vpos TO,
   down by AMOUNT lines (AMOUNT may be negative).
   These are the same arguments that might be given to
   scroll_screen_lines to perform this scrolling.  */

Cscroll_cost (from, to, amount, rb)
     int from, to, amount;
	struct Root_Block *rb;
{
  /* Compute how many lines, at bottom of screen,
     will not be involved in actual motion.  */
  int ok_below = XWSCREEN(rb->win)->height - to;
  if (amount > 0) ok_below -= amount;
  if (! scroll_region_ok) ok_below = 0;

  if (amount == 0)
    return 0;

  if (amount < 0)
    {
      int temp = to;
      to = from + amount;
      from = temp + amount;
      amount = - amount;
    }

  from += ok_below;
  to += ok_below;

  return ILcost[from] + (amount - 1) * ILncost[from]
    + DLcost[to] + (amount - 1) * DLncost[to];
}
