(xbufobjfwd, xbuflocal, xwinconfig):
[bpt/emacs.git] / src / buffer.c
index 1b4a885..78f7552 100644 (file)
@@ -164,6 +164,27 @@ DEFUN ("buffer-list", Fbuffer_list, Sbuffer_list, 0, 0, 0,
   return Fmapcar (Qcdr, Vbuffer_alist);
 }
 
+/* Like Fassoc, but use Fstring_equal to compare
+   (which ignores text properties),
+   and don't ever QUIT.  */
+
+static Lisp_Object
+assoc_ignore_text_properties (key, list)
+     register Lisp_Object key;
+     Lisp_Object list;
+{
+  register Lisp_Object tail;
+  for (tail = list; !NILP (tail); tail = Fcdr (tail))
+    {
+      register Lisp_Object elt, tem;
+      elt = Fcar (tail);
+      tem = Fstring_equal (Fcar (elt), key);
+      if (!NILP (tem))
+       return elt;
+    }
+  return Qnil;
+}
+
 DEFUN ("get-buffer", Fget_buffer, Sget_buffer, 1, 1, 0,
   "Return the buffer named NAME (a string).\n\
 If there is no live buffer named NAME, return nil.\n\
@@ -175,7 +196,7 @@ NAME may also be a buffer; if so, the value is that buffer.")
     return name;
   CHECK_STRING (name, 0);
 
-  return Fcdr (Fassoc (name, Vbuffer_alist));
+  return Fcdr (assoc_ignore_text_properties (name, Vbuffer_alist));
 }
 
 DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
@@ -234,6 +255,12 @@ The value is never nil.")
 
   b = (struct buffer *) xmalloc (sizeof (struct buffer));
 
+  b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
+
+  /* An ordinary buffer uses its own struct buffer_text.  */
+  b->text = &b->own_text;
+  b->base_buffer = 0;
+
   BUF_GAP_SIZE (b) = 20;
   BLOCK_INPUT;
   BUFFER_ALLOC (BUF_BEG_ADDR (b), BUF_GAP_SIZE (b));
@@ -247,6 +274,8 @@ The value is never nil.")
   BUF_ZV (b) = 1;
   BUF_Z (b) = 1;
   BUF_MODIFF (b) = 1;
+  BUF_SAVE_MODIFF (b) = 1;
+  BUF_INTERVALS (b) = 0;
 
   b->newline_cache = 0;
   b->width_run_cache = 0;
@@ -256,9 +285,16 @@ The value is never nil.")
   b->next = all_buffers;
   all_buffers = b;
 
-  b->mark = Fmake_marker ();
-  /*b->number = make_number (++buffer_count);*/
+  /* An ordinary buffer normally doesn't need markers
+     to handle BEGV and ZV.  */
+  b->pt_marker = Qnil;
+  b->begv_marker = Qnil;
+  b->zv_marker = Qnil;
+
+  name = Fcopy_sequence (name);
+  INITIALIZE_INTERVAL (XSTRING (name), NULL_INTERVAL);
   b->name = name;
+
   if (XSTRING (name)->data[0] != ' ')
     b->undo_list = Qnil;
   else
@@ -272,11 +308,100 @@ The value is never nil.")
   Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
 
   b->mark = Fmake_marker ();
-  b->markers = Qnil;
+  BUF_MARKERS (b) = Qnil;
   b->name = name;
   return buf;
 }
 
