(adjust_markers_for_insert): Adjust markers by taking
[bpt/emacs.git] / src / insdel.c
index 3b54364..cc02c04 100644 (file)
@@ -400,6 +400,7 @@ adjust_markers_for_delete (from, from_byte, to, to_byte)
       marker = m->chain;
     }
 }
       marker = m->chain;
     }
 }
+
 \f
 /* Adjust all markers for calling record_delete for combining bytes.
    whose range in bytes is FROM_BYTE to TO_BYTE.
 \f
 /* Adjust all markers for calling record_delete for combining bytes.
    whose range in bytes is FROM_BYTE to TO_BYTE.
@@ -477,8 +478,8 @@ adjust_markers_for_insert (from, from_byte, to, to_byte,
        {
          if (m->insertion_type || before_markers)
            {
        {
          if (m->insertion_type || before_markers)
            {
-             m->bytepos += nbytes + combined_after_bytes;
-             m->charpos += nchars + !!combined_after_bytes;
+             m->bytepos = to_byte + combined_after_bytes;
+             m->charpos = to - combined_before_bytes;
              /* Point the marker before the combined character,
                 so that undoing the insertion puts it back where it was.  */
              if (combined_after_bytes)
              /* Point the marker before the combined character,
                 so that undoing the insertion puts it back where it was.  */
              if (combined_after_bytes)
@@ -492,11 +493,7 @@ adjust_markers_for_insert (from, from_byte, to, to_byte,
                 but don't leave it pointing in the middle of a character.
                 Point the marker after the combined character,
                 so that undoing the insertion puts it back where it was.  */
                 but don't leave it pointing in the middle of a character.
                 Point the marker after the combined character,
                 so that undoing the insertion puts it back where it was.  */
-
-             /* Here we depend on the fact that the gap is after
-                all of the combining bytes that we are going to skip over.  */
-             DEC_BOTH (m->charpos, m->bytepos);
-             INC_BOTH (m->charpos, m->bytepos);
+             m->bytepos += combined_before_bytes;
            }
        }
       /* If a marker was pointing into the combining bytes
            }
        }
       /* If a marker was pointing into the combining bytes
@@ -507,7 +504,7 @@ adjust_markers_for_insert (from, from_byte, to, to_byte,
        {
          /* Put it after the combining bytes.  */
          m->bytepos = to_byte + combined_after_bytes;
        {
          /* Put it after the combining bytes.  */
          m->bytepos = to_byte + combined_after_bytes;
-         m->charpos = to + 1;
+         m->charpos = to - combined_before_bytes;
          /* Now move it back before the combined character,
             so that undoing the insertion will put it where it was.  */
          DEC_BOTH (m->charpos, m->bytepos);
          /* Now move it back before the combined character,
             so that undoing the insertion will put it where it was.  */
          DEC_BOTH (m->charpos, m->bytepos);
@@ -515,7 +512,7 @@ adjust_markers_for_insert (from, from_byte, to, to_byte,
       else if (m->bytepos > from_byte)
        {
          m->bytepos += nbytes;
       else if (m->bytepos > from_byte)
        {
          m->bytepos += nbytes;
-         m->charpos += nchars;
+         m->charpos += nchars - combined_after_bytes - combined_before_bytes;
        }
 
       marker = m->chain;
        }
 
       marker = m->chain;
@@ -566,7 +563,8 @@ adjust_markers_for_replace (from, from_byte, old_chars, old_bytes,
 {
   Lisp_Object marker = BUF_MARKERS (current_buffer);
   int prev_to_byte = from_byte + old_bytes;
 {
   Lisp_Object marker = BUF_MARKERS (current_buffer);
   int prev_to_byte = from_byte + old_bytes;
-  int diff_chars = new_chars - old_chars;
+  int diff_chars
+    = (new_chars - combined_before_bytes) - (old_chars + combined_after_bytes);
   int diff_bytes = new_bytes - old_bytes;
 
   while (!NILP (marker))
   int diff_bytes = new_bytes - old_bytes;
 
   while (!NILP (marker))
@@ -578,34 +576,30 @@ adjust_markers_for_replace (from, from_byte, old_chars, old_bytes,
          if (m->bytepos < prev_to_byte + combined_after_bytes)
            {
              /* Put it after the combining bytes.  */
          if (m->bytepos < prev_to_byte + combined_after_bytes)
            {
              /* Put it after the combining bytes.  */
-             m->bytepos = from_byte + new_bytes;
-             m->charpos = from + new_chars;
+             m->bytepos = from_byte + new_bytes + combined_after_bytes;
+             m->charpos = from + new_chars - combined_before_bytes;
            }
          else
            {
              m->charpos += diff_chars;
              m->bytepos += diff_bytes;
            }
            }
          else
            {
              m->charpos += diff_chars;
              m->bytepos += diff_bytes;
            }
-         if (m->charpos == from + new_chars)
-           record_marker_adjustment (marker, - old_chars);
        }
        }
-      else if (m->bytepos > from_byte)
+      else if (m->bytepos >= from_byte)
        {
        {
-         record_marker_adjustment (marker, from - m->charpos);
          m->charpos = from;
          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);
-           }
+         m->bytepos = from_byte + combined_before_bytes;
+         /* If all new bytes are combined in addition to that there
+             are after combining bytes, we must set byte position of
+             the marker after the after combining bytes.  */
+         if (combined_before_bytes == new_bytes)
+           m->bytepos += combined_after_bytes;
        }
 
       marker = m->chain;
     }
        }
 
       marker = m->chain;
     }
