(advance_to_char_boundary): Don't use DEC_POS to find a
[bpt/emacs.git] / src / buffer.c
index 2fb9fef..9587ec7 100644 (file)
@@ -73,8 +73,8 @@ static Lisp_Object Vbuffer_defaults;
    The value has only one nonzero bit.
 
    When a buffer has its own local value for a slot,
-   the bit for that slot (found in the same slot in this structure)
-   is turned on in the buffer's local_var_flags slot.
+   the entry for that slot (found in the same slot in this structure)
+   is turned on in the buffer's local_flags array.
 
    If a slot in this structure is -1, then even though there may
    be a DEFVAR_PER_BUFFER for the slot, there is no default value for it;
@@ -112,7 +112,11 @@ struct buffer buffer_local_types;
 
 /* Flags indicating which built-in buffer-local variables
    are permanent locals.  */
-static int buffer_permanent_local_flags;
+static char buffer_permanent_local_flags[MAX_PER_BUFFER_VARS];
+
+/* Number of per-buffer variables used.  */
+
+int last_per_buffer_idx;
 
 Lisp_Object Fset_buffer ();
 void set_buffer_internal ();
@@ -330,7 +334,7 @@ The value is never nil.")
   if (XSTRING (name)->size == 0)
     error ("Empty string for buffer name is not allowed");
 
-  b = (struct buffer *) xmalloc (sizeof (struct buffer));
+  b = (struct buffer *) allocate_buffer ();
 
   b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
 
@@ -404,16 +408,61 @@ The value is never nil.")
   return buf;
 }
 
-DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer, 2, 2,
+
+/* Clone per-buffer values of buffer FROM.
+
+   Buffer TO gets the same per-buffer values as FROM, with the
+   following exceptions: (1) TO's name is left untouched, (2) markers
+   are copied and made to refer to TO, and (3) overlay lists are
+   copied.  */
+
+static void
+clone_per_buffer_values (from, to)
+     struct buffer *from, *to;
+{
+  Lisp_Object to_buffer;
+  int offset;
+
+  XSETBUFFER (to_buffer, to);
+  
+  for (offset = PER_BUFFER_VAR_OFFSET (name) + sizeof (Lisp_Object);
+       offset < sizeof *to;
+       offset += sizeof (Lisp_Object))
+    {
+      Lisp_Object obj;
+
+      obj = PER_BUFFER_VALUE (from, offset);
+      if (MARKERP (obj))
+       {
+         struct Lisp_Marker *m = XMARKER (obj);
+         obj = Fmake_marker ();
+         XMARKER (obj)->insertion_type = m->insertion_type;
+         set_marker_both (obj, to_buffer, m->charpos, m->bytepos);
+       }
+
+      PER_BUFFER_VALUE (to, offset) = obj;
+    }
+
+  to->overlays_after = Fcopy_sequence (from->overlays_after);
+  to->overlays_before = Fcopy_sequence (to->overlays_before);
+  bcopy (from->local_flags, to->local_flags, sizeof to->local_flags);
+}
+
+
+DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
+       2, 3,
        "bMake indirect buffer (to buffer): \nBName of indirect buffer: ",
   "Create and return an indirect buffer for buffer BASE-BUFFER, named NAME.\n\
 BASE-BUFFER should be an existing buffer (or buffer name).\n\
-NAME should be a string which is not the name of an existing buffer.")
-  (base_buffer, name)
-     register Lisp_Object base_buffer, name;
+NAME should be a string which is not the name of an existing buffer.\n\
+Optional argument CLONE non-nil means preserve BASE-BUFFER's state,\n\
+such as major and minor modes, in the indirect buffer.\n\
+CLONE nil means the indirect buffer's state is reset to default values.")
+  (base_buffer, name, clone)
+     Lisp_Object base_buffer, name, clone;
 {
-  register Lisp_Object buf;
-  register struct buffer *b;
+  Lisp_Object buf;
+  struct buffer *b;
 
   buf = Fget_buffer (name);
   if (!NILP (buf))
@@ -427,8 +476,7 @@ NAME should be a string which is not the name of an existing buffer.")
   if (XSTRING (name)->size == 0)
     error ("Empty string for buffer name is not allowed");
 
-  b = (struct buffer *) xmalloc (sizeof (struct buffer));
-
+  b = (struct buffer *) allocate_buffer ();
   b->size = sizeof (struct buffer) / sizeof (EMACS_INT);
 
   if (XBUFFER (base_buffer)->base_buffer)
@@ -495,14 +543,19 @@ NAME should be a string which is not the name of an existing buffer.")
       XMARKER (b->base_buffer->zv_marker)->insertion_type = 1;
     }
 
