Use BSET for write access to Lisp_Object members of struct buffer.
[bpt/emacs.git] / src / insdel.c
index 9b3d61e..14d2dab 100644 (file)
@@ -1,6 +1,5 @@
 /* Buffer insertion/deletion and gap motion for GNU Emacs.
-   Copyright (C) 1985, 1986, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
-                 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
+   Copyright (C) 1985-1986, 1993-1995, 1997-2012
                  Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
@@ -21,41 +20,26 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <config.h>
 #include <setjmp.h>
+
+#include <intprops.h>
+
 #include "lisp.h"
 #include "intervals.h"
-#include "buffer.h"
 #include "character.h"
+#include "buffer.h"
 #include "window.h"
 #include "blockinput.h"
 #include "region-cache.h"
 
-#ifndef NULL
-#define NULL 0
-#endif
-
 static void insert_from_string_1 (Lisp_Object string,
-                                 EMACS_INT pos, EMACS_INT pos_byte,
-                                 EMACS_INT nchars, EMACS_INT nbytes,
+                                 ptrdiff_t pos, ptrdiff_t pos_byte,
+                                 ptrdiff_t nchars, ptrdiff_t nbytes,
                                  int inherit, int before_markers);
 static void insert_from_buffer_1 (struct buffer *buf,
-                                 EMACS_INT from, EMACS_INT nchars,
+                                 ptrdiff_t from, ptrdiff_t nchars,
                                  int inherit);
-static void gap_left (EMACS_INT charpos, EMACS_INT bytepos, int newgap);
-static void gap_right (EMACS_INT charpos, EMACS_INT bytepos);
-static void adjust_markers_gap_motion (EMACS_INT from, EMACS_INT to,
-                                      EMACS_INT amount);
-static void adjust_markers_for_insert (EMACS_INT from, EMACS_INT from_byte,
-                                      EMACS_INT to, EMACS_INT to_byte,
-                                      int before_markers);
-static void adjust_markers_for_replace (EMACS_INT, EMACS_INT, EMACS_INT,
-                                       EMACS_INT, EMACS_INT, EMACS_INT);
-static void adjust_point (EMACS_INT nchars, EMACS_INT nbytes);
-
-Lisp_Object Fcombine_after_change_execute ();
-
-/* Non-nil means don't call the after-change-functions right away,
-   just record an element in Vcombine_after_change_calls_list.  */
-Lisp_Object Vcombine_after_change_calls;
+static void gap_left (ptrdiff_t charpos, ptrdiff_t bytepos, int newgap);
+static void gap_right (ptrdiff_t charpos, ptrdiff_t bytepos);
 
 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
    describing changes which happened while combine_after_change_calls
@@ -67,28 +51,24 @@ Lisp_Object Vcombine_after_change_calls;
    END-UNCHANGED is the number of chars after the changed range,
    and CHANGE-AMOUNT is the number of characters inserted by the change
    (negative for a deletion).  */
-Lisp_Object combine_after_change_list;
+static Lisp_Object combine_after_change_list;
 
 /* Buffer which combine_after_change_list is about.  */
-Lisp_Object combine_after_change_buffer;
+static Lisp_Object combine_after_change_buffer;
 
 Lisp_Object Qinhibit_modification_hooks;
 
-\f
-/* Check all markers in the current buffer, looking for something invalid.  */
+static void signal_before_change (ptrdiff_t, ptrdiff_t, ptrdiff_t *);
 
-static int check_markers_debug_flag;
+/* Also used in marker.c to enable expensive marker checks.  */
 
-#define CHECK_MARKERS()                                \
-  if (check_markers_debug_flag)                        \
-    check_markers ();                          \
-  else
+#ifdef MARKER_DEBUG
 
-void
-check_markers ()
+static void
+check_markers (void)
 {
   register struct Lisp_Marker *tail;
-  int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
+  int multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters));
 
   for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
     {
@@ -102,12 +82,18 @@ check_markers ()
        abort ();
     }
 }
-\f
+
+#else /* not MARKER_DEBUG */
+
+#define check_markers() do { } while (0)
+
+#endif /* MARKER_DEBUG */
+
 /* Move gap to position CHARPOS.
    Note that this can quit!  */
 
 void
-move_gap (EMACS_INT charpos)
+move_gap (ptrdiff_t charpos)
 {
   move_gap_both (charpos, charpos_to_bytepos (charpos));
 }
@@ -116,7 +102,7 @@ move_gap (EMACS_INT charpos)
    Note that this can quit!  */
 
 void
-move_gap_both (EMACS_INT charpos, EMACS_INT bytepos)
+move_gap_both (ptrdiff_t charpos, ptrdiff_t bytepos)
 {
   if (bytepos < GPT_BYTE)
     gap_left (charpos, bytepos, 0);
@@ -130,11 +116,11 @@ move_gap_both (EMACS_INT charpos, EMACS_INT bytepos)
    If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged.  */
 
 static void
-gap_left (EMACS_INT charpos, EMACS_INT bytepos, int newgap)
+gap_left (ptrdiff_t charpos, ptrdiff_t bytepos, int newgap)
 {
   register unsigned char *to, *from;
-  register EMACS_INT i;
-  EMACS_INT new_s1;
+  register ptrdiff_t i;
+  ptrdiff_t new_s1;
 
   if (!newgap)
     BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
@@ -164,38 +150,17 @@ gap_left (EMACS_INT charpos, EMACS_INT bytepos, int newgap)
       /* Move at most 32000 chars before checking again for a quit.  */
       if (i > 32000)
        i = 32000;
-#ifdef GAP_USE_BCOPY
-      if (i >= 128
-         /* bcopy is safe if the two areas of memory do not overlap
-            or on systems where bcopy is always safe for moving upward.  */
-         && (BCOPY_UPWARD_SAFE
-             || to - from >= 128))
-       {
-         /* If overlap is not safe, avoid it by not moving too many
-            characters at once.  */
-         if (!BCOPY_UPWARD_SAFE && i > to - from)
-           i = to - from;
-         new_s1 -= i;
-         from -= i, to -= i;
-         bcopy (from, to, i);
-       }
-      else
-#endif
-       {
-         new_s1 -= i;
-         while (--i >= 0)
-           *--to = *--from;
-       }
+      new_s1 -= i;
+      from -= i, to -= i;
+      memmove (to, from, i);
     }
 
-  /* Adjust markers, and buffer data structure, to put the gap at BYTEPOS.
-     BYTEPOS is where the loop above stopped, which may be what was specified
-     or may be where a quit was detected.  */
-  adjust_markers_gap_motion (bytepos, GPT_BYTE, GAP_SIZE);
+  /* Adjust buffer data structure, to put the gap at BYTEPOS.
+     BYTEPOS is where the loop above stopped, which may be what
+     was specified or may be where a quit was detected.  */
   GPT_BYTE = bytepos;
   GPT = charpos;
-  if (bytepos < charpos)
-    abort ();
+  eassert (charpos <= bytepos);
   if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor.  */
   QUIT;
 }
@@ -205,11 +170,11 @@ gap_left (EMACS_INT charpos, EMACS_INT bytepos, int newgap)
    and CHARPOS is the corresponding char position.  */
 
 static void
-gap_right (EMACS_INT charpos, EMACS_INT bytepos)
+gap_right (ptrdiff_t charpos, ptrdiff_t bytepos)
 {
   register unsigned char *to, *from;
-  register EMACS_INT i;
-  EMACS_INT new_s1;
+  register ptrdiff_t i;
+  ptrdiff_t new_s1;
 
   BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
 
@@ -238,101 +203,18 @@ gap_right (EMACS_INT charpos, EMACS_INT bytepos)
       /* Move at most 32000 chars before checking again for a quit.  */
       if (i > 32000)
        i = 32000;
-#ifdef GAP_USE_BCOPY
-      if (i >= 128
-         /* bcopy is safe if the two areas of memory do not overlap
-            or on systems where bcopy is always safe for moving downward.  */
-         && (BCOPY_DOWNWARD_SAFE
-             || from - to >= 128))
-       {
-         /* If overlap is not safe, avoid it by not moving too many
-            characters at once.  */
-         if (!BCOPY_DOWNWARD_SAFE && i > from - to)
-           i = from - to;
-         new_s1 += i;
-         bcopy (from, to, i);
-         from += i, to += i;
-       }
-      else
-#endif
-       {
-         new_s1 += i;
-         while (--i >= 0)
-           *to++ = *from++;
-       }
+      new_s1 += i;
+      memmove (to, from, i);
+      from += i, to += i;
     }
 
-  adjust_markers_gap_motion (GPT_BYTE + GAP_SIZE, bytepos + GAP_SIZE,
-                            - GAP_SIZE);
   GPT = charpos;
   GPT_BYTE = bytepos;
-  if (bytepos < charpos)
-    abort ();
+  eassert (charpos <= bytepos);
   if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor.  */
   QUIT;
 }
 \f