+
+  CHECK_MARKERS ();
 }
 
 \f
 }
 
 \f
@@ -699,15 +693,28 @@ copy_text (from_addr, to_addr, nbytes,
     {
       int nchars = 0;
       int bytes_left = 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)
        {
 
       /* 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;
          from_addr += thislen;
-         bytes_left--;
+         bytes_left -= thislen;
          nchars++;
        }
       return nchars;
          nchars++;
        }
       return nchars;
@@ -723,7 +730,7 @@ copy_text (from_addr, to_addr, nbytes,
          unsigned char workbuf[4], *str;
          int len;
 
          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);
            {
              c = unibyte_char_to_multibyte (c);
              len = CHAR_STRING (c, workbuf, str);
@@ -755,12 +762,12 @@ count_size_as_multibyte (ptr, nbytes)
     {
       unsigned int c = *ptr++;
 
     {
       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++;
       else
        {
          c = unibyte_char_to_multibyte (c);
-         outgoing_nbytes += XINT (Fchar_bytes (make_number (c)));
+         outgoing_nbytes += CHAR_BYTES (c);
        }
     }
 
        }
     }
 
@@ -934,14 +941,26 @@ count_combining_after (string, length, pos, pos_byte)
 
   if (NILP (current_buffer->enable_multibyte_characters))
     return 0;
 
   if (NILP (current_buffer->enable_multibyte_characters))
     return 0;
-  if (length == 0 || ASCII_BYTE_P (string[length - 1]))
+  if (length > 0 && ASCII_BYTE_P (string[length - 1]))
     return 0;
   i = length - 1;
     return 0;
   i = length - 1;
-  while (i > 0 && ! CHAR_HEAD_P (string[i]))
+  while (i >= 0 && ! CHAR_HEAD_P (string[i]))
     {
       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)
     return 0;
 
   if (pos == ZV)
@@ -981,15 +1000,17 @@ count_combining_after (string, length, pos, pos_byte)
 /* Combine NBYTES stray trailing-codes, which were formerly separate
    characters, with the preceding character.  These bytes
    are located after position POS / POS_BYTE, and the preceding character
 /* Combine NBYTES stray trailing-codes, which were formerly separate
    characters, with the preceding character.  These bytes
    are located after position POS / POS_BYTE, and the preceding character
-   is located just before that position.  */
+   is located just before that position.
+
+   This function does not adjust markers for byte combining.  That
+   should be done in advance by the functions
+   adjust_markers_for_insert, adjust_markers_for_delete, or
+   adjust_markers_for_replace.  */
 
 static void
 combine_bytes (pos, pos_byte, nbytes)
      int pos, pos_byte, nbytes;
 {
 
 static void
 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_overlays_for_delete (pos, nbytes);
 
   ADJUST_CHAR_POS (BUF_PT (current_buffer), BUF_PT_BYTE (current_buffer));
   adjust_overlays_for_delete (pos, nbytes);
 
   ADJUST_CHAR_POS (BUF_PT (current_buffer), BUF_PT_BYTE (current_buffer));
@@ -1000,8 +1021,6 @@ combine_bytes (pos, pos_byte, nbytes)
   if (BUF_INTERVALS (current_buffer) != 0)
     /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES.  */
     offset_intervals (current_buffer, pos, - nbytes);
   if (BUF_INTERVALS (current_buffer) != 0)
     /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES.  */
     offset_intervals (current_buffer, pos, - nbytes);
-
-  CHECK_MARKERS ();
 }
 \f
 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
 }
 \f
 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
@@ -1014,20 +1033,23 @@ insert_1_both (string, nchars, nbytes, inherit, prepare, before_markers)
      register int nchars, nbytes;
      int 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;
 
   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 (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
   combined_before_bytes
     = count_combining_before (string, nbytes, PT, PT_BYTE);
   combined_after_bytes
@@ -1042,23 +1064,33 @@ insert_1_both (string, nchars, nbytes, inherit, prepare, before_markers)
 
   if (combined_after_bytes)
     {
 
   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);
 
       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)
     {
     }
 
   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);
       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,
     }
 
   record_insert (PT - !!combined_before_bytes,
@@ -1113,6 +1145,8 @@ insert_1_both (string, nchars, nbytes, inherit, prepare, before_markers)
     if (combined_before_bytes)
       combine_bytes (pos, pos_byte, combined_before_bytes);
   }
     if (combined_before_bytes)
       combine_bytes (pos, pos_byte, combined_before_bytes);
   }
