changeset 7862:0b6f46029ea2

(transpose_markers): Allow for gap at start of region. (Ftranspose_regions): Don't precompute pointers into buffer text.
author Karl Heuer <kwzh@gnu.org>
date Sun, 12 Jun 1994 19:58:00 +0000
parents 8996abee8718
children c4adb7401604
files src/editfns.c
diffstat 1 files changed, 38 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/src/editfns.c	Sun Jun 12 15:58:51 1994 +0000
+++ b/src/editfns.c	Sun Jun 12 19:58:00 1994 +0000
@@ -1626,11 +1626,7 @@
    appropriate amount to some, subtracting from some, and leaving the
    rest untouched.  Most of this is copied from adjust_markers in insdel.c.
   
-   It's caller's job to see that (start1 <= end1 <= start2 <= end2),
-   and that the buffer gap will not conflict with the markers.  This
-   last requirement is odd and maybe should be taken out, but it works
-   for now because Ftranspose_regions does in fact guarantee that, in
-   addition to providing universal health-care coverage.  */
+   It's the caller's job to see that (start1 <= end1 <= start2 <= end2).  */
 
 void
 transpose_markers (start1, end1, start2, end2)
@@ -1638,10 +1634,8 @@
 {
   register int amt1, amt2, diff, mpos;
   register Lisp_Object marker;
-  register struct Lisp_Marker *m;
 
-  /* Update point as if it were a marker.
-     Do this before adjusting the start/end values for the gap.  */
+  /* Update point as if it were a marker.  */
   if (PT < start1)
     ;
   else if (PT < end1)
@@ -1651,25 +1645,13 @@
   else if (PT < end2)
     TEMP_SET_PT (PT - (start2 - start1));
 
-  /* Internally, marker positions take the gap into account, so if the
-   * gap is before one or both of the regions, the region's limits
-   * must be adjusted to compensate.  The caller guaranteed that the
-   * gap is not inside any of the regions, however, so this is fairly
-   * simple.
-   */
-  if (GPT < start1)
-    {
-      register int gs = GAP_SIZE;
-      start1 += gs; end1 += gs;
-      start2 += gs; end2 += gs;
-    }
-  else if (GPT < start2)
-    {
-      /* If the regions are of equal size, the gap could, in theory,
-       * be somewhere between them. */
-      register int gs = GAP_SIZE;
-      start2 += gs; end2 += gs;
-    }
+  /* We used to adjust the endpoints here to account for the gap, but that
+     isn't good enough.  Even if we assume the caller has tried to move the
+     gap out of our way, it might still be at start1 exactly, for example;
+     and that places it `inside' the interval, for our purposes.  The amount
+     of adjustment is nontrivial if there's a `denormalized' marker whose
+     position is between GPT and GPT + GAP_SIZE, so it's simpler to leave
+     the dirty work to Fmarker_position, below.  */
 
   /* The difference between the region's lengths */
   diff = (end2 - start2) - (end1 - start1);
@@ -1680,25 +1662,21 @@
   amt1 = (end2 - start2) + (start2 - end1);
   amt2 = (end1 - start1) + (start2 - end1);
 
-  marker = current_buffer->markers;
-
-  while (!NILP (marker))
+  for (marker = current_buffer->markers; !NILP (marker);
+       marker = XMARKER (marker)->chain)
     {
-      m = XMARKER (marker);
-      mpos = m->bufpos;
-      if (mpos >= start1 && mpos < end1)       /* in region 1 */
-        {
-          m->bufpos += amt1;
-        }
-      else if (mpos >= start2 && mpos < end2)  /* in region 2 */
-        {
-          m->bufpos -= amt2;
-        }
-      else if (mpos >= end1 && mpos < start2)  /* between the regions */
-        {
-          m->bufpos += diff;
-        }
-      marker = m->chain;
+      mpos = Fmarker_position (marker);
+      if (mpos >= start1 && mpos < end2)
+	{
+	  if (mpos < end1)
+	    mpos += amt1;
+	  else if (mpos < start2)
+	    mpos += diff;
+	  else
+	    mpos -= amt2;
+	  if (mpos > GPT) mpos += GAP_SIZE;
+	  XMARKER (marker)->bufpos = mpos;
+	}
     }
 }
 
@@ -1782,9 +1760,6 @@
       else
 	move_gap (end2);
     }
-
-  start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1);
-  start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2);
       
   /* Hmmm... how about checking to see if the gap is large
      enough to use as the temporary storage?  That would avoid an
@@ -1813,6 +1788,13 @@
 	    temp = (unsigned char *) xmalloc (len2);
 	  else
 	    temp = (unsigned char *) alloca (len2);
+
+	  /* Don't precompute these addresses.  We have to compute them
+	     at the last minute, because the relocating allocator might
+	     have moved the buffer around during the xmalloc.  */
+	  start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1);
+	  start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2);
+
           bcopy (start2_addr, temp, len2);
           bcopy (start1_addr, start1_addr + len2, len1);
           bcopy (temp, start1_addr, len2);
@@ -1826,6 +1808,8 @@
 	    temp = (unsigned char *) xmalloc (len1);
 	  else
 	    temp = (unsigned char *) alloca (len1);
+	  start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1);
+	  start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2);
           bcopy (start1_addr, temp, len1);
           bcopy (start2_addr, start1_addr, len2);
           bcopy (temp, start1_addr + len2, len1);
@@ -1860,6 +1844,8 @@
 	    temp = (unsigned char *) xmalloc (len1);
 	  else
 	    temp = (unsigned char *) alloca (len1);
+	  start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1);
+	  start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2);
           bcopy (start1_addr, temp, len1);
           bcopy (start2_addr, start1_addr, len2);
           bcopy (temp, start2_addr, len1);
@@ -1891,6 +1877,8 @@
 	    temp = (unsigned char *) xmalloc (len2);
 	  else
 	    temp = (unsigned char *) alloca (len2);
+	  start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1);
+	  start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2);
           bcopy (start2_addr, temp, len2);
           bcopy (start1_addr, start1_addr + len_mid + len2, len1);
           safe_bcopy (start1_addr + len1, start1_addr + len2, len_mid);
@@ -1925,6 +1913,8 @@
 	    temp = (unsigned char *) xmalloc (len1);
 	  else
 	    temp = (unsigned char *) alloca (len1);
+	  start1_addr = BUF_CHAR_ADDRESS (current_buffer, start1);
+	  start2_addr = BUF_CHAR_ADDRESS (current_buffer, start2);
           bcopy (start1_addr, temp, len1);
           bcopy (start2_addr, start1_addr, len2);
           bcopy (start1_addr + len1, start1_addr + len2, len_mid);