+DEFUN ("make-indirect-buffer",
+       Fmake_indirect_buffer, Smake_indirect_buffer, 2, 2,
+       "BMake indirect buffer: \nbIndirect to base buffer: ",
+  "Create and return an indirect buffer named NAME, with base buffer BASE.\n\
+BASE should be an existing buffer (or buffer name).")
+  (name, base_buffer)
+     register Lisp_Object name, base_buffer;
+{
+  register Lisp_Object buf;
+  register struct buffer *b;
+
+  buf = Fget_buffer (name);
+  if (!NILP (buf))
+    error ("Buffer name `%s' is in use", XSTRING (name)->data);
+
+  base_buffer = Fget_buffer (base_buffer);
+  if (NILP (base_buffer))
+    error ("No such buffer: `%s'",
+          XSTRING (XBUFFER (base_buffer)->name)->data);
+
+  if (XSTRING (name)->size == 0)
+    error ("Empty string for buffer name is not allowed");
+
+  b = (struct buffer *) xmalloc (sizeof (struct buffer));
+
+  b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
+
+  if (XBUFFER (base_buffer)->base_buffer)
+    b->base_buffer = XBUFFER (base_buffer)->base_buffer;
+  else
+    b->base_buffer = XBUFFER (base_buffer);
+
+  /* Use the base buffer's text object.  */
+  b->text = b->base_buffer->text;
+
+  BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
+  BUF_ZV (b) = BUF_ZV (b->base_buffer);
+  BUF_PT (b) = BUF_PT (b->base_buffer);
+
+  b->newline_cache = 0;
+  b->width_run_cache = 0;
+  b->width_table = Qnil;
+
+  /* Put this on the chain of all buffers including killed ones.  */
+  b->next = all_buffers;
+  all_buffers = b;
+
+  name = Fcopy_sequence (name);
+  INITIALIZE_INTERVAL (XSTRING (name), NULL_INTERVAL);
+  b->name = name;
+
+  reset_buffer (b);
+  reset_buffer_local_variables (b);
+
+  /* Put this in the alist of all live buffers.  */
+  XSETBUFFER (buf, b);
+  Vbuffer_alist = nconc2 (Vbuffer_alist, Fcons (Fcons (name, buf), Qnil));
+
+  b->mark = Fmake_marker ();
+  b->name = name;
+
+  /* Make sure the base buffer has markers for its narrowing.  */
+  if (NILP (b->base_buffer->pt_marker))
+    {
+      b->base_buffer->pt_marker = Fmake_marker ();
+      Fset_marker (b->base_buffer->pt_marker,
+                  make_number (BUF_PT (b->base_buffer)), base_buffer);
+    }
+  if (NILP (b->base_buffer->begv_marker))
+    {
+      b->base_buffer->begv_marker = Fmake_marker ();
+      Fset_marker (b->base_buffer->begv_marker,
+                  make_number (BUF_BEGV (b->base_buffer)), base_buffer);
+    }
+  if (NILP (b->base_buffer->zv_marker))
+    {
+      b->base_buffer->zv_marker = Fmake_marker ();
+      Fset_marker (b->base_buffer->zv_marker,
+                  make_number (BUF_ZV (b->base_buffer)), base_buffer);
+    }
+
+  /* Give the indirect buffer markers for its narrowing.  */
+  b->pt_marker = Fpoint_marker ();
+  b->begv_marker = Fpoint_min_marker ();
+  b->zv_marker = Fpoint_max_marker ();
+
+  return buf;
+}
+
 /* Reinitialize everything about a buffer except its name and contents
    and local variables.  */
 
@@ -287,7 +412,6 @@ reset_buffer (b)
   b->filename = Qnil;
   b->directory = (current_buffer) ? current_buffer->directory : Qnil;
   b->modtime = 0;
-  b->save_modified = 1;
   XSETFASTINT (b->save_length, 0);
   b->last_window_start = 1;
   b->backed_up = Qnil;
@@ -299,9 +423,6 @@ reset_buffer (b)
   b->overlays_after = Qnil;
   XSETFASTINT (b->overlay_center, 1);
   b->mark_active = Qnil;
-
-  /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
-  INITIALIZE_INTERVAL (b, NULL_INTERVAL);
 }
 
 /* Reset buffer B's local variables info.
@@ -327,6 +448,7 @@ reset_buffer_local_variables (b)
   b->upcase_table = Vascii_upcase_table;
   b->case_canon_table = Vascii_canon_table;
   b->case_eqv_table = Vascii_eqv_table;
+  b->buffer_file_type = Qnil;
 #if 0
   b->sort_table = XSTRING (Vascii_sort_table);
   b->folding_sort_table = XSTRING (Vascii_folding_sort_table);
@@ -415,6 +537,30 @@ No argument or nil as argument means use the current buffer.")
   return XBUFFER (buffer)->filename;
 }
 
+DEFUN ("buffer-base-buffer", Fbuffer_base_buffer, Sbuffer_base_buffer,
+       0, 1, 0,
+  "Return the base buffer of indirect buffer BUFFER.\n\
+If BUFFER is not indirect, return nil.")
+  (buffer)
+     register Lisp_Object buffer;
+{
+  struct buffer *base;
+  Lisp_Object base_buffer;
+
+  if (NILP (buffer))
+    base = current_buffer->base_buffer;
+  else
+    {
+      CHECK_BUFFER (buffer, 0);
+      base = XBUFFER (buffer)->base_buffer;
+    }
+
+  if (! base)
+    return Qnil;
+  XSETBUFFER (base_buffer, base);
+  return base_buffer;
+}
+
 DEFUN ("buffer-local-variables", Fbuffer_local_variables,
   Sbuffer_local_variables, 0, 1, 0,
   "Return an alist of variables that are buffer-local in BUFFER.\n\
@@ -504,7 +650,7 @@ No argument or nil as argument means use current buffer as BUFFER.")
       buf = XBUFFER (buffer);
     }
 
-  return buf->save_modified < BUF_MODIFF (buf) ? Qt : Qnil;
+  return BUF_SAVE_MODIFF (buf) < BUF_MODIFF (buf) ? Qt : Qnil;
 }
 
 DEFUN ("set-buffer-modified-p", Fset_buffer_modified_p, Sset_buffer_modified_p,
@@ -524,7 +670,7 @@ A non-nil FLAG means mark the buffer modified.")
   fn = current_buffer->filename;
   if (!NILP (fn))
     {
-      already = current_buffer->save_modified < MODIFF;
+      already = SAVE_MODIFF < MODIFF;
       if (!already && !NILP (flag))
        lock_file (fn);
       else if (already && NILP (flag))
@@ -532,7 +678,7 @@ A non-nil FLAG means mark the buffer modified.")
     }
 #endif /* CLASH_DETECTION */
 