+
+  CHECK_MARKERS ();
 }
 \f
 /* Insert the part of the text of STRING, a Lisp object assumed to be
 }
 \f
 /* Insert the part of the text of STRING, a Lisp object assumed to be
@@ -1130,13 +1164,10 @@ insert_from_string (string, pos, pos_byte, length, length_byte, inherit)
      register int pos, pos_byte, length, length_byte;
      int inherit;
 {
      register int pos, pos_byte, length, length_byte;
      int inherit;
 {
-  if (length > 0)
-    {
-      int opoint = PT;
-      insert_from_string_1 (string, pos, pos_byte, length, length_byte,
-                           inherit, 0);
-      signal_after_change (opoint, 0, PT - opoint);
-    }
+  int opoint = PT;
+  insert_from_string_1 (string, pos, pos_byte, length, length_byte,
+                       inherit, 0);
+  signal_after_change (opoint, 0, PT - opoint);
 }
 
 /* Like `insert_from_string' except that all markers pointing
 }
 
 /* Like `insert_from_string' except that all markers pointing
@@ -1149,13 +1180,10 @@ insert_from_string_before_markers (string, pos, pos_byte,
      register int pos, pos_byte, length, length_byte;
      int inherit;
 {
      register int pos, pos_byte, length, length_byte;
      int inherit;
 {
-  if (length > 0)
-    {
-      int opoint = PT;
-      insert_from_string_1 (string, pos, pos_byte, length, length_byte,
-                           inherit, 1);
-      signal_after_change (opoint, 0, PT - opoint);
-    }
+  int opoint = PT;
+  insert_from_string_1 (string, pos, pos_byte, length, length_byte,
+                       inherit, 1);
+  signal_after_change (opoint, 0, PT - opoint);
 }
 
 /* Subroutine of the insertion functions above.  */
 }
 
 /* Subroutine of the insertion functions above.  */
@@ -1173,7 +1201,6 @@ insert_from_string_1 (string, pos, pos_byte, nchars, nbytes,
   int combined_before_bytes, combined_after_bytes;
   int adjusted_nchars;
   INTERVAL intervals;
   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.  */
 
   /* Make OUTGOING_NBYTES describe the text
      as it will be inserted in this buffer.  */
@@ -1185,12 +1212,10 @@ insert_from_string_1 (string, pos, pos_byte, nchars, nbytes,
       = count_size_as_multibyte (&XSTRING (string)->data[pos_byte],
                                 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);
   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)
   prepare_to_modify_buffer (PT, PT, NULL);
 
   if (PT != GPT)
@@ -1225,23 +1250,33 @@ insert_from_string_1 (string, pos, pos_byte, nchars, nbytes,
 
   if (combined_after_bytes)
     {
 
   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);
 
       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)
     {
     }
 
   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);
       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,
     }
 
   record_insert (PT - !!combined_before_bytes,
@@ -1311,13 +1346,10 @@ insert_from_buffer (buf, charpos, nchars, inherit)
      int charpos, nchars;
      int inherit;
 {
      int charpos, nchars;
      int inherit;
 {
-  if (nchars > 0)
-    {
-      int opoint = PT;
+  int opoint = PT;
 
 
-      insert_from_buffer_1 (buf, charpos, nchars, inherit);
-      signal_after_change (opoint, 0, PT - opoint);
-    }
+  insert_from_buffer_1 (buf, charpos, nchars, inherit);
+  signal_after_change (opoint, 0, PT - opoint);
 }
 
 static void
 }
 
 static void
@@ -1326,7 +1358,7 @@ insert_from_buffer_1 (buf, from, nchars, inherit)
      int from, nchars;
      int 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);
   int chunk;
   int from_byte = buf_charpos_to_bytepos (buf, from);
   int to_byte = buf_charpos_to_bytepos (buf, from + nchars);
