home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / src / undo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-13  |  7.9 KB  |  300 lines

  1. /* undo handling for GNU Emacs.
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the GNU Emacs General Public
  11. License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. GNU Emacs, but only under the conditions described in the
  15. GNU Emacs General Public License.   A copy of this license is
  16. supposed to have been given to you along with GNU Emacs so you
  17. can know your rights and responsibilities.  It should be in a
  18. file named COPYING.  Among other things, the copyright notice
  19. and this notice must be preserved on all copies.  */
  20.  
  21.  
  22. #include "config.h"
  23. #include "lisp.h"
  24. #include "buffer.h"
  25.  
  26. /* Extent code needs to know about undo because the behavior of insert()
  27.    with regard to extents varies depending on whether we are inside
  28.    and undo or not. */
  29. int inside_undo;
  30. Lisp_Object Qinside_undo;
  31.  
  32. /* Last buffer for which undo information was recorded.  */
  33. static Lisp_Object last_undo_buffer;
  34.  
  35. /* Record that an unmodified buffer is about to be changed.
  36.    Record the file modification date so that when undoing this entry
  37.    we can tell whether it is obsolete because the file was saved again.  */
  38.  
  39. static void
  40. record_first_change ()
  41. {
  42.   Lisp_Object high, low;
  43.   XFASTINT (high) = (current_buffer->modtime >> 16) & 0xffff;
  44.   XFASTINT (low) = current_buffer->modtime & 0xffff;
  45.   current_buffer->undo_list = Fcons (Fcons (Qt, Fcons (high, low)), current_buffer->undo_list);
  46. }
  47.  
  48. /* Record an insertion that just happened or is about to happen,
  49.    for LENGTH characters at position BEG.
  50.    (It is possible to record an insertion before or after the fact
  51.    because we don't need to record the contents.)  */
  52.  
  53. void
  54. record_insert (beg, length)
  55.      Lisp_Object beg, length;
  56. {
  57.   Lisp_Object lbeg, lend;
  58.  
  59.   if (current_buffer != XBUFFER (last_undo_buffer))
  60.     Fundo_boundary ();
  61.   XSET (last_undo_buffer, Lisp_Buffer, current_buffer);
  62.  
  63.   if (EQ (current_buffer->undo_list, Qt))
  64.     return;
  65.   if (MODIFF <= current_buffer->save_modified)
  66.     record_first_change ();
  67.  
  68.   /* If this is following another insertion and consecutive with it
  69.      in the buffer, combine the two.  */
  70.   if (CONSP (current_buffer->undo_list))
  71.     {
  72.       Lisp_Object elt;
  73.       elt = XCONS (current_buffer->undo_list)->car;
  74.       if (CONSP (elt)
  75.       && FIXNUMP (XCONS (elt)->car)
  76.       && FIXNUMP (XCONS (elt)->cdr)
  77.       && XINT (XCONS (elt)->cdr) == beg)
  78.     {
  79.       XSETINT (XCONS (elt)->cdr, beg + length);
  80.       return;
  81.     }
  82.     }
  83.  
  84.   XFASTINT (lbeg) = beg;
  85.   XFASTINT (lend) = beg + length;
  86.   current_buffer->undo_list = Fcons (Fcons (lbeg, lend), current_buffer->undo_list);
  87. }
  88.  
  89. /* Record that a deletion is about to take place,
  90.    for LENGTH characters at location BEG.  */
  91.  
  92. void
  93. record_delete (beg, length)
  94.      int beg, length;
  95. {
  96.   Lisp_Object sbeg;
  97.  
  98.   if (current_buffer != XBUFFER (last_undo_buffer))
  99.     Fundo_boundary ();
  100.   XSET (last_undo_buffer, Lisp_Buffer, current_buffer);
  101.  
  102.   if (EQ (current_buffer->undo_list, Qt))
  103.     return;
  104.  
  105.   if (MODIFF <= current_buffer->save_modified)
  106.     record_first_change ();
  107.  
  108.   if (point == beg + length)
  109.     XSET (sbeg, Lisp_Int, -beg);
  110.   else
  111.     XFASTINT (sbeg) = beg;
  112.  
  113.   {
  114.     int count = specpdl_ptr - specpdl;
  115.     specbind (Qinside_undo, Qt);
  116.  
  117.     current_buffer->undo_list
  118.       = Fcons (Fcons (make_string_from_buffer (current_buffer, beg, length), sbeg),
  119.                current_buffer->undo_list);
  120.  
  121.     unbind_to (count, Qnil);
  122.   }
  123. }
  124.  
  125. /* Record that a replacement is about to take place,
  126.    for LENGTH characters at location BEG.
  127.    The replacement does not change the number of characters.  */
  128.  
  129. void
  130. record_change (beg, length)
  131.      int beg, length;
  132. {
  133.   record_delete (beg, length);
  134.   record_insert (beg, length);
  135. }
  136.  
  137. DEFUN ("undo-boundary", Fundo_boundary, Sundo_boundary, 0, 0, 0,
  138.   "Mark a boundary between units of undo.\n\
  139. An undo command will stop at this point,\n\
  140. but another undo command will undo to the previous boundary.")
  141.   ()
  142. {
  143.   Lisp_Object tem;
  144.   if (EQ (current_buffer->undo_list, Qt))
  145.     return Qnil;
  146.   tem = Fcar (current_buffer->undo_list);
  147.   if (!NILP (tem))
  148.     current_buffer->undo_list = Fcons (Qnil, current_buffer->undo_list);
  149.   return Qnil;
  150. }
  151.  
  152. /* At garbage collection time, make an undo list shorter at the end,
  153.    returning the truncated list.
  154.    MINSIZE and MAXSIZE are the limits on size allowed, as described below.
  155.    In practice, these are the values of undo-threshold and
  156.    undo-high-threshold.  */
  157.  
  158. Lisp_Object
  159. truncate_undo_list (list, minsize, maxsize)
  160.      Lisp_Object list;
  161.      int minsize, maxsize;
  162. {
  163.   Lisp_Object prev, next, save_prev;
  164.   int size_so_far = 0;
  165.  
  166.   prev = Qnil;
  167.   next = list;
  168.   save_prev = Qnil;
  169.  
  170.   while (CONSP (next))
  171.     {
  172.       Lisp_Object elt;
  173.       elt = XCONS (next)->car;
  174.  
  175.       /* When we get to a boundary, decide whether to truncate
  176.      either before or after it.  The lower threshold, MINSIZE,
  177.      tells us to truncate after it.  If its size pushes past
  178.      the higher threshold MAXSIZE as well, we truncate before it.  */
  179.       if (NILP (elt))
  180.     {
  181.       if (size_so_far > maxsize)
  182.         break;
  183.       save_prev = prev;
  184.       if (size_so_far > minsize)
  185.         break;
  186.     }
  187.  
  188.       /* Add in the space occupied by this element and its chain link.  */
  189.       size_so_far += 8;
  190.       if (CONSP (elt))
  191.     {
  192.       size_so_far += 8;
  193.       if (STRINGP (XCONS (elt)->car))
  194.         size_so_far += 6 + XSTRING (XCONS (elt)->car)->size;
  195.     }
  196.  
  197.       /* Advance to next element.  */
  198.       prev = next;
  199.       next = XCONS (next)->cdr;
  200.     }
  201.  
  202.   /* If we scanned the whole list, it is short enough; don't change it.  */
  203.   if (NILP (next))
  204.     return list;
  205.  
  206.   /* Truncate at the boundary where we decided to truncate.  */
  207.   if (!NILP (save_prev))
  208.     {
  209.       XCONS (save_prev)->cdr = Qnil;
  210.       return list;
  211.     }
  212.   else
  213.     return Qnil;
  214. }
  215.  
  216. DEFUN ("primitive-undo", Fprimitive_undo, Sprimitive_undo, 2, 2, 0,
  217.   "Undo N records from the front of the list LIST.\n\
  218. Return what remains of the list.")
  219.   (count, list)
  220.      Lisp_Object count, list;
  221. {
  222.   int arg = XINT (count);
  223.   int binding_count = specpdl_ptr - specpdl;
  224.   specbind (Qinside_undo, Qt);
  225.  
  226.   while (arg > 0)
  227.     {
  228.       while (1)
  229.     {
  230.       Lisp_Object next, car, cdr;
  231.       next = Fcar (list);
  232.       list = Fcdr (list);
  233.       if (NILP (next))
  234.         break;
  235.       car = Fcar (next);
  236.       cdr = Fcdr (next);
  237.       if (EQ (car, Qt))
  238.         {
  239.           Lisp_Object high, low;
  240.           int mod_time;
  241.           high = Fcar (cdr);
  242.           low = Fcdr (cdr);
  243.           mod_time = (high << 16) + low;
  244.           /* If this records an obsolete save
  245.          (not matching the actual disk file)
  246.          then don't mark unmodified.  */
  247.           if (mod_time != current_buffer->modtime)
  248.         break;
  249. #ifdef CLASH_DETECTION
  250.           Funlock_buffer ();
  251. #endif /* CLASH_DETECTION */
  252.           Fset_buffer_modified_p (Qnil);
  253.         }
  254.       else if (FIXNUMP (car) && FIXNUMP (cdr))
  255.         {
  256.           if (XINT (car) < BEGV
  257.           || XINT (cdr) > ZV)
  258.         error ("Changes to be undone are outside visible portion of buffer");
  259.           Fdelete_region (car, cdr);
  260.           Fgoto_char (car);
  261.         }
  262.       else if (STRINGP (car) && FIXNUMP (cdr))
  263.         {
  264.           Lisp_Object membuf;
  265.           int pos = XINT (cdr);
  266.           membuf = car;
  267.           if (pos < 0)
  268.         {
  269.           if (-pos < BEGV || -pos > ZV)
  270.             error ("Changes to be undone are outside visible portion of buffer");
  271.           SET_PT (-pos);
  272.           Finsert (1, &membuf);
  273.         }
  274.           else
  275.         {
  276.           if (pos < BEGV || pos > ZV)
  277.             error ("Changes to be undone are outside visible portion of buffer");
  278.           SET_PT (pos);
  279.           Finsert (1, &membuf);
  280.           SET_PT (pos);
  281.         }
  282.         }
  283.     }
  284.       arg--;
  285.     }
  286.  
  287.   return unbind_to (binding_count, list);
  288. }
  289.  
  290. void
  291. syms_of_undo ()
  292. {
  293.   DEFVAR_BOOL ("   inside-undo", &inside_undo,
  294.                "internal variable used to control undo");
  295.   inside_undo = 0;
  296.   Qinside_undo = intern ("   inside-undo");
  297.   defsubr (&Sprimitive_undo);
  298.   defsubr (&Sundo_boundary);
  299. }
  300.