-  current_buffer->save_modified = NILP (flag) ? MODIFF : 0;
+  SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
   update_mode_lines++;
   return flag;
 }
@@ -568,43 +714,43 @@ If UNIQUE is non-nil, come up with a new name using\n\
 Interactively, you can set UNIQUE with a prefix argument.\n\
 We return the name we actually gave the buffer.\n\
 This does not change the name of the visited file (if any).")
-  (name, unique)
-     register Lisp_Object name, unique;
+  (newname, unique)
+     register Lisp_Object newname, unique;
 {
   register Lisp_Object tem, buf;
 
-  CHECK_STRING (name, 0);
+  CHECK_STRING (newname, 0);
 
-  if (XSTRING (name)->size == 0)
+  if (XSTRING (newname)->size == 0)
     error ("Empty string is invalid as a buffer name");
 
-  tem = Fget_buffer (name);
+  tem = Fget_buffer (newname);
   /* Don't short-circuit if UNIQUE is t.  That is a useful way to rename
      the buffer automatically so you can create another with the original name.
      It makes UNIQUE equivalent to
-     (rename-buffer (generate-new-buffer-name NAME)).  */
+     (rename-buffer (generate-new-buffer-name NEWNAME)).  */
   if (NILP (unique) && XBUFFER (tem) == current_buffer)
     return current_buffer->name;
   if (!NILP (tem))
     {
       if (!NILP (unique))
-       name = Fgenerate_new_buffer_name (name, current_buffer->name);
+       newname = Fgenerate_new_buffer_name (newname, current_buffer->name);
       else
-       error ("Buffer name \"%s\" is in use", XSTRING (name)->data);
+       error ("Buffer name `%s' is in use", XSTRING (newname)->data);
     }
 
-  current_buffer->name = name;
+  current_buffer->name = newname;
 
   /* Catch redisplay's attention.  Unless we do this, the mode lines for
      any windows displaying current_buffer will stay unchanged.  */
   update_mode_lines++;
 
   XSETBUFFER (buf, current_buffer);
-  Fsetcar (Frassq (buf, Vbuffer_alist), name);
+  Fsetcar (Frassq (buf, Vbuffer_alist), newname);
   if (NILP (current_buffer->filename)
       && !NILP (current_buffer->auto_save_file_name))
     call0 (intern ("rename-auto-save-file"));
-  /* refetch since that last call may have done GC */
+  /* Refetch since that last call may have done GC.  */
   return current_buffer->name;
 }
 
@@ -627,6 +773,18 @@ If BUFFER is omitted or nil, some interesting buffer is returned.")
        continue;
       if (XSTRING (XBUFFER (buf)->name)->data[0] == ' ')
        continue;
+#ifdef MULTI_FRAME
+      /* If the selected frame has a buffer_predicate,
+        disregard buffers that don't fit the predicate.  */
+      tem = frame_buffer_predicate ();
+      if (!NILP (tem))
+       {
+         tem = call1 (tem, buf);
+         if (NILP (tem))
+           continue;
+       }
+#endif
+
       if (NILP (visible_ok))
        tem = Fget_buffer_window (buf, Qt);
       else
@@ -725,7 +883,7 @@ with `delete-process'.")
 
   /* Query if the buffer is still modified.  */
   if (INTERACTIVE && !NILP (b->filename)
-      && BUF_MODIFF (b) > b->save_modified)
+      && BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
     {
       GCPRO2 (buf, bufname);
       tem = do_yes_or_no_p (format1 ("Buffer %s modified; kill anyway? ",
@@ -770,6 +928,26 @@ with `delete-process'.")
   if (NILP (b->name))
     return Qnil;
 
+  /* When we kill a base buffer, kill all its indirect buffers.
+     We do it at this stage so nothing terrible happens if they
+     ask questions or their hooks get errors.  */
+  if (! b->base_buffer)
+    {
+      struct buffer *other;
+
+      GCPRO1 (buf);
+
+      for (other = all_buffers; other; other = other->next)
+       if (other->base_buffer == b)
+         {
+           Lisp_Object buf;
+           XSETBUFFER (buf, other);
+           Fkill_buffer (buf);
+         }
+
+      UNGCPRO;
+    }
+  
   /* Make this buffer not be current.
      In the process, notice if this is the sole visible buffer
      and give up if so.  */