-  /* Give the indirect buffer markers for its narrowing.  */
-  b->pt_marker = Fmake_marker ();
-  set_marker_both (b->pt_marker, buf, BUF_PT (b), BUF_PT_BYTE (b));
-  b->begv_marker = Fmake_marker ();
-  set_marker_both (b->begv_marker, buf, BUF_BEGV (b), BUF_BEGV_BYTE (b));
-  b->zv_marker = Fmake_marker ();
-  set_marker_both (b->zv_marker, buf, BUF_ZV (b), BUF_ZV_BYTE (b));
-  XMARKER (b->zv_marker)->insertion_type = 1;
+  if (NILP (clone))
+    {
+      /* Give the indirect buffer markers for its narrowing.  */
+      b->pt_marker = Fmake_marker ();
+      set_marker_both (b->pt_marker, buf, BUF_PT (b), BUF_PT_BYTE (b));
+      b->begv_marker = Fmake_marker ();
+      set_marker_both (b->begv_marker, buf, BUF_BEGV (b), BUF_BEGV_BYTE (b));
+      b->zv_marker = Fmake_marker ();
+      set_marker_both (b->zv_marker, buf, BUF_ZV (b), BUF_ZV_BYTE (b));
+      XMARKER (b->zv_marker)->insertion_type = 1;
+    }
+  else
+    clone_per_buffer_values (b->base_buffer, b);
 
   return buf;
 }
@@ -537,9 +590,9 @@ reset_buffer (b)
   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;
+  b->cursor_type = buffer_defaults.cursor_type;
+  b->extra_line_spacing = buffer_defaults.extra_line_spacing;
 }
 
 /* Reset buffer B's local variables info.
@@ -557,13 +610,7 @@ reset_buffer_local_variables (b, permanent_too)
      int permanent_too;
 {
   register int offset;
-  int dont_reset;
-
-  /* Decide which built-in local variables to reset.  */
-  if (permanent_too)
-    dont_reset = 0;
-  else
-    dont_reset = buffer_permanent_local_flags;
+  int i;
 
   /* Reset the major mode to Fundamental, together with all the
      things that depend on the major mode.
@@ -598,22 +645,24 @@ reset_buffer_local_variables (b, permanent_too)
 
   /* Reset all (or most) per-buffer variables to their defaults.  */
   b->local_var_alist = Qnil;
-  b->local_var_flags &= dont_reset;
+  for (i = 0; i < last_per_buffer_idx; ++i)
+    if (permanent_too || buffer_permanent_local_flags[i] == 0)
+      SET_PER_BUFFER_VALUE_P (b, i, 0);
 
   /* For each slot that has a default value,
      copy that into the slot.  */
 
-  for (offset = (char *)&buffer_local_flags.name - (char *)&buffer_local_flags;
-       offset < sizeof (struct buffer);
-       offset += sizeof (Lisp_Object)) /* sizeof EMACS_INT == sizeof Lisp_Object */
+  for (offset = PER_BUFFER_VAR_OFFSET (name);
+       offset < sizeof *b;
+       offset += sizeof (Lisp_Object))
     {
-      int flag = XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_flags));
-      if ((flag > 0
-          /* Don't reset a permanent local.  */
-          && ! (dont_reset & flag))
-         || flag == -2)
-       *(Lisp_Object *)(offset + (char *)b)
-         = *(Lisp_Object *)(offset + (char *)&buffer_defaults);
+      int idx = PER_BUFFER_IDX (offset);
+      if ((idx > 0
+          && (permanent_too
+              || buffer_permanent_local_flags[idx] == 0))
+         /* Is -2 used anywhere?  */
+         || idx == -2)
+       PER_BUFFER_VALUE (b, offset) = PER_BUFFER_DEFAULT (offset);
     }
 }
 