-/* Add AMOUNT to the byte position of every marker in the current buffer
-   whose current byte position is between FROM (exclusive) and TO (inclusive).
-
-   Also, any markers past the outside of that interval, in the direction
-   of adjustment, are first moved back to the near end of the interval
-   and then adjusted by AMOUNT.
-
-   When the latter adjustment is done, if AMOUNT is negative,
-   we record the adjustment for undo.  (This case happens only for
-   deletion.)
-
-   The markers' character positions are not altered,
-   because gap motion does not affect character positions.  */
-
-int adjust_markers_test;
-
-static void
-adjust_markers_gap_motion (EMACS_INT from, EMACS_INT to, EMACS_INT amount)
-{
-  /* Now that a marker has a bytepos, not counting the gap,
-     nothing needs to be done here.  */
-#if 0
-  Lisp_Object marker;
-  register struct Lisp_Marker *m;
-  register EMACS_INT mpos;
-
-  marker = BUF_MARKERS (current_buffer);
-
-  while (!NILP (marker))
-    {
-      m = XMARKER (marker);
-      mpos = m->bytepos;
-      if (amount > 0)
-       {
-         if (mpos > to && mpos < to + amount)
-           {
-             if (adjust_markers_test)
-               abort ();
-             mpos = to + amount;
-           }
-       }
-      else
-       {
-         /* Here's the case where a marker is inside text being deleted.
-            AMOUNT can be negative for gap motion, too,
-            but then this range contains no markers.  */
-         if (mpos > from + amount && mpos <= from)
-           {
-             if (adjust_markers_test)
-               abort ();
-             mpos = from + amount;
-           }
-       }
-      if (mpos > from && mpos <= to)
-       mpos += amount;
-      m->bufpos = mpos;
-      marker = m->chain;
-    }
-#endif
-}
-\f
 /* Adjust all markers for a deletion
    whose range in bytes is FROM_BYTE to TO_BYTE.
    The range in charpos is FROM to TO.
@@ -341,19 +223,17 @@ adjust_markers_gap_motion (EMACS_INT from, EMACS_INT to, EMACS_INT amount)
    or inside of the range being deleted.  */
 
 void