@@ -803,27 +981,35 @@ with `delete-process'.")
       Lisp_Object tem;
       tem = Fsymbol_value (intern ("delete-auto-save-files"));
       if (! NILP (tem))
-       unlink (XSTRING (b->auto_save_file_name)->data);
+       internal_delete_file (b->auto_save_file_name);
     }
 
-  /* Unchain all markers of this buffer
-     and leave them pointing nowhere.  */
-  for (tem = b->markers; !EQ (tem, Qnil); )
+  if (! b->base_buffer)
     {
-      m = XMARKER (tem);
-      m->buffer = 0;
-      tem = m->chain;
-      m->chain = Qnil;
-    }
-  b->markers = Qnil;
+      /* Unchain all markers of this buffer
+        and leave them pointing nowhere.  */
+      for (tem = BUF_MARKERS (b); !EQ (tem, Qnil); )
+       {
+         m = XMARKER (tem);
+         m->buffer = 0;
+         tem = m->chain;
+         m->chain = Qnil;
+       }
+      BUF_MARKERS (b) = Qnil;
 
-  /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
-  INITIALIZE_INTERVAL (b, NULL_INTERVAL);
-  /* Perhaps we should explicitly free the interval tree here... */
+#ifdef USE_TEXT_PROPERTIES
+      BUF_INTERVALS (b) = NULL_INTERVAL;
+#endif
+
+      /* Perhaps we should explicitly free the interval tree here... */
+    }
 
   b->name = Qnil;
+
   BLOCK_INPUT;
-  BUFFER_FREE (BUF_BEG_ADDR (b));
+  if (! b->base_buffer)
+    BUFFER_FREE (BUF_BEG_ADDR (b));
+
   if (b->newline_cache)
     {
       free_region_cache (b->newline_cache);
@@ -992,6 +1178,49 @@ set_buffer_internal (b)
   current_buffer = b;
   last_known_column_point = -1;   /* invalidate indentation cache */
 
+  if (old_buf)
+    {
+      /* Put the undo list back in the base buffer, so that it appears
+        that an indirect buffer shares the undo list of its base.  */
+      if (old_buf->base_buffer)
+       old_buf->base_buffer->undo_list = old_buf->undo_list;
+
+      /* If the old current buffer has markers to record PT, BEGV and ZV
+        when it is not current, update them now.  */
+      if (! NILP (old_buf->pt_marker))
+       {
+         Lisp_Object obuf;
+         XSETBUFFER (obuf, old_buf);
+         Fset_marker (old_buf->pt_marker, BUF_PT (old_buf), obuf);
+       }
+      if (! NILP (old_buf->begv_marker))
+       {
+         Lisp_Object obuf;
+         XSETBUFFER (obuf, old_buf);
+         Fset_marker (old_buf->begv_marker, BUF_BEGV (old_buf), obuf);
+       }
+      if (! NILP (old_buf->zv_marker))
+       {
+         Lisp_Object obuf;
+         XSETBUFFER (obuf, old_buf);
+         Fset_marker (old_buf->zv_marker, BUF_ZV (old_buf), obuf);
+       }
+    }
+
+  /* Get the undo list from the base buffer, so that it appears
+     that an indirect buffer shares the undo list of its base.  */
+  if (b->base_buffer)
+    b->undo_list = b->base_buffer->undo_list;
+
+  /* If the new current buffer has markers to record PT, BEGV and ZV
+     when it is not current, fetch them now.  */
+  if (! NILP (b->pt_marker))
+    BUF_PT (b) = marker_position (b->pt_marker);
+  if (! NILP (b->begv_marker))
+    BUF_BEGV (b) = marker_position (b->begv_marker);
+  if (! NILP (b->zv_marker))
+    BUF_ZV (b) = marker_position (b->zv_marker);
+
   /* Look down buffer's list of local Lisp variables
      to find and update any that forward into C variables. */
 
@@ -1023,6 +1252,55 @@ set_buffer_internal (b)
       }
 }
 
+/* Switch to buffer B temporarily for redisplay purposes.
+   This avoids certain things thatdon't need to be done within redisplay.  */
+
+void
+set_buffer_temp (b)
+     struct buffer *b;
+{
+  register struct buffer *old_buf;
+
+  if (current_buffer == b)
+    return;
+
+  old_buf = current_buffer;
+  current_buffer = b;
+
+  if (old_buf)
+    {
+      /* If the old current buffer has markers to record PT, BEGV and ZV
+        when it is not current, update them now.  */
+      if (! NILP (old_buf->pt_marker))
+       {
+         Lisp_Object obuf;
+         XSETBUFFER (obuf, old_buf);
+         Fset_marker (old_buf->pt_marker, BUF_PT (old_buf), obuf);
+       }
+      if (! NILP (old_buf->begv_marker))
+       {
+         Lisp_Object obuf;
+         XSETBUFFER (obuf, old_buf);
+         Fset_marker (old_buf->begv_marker, BUF_BEGV (old_buf), obuf);
+       }
+      if (! NILP (old_buf->zv_marker))
+       {
+         Lisp_Object obuf;
+         XSETBUFFER (obuf, old_buf);
+         Fset_marker (old_buf->zv_marker, BUF_ZV (old_buf), obuf);
+       }
+    }
+
+  /* If the new current buffer has markers to record PT, BEGV and ZV
+     when it is not current, fetch them now.  */
+  if (! NILP (b->pt_marker))
+    BUF_PT (b) = marker_position (b->pt_marker);
+  if (! NILP (b->begv_marker))
+    BUF_BEGV (b) = marker_position (b->begv_marker);
+  if (! NILP (b->zv_marker))
+    BUF_ZV (b) = marker_position (b->zv_marker);
+}
+
 DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
   "Make the buffer BUFFER current for editing operations.\n\
 BUFFER may be a buffer or the name of an existing buffer.\n\
@@ -1129,117 +1407,6 @@ validate_region (b, e)
     args_out_of_range (*b, *e);
 }
 \f
-static Lisp_Object
-list_buffers_1 (files)
-     Lisp_Object files;
-{
-  register Lisp_Object tail, tem, buf;
-  Lisp_Object col1, col2, col3, minspace;
-  register struct buffer *old = current_buffer, *b;
-  Lisp_Object desired_point;
-  Lisp_Object other_file_symbol;
-
-  desired_point = Qnil;
-  other_file_symbol = intern ("list-buffers-directory");
-
-  XSETFASTINT (col1, 19);
-  XSETFASTINT (col2, 26);
-  XSETFASTINT (col3, 40);
-  XSETFASTINT (minspace, 1);
-
-  Fset_buffer (Vstandard_output);
-  Fbuffer_disable_undo (Vstandard_output);
-  current_buffer->read_only = Qnil;
-
-  write_string ("\
- MR Buffer         Size   Mode          File\n\
- -- ------         ----   ----          ----\n", -1);
-
-  for (tail = Vbuffer_alist; !NILP (tail); tail = Fcdr (tail))
-    {
-      buf = Fcdr (Fcar (tail));
-      b = XBUFFER (buf);
-      /* Don't mention the minibuffers. */
-      if (XSTRING (b->name)->data[0] == ' ')
-       continue;
-      /* Optionally don't mention buffers that lack files. */
-      if (!NILP (files) && NILP (b->filename))
-       continue;
-      /* Identify the current buffer. */
-      if (b == old)
-       XSETFASTINT (desired_point, point);
-      write_string (b == old ? "." : " ", -1);
-      /* Identify modified buffers */
-      write_string (BUF_MODIFF (b) > b->save_modified ? "*" : " ", -1);
-      /* The current buffer is special-cased to be marked read-only.
-        It is actually made read-only by the call to
-        Buffer-menu-mode, below. */
-      write_string ((b != current_buffer && NILP (b->read_only))
-                   ? "  " : "% ", -1);
-      Fprinc (b->name, Qnil);
-      Findent_to (col1, make_number (2));
-      XSETFASTINT (tem, BUF_Z (b) - BUF_BEG (b));
-      Fprin1 (tem, Qnil);
-      Findent_to (col2, minspace);
-      Fprinc (b->mode_name, Qnil);
-      Findent_to (col3, minspace);
-
-      if (!NILP (b->filename))
-       Fprinc (b->filename, Qnil);
-      else
-       {
-         /* No visited file; check local value of list-buffers-directory.  */
-         Lisp_Object tem;
-         set_buffer_internal (b);
-         tem = Fboundp (other_file_symbol);
-         if (!NILP (tem))
-           {
-             tem = Fsymbol_value (other_file_symbol);
-             Fset_buffer (Vstandard_output);
-             if (STRINGP (tem))
-               Fprinc (tem, Qnil);
-           }
-         else
-           Fset_buffer (Vstandard_output);
-       }
-      write_string ("\n", -1);
-    }
-
-  tail = intern ("Buffer-menu-mode");
-  if ((tem = Ffboundp (tail), !NILP (tem)))
-    call0 (tail);
-  set_buffer_internal (old);
-  return desired_point;
-}
-
-DEFUN ("list-buffers", Flist_buffers, Slist_buffers, 0, 1, "P",
-  "Display a list of names of existing buffers.\n\
-The list is displayed in a buffer named `*Buffer List*'.\n\
-Note that buffers with names starting with spaces are omitted.\n\
-Non-null optional arg FILES-ONLY means mention only file buffers.\n\
-\n\
-The M column contains a * for buffers that are modified.\n\
-The R column contains a % for buffers that are read-only.")
-  (files)
-     Lisp_Object files;
-{
-  Lisp_Object desired_point;
-
-  desired_point
-    = internal_with_output_to_temp_buffer ("*Buffer List*",
-                                          list_buffers_1, files);
-
-  if (NUMBERP (desired_point))
-    {
-      int count = specpdl_ptr - specpdl;
-      record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
-      Fset_buffer (build_string ("*Buffer List*"));
-      SET_PT (XINT (desired_point));
-      return unbind_to (count, Qnil);
-    }
-  return Qnil;
-}
-
 DEFUN ("kill-all-local-variables", Fkill_all_local_variables, Skill_all_local_variables,
   0, 0, 0,
   "Switch to Fundamental mode by killing current buffer's local variables.\n\
@@ -1273,25 +1440,26 @@ the normal hook `change-major-mode-hook'.")
       sym = XCONS (XCONS (alist)->car)->car;
 
       /* Need not do anything if some other buffer's binding is now encached.  */