@@ -1351,6 +1383,9 @@ insert_from_buffer_1 (buf, from, nchars, inherit)
   if (outgoing_nbytes + Z != XINT (temp))
     error ("Maximum buffer size exceeded");
 
   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)
   prepare_to_modify_buffer (PT, PT, NULL);
 
   if (PT != GPT)
@@ -1396,23 +1431,33 @@ insert_from_buffer_1 (buf, from, nchars, inherit)
 
   if (combined_after_bytes)
     {
 
   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);
 
       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)
     {
     }
 
   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);
       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,
     }
 
   record_insert (PT - !!combined_before_bytes,
@@ -1479,12 +1524,17 @@ adjust_before_replace (from, from_byte, to, to_byte)
      int from, from_byte, to, to_byte;
 {
   Lisp_Object deletion;
      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);
 
   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);
 }
 
   adjust_overlays_for_delete (from, to - from);
 }
 
@@ -1504,28 +1554,46 @@ 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);
     = 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;
 
   int nchars_del = 0, nbytes_del = 0;
 
+  if (STRINGP (prev_text))
+    {
+      nchars_del = XSTRING (prev_text)->size;
+      nbytes_del = STRING_BYTES (XSTRING (prev_text));
+    }
+
   if (combined_after_bytes)
     {
   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);
 
       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 + nchars_del, deletion);
     }
 
     }
 
-  if (combined_before_bytes)
+  if (combined_before_bytes
+      || len_byte == 0 && combined_after_bytes > 0)
     {
     {
-      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);
       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.  */
     }
 
   /* Update various buffer positions for the new text.  */
@@ -1535,22 +1603,29 @@ adjust_after_replace (from, from_byte, prev_text, len, len_byte)
   GPT += len; GPT_BYTE += len_byte;
   if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
 
   GPT += len; GPT_BYTE += len_byte;
   if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
 
+  /* The gap should be at character boundary.  */
   if (combined_after_bytes)
     move_gap_both (GPT + combined_after_bytes,
                   GPT_BYTE + combined_after_bytes);
 
   if (combined_after_bytes)
     move_gap_both (GPT + combined_after_bytes,
                   GPT_BYTE + combined_after_bytes);
 
-  if (STRINGP (prev_text))
-    {
-      nchars_del = XSTRING (prev_text)->size;
-      nbytes_del = STRING_BYTES (XSTRING (prev_text));
-    }
   adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
                              len, len_byte,
                              combined_before_bytes, combined_after_bytes);
   adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
                              len, len_byte,
                              combined_before_bytes, combined_after_bytes);
-  if (STRINGP (prev_text))
-    record_delete (from, prev_text);
-  record_insert (from - !!combined_before_bytes,
-                len - combined_before_bytes + !!combined_before_bytes);
+  if (! EQ (current_buffer->undo_list, Qt))
+    {
+      /* This flag tells if we combine some bytes with a character
+        before FROM.  This happens even if combined_before_bytes is
+        zero.  */
+      int combine_before = (combined_before_bytes
+                           || (len == 0 && combined_after_bytes));
+
+      if (nchars_del > 0)
+       record_delete (from - combine_before, prev_text);
+      if (combine_before)
+       record_insert (from - 1, len - combined_before_bytes + 1);
+      else
+       record_insert (from, len);
+    }
 
   if (len > nchars_del)
     adjust_overlays_for_insert (from, len - nchars_del);
 
   if (len > nchars_del)
     adjust_overlays_for_insert (from, len - nchars_del);
@@ -1558,17 +1633,16 @@ 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)
     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)
 #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_after_bytes)
       combine_bytes (from + len, from_byte + len_byte, combined_after_bytes);
