Finish making it fit Emacs conventions. Add autoloads.
[bpt/emacs.git] / src / buffer.c
index da3bfb3..d4c60f9 100644 (file)
@@ -1,5 +1,5 @@
 /* Buffer manipulation primitives for GNU Emacs.
-   Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993, 1994, 1995, 1997
+   Copyright (C) 1985,86,87,88,89,93,94,95,97,98, 1999
        Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
@@ -23,6 +23,9 @@ Boston, MA 02111-1307, USA.  */
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/param.h>
+#include <errno.h>
+
+extern int errno;
 
 #ifndef MAXPATHLEN
 /* in 4.1, param.h fails to define this. */
@@ -30,6 +33,12 @@ Boston, MA 02111-1307, USA.  */
 #endif /* not MAXPATHLEN */
 
 #include <config.h>
+#ifdef STDC_HEADERS
+#include <stdlib.h>
+#endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
 #include "lisp.h"
 #include "intervals.h"
 #include "window.h"
@@ -97,8 +106,11 @@ static Lisp_Object Vbuffer_local_symbols;
    buffer-local slots.  If a slot contains Qnil, then the
    corresponding buffer slot may contain a value of any type.  If a
    slot contains an integer, then prospective values' tags must be
-   equal to that integer.  When a tag does not match, the function
-   buffer_slot_type_mismatch will signal an error.  */
+   equal to that integer (except nil is always allowed).
+   When a tag does not match, the function
+   buffer_slot_type_mismatch will signal an error.
+
+   If a slot here contains -1, the corresponding variable is read-only.  */
 struct buffer buffer_local_types;
 
 /* Flags indicating which built-in buffer-local variables
@@ -141,6 +153,9 @@ Lisp_Object Qfirst_change_hook;
 Lisp_Object Qbefore_change_functions;
 Lisp_Object Qafter_change_functions;
 
+/* If nonzero, all modification hooks are suppressed.  */
+int inhibit_modification_hooks;
+
 Lisp_Object Qfundamental_mode, Qmode_class, Qpermanent_local;
 
 Lisp_Object Qprotected_field;
@@ -162,6 +177,7 @@ Lisp_Object Qinsert_behind_hooks;
 /* For debugging; temporary.  See set_buffer_internal.  */
 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
 
+void
 nsberror (spec)
      Lisp_Object spec;
 {
@@ -339,15 +355,26 @@ The value is never nil.")
   BUF_BEGV (b) = 1;
   BUF_ZV (b) = 1;
   BUF_Z (b) = 1;
+  BUF_PT_BYTE (b) = 1;
+  BUF_GPT_BYTE (b) = 1;
+  BUF_BEGV_BYTE (b) = 1;
+  BUF_ZV_BYTE (b) = 1;
+  BUF_Z_BYTE (b) = 1;
   BUF_MODIFF (b) = 1;
   BUF_OVERLAY_MODIFF (b) = 1;
   BUF_SAVE_MODIFF (b) = 1;
   BUF_INTERVALS (b) = 0;
+  BUF_UNCHANGED_MODIFIED (b) = 1;
+  BUF_OVERLAY_UNCHANGED_MODIFIED (b) = 1;
+  BUF_END_UNCHANGED (b) = 0;
+  BUF_BEG_UNCHANGED (b) = 0;
   *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'.  */
 
   b->newline_cache = 0;
   b->width_run_cache = 0;
   b->width_table = Qnil;
+  b->minibuffer_prompt_length = Qnil;
+  b->prevent_redisplay_optimizations_p = 1;
 
   /* Put this on the chain of all buffers including killed ones.  */
   b->next = all_buffers;
@@ -419,10 +446,14 @@ NAME should be a string which is not the name of an existing buffer.")
   BUF_BEGV (b) = BUF_BEGV (b->base_buffer);
   BUF_ZV (b) = BUF_ZV (b->base_buffer);
   BUF_PT (b) = BUF_PT (b->base_buffer);
+  BUF_BEGV_BYTE (b) = BUF_BEGV_BYTE (b->base_buffer);
+  BUF_ZV_BYTE (b) = BUF_ZV_BYTE (b->base_buffer);
+  BUF_PT_BYTE (b) = BUF_PT_BYTE (b->base_buffer);
 
   b->newline_cache = 0;
   b->width_run_cache = 0;
   b->width_table = Qnil;
+  b->minibuffer_prompt_length = Qnil;
 
   /* Put this on the chain of all buffers including killed ones.  */
   b->next = all_buffers;
@@ -442,35 +473,40 @@ NAME should be a string which is not the name of an existing buffer.")
   b->mark = Fmake_marker ();
   b->name = name;
 
+  /* The multibyte status belongs to the base buffer.  */
+  b->enable_multibyte_characters = b->base_buffer->enable_multibyte_characters;
+
   /* 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);
+      set_marker_both (b->base_buffer->pt_marker, base_buffer,
+                      BUF_PT (b->base_buffer),
+                      BUF_PT_BYTE (b->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);
+      set_marker_both (b->base_buffer->begv_marker, base_buffer,
+                      BUF_BEGV (b->base_buffer),
+                      BUF_BEGV_BYTE (b->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);
+      set_marker_both (b->base_buffer->zv_marker, base_buffer,
+                      BUF_ZV (b->base_buffer),
+                      BUF_ZV_BYTE (b->base_buffer));
       XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
     }
 
   /* Give the indirect buffer markers for its narrowing.  */
   b->pt_marker = Fmake_marker ();
-  Fset_marker (b->pt_marker, make_number (BUF_PT (b)), buf);
+  set_marker_both (b->pt_marker, buf, BUF_PT (b), BUF_PT_BYTE (b));
   b->begv_marker = Fmake_marker ();
-  Fset_marker (b->begv_marker, make_number (BUF_BEGV (b)), buf);
+  set_marker_both (b->begv_marker, buf, BUF_BEGV (b), BUF_BEGV_BYTE (b));
   b->zv_marker = Fmake_marker ();
-  Fset_marker (b->zv_marker, make_number (BUF_ZV (b)), buf);
-
+  set_marker_both (b->zv_marker, buf, BUF_ZV (b), BUF_ZV_BYTE (b));
   XMARKER (b->zv_marker)->insertion_type = 1;
 
   return buf;
@@ -490,7 +526,8 @@ reset_buffer (b)
   XSETFASTINT (b->save_length, 0);
   b->last_window_start = 1;
   /* It is more conservative to start out "changed" than "unchanged".  */
-  b->clip_changed = 1;
+  b->clip_changed = 0;
+  b->prevent_redisplay_optimizations_p = 1;
   b->backed_up = Qnil;
   b->auto_save_modified = 0;
   b->auto_save_failure_time = -1;
@@ -504,8 +541,10 @@ reset_buffer (b)
   b->file_format = Qnil;
   b->last_selected_window = Qnil;
   XSETINT (b->display_count, 0);
+  b->display_time = Qnil;
   b->extra2 = Qnil;
   b->extra3 = Qnil;
+  b->enable_multibyte_characters = buffer_defaults.enable_multibyte_characters;
 }
 
 /* Reset buffer B's local variables info.
@@ -779,7 +818,8 @@ A non-nil FLAG means mark the buffer modified.")
      If buffer becoming unmodified, unlock the file.  */
 
   fn = current_buffer->file_truename;
-  if (!NILP (fn))
+  /* Test buffer-file-name so that binding it to nil is effective.  */
+  if (!NILP (fn) && ! NILP (current_buffer->filename))
     {
       already = SAVE_MODIFF < MODIFF;
       if (!already && !NILP (flag))
@@ -865,26 +905,31 @@ This does not change the name of the visited file (if any).")
   return current_buffer->name;
 }
 
-DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 2, 0,
+DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 3, 0,
   "Return most recently selected buffer other than BUFFER.\n\
 Buffers not visible in windows are preferred to visible buffers,\n\
 unless optional second argument VISIBLE-OK is non-nil.\n\
+If the optional third argument FRAME is non-nil, use that frame's\n\
+buffer list instead of the selected frame's buffer list.\n\
 If no other buffer exists, the buffer `*scratch*' is returned.\n\
 If BUFFER is omitted or nil, some interesting buffer is returned.")
-  (buffer, visible_ok)
-     register Lisp_Object buffer, visible_ok;
+  (buffer, visible_ok, frame)
+     register Lisp_Object buffer, visible_ok, frame;
 {
   Lisp_Object Fset_buffer_major_mode ();
   register Lisp_Object tail, buf, notsogood, tem, pred, add_ons;
   notsogood = Qnil;
 
+  if (NILP (frame))
+    frame = Fselected_frame ();
+
   tail = Vbuffer_alist;
-  pred = frame_buffer_predicate ();
+  pred = frame_buffer_predicate (frame);
 
   /* Consider buffers that have been seen in the selected frame
      before other buffers.  */
     
-  tem = frame_buffer_list ();
+  tem = frame_buffer_list (frame);
   add_ons = Qnil;
   while (CONSP (tem))
     {
@@ -1086,7 +1131,7 @@ with SIGHUP.")
      and give up if so.  */
   if (b == current_buffer)
     {
-      tem = Fother_buffer (buf, Qnil);
+      tem = Fother_buffer (buf, Qnil, Qnil);
       Fset_buffer (tem);
       if (b == current_buffer)
        return Qnil;
@@ -1111,7 +1156,7 @@ with SIGHUP.")
   /* Delete any auto-save file, if we saved it in this session.  */
   if (STRINGP (b->auto_save_file_name)
       && b->auto_save_modified != 0
-      && SAVE_MODIFF < b->auto_save_modified)
+      && BUF_SAVE_MODIFF (b) < b->auto_save_modified)
     {
       Lisp_Object tem;
       tem = Fsymbol_value (intern ("delete-auto-save-files"));
@@ -1178,6 +1223,7 @@ with SIGHUP.")
       b->width_run_cache = 0;
     }
   b->width_table = Qnil;
+  b->minibuffer_prompt_length = Qnil;
   UNBLOCK_INPUT;
   b->undo_list = Qnil;
 
@@ -1189,10 +1235,13 @@ with SIGHUP.")
    selected buffers are always closer to the front of the list.  This
    means that other_buffer is more likely to choose a relevant buffer.  */
 
+void
 record_buffer (buf)
      Lisp_Object buf;
 {
   register Lisp_Object link, prev;
+  Lisp_Object frame;
+  frame = Fselected_frame ();
 
   prev = Qnil;
   for (link = Vbuffer_alist; CONSP (link); link = XCONS (link)->cdr)
@@ -1216,7 +1265,8 @@ record_buffer (buf)
   /* Now move this buffer to the front of frame_buffer_list also.  */
 
   prev = Qnil;
-  for (link = frame_buffer_list (); CONSP (link); link = XCONS (link)->cdr)
+  for (link = frame_buffer_list (frame); CONSP (link);
+       link = XCONS (link)->cdr)
     {
       if (EQ (XCONS (link)->car, buf))
        break;
@@ -1228,15 +1278,16 @@ record_buffer (buf)
   if (CONSP (link))
     {
       if (NILP (prev))
-       set_frame_buffer_list (XCONS (frame_buffer_list ())->cdr);
+       set_frame_buffer_list (frame,
+                              XCONS (frame_buffer_list (frame))->cdr);
       else
        XCONS (prev)->cdr = XCONS (XCONS (prev)->cdr)->cdr;
        
-      XCONS (link)->cdr = frame_buffer_list ();
-      set_frame_buffer_list (link);
+      XCONS (link)->cdr = frame_buffer_list (frame);
+      set_frame_buffer_list (frame, link);
     }
   else
-    set_frame_buffer_list (Fcons (buf, frame_buffer_list ()));
+    set_frame_buffer_list (frame, Fcons (buf, frame_buffer_list (frame)));
 }
 
 DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
@@ -1269,29 +1320,33 @@ the current buffer's major mode.")
   return unbind_to (count, Qnil);
 }
 
-DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2, "BSwitch to buffer: ",
-  "Select buffer BUFFER in the current window.\n\
-BUFFER may be a buffer or a buffer name.\n\
-Optional second arg NORECORD non-nil means\n\
-do not put this buffer at the front of the list of recently selected ones.\n\
-\n\
-WARNING: This is NOT the way to work on another buffer temporarily\n\
-within a Lisp program!  Use `set-buffer' instead.  That avoids messing with\n\
-the window-buffer correspondences.")
-  (buffer, norecord)
-     Lisp_Object buffer, norecord;
+/* If switching buffers in WINDOW would be an error, return
+   a C string saying what the error would be.  */
+
+char *
+no_switch_window (window)
+     Lisp_Object window;
 {
-  register Lisp_Object buf;
   Lisp_Object tem;
-
-  if (EQ (minibuf_window, selected_window))
-    error ("Cannot switch buffers in minibuffer window");
-  tem = Fwindow_dedicated_p (selected_window);
+  if (EQ (minibuf_window, window))
+    return "Cannot switch buffers in minibuffer window";
+  tem = Fwindow_dedicated_p (window);
   if (!NILP (tem))
-    error ("Cannot switch buffers in a dedicated window");
+    return "Cannot switch buffers in a dedicated window";
+  return NULL;
+}
+
+/* Switch to buffer BUFFER in the selected window.
+   If NORECORD is non-nil, don't call record_buffer.  */
+
+Lisp_Object
+switch_to_buffer_1 (buffer, norecord)
+     Lisp_Object buffer, norecord;
+{
+  register Lisp_Object buf;
 
   if (NILP (buffer))
-    buf = Fother_buffer (Fcurrent_buffer (), Qnil);
+    buf = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
   else
     {
       buf = Fget_buffer (buffer);
@@ -1313,6 +1368,26 @@ the window-buffer correspondences.")
   return buf;
 }
 
+DEFUN ("switch-to-buffer", Fswitch_to_buffer, Sswitch_to_buffer, 1, 2, "BSwitch to buffer: ",
+  "Select buffer BUFFER in the current window.\n\
+BUFFER may be a buffer or a buffer name.\n\
+Optional second arg NORECORD non-nil means\n\
+do not put this buffer at the front of the list of recently selected ones.\n\
+\n\
+WARNING: This is NOT the way to work on another buffer temporarily\n\
+within a Lisp program!  Use `set-buffer' instead.  That avoids messing with\n\
+the window-buffer correspondences.")
+  (buffer, norecord)
+     Lisp_Object buffer, norecord;
+{
+  char *err;
+
+  err = no_switch_window (selected_window);
+  if (err) error (err);
+
+  return switch_to_buffer_1 (buffer, norecord);
+}
+
 DEFUN ("pop-to-buffer", Fpop_to_buffer, Spop_to_buffer, 1, 3, 0,
   "Select buffer BUFFER in some window, preferably a different one.\n\
 If BUFFER is nil, then some other buffer is chosen.\n\
@@ -1329,7 +1404,7 @@ do not put this buffer at the front of the list of recently selected ones.")
 {
   register Lisp_Object buf;
   if (NILP (buffer))
-    buf = Fother_buffer (Fcurrent_buffer (), Qnil);
+    buf = Fother_buffer (Fcurrent_buffer (), Qnil, Qnil);
   else
     {
       buf = Fget_buffer (buffer);
@@ -1342,7 +1417,7 @@ do not put this buffer at the front of the list of recently selected ones.")
   Fset_buffer (buf);
   if (NILP (norecord))
     record_buffer (buf);
-  Fselect_window (Fdisplay_buffer (buf, other_window));
+  Fselect_window (Fdisplay_buffer (buf, other_window, Qnil));
   return buf;
 }
 
@@ -1355,21 +1430,21 @@ DEFUN ("current-buffer", Fcurrent_buffer, Scurrent_buffer, 0, 0, 0,
   return buf;
 }
 \f
-/* Set the current buffer to B.  */
+/* Set the current buffer to B.
+
+   We previously set windows_or_buffers_changed here to invalidate
+   global unchanged information in beg_unchanged and end_unchanged.
+   This is no longer necessary because we now compute unchanged
+   information on a buffer-basis.  Every action affecting other
+   windows than the selected one requires a select_window at some
+   time, and that increments windows_or_buffers_changed.  */
 
 void
 set_buffer_internal (b)
      register struct buffer *b;
 {
-  register struct buffer *old_buf;
-  register Lisp_Object tail, valcontents;
-  Lisp_Object tem;
-
-  if (current_buffer == b)
-    return;
-
-  windows_or_buffers_changed = 1;
-  set_buffer_internal_1 (b);
+  if (current_buffer != b)
+    set_buffer_internal_1 (b);
 }
 
 /* Set the current buffer to B, and do not set windows_or_buffers_changed.
@@ -1403,22 +1478,22 @@ set_buffer_internal_1 (b)
        {
          Lisp_Object obuf;
          XSETBUFFER (obuf, old_buf);
-         Fset_marker (old_buf->pt_marker, make_number (BUF_PT (old_buf)),
-                      obuf);
+         set_marker_both (old_buf->pt_marker, obuf,
+                          BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
        }
       if (! NILP (old_buf->begv_marker))
        {
          Lisp_Object obuf;
          XSETBUFFER (obuf, old_buf);
-         Fset_marker (old_buf->begv_marker, make_number (BUF_BEGV (old_buf)),
-                      obuf);
+         set_marker_both (old_buf->begv_marker, obuf,
+                          BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
        }
       if (! NILP (old_buf->zv_marker))
        {
          Lisp_Object obuf;
          XSETBUFFER (obuf, old_buf);
-         Fset_marker (old_buf->zv_marker, make_number (BUF_ZV (old_buf)),
-                      obuf);
+         set_marker_both (old_buf->zv_marker, obuf,
+                          BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
        }
     }
 
@@ -1430,11 +1505,20 @@ set_buffer_internal_1 (b)
   /* 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);
+    {
+      BUF_PT (b) = marker_position (b->pt_marker);
+      BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
+    }
   if (! NILP (b->begv_marker))
-    BUF_BEGV (b) = marker_position (b->begv_marker);
+    {
+      BUF_BEGV (b) = marker_position (b->begv_marker);
+      BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
+    }
   if (! NILP (b->zv_marker))
-    BUF_ZV (b) = marker_position (b->zv_marker);
+    {
+      BUF_ZV (b) = marker_position (b->zv_marker);
+      BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
+    }
 
   /* Look down buffer's list of local Lisp variables
      to find and update any that forward into C variables. */
@@ -1444,7 +1528,7 @@ set_buffer_internal_1 (b)
       valcontents = XSYMBOL (XCONS (XCONS (tail)->car)->car)->value;
       if ((BUFFER_LOCAL_VALUEP (valcontents)
           || SOME_BUFFER_LOCAL_VALUEP (valcontents))
-         && (tem = XBUFFER_LOCAL_VALUE (valcontents)->car,
+         && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
              (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
        /* Just reference the variable
             to cause it to become set for this buffer.  */
@@ -1459,7 +1543,7 @@ set_buffer_internal_1 (b)
        valcontents = XSYMBOL (XCONS (XCONS (tail)->car)->car)->value;
        if ((BUFFER_LOCAL_VALUEP (valcontents)
             || SOME_BUFFER_LOCAL_VALUEP (valcontents))
-           && (tem = XBUFFER_LOCAL_VALUE (valcontents)->car,
+           && (tem = XBUFFER_LOCAL_VALUE (valcontents)->realvalue,
                (BOOLFWDP (tem) || INTFWDP (tem) || OBJFWDP (tem))))
          /* Just reference the variable
                to cause it to become set for this buffer.  */
@@ -1490,33 +1574,42 @@ set_buffer_temp (b)
        {
          Lisp_Object obuf;
          XSETBUFFER (obuf, old_buf);
-         Fset_marker (old_buf->pt_marker, make_number (BUF_PT (old_buf)),
-                      obuf);
+         set_marker_both (old_buf->pt_marker, obuf,
+                          BUF_PT (old_buf), BUF_PT_BYTE (old_buf));
        }
       if (! NILP (old_buf->begv_marker))
        {
          Lisp_Object obuf;
          XSETBUFFER (obuf, old_buf);
-         Fset_marker (old_buf->begv_marker, make_number (BUF_BEGV (old_buf)),
-                      obuf);
+         set_marker_both (old_buf->begv_marker, obuf,
+                          BUF_BEGV (old_buf), BUF_BEGV_BYTE (old_buf));
        }
       if (! NILP (old_buf->zv_marker))
        {
          Lisp_Object obuf;
          XSETBUFFER (obuf, old_buf);
-         Fset_marker (old_buf->zv_marker, make_number (BUF_ZV (old_buf)),
-                      obuf);
+         set_marker_both (old_buf->zv_marker, obuf,
+                          BUF_ZV (old_buf), BUF_ZV_BYTE (old_buf));
        }
     }
 
   /* 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);
+    {
+      BUF_PT (b) = marker_position (b->pt_marker);
+      BUF_PT_BYTE (b) = marker_byte_position (b->pt_marker);
+    }
   if (! NILP (b->begv_marker))
-    BUF_BEGV (b) = marker_position (b->begv_marker);
+    {
+      BUF_BEGV (b) = marker_position (b->begv_marker);
+      BUF_BEGV_BYTE (b) = marker_byte_position (b->begv_marker);
+    }
   if (! NILP (b->zv_marker))
-    BUF_ZV (b) = marker_position (b->zv_marker);
+    {
+      BUF_ZV (b) = marker_position (b->zv_marker);
+      BUF_ZV_BYTE (b) = marker_byte_position (b->zv_marker);
+    }
 }
 
 DEFUN ("set-buffer", Fset_buffer, Sset_buffer, 1, 1, 0,
@@ -1538,6 +1631,17 @@ Use `switch-to-buffer' or `pop-to-buffer' to switch buffers permanently.")
   set_buffer_internal (XBUFFER (buf));
   return buf;
 }
+
+/* Set the current buffer to BUFFER provided it is alive.  */
+
+Lisp_Object
+set_buffer_if_live (buffer)
+     Lisp_Object buffer;
+{
+  if (! NILP (XBUFFER (buffer)->name))
+    Fset_buffer (buffer);
+  return Qnil;
+}
 \f
 DEFUN ("barf-if-buffer-read-only", Fbarf_if_buffer_read_only,
                                   Sbarf_if_buffer_read_only, 0, 0, 0,
@@ -1566,7 +1670,7 @@ selected window if it is displayed there.")
       XSETBUFFER (buffer, current_buffer);
 
       /* If we're burying the current buffer, unshow it.  */
-      Fswitch_to_buffer (Fother_buffer (buffer, Qnil), Qnil);
+      Fswitch_to_buffer (Fother_buffer (buffer, Qnil, Qnil), Qnil);
     }
   else
     {
@@ -1610,6 +1714,7 @@ so the buffer is truly empty after this.")
   return Qnil;
 }
 
+void
 validate_region (b, e)
      register Lisp_Object *b, *e;
 {
@@ -1627,6 +1732,173 @@ validate_region (b, e)
     args_out_of_range (*b, *e);
 }
 \f
+/* Advance BYTE_POS up to a character boundary
+   and return the adjusted position.  */
+
+static int
+advance_to_char_boundary (byte_pos)
+     int byte_pos;
+{
+  int c;
+
+  if (byte_pos == BEG)
+    /* Beginning of buffer is always a character boundary.  */
+    return 1;
+
+  c = FETCH_BYTE (byte_pos);
+  if (! CHAR_HEAD_P (c))
+    {
+      /* We should advance BYTE_POS only when C is a constituent of a
+         multibyte sequence.  */
+      DEC_POS (byte_pos);
+      INC_POS (byte_pos);
+      /* If C is a constituent of a multibyte sequence, BYTE_POS was
+         surely advance to the correct character boundary.  If C is
+         not, BYTE_POS was unchanged.  */
+    }
+
+  return byte_pos;
+}
+
+DEFUN ("set-buffer-multibyte", Fset_buffer_multibyte, Sset_buffer_multibyte,
+       1, 1, 0,
+  "Set the multibyte flag of the current buffer to FLAG.\n\
+If FLAG is t, this makes the buffer a multibyte buffer.\n\
+If FLAG is nil, this makes the buffer a single-byte buffer.\n\
+The buffer contents remain unchanged as a sequence of bytes\n\
+but the contents viewed as characters do change.")
+  (flag)
+     Lisp_Object flag;
+{
+  Lisp_Object tail, markers;
+  struct buffer *other;
+
+  if (current_buffer->base_buffer)
+    error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
+
+  /* Do nothing if nothing actually changes.  */
+  if (NILP (flag) == NILP (current_buffer->enable_multibyte_characters))
+    return flag;
+
+  /* It would be better to update the list,
+     but this is good enough for now.  */
+  if (! EQ (current_buffer->undo_list, Qt))
+    current_buffer->undo_list = Qnil;
+
+  /* If the cached position is for this buffer, clear it out.  */
+  clear_charpos_cache (current_buffer);
+
+  if (NILP (flag))
+    {
+      /* Do this first, so it can use CHAR_TO_BYTE
+        to calculate the old correspondences.  */
+      set_intervals_multibyte (0);
+
+      current_buffer->enable_multibyte_characters = Qnil;
+
+      Z = Z_BYTE;
+      BEGV = BEGV_BYTE;
+      ZV = ZV_BYTE;
+      GPT = GPT_BYTE;
+      TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
+
+      tail = BUF_MARKERS (current_buffer);
+      while (XSYMBOL (tail) != XSYMBOL (Qnil))
+       {
+         XMARKER (tail)->charpos = XMARKER (tail)->bytepos;
+         tail = XMARKER (tail)->chain;
+       }
+    }
+  else
+    {
+      /* Be sure not to have a multibyte sequence striding over the GAP.
+        Ex: We change this: "...abc\201\241\241 _GAP_ \241\241\241..."
+            to: "...abc _GAP_ \201\241\241\241\241\241..."  */
+
+      if (GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
+         && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
+       {
+         unsigned char *p = GPT_ADDR - 1;
+
+         while (! CHAR_HEAD_P (*p) && p > BEG_ADDR) p--;
+         if (BASE_LEADING_CODE_P (*p))
+           {
+             int new_gpt = GPT_BYTE - (GPT_ADDR - p);
+
+             move_gap_both (new_gpt, new_gpt);
+           }
+       }
+
+      /* Do this first, so that chars_in_text asks the right question.
+        set_intervals_multibyte needs it too.  */
+      current_buffer->enable_multibyte_characters = Qt;
+
+      GPT_BYTE = advance_to_char_boundary (GPT_BYTE);
+      GPT = chars_in_text (BEG_ADDR, GPT_BYTE - BEG_BYTE) + BEG;
+
+      Z = chars_in_text (GAP_END_ADDR, Z_BYTE - GPT_BYTE) + GPT;
+
+      BEGV_BYTE = advance_to_char_boundary (BEGV_BYTE);
+      if (BEGV_BYTE > GPT_BYTE)
+       BEGV = chars_in_text (GAP_END_ADDR, BEGV_BYTE - GPT_BYTE) + GPT;
+      else
+       BEGV = chars_in_text (BEG_ADDR, BEGV_BYTE - BEG_BYTE) + BEG;
+
+      ZV_BYTE = advance_to_char_boundary (ZV_BYTE);
+      if (ZV_BYTE > GPT_BYTE)
+       ZV = chars_in_text (GAP_END_ADDR, ZV_BYTE - GPT_BYTE) + GPT;
+      else
+       ZV = chars_in_text (BEG_ADDR, ZV_BYTE - BEG_BYTE) + BEG;
+
+      {
+       int pt_byte = advance_to_char_boundary (PT_BYTE);
+       int pt;
+
+       if (pt_byte > GPT_BYTE)
+         pt = chars_in_text (GAP_END_ADDR, pt_byte - GPT_BYTE) + GPT;
+       else
+         pt = chars_in_text (BEG_ADDR, pt_byte - BEG_BYTE) + BEG;
+       TEMP_SET_PT_BOTH (pt, pt_byte);
+      }
+
+      tail = markers = BUF_MARKERS (current_buffer);
+
+      /* This prevents BYTE_TO_CHAR (that is, buf_bytepos_to_charpos) from
+        getting confused by the markers that have not yet been updated.
+        It is also a signal that it should never create a marker.  */
+      BUF_MARKERS (current_buffer) = Qnil;
+
+      while (XSYMBOL (tail) != XSYMBOL (Qnil))
+       {
+         XMARKER (tail)->bytepos
+           = advance_to_char_boundary (XMARKER (tail)->bytepos);
+         XMARKER (tail)->charpos = BYTE_TO_CHAR (XMARKER (tail)->bytepos);
+
+         tail = XMARKER (tail)->chain;
+       }
+
+      /* Make sure no markers were put on the chain
+        while the chain value was incorrect.  */
+      if (! EQ (BUF_MARKERS (current_buffer), Qnil))
+       abort ();
+
+      BUF_MARKERS (current_buffer) = markers;
+
+      /* Do this last, so it can calculate the new correspondences
+        between chars and bytes.  */
+      set_intervals_multibyte (1);
+    }
+
+  /* Copy this buffer's new multibyte status
+     into all of its indirect buffers.  */
+  for (other = all_buffers; other; other = other->next)
+    if (other->base_buffer == current_buffer && !NILP (other->name))
+      other->enable_multibyte_characters
+       = current_buffer->enable_multibyte_characters;
+
+  return flag;
+}
+\f
 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\
@@ -1703,26 +1975,26 @@ swap_out_buffer_local_variables (b)
       sym = XCONS (XCONS (alist)->car)->car;
 
       /* Need not do anything if some other buffer's binding is now encached.  */
-      tem = XCONS (XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->cdr)->car;
+      tem = XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->buffer;
       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 (XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->cdr)->cdr;
+         tem = XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->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
             later in Fkill_all_local_variables, we don't lose the value.  */
          XCONS (XCONS (tem)->car)->cdr
-           = do_symval_forwarding (XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->car);
+           = do_symval_forwarding (XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->realvalue);
          /* Switch to the symbol's default-value alist entry.  */
          XCONS (tem)->car = tem;
          /* Mark it as current for buffer B.  */
-         XCONS (XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->cdr)->car
-           = buffer;
+         XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->buffer = buffer;
          /* Store the current value into any forwarding in the symbol.  */
-         store_symval_forwarding (sym, XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->car,
+         store_symval_forwarding (sym,
+                                  XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->realvalue,
                                   XCONS (tem)->cdr);
        }
     }
@@ -1734,7 +2006,8 @@ swap_out_buffer_local_variables (b)
    Store in *NEXT_PTR the next position after POS where an overlay starts,
      or ZV if there are no more overlays.
    Store in *PREV_PTR the previous position before POS where an overlay ends,
-     or BEGV if there are no previous overlays.
+     or where an overlay starts which ends at or after POS;
+     or BEGV if there are no such overlays.
    NEXT_PTR and/or PREV_PTR may be 0, meaning don't store that info.
 
    *VEC_PTR and *LEN_PTR should contain a valid vector and size
@@ -1779,9 +2052,13 @@ overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
            prev = endpos;
          break;
        }
+      startpos = OVERLAY_POSITION (start);
+      /* This one ends at or after POS
+        so its start counts for PREV_PTR if it's before POS.  */
+      if (prev < startpos && startpos < pos)
+       prev = startpos;
       if (endpos == pos)
        continue;
-      startpos = OVERLAY_POSITION (start);
       if (startpos <= pos)
        {
          if (idx == len)
@@ -1790,7 +2067,11 @@ overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
                 Either make it bigger, or don't store any more in it.  */
              if (extend)
                {
-                 *len_ptr = len *= 2;
+                 /* Make it work with an initial len == 0.  */
+                 len *= 2;
+                 if (len == 0)
+                   len = 4;
+                 *len_ptr = len;
                  vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
                  *vec_ptr = vec;
                }
@@ -1832,6 +2113,8 @@ overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
              if (extend)
                {
                  *len_ptr = len *= 2;
+                 if (len == 0)
+                   len = *len_ptr = 4;
                  vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
                  *vec_ptr = vec;
                }
@@ -1842,9 +2125,14 @@ overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr, prev_ptr)
          if (!inhibit_storing)
            vec[idx] = overlay;
          idx++;
+
+         if (startpos < pos && startpos > prev)
+           prev = startpos;
        }
       else if (endpos < pos && endpos > prev)
        prev = endpos;
+      else if (endpos == pos && startpos > prev)
+       prev = startpos;
     }
 
   if (next_ptr)
@@ -2035,9 +2323,11 @@ struct sortvec
 };
 
 static int
-compare_overlays (s1, s2)
-     struct sortvec *s1, *s2;
+compare_overlays (v1, v2)
+     const void *v1, *v2;
 {
+  const struct sortvec *s1 = (const struct sortvec *) v1;
+  const struct sortvec *s2 = (const struct sortvec *) v2;
   if (s1->priority != s2->priority)
     return s1->priority - s2->priority;
   if (s1->beg != s2->beg)
@@ -2152,6 +2442,8 @@ record_overlay_string (ssl, str, str2, pri, size)
      Lisp_Object str, str2, pri;
      int size;
 {
+  int nbytes;
+
   if (ssl->used == ssl->size)
     {
       if (ssl->buf)
@@ -2166,9 +2458,29 @@ record_overlay_string (ssl, str, str2, pri, size)
   ssl->buf[ssl->used].size = size;
   ssl->buf[ssl->used].priority = (INTEGERP (pri) ? XINT (pri) : 0);
   ssl->used++;
-  ssl->bytes += XSTRING (str)->size;
+
+  if (NILP (current_buffer->enable_multibyte_characters))
+    nbytes = XSTRING (str)->size;
+  else if (! STRING_MULTIBYTE (str))
+    nbytes = count_size_as_multibyte (XSTRING (str)->data,
+                                     STRING_BYTES (XSTRING (str)));
+  else
+    nbytes = STRING_BYTES (XSTRING (str));
+
+  ssl->bytes += nbytes;
+
   if (STRINGP (str2))
-    ssl->bytes += XSTRING (str2)->size;
+    {
+      if (NILP (current_buffer->enable_multibyte_characters))
+       nbytes = XSTRING (str2)->size;
+      else if (! STRING_MULTIBYTE (str2))
+       nbytes = count_size_as_multibyte (XSTRING (str2)->data,
+                                         STRING_BYTES (XSTRING (str2)));
+      else
+       nbytes = STRING_BYTES (XSTRING (str2));
+
+      ssl->bytes += nbytes;
+    }
 }
 
 /* Return the concatenation of the strings associated with overlays that
@@ -2191,6 +2503,7 @@ overlay_strings (pos, w, pstr)
 {
   Lisp_Object ov, overlay, window, str;
   int startpos, endpos;
+  int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
 
   overlay_heads.used = overlay_heads.bytes = 0;
   overlay_tails.used = overlay_tails.bytes = 0;
@@ -2274,20 +2587,28 @@ overlay_strings (pos, w, pstr)
       p = overlay_str_buf;
       for (i = overlay_tails.used; --i >= 0;)
        {
+         int nbytes;
          tem = overlay_tails.buf[i].string;
-         bcopy (XSTRING (tem)->data, p, XSTRING (tem)->size);
-         p += XSTRING (tem)->size;
+         nbytes = copy_text (XSTRING (tem)->data, p,
+                             STRING_BYTES (XSTRING (tem)),
+                             STRING_MULTIBYTE (tem), multibyte);
+         p += nbytes;
        }
       for (i = 0; i < overlay_heads.used; ++i)
        {
+         int nbytes;
          tem = overlay_heads.buf[i].string;
-         bcopy (XSTRING (tem)->data, p, XSTRING (tem)->size);
-         p += XSTRING (tem)->size;
+         nbytes = copy_text (XSTRING (tem)->data, p,
+                             STRING_BYTES (XSTRING (tem)),
+                             STRING_MULTIBYTE (tem), multibyte);
+         p += nbytes;
          tem = overlay_heads.buf[i].string2;
          if (STRINGP (tem))
            {
-             bcopy (XSTRING (tem)->data, p, XSTRING (tem)->size);
-             p += XSTRING (tem)->size;
+             nbytes = copy_text (XSTRING (tem)->data, p,
+                                 STRING_BYTES (XSTRING (tem)),
+                                 STRING_MULTIBYTE (tem), multibyte);
+             p += nbytes;
            }
        }
       if (p != overlay_str_buf + total)
@@ -2614,6 +2935,7 @@ fix_overlays_in_range (start, end)
    `overlays_before' of the buffer *BP.  Before the insertion, `point'
    was at PREV, and now is at POS.  */
 
+void
 fix_overlays_before (bp, prev, pos)
      struct buffer *bp;
      int prev, pos;
@@ -2772,30 +3094,13 @@ modify_overlay (buf, start, end)
      we must do other windows.  */
   if (buf != XBUFFER (XWINDOW (selected_window)->buffer))
     windows_or_buffers_changed = 1;
-  /* If it's not current, we can't use beg_unchanged, end_unchanged for it.  */
-  else if (buf != current_buffer)
-    windows_or_buffers_changed = 1;
   /* If multiple windows show this buffer, we must do other windows.  */
   else if (buffer_shared > 1)
     windows_or_buffers_changed = 1;
   else
-    {
-      if (unchanged_modified == MODIFF
-         && overlay_unchanged_modified == OVERLAY_MODIFF)
-       {
-         beg_unchanged = start - BEG;
-         end_unchanged = Z - end;
-       }
-      else
-       {
-         if (Z - end < end_unchanged)
-           end_unchanged = Z - end;
-         if (start - BEG < beg_unchanged)
-           beg_unchanged = start - BEG;
-       }
-    }
+    BUF_COMPUTE_UNCHANGED (buf, start, end);
 
-  ++OVERLAY_MODIFF;
+  ++BUF_OVERLAY_MODIFF (buf);
 }
 
 \f\f
@@ -2929,8 +3234,8 @@ DEFUN ("delete-overlay", Fdelete_overlay, Sdelete_overlay, 1, 1, 0,
   b->overlays_after  = Fdelq (overlay, b->overlays_after);
 
   modify_overlay (b,
-                   marker_position (OVERLAY_START (overlay)),
-                   marker_position (OVERLAY_END   (overlay)));
+                 marker_position (OVERLAY_START (overlay)),
+                 marker_position (OVERLAY_END   (overlay)));
 
   Fset_marker (OVERLAY_START (overlay), Qnil, Qnil);
   Fset_marker (OVERLAY_END   (overlay), Qnil, Qnil);
@@ -3109,39 +3414,10 @@ If there are no more overlay boundaries before POS, return (point-min).")
 
   /* Put all the overlays we want in a vector in overlay_vec.
      Store the length in len.
-     prevpos gets the position of an overlay end.  */
+     prevpos gets the position of the previous change.  */
   noverlays = overlays_at (XINT (pos), 1, &overlay_vec, &len,
                           (int *) 0, &prevpos);
 
-  /* If any of these overlays starts after prevpos,
-     maybe use its starting point instead.  */
-  for (i = 0; i < noverlays; i++)
-    {
-      Lisp_Object ostart;
-      int ostartpos;
-
-      ostart = OVERLAY_START (overlay_vec[i]);
-      ostartpos = OVERLAY_POSITION (ostart);
-      if (ostartpos > prevpos && ostartpos < XINT (pos))
-       prevpos = ostartpos;
-    }
-
-  /* If any overlay ends at pos, consider its starting point too.  */
-  for (tail = current_buffer->overlays_before;
-       GC_CONSP (tail);
-       tail = XCONS (tail)->cdr)
-    {
-      Lisp_Object overlay, ostart;
-      int ostartpos;
-
-      overlay = XCONS (tail)->car;
-
-      ostart = OVERLAY_START (overlay);
-      ostartpos = OVERLAY_POSITION (ostart);
-      if (ostartpos > prevpos && ostartpos < XINT (pos))
-       prevpos = ostartpos;
-    }
-
   xfree (overlay_vec);
   return make_number (prevpos);
 }
@@ -3523,7 +3799,8 @@ evaporate_overlays (pos)
 }
 \f
 /* Somebody has tried to store a value with an unacceptable type
-   into the buffer-local slot with offset OFFSET.  */
+   in the slot with offset OFFSET.  */
+
 void
 buffer_slot_type_mismatch (offset)
      int offset;
@@ -3536,14 +3813,16 @@ buffer_slot_type_mismatch (offset)
     case Lisp_Int:     type_name = "integers";  break;
     case Lisp_String:  type_name = "strings";   break;
     case Lisp_Symbol:  type_name = "symbols";   break;
+
     default:
       abort ();
     }
 