-      tem = XCONS (XCONS (XSYMBOL (sym)->value)->cdr)->car;
+      tem = XCONS (XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->cdr)->car;
       if (XBUFFER (tem) == current_buffer)
        {
          /* Symbol is set up for this buffer's old local value.
             Set it up for the current buffer with the default value.  */
 
-         tem = XCONS (XCONS (XSYMBOL (sym)->value)->cdr)->cdr;
+         tem = XCONS (XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->cdr)->cdr;
          /* Store the symbol's current value into the alist entry
             it is currently set up for.  This is so that, if the
             local is marked permanent, and we make it local again below,
             we don't lose the value.  */
          XCONS (XCONS (tem)->car)->cdr
-           = do_symval_forwarding (XCONS (XSYMBOL (sym)->value)->car);
+           = do_symval_forwarding (XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->car);
          /* Switch to the symbol's default-value alist entry.  */
          XCONS (tem)->car = tem;
          /* Mark it as current for the current buffer.  */
-         XCONS (XCONS (XSYMBOL (sym)->value)->cdr)->car = Fcurrent_buffer ();
+         XCONS (XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->cdr)->car
+           = Fcurrent_buffer ();
          /* Store the current value into any forwarding in the symbol.  */
-         store_symval_forwarding (sym, XCONS (XSYMBOL (sym)->value)->car,
+         store_symval_forwarding (sym, XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->car,
                                   XCONS (tem)->cdr);
        }
     }