@@ -1612,12 +1686,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.
 /* 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
 
 void
-replace_range (from, to, new, prepare, inherit, nomarkers)
+replace_range (from, to, new, prepare, inherit, markers)
      Lisp_Object new;
      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));
 {
   int inschars = XSTRING (new)->size;
   int insbytes = STRING_BYTES (XSTRING (new));
@@ -1681,17 +1757,18 @@ replace_range (from, to, new, prepare, inherit, nomarkers)
   if (to < GPT)
     gap_left (to, to_byte, 0);
 
   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);
 
     /* 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;
   GAP_SIZE += nbytes_del;
   ZV -= nchars_del;
   Z -= nchars_del;
@@ -1718,16 +1795,16 @@ replace_range (from, to, new, prepare, inherit, nomarkers)
             STRING_MULTIBYTE (new),
             ! NILP (current_buffer->enable_multibyte_characters));
 
             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
      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
   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,
 
   /* Record deletion of the surrounding text that combines with
      the insertion.  This, together with recording the insertion,
@@ -1738,27 +1815,43 @@ replace_range (from, to, new, prepare, inherit, nomarkers)
 
   if (combined_after_bytes)
     {
 
   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 + nchars_del, deletion);
     }
 
   if (combined_before_bytes)
     {
     }
 
   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;
 
   GAP_SIZE -= outgoing_insbytes;
   GPT += inschars;
@@ -1780,13 +1873,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);
      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
     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.  */
 
   /* Get the intervals for the part of the string we are inserting--
      not including the combined-before bytes.  */
@@ -1798,16 +1891,13 @@ replace_range (from, to, new, prepare, inherit, nomarkers)
 
   /* Relocate point as if it were a marker.  */
   if (from < PT)
 
   /* 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
                  (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_after_bytes)
     combine_bytes (from + inschars, from_byte + outgoing_insbytes,
                   combined_after_bytes);
-
   if (combined_before_bytes)
     combine_bytes (from, from_byte, combined_before_bytes);
 
   if (combined_before_bytes)
     combine_bytes (from, from_byte, combined_before_bytes);
 
@@ -1819,7 +1909,7 @@ replace_range (from, to, new, prepare, inherit, nomarkers)
   MODIFF++;
   UNGCPRO;
 
   MODIFF++;
   UNGCPRO;
 
-  signal_after_change (from, nchars_del, PT - from);
+  signal_after_change (from, nchars_del, GPT - from);
 }
 \f
 /* Delete characters in current buffer
 }
 \f
 /* Delete characters in current buffer
@@ -1971,11 +2061,12 @@ del_range_2 (from, from_byte, to, to_byte)
   else
     from_byte_1 = from_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.  */
   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.  */
@@ -2002,7 +2093,8 @@ del_range_2 (from, from_byte, to, to_byte)
       adjust_markers_for_record_delete (from - 1, from_byte_1,
                                        from, from_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.  */
   MODIFF++;
 
   /* Relocate point as if it were a marker.  */
@@ -2041,6 +2133,11 @@ del_range_2 (from, from_byte, to, to_byte)
 
   if (combined_after_bytes)
     {
 
   if (combined_after_bytes)
     {
+      /* Adjust markers for byte combining.  As we have already
+         adjuted markers without concerning byte combining, here we
+         must concern only byte combining.  */
+      adjust_markers_for_replace (from, from_byte, 0, 0, 0, 0,
+                                 0, combined_after_bytes);
       combine_bytes (from, from_byte, combined_after_bytes);
 
       record_insert (GPT - 1, 1);
       combine_bytes (from, from_byte, combined_after_bytes);
 
       record_insert (GPT - 1, 1);
@@ -2197,6 +2294,9 @@ signal_before_change (start_int, end_int, preserve_ptr)
   Lisp_Object preserve_marker;
   struct gcpro gcpro1, gcpro2, gcpro3;
 
   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;
   start = make_number (start_int);
   end = make_number (end_int);
   preserve_marker = Qnil;
@@ -2283,6 +2383,9 @@ void
 signal_after_change (charpos, lendel, lenins)
      int charpos, lendel, lenins;
 {
 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.  */
   /* 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.  */
@@ -2360,7 +2463,8 @@ signal_after_change (charpos, lendel, lenins)
   /* After an insertion, call the text properties
      insert-behind-hooks or insert-in-front-hooks.  */
   if (lendel == 0)
   /* After an insertion, call the text properties
      insert-behind-hooks or insert-in-front-hooks.  */
   if (lendel == 0)
-    report_interval_modification (charpos, charpos + lenins);
+    report_interval_modification (make_number (charpos),
+                                 make_number (charpos + lenins));
 }
 
 Lisp_Object
 }
 
 Lisp_Object