@@ -758,20 +807,19 @@ No argument or nil as argument means use current buffer as BUFFER.")
 
   /* Add on all the variables stored in special slots.  */
   {
-    register int offset, mask;
+    int offset, idx;
 
-    for (offset = (char *)&buffer_local_symbols.name - (char *)&buffer_local_symbols;
+    for (offset = PER_BUFFER_VAR_OFFSET (name);
         offset < sizeof (struct buffer);
-        offset += (sizeof (EMACS_INT))) /* sizeof EMACS_INT == sizeof Lisp_Object */
+        /* sizeof EMACS_INT == sizeof Lisp_Object */
+        offset += (sizeof (EMACS_INT)))
       {
-       mask = XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_flags));
-       if (mask == -1 || (buf->local_var_flags & mask))
-         if (SYMBOLP (*(Lisp_Object *)(offset
-                                       + (char *)&buffer_local_symbols)))
-           result = Fcons (Fcons (*((Lisp_Object *)
-                                    (offset + (char *)&buffer_local_symbols)),
-                                  *(Lisp_Object *)(offset + (char *)buf)),
-                           result);
+       idx = PER_BUFFER_IDX (offset);
+       if ((idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
+           && SYMBOLP (PER_BUFFER_SYMBOL (offset)))
+         result = Fcons (Fcons (PER_BUFFER_SYMBOL (offset),
+                                PER_BUFFER_VALUE (buf, offset)),
+                         result);
       }
   }
 
@@ -807,6 +855,7 @@ A non-nil FLAG means mark the buffer modified.")
 {
   register int already;
   register Lisp_Object fn;
+  Lisp_Object buffer, window;
 
 #ifdef CLASH_DETECTION
   /* If buffer becoming modified, lock the file.
@@ -825,7 +874,53 @@ A non-nil FLAG means mark the buffer modified.")
 #endif /* CLASH_DETECTION */
 
   SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
-  update_mode_lines++;
+  
+  /* Set update_mode_lines only if buffer is displayed in some window.
+     Packages like jit-lock or lazy-lock preserve a buffer's modified
+     state by recording/restoring the state around blocks of code.
+     Setting update_mode_lines makes redisplay consider all windows
+     (on all frames).  Stealth fontification of buffers not displayed
+     would incur additional redisplay costs if we'd set
+     update_modes_lines unconditionally.
+
+     Ideally, I think there should be another mechanism for fontifying
+     buffers without "modifying" buffers, or redisplay should be
+     smarter about updating the `*' in mode lines.  --gerd  */
+  XSETBUFFER (buffer, current_buffer);
+  window = Fget_buffer_window (buffer, Qt);
+  if (WINDOWP (window))
+    update_mode_lines++;
+  
+  return flag;
+}
+
+DEFUN ("restore-buffer-modified-p", Frestore_buffer_modified_p,
+       Srestore_buffer_modified_p, 1, 1, 0,
+   "Like `set-buffer-modified-p', with a differences concerning redisplay.\n\
+It is not ensured that mode lines will be updated to show the modified\n\
+state of the current buffer.  Use with care.")
+  (flag)
+     Lisp_Object flag;
+{
+#ifdef CLASH_DETECTION
+  Lisp_Object fn;
+  
+  /* If buffer becoming modified, lock the file.
+     If buffer becoming unmodified, unlock the file.  */
+
+  fn = current_buffer->file_truename;
+  /* Test buffer-file-name so that binding it to nil is effective.  */
+  if (!NILP (fn) && ! NILP (current_buffer->filename))
+    {
+      int already = SAVE_MODIFF < MODIFF;
+      if (!already && !NILP (flag))
+       lock_file (fn);
+      else if (already && NILP (flag))
+       unlock_file (fn);
+    }
+#endif /* CLASH_DETECTION */
+  
+  SAVE_MODIFF = NILP (flag) ? MODIFF : 0;
   return flag;
 }
 
