(signal_before_change): If inhibit_modification_hooks
[bpt/emacs.git] / src / insdel.c
index 9a3aebf..a7807dd 100644 (file)
@@ -400,6 +400,45 @@ adjust_markers_for_delete (from, from_byte, to, to_byte)
       marker = m->chain;
     }
 }
+
+/* Adjust all markers for a byte combining of NBYTES at char position
+   FROM and byte position FROM_BYTE.  */
+
+static void
+adjust_markers_for_combining (from, from_byte, nbytes)
+     register int from, from_byte, nbytes;
+{
+  Lisp_Object marker;
+  register struct Lisp_Marker *m;
+  register int bytepos;
+  register int to_byte = from_byte + nbytes;
+
+  marker = BUF_MARKERS (current_buffer);
+
+  while (!NILP (marker))
+    {
+      m = XMARKER (marker);
+      bytepos = m->bytepos;
+
+      if (bytepos >= to_byte)
+       {
+         record_marker_adjustment (marker,  - nbytes);
+         m->charpos -= nbytes;
+       }
+      else if (bytepos > from_byte)
+       {
+         record_marker_adjustment (marker,  from - m->charpos);
+         m->charpos = from;
+         m->bytepos = to_byte;
+       }
+      else if (bytepos == from_byte)
+       {
+         m->bytepos = to_byte;
+       }
+
+      marker = m->chain;
+    }
+}
 \f
 /* Adjust all markers for calling record_delete for combining bytes.
    whose range in bytes is FROM_BYTE to TO_BYTE.
@@ -595,14 +634,6 @@ adjust_markers_for_replace (from, from_byte, old_chars, old_bytes,
          m->charpos = from;
          m->bytepos = from_byte;
        }
-      else if (m->bytepos == from_byte)
-       {
-         if (combined_before_bytes)
-           {
-             DEC_BOTH (m->charpos, m->bytepos);
-             INC_BOTH (m->charpos, m->bytepos);
-           }
-       }
 
       marker = m->chain;
     }
@@ -1013,7 +1044,7 @@ combine_bytes (pos, pos_byte, nbytes)
      int pos, pos_byte, nbytes;
 {
   /* Adjust all markers.  */
-  adjust_markers_for_delete (pos, pos_byte, pos + nbytes, pos_byte);
+  adjust_markers_for_combining (pos, pos_byte, nbytes);
 
   adjust_overlays_for_delete (pos, nbytes);
 
@@ -1576,7 +1607,7 @@ adjust_after_replace (from, from_byte, prev_text, len, len_byte)
                                        from_byte + combined_after_bytes);
 
       if (! EQ (current_buffer->undo_list, Qt))
-       record_delete (from, deletion);
+       record_delete (from + len, deletion);
     }
 
   if (combined_before_bytes)
@@ -1625,10 +1656,6 @@ adjust_after_replace (from, from_byte, prev_text, len, len_byte)
   if (BUF_INTERVALS (current_buffer) != 0)
     {
       offset_intervals (current_buffer, from, len - nchars_del);
-      if (len - nchars_del > 0)
-       Fset_text_properties (make_number (from),
-                             make_number (from + len - nchars_del),
-                             Qnil, Qnil);
     }
 #endif
 
@@ -1636,13 +1663,16 @@ adjust_after_replace (from, from_byte, prev_text, len, len_byte)
     int pos = PT, pos_byte = PT_BYTE;
 
     if (from < PT)
-      adjust_point (len - nchars_del + combined_after_bytes,
-                   len_byte - nbytes_del + combined_after_bytes);
-    else if (from == PT && combined_before_bytes)
-      adjust_point (0, combined_before_bytes);
+      adjust_point (len - nchars_del, len_byte - nbytes_del);
 
     if (combined_after_bytes)
-      combine_bytes (from + len, from_byte + len_byte, combined_after_bytes);
+      {
+       if (combined_before_bytes)
+         combined_before_bytes += combined_after_bytes;
+       else
+         combine_bytes (from + len, from_byte + len_byte,
+                        combined_after_bytes);
+      }
 
     if (combined_before_bytes)
       combine_bytes (from, from_byte, combined_before_bytes);