-  error ("only %s should be stored in the buffer-local variable %s",
+  error ("Only %s should be stored in the buffer-local variable %s",
         type_name, XSYMBOL (sym)->name->data);
 }
 \f
+void
 init_buffer_once ()
 {
   register Lisp_Object tem;
@@ -3571,6 +3850,7 @@ init_buffer_once ()
 
   /* real setup is done in loaddefs.el */
   buffer_defaults.mode_line_format = build_string ("%-");
+  buffer_defaults.header_line_format = Qnil;
   buffer_defaults.abbrev_mode = Qnil;
   buffer_defaults.overwrite_mode = Qnil;
   buffer_defaults.case_fold_search = Qt;
@@ -3603,6 +3883,10 @@ init_buffer_once ()
   buffer_defaults.cache_long_line_scans = Qnil;
   buffer_defaults.file_truename = Qnil;
   XSETFASTINT (buffer_defaults.display_count, 0);
+  buffer_defaults.indicate_empty_lines = Qnil;
+  buffer_defaults.scroll_up_aggressively = Qnil;
+  buffer_defaults.scroll_down_aggressively = Qnil;
+  buffer_defaults.display_time = Qnil;
 
   /* Assign the local-flags to the slots that have default values.
      The local flag is a bit that is used in the buffer
@@ -3629,6 +3913,8 @@ init_buffer_once ()
   XSETINT (buffer_local_flags.invisibility_spec, -1);
   XSETINT (buffer_local_flags.file_format, -1);
   XSETINT (buffer_local_flags.display_count, -1);
+  XSETINT (buffer_local_flags.display_time, -1);
+  XSETINT (buffer_local_flags.enable_multibyte_characters, -1);
 
   XSETFASTINT (buffer_local_flags.mode_line_format, 1);
   XSETFASTINT (buffer_local_flags.abbrev_mode, 2);
@@ -3655,12 +3941,15 @@ init_buffer_once ()
   XSETFASTINT (buffer_local_flags.cache_long_line_scans, 0x10000);
   XSETFASTINT (buffer_local_flags.category_table, 0x20000);
   XSETFASTINT (buffer_local_flags.direction_reversed, 0x40000);
-  XSETFASTINT (buffer_local_flags.enable_multibyte_characters, 0x80000);
+  XSETFASTINT (buffer_local_flags.buffer_file_coding_system, 0x80000);
   /* Make this one a permanent local.  */
   buffer_permanent_local_flags |= 0x80000;
-  XSETFASTINT (buffer_local_flags.buffer_file_coding_system, 0x100000);
-  /* Make this one a permanent local.  */
-  buffer_permanent_local_flags |= 0x100000;
+  XSETFASTINT (buffer_local_flags.left_margin_width, 0x100000);
+  XSETFASTINT (buffer_local_flags.right_margin_width, 0x200000);
+  XSETFASTINT (buffer_local_flags.indicate_empty_lines, 0x400000);
+  XSETFASTINT (buffer_local_flags.scroll_up_aggressively, 0x800000);
+  XSETFASTINT (buffer_local_flags.scroll_down_aggressively, 0x1000000);
+  XSETFASTINT (buffer_local_flags.header_line_format, 0x2000000);
   
   Vbuffer_alist = Qnil;
   current_buffer = 0;
@@ -3685,8 +3974,11 @@ init_buffer_once ()
   Vbuffer_alist = Qnil;
 
   Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
+
+  inhibit_modification_hooks = 0;
 }
 