@@ -871,14 +966,14 @@ This does not change the name of the visited file (if any).")
     error ("Empty string is invalid as a 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 NEWNAME)).  */
-  if (NILP (unique) && XBUFFER (tem) == current_buffer)
-    return current_buffer->name;
   if (!NILP (tem))
     {
+      /* 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 NEWNAME)).  */
+      if (NILP (unique) && XBUFFER (tem) == current_buffer)
+       return current_buffer->name;
       if (!NILP (unique))
        newname = Fgenerate_new_buffer_name (newname, current_buffer->name);
       else
@@ -1676,18 +1771,20 @@ selected window if it is displayed there.")
       buffer = buf1;
     }
 
-  /* Move buffer to the end of the buffer list.  */
-  {
-    register Lisp_Object aelt, link;
+  /* Move buffer to the end of the buffer list.  Do nothing if the
+     buffer is killed.  */
+  if (!NILP (XBUFFER (buffer)->name))
+    {
+      Lisp_Object aelt, link;
 
-    aelt = Frassq (buffer, Vbuffer_alist);
-    link = Fmemq (aelt, Vbuffer_alist);
-    Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
-    XCDR (link) = Qnil;
-    Vbuffer_alist = nconc2 (Vbuffer_alist, link);
-  }
+      aelt = Frassq (buffer, Vbuffer_alist);
+      link = Fmemq (aelt, Vbuffer_alist);
+      Vbuffer_alist = Fdelq (aelt, Vbuffer_alist);
+      XCDR (link) = Qnil;
+      Vbuffer_alist = nconc2 (Vbuffer_alist, link);
 
-  frames_bury_buffer (buffer);
+      frames_bury_buffer (buffer);
+    }
 
   return Qnil;
 }