@@ -1702,6 +1732,7 @@ replace_range (from, to, new, prepare, inherit, markers)
   int adjusted_inschars;
   INTERVAL intervals;
   int outgoing_insbytes = insbytes;
+  Lisp_Object deletion;
 
   CHECK_MARKERS ();
 
@@ -1753,23 +1784,17 @@ replace_range (from, to, new, prepare, inherit, markers)
   if (to < GPT)
     gap_left (to, to_byte, 0);
 
-  {
-    Lisp_Object deletion;
-    deletion = Qnil;
-
-    if (! EQ (current_buffer->undo_list, Qt))
-      deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
+  deletion = Qnil;
 
-    if (markers)
-      /* Relocate all markers pointing into the new, larger gap
-        to point at the end of the text before the gap.
-        Do this before recording the deletion,
-        so that undo handles this after reinserting the text.  */
-      adjust_markers_for_delete (from, from_byte, to, to_byte);
+  if (! EQ (current_buffer->undo_list, Qt))
+    deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
 
-    if (! EQ (current_buffer->undo_list, Qt))
-      record_delete (from, deletion);
-  }
+  if (markers)
+    /* Relocate all markers pointing into the new, larger gap
+       to point at the end of the text before the gap.
+       Do this before recording the deletion,
+       so that undo handles this after reinserting the text.  */
+    adjust_markers_for_delete (from, from_byte, to, to_byte);
 
   GAP_SIZE += nbytes_del;
   ZV -= nchars_del;
@@ -1829,7 +1854,7 @@ replace_range (from, to, new, prepare, inherit, markers)
                                        from + combined_after_bytes,
                                        from_byte + combined_after_bytes);
       if (! EQ (current_buffer->undo_list, Qt))
-       record_delete (from, deletion);
+       record_delete (from + inschars, deletion);
     }
 
   if (combined_before_bytes)
@@ -1846,8 +1871,13 @@ replace_range (from, to, new, prepare, inherit, markers)
        record_delete (from - 1, deletion);
     }
 
-  record_insert (from - !!combined_before_bytes,
-                inschars - combined_before_bytes + !!combined_before_bytes);
+  if (! EQ (current_buffer->undo_list, Qt))
+    {
+      record_delete (from - !!combined_before_bytes, deletion);
+      record_insert (from - !!combined_before_bytes,
+                    (inschars - combined_before_bytes
+                     + !!combined_before_bytes));
+    }
 
   GAP_SIZE -= outgoing_insbytes;
   GPT += inschars;
@@ -1887,16 +1917,18 @@ replace_range (from, to, new, prepare, inherit, markers)
 
   /* Relocate point as if it were a marker.  */
   if (from < PT)
-    adjust_point ((from + inschars - (PT < to ? PT : to)
-                  + combined_after_bytes),
+    adjust_point ((from + inschars - (PT < to ? PT : to)),
                  (from_byte + outgoing_insbytes
-                  - (PT_BYTE < to_byte ? PT_BYTE : to_byte)
-                  + combined_after_bytes));
+                  - (PT_BYTE < to_byte ? PT_BYTE : to_byte)));
 
   if (combined_after_bytes)
-    combine_bytes (from + inschars, from_byte + outgoing_insbytes,
-                  combined_after_bytes);
-
+    {
+      if (combined_before_bytes)
+       combined_before_bytes += combined_after_bytes;
+      else
+       combine_bytes (from + inschars, from_byte + outgoing_insbytes,
+                      combined_after_bytes);
+    }
   if (combined_before_bytes)
     combine_bytes (from, from_byte, combined_before_bytes);
 
@@ -2288,6 +2320,9 @@ signal_before_change (start_int, end_int, preserve_ptr)
   Lisp_Object preserve_marker;
   struct gcpro gcpro1, gcpro2, gcpro3;
 
+  if (inhibit_modification_hooks)
+    return;
+
   start = make_number (start_int);
   end = make_number (end_int);
   preserve_marker = Qnil;
@@ -2374,6 +2409,9 @@ void
 signal_after_change (charpos, lendel, lenins)
      int charpos, lendel, lenins;
 {
+  if (inhibit_modification_hooks)
+    return;
+
   /* If we are deferring calls to the after-change functions
      and there are no before-change functions,
      just record the args that we were going to use.  */