-adjust_markers_for_delete (EMACS_INT from, EMACS_INT from_byte,
-                          EMACS_INT to, EMACS_INT to_byte)
+adjust_markers_for_delete (ptrdiff_t from, ptrdiff_t from_byte,
+                          ptrdiff_t to, ptrdiff_t to_byte)
 {
   Lisp_Object marker;
   register struct Lisp_Marker *m;
-  register EMACS_INT charpos;
+  register ptrdiff_t charpos;
 
   for (m = BUF_MARKERS (current_buffer); m; m = m->next)
     {
       charpos = m->charpos;
-
-      if (charpos > Z)
-       abort ();
+      eassert (charpos <= Z);
 
       /* If the marker is after the deletion,
         relocate by number of chars / bytes deleted.  */
@@ -406,13 +286,13 @@ adjust_markers_for_delete (EMACS_INT from, EMACS_INT from_byte,
    or BEFORE_MARKERS is true.  */
 
 static void
-adjust_markers_for_insert (EMACS_INT from, EMACS_INT from_byte,
-                          EMACS_INT to, EMACS_INT to_byte, int before_markers)
+adjust_markers_for_insert (ptrdiff_t from, ptrdiff_t from_byte,
+                          ptrdiff_t to, ptrdiff_t to_byte, int before_markers)
 {
   struct Lisp_Marker *m;
   int adjusted = 0;
-  EMACS_INT nchars = to - from;
-  EMACS_INT nbytes = to_byte - from_byte;
+  ptrdiff_t nchars = to - from;
+  ptrdiff_t nbytes = to_byte - from_byte;
 
   for (m = BUF_MARKERS (current_buffer); m; m = m->next)
     {
@@ -437,11 +317,11 @@ adjust_markers_for_insert (EMACS_INT from, EMACS_INT from_byte,
     }
 
   /* Adjusting only markers whose insertion-type is t may result in
-     - disordered start and end in overlays, and 
+     - disordered start and end in overlays, and
      - disordered overlays in the slot `overlays_before' of current_buffer.  */
   if (adjusted)
     {
-      fix_start_end_in_overlays(from, to);
+      fix_start_end_in_overlays (from, to);
       fix_overlays_before (current_buffer, from, to);
     }
 }
@@ -457,7 +337,7 @@ adjust_markers_for_insert (EMACS_INT from, EMACS_INT from_byte,
    intervals.  */
 
 static void
-adjust_point (EMACS_INT nchars, EMACS_INT nbytes)
+adjust_point (ptrdiff_t nchars, ptrdiff_t nbytes)
 {
   SET_BUF_PT_BOTH (current_buffer, PT + nchars, PT_BYTE + nbytes);
   /* In a single-byte buffer, the two positions must be equal.  */
@@ -470,14 +350,14 @@ adjust_point (EMACS_INT nchars, EMACS_INT nbytes)
    an insertion.  */
 
 static void
-adjust_markers_for_replace (EMACS_INT from, EMACS_INT from_byte,
-                           EMACS_INT old_chars, EMACS_INT old_bytes,
-                           EMACS_INT new_chars, EMACS_INT new_bytes)
+adjust_markers_for_replace (ptrdiff_t from, ptrdiff_t from_byte,
+                           ptrdiff_t old_chars, ptrdiff_t old_bytes,
+                           ptrdiff_t new_chars, ptrdiff_t new_bytes)
 {
   register struct Lisp_Marker *m;
-  EMACS_INT prev_to_byte = from_byte + old_bytes;
-  EMACS_INT diff_chars = new_chars - old_chars;
-  EMACS_INT diff_bytes = new_bytes - old_bytes;
+  ptrdiff_t prev_to_byte = from_byte + old_bytes;
+  ptrdiff_t diff_chars = new_chars - old_chars;
+  ptrdiff_t diff_bytes = new_bytes - old_bytes;
 
   for (m = BUF_MARKERS (current_buffer); m; m = m->next)
     {
@@ -493,33 +373,35 @@ adjust_markers_for_replace (EMACS_INT from, EMACS_INT from_byte,
        }
     }
 
-  CHECK_MARKERS ();
+  check_markers ();
 }
 
 \f
+void
+buffer_overflow (void)
+{
+  error ("Maximum buffer size exceeded");
+}
+
 /* Make the gap NBYTES_ADDED bytes longer.  */
 
-void
-make_gap_larger (EMACS_INT nbytes_added)
+static void
+make_gap_larger (ptrdiff_t nbytes_added)
 {
   Lisp_Object tem;
-  EMACS_INT real_gap_loc;
-  EMACS_INT real_gap_loc_byte;
-  EMACS_INT old_gap_size;
-
-  /* If we have to get more space, get enough to last a while.  */
-  nbytes_added += 2000;
-
-  { EMACS_INT total_size = Z_BYTE - BEG_BYTE + GAP_SIZE + nbytes_added;
-    if (total_size < 0
-       /* Don't allow a buffer size that won't fit in a Lisp integer.  */
-       || total_size != XINT (make_number (total_size))
-       /* Don't allow a buffer size that won't fit in an int
-          even if it will fit in a Lisp integer.
-          That won't work because so many places still use `int'.  */
-       || total_size != (EMACS_INT) (int) total_size)
-      error ("Buffer exceeds maximum size");
-  }
+  ptrdiff_t real_gap_loc;
+  ptrdiff_t real_gap_loc_byte;
+  ptrdiff_t old_gap_size;
+  ptrdiff_t current_size = Z_BYTE - BEG_BYTE + GAP_SIZE;
+  enum { enough_for_a_while = 2000 };
+
+  if (BUF_BYTES_MAX - current_size < nbytes_added)
+    buffer_overflow ();
+
+  /* If we have to get more space, get enough to last a while;
+     but do not exceed the maximum buffer size.  */
+  nbytes_added = min (nbytes_added + enough_for_a_while,
+                     BUF_BYTES_MAX - current_size);
 
   enlarge_buffer_text (current_buffer, nbytes_added);
 
@@ -551,19 +433,20 @@ make_gap_larger (EMACS_INT nbytes_added)
   Vinhibit_quit = tem;
 }
 
+#if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
 
 /* Make the gap NBYTES_REMOVED bytes shorter.  */
 
-void
-make_gap_smaller (EMACS_INT nbytes_removed)
+static void
+make_gap_smaller (ptrdiff_t nbytes_removed)
 {
   Lisp_Object tem;
-  EMACS_INT real_gap_loc;
-  EMACS_INT real_gap_loc_byte;
-  EMACS_INT real_Z;
-  EMACS_INT real_Z_byte;
-  EMACS_INT real_beg_unchanged;
-  EMACS_INT new_gap_size;
+  ptrdiff_t real_gap_loc;
+  ptrdiff_t real_gap_loc_byte;
+  ptrdiff_t real_Z;
+  ptrdiff_t real_Z_byte;
+  ptrdiff_t real_beg_unchanged;
+  ptrdiff_t new_gap_size;
 
   /* Make sure the gap is at least 20 bytes.  */
   if (GAP_SIZE - nbytes_removed < 20)
@@ -583,7 +466,7 @@ make_gap_smaller (EMACS_INT nbytes_removed)
   /* Pretend that the last unwanted part of the gap is the entire gap,
      and that the first desired part of the gap is part of the buffer
      text.  */
-  bzero (GPT_ADDR, new_gap_size);
+  memset (GPT_ADDR, 0, new_gap_size);
   GPT += new_gap_size;
   GPT_BYTE += new_gap_size;
   Z += new_gap_size;
@@ -610,8 +493,10 @@ make_gap_smaller (EMACS_INT nbytes_removed)
   Vinhibit_quit = tem;
 }
 
+#endif /* USE_MMAP_FOR_BUFFERS || REL_ALLOC || DOUG_LEA_MALLOC */
+
 void
-make_gap (EMACS_INT nbytes_added)
+make_gap (ptrdiff_t nbytes_added)
 {
   if (nbytes_added >= 0)
     make_gap_larger (nbytes_added);
@@ -628,20 +513,19 @@ make_gap (EMACS_INT nbytes_added)
 
    Return the number of bytes stored at TO_ADDR.  */
 
-EMACS_INT
+ptrdiff_t
 copy_text (const unsigned char *from_addr, unsigned char *to_addr,
-          EMACS_INT nbytes, int from_multibyte, int to_multibyte)
+          ptrdiff_t nbytes, int from_multibyte, int to_multibyte)
 {
   if (from_multibyte == to_multibyte)
     {
-      bcopy (from_addr, to_addr, nbytes);
+      memcpy (to_addr, from_addr, nbytes);
       return nbytes;
     }
   else if (from_multibyte)
     {
-      EMACS_INT nchars = 0;
-      EMACS_INT bytes_left = nbytes;
-      Lisp_Object tbl = Qnil;
+      ptrdiff_t nchars = 0;
+      ptrdiff_t bytes_left = nbytes;
 
       while (bytes_left > 0)
        {
@@ -678,32 +562,6 @@ copy_text (const unsigned char *from_addr, unsigned char *to_addr,
       return to_addr - initial_to_addr;
     }
 }
-
-/* Return the number of bytes it would take
-   to convert some single-byte text to multibyte.
-   The single-byte text consists of NBYTES bytes at PTR.  */
-
-EMACS_INT
-count_size_as_multibyte (const unsigned char *ptr, EMACS_INT nbytes)
-{
-  EMACS_INT i;
-  EMACS_INT outgoing_nbytes = 0;
-
-  for (i = 0; i < nbytes; i++)
-    {
-      unsigned int c = *ptr++;
-
-      if (ASCII_CHAR_P (c))
-       outgoing_nbytes++;
-      else
-       {
-         c = BYTE8_TO_CHAR (c);
-         outgoing_nbytes += CHAR_BYTES (c);
-       }
-    }
-
-  return outgoing_nbytes;
-}
 \f
 /* Insert a string of specified length before point.
    This function judges multibyteness based on
@@ -714,11 +572,11 @@ count_size_as_multibyte (const unsigned char *ptr, EMACS_INT nbytes)
    prepare_to_modify_buffer could relocate the text.  */
 
 void
-insert (const unsigned char *string, EMACS_INT nbytes)
+insert (const char *string, ptrdiff_t nbytes)
 {
   if (nbytes > 0)
     {
-      EMACS_INT len = chars_in_text (string, nbytes), opoint;
+      ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
       insert_1_both (string, len, nbytes, 0, 1, 0);
       opoint = PT - len;
       signal_after_change (opoint, 0, len);
@@ -729,11 +587,11 @@ insert (const unsigned char *string, EMACS_INT nbytes)
 /* Likewise, but inherit text properties from neighboring characters.  */
 
 void
-insert_and_inherit (const unsigned char *string, EMACS_INT nbytes)
+insert_and_inherit (const char *string, ptrdiff_t nbytes)
 {
   if (nbytes > 0)
     {
-      EMACS_INT len = chars_in_text (string, nbytes), opoint;
+      ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
       insert_1_both (string, len, nbytes, 1, 1, 0);
       opoint = PT - len;
       signal_after_change (opoint, 0, len);
@@ -749,7 +607,7 @@ insert_char (int c)
   unsigned char str[MAX_MULTIBYTE_LENGTH];
   int len;
 
-  if (! NILP (current_buffer->enable_multibyte_characters))
+  if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
     len = CHAR_STRING (c, str);
   else
     {
@@ -757,7 +615,7 @@ insert_char (int c)
       str[0] = c;
     }
 
-  insert (str, len);
+  insert ((char *) str, len);
 }
 
 /* Insert the null-terminated string S before point.  */
@@ -774,11 +632,11 @@ insert_string (const char *s)
    since gc could happen and relocate it.  */
 
 void
-insert_before_markers (const unsigned char *string, EMACS_INT nbytes)
+insert_before_markers (const char *string, ptrdiff_t nbytes)
 {
   if (nbytes > 0)
     {
-      EMACS_INT len = chars_in_text (string, nbytes), opoint;
+      ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
       insert_1_both (string, len, nbytes, 0, 1, 1);
       opoint = PT - len;
       signal_after_change (opoint, 0, len);
@@ -789,12 +647,12 @@ insert_before_markers (const unsigned char *string, EMACS_INT nbytes)
 /* Likewise, but inherit text properties from neighboring characters.  */
 
 void
-insert_before_markers_and_inherit (const unsigned char *string,
-                                  EMACS_INT nbytes)
+insert_before_markers_and_inherit (const char *string,
+                                  ptrdiff_t nbytes)
 {
   if (nbytes > 0)
     {
-      EMACS_INT len = chars_in_text (string, nbytes), opoint;
+      ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
       insert_1_both (string, len, nbytes, 1, 1, 1);
       opoint = PT - len;
       signal_after_change (opoint, 0, len);
@@ -805,11 +663,11 @@ insert_before_markers_and_inherit (const unsigned char *string,
 /* Subroutine used by the insert functions above.  */
 
 void
-insert_1 (const unsigned char *string, EMACS_INT nbytes,
+insert_1 (const char *string, ptrdiff_t nbytes,
          int inherit, int prepare, int before_markers)
 {
-  insert_1_both (string, chars_in_text (string, nbytes), nbytes,
-                inherit, prepare, before_markers);
+  insert_1_both (string, chars_in_text ((unsigned char *) string, nbytes),
+                nbytes, inherit, prepare, before_markers);
 }
 
 \f
@@ -821,8 +679,8 @@ insert_1 (const unsigned char *string, EMACS_INT nbytes,
    which combine in this way.  Otherwise, return 0.  */
 
 int
-count_combining_before (const unsigned char *string, EMACS_INT length,
-                       EMACS_INT pos, EMACS_INT pos_byte)
+count_combining_before (const unsigned char *string, ptrdiff_t length,
+                       ptrdiff_t pos, ptrdiff_t pos_byte)
 {
   int len, combining_bytes;
   const unsigned char *p;
@@ -841,7 +699,7 @@ count_combining_before (const unsigned char *string, EMACS_INT length,
   len = 1;
   p = BYTE_POS_ADDR (pos_byte - 1);
   while (! CHAR_HEAD_P (*p)) p--, len++;
-  if (! BASE_LEADING_CODE_P (*p)) /* case (3) */
+  if (! LEADING_CODE_P (*p)) /* case (3) */
     return 0;
 
   combining_bytes = BYTES_BY_CHAR_HEAD (*p) - len;
@@ -867,11 +725,11 @@ count_combining_before (const unsigned char *string, EMACS_INT length,
 
 int
 count_combining_after (const unsigned char *string,
-                      EMACS_INT length, EMACS_INT pos, EMACS_INT pos_byte)
+                      ptrdiff_t length, ptrdiff_t pos, ptrdiff_t pos_byte)
 {
-  EMACS_INT opos_byte = pos_byte;
-  EMACS_INT i;
-  EMACS_INT bytes;
+  ptrdiff_t opos_byte = pos_byte;
+  ptrdiff_t i;
+  ptrdiff_t bytes;
   unsigned char *bufp;
 
   if (NILP (current_buffer->enable_multibyte_characters))
@@ -904,7 +762,7 @@ count_combining_after (const unsigned char *string,
       i = pos_byte - 2;
       while (i >= 0 && ! CHAR_HEAD_P (p[i]))
        i--;
-      if (i < 0 || !BASE_LEADING_CODE_P (p[i]))
+      if (i < 0 || !LEADING_CODE_P (p[i]))
        return 0;
 
       bytes = BYTES_BY_CHAR_HEAD (p[i]);
@@ -912,7 +770,7 @@ count_combining_after (const unsigned char *string,
              ? 0
              : bytes - (pos_byte - 1 - i + length));
     }
-  if (!BASE_LEADING_CODE_P (string[i]))
+  if (!LEADING_CODE_P (string[i]))
     return 0;
 
   bytes = BYTES_BY_CHAR_HEAD (string[i]) - (length - i);
@@ -930,14 +788,14 @@ count_combining_after (const unsigned char *string,
    are the same as in insert_1.  */
 
 void
-insert_1_both (const unsigned char *string,
-              EMACS_INT nchars, EMACS_INT nbytes,
+insert_1_both (const char *string,
+              ptrdiff_t nchars, ptrdiff_t nbytes,
               int inherit, int prepare, int before_markers)
 {
   if (nchars == 0)
     return;
 
-  if (NILP (current_buffer->enable_multibyte_characters))
+  if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
     nchars = nbytes;
 
   if (prepare)
@@ -964,7 +822,7 @@ insert_1_both (const unsigned char *string,
   MODIFF++;
   CHARS_MODIFF = MODIFF;
 
-  bcopy (string, GPT_ADDR, nbytes);
+  memcpy (GPT_ADDR, string, nbytes);
 
   GAP_SIZE -= nbytes;
   GPT += nchars;
@@ -975,8 +833,7 @@ insert_1_both (const unsigned char *string,
   Z_BYTE += nbytes;
   if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor.  */
 
-  if (GPT_BYTE < GPT)
-    abort ();
+  eassert (GPT <= GPT_BYTE);
 
   /* The insert may have been in the unchanged region, so check again. */
   if (Z - GPT < END_UNCHANGED)
@@ -987,16 +844,16 @@ insert_1_both (const unsigned char *string,
                             PT + nchars, PT_BYTE + nbytes,
                             before_markers);
 
-  if (BUF_INTERVALS (current_buffer) != 0)
+  if (buffer_get_intervals (current_buffer))
     offset_intervals (current_buffer, PT, nchars);
 
-  if (!inherit && BUF_INTERVALS (current_buffer) != 0)
+  if (!inherit && buffer_get_intervals (current_buffer))
     set_text_properties (make_number (PT), make_number (PT + nchars),
                         Qnil, Qnil, Qnil);
 
   adjust_point (nchars, nbytes);
 
-  CHECK_MARKERS ();
+  check_markers ();
 }
 \f
 /* Insert the part of the text of STRING, a Lisp object assumed to be
@@ -1005,14 +862,14 @@ insert_1_both (const unsigned char *string,
    copy them into the buffer.
 
    It does not work to use `insert' for this, because a GC could happen
-   before we bcopy the stuff into the buffer, and relocate the string
+   before we copy the stuff into the buffer, and relocate the string
    without insert noticing.  */
 
 void
-insert_from_string (Lisp_Object string, EMACS_INT pos, EMACS_INT pos_byte,
-                   EMACS_INT length, EMACS_INT length_byte, int inherit)
+insert_from_string (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
+                   ptrdiff_t length, ptrdiff_t length_byte, int inherit)
 {
-  EMACS_INT opoint = PT;
+  ptrdiff_t opoint = PT;
 
   if (SCHARS (string) == 0)
     return;
@@ -1028,11 +885,11 @@ insert_from_string (Lisp_Object string, EMACS_INT pos, EMACS_INT pos_byte,
 
 void
 insert_from_string_before_markers (Lisp_Object string,
-                                  EMACS_INT pos, EMACS_INT pos_byte,
-                                  EMACS_INT length, EMACS_INT length_byte,
+                                  ptrdiff_t pos, ptrdiff_t pos_byte,
+                                  ptrdiff_t length, ptrdiff_t length_byte,
                                   int inherit)
 {
-  EMACS_INT opoint = PT;
+  ptrdiff_t opoint = PT;
 
   if (SCHARS (string) == 0)
     return;
@@ -1046,18 +903,18 @@ insert_from_string_before_markers (Lisp_Object string,
 /* Subroutine of the insertion functions above.  */
 
 static void
-insert_from_string_1 (Lisp_Object string, EMACS_INT pos, EMACS_INT pos_byte,
-                     EMACS_INT nchars, EMACS_INT nbytes,
+insert_from_string_1 (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
+                     ptrdiff_t nchars, ptrdiff_t nbytes,
                      int inherit, int before_markers)
 {
   struct gcpro gcpro1;
-  EMACS_INT outgoing_nbytes = nbytes;
+  ptrdiff_t outgoing_nbytes = nbytes;
   INTERVAL intervals;
 
   /* Make OUTGOING_NBYTES describe the text
      as it will be inserted in this buffer.  */
 
-  if (NILP (current_buffer->enable_multibyte_characters))
+  if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
     outgoing_nbytes = nchars;
   else if (! STRING_MULTIBYTE (string))
     outgoing_nbytes
@@ -1080,7 +937,7 @@ insert_from_string_1 (Lisp_Object string, EMACS_INT pos, EMACS_INT pos_byte,
      between single-byte and multibyte.  */
   copy_text (SDATA (string) + pos_byte, GPT_ADDR, nbytes,
             STRING_MULTIBYTE (string),
-            ! NILP (current_buffer->enable_multibyte_characters));
+            ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
 
 #ifdef BYTE_COMBINING_DEBUG
   /* We have copied text into the gap, but we have not altered
@@ -1106,8 +963,7 @@ insert_from_string_1 (Lisp_Object string, EMACS_INT pos, EMACS_INT pos_byte,
   Z_BYTE += outgoing_nbytes;
   if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor.  */
 
-  if (GPT_BYTE < GPT)
-    abort ();
+  eassert (GPT <= GPT_BYTE);
 
   /* The insert may have been in the unchanged region, so check again. */
   if (Z - GPT < END_UNCHANGED)
@@ -1120,7 +976,7 @@ insert_from_string_1 (Lisp_Object string, EMACS_INT pos, EMACS_INT pos_byte,
 
   offset_intervals (current_buffer, PT, nchars);
 
-  intervals = STRING_INTERVALS (string);
+  intervals = string_get_intervals (string);
   /* Get the intervals for the part of the string we are inserting.  */
   if (nbytes < SBYTES (string))
     intervals = copy_intervals (intervals, pos, nchars);
@@ -1131,16 +987,16 @@ insert_from_string_1 (Lisp_Object string, EMACS_INT pos, EMACS_INT pos_byte,
 
   adjust_point (nchars, outgoing_nbytes);
 
-  CHECK_MARKERS ();
+  check_markers ();
 }
 \f
 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
    starting at GPT_ADDR.  */
 
 void
-insert_from_gap (EMACS_INT nchars, EMACS_INT nbytes)
+insert_from_gap (ptrdiff_t nchars, ptrdiff_t nbytes)
 {
-  if (NILP (current_buffer->enable_multibyte_characters))
+  if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
     nchars = nbytes;
 
   record_insert (GPT, nchars);
@@ -1155,24 +1011,23 @@ insert_from_gap (EMACS_INT nchars, EMACS_INT nbytes)
   Z_BYTE += nbytes;
   if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor.  */
 
-  if (GPT_BYTE < GPT)
-    abort ();
+  eassert (GPT <= GPT_BYTE);
 
   adjust_overlays_for_insert (GPT - nchars, nchars);
   adjust_markers_for_insert (GPT - nchars, GPT_BYTE - nbytes,
                             GPT, GPT_BYTE, 0);
 
-  if (BUF_INTERVALS (current_buffer) != 0)
+  if (buffer_get_intervals (current_buffer))
     {
       offset_intervals (current_buffer, GPT - nchars, nchars);
-      graft_intervals_into_buffer (NULL_INTERVAL, GPT - nchars, nchars,
+      graft_intervals_into_buffer (NULL, GPT - nchars, nchars,
                                   current_buffer, 0);
     }
 
   if (GPT - nchars < PT)
     adjust_point (nchars, nbytes);
 
-  CHECK_MARKERS ();
+  check_markers ();
 }
 \f
 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
@@ -1180,13 +1035,13 @@ insert_from_gap (EMACS_INT nchars, EMACS_INT nbytes)
    into the current buffer.
 
    It does not work to use `insert' for this, because a malloc could happen
-   and relocate BUF's text before the bcopy happens.  */
+   and relocate BUF's text before the copy happens.  */
 
 void
 insert_from_buffer (struct buffer *buf,
-                   EMACS_INT charpos, EMACS_INT nchars, int inherit)
+                   ptrdiff_t charpos, ptrdiff_t nchars, int inherit)
 {
-  EMACS_INT opoint = PT;
+  ptrdiff_t opoint = PT;
 
   insert_from_buffer_1 (buf, charpos, nchars, inherit);
   signal_after_change (opoint, 0, PT - opoint);
@@ -1195,25 +1050,24 @@ insert_from_buffer (struct buffer *buf,
 
 static void
 insert_from_buffer_1 (struct buffer *buf,
-                     EMACS_INT from, EMACS_INT nchars, int inherit)
+                     ptrdiff_t from, ptrdiff_t nchars, int inherit)
 {
-  register Lisp_Object temp;
-  EMACS_INT chunk, chunk_expanded;
-  EMACS_INT from_byte = buf_charpos_to_bytepos (buf, from);
-  EMACS_INT to_byte = buf_charpos_to_bytepos (buf, from + nchars);
-  EMACS_INT incoming_nbytes = to_byte - from_byte;
-  EMACS_INT outgoing_nbytes = incoming_nbytes;
+  ptrdiff_t chunk, chunk_expanded;
+  ptrdiff_t from_byte = buf_charpos_to_bytepos (buf, from);
+  ptrdiff_t to_byte = buf_charpos_to_bytepos (buf, from + nchars);
+  ptrdiff_t incoming_nbytes = to_byte - from_byte;
+  ptrdiff_t outgoing_nbytes = incoming_nbytes;
   INTERVAL intervals;
 
   /* Make OUTGOING_NBYTES describe the text
      as it will be inserted in this buffer.  */
 
-  if (NILP (current_buffer->enable_multibyte_characters))
+  if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
     outgoing_nbytes = nchars;
-  else if (NILP (buf->enable_multibyte_characters))
+  else if (NILP (BVAR (buf, enable_multibyte_characters)))
     {
-      EMACS_INT outgoing_before_gap = 0;
-      EMACS_INT outgoing_after_gap = 0;
+      ptrdiff_t outgoing_before_gap = 0;
+      ptrdiff_t outgoing_after_gap = 0;
 
       if (from < BUF_GPT (buf))
        {
@@ -1236,11 +1090,6 @@ insert_from_buffer_1 (struct buffer *buf,
       outgoing_nbytes = outgoing_before_gap + outgoing_after_gap;
     }
 
-  /* 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");
-
   /* Do this before moving and increasing the gap,
      because the before-change hooks might move the gap
      or make it smaller.  */
@@ -1261,8 +1110,8 @@ insert_from_buffer_1 (struct buffer *buf,
       chunk_expanded
        = copy_text (BUF_BYTE_ADDRESS (buf, from_byte),
                     GPT_ADDR, chunk,
-                    ! NILP (buf->enable_multibyte_characters),
-                    ! NILP (current_buffer->enable_multibyte_characters));
+                    ! NILP (BVAR (buf, enable_multibyte_characters)),
+                    ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
     }
   else
     chunk_expanded = chunk = 0;
@@ -1270,8 +1119,8 @@ insert_from_buffer_1 (struct buffer *buf,
   if (chunk < incoming_nbytes)
     copy_text (BUF_BYTE_ADDRESS (buf, from_byte + chunk),
               GPT_ADDR + chunk_expanded, incoming_nbytes - chunk,
-              ! NILP (buf->enable_multibyte_characters),
-              ! NILP (current_buffer->enable_multibyte_characters));
+              ! NILP (BVAR (buf, enable_multibyte_characters)),
+              ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
 
 #ifdef BYTE_COMBINING_DEBUG
   /* We have copied text into the gap, but we have not altered
@@ -1297,8 +1146,7 @@ insert_from_buffer_1 (struct buffer *buf,
   Z_BYTE += outgoing_nbytes;
   if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor.  */
 
-  if (GPT_BYTE < GPT)
-    abort ();
+  eassert (GPT <= GPT_BYTE);
 
   /* The insert may have been in the unchanged region, so check again. */
   if (Z - GPT < END_UNCHANGED)
@@ -1309,11 +1157,11 @@ insert_from_buffer_1 (struct buffer *buf,
                             PT_BYTE + outgoing_nbytes,
                             0);
 
-  if (BUF_INTERVALS (current_buffer) != 0)
+  if (buffer_get_intervals (current_buffer))
     offset_intervals (current_buffer, PT, nchars);
 
   /* Get the intervals for the part of the string we are inserting.  */
-  intervals = BUF_INTERVALS (buf);
+  intervals = buffer_get_intervals (buf);
   if (nchars < BUF_Z (buf) - BUF_BEG (buf))
     {
       if (buf == current_buffer && PT <= from)
@@ -1334,11 +1182,11 @@ insert_from_buffer_1 (struct buffer *buf,
 
    PREV_TEXT nil means the new text was just inserted.  */
 
-void
-adjust_after_replace (EMACS_INT from, EMACS_INT from_byte,
-                     Lisp_Object prev_text, EMACS_INT len, EMACS_INT len_byte)
+static void
+adjust_after_replace (ptrdiff_t from, ptrdiff_t from_byte,
+                     Lisp_Object prev_text, ptrdiff_t len, ptrdiff_t len_byte)
 {
-  EMACS_INT nchars_del = 0, nbytes_del = 0;
+  ptrdiff_t nchars_del = 0, nbytes_del = 0;
 
 #ifdef BYTE_COMBINING_DEBUG
   if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
@@ -1366,7 +1214,7 @@ adjust_after_replace (EMACS_INT from, EMACS_INT from_byte,
     adjust_markers_for_insert (from, from_byte,
                               from + len, from_byte + len_byte, 0);
 
-  if (! EQ (current_buffer->undo_list, Qt))
+  if (! EQ (BVAR (current_buffer, undo_list), Qt))
     {
       if (nchars_del > 0)
        record_delete (from, prev_text);
@@ -1377,62 +1225,9 @@ adjust_after_replace (EMACS_INT from, EMACS_INT from_byte,
     adjust_overlays_for_insert (from, len - nchars_del);
   else if (len < nchars_del)
     adjust_overlays_for_delete (from, nchars_del - len);
-  if (BUF_INTERVALS (current_buffer) != 0)
-    {
-      offset_intervals (current_buffer, from, len - nchars_del);
-    }
 
-  if (from < PT)
-    adjust_point (len - nchars_del, len_byte - nbytes_del);
-
-  /* As byte combining will decrease Z, we must check this again. */
-  if (Z - GPT < END_UNCHANGED)
-    END_UNCHANGED = Z - GPT;
-
-  CHECK_MARKERS ();
-
-  if (len == 0)
-    evaporate_overlays (from);
-  MODIFF++;
-  CHARS_MODIFF = MODIFF;
-}
-
-/* Like adjust_after_replace, but doesn't require PREV_TEXT.
-   This is for use when undo is not enabled in the current buffer.  */
-
-void
-adjust_after_replace_noundo (EMACS_INT from, EMACS_INT from_byte,
-                            EMACS_INT nchars_del, EMACS_INT nbytes_del,
-                            EMACS_INT len, EMACS_INT len_byte)
-{
-#ifdef BYTE_COMBINING_DEBUG
-  if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
-      || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
-    abort ();
-#endif
-
-  /* Update various buffer positions for the new text.  */
-  GAP_SIZE -= len_byte;
-  ZV += len; Z+= len;
-  ZV_BYTE += len_byte; Z_BYTE += len_byte;
-  GPT += len; GPT_BYTE += len_byte;
-  if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
-
-  if (nchars_del > 0)
-    adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
-                               len, len_byte);
-  else
-    adjust_markers_for_insert (from, from_byte,
-                              from + len, from_byte + len_byte, 0);
-
-  if (len > nchars_del)
-    adjust_overlays_for_insert (from, len - nchars_del);
-  else if (len < nchars_del)
-    adjust_overlays_for_delete (from, nchars_del - len);
-  if (BUF_INTERVALS (current_buffer) != 0)
-    {
-      offset_intervals (current_buffer, from, len - nchars_del);
-    }
+  if (buffer_get_intervals (current_buffer))
+    offset_intervals (current_buffer, from, len - nchars_del);
 
   if (from < PT)
     adjust_point (len - nchars_del, len_byte - nbytes_del);
@@ -1441,7 +1236,7 @@ adjust_after_replace_noundo (EMACS_INT from, EMACS_INT from_byte,
   if (Z - GPT < END_UNCHANGED)
     END_UNCHANGED = Z - GPT;
 
-  CHECK_MARKERS ();
+  check_markers ();
 
   if (len == 0)
     evaporate_overlays (from);
@@ -1455,10 +1250,10 @@ adjust_after_replace_noundo (EMACS_INT from, EMACS_INT from_byte,
    - FROM) may be incorrect, the correct length is NEWLEN.  */
 
 void
-adjust_after_insert (EMACS_INT from, EMACS_INT from_byte,
-                    EMACS_INT to, EMACS_INT to_byte, EMACS_INT newlen)
+adjust_after_insert (ptrdiff_t from, ptrdiff_t from_byte,
+                    ptrdiff_t to, ptrdiff_t to_byte, ptrdiff_t newlen)
 {
-  EMACS_INT len = to - from, len_byte = to_byte - from_byte;
+  ptrdiff_t len = to - from, len_byte = to_byte - from_byte;
 
   if (GPT != to)
     move_gap_both (to, to_byte);
@@ -1482,34 +1277,33 @@ adjust_after_insert (EMACS_INT from, EMACS_INT from_byte,
    But if MARKERS is 0, don't relocate markers.  */
 
 void
-replace_range (EMACS_INT from, EMACS_INT to, Lisp_Object new,
+replace_range (ptrdiff_t from, ptrdiff_t to, Lisp_Object new,
               int prepare, int inherit, int markers)
 {
-  EMACS_INT inschars = SCHARS (new);
-  EMACS_INT insbytes = SBYTES (new);
-  EMACS_INT from_byte, to_byte;
-  EMACS_INT nbytes_del, nchars_del;
-  register Lisp_Object temp;
+  ptrdiff_t inschars = SCHARS (new);
+  ptrdiff_t insbytes = SBYTES (new);
+  ptrdiff_t from_byte, to_byte;
+  ptrdiff_t nbytes_del, nchars_del;
   struct gcpro gcpro1;
   INTERVAL intervals;
-  EMACS_INT outgoing_insbytes = insbytes;
+  ptrdiff_t outgoing_insbytes = insbytes;
   Lisp_Object deletion;
 
-  CHECK_MARKERS ();
+  check_markers ();
 
   GCPRO1 (new);
   deletion = Qnil;
 
   if (prepare)
     {
-      EMACS_INT range_length = to - from;
+      ptrdiff_t range_length = to - from;
       prepare_to_modify_buffer (from, to, &from);
       to = from + range_length;
     }
 
   UNGCPRO;
 
-  /* Make args be valid */
+  /* Make args be valid */
   if (from < BEGV)
     from = BEGV;
   if (to > ZV)
@@ -1527,17 +1321,12 @@ replace_range (EMACS_INT from, EMACS_INT to, Lisp_Object new,
   /* Make OUTGOING_INSBYTES describe the text
      as it will be inserted in this buffer.  */
 
-  if (NILP (current_buffer->enable_multibyte_characters))
+  if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
     outgoing_insbytes = inschars;
   else if (! STRING_MULTIBYTE (new))
     outgoing_insbytes
       = count_size_as_multibyte (SDATA (new), insbytes);
 
-  /* Make sure point-max won't overflow after this insertion.  */
-  XSETINT (temp, Z_BYTE - nbytes_del + insbytes);
-  if (Z_BYTE - nbytes_del + insbytes != XINT (temp))
-    error ("Maximum buffer size exceeded");
-
   GCPRO1 (new);
 
   /* Make sure the gap is somewhere in or next to what we are deleting.  */
@@ -1549,7 +1338,7 @@ replace_range (EMACS_INT from, EMACS_INT to, Lisp_Object new,
   /* Even if we don't record for undo, we must keep the original text
      because we may have to recover it because of inappropriate byte
      combining.  */
-  if (! EQ (current_buffer->undo_list, Qt))
+  if (! EQ (BVAR (current_buffer, undo_list), Qt))
     deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
 
   GAP_SIZE += nbytes_del;
@@ -1561,22 +1350,21 @@ replace_range (EMACS_INT from, EMACS_INT to, Lisp_Object new,
   GPT_BYTE = from_byte;
   if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor.  */
 
-  if (GPT_BYTE < GPT)
-    abort ();
+  eassert (GPT <= GPT_BYTE);
 
   if (GPT - BEG < BEG_UNCHANGED)
     BEG_UNCHANGED = GPT - BEG;
   if (Z - GPT < END_UNCHANGED)
     END_UNCHANGED = Z - GPT;
 
-  if (GAP_SIZE < insbytes)
-    make_gap (insbytes - GAP_SIZE);
+  if (GAP_SIZE < outgoing_insbytes)
+    make_gap (outgoing_insbytes - GAP_SIZE);
 
   /* Copy the string text into the buffer, perhaps converting
      between single-byte and multibyte.  */
   copy_text (SDATA (new), GPT_ADDR, insbytes,
             STRING_MULTIBYTE (new),
-            ! NILP (current_buffer->enable_multibyte_characters));
+            ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
 
 #ifdef BYTE_COMBINING_DEBUG
   /* We have copied text into the gap, but we have not marked
@@ -1589,7 +1377,7 @@ replace_range (EMACS_INT from, EMACS_INT to, Lisp_Object new,
     abort ();
 #endif
 
-  if (! EQ (current_buffer->undo_list, Qt))
+  if (! EQ (BVAR (current_buffer, undo_list), Qt))
     {
       /* Record the insertion first, so that when we undo,
         the deletion will be undone first.  Thus, undo
@@ -1608,8 +1396,7 @@ replace_range (EMACS_INT from, EMACS_INT to, Lisp_Object new,
   Z_BYTE += outgoing_insbytes;
   if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor.  */
 
-  if (GPT_BYTE < GPT)
-    abort ();
+  eassert (GPT <= GPT_BYTE);
 
   /* Adjust the overlay center as needed.  This must be done after
      adjusting the markers that bound the overlays.  */
@@ -1625,7 +1412,7 @@ replace_range (EMACS_INT from, EMACS_INT to, Lisp_Object new,
 
   /* Get the intervals for the part of the string we are inserting--
      not including the combined-before bytes.  */
-  intervals = STRING_INTERVALS (new);
+  intervals = string_get_intervals (new);
   /* Insert those intervals.  */
   graft_intervals_into_buffer (intervals, from, inschars,
                               current_buffer, inherit);
@@ -1639,7 +1426,7 @@ replace_range (EMACS_INT from, EMACS_INT to, Lisp_Object new,
   if (outgoing_insbytes == 0)
     evaporate_overlays (from);
 
-  CHECK_MARKERS ();
+  check_markers ();
 
   MODIFF++;
   CHARS_MODIFF = MODIFF;
@@ -1662,15 +1449,14 @@ replace_range (EMACS_INT from, EMACS_INT to, Lisp_Object new,
    prepare_to_modify_buffer and never call signal_after_change.  */
 
 void
-replace_range_2 (EMACS_INT from, EMACS_INT from_byte,
-                EMACS_INT to, EMACS_INT to_byte,
-                char *ins, EMACS_INT inschars, EMACS_INT insbytes,
+replace_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
+                ptrdiff_t to, ptrdiff_t to_byte,
+                const char *ins, ptrdiff_t inschars, ptrdiff_t insbytes,
                 int markers)
 {
-  EMACS_INT nbytes_del, nchars_del;
-  Lisp_Object temp;
+  ptrdiff_t nbytes_del, nchars_del;
 
-  CHECK_MARKERS ();
+  check_markers ();
 
   nchars_del = to - from;
   nbytes_del = to_byte - from_byte;
@@ -1678,11 +1464,6 @@ replace_range_2 (EMACS_INT from, EMACS_INT from_byte,
   if (nbytes_del <= 0 && insbytes == 0)
     return;
 
-  /* Make sure point-max won't overflow after this insertion.  */
-  XSETINT (temp, Z_BYTE - nbytes_del + insbytes);
-  if (Z_BYTE - nbytes_del + insbytes != XINT (temp))
-    error ("Maximum buffer size exceeded");
-
   /* Make sure the gap is somewhere in or next to what we are deleting.  */
   if (from > GPT)
     gap_right (from, from_byte);
@@ -1698,8 +1479,7 @@ replace_range_2 (EMACS_INT from, EMACS_INT from_byte,
   GPT_BYTE = from_byte;
   if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor.  */
 
-  if (GPT_BYTE < GPT)
-    abort ();
+  eassert (GPT <= GPT_BYTE);
 
   if (GPT - BEG < BEG_UNCHANGED)
     BEG_UNCHANGED = GPT - BEG;
@@ -1710,7 +1490,7 @@ replace_range_2 (EMACS_INT from, EMACS_INT from_byte,
     make_gap (insbytes - GAP_SIZE);
 
   /* Copy the replacement text into the buffer.  */
-  bcopy (ins, GPT_ADDR, insbytes);
+  memcpy (GPT_ADDR, ins, insbytes);
 
 #ifdef BYTE_COMBINING_DEBUG
   /* We have copied text into the gap, but we have not marked
@@ -1732,8 +1512,7 @@ replace_range_2 (EMACS_INT from, EMACS_INT from_byte,
   Z_BYTE += insbytes;
   if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor.  */
 
-  if (GPT_BYTE < GPT)
-    abort ();
+  eassert (GPT <= GPT_BYTE);
 
   /* Adjust the overlay center as needed.  This must be done after
      adjusting the markers that bound the overlays.  */
@@ -1764,7 +1543,7 @@ replace_range_2 (EMACS_INT from, EMACS_INT from_byte,
   if (insbytes == 0)
     evaporate_overlays (from);
 
-  CHECK_MARKERS ();
+  check_markers ();
 
   MODIFF++;
   CHARS_MODIFF = MODIFF;
@@ -1775,7 +1554,7 @@ replace_range_2 (EMACS_INT from, EMACS_INT from_byte,
    If TO comes before FROM, we delete nothing.  */
 
 void
-del_range (EMACS_INT from, EMACS_INT to)
+del_range (ptrdiff_t from, ptrdiff_t to)
 {
   del_range_1 (from, to, 1, 0);
 }
@@ -1784,9 +1563,9 @@ del_range (EMACS_INT from, EMACS_INT to)
    RET_STRING says to return the deleted text. */
 
 Lisp_Object
-del_range_1 (EMACS_INT from, EMACS_INT to, int prepare, int ret_string)
+del_range_1 (ptrdiff_t from, ptrdiff_t to, int prepare, int ret_string)
 {
-  EMACS_INT from_byte, to_byte;
+  ptrdiff_t from_byte, to_byte;
   Lisp_Object deletion;
   struct gcpro gcpro1;
 
@@ -1801,7 +1580,7 @@ del_range_1 (EMACS_INT from, EMACS_INT to, int prepare, int ret_string)
 
   if (prepare)
     {
-      EMACS_INT range_length = to - from;
+      ptrdiff_t range_length = to - from;
       prepare_to_modify_buffer (from, to, &from);
       to = min (ZV, from + range_length);
     }
@@ -1810,7 +1589,7 @@ del_range_1 (EMACS_INT from, EMACS_INT to, int prepare, int ret_string)
   to_byte = CHAR_TO_BYTE (to);
 
   deletion = del_range_2 (from, from_byte, to, to_byte, ret_string);
-  GCPRO1(deletion);
+  GCPRO1 (deletion);
   signal_after_change (from, to - from, 0);
   update_compositions (from, from, CHECK_HEAD);
   UNGCPRO;
@@ -1820,9 +1599,9 @@ del_range_1 (EMACS_INT from, EMACS_INT to, int prepare, int ret_string)
 /* Like del_range_1 but args are byte positions, not char positions.  */
 
 void
-del_range_byte (EMACS_INT from_byte, EMACS_INT to_byte, int prepare)
+del_range_byte (ptrdiff_t from_byte, ptrdiff_t to_byte, int prepare)
 {
-  EMACS_INT from, to;
+  ptrdiff_t from, to;
 
   /* Make args be valid */
   if (from_byte < BEGV_BYTE)
@@ -1838,8 +1617,8 @@ del_range_byte (EMACS_INT from_byte, EMACS_INT to_byte, int prepare)
 
   if (prepare)
     {
-      EMACS_INT old_from = from, old_to = Z - to;
-      EMACS_INT range_length = to - from;
+      ptrdiff_t old_from = from, old_to = Z - to;
+      ptrdiff_t range_length = to - from;
       prepare_to_modify_buffer (from, to, &from);
       to = from + range_length;
 
@@ -1863,8 +1642,8 @@ del_range_byte (EMACS_INT from_byte, EMACS_INT to_byte, int prepare)
    and bytepos.  */
 
 void
-del_range_both (EMACS_INT from, EMACS_INT from_byte,
-               EMACS_INT to, EMACS_INT to_byte, int prepare)
+del_range_both (ptrdiff_t from, ptrdiff_t from_byte,
+               ptrdiff_t to, ptrdiff_t to_byte, int prepare)
 {
   /* Make args be valid */
   if (from_byte < BEGV_BYTE)
@@ -1882,8 +1661,8 @@ del_range_both (EMACS_INT from, EMACS_INT from_byte,
 
   if (prepare)
     {
-      EMACS_INT old_from = from, old_to = Z - to;
-      EMACS_INT range_length = to - from;
+      ptrdiff_t old_from = from, old_to = Z - to;
+      ptrdiff_t range_length = to - from;
       prepare_to_modify_buffer (from, to, &from);
       to = from + range_length;
 
@@ -1909,13 +1688,13 @@ del_range_both (EMACS_INT from, EMACS_INT from_byte,
    If RET_STRING is true, the deleted area is returned as a string. */
 
 Lisp_Object
-del_range_2 (EMACS_INT from, EMACS_INT from_byte,
-            EMACS_INT to, EMACS_INT to_byte, int ret_string)
+del_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
+            ptrdiff_t to, ptrdiff_t to_byte, int ret_string)
 {
-  register EMACS_INT nbytes_del, nchars_del;
+  register ptrdiff_t nbytes_del, nchars_del;
   Lisp_Object deletion;
 
-  CHECK_MARKERS ();
+  check_markers ();
 
   nchars_del = to - from;
   nbytes_del = to_byte - from_byte;
@@ -1932,7 +1711,7 @@ del_range_2 (EMACS_INT from, EMACS_INT from_byte,
     abort ();
 #endif
 
-  if (ret_string || ! EQ (current_buffer->undo_list, Qt))
+  if (ret_string || ! EQ (BVAR (current_buffer, undo_list), Qt))
     deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
   else
     deletion = Qnil;
@@ -1943,7 +1722,7 @@ del_range_2 (EMACS_INT from, EMACS_INT from_byte,
      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))
+  if (! EQ (BVAR (current_buffer, undo_list), Qt))
     record_delete (from, deletion);
   MODIFF++;
   CHARS_MODIFF = MODIFF;
@@ -1971,15 +1750,14 @@ del_range_2 (EMACS_INT from, EMACS_INT from_byte,
        needs to access the previous gap contents.  */
     *(GPT_ADDR) = 0;
 
-  if (GPT_BYTE < GPT)
-    abort ();
+  eassert (GPT <= GPT_BYTE);
 
   if (GPT - BEG < BEG_UNCHANGED)
     BEG_UNCHANGED = GPT - BEG;
   if (Z - GPT < END_UNCHANGED)
     END_UNCHANGED = Z - GPT;
 
-  CHECK_MARKERS ();
+  check_markers ();
 
   evaporate_overlays (from);
 
@@ -1996,7 +1774,7 @@ del_range_2 (EMACS_INT from, EMACS_INT from_byte,
    Otherwise set CHARS_MODIFF to the new value of MODIFF.  */
 
 void
-modify_region (struct buffer *buffer, EMACS_INT start, EMACS_INT end,
+modify_region (struct buffer *buffer, ptrdiff_t start, ptrdiff_t end,
               int preserve_chars_modiff)
 {
   struct buffer *old_buffer = current_buffer;
@@ -2014,7 +1792,7 @@ modify_region (struct buffer *buffer, EMACS_INT start, EMACS_INT end,
   if (! preserve_chars_modiff)
     CHARS_MODIFF = MODIFF;
 
-  buffer->point_before_scroll = Qnil;
+  BSET (buffer, point_before_scroll, Qnil);
 
   if (buffer != old_buffer)
     set_buffer_internal (old_buffer);
@@ -2031,12 +1809,12 @@ modify_region (struct buffer *buffer, EMACS_INT start, EMACS_INT end,
    by holding its value temporarily in a marker.  */
 
 void
-prepare_to_modify_buffer (EMACS_INT start, EMACS_INT end,
-                         EMACS_INT *preserve_ptr)
+prepare_to_modify_buffer (ptrdiff_t start, ptrdiff_t end,
+                         ptrdiff_t *preserve_ptr)
 {
   struct buffer *base_buffer;
 
-  if (!NILP (current_buffer->read_only))
+  if (!NILP (BVAR (current_buffer, read_only)))
     Fbarf_if_buffer_read_only ();
 
   /* Let redisplay consider other windows than selected_window
@@ -2044,7 +1822,7 @@ prepare_to_modify_buffer (EMACS_INT start, EMACS_INT end,
   if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
     ++windows_or_buffers_changed;
 
-  if (BUF_INTERVALS (current_buffer) != 0)
+  if (buffer_get_intervals (current_buffer))
     {
       if (preserve_ptr)
        {
@@ -2068,21 +1846,39 @@ prepare_to_modify_buffer (EMACS_INT start, EMACS_INT end,
     base_buffer = current_buffer;
 
 #ifdef CLASH_DETECTION
-  if (!NILP (base_buffer->file_truename)
+  if (!NILP (BVAR (base_buffer, file_truename))
       /* Make binding buffer-file-name to nil effective.  */
-      && !NILP (base_buffer->filename)
+      && !NILP (BVAR (base_buffer, filename))
       && SAVE_MODIFF >= MODIFF)
-    lock_file (base_buffer->file_truename);
+    lock_file (BVAR (base_buffer, file_truename));
 #else
   /* At least warn if this file has changed on disk since it was visited.  */
-  if (!NILP (base_buffer->filename)
+  if (!NILP (BVAR (base_buffer, filename))
       && SAVE_MODIFF >= MODIFF
       && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
-      && !NILP (Ffile_exists_p (base_buffer->filename)))
+      && !NILP (Ffile_exists_p (BVAR (base_buffer, filename))))
     call1 (intern ("ask-user-about-supersession-threat"),
-          base_buffer->filename);
+          BVAR (base_buffer,filename));
 #endif /* not CLASH_DETECTION */
 
+  /* If `select-active-regions' is non-nil, save the region text.  */
+  if (!NILP (BVAR (current_buffer, mark_active))
+      && !inhibit_modification_hooks
+      && XMARKER (BVAR (current_buffer, mark))->buffer
+      && NILP (Vsaved_region_selection)
+      && (EQ (Vselect_active_regions, Qonly)
+         ? EQ (CAR_SAFE (Vtransient_mark_mode), Qonly)
+         : (!NILP (Vselect_active_regions)
+            && !NILP (Vtransient_mark_mode))))
+    {
+      ptrdiff_t b = XMARKER (BVAR (current_buffer, mark))->charpos;
+      ptrdiff_t e = PT;
+      if (b < e)
+       Vsaved_region_selection = make_buffer_string (b, e, 0);
+      else if (b > e)
+       Vsaved_region_selection = make_buffer_string (e, b, 0);
+    }
+
   signal_before_change (start, end, preserve_ptr);
 
   if (current_buffer->newline_cache)
@@ -2129,9 +1925,8 @@ prepare_to_modify_buffer (EMACS_INT start, EMACS_INT end,
    VARIABLE is the variable to maybe set to nil.
    NO-ERROR-FLAG is nil if there was an error,
    anything else meaning no error (so this function does nothing).  */
-Lisp_Object
-reset_var_on_error (val)
-     Lisp_Object val;
+static Lisp_Object
+reset_var_on_error (Lisp_Object val)
 {
   if (NILP (XCDR (val)))
     Fset (XCAR (val), Qnil);
@@ -2144,15 +1939,15 @@ reset_var_on_error (val)
    If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
    by holding its value temporarily in a marker.  */
 
-void
-signal_before_change (EMACS_INT start_int, EMACS_INT end_int,
-                     EMACS_INT *preserve_ptr)
+static void
+signal_before_change (ptrdiff_t start_int, ptrdiff_t end_int,
+                     ptrdiff_t *preserve_ptr)
 {
   Lisp_Object start, end;
   Lisp_Object start_marker, end_marker;
   Lisp_Object preserve_marker;
   struct gcpro gcpro1, gcpro2, gcpro3;
-  int count = SPECPDL_INDEX ();
+  ptrdiff_t count = SPECPDL_INDEX ();
 
   if (inhibit_modification_hooks)
     return;
@@ -2166,14 +1961,14 @@ signal_before_change (EMACS_INT start_int, EMACS_INT end_int,
 
   specbind (Qinhibit_modification_hooks, Qt);
 
-  /* If buffer is unmodified, run a special hook for that case.  */
+  /* If buffer is unmodified, run a special hook for that case.  The
+   check for Vfirst_change_hook is just a minor optimization. */
   if (SAVE_MODIFF >= MODIFF
-      && !NILP (Vfirst_change_hook)
-      && !NILP (Vrun_hooks))
+      && !NILP (Vfirst_change_hook))
     {
       PRESERVE_VALUE;
       PRESERVE_START_END;
-      call1 (Vrun_hooks, Qfirst_change_hook);
+      Frun_hooks (1, &Qfirst_change_hook);
     }
 
   /* Now run the before-change-functions if any.  */
@@ -2198,7 +1993,7 @@ signal_before_change (EMACS_INT start_int, EMACS_INT end_int,
       XSETCDR (rvoe_arg, Qt);
     }
 
-  if (current_buffer->overlays_before || current_buffer->overlays_after)
+  if (buffer_has_overlays ())
     {
       PRESERVE_VALUE;
       report_overlay_modification (FETCH_START, FETCH_END, 0,
@@ -2223,9 +2018,9 @@ signal_before_change (EMACS_INT start_int, EMACS_INT end_int,
    after the change.  */
 
 void
-signal_after_change (EMACS_INT charpos, EMACS_INT lendel, EMACS_INT lenins)
+signal_after_change (ptrdiff_t charpos, ptrdiff_t lendel, ptrdiff_t lenins)
 {
-  int count = SPECPDL_INDEX ();
+  ptrdiff_t count = SPECPDL_INDEX ();
   if (inhibit_modification_hooks)
     return;
 
@@ -2234,8 +2029,7 @@ signal_after_change (EMACS_INT charpos, EMACS_INT lendel, EMACS_INT lenins)
      just record the args that we were going to use.  */
   if (! NILP (Vcombine_after_change_calls)
       && NILP (Vbefore_change_functions)
-      && !current_buffer->overlays_before
-      && !current_buffer->overlays_after)
+      && !buffer_has_overlays ())
     {
       Lisp_Object elt;
 
@@ -2277,7 +2071,7 @@ signal_after_change (EMACS_INT charpos, EMACS_INT lendel, EMACS_INT lenins)
       XSETCDR (rvoe_arg, Qt);
     }
 
-  if (current_buffer->overlays_before || current_buffer->overlays_after)
+  if (buffer_has_overlays ())
     report_overlay_modification (make_number (charpos),
                                 make_number (charpos + lenins),
                                 1,
@@ -2294,9 +2088,8 @@ signal_after_change (EMACS_INT charpos, EMACS_INT lendel, EMACS_INT lenins)
   unbind_to (count, Qnil);
 }
 
-Lisp_Object
-Fcombine_after_change_execute_1 (val)
-     Lisp_Object val;
+static Lisp_Object
+Fcombine_after_change_execute_1 (Lisp_Object val)
 {
   Vcombine_after_change_calls = val;
   return val;
@@ -2305,11 +2098,11 @@ Fcombine_after_change_execute_1 (val)
 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
        Scombine_after_change_execute, 0, 0, 0,
        doc: /* This function is for use internally in `combine-after-change-calls'.  */)
-     ()
+  (void)
 {
-  int count = SPECPDL_INDEX ();
-  EMACS_INT beg, end, change;
-  EMACS_INT begpos, endpos;
+  ptrdiff_t count = SPECPDL_INDEX ();
+  ptrdiff_t beg, end, change;
+  ptrdiff_t begpos, endpos;
   Lisp_Object tail;
 
   if (NILP (combine_after_change_list))
@@ -2320,7 +2113,7 @@ DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
      non-nil, and insertion calls a file handler (e.g. through
      lock_file) which scribbles into a temp file -- cyd  */
   if (!BUFFERP (combine_after_change_buffer)
-      || NILP (XBUFFER (combine_after_change_buffer)->name))
+      || NILP (BVAR (XBUFFER (combine_after_change_buffer), name)))
     {
       combine_after_change_list = Qnil;
       return Qnil;
@@ -2343,7 +2136,7 @@ DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
        tail = XCDR (tail))
     {
       Lisp_Object elt;
-      EMACS_INT thisbeg, thisend, thischange;
+      ptrdiff_t thisbeg, thisend, thischange;
 
       /* Extract the info from the next element.  */
       elt = XCAR (tail);
@@ -2388,30 +2181,23 @@ DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
 }
 \f
 void
-syms_of_insdel ()
+syms_of_insdel (void)
 {
   staticpro (&combine_after_change_list);
   staticpro (&combine_after_change_buffer);
   combine_after_change_list = Qnil;
   combine_after_change_buffer = Qnil;
 
-  DEFVAR_BOOL ("check-markers-debug-flag", &check_markers_debug_flag,
-              doc: /* Non-nil means enable debugging checks for invalid marker positions.  */);
-  check_markers_debug_flag = 0;
-  DEFVAR_LISP ("combine-after-change-calls", &Vcombine_after_change_calls,
+  DEFVAR_LISP ("combine-after-change-calls", Vcombine_after_change_calls,
               doc: /* Used internally by the `combine-after-change-calls' macro.  */);
   Vcombine_after_change_calls = Qnil;
 
-  DEFVAR_BOOL ("inhibit-modification-hooks", &inhibit_modification_hooks,
+  DEFVAR_BOOL ("inhibit-modification-hooks", inhibit_modification_hooks,
               doc: /* Non-nil means don't run any of the hooks that respond to buffer changes.
 This affects `before-change-functions' and `after-change-functions',
 as well as hooks attached to text properties and overlays.  */);
   inhibit_modification_hooks = 0;
-  Qinhibit_modification_hooks = intern_c_string ("inhibit-modification-hooks");
-  staticpro (&Qinhibit_modification_hooks);
+  DEFSYM (Qinhibit_modification_hooks, "inhibit-modification-hooks");
 
   defsubr (&Scombine_after_change_execute);
 }
-
-/* arch-tag: 9b34b886-47d7-465e-a234-299af411b23d
-   (do not change this comment) */