(signal_before_change): If inhibit_modification_hooks
[bpt/emacs.git] / src / insdel.c
index 813e98c..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;
     }
@@ -699,13 +730,26 @@ copy_text (from_addr, to_addr, nbytes,
     {
       int nchars = 0;
       int bytes_left = nbytes;
+      Lisp_Object tbl = Qnil, temp;
+
+      /* We set the variable tbl to the reverse table of
+         Vnonascii_translation_table in advance.  */
+      if (CHAR_TABLE_P (Vnonascii_translation_table))
+       {
+         tbl = Fchar_table_extra_slot (Vnonascii_translation_table,
+                                       make_number (0));
+         if (!CHAR_TABLE_P (tbl))
+           tbl = Qnil;
+       }
 
       /* Convert multibyte to single byte.  */
       while (bytes_left > 0)
        {
-         int thislen, c;
-         c = STRING_CHAR_AND_LENGTH (from_addr, bytes_left, thislen);
-         *to_addr++ = SINGLE_BYTE_CHAR_P (c) ? c : (c & 0177) + 0200;
+         int thislen, c, c_save;
+         c = c_save = STRING_CHAR_AND_LENGTH (from_addr, bytes_left, thislen);
+         if (!SINGLE_BYTE_CHAR_P (c))
+           c = multibyte_char_to_unibyte (c, tbl);
+         *to_addr++ = c;
          from_addr += thislen;
          bytes_left -= thislen;
          nchars++;
@@ -723,7 +767,7 @@ copy_text (from_addr, to_addr, nbytes,
          unsigned char workbuf[4], *str;
          int len;
 
-         if (c >= 0240 && c < 0400)
+         if ((c >= 0240 || !NILP (Vnonascii_translation_table)) && c < 0400)
            {
              c = unibyte_char_to_multibyte (c);
              len = CHAR_STRING (c, workbuf, str);
@@ -755,12 +799,12 @@ count_size_as_multibyte (ptr, nbytes)
     {
       unsigned int c = *ptr++;
 
-      if (c < 0240)
+      if (c < 0240 && NILP (Vnonascii_translation_table))
        outgoing_nbytes++;
       else
        {
          c = unibyte_char_to_multibyte (c);
-         outgoing_nbytes += XINT (Fchar_bytes (make_number (c)));
+         outgoing_nbytes += CHAR_BYTES (c);
        }
     }
 
@@ -937,11 +981,23 @@ count_combining_after (string, length, pos, pos_byte)
   if (length == 0 || ASCII_BYTE_P (string[length - 1]))
     return 0;
   i = length - 1;
-  while (i > 0 && ! CHAR_HEAD_P (string[i]))
+  while (i >= 0 && ! CHAR_HEAD_P (string[i]))
     {
       i--;
     }
-  if (! BASE_LEADING_CODE_P (string[i]))
+  if (i < 0)
+    {
+      /* All characters in `string' are not character head.
+        We must check also preceding bytes at POS.
+        We are sure that the gap is at POS.  */
+      string = BEG_ADDR;
+      i = pos_byte - 2;
+      while (i >= 0 && ! CHAR_HEAD_P (string[i]))
+       i--;
+      if (i < 0 || !BASE_LEADING_CODE_P (string[i]))
+       return 0;
+    }
+  else if (!BASE_LEADING_CODE_P (string[i]))
     return 0;
 
   if (pos == ZV)
@@ -988,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);
 
@@ -1014,20 +1070,23 @@ insert_1_both (string, nchars, nbytes, inherit, prepare, before_markers)
      register int nchars, nbytes;
      int inherit, prepare, before_markers;
 {
-  register Lisp_Object temp, deletion;
+  register Lisp_Object temp;
   int combined_before_bytes, combined_after_bytes;
 
   if (NILP (current_buffer->enable_multibyte_characters))
     nchars = nbytes;
 
+  if (prepare)
+    /* Do this before moving and increasing the gap,
+       because the before-change hooks might move the gap
+       or make it smaller.  */
+    prepare_to_modify_buffer (PT, PT, NULL);
+
   if (PT != GPT)
     move_gap_both (PT, PT_BYTE);
   if (GAP_SIZE < nbytes)
     make_gap (nbytes - GAP_SIZE);
 
-  if (prepare)
-    prepare_to_modify_buffer (PT, PT, NULL);
-
   combined_before_bytes
     = count_combining_before (string, nbytes, PT, PT_BYTE);
   combined_after_bytes
@@ -1042,23 +1101,33 @@ insert_1_both (string, nchars, nbytes, inherit, prepare, before_markers)
 
   if (combined_after_bytes)
     {
-      deletion = make_buffer_string_both (PT, PT_BYTE,
-                                         PT + combined_after_bytes,
-                                         PT_BYTE + combined_after_bytes, 1);
+      Lisp_Object deletion;
+      deletion = Qnil;
+
+      if (! EQ (current_buffer->undo_list, Qt))
+       deletion = make_buffer_string_both (PT, PT_BYTE,
+                                           PT + combined_after_bytes,
+                                           PT_BYTE + combined_after_bytes, 1);
 
       adjust_markers_for_record_delete (PT, PT_BYTE,
                                        PT + combined_after_bytes,
                                        PT_BYTE + combined_after_bytes);
-      record_delete (PT, deletion);
+      if (! EQ (current_buffer->undo_list, Qt))
+       record_delete (PT, deletion);
     }
 
   if (combined_before_bytes)
     {
-      deletion = make_buffer_string_both (PT - 1, CHAR_TO_BYTE (PT - 1),
-                                         PT, PT_BYTE, 1);
+      Lisp_Object deletion;
+      deletion = Qnil;
+
+      if (! EQ (current_buffer->undo_list, Qt))
+       deletion = make_buffer_string_both (PT - 1, CHAR_TO_BYTE (PT - 1),
+                                           PT, PT_BYTE, 1);
       adjust_markers_for_record_delete (PT - 1, CHAR_TO_BYTE (PT - 1),
                                        PT, PT_BYTE);
-      record_delete (PT - 1, deletion);
+      if (! EQ (current_buffer->undo_list, Qt))
+       record_delete (PT - 1, deletion);
     }
 
   record_insert (PT - !!combined_before_bytes,
@@ -1167,7 +1236,6 @@ insert_from_string_1 (string, pos, pos_byte, nchars, nbytes,
   int combined_before_bytes, combined_after_bytes;
   int adjusted_nchars;
   INTERVAL intervals;
-  Lisp_Object deletion;
 
   /* Make OUTGOING_NBYTES describe the text
      as it will be inserted in this buffer.  */
@@ -1179,12 +1247,10 @@ insert_from_string_1 (string, pos, pos_byte, nchars, nbytes,
       = count_size_as_multibyte (&XSTRING (string)->data[pos_byte],
                                 nbytes);
 
-  /* Make sure point-max won't overflow after this insertion.  */
-  XSETINT (temp, outgoing_nbytes + Z);
-  if (outgoing_nbytes + Z != XINT (temp))
-    error ("Maximum buffer size exceeded");
-
   GCPRO1 (string);
+  /* Do this before moving and increasing the gap,
+     because the before-change hooks might move the gap
+     or make it smaller.  */
   prepare_to_modify_buffer (PT, PT, NULL);
 
   if (PT != GPT)
@@ -1219,23 +1285,33 @@ insert_from_string_1 (string, pos, pos_byte, nchars, nbytes,
 
   if (combined_after_bytes)
     {
-      deletion = make_buffer_string_both (PT, PT_BYTE,
-                                         PT + combined_after_bytes,
-                                         PT_BYTE + combined_after_bytes, 1);
+      Lisp_Object deletion;
+      deletion = Qnil;
+
+      if (! EQ (current_buffer->undo_list, Qt))
+       deletion = make_buffer_string_both (PT, PT_BYTE,
+                                           PT + combined_after_bytes,
+                                           PT_BYTE + combined_after_bytes, 1);
 
       adjust_markers_for_record_delete (PT, PT_BYTE,
                                        PT + combined_after_bytes,
                                        PT_BYTE + combined_after_bytes);
-      record_delete (PT, deletion);
+      if (! EQ (current_buffer->undo_list, Qt))
+       record_delete (PT, deletion);
     }
 
   if (combined_before_bytes)
     {
-      deletion = make_buffer_string_both (PT - 1, CHAR_TO_BYTE (PT - 1),
-                                         PT, PT_BYTE, 1);
+      Lisp_Object deletion;
+      deletion = Qnil;
+
+      if (! EQ (current_buffer->undo_list, Qt))
+       deletion = make_buffer_string_both (PT - 1, CHAR_TO_BYTE (PT - 1),
+                                           PT, PT_BYTE, 1);
       adjust_markers_for_record_delete (PT - 1, CHAR_TO_BYTE (PT - 1),
                                        PT, PT_BYTE);
-      record_delete (PT - 1, deletion);
+      if (! EQ (current_buffer->undo_list, Qt))
+       record_delete (PT - 1, deletion);
     }
 
   record_insert (PT - !!combined_before_bytes,
@@ -1317,7 +1393,7 @@ insert_from_buffer_1 (buf, from, nchars, inherit)
      int from, nchars;
      int inherit;
 {
-  register Lisp_Object temp, deletion;
+  register Lisp_Object temp;
   int chunk;
   int from_byte = buf_charpos_to_bytepos (buf, from);
   int to_byte = buf_charpos_to_bytepos (buf, from + nchars);
@@ -1342,6 +1418,9 @@ insert_from_buffer_1 (buf, from, nchars, inherit)
   if (outgoing_nbytes + Z != XINT (temp))
     error ("Maximum buffer size exceeded");
 
+  /* Do this before moving and increasing the gap,
+     because the before-change hooks might move the gap
+     or make it smaller.  */
   prepare_to_modify_buffer (PT, PT, NULL);
 
   if (PT != GPT)
@@ -1387,23 +1466,33 @@ insert_from_buffer_1 (buf, from, nchars, inherit)
 
   if (combined_after_bytes)
     {
-      deletion = make_buffer_string_both (PT, PT_BYTE,
-                                         PT + combined_after_bytes,
-                                         PT_BYTE + combined_after_bytes, 1);
+      Lisp_Object deletion;
+      deletion = Qnil;
+
+      if (! EQ (current_buffer->undo_list, Qt))
+       deletion = make_buffer_string_both (PT, PT_BYTE,
+                                           PT + combined_after_bytes,
+                                           PT_BYTE + combined_after_bytes, 1);
 
       adjust_markers_for_record_delete (PT, PT_BYTE,
                                        PT + combined_after_bytes,
                                        PT_BYTE + combined_after_bytes);
-      record_delete (PT, deletion);
+      if (! EQ (current_buffer->undo_list, Qt))
+       record_delete (PT, deletion);
     }
 
   if (combined_before_bytes)
     {
-      deletion = make_buffer_string_both (PT - 1, CHAR_TO_BYTE (PT - 1),
-                                         PT, PT_BYTE, 1);
+      Lisp_Object deletion;
+      deletion = Qnil;
+
+      if (! EQ (current_buffer->undo_list, Qt))
+       deletion = make_buffer_string_both (PT - 1, CHAR_TO_BYTE (PT - 1),
+                                           PT, PT_BYTE, 1);
       adjust_markers_for_record_delete (PT - 1, CHAR_TO_BYTE (PT - 1),
                                        PT, PT_BYTE);
-      record_delete (PT - 1, deletion);
+      if (! EQ (current_buffer->undo_list, Qt))
+       record_delete (PT - 1, deletion);
     }
 
   record_insert (PT - !!combined_before_bytes,
@@ -1470,12 +1559,17 @@ adjust_before_replace (from, from_byte, to, to_byte)
      int from, from_byte, to, to_byte;
 {
   Lisp_Object deletion;
-  deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
+
+  if (! EQ (current_buffer->undo_list, Qt))
+    deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
 
   CHECK_MARKERS ();
 
   adjust_markers_for_delete (from, from_byte, to, to_byte);
-  record_delete (from, deletion);
+
+  if (! EQ (current_buffer->undo_list, Qt))
+    record_delete (from, deletion);
+
   adjust_overlays_for_delete (from, to - from);
 }
 
@@ -1495,28 +1589,39 @@ adjust_after_replace (from, from_byte, prev_text, len, len_byte)
     = count_combining_before (GPT_ADDR, len_byte, from, from_byte);
   int combined_after_bytes
     = count_combining_after (GPT_ADDR, len_byte, from, from_byte);
-  Lisp_Object deletion;
   int nchars_del = 0, nbytes_del = 0;
 
   if (combined_after_bytes)
     {
-      deletion = make_buffer_string_both (from, from_byte,
-                                         from + combined_after_bytes,
-                                         from_byte + combined_after_bytes, 1);
+      Lisp_Object deletion;
+      deletion = Qnil;
+
+      if (! EQ (current_buffer->undo_list, Qt))
+       deletion = make_buffer_string_both (from, from_byte,
+                                           from + combined_after_bytes,
+                                           from_byte + combined_after_bytes,
+                                           1);
 
       adjust_markers_for_record_delete (from, from_byte,
                                        from + combined_after_bytes,
                                        from_byte + combined_after_bytes);
-      record_delete (from, deletion);
+
+      if (! EQ (current_buffer->undo_list, Qt))
+       record_delete (from + len, deletion);
     }
 
   if (combined_before_bytes)
     {
-      deletion = make_buffer_string_both (from - 1, CHAR_TO_BYTE (from - 1),
-                                         from, from_byte, 1);
+      Lisp_Object deletion;
+      deletion = Qnil;
+
+      if (! EQ (current_buffer->undo_list, Qt))
+       deletion = make_buffer_string_both (from - 1, CHAR_TO_BYTE (from - 1),
+                                           from, from_byte, 1);
       adjust_markers_for_record_delete (from - 1, CHAR_TO_BYTE (from - 1),
                                        from, from_byte);
-      record_delete (from - 1, deletion);
+      if (! EQ (current_buffer->undo_list, Qt))
+       record_delete (from - 1, deletion);
     }
 
   /* Update various buffer positions for the new text.  */
@@ -1539,7 +1644,7 @@ adjust_after_replace (from, from_byte, prev_text, len, len_byte)
                              len, len_byte,
                              combined_before_bytes, combined_after_bytes);
   if (STRINGP (prev_text))
-    record_delete (from, prev_text);
+    record_delete (from - !!combined_before_bytes, prev_text);
   record_insert (from - !!combined_before_bytes,
                 len - combined_before_bytes + !!combined_before_bytes);
 
@@ -1549,20 +1654,25 @@ adjust_after_replace (from, from_byte, prev_text, len, len_byte)
     adjust_overlays_for_delete (from, nchars_del - len);
 #ifdef USE_TEXT_PROPERTIES
   if (BUF_INTERVALS (current_buffer) != 0)
-    offset_intervals (current_buffer, from, len - nchars_del);
+    {
+      offset_intervals (current_buffer, from, len - nchars_del);
+    }
 #endif
 
   {
     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);
@@ -1603,12 +1713,14 @@ adjust_after_insert (from, from_byte, to, to_byte, newlen)
 /* Note that this does not yet handle markers quite right.
    Also it needs to record a single undo-entry that does a replacement
    rather than a separate delete and insert.
-   That way, undo will also handle markers properly.  */
+   That way, undo will also handle markers properly.
+
+   But if MARKERS is 0, don't relocate markers.  */
 
 void
-replace_range (from, to, new, prepare, inherit, nomarkers)
+replace_range (from, to, new, prepare, inherit, markers)
      Lisp_Object new;
-     int from, to, prepare, inherit, nomarkers;
+     int from, to, prepare, inherit, markers;
 {
   int inschars = XSTRING (new)->size;
   int insbytes = STRING_BYTES (XSTRING (new));
@@ -1672,17 +1784,18 @@ replace_range (from, to, new, prepare, inherit, nomarkers)
   if (to < GPT)
     gap_left (to, to_byte, 0);
 
-  deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
+  deletion = Qnil;
 
-  if (nomarkers)
+  if (! EQ (current_buffer->undo_list, Qt))
+    deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
+
+  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);
 
-  record_delete (from, deletion);
-
   GAP_SIZE += nbytes_del;
   ZV -= nchars_del;
   Z -= nchars_del;
@@ -1709,16 +1822,16 @@ replace_range (from, to, new, prepare, inherit, nomarkers)
             STRING_MULTIBYTE (new),
             ! NILP (current_buffer->enable_multibyte_characters));
 
-  /* We have copied text into the gap, but we have not altered
-     PT or PT_BYTE yet.  So we can pass PT and PT_BYTE
-     to these functions and get the same results as we would
-     have got earlier on.  Meanwhile, GPT_ADDR does point to
+  /* We have copied text into the gap, but we have not marked
+     it as part of the buffer.  So we can use the old FROM and FROM_BYTE
+     here, for both the previous text and the following text.
+     Meanwhile, GPT_ADDR does point to
      the text that has been stored by copy_text.  */
 
   combined_before_bytes
-    = count_combining_before (GPT_ADDR, outgoing_insbytes, PT, PT_BYTE);
+    = count_combining_before (GPT_ADDR, outgoing_insbytes, from, from_byte);
   combined_after_bytes
-    = count_combining_after (GPT_ADDR, outgoing_insbytes, PT, PT_BYTE);
+    = count_combining_after (GPT_ADDR, outgoing_insbytes, from, from_byte);
 
   /* Record deletion of the surrounding text that combines with
      the insertion.  This, together with recording the insertion,
@@ -1729,27 +1842,42 @@ replace_range (from, to, new, prepare, inherit, nomarkers)
 
   if (combined_after_bytes)
     {
-      deletion = make_buffer_string_both (PT, PT_BYTE,
-                                         PT + combined_after_bytes,
-                                         PT_BYTE + combined_after_bytes, 1);
+      Lisp_Object deletion;
+      deletion = Qnil;
 
-      adjust_markers_for_record_delete (PT, PT_BYTE,
-                                       PT + combined_after_bytes,
-                                       PT_BYTE + combined_after_bytes);
-      record_delete (PT, deletion);
+      if (! EQ (current_buffer->undo_list, Qt))
+       deletion = make_buffer_string_both (from, from_byte,
+                                           from + combined_after_bytes,
+                                           from_byte + combined_after_bytes, 1);
+
+      adjust_markers_for_record_delete (from, from_byte,
+                                       from + combined_after_bytes,
+                                       from_byte + combined_after_bytes);
+      if (! EQ (current_buffer->undo_list, Qt))
+       record_delete (from + inschars, deletion);
     }
 
   if (combined_before_bytes)
     {
-      deletion = make_buffer_string_both (PT - 1, CHAR_TO_BYTE (PT - 1),
-                                         PT, PT_BYTE, 1);
-      adjust_markers_for_record_delete (PT - 1, CHAR_TO_BYTE (PT - 1),
-                                       PT, PT_BYTE);
-      record_delete (PT - 1, deletion);
+      Lisp_Object deletion;
+      deletion = Qnil;
+
+      if (! EQ (current_buffer->undo_list, Qt))
+       deletion = make_buffer_string_both (from - 1, CHAR_TO_BYTE (from - 1),
+                                           from, from_byte, 1);
+      adjust_markers_for_record_delete (from - 1, CHAR_TO_BYTE (from - 1),
+                                       from, from_byte);
+      if (! EQ (current_buffer->undo_list, Qt))
+       record_delete (from - 1, deletion);
     }
 
-  record_insert (PT - !!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;
@@ -1771,13 +1899,13 @@ replace_range (from, to, new, prepare, inherit, nomarkers)
      adjusting the markers that bound the overlays.  */
   adjust_overlays_for_delete (from, nchars_del);
   adjust_overlays_for_insert (from, inschars);
-  if (nomarkers)
+  if (markers)
     adjust_markers_for_insert (from, from_byte,
                               from + inschars, from_byte + outgoing_insbytes,
                               combined_before_bytes, combined_after_bytes, 0);
 
 #ifdef USE_TEXT_PROPERTIES
-  offset_intervals (current_buffer, PT, inschars - nchars_del);
+  offset_intervals (current_buffer, from, inschars - nchars_del);
 
   /* Get the intervals for the part of the string we are inserting--
      not including the combined-before bytes.  */
@@ -1789,16 +1917,18 @@ replace_range (from, to, new, prepare, inherit, nomarkers)
 
   /* 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);
 
@@ -1810,7 +1940,7 @@ replace_range (from, to, new, prepare, inherit, nomarkers)
   MODIFF++;
   UNGCPRO;
 
-  signal_after_change (from, nchars_del, PT - from);
+  signal_after_change (from, nchars_del, GPT - from);
 }
 \f
 /* Delete characters in current buffer
@@ -1962,11 +2092,12 @@ del_range_2 (from, from_byte, to, to_byte)
   else
     from_byte_1 = from_byte;
 
-  deletion
-    = make_buffer_string_both (from - !!combined_after_bytes,
-                              from_byte_1,
-                              to + combined_after_bytes,
-                              to_byte + combined_after_bytes, 1);
+  if (! EQ (current_buffer->undo_list, Qt))
+    deletion
+      = make_buffer_string_both (from - !!combined_after_bytes,
+                                from_byte_1,
+                                to + combined_after_bytes,
+                                to_byte + combined_after_bytes, 1);
   if (combined_after_bytes)
     /* COMBINED_AFTER_BYTES nonzero means that the above code moved
        the gap.  We must move the gap again to a proper place.  */
@@ -1993,7 +2124,8 @@ del_range_2 (from, from_byte, to, to_byte)
       adjust_markers_for_record_delete (from - 1, from_byte_1,
                                        from, from_byte);
     }
-  record_delete (from - !!combined_after_bytes, deletion);
+  if (! EQ (current_buffer->undo_list, Qt))
+    record_delete (from - !!combined_after_bytes, deletion);
   MODIFF++;
 
   /* Relocate point as if it were a marker.  */
@@ -2188,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;
@@ -2274,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.  */