+void
 init_buffer ()
 {
   char buf[MAXPATHLEN+1];
@@ -3696,10 +3988,13 @@ init_buffer ()
   int rc;
 
   Fset_buffer (Fget_buffer_create (build_string ("*scratch*")));
+  if (NILP (buffer_defaults.enable_multibyte_characters))
+    Fset_buffer_multibyte (Qnil);
 
   /* If PWD is accurate, use it instead of calling getwd.  This is faster
      when PWD is right, and may avoid a fatal error.  */
-  if ((pwd = getenv ("PWD")) != 0 && IS_DIRECTORY_SEP (*pwd)
+  if ((pwd = getenv ("PWD")) != 0
+      && (IS_DIRECTORY_SEP (*pwd) || (*pwd && IS_DEVICE_SEP (pwd[1])))
       && stat (pwd, &pwdstat) == 0
       && stat (".", &dotstat) == 0
       && dotstat.st_ino == pwdstat.st_ino
@@ -3708,7 +4003,7 @@ init_buffer ()
     strcpy (buf, pwd);
 #ifdef HAVE_GETCWD
   else if (getcwd (buf, MAXPATHLEN+1) == 0)
-    fatal ("`getcwd' failed: %s\n", buf);
+    fatal ("`getcwd' failed: %s\n", strerror (errno));
 #else
   else if (getwd (buf) == 0)
     fatal ("`getwd' failed: %s\n", buf);
@@ -3730,7 +4025,12 @@ init_buffer ()
   /* Add /: to the front of the name
      if it would otherwise be treated as magic.  */
   temp = Ffind_file_name_handler (current_buffer->directory, Qt);
-  if (! NILP (temp))
+  if (! NILP (temp)
+      /* If the default dir is just /, TEMP is non-nil
+        because of the ange-ftp completion handler.
+        However, it is not necessary to turn / into /:/.
+        So avoid doing that.  */
+      && strcmp ("/", XSTRING (current_buffer->directory)->data))
     current_buffer->directory
       = concat2 (build_string ("/:"), current_buffer->directory);
 
@@ -3739,6 +4039,7 @@ init_buffer ()
 }
 
 /* initialize the buffer routines */
+void
 syms_of_buffer ()
 {
   extern Lisp_Object Qdisabled;
@@ -3796,6 +4097,11 @@ syms_of_buffer ()
     "Default value of `mode-line-format' for buffers that don't override it.\n\
 This is the same as (default-value 'mode-line-format).");
 
+  DEFVAR_LISP_NOPRO ("default-header-line-format",
+                    &buffer_defaults.header_line_format,
+    "Default value of `header-line-format' for buffers that don't override it.\n\
+This is the same as (default-value 'header-line-format).");
+
   DEFVAR_LISP_NOPRO ("default-abbrev-mode",
              &buffer_defaults.abbrev_mode,
     "Default value of `abbrev-mode' for buffers that do not override it.\n\
@@ -3813,13 +4119,13 @@ This is the same as (default-value 'ctl-arrow).");
  
    DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters",
              &buffer_defaults.enable_multibyte_characters,
-     "Default value of `enable-multibyte-characters' for buffers not overriding it.\n\
- This is the same as (default-value 'enable-multibyte-characters).");
+     "*Default value of `enable-multibyte-characters' for buffers not overriding it.\n\
+This is the same as (default-value 'enable-multibyte-characters).");
  
    DEFVAR_LISP_NOPRO ("default-buffer-file-coding-system",
              &buffer_defaults.buffer_file_coding_system,
      "Default value of `buffer-file-coding-system' for buffers not overriding it.\n\
- This is the same as (default-value 'buffer-file-coding-system).");
+This is the same as (default-value 'buffer-file-coding-system).");
  
   DEFVAR_LISP_NOPRO ("default-truncate-lines",
              &buffer_defaults.truncate_lines,
@@ -3854,6 +4160,39 @@ This is the same as (default-value 'buffer-file-type).\n\
 The file type is nil for text, t for binary.");
 #endif
 
+  DEFVAR_LISP_NOPRO ("default-left-margin-width",
+             &buffer_defaults.left_margin_width,
+    "Default value of `left-margin-width' for buffers that don't override it.\n\
+This is the same as (default-value 'left-margin-width).");
+
+  DEFVAR_LISP_NOPRO ("default-right-margin-width",
+             &buffer_defaults.right_margin_width,
+    "Default value of `right_margin_width' for buffers that don't override it.\n\
+This is the same as (default-value 'right-margin-width).");
+  
+  DEFVAR_LISP_NOPRO ("default-indicate-empty-lines",
+             &buffer_defaults.indicate_empty_lines,
+    "Default value of `indicate-empty-lines' for buffers that don't override it.\n\
+This is the same as (default-value 'indicate-empty-lines).");
+  
+  DEFVAR_LISP_NOPRO ("default-scroll-up-aggressively",
+             &buffer_defaults.scroll_up_aggressively,
+    "Default value of `scroll-up-aggressively' for buffers that\n\
+don't override it.  This is the same as (default-value\n\
+'scroll-up-aggressively).");
+  
+  DEFVAR_LISP_NOPRO ("default-scroll-down-aggressively",
+             &buffer_defaults.scroll_down_aggressively,
+    "Default value of `scroll-down-aggressively' for buffers that\n\
+don't override it.  This is the same as (default-value\n\
+'scroll-down-aggressively).");
+  
+  DEFVAR_PER_BUFFER ("header-line-format",
+                    &current_buffer->header_line_format, 
+                    Qnil,
+   "Analogous to `mode-line-format', but for a mode line displayed\n\
+at the top of windows.");
+  
   DEFVAR_PER_BUFFER ("mode-line-format", &current_buffer->mode_line_format, 
                     Qnil, 0);
 
@@ -3863,7 +4202,8 @@ The file type is nil for text, t for binary.");
     Qnil,
     "Template for displaying mode line for current buffer.\n\
 Each buffer has its own value of this variable.\n\
-Value may be a string, a symbol or a list or cons cell.\n\
+Value may be nil, a string, a symbol or a list or cons cell.\n\
+A value of nil means don't display a mode line.\n\
 For a symbol, its value is used (but it is ignored if t or nil).\n\
  A string appearing directly as the value of a symbol is processed verbatim\n\
  in that the %-constructs below are not recognized.\n\
@@ -3879,6 +4219,7 @@ A string is printed verbatim in the mode line except for %-constructs:\n\
   (%-constructs are allowed when the string is the entire mode-line-format\n\
    or when it is found in a cons-cell or a list)\n\
   %b -- print buffer name.      %f -- print visited file name.\n\
+  %F -- print frame name.\n\
   %* -- print %, * or hyphen.   %+ -- print *, % or hyphen.\n\
        % means buffer is read-only and * means it is modified.\n\
        For a modified read-only buffer, %* gives % and %+ gives *.\n\
@@ -3914,7 +4255,7 @@ Automatically becomes buffer-local when set in any fashion.");
 
   DEFVAR_PER_BUFFER ("case-fold-search", &current_buffer->case_fold_search,
                     Qnil,
-    "*Non-nil if searches should ignore case.\n\
+    "*Non-nil if searches and matches should ignore case.\n\
 Automatically becomes buffer-local when set in any fashion.");
 
   DEFVAR_PER_BUFFER ("fill-column", &current_buffer->fill_column,
@@ -3941,18 +4282,31 @@ This variable does not apply to characters whose display is specified\n\
 in the current display table (if there is one).");
 
   DEFVAR_PER_BUFFER ("enable-multibyte-characters",
-                    &current_buffer->enable_multibyte_characters, Qnil,
-    "*Non-nil means the buffer contents are regarded as multi-byte form\n\
-of characters, not a binary code.  This affects the display, file I/O,\n\
-and behaviors of various editing commands.");
+                    &current_buffer->enable_multibyte_characters,
+                    make_number (-1),
+    "Non-nil means the buffer contents are regarded as multi-byte characters.\n\
+Otherwise they are regarded as unibyte.  This affects the display,\n\
+file I/O and the behavior of various editing commands.\n\
+\n\
+This variable is buffer-local but you cannot set it directly;\n\
+use the function `set-buffer-multibyte' to change a buffer's representation.\n\
+Changing its default value with `setq-default' is supported.\n\
+See also variable `default-enable-multibyte-characters' and Info node\n\
+`(elisp)Text Representations'.");
 
   DEFVAR_PER_BUFFER ("buffer-file-coding-system",
                     &current_buffer->buffer_file_coding_system, Qnil,
     "Coding system to be used for encoding the buffer contents on saving.\n\
-If it is nil, the buffer is saved without any code conversion unless\n\
-some coding system is specified in file-coding-system-alist\n\
+This variable applies to saving the buffer, and also to `write-region'\n\
+and other functions that use `write-region'.\n\
+It does not apply to sending output to subprocesses, however.\n\
+\n\
+If this is nil, the buffer is saved without any code conversion\n\
+unless some coding system is specified in `file-coding-system-alist'\n\
 for the buffer file.\n\
 \n\
+The variable `coding-system-for-write', if non-nil, overrides this variable.\n\
+\n\
 This variable is never applied to a way of decoding\n\
 a file while reading it.");
 
@@ -4080,6 +4434,39 @@ Each window can have its own, overriding display table.");
   DEFVAR_PER_BUFFER ("buffer-display-table", &current_buffer->display_table,
                     Qnil, 0);
 
+  DEFVAR_PER_BUFFER ("left-margin-width", &current_buffer->left_margin_width,
+                    Qnil,
+    "*Width of left marginal area for display of a buffer.\n\
+Automatically becomes buffer-local when set in any fashion.\n\
+A value of nil means no marginal area.");
+  
+  DEFVAR_PER_BUFFER ("right-margin-width", &current_buffer->right_margin_width,
+                    Qnil,
+    "*Width of right marginal area for display of a buffer.\n\
+Automatically becomes buffer-local when set in any fashion.\n\
+A value of nil means no marginal area.");
+  
+  DEFVAR_PER_BUFFER ("indicate-empty-lines",
+                    &current_buffer->indicate_empty_lines, Qnil,
+    "*Non-nil means visually indicate lines not displaying text.\n\
+Automatically becomes buffer-local when set in any fashion.\n");
+  
+  DEFVAR_PER_BUFFER ("scroll-up-aggressively",
+                    &current_buffer->scroll_up_aggressively, Qnil,
+    "*If a number, scroll display up aggressively.\n\
+If scrolling a window because point is above the window start, choose\n\
+a new window start so that point ends up that fraction of the window's\n\
+height from the bottom of the window.\n\
+Automatically becomes buffer-local when set in any fashion.");
+  
+  DEFVAR_PER_BUFFER ("scroll-down-aggressively",
+                    &current_buffer->scroll_down_aggressively, Qnil,
+    "*If a number, scroll display down aggressively.\n\
+If scrolling a window because point is below the window end, choose\n\
+a new window start so that point ends up that fraction of the window's\n\
+height from the top of the window.\n\
+Automatically becomes buffer-local when set in any fashion.");
+  
 /*DEFVAR_LISP ("debug-check-symbol", &Vcheck_symbol,
     "Don't ask.");
 */
@@ -4105,7 +4492,7 @@ Three arguments are passed to the function: the positions of\n\
 the beginning and end of the range of changed text,\n\
 and the length of the pre-change text replaced by that range.\n\
 \(For an insertion, the pre-change length is zero;\n\
-for a deletion, that length is the number of characters deleted,\n\
+for a deletion, that length is the number of bytes deleted,\n\
 and the post-change beginning and end are at the same place.)\n\
 \n\
 Buffer changes made while executing the `after-change-function'\n\
@@ -4129,16 +4516,20 @@ don't call any before-change or after-change functions.\n\
 That's because these variables are temporarily set to nil.\n\
 As a result, a hook function cannot straightforwardly alter the value of\n\
 these variables.  See the Emacs Lisp manual for a way of\n\
-accomplishing an equivalent result by using other variables.");
+accomplishing an equivalent result by using other variables.\n\
+\n\
+If an unhandled error happens in running these functions,\n\
+the variable's value remains nil.  That prevents the error\n\
+from happening repeatedly and making Emacs nonfunctional.");
   Vbefore_change_functions = Qnil;
 
   DEFVAR_LISP ("after-change-functions", &Vafter_change_functions,
               "List of function to call after each text change.\n\
 Three arguments are passed to each function: the positions of\n\
 the beginning and end of the range of changed text,\n\
-and the length of the pre-change text replaced by that range.\n\
+and the length in bytes of the pre-change text replaced by that range.\n\
 \(For an insertion, the pre-change length is zero;\n\
-for a deletion, that length is the number of characters deleted,\n\
+for a deletion, that length is the number of bytes deleted,\n\
 and the post-change beginning and end are at the same place.)\n\
 \n\
 Buffer changes made while executing the `after-change-functions'\n\
@@ -4146,8 +4537,11 @@ don't call any before-change or after-change functions.\n\
 That's because these variables are temporarily set to nil.\n\
 As a result, a hook function cannot straightforwardly alter the value of\n\
 these variables.  See the Emacs Lisp manual for a way of\n\
-accomplishing an equivalent result by using other variables.");
-
+accomplishing an equivalent result by using other variables.\n\
+\n\
+If an unhandled error happens in running these functions,\n\
+the variable's value remains nil.  That prevents the error\n\
+from happening repeatedly and making Emacs nonfunctional.");
   Vafter_change_functions = Qnil;
 
   DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
@@ -4159,6 +4553,7 @@ The functions are run using the `run-hooks' function.");
         but make-docfile can find it in this comment.  */
   DEFVAR_PER_BUFFER ("buffer-undo-list", &current_buffer->undo_list, Qnil,
     "List of undo entries in current buffer.\n\
+This variable is always local in all buffers.\n\
 Recent changes come first; older changes follow newer.\n\
 \n\
 An entry (BEG . END) represents an insertion which begins at\n\
@@ -4227,16 +4622,19 @@ the cache should not affect the behavior of any of the motion\n\
 functions; it should only affect their performance.");
 
   DEFVAR_PER_BUFFER ("point-before-scroll", &current_buffer->point_before_scroll, Qnil,
-  "Value of point before the last series of scroll operations, or nil.");
+  "Value of point before the last series of scroll operations, or nil.\n\
+This variable is always local in all buffers.");
 
   DEFVAR_PER_BUFFER ("buffer-file-format", &current_buffer->file_format, Qnil,
     "List of formats to use when saving this buffer.\n\
+This variable is always local in all buffers.\n\
 Formats are defined by `format-alist'.  This variable is\n\
 set when a file is visited.  Automatically local in all buffers.");
 
   DEFVAR_PER_BUFFER ("buffer-invisibility-spec",
                     &current_buffer->invisibility_spec, Qnil,
   "Invisibility spec of this buffer.\n\
+This variable is always local in all buffers.\n\
 The default is t, which means that text is invisible\n\
 if it has a non-nil `invisible' property.\n\
 If the value is a list, a text character is invisible if its `invisible'\n\
@@ -4247,7 +4645,17 @@ and they have an ellipsis as well if ELLIPSIS is non-nil.");
 
   DEFVAR_PER_BUFFER ("buffer-display-count",
                     &current_buffer->display_count, Qnil,
-  "A number incremented each time the buffer is displayed in a window.");
+  "A number incremented each time this buffer is displayed in a window.\n\
+This variable is always local in all buffers.\n\
+The function `set-window-buffer increments it.");
+
+  DEFVAR_PER_BUFFER ("buffer-display-time",
+                    &current_buffer->display_time, Qnil,
+  "Time stamp updated each time this buffer is displayed in a window.\n\
+This variable is always local in all buffers.\n\
+The function `set-window-buffer' updates this variable\n\
+to the value obtained by calling `current-time'.\n\
+If the buffer has never been shown in a window, the value is nil.");
 
   DEFVAR_LISP ("transient-mark-mode", &Vtransient_mark_mode,
     "*Non-nil means deactivate the mark when the buffer contents change.\n\
@@ -4288,7 +4696,6 @@ is a member of the list.");
   defsubr (&Sbuffer_disable_undo);
   defsubr (&Sbuffer_enable_undo);
   defsubr (&Skill_buffer);
-  defsubr (&Serase_buffer);
   defsubr (&Sset_buffer_major_mode);
   defsubr (&Sswitch_to_buffer);
   defsubr (&Spop_to_buffer);
@@ -4296,6 +4703,8 @@ is a member of the list.");
   defsubr (&Sset_buffer);
   defsubr (&Sbarf_if_buffer_read_only);
   defsubr (&Sbury_buffer);
+  defsubr (&Serase_buffer);
+  defsubr (&Sset_buffer_multibyte);
   defsubr (&Skill_all_local_variables);
 
   defsubr (&Soverlayp);
@@ -4316,6 +4725,7 @@ is a member of the list.");
   defsubr (&Soverlay_put);
 }
 
+void
 keys_of_buffer ()
 {
   initial_define_key (control_x_map, 'b', "switch-to-buffer");