]> git.eshelyaron.com Git - emacs.git/commitdiff
(transpose_markers): Allow for gap at start of region.
authorKarl Heuer <kwzh@gnu.org>
Sun, 12 Jun 1994 19:58:00 +0000 (19:58 +0000)
committerKarl Heuer <kwzh@gnu.org>
Sun, 12 Jun 1994 19:58:00 +0000 (19:58 +0000)
(Ftranspose_regions): Don't precompute pointers into buffer text.

src/editfns.c

index 8b0158c322bb9c829c0315aec98ba6be2d8833b4..8a7becbcc33d61c9dda1457dff23cc4e7d460eee 100644 (file)
@@ -1626,11 +1626,7 @@ Case is ignored if `case-fold-search' is non-nil in the current buffer.")
    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 @@ transpose_markers (start1, end1, start2, end2)
 {
   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 @@ transpose_markers (start1, end1, start2, end2)
   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 @@ transpose_markers (start1, end1, start2, end2)
   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 @@ Transposing beyond buffer boundaries is an error.")
       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 @@ Transposing beyond buffer boundaries is an error.")
            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 @@ Transposing beyond buffer boundaries is an error.")
            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 @@ Transposing beyond buffer boundaries is an error.")
            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 @@ Transposing beyond buffer boundaries is an error.")
            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 @@ Transposing beyond buffer boundaries is an error.")
            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);