@@ -1360,14 +1528,12 @@ overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
   int inhibit_storing = 0;
 
   for (tail = current_buffer->overlays_before;
-       XGCTYPE (tail) == Lisp_Cons;
+       GC_CONSP (tail);
        tail = XCONS (tail)->cdr)
     {
       int startpos, endpos;
 
       overlay = XCONS (tail)->car;
-      if (XGCTYPE (overlay) != Lisp_Overlay)
-       abort ();
 
       start = OVERLAY_START (overlay);
       end = OVERLAY_END (overlay);
@@ -1407,14 +1573,12 @@ overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
     }
 
   for (tail = current_buffer->overlays_after;
-       XGCTYPE (tail) == Lisp_Cons;
+       GC_CONSP (tail);
        tail = XCONS (tail)->cdr)
     {
       int startpos, endpos;
 
       overlay = XCONS (tail)->car;
-      if (XGCTYPE (overlay) != Lisp_Overlay)
-       abort ();
 
       start = OVERLAY_START (overlay);
       end = OVERLAY_END (overlay);
@@ -1843,8 +2007,11 @@ BEG and END may be integers or markers.")
   beg = Fset_marker (Fmake_marker (), beg, buffer);
   end = Fset_marker (Fmake_marker (), end, buffer);
 
-  overlay = Fcons (Fcons (beg, end), Qnil);
-  XSETTYPE (overlay, Lisp_Overlay);
+  overlay = allocate_misc ();
+  XMISC (overlay)->type = Lisp_Misc_Overlay;
+  XOVERLAY (overlay)->start = beg;
+  XOVERLAY (overlay)->end = end;
+  XOVERLAY (overlay)->plist = Qnil;
 
   /* Put the new overlay on the wrong list.  */ 
   end = OVERLAY_END (overlay);
@@ -2048,7 +2215,7 @@ OVERLAY.")
 {
   CHECK_OVERLAY (overlay, 0);
 
-  return Fcopy_sequence (Fcdr_safe (XCONS (overlay)->cdr));
+  return Fcopy_sequence (XOVERLAY (overlay)->plist);
 }
 
 \f
@@ -2202,7 +2369,7 @@ DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
 
   fallback = Qnil;
 
-  for (plist = Fcdr_safe (XCONS (overlay)->cdr);
+  for (plist = XOVERLAY (overlay)->plist;
        CONSP (plist) && CONSP (XCONS (plist)->cdr);
        plist = XCONS (XCONS (plist)->cdr)->cdr)
     {
@@ -2225,16 +2392,14 @@ DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
   (overlay, prop, value)
      Lisp_Object overlay, prop, value;
 {
-  Lisp_Object plist, tail, buffer;
+  Lisp_Object tail, buffer;
   int changed;
 
   CHECK_OVERLAY (overlay, 0);
 
   buffer = Fmarker_buffer (OVERLAY_START (overlay));
 
-  plist = Fcdr_safe (XCONS (overlay)->cdr);
-
-  for (tail = plist;
+  for (tail = XOVERLAY (overlay)->plist;
        CONSP (tail) && CONSP (XCONS (tail)->cdr);
        tail = XCONS (XCONS (tail)->cdr)->cdr)
     if (EQ (XCONS (tail)->car, prop))
@@ -2245,9 +2410,8 @@ DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
       }
   /* It wasn't in the list, so add it to the front.  */
   changed = !NILP (value);
-  if (! CONSP (XCONS (overlay)->cdr))
-    XCONS (overlay)->cdr = Fcons (Qnil, Qnil);
-  XCONS (XCONS (overlay)->cdr)->cdr = Fcons (prop, Fcons (value, plist));
+  XOVERLAY (overlay)->plist
+    = Fcons (prop, Fcons (value, XOVERLAY (overlay)->plist));
  found:
   if (! NILP (buffer))
     {
@@ -2266,20 +2430,27 @@ DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
 /* Run the modification-hooks of overlays that include
    any part of the text in START to END.
    Run the insert-before-hooks of overlay starting at END,
-   and the insert-after-hooks of overlay ending at START.  */
+   and the insert-after-hooks of overlay ending at START.
+
+   This is called both before and after the modification.
+   AFTER is nonzero when we call after the modification.
+
+   ARG1, ARG2, ARG3 are arguments to pass to the hook functions.  */
 
 void
-verify_overlay_modification (start, end)
+report_overlay_modification (start, end, after, arg1, arg2, arg3)
      Lisp_Object start, end;
+     int after;
+     Lisp_Object arg1, arg2, arg3;
 {
   Lisp_Object prop, overlay, tail;
   int insertion = EQ (start, end);
   int tail_copied;
-  struct gcpro gcpro1, gcpro2;
+  struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
 
   overlay = Qnil;
   tail = Qnil;
-  GCPRO2 (overlay, tail);
+  GCPRO5 (overlay, tail, arg1, arg2, arg3);
 
   tail_copied = 0;
   for (tail = current_buffer->overlays_before;
@@ -2306,7 +2477,7 @@ verify_overlay_modification (start, end)
              if (!tail_copied)
                tail = Fcopy_sequence (tail);
              tail_copied = 1;
-             call_overlay_mod_hooks (prop, overlay, start, end);
+             call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
            }
        }
       if (XFASTINT (start) == endpos && insertion)
@@ -2317,7 +2488,7 @@ verify_overlay_modification (start, end)
              if (!tail_copied)
                tail = Fcopy_sequence (tail);
              tail_copied = 1;
-             call_overlay_mod_hooks (prop, overlay, start, end);
+             call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
            }
        }
       /* Test for intersecting intervals.  This does the right thing
@@ -2330,7 +2501,7 @@ verify_overlay_modification (start, end)
              if (!tail_copied)
                tail = Fcopy_sequence (tail);
              tail_copied = 1;
-             call_overlay_mod_hooks (prop, overlay, start, end);
+             call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
            }
        }
     }
@@ -2359,7 +2530,7 @@ verify_overlay_modification (start, end)
              if (!tail_copied)
                tail = Fcopy_sequence (tail);
              tail_copied = 1;
-             call_overlay_mod_hooks (prop, overlay, start, end);
+             call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
            }
        }
       if (XFASTINT (start) == endpos && insertion)
@@ -2370,7 +2541,7 @@ verify_overlay_modification (start, end)
              if (!tail_copied)
                tail = Fcopy_sequence (tail);
              tail_copied = 1;
-             call_overlay_mod_hooks (prop, overlay, start, end);
+             call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
            }
        }
       /* Test for intersecting intervals.  This does the right thing
@@ -2383,7 +2554,7 @@ verify_overlay_modification (start, end)
              if (!tail_copied)
                tail = Fcopy_sequence (tail);
              tail_copied = 1;
-             call_overlay_mod_hooks (prop, overlay, start, end);
+             call_overlay_mod_hooks (prop, overlay, after, arg1, arg2, arg3);
            }
        }
     }
@@ -2392,14 +2563,19 @@ verify_overlay_modification (start, end)
 }
 
 static void
-call_overlay_mod_hooks (list, overlay, start, end)
-     Lisp_Object list, overlay, start, end;
+call_overlay_mod_hooks (list, overlay, after, arg1, arg2, arg3)
+     Lisp_Object list, overlay;
+     int after;
+     Lisp_Object arg1, arg2, arg3;
 {
-  struct gcpro gcpro1;
-  GCPRO1 (list);
+  struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
+  GCPRO4 (list, arg1, arg2, arg3);
   while (!NILP (list))
     {
-      call3 (Fcar (list), overlay, start, end);
+      if (NILP (arg3))
+       call4 (Fcar (list), overlay, after ? Qt : Qnil, arg1, arg2);
+      else
+       call5 (Fcar (list), overlay, after ? Qt : Qnil, arg1, arg2, arg3);
       list = Fcdr (list);
     }
   UNGCPRO;
@@ -2476,6 +2652,13 @@ init_buffer_once ()
   reset_buffer_local_variables (&buffer_defaults);
   reset_buffer (&buffer_local_symbols);
   reset_buffer_local_variables (&buffer_local_symbols);
+  /* Prevent GC from getting confused.  */
+  buffer_defaults.text = &buffer_defaults.own_text;
+  buffer_local_symbols.text = &buffer_local_symbols.own_text;
+#ifdef USE_TEXT_PROPERTIES
+  BUF_INTERVALS (&buffer_defaults) = 0;
+  BUF_INTERVALS (&buffer_local_symbols) = 0;
+#endif
   XSETBUFFER (Vbuffer_defaults, &buffer_defaults);
   XSETBUFFER (Vbuffer_local_symbols, &buffer_local_symbols);
 
@@ -2799,7 +2982,10 @@ and this buffer is not full-frame width.");
 #ifdef DOS_NT
   DEFVAR_PER_BUFFER ("buffer-file-type", &current_buffer->buffer_file_type,
                     Qnil,
-    "*If visited file is text, nil; otherwise, t.");
+    "Non-nil if the visited file is a binary file.\n\
+This variable is meaningful on MS-DOG and Windows NT.\n\
+On those systems, it is automatically local in every buffer.\n\
+On other systems, this variable is normally always nil.")
 #endif
 
   DEFVAR_PER_BUFFER ("default-directory", &current_buffer->directory,
@@ -2982,15 +3168,15 @@ An entry (TEXT . POSITION) represents the deletion of the string TEXT\n\
 from (abs POSITION).  If POSITION is positive, point was at the front\n\
 of the text being deleted; if negative, point was at the end.\n\
 \n\
-An entry (t HIGHWORD LOWWORD) indicates that the buffer had been\n\
-previously unmodified.  HIGHWORD and LOWWORD are the high and low\n\
-16-bit words of the buffer's modification count at the time.  If the\n\
-modification count of the most recent save is different, this entry is\n\
+An entry (t HIGH . LOW) indicates that the buffer previously had\n\
+\"unmodified\" status.  HIGH and LOW are the high and low 16-bit portions\n\
+of the visited file's modification time, as of that time.  If the\n\
+modification time of the most recent save is different, this entry is\n\
 obsolete.\n\
 \n\
-An entry (nil PROP VAL BEG . END) indicates that a text property\n\
-was modified between BEG and END.  PROP is the property name,\n\
-and VAL is the old value.\n\
+An entry (nil PROPERTY VALUE BEG . END) indicates that a text property\n\
+was modified between BEG and END.  PROPERTY is the property name,\n\
+and VALUE is the old value.\n\
 \n\
 An entry of the form POSITION indicates that point was at the buffer\n\
 location given by the integer.  Undoing an entry of this form places\n\
@@ -3057,10 +3243,12 @@ is a member of the list.");
   defsubr (&Sget_buffer);
   defsubr (&Sget_file_buffer);
   defsubr (&Sget_buffer_create);
+  defsubr (&Smake_indirect_buffer);
   defsubr (&Sgenerate_new_buffer_name);
   defsubr (&Sbuffer_name);
 /*defsubr (&Sbuffer_number);*/
   defsubr (&Sbuffer_file_name);
+  defsubr (&Sbuffer_base_buffer);
   defsubr (&Sbuffer_local_variables);
   defsubr (&Sbuffer_modified_p);
   defsubr (&Sset_buffer_modified_p);
@@ -3078,7 +3266,6 @@ is a member of the list.");
   defsubr (&Sset_buffer);
   defsubr (&Sbarf_if_buffer_read_only);
   defsubr (&Sbury_buffer);
-  defsubr (&Slist_buffers);
   defsubr (&Skill_all_local_variables);
 
   defsubr (&Soverlayp);
@@ -3102,7 +3289,6 @@ keys_of_buffer ()
 {
   initial_define_key (control_x_map, 'b', "switch-to-buffer");
   initial_define_key (control_x_map, 'k', "kill-buffer");
-  initial_define_key (control_x_map, Ctl ('B'), "list-buffers");
 
   /* This must not be in syms_of_buffer, because Qdisabled is not
      initialized when that function gets called.  */