home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / emacs-19.28-src.tgz / tar.out / fsf / emacs / src / scroll.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  22KB  |  675 lines

  1. /* Calculate what line insertion or deletion to do, and do it,
  2.    Copyright (C) 1985, 1986, 1990, 1993, 1994 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. 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)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <config.h>
  22. #include "termchar.h"
  23. #include "lisp.h"
  24. #include "dispextern.h"
  25. #include "frame.h"
  26.  
  27. extern struct display_line **ophys_lines;
  28.  
  29. #define max(a, b) ((a) > (b) ? (a) : (b))
  30. #define min(a, b) ((a) < (b) ? (a) : (b))
  31.  
  32. /* All costs measured in characters.
  33.    So no cost can exceed the area of a frame, measured in characters.
  34.    Let's hope this is never more than 1000000 characters.  */
  35.  
  36. #define INFINITY 1000000
  37.  
  38. struct matrix_elt
  39.   {
  40.     /* Cost of outputting through this line
  41.        if no insert/delete is done just above it.  */
  42.     int writecost;
  43.     /* Cost of outputting through this line
  44.        if an insert is done just above it.  */
  45.     int insertcost;
  46.     /* Cost of outputting through this line
  47.        if a delete is done just above it.  */
  48.     int deletecost;
  49.     /* Number of inserts so far in this run of inserts,
  50.        for the cost in insertcost.  */
  51.     unsigned char insertcount;
  52.     /* Number of deletes so far in this run of deletes,
  53.        for the cost in deletecost.  */
  54.     unsigned char deletecount;
  55.   };
  56.  
  57.  
  58. #ifdef FAST_DISPLAY
  59. scrolling_1 (window_size, unchanged_at_top, unchanged_at_bottom,
  60.          draw_cost, old_hash, new_hash, free_at_end)
  61.      int window_size, unchanged_at_top, unchanged_at_bottom;
  62.      int *draw_cost;
  63.      int *old_hash;
  64.      int *new_hash;
  65.      int free_at_end;
  66. {
  67.   int lines, i;
  68.   int window_end = unchanged_at_top + window_size;
  69.  
  70.   /* Rebase arrays at line 0 */
  71.   old_hash -= unchanged_at_top - 1;
  72.   new_hash -= unchanged_at_top - 1;
  73.   draw_cost -= unchanged_at_top - 1;
  74.  
  75.   /* We can't allow un-enabled lines to be scrolled (they are not redrawable). 
  76.      Restrict window to the first set of contiguous enabled lines 
  77.      (an enabled line has draw_cost[x] != INFINITY) */
  78.   for (i = unchanged_at_top; i < window_end && draw_cost[i] != INFINITY; i++) ;
  79.   /*unchanged_at_bottom += window_size - i;*/
  80.   /*window_size = i - unchanged_at_top;*/
  81.   window_end = i;
  82.  
  83.   if (lines = calc_scroll(old_hash, new_hash, unchanged_at_top, window_end))
  84.       scroll_frame_lines(selected_frame, unchanged_at_top, window_end - lines, lines, unchanged_at_top); /* CHFIXME: last param ok? */
  85.   else if (lines = calc_scroll(new_hash, old_hash, unchanged_at_top, window_end))
  86.       scroll_frame_lines(selected_frame, unchanged_at_top + lines , window_end , -lines, unchanged_at_top + lines); /* CHFIXME: last param ok? */
  87. }
  88.  
  89. int calc_scroll(int *old_hash, int *new_hash, int from, int to)
  90. /* For insert attempt, the parameters are correct.
  91.    For delete attempt, swap the old & new hash parameters
  92. */
  93. {
  94.     int try = from + 1, lines, i;
  95.     int hash1 = old_hash[from];
  96.  
  97.     do
  98.     {
  99.     if (hash1 == new_hash[try])
  100.     {
  101.         /* Check if other lines match too */
  102.         lines = try - from; /* Amount to insert */
  103.         for (i = try + 1; i < to; i++)
  104.         if (old_hash[i - lines] != new_hash[i]) break;
  105.         if (i == to) /* It works ! */
  106.         return lines;
  107.     }
  108.     try++;
  109.     }
  110.     while (try < to);
  111.  
  112.     return 0;
  113. }
  114.  
  115. #else /* not FAST_DISPLAY */
  116. /* Determine, in matrix[i,j], the cost of updating the first j old lines
  117.    into the first i new lines.
  118.    This involves using insert or delete somewhere if i != j.
  119.    For each matrix elements, three kinds of costs are recorded:
  120.    the smallest cost that ends with an insert, the smallest
  121.    cost that ends with a delete, and the smallest cost that
  122.    ends with neither one.  These are kept separate because
  123.    on some terminals the cost of doing an insert varies
  124.    depending on whether one was just done, etc.  */
  125.  
  126. /* draw_cost[VPOS] is the cost of outputting new line at VPOS.
  127.    old_hash[VPOS] is the hash code of the old line at VPOS.
  128.    new_hash[VPOS] is the hash code of the new line at VPOS.
  129.    Note that these are not true frame vpos's, but relative
  130.    to the place at which the first mismatch between old and
  131.    new contents appears.  */
  132.  
  133. static void
  134. calculate_scrolling (frame, matrix, window_size, lines_below,
  135.              draw_cost, old_hash, new_hash,
  136.              free_at_end)
  137.      FRAME_PTR frame;
  138.      /* matrix is of size window_size + 1 on each side.  */
  139.      struct matrix_elt *matrix;
  140.      int window_size;
  141.      int *draw_cost;
  142.      int *old_hash;
  143.      int *new_hash;
  144.      int free_at_end;
  145. {
  146.   register int i, j;
  147.   int frame_height = FRAME_HEIGHT (frame);
  148.   register struct matrix_elt *p, *p1;
  149.   register int cost, cost1;
  150.  
  151.   int lines_moved = window_size + (scroll_region_ok ? 0 : lines_below);
  152.   /* first_insert_cost[I] is the cost of doing the first insert-line
  153.      at the I'th line of the lines we are considering,
  154.      where I is origin 1 (as it is below).  */
  155.   int *first_insert_cost
  156.     = &FRAME_INSERT_COST (frame)[frame_height - 1 - lines_moved];
  157.   int *first_delete_cost
  158.     = &FRAME_DELETE_COST (frame)[frame_height - 1 - lines_moved];
  159.   int *next_insert_cost
  160.     = &FRAME_INSERTN_COST (frame)[frame_height - 1 - lines_moved];
  161.   int *next_delete_cost
  162.     = &FRAME_DELETEN_COST (frame)[frame_height - 1 - lines_moved];
  163.  
  164.   /* Discourage long scrolls on fast lines.
  165.      Don't scroll nearly a full frame height unless it saves
  166.      at least 1/4 second.  */
  167.   int extra_cost = baud_rate / (10 * 4 * FRAME_HEIGHT (frame));
  168.  
  169.   if (baud_rate <= 0)
  170.     extra_cost = 1;
  171.  
  172.   /* initialize the top left corner of the matrix */
  173.   matrix->writecost = 0;
  174.   matrix->insertcost = INFINITY;
  175.   matrix->deletecost = INFINITY;
  176.   matrix->insertcount = 0;
  177.   matrix->deletecount = 0;
  178.  
  179.   /* initialize the left edge of the matrix */
  180.   cost = first_insert_cost[1] - next_insert_cost[1];
  181.   for (i = 1; i <= window_size; i++)
  182.     {
  183.       p = matrix + i * (window_size + 1);
  184.       cost += draw_cost[i] + next_insert_cost[i] + extra_cost;
  185.       p->insertcost = cost;
  186.       p->writecost = INFINITY;
  187.       p->deletecost = INFINITY;
  188.       p->insertcount = i;
  189.       p->deletecount = 0;
  190.     }
  191.  
  192.   /* initialize the top edge of the matrix */
  193.   cost = first_delete_cost[1] - next_delete_cost[1];
  194.   for (j = 1; j <= window_size; j++)
  195.     {
  196.       cost += next_delete_cost[j];
  197.       matrix[j].deletecost = cost;
  198.       matrix[j].writecost = INFINITY;
  199.       matrix[j].insertcost = INFINITY;
  200.       matrix[j].deletecount = j;
  201.       matrix[j].insertcount = 0;
  202.     }
  203.  
  204.   /* `i' represents the vpos among new frame contents.
  205.      `j' represents the vpos among the old frame contents.  */
  206.   p = matrix + window_size + 2;    /* matrix [1, 1] */
  207.   for (i = 1; i <= window_size; i++, p++)
  208.     for (j = 1; j <= window_size; j++, p++)
  209.       {
  210.     /* p contains the address of matrix [i, j] */
  211.  
  212.     /* First calculate the cost assuming we do
  213.        not insert or delete above this line.
  214.        That is, if we update through line i-1
  215.        based on old lines through j-1,
  216.        and then just change old line j to new line i.  */
  217.     p1 = p - window_size - 2; /* matrix [i-1, j-1] */
  218.     cost = p1->writecost;
  219.     if (cost > p1->insertcost)
  220.       cost = p1->insertcost;
  221.     if (cost > p1->deletecost)
  222.       cost = p1->deletecost;
  223.     if (old_hash[j] != new_hash[i])
  224.       cost += draw_cost[i];
  225.     p->writecost = cost;
  226.  
  227.     /* Calculate the cost if we do an insert-line
  228.        before outputting this line.
  229.        That is, we update through line i-1
  230.        based on old lines through j,
  231.        do an insert-line on line i,
  232.        and then output line i from scratch,
  233.        leaving old lines starting from j for reuse below.  */
  234.     p1 = p - window_size - 1; /* matrix [i-1, j] */
  235.     /* No need to think about doing a delete followed
  236.        immediately by an insert.  It cannot be as good
  237.        as not doing either of them.  */
  238.     if (free_at_end == i)
  239.       {
  240.         cost = p1->writecost;
  241.         cost1 = p1->insertcost;
  242.       }
  243.     else
  244.       {
  245.         cost = p1->writecost + first_insert_cost[i];
  246.         if ((int) p1->insertcount > i)
  247.           abort ();
  248.         cost1 = p1->insertcost + next_insert_cost[i - p1->insertcount];
  249.       }
  250.     p->insertcost = min (cost, cost1) + draw_cost[i] + extra_cost;
  251.     p->insertcount = (cost < cost1) ? 1 : p1->insertcount + 1;
  252.     if ((int) p->insertcount > i)
  253.       abort ();
  254.  
  255.     /* Calculate the cost if we do a delete line after
  256.        outputting this line.
  257.        That is, we update through line i
  258.        based on old lines through j-1,
  259.        and throw away old line j.  */
  260.     p1 = p - 1;        /* matrix [i, j-1] */
  261.     /* No need to think about doing an insert followed
  262.        immediately by a delete.  */
  263.     if (free_at_end == i)
  264.       {
  265.         cost = p1->writecost;
  266.         cost1 = p1->deletecost;
  267.       }
  268.     else
  269.       {
  270.         cost = p1->writecost + first_delete_cost[i];
  271.         cost1 = p1->deletecost + next_delete_cost[i];
  272.       }
  273.     p->deletecost = min (cost, cost1);
  274.     p->deletecount = (cost < cost1) ? 1 : p1->deletecount + 1;
  275.       }
  276. }
  277.  
  278. /* Perform insert-lines and delete-lines operations on FRAME
  279.    according to the costs in MATRIX.
  280.    Update the frame's current_glyphs info to record what was done.
  281.  
  282.    WINDOW_SIZE is the number of lines being considered for scrolling
  283.    and UNCHANGED_AT_TOP is the vpos of the first line being considered.
  284.    These two arguments can specify any contiguous range of lines.
  285.  
  286.    We also shuffle the charstarts vectors for the lines
  287.    along with the glyphs; but the results are not quite right,
  288.    since we cannot offset them for changes in amount of text
  289.    in this line or that line.  Luckily it doesn't matter,
  290.    since update_frame and update_line will copy in the proper
  291.    new charstarts vectors from the frame's desired_glyphs.  */
  292.  
  293. static void
  294. do_scrolling (frame, matrix, window_size, unchanged_at_top)
  295.      FRAME_PTR frame;
  296.      struct matrix_elt *matrix;
  297.      int window_size;
  298.      int unchanged_at_top;
  299. {
  300.   register struct matrix_elt *p;
  301.   register int i, j;
  302.   register struct frame_glyphs *current_frame;
  303.   /* temp_frame->enable[i] means line i has been moved to current_frame.  */
  304.   register struct frame_glyphs *temp_frame;
  305.   struct queue { int count, pos; } *queue;
  306.   int offset = unchanged_at_top;
  307.   int qi = 0;
  308.   int window = 0;
  309.   register int tem;
  310.   int next;
  311.  
  312.   queue = (struct queue *) alloca (FRAME_HEIGHT (frame)
  313.                    * sizeof (struct queue));
  314.  
  315.   current_frame = FRAME_CURRENT_GLYPHS (frame);
  316.   temp_frame = FRAME_TEMP_GLYPHS (frame);
  317.  
  318.   bcopy (current_frame->glyphs, temp_frame->glyphs,
  319.      current_frame->height * sizeof (GLYPH *));
  320.   bcopy (current_frame->charstarts, temp_frame->charstarts,
  321.      current_frame->height * sizeof (GLYPH *));
  322.   bcopy (current_frame->used, temp_frame->used,
  323.      current_frame->height * sizeof (int));
  324.   bcopy (current_frame->highlight, temp_frame->highlight,
  325.      current_frame->height * sizeof (char));
  326.   bzero (temp_frame->enable, temp_frame->height * sizeof (char));
  327.   bcopy (current_frame->bufp, temp_frame->bufp,
  328.      current_frame->height * sizeof (int));
  329.  
  330. #ifdef HAVE_X_WINDOWS
  331.   if (FRAME_X_P (frame))
  332.     {
  333.       bcopy (current_frame->top_left_x, temp_frame->top_left_x,
  334.          current_frame->height * sizeof (short));
  335.       bcopy (current_frame->top_left_y, temp_frame->top_left_y,
  336.          current_frame->height * sizeof (short));
  337.       bcopy (current_frame->pix_width, temp_frame->pix_width,
  338.          current_frame->height * sizeof (short));
  339.       bcopy (current_frame->pix_height, temp_frame->pix_height,
  340.          current_frame->height * sizeof (short));
  341.       bcopy (current_frame->max_ascent, temp_frame->max_ascent,
  342.          current_frame->height * sizeof (short));
  343.     }
  344. #endif
  345.  
  346.   i = j = window_size;
  347.  
  348.   while (i > 0 || j > 0)
  349.     {
  350.       p = matrix + i * (window_size + 1) + j;
  351.       tem = p->insertcost;
  352.       if (tem < p->writecost && tem < p->deletecost)
  353.     {
  354.       /* Insert should be done at vpos i-1, plus maybe some before */
  355.       queue[qi].count = p->insertcount;
  356.       i -= p->insertcount;
  357.       queue[qi++].pos = i + unchanged_at_top;
  358.     }
  359.       else if (p->deletecost < p->writecost)
  360.     {
  361.       /* Old line at vpos j-1, and maybe some before it,
  362.          should be deleted */
  363.       j -= p->deletecount;
  364.        if (!window)
  365.         {
  366.           set_terminal_window (window_size + unchanged_at_top);
  367.           window = 1;
  368.         }
  369.       ins_del_lines (j + unchanged_at_top, - p->deletecount);
  370.     }
  371.       else
  372.     {
  373.       /* Best thing done here is no insert or delete */
  374.       /* Old line at vpos j-1 ends up at vpos i-1 */
  375.       current_frame->glyphs[i + offset - 1]
  376.         = temp_frame->glyphs[j + offset - 1];
  377.       current_frame->charstarts[i + offset - 1]
  378.         = temp_frame->charstarts[j + offset - 1];
  379.       current_frame->used[i + offset - 1]
  380.         = temp_frame->used[j + offset - 1];
  381.       current_frame->highlight[i + offset - 1]
  382.         = temp_frame->highlight[j + offset - 1];
  383.  
  384.       temp_frame->enable[j + offset - 1] = 1;
  385.       i--;
  386.       j--;
  387.     }
  388.     }
  389.  
  390.   if (!window && qi)
  391.     {
  392.       set_terminal_window (window_size + unchanged_at_top);
  393.       window = 1;
  394.     }
  395.  
  396.   /* Now do all insertions */
  397.  
  398.   next = unchanged_at_top;
  399.   for (i = qi - 1; i >= 0; i--)
  400.     {
  401.       ins_del_lines (queue[i].pos, queue[i].count);
  402.  
  403.       /* Mark the inserted lines as clear,
  404.      and put into them the line-contents strings
  405.      that were discarded during the deletions.
  406.      Those are the ones for which temp_frame->enable was not set.  */
  407.       tem = queue[i].pos;
  408.       for (j = tem + queue[i].count - 1; j >= tem; j--)
  409.     {
  410.       current_frame->enable[j] = 0;
  411.       while (temp_frame->enable[next])
  412.         next++;
  413.       current_frame->glyphs[j] = temp_frame->glyphs[next];
  414.       current_frame->charstarts[j] = temp_frame->charstarts[next++];
  415.     }
  416.     }
  417.  
  418.   if (window)
  419.     set_terminal_window (0);
  420. }
  421.  
  422. void
  423. scrolling_1 (frame, window_size, unchanged_at_top, unchanged_at_bottom,
  424.          draw_cost, old_hash, new_hash, free_at_end)
  425.      FRAME_PTR frame;
  426.      int window_size, unchanged_at_top, unchanged_at_bottom;
  427.      int *draw_cost;
  428.      int *old_hash;
  429.      int *new_hash;
  430.      int free_at_end;
  431. {
  432.   struct matrix_elt *matrix;
  433.   matrix = ((struct matrix_elt *)
  434.         alloca ((window_size + 1) * (window_size + 1) * sizeof *matrix));
  435.  
  436.   calculate_scrolling (frame, matrix, window_size, unchanged_at_bottom,
  437.                draw_cost, old_hash, new_hash,
  438.                free_at_end);
  439.   do_scrolling (frame, matrix, window_size, unchanged_at_top);
  440. }
  441. #endif /* not FAST_DISPLAY */
  442.  
  443. /* Return number of lines in common between current and desired frame contents
  444.    described to us only as vectors of hash codes OLDHASH and NEWHASH.
  445.    Consider only vpos range START to END (not including END).
  446.    Ignore short lines on the assumption that
  447.    avoiding redrawing such a line will have little weight.  */
  448.  
  449. int
  450. scrolling_max_lines_saved (start, end, oldhash, newhash, cost)
  451.      int start, end;
  452.      int *oldhash, *newhash, *cost;
  453. {
  454.   struct { int hash; int count; } lines[01000];
  455.   register int i, h;
  456.   register int matchcount = 0;
  457.   int avg_length = 0;
  458.   int threshold;
  459.  
  460.   /* Compute a threshold which is 1/4 of average length of these lines.  */
  461.  
  462.   for (i = start; i < end; i++)
  463.     avg_length += cost[i];
  464.  
  465.   avg_length /= end - start;
  466.   threshold = avg_length / 4;
  467.  
  468.   bzero (lines, sizeof lines);
  469.  
  470.   /* Put new lines' hash codes in hash table.
  471.      Ignore lines shorter than the threshold.
  472.      Thus, if the lines that are in common
  473.      are mainly the ones that are short,
  474.      they won't count.  */
  475.   for (i = start; i < end; i++)
  476.     {
  477.       if (cost[i] > threshold)
  478.     {
  479.       h = newhash[i] & 0777;
  480.       lines[h].hash = newhash[i];
  481.       lines[h].count++;
  482.     }
  483.     }
  484.  
  485.   /* Look up old line hash codes in the hash table.
  486.      Count number of matches between old lines and new.  */
  487.  
  488.   for (i = start; i < end; i++)
  489.     {
  490.       h = oldhash[i] & 0777;
  491.       if (oldhash[i] == lines[h].hash)
  492.     {
  493.       matchcount++;
  494.       if (--lines[h].count == 0)
  495.         lines[h].hash = 0;
  496.     }
  497.     }
  498.  
  499.   return matchcount;
  500. }
  501.  
  502. /* Return a measure of the cost of moving the lines
  503.    starting with vpos FROM, up to but not including vpos TO,
  504.    down by AMOUNT lines (AMOUNT may be negative).
  505.    These are the same arguments that might be given to
  506.    scroll_frame_lines to perform this scrolling.  */
  507.  
  508. scroll_cost (frame, from, to, amount)
  509.      FRAME_PTR frame;
  510.      int from, to, amount;
  511. {
  512.   /* Compute how many lines, at bottom of frame,
  513.      will not be involved in actual motion.  */
  514.   int limit = to;
  515.   int offset;
  516.   int height = FRAME_HEIGHT (frame);
  517.  
  518.   if (amount == 0)
  519.     return 0;
  520.  
  521.   if (! scroll_region_ok)
  522.     limit = height;
  523.   else if (amount > 0)
  524.     limit += amount;
  525.  
  526.   if (amount < 0)
  527.     {
  528.       int temp = to;
  529.       to = from + amount;
  530.       from = temp + amount;
  531.       amount = - amount;
  532.     }
  533.  
  534.   offset = height - limit;
  535.  
  536.   return
  537.     (FRAME_INSERT_COST (frame)[offset + from]
  538.      + (amount - 1) * FRAME_INSERTN_COST (frame)[offset + from]
  539.      + FRAME_DELETEN_COST (frame)[offset + to]
  540.      + (amount - 1) * FRAME_DELETE_COST (frame)[offset + to]);
  541. }
  542.  
  543. /* Calculate the line insertion/deletion
  544.    overhead and multiply factor values */
  545.  
  546. static void
  547. line_ins_del (frame, ov1, pf1, ovn, pfn, ov, mf)
  548.      FRAME_PTR frame;
  549.      int ov1, ovn;
  550.      int pf1, pfn;
  551.      register int *ov, *mf;
  552. {
  553.   register int i;
  554.   register int frame_height = FRAME_HEIGHT (frame);
  555.   register int insert_overhead = ov1 * 10;
  556.   register int next_insert_cost = ovn * 10;
  557.  
  558.   for (i = frame_height-1; i >= 0; i--)
  559.     {
  560.       mf[i] = next_insert_cost / 10;
  561.       next_insert_cost += pfn;
  562.       ov[i] = (insert_overhead + next_insert_cost) / 10;
  563.       insert_overhead += pf1;
  564.     }
  565. }
  566.  
  567. static void
  568. ins_del_costs (frame,
  569.            one_line_string, multi_string,
  570.            setup_string, cleanup_string,
  571.            costvec, ncostvec, coefficient)
  572.      FRAME_PTR frame;
  573.      char *one_line_string, *multi_string;
  574.      char *setup_string, *cleanup_string;
  575.      int *costvec, *ncostvec;
  576.      int coefficient;
  577. {
  578.   if (multi_string)
  579.     line_ins_del (frame,
  580.           string_cost (multi_string) * coefficient,
  581.           per_line_cost (multi_string) * coefficient,
  582.           0, 0, costvec, ncostvec);
  583.   else if (one_line_string)
  584.     line_ins_del (frame,
  585.           string_cost (setup_string) + string_cost (cleanup_string), 0,
  586.           string_cost (one_line_string),
  587.           per_line_cost (one_line_string),
  588.           costvec, ncostvec);
  589.   else
  590.     line_ins_del (frame,
  591.           9999, 0, 9999, 0,
  592.           costvec, ncostvec);
  593. }
  594.  
  595. /* Calculate the insert and delete line costs.
  596.    Note that this is done even when running with a window system
  597.    because we want to know how long scrolling takes (and avoid it).
  598.    This must be redone whenever the frame height changes.
  599.  
  600.    We keep the ID costs in a precomputed array based on the position
  601.    at which the I or D is performed.  Also, there are two kinds of ID
  602.    costs: the "once-only" and the "repeated".  This is to handle both
  603.    those terminals that are able to insert N lines at a time (once-
  604.    only) and those that must repeatedly insert one line.
  605.  
  606.    The cost to insert N lines at line L is
  607.            [tt.t_ILov  + (frame_height + 1 - L) * tt.t_ILpf] +
  608.     N * [tt.t_ILnov + (frame_height + 1 - L) * tt.t_ILnpf]
  609.  
  610.    ILov represents the basic insert line overhead.  ILpf is the padding
  611.    required to allow the terminal time to move a line: insertion at line
  612.    L changes (frame_height + 1 - L) lines.
  613.  
  614.    The first bracketed expression above is the overhead; the second is
  615.    the multiply factor.  Both are dependent only on the position at
  616.    which the insert is performed.  We store the overhead in
  617.    FRAME_INSERT_COST (frame) and the multiply factor in
  618.    FRAME_INSERTN_COST (frame).  Note however that any insertion
  619.    must include at least one multiply factor.  Rather than compute this
  620.    as FRAME_INSERT_COST (frame)[line]+FRAME_INSERTN_COST (frame)[line],
  621.    we add FRAME_INSERTN_COST (frame) into FRAME_INSERT_COST (frame).
  622.    This is reasonable because of the particular algorithm used in calcM.
  623.  
  624.    Deletion is essentially the same as insertion.
  625.  */
  626.  
  627. do_line_insertion_deletion_costs (frame,
  628.                   ins_line_string, multi_ins_string,
  629.                   del_line_string, multi_del_string,
  630.                   setup_string, cleanup_string, coefficient)
  631.      FRAME_PTR frame;
  632.      char *ins_line_string, *multi_ins_string;
  633.      char *del_line_string, *multi_del_string;
  634.      char *setup_string, *cleanup_string;
  635.      int coefficient;
  636. {
  637.   if (FRAME_INSERT_COST (frame) != 0)
  638.     {
  639.       FRAME_INSERT_COST (frame) =
  640.     (int *) xrealloc (FRAME_INSERT_COST (frame),
  641.               FRAME_HEIGHT (frame) * sizeof (int));
  642.       FRAME_DELETEN_COST (frame) =
  643.     (int *) xrealloc (FRAME_DELETEN_COST (frame),
  644.               FRAME_HEIGHT (frame) * sizeof (int));
  645.       FRAME_INSERTN_COST (frame) =
  646.     (int *) xrealloc (FRAME_INSERTN_COST (frame),
  647.               FRAME_HEIGHT (frame) * sizeof (int));
  648.       FRAME_DELETE_COST (frame) =
  649.     (int *) xrealloc (FRAME_DELETE_COST (frame),
  650.               FRAME_HEIGHT (frame) * sizeof (int));
  651.     }
  652.   else
  653.     {
  654.       FRAME_INSERT_COST (frame) =
  655.     (int *) xmalloc (FRAME_HEIGHT (frame) * sizeof (int));
  656.       FRAME_DELETEN_COST (frame) =
  657.     (int *) xmalloc (FRAME_HEIGHT (frame) * sizeof (int));
  658.       FRAME_INSERTN_COST (frame) =
  659.     (int *) xmalloc (FRAME_HEIGHT (frame) * sizeof (int));
  660.       FRAME_DELETE_COST (frame) = 
  661.     (int *) xmalloc (FRAME_HEIGHT (frame) * sizeof (int));
  662.     }
  663.  
  664.   ins_del_costs (frame,
  665.          ins_line_string, multi_ins_string,
  666.          setup_string, cleanup_string,
  667.          FRAME_INSERT_COST (frame), FRAME_INSERTN_COST (frame),
  668.          coefficient);
  669.   ins_del_costs (frame,
  670.          del_line_string, multi_del_string,
  671.          setup_string, cleanup_string,
  672.          FRAME_DELETEN_COST (frame), FRAME_DELETE_COST (frame),
  673.          coefficient);
  674. }
  675.