@@ -1746,8 +1843,17 @@ advance_to_char_boundary (byte_pos)
     {
       /* We should advance BYTE_POS only when C is a constituent of a
          multibyte sequence.  */
-      DEC_POS (byte_pos);
+      int orig_byte_pos = byte_pos;
+
+      do
+       {
+         byte_pos--;
+         c = FETCH_BYTE (byte_pos);
+       }
+      while (! CHAR_HEAD_P (c) && byte_pos > BEG);
       INC_POS (byte_pos);
+      if (byte_pos < orig_byte_pos)
+       byte_pos = orig_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.  */
@@ -1768,6 +1874,9 @@ but the contents viewed as characters do change.")
 {
   Lisp_Object tail, markers;
   struct buffer *other;
+  int undo_enabled_p = !EQ (current_buffer->undo_list, Qt);
+  int begv = BEGV, zv = ZV;
+  int narrowed = (BEG != begv || Z != zv);
 
   if (current_buffer->base_buffer)
     error ("Cannot do `set-buffer-multibyte' on an indirect buffer");
@@ -1778,14 +1887,20 @@ but the contents viewed as characters do change.")
 
   /* 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 (undo_enabled_p)
+    current_buffer->undo_list = Qt;
 
   /* If the cached position is for this buffer, clear it out.  */
   clear_charpos_cache (current_buffer);
 
+  if (narrowed)
+    Fwiden ();
+
   if (NILP (flag))
     {
+      int pos, stop;
+      unsigned char *p;
+
       /* Do this first, so it can use CHAR_TO_BYTE
         to calculate the old correspondences.  */
       set_intervals_multibyte (0);
@@ -1799,17 +1914,59 @@ but the contents viewed as characters do change.")
       TEMP_SET_PT_BOTH (PT_BYTE, PT_BYTE);
 
       tail = BUF_MARKERS (current_buffer);
-      while (XSYMBOL (tail) != XSYMBOL (Qnil))
+      while (! NILP (tail))
        {
          XMARKER (tail)->charpos = XMARKER (tail)->bytepos;
          tail = XMARKER (tail)->chain;
        }
+
+      /* Convert multibyte form of 8-bit characters to unibyte.  */
+      pos = BEG;
+      stop = GPT;
+      p = BEG_ADDR;
+      while (1)
+       {
+         int c, bytes;
+
+         if (pos == stop)
+           {
+             if (pos == Z)
+               break;
+             p = GAP_END_ADDR;
+             stop = Z;
+           }
+         if (MULTIBYTE_STR_AS_UNIBYTE_P (p, bytes))
+           p += bytes, pos += bytes;
+         else
+           {
+             c = STRING_CHAR (p, stop - pos);
+             /* Delete all bytes for this 8-bit character but the
+                last one, and change the last one to the charcter
+                code.  */
+             bytes--;
+             del_range_2 (pos, pos, pos + bytes, pos + bytes, 0);
+             p = GAP_END_ADDR;
+             *p++ = c;
+             pos++;
+             if (begv > pos)
+               begv -= bytes;
+             if (zv > pos)
+               zv -= bytes;
+             stop = Z;
+           }
+       }
+      if (narrowed)
+       Fnarrow_to_region (make_number (begv), make_number (zv));
     }
   else
     {
+      int pt = PT;
+      int pos, stop;
+      unsigned char *p;
+
       /* 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..."  */
+        Ex: We change this: "...abc\201 _GAP_ \241def..."
+            to: "...abc _GAP_ \201\241def..."  */
 
       if (GPT_BYTE > 1 && GPT_BYTE < Z_BYTE
          && ! CHAR_HEAD_P (*(GAP_END_ADDR)))
@@ -1825,6 +1982,53 @@ but the contents viewed as characters do change.")
            }
        }
 
+      /* Make the buffer contents valid as multibyte by converting
+        8-bit characters to multibyte form.  */
+      pos = BEG;
+      stop = GPT;
+      p = BEG_ADDR;
+      while (1)
+       {
+         int bytes;
+
+         if (pos == stop)
+           {
+             if (pos == Z)
+               break;
+             p = GAP_END_ADDR;
+             stop = Z;
+           }
+             
+         if (UNIBYTE_STR_AS_MULTIBYTE_P (p, stop - pos, bytes))
+           p += bytes, pos += bytes;
+         else
+           {
+             unsigned char tmp[MAX_MULTIBYTE_LENGTH];
+
+             bytes = CHAR_STRING (*p, tmp);
+             *p = tmp[0];
+             TEMP_SET_PT_BOTH (pos + 1, pos + 1);
+             bytes--;
+             insert_1_both (tmp + 1, bytes, bytes, 1, 0, 0);
+             /* Now the gap is after the just inserted data.  */
+             pos = GPT;
+             p = GAP_END_ADDR;
+             if (pos <= begv)
+               begv += bytes;
+             if (pos <= zv)
+               zv += bytes;
+             if (pos <= pt)
+               pt += bytes;
+             stop = Z;
+           }
+       }
+
+      if (pt != PT)
+       TEMP_SET_PT (pt);
+
+      if (narrowed)
+       Fnarrow_to_region (make_number (begv), make_number (zv));
+
       /* Do this first, so that chars_in_text asks the right question.
         set_intervals_multibyte needs it too.  */
       current_buffer->enable_multibyte_characters = Qt;
@@ -1864,7 +2068,7 @@ but the contents viewed as characters do change.")
         It is also a signal that it should never create a marker.  */
       BUF_MARKERS (current_buffer) = Qnil;
 
-      while (XSYMBOL (tail) != XSYMBOL (Qnil))
+      while (! NILP (tail))
        {
          XMARKER (tail)->bytepos
            = advance_to_char_boundary (XMARKER (tail)->bytepos);
@@ -1885,6 +2089,9 @@ but the contents viewed as characters do change.")
       set_intervals_multibyte (1);
     }
 
+  if (undo_enabled_p)
+    current_buffer->undo_list = Qnil;
+
   /* Changing the multibyteness of a buffer means that all windows
      showing that buffer must be updated thoroughly.  */
   current_buffer->prevent_redisplay_optimizations_p = 1;
@@ -1980,7 +2187,7 @@ swap_out_buffer_local_variables (b)
 
       /* Need not do anything if some other buffer's binding is now encached.  */
       tem = XBUFFER_LOCAL_VALUE (XSYMBOL (sym)->value)->buffer;
-      if (XBUFFER (tem) == current_buffer)
+      if (BUFFERP (tem) && 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.  */
@@ -3150,7 +3357,7 @@ buffer.")
 
   obuffer = Fmarker_buffer (OVERLAY_START (overlay));
   b = XBUFFER (buffer);
-  ob = XBUFFER (obuffer);
+  ob = BUFFERP (obuffer) ? XBUFFER (obuffer) : (struct buffer *) 0;
 
   /* If the overlay has changed buffers, do a thorough redisplay.  */
   if (!EQ (buffer, obuffer))
@@ -3806,25 +4013,37 @@ buffer_slot_type_mismatch (offset)
 {
   Lisp_Object sym;
   char *type_name;
-  sym = *(Lisp_Object *)(offset + (char *)&buffer_local_symbols);
-  switch (XINT (*(Lisp_Object *)(offset + (char *)&buffer_local_types)))
+  
+  switch (XINT (PER_BUFFER_TYPE (offset)))
     {
-    case Lisp_Int:     type_name = "integers";  break;
-    case Lisp_String:  type_name = "strings";   break;
-    case Lisp_Symbol:  type_name = "symbols";   break;
-
+    case Lisp_Int:
+      type_name = "integers";
+      break;
+      
+    case Lisp_String:
+      type_name = "strings";
+      break;
+      
+    case Lisp_Symbol:
+      type_name = "symbols";
+      break;
+      
     default:
       abort ();
     }
 
+  sym = PER_BUFFER_SYMBOL (offset);
   error ("Only %s should be stored in the buffer-local variable %s",
         type_name, XSYMBOL (sym)->name->data);
 }
+
 \f
 void
 init_buffer_once ()
 {
-  buffer_permanent_local_flags = 0;
+  int idx;
+
+  bzero (buffer_permanent_local_flags, sizeof buffer_permanent_local_flags);
 
   /* Make sure all markable slots in buffer_defaults
      are initialized reasonably, so mark_buffer won't choke.  */
@@ -3843,7 +4062,7 @@ init_buffer_once ()
   /* Set up the default values of various buffer slots.  */
   /* Must do these before making the first buffer! */
 
-  /* real setup is done in loaddefs.el */
+  /* real setup is done in bindings.el */
   buffer_defaults.mode_line_format = build_string ("%-");
   buffer_defaults.header_line_format = Qnil;
   buffer_defaults.abbrev_mode = Qnil;
@@ -3867,6 +4086,8 @@ init_buffer_once ()
   buffer_defaults.truncate_lines = Qnil;
   buffer_defaults.ctl_arrow = Qt;
   buffer_defaults.direction_reversed = Qnil;
+  buffer_defaults.cursor_type = Qt;
+  buffer_defaults.extra_line_spacing = Qnil;
 
 #ifdef DOS_NT
   buffer_defaults.buffer_file_type = Qnil; /* TEXT */
@@ -3911,40 +4132,48 @@ init_buffer_once ()
   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);
-  XSETFASTINT (buffer_local_flags.overwrite_mode, 4);
-  XSETFASTINT (buffer_local_flags.case_fold_search, 8);
-  XSETFASTINT (buffer_local_flags.auto_fill_function, 0x10);
-  XSETFASTINT (buffer_local_flags.selective_display, 0x20);
+  idx = 1;
+  XSETFASTINT (buffer_local_flags.mode_line_format, idx); ++idx;
+  XSETFASTINT (buffer_local_flags.abbrev_mode, idx); ++idx;
+  XSETFASTINT (buffer_local_flags.overwrite_mode, idx); ++idx;
+  XSETFASTINT (buffer_local_flags.case_fold_search, idx); ++idx;
+  XSETFASTINT (buffer_local_flags.auto_fill_function, idx); ++idx;
+  XSETFASTINT (buffer_local_flags.selective_display, idx); ++idx;
 #ifndef old
-  XSETFASTINT (buffer_local_flags.selective_display_ellipses, 0x40);
+  XSETFASTINT (buffer_local_flags.selective_display_ellipses, idx); ++idx;
 #endif
-  XSETFASTINT (buffer_local_flags.tab_width, 0x80);
-  XSETFASTINT (buffer_local_flags.truncate_lines, 0x100);
-  XSETFASTINT (buffer_local_flags.ctl_arrow, 0x200);
-  XSETFASTINT (buffer_local_flags.fill_column, 0x400);
-  XSETFASTINT (buffer_local_flags.left_margin, 0x800);
-  XSETFASTINT (buffer_local_flags.abbrev_table, 0x1000);
-  XSETFASTINT (buffer_local_flags.display_table, 0x2000);
+  XSETFASTINT (buffer_local_flags.tab_width, idx); ++idx;
+  XSETFASTINT (buffer_local_flags.truncate_lines, idx); ++idx;
+  XSETFASTINT (buffer_local_flags.ctl_arrow, idx); ++idx;
+  XSETFASTINT (buffer_local_flags.fill_column, idx); ++idx;
+  XSETFASTINT (buffer_local_flags.left_margin, idx); ++idx;
+  XSETFASTINT (buffer_local_flags.abbrev_table, idx); ++idx;
+  XSETFASTINT (buffer_local_flags.display_table, idx); ++idx;
 #ifdef DOS_NT
-  XSETFASTINT (buffer_local_flags.buffer_file_type, 0x4000);
+  XSETFASTINT (buffer_local_flags.buffer_file_type, idx);
   /* Make this one a permanent local.  */
-  buffer_permanent_local_flags |= 0x4000;
+  buffer_permanent_local_flags[idx++] = 1;
 #endif
-  XSETFASTINT (buffer_local_flags.syntax_table, 0x8000);
-  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.buffer_file_coding_system, 0x80000);
+  XSETFASTINT (buffer_local_flags.syntax_table, idx); ++idx;
+  XSETFASTINT (buffer_local_flags.cache_long_line_scans, idx); ++idx;
+  XSETFASTINT (buffer_local_flags.category_table, idx); ++idx;
+  XSETFASTINT (buffer_local_flags.direction_reversed, idx); ++idx;
+  XSETFASTINT (buffer_local_flags.buffer_file_coding_system, idx); 
   /* Make this one a permanent local.  */
-  buffer_permanent_local_flags |= 0x80000;
-  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);
+  buffer_permanent_local_flags[idx++] = 1;
+  XSETFASTINT (buffer_local_flags.left_margin_width, idx); ++idx;
+  XSETFASTINT (buffer_local_flags.right_margin_width, idx); ++idx;
+  XSETFASTINT (buffer_local_flags.indicate_empty_lines, idx); ++idx;
+  XSETFASTINT (buffer_local_flags.scroll_up_aggressively, idx); ++idx;
+  XSETFASTINT (buffer_local_flags.scroll_down_aggressively, idx); ++idx;
+  XSETFASTINT (buffer_local_flags.header_line_format, idx); ++idx;
+  XSETFASTINT (buffer_local_flags.cursor_type, idx); ++idx;
+  XSETFASTINT (buffer_local_flags.extra_line_spacing, idx); ++idx;
+
+  /* Need more room? */
+  if (idx >= MAX_PER_BUFFER_VARS)
+    abort ();
+  last_per_buffer_idx = idx;
   
   Vbuffer_alist = Qnil;
   current_buffer = 0;
@@ -4095,6 +4324,15 @@ This is the same as (default-value 'mode-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-cursor-type", &buffer_defaults.cursor_type,
+    "Default value of `cursor-type' for buffers that don't override it.\n\
+This is the same as (default-value 'cursor-type).");
+
+  DEFVAR_LISP_NOPRO ("default-line-spacing",
+                    &buffer_defaults.extra_line_spacing,
+    "Default value of `line-spacing' for buffers that don't override it.\n\
+This is the same as (default-value 'line-spacing).");
+
   DEFVAR_LISP_NOPRO ("default-abbrev-mode",
              &buffer_defaults.abbrev_mode,
     "Default value of `abbrev-mode' for buffers that do not override it.\n\
@@ -4338,7 +4576,8 @@ Each buffer has its own value of this variable.");
   DEFVAR_PER_BUFFER ("auto-fill-function", &current_buffer->auto_fill_function,
                     Qnil,
     "Function called (if non-nil) to perform auto-fill.\n\
-It is called after self-inserting a space or newline.\n\
+It is called after self-inserting any character specified in\n\
+the `auto-fill-chars' table.\n\
 Each buffer has its own value of this variable.\n\
 NOTE: This variable is not a hook;\n\
 its value may not be a list of functions.");
@@ -4672,6 +4911,21 @@ and disregard a `read-only' text property if the property value\n\
 is a member of the list.");
   Vinhibit_read_only = Qnil;
 
+  DEFVAR_PER_BUFFER ("cursor-type", &current_buffer->cursor_type, Qnil,
+    "Cursor to use in window displaying this buffer.\n\
+Values are interpreted as follows:\n\
+\n\
+  t            use the cursor specified for the frame\n\
+  nil          don't display a cursor\n\
+  `bar'                display a bar cursor with default width\n\
+  (bar . WIDTH)        display a bar cursor with width WIDTH\n\
+  others       display a box cursor.");
+
+  DEFVAR_PER_BUFFER ("line-spacing",
+                    &current_buffer->extra_line_spacing, Qnil,
+    "Additional space to put between lines when displaying a buffer.\n\
+The space is measured in pixels, and put below lines on window systems.");
+
   DEFVAR_LISP ("kill-buffer-query-functions", &Vkill_buffer_query_functions,
     "List of functions called with no args to query before killing a buffer.");
   Vkill_buffer_query_functions = Qnil;
@@ -4723,6 +4977,7 @@ is a member of the list.");
   defsubr (&Soverlay_lists);
   defsubr (&Soverlay_get);
   defsubr (&Soverlay_put);
+  defsubr (&Srestore_buffer_modified_p);
 }
 
 void