[!HAVE_X_WINDOWS] (store_frame_title): Dummy macro.
[bpt/emacs.git] / src / buffer.c
index 0ec2177..e132f77 100644 (file)
@@ -109,6 +109,8 @@ Lisp_Object Vbuffer_alist;
 /* Functions to call before and after each text change. */
 Lisp_Object Vbefore_change_function;
 Lisp_Object Vafter_change_function;
+Lisp_Object Vbefore_change_functions;
+Lisp_Object Vafter_change_functions;
 
 Lisp_Object Vtransient_mark_mode;
 
@@ -137,7 +139,7 @@ Lisp_Object Qget_file_buffer;
 
 Lisp_Object Qoverlayp;
 
-Lisp_Object Qpriority, Qwindow;
+Lisp_Object Qpriority, Qwindow, Qevaporate;
 
 Lisp_Object Qmodification_hooks;
 Lisp_Object Qinsert_in_front_hooks;
@@ -146,10 +148,6 @@ Lisp_Object Qinsert_behind_hooks;
 /* For debugging; temporary.  See set_buffer_internal.  */
 /* Lisp_Object Qlisp_mode, Vcheck_symbol; */
 
-#ifdef MSDOS
-Lisp_Object Qbuffer_file_type;
-#endif
-
 nsberror (spec)
      Lisp_Object spec;
 {
@@ -194,7 +192,7 @@ If there is no such live buffer, return nil.")
 
   /* If the file name has special constructs in it,
      call the corresponding file handler.  */
-  handler = Ffind_file_name_handler (filename);
+  handler = Ffind_file_name_handler (filename, Qget_file_buffer);
   if (!NILP (handler))
     return call2 (handler, Qget_file_buffer, filename);
 
@@ -230,6 +228,9 @@ The value is never nil.")
   if (!NILP (buf))
     return buf;
 
+  if (XSTRING (name)->size == 0)
+    error ("Empty string for buffer name is not allowed");
+
   b = (struct buffer *) xmalloc (sizeof (struct buffer));
 
   BUF_GAP_SIZE (b) = 20;
@@ -259,6 +260,7 @@ The value is never nil.")
     b->undo_list = Qt;
 
   reset_buffer (b);
+  reset_buffer_local_variables (b);
 
   /* Put this in the alist of all live buffers.  */
   XSET (buf, Lisp_Buffer, b);
@@ -290,7 +292,8 @@ The value is never nil.")
   return unbind_to (count, buf);
 }
 
-/* Reinitialize everything about a buffer except its name and contents.  */
+/* Reinitialize everything about a buffer except its name and contents
+   and local variables.  */
 
 void
 reset_buffer (b)
@@ -304,6 +307,7 @@ reset_buffer (b)
   b->last_window_start = 1;
   b->backed_up = Qnil;
   b->auto_save_modified = 0;
+  b->auto_save_failure_time = -1;
   b->auto_save_file_name = Qnil;
   b->read_only = Qnil;
   b->overlays_before = Qnil;
@@ -313,10 +317,13 @@ reset_buffer (b)
 
   /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
   INITIALIZE_INTERVAL (b, NULL_INTERVAL);
-
-  reset_buffer_local_variables(b);
 }
 
+/* Reset buffer B's local variables info.
+   Don't use this on a buffer that has already been in use;
+   it does not treat permanent locals consistently.
+   Instead, use Fkill_all_local_variables.  */
+
 reset_buffer_local_variables (b)
      register struct buffer *b;
 {
@@ -333,8 +340,8 @@ reset_buffer_local_variables (b)
   b->minor_modes = Qnil;
   b->downcase_table = Vascii_downcase_table;
   b->upcase_table = Vascii_upcase_table;
-  b->case_canon_table = Vascii_downcase_table;
-  b->case_eqv_table = Vascii_upcase_table;
+  b->case_canon_table = Vascii_canon_table;
+  b->case_eqv_table = Vascii_eqv_table;
 #if 0
   b->sort_table = XSTRING (Vascii_sort_table);
   b->folding_sort_table = XSTRING (Vascii_folding_sort_table);
@@ -349,9 +356,9 @@ reset_buffer_local_variables (b)
 
   for (offset = (char *)&buffer_local_flags.name - (char *)&buffer_local_flags;
        offset < sizeof (struct buffer);
-       offset += sizeof (Lisp_Object)) /* sizeof int == sizeof Lisp_Object */
-    if (*(int *)(offset + (char *) &buffer_local_flags) > 0
-       || *(int *)(offset + (char *) &buffer_local_flags) == -2)
+       offset += sizeof (Lisp_Object)) /* sizeof EMACS_INT == sizeof Lisp_Object */
+    if (*(EMACS_INT *)(offset + (char *) &buffer_local_flags) > 0
+       || *(EMACS_INT *)(offset + (char *) &buffer_local_flags) == -2)
       *(Lisp_Object *)(offset + (char *)b) =
        *(Lisp_Object *)(offset + (char *)&buffer_defaults);
 }
@@ -477,9 +484,9 @@ No argument or nil as argument means use current buffer as BUFFER.")
 
     for (offset = (char *)&buffer_local_symbols.name - (char *)&buffer_local_symbols;
         offset < sizeof (struct buffer);
-        offset += (sizeof (int))) /* sizeof int == sizeof Lisp_Object */
+        offset += (sizeof (EMACS_INT))) /* sizeof EMACS_INT == sizeof Lisp_Object */
       {
-       mask = *(int *)(offset + (char *) &buffer_local_flags);
+       mask = *(EMACS_INT *)(offset + (char *) &buffer_local_flags);
        if (mask == -1 || (buf->local_var_flags & mask))
          if (XTYPE (*(Lisp_Object *)(offset + (char *)&buffer_local_symbols))
              == Lisp_Symbol)
@@ -579,6 +586,10 @@ This does not change the name of the visited file (if any).")
   register Lisp_Object tem, buf;
 
   CHECK_STRING (name, 0);
+
+  if (XSTRING (name)->size == 0)
+    error ("Empty string is invalid as a buffer name");
+
   tem = Fget_buffer (name);
   /* 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.
@@ -602,9 +613,11 @@ This does not change the name of the visited file (if any).")
 
   XSET (buf, Lisp_Buffer, current_buffer);
   Fsetcar (Frassq (buf, Vbuffer_alist), name);
-  if (NILP (current_buffer->filename) && !NILP (current_buffer->auto_save_file_name))
+  if (NILP (current_buffer->filename)
+      && !NILP (current_buffer->auto_save_file_name))
     call0 (intern ("rename-auto-save-file"));
-  return name;
+  /* refetch since that last call may have done GC */
+  return current_buffer->name;
 }
 
 DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 2, 0,
@@ -795,8 +808,9 @@ with `delete-process'.")
   Freplace_buffer_in_windows (buf);
   Vinhibit_quit = tem;
 
-  /* Delete any auto-save file.  */
-  if (XTYPE (b->auto_save_file_name) == Lisp_String)
+  /* Delete any auto-save file, if we saved it in this session.  */
+  if (XTYPE (b->auto_save_file_name) == Lisp_String
+      && b->auto_save_modified != 0)
     {
       Lisp_Object tem;
       tem = Fsymbol_value (intern ("delete-auto-save-files"));
@@ -1084,7 +1098,7 @@ validate_region (b, e)
     args_out_of_range (*b, *e);
 }
 \f
-Lisp_Object
+static Lisp_Object
 list_buffers_1 (files)
      Lisp_Object files;
 {
@@ -1098,22 +1112,17 @@ list_buffers_1 (files)
   other_file_symbol = intern ("list-buffers-directory");
 
   XFASTINT (col1) = 19;
-  XFASTINT (col2) = 25;
+  XFASTINT (col2) = 26;
   XFASTINT (col3) = 40;
   XFASTINT (minspace) = 1;
 
   Fset_buffer (Vstandard_output);
-
-  tail = intern ("Buffer-menu-mode");
-  if (!EQ (tail, current_buffer->major_mode)
-      && (tem = Ffboundp (tail), !NILP (tem)))
-    call0 (tail);
   Fbuffer_disable_undo (Vstandard_output);
   current_buffer->read_only = Qnil;
 
   write_string ("\
- MR Buffer         Size  Mode           File\n\
- -- ------         ----  ----           ----\n", -1);
+ MR Buffer         Size   Mode           File\n\
+ -- ------         ----   ----           ----\n", -1);
 
   for (tail = Vbuffer_alist; !NILP (tail); tail = Fcdr (tail))
     {
@@ -1161,7 +1170,9 @@ list_buffers_1 (files)
       write_string ("\n", -1);
     }
 
-  current_buffer->read_only = Qt;
+  tail = intern ("Buffer-menu-mode");
+  if ((tem = Ffboundp (tail), !NILP (tem)))
+    call0 (tail);
   set_buffer_internal (old);
   return desired_point;
 }
@@ -1206,7 +1217,10 @@ This function also forces redisplay of the mode line.\n\
 Every function to select a new major mode starts by\n\
 calling this function.\n\n\
 As a special exception, local variables whose names have\n\
-a non-nil `permanent-local' property are not eliminated by this function.")
+a non-nil `permanent-local' property are not eliminated by this function.\n\
+\n\
+The first thing this function does is run\n\
+the normal hook `change-major-mode-hook'.")
   ()
 {
   register Lisp_Object alist, sym, tem;
@@ -1306,13 +1320,13 @@ overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr)
   int inhibit_storing = 0;
 
   for (tail = current_buffer->overlays_before;
-       CONSP (tail);
+       XGCTYPE (tail) == Lisp_Cons;
        tail = XCONS (tail)->cdr)
     {
       int startpos;
 
       overlay = XCONS (tail)->car;
-      if (! OVERLAY_VALID (overlay))
+      if (XGCTYPE (overlay) != Lisp_Overlay)
        abort ();
 
       start = OVERLAY_START (overlay);
@@ -1346,13 +1360,13 @@ overlays_at (pos, extend, vec_ptr, len_ptr, next_ptr)
     }
 
   for (tail = current_buffer->overlays_after;
-       CONSP (tail);
+       XGCTYPE (tail) == Lisp_Cons;
        tail = XCONS (tail)->cdr)
     {
       int startpos;
 
       overlay = XCONS (tail)->car;
-      if (! OVERLAY_VALID (overlay))
+      if (XGCTYPE (overlay) != Lisp_Overlay)
        abort ();
 
       start = OVERLAY_START (overlay);
@@ -1426,8 +1440,9 @@ sort_overlays (overlay_vec, noverlays, w)
   for (i = 0, j = 0; i < noverlays; i++)
     {
       Lisp_Object tem;
-      Lisp_Object overlay = overlay_vec[i];
+      Lisp_Object overlay;
 
+      overlay = overlay_vec[i];
       if (OVERLAY_VALID (overlay)
          && OVERLAY_POSITION (OVERLAY_START (overlay)) > 0
          && OVERLAY_POSITION (OVERLAY_END (overlay)) > 0)
@@ -1628,6 +1643,109 @@ recenter_overlay_lists (buf, pos)
 
   XFASTINT (buf->overlay_center) = pos;
 }
+
+/* Fix up overlays that were garbled as a result of permuting markers
+   in the range START through END.  Any overlay with at least one
+   endpoint in this range will need to be unlinked from the overlay
+   list and reinserted in its proper place.
+   Such an overlay might even have negative size at this point.
+   If so, we'll reverse the endpoints.  Can you think of anything
+   better to do in this situation?  */
+void
+fix_overlays_in_range (start, end)
+     register int start, end;
+{
+  Lisp_Object tem, overlay;
+  Lisp_Object before_list, after_list;
+  Lisp_Object *ptail, *pbefore = &before_list, *pafter = &after_list;
+  int startpos, endpos;
+
+  /* This algorithm shifts links around instead of consing and GCing.
+     The loop invariant is that before_list (resp. after_list) is a
+     well-formed list except that its last element, the one that
+     *pbefore (resp. *pafter) points to, is still uninitialized.
+     So it's not a bug that before_list isn't initialized, although
+     it may look strange.  */
+  for (ptail = &current_buffer->overlays_before; CONSP (*ptail);)
+    {
+      overlay = XCONS (*ptail)->car;
+      endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
+      if (endpos < start)
+       break;
+      startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
+      if (endpos < end
+         || (startpos >= start && startpos < end))
+       {
+         /* If the overlay is backwards, fix that now.  */
+         if (startpos > endpos)
+           {
+             int tem;
+             Fset_marker (OVERLAY_START (overlay), endpos, Qnil);
+             Fset_marker (OVERLAY_END (overlay), startpos, Qnil);
+             tem = startpos; startpos = endpos; endpos = tem;
+           }
+         /* Add it to the end of the wrong list.  Later on,
+            recenter_overlay_lists will move it to the right place.  */
+         if (endpos < XINT (current_buffer->overlay_center))
+           {
+             *pafter = *ptail;
+             pafter = &XCONS (*ptail)->cdr;
+           }
+         else
+           {
+             *pbefore = *ptail;
+             pbefore = &XCONS (*ptail)->cdr;
+           }
+         *ptail = XCONS (*ptail)->cdr;
+       }
+      else
+       ptail = &XCONS (*ptail)->cdr;
+    }
+  for (ptail = &current_buffer->overlays_after; CONSP (*ptail);)
+    {
+      overlay = XCONS (*ptail)->car;
+      startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
+      if (startpos >= end)
+       break;
+      endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
+      if (startpos >= start
+         || (endpos >= start && endpos < end))
+       {
+         if (startpos > endpos)
+           {
+             int tem;
+             Fset_marker (OVERLAY_START (overlay), endpos, Qnil);
+             Fset_marker (OVERLAY_END (overlay), startpos, Qnil);
+             tem = startpos; startpos = endpos; endpos = tem;
+           }
+         if (endpos < XINT (current_buffer->overlay_center))
+           {
+             *pafter = *ptail;
+             pafter = &XCONS (*ptail)->cdr;
+           }
+         else
+           {
+             *pbefore = *ptail;
+             pbefore = &XCONS (*ptail)->cdr;
+           }
+         *ptail = XCONS (*ptail)->cdr;
+       }
+      else
+       ptail = &XCONS (*ptail)->cdr;
+    }
+
+  /* Splice the constructed (wrong) lists into the buffer's lists,
+     and let the recenter function make it sane again.  */
+  *pbefore = current_buffer->overlays_before;
+  current_buffer->overlays_before = before_list;
+  recenter_overlay_lists (current_buffer,
+                         XINT (current_buffer->overlay_center));
+
+  *pafter = current_buffer->overlays_after;
+  current_buffer->overlays_after = after_list;
+  recenter_overlay_lists (current_buffer,
+                         XINT (current_buffer->overlay_center));
+}
 \f
 DEFUN ("overlayp", Foverlayp, Soverlayp, 1, 1, 0,
   "Return t if OBJECT is an overlay.")
@@ -1663,8 +1781,8 @@ BEG and END may be integers or markers.")
 
   if (XINT (beg) > XINT (end))
     {
-      Lisp_Object temp = beg;
-      beg = end; end = temp;
+      Lisp_Object temp;
+      temp = beg; beg = end; end = temp;
     }
 
   b = XBUFFER (buffer);
@@ -1720,21 +1838,44 @@ buffer.")
   CHECK_NUMBER_COERCE_MARKER (beg, 1);
   CHECK_NUMBER_COERCE_MARKER (end, 1);
 
-  specbind (Qinhibit_quit, Qt);
+  if (XINT (beg) == XINT (end) && ! NILP (Foverlay_get (overlay, Qevaporate)))
+    return Fdelete_overlay (overlay);
 
   if (XINT (beg) > XINT (end))
     {
-      Lisp_Object temp = beg;
-      beg = end; end = temp;
+      Lisp_Object temp;
+      temp = beg; beg = end; end = temp;
     }
 
+  specbind (Qinhibit_quit, Qt);
+
   obuffer = Fmarker_buffer (OVERLAY_START (overlay));
   b = XBUFFER (buffer);
   ob = XBUFFER (obuffer);
 
   /* If the overlay has changed buffers, do a thorough redisplay.  */
   if (!EQ (buffer, obuffer))
-    windows_or_buffers_changed = 1;
+    {
+      /* Redisplay where the overlay was.  */
+      if (!NILP (obuffer))
+       {
+         Lisp_Object o_beg;
+         Lisp_Object o_end;
+
+         o_beg = OVERLAY_START (overlay);
+         o_end = OVERLAY_END   (overlay);
+         o_beg = OVERLAY_POSITION (o_beg);
+         o_end = OVERLAY_POSITION (o_end);
+
+         redisplay_region (ob, XINT (o_beg), XINT (o_end));
+       }
+
+      /* Redisplay where the overlay is going to be.  */
+      redisplay_region (b, XINT (beg), XINT (end));
+
+      /* Don't limit redisplay to the selected window.  */
+      windows_or_buffers_changed = 1;
+    }
   else
     /* Redisplay the area the overlay has just left, or just enclosed.  */
     {
@@ -1887,7 +2028,8 @@ DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
 
 DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
   1, 1, 0,
-  "Return the next position after POS where an overlay starts or ends.")
+  "Return the next position after POS where an overlay starts or ends.\n\
+If there are no more overlay boundaries after POS, return (point-max).")
   (pos)
      Lisp_Object pos;
 {
@@ -1930,7 +2072,7 @@ DEFUN ("next-overlay-change", Fnext_overlay_change, Snext_overlay_change,
 DEFUN ("overlay-lists", Foverlay_lists, Soverlay_lists, 0, 0, 0,
   "Return a pair of lists giving all the overlays of the current buffer.\n\
 The car has all the overlays before the overlay center;\n\
-the cdr has all the overlays before the overlay center.\n\
+the cdr has all the overlays after the overlay center.\n\
 Recentering overlays moves overlays between these lists.\n\
 The lists you get are copies, so that changing them has no effect.\n\
 However, the overlays you get are the real objects that the buffer uses.")
@@ -1983,32 +2125,41 @@ DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
   (overlay, prop, value)
      Lisp_Object overlay, prop, value;
 {
-  Lisp_Object plist, tail;
+  Lisp_Object plist, tail, buffer;
+  int changed;
 
   CHECK_OVERLAY (overlay, 0);
 
-  tail = Fmarker_buffer (OVERLAY_START (overlay));
-  if (! NILP (tail))
-    redisplay_region (XMARKER (OVERLAY_START (overlay))->buffer,
-                     marker_position (OVERLAY_START (overlay)),
-                     marker_position (OVERLAY_END   (overlay)));
-  
+  buffer = Fmarker_buffer (OVERLAY_START (overlay));
+
   plist = Fcdr_safe (XCONS (overlay)->cdr);
 
   for (tail = plist;
        CONSP (tail) && CONSP (XCONS (tail)->cdr);
        tail = XCONS (XCONS (tail)->cdr)->cdr)
-    {
-      if (EQ (XCONS (tail)->car, prop))
-       return XCONS (XCONS (tail)->cdr)->car = value;
-    }
-
+    if (EQ (XCONS (tail)->car, prop))
+      {
+       changed = !EQ (XCONS (XCONS (tail)->cdr)->car, value);
+       XCONS (XCONS (tail)->cdr)->car = value;
+       goto found;
+      }
+  /* 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));
-
+  XCONS (XCONS (overlay)->cdr)->cdr = Fcons (prop, Fcons (value, plist));
+ found:
+  if (! NILP (buffer))
+    {
+      if (changed)
+       redisplay_region (XBUFFER (buffer),
+                         marker_position (OVERLAY_START (overlay)),
+                         marker_position (OVERLAY_END   (overlay)));
+      if (EQ (prop, Qevaporate) && ! NILP (value)
+         && (OVERLAY_POSITION (OVERLAY_START (overlay))
+             == OVERLAY_POSITION (OVERLAY_END (overlay))))
+       Fdelete_overlay (overlay);
+    }
   return value;
 }
 \f
@@ -2023,7 +2174,14 @@ verify_overlay_modification (start, end)
 {
   Lisp_Object prop, overlay, tail;
   int insertion = EQ (start, end);
+  int tail_copied;
+  struct gcpro gcpro1, gcpro2;
 
+  overlay = Qnil;
+  tail = Qnil;
+  GCPRO2 (overlay, tail);
+
+  tail_copied = 0;
   for (tail = current_buffer->overlays_before;
        CONSP (tail);
        tail = XCONS (tail)->cdr)
@@ -2042,22 +2200,42 @@ verify_overlay_modification (start, end)
       if (XFASTINT (end) == startpos && insertion)
        {
          prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
-         call_overlay_mod_hooks (prop, overlay, start, end);
+         if (!NILP (prop))
+           {
+             /* Copy TAIL in case the hook recenters the overlay lists.  */
+             if (!tail_copied)
+               tail = Fcopy_sequence (tail);
+             tail_copied = 1;
+             call_overlay_mod_hooks (prop, overlay, start, end);
+           }
        }
       if (XFASTINT (start) == endpos && insertion)
        {
          prop = Foverlay_get (overlay, Qinsert_behind_hooks);
-         call_overlay_mod_hooks (prop, overlay, start, end);
+         if (!NILP (prop))
+           {
+             if (!tail_copied)
+               tail = Fcopy_sequence (tail);
+             tail_copied = 1;
+             call_overlay_mod_hooks (prop, overlay, start, end);
+           }
        }
-      if (insertion
-         ? (XFASTINT (start) > startpos && XFASTINT (end) < endpos)
-         : (XFASTINT (start) >= startpos && XFASTINT (end) <= endpos))
+      /* Test for intersecting intervals.  This does the right thing
+        for both insertion and deletion.  */
+      if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
        {
          prop = Foverlay_get (overlay, Qmodification_hooks);
-         call_overlay_mod_hooks (prop, overlay, start, end);
+         if (!NILP (prop))
+           {
+             if (!tail_copied)
+               tail = Fcopy_sequence (tail);
+             tail_copied = 1;
+             call_overlay_mod_hooks (prop, overlay, start, end);
+           }
        }
     }
 
+  tail_copied = 0;
   for (tail = current_buffer->overlays_after;
        CONSP (tail);
        tail = XCONS (tail)->cdr)
@@ -2076,21 +2254,41 @@ verify_overlay_modification (start, end)
       if (XFASTINT (end) == startpos && insertion)
        {
          prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
-         call_overlay_mod_hooks (prop, overlay, start, end);
+         if (!NILP (prop))
+           {
+             if (!tail_copied)
+               tail = Fcopy_sequence (tail);
+             tail_copied = 1;
+             call_overlay_mod_hooks (prop, overlay, start, end);
+           }
        }
       if (XFASTINT (start) == endpos && insertion)
        {
          prop = Foverlay_get (overlay, Qinsert_behind_hooks);
-         call_overlay_mod_hooks (prop, overlay, start, end);
+         if (!NILP (prop))
+           {
+             if (!tail_copied)
+               tail = Fcopy_sequence (tail);
+             tail_copied = 1;
+             call_overlay_mod_hooks (prop, overlay, start, end);
+           }
        }
-      if (insertion
-         ? (XFASTINT (start) > startpos && XFASTINT (end) < endpos)
-         : (XFASTINT (start) >= startpos && XFASTINT (end) <= endpos))
+      /* Test for intersecting intervals.  This does the right thing
+        for both insertion and deletion.  */
+      if (XFASTINT (end) > startpos && XFASTINT (start) < endpos)
        {
          prop = Foverlay_get (overlay, Qmodification_hooks);
-         call_overlay_mod_hooks (prop, overlay, start, end);
+         if (!NILP (prop))
+           {
+             if (!tail_copied)
+               tail = Fcopy_sequence (tail);
+             tail_copied = 1;
+             call_overlay_mod_hooks (prop, overlay, start, end);
+           }
        }
     }
+
+  UNGCPRO;
 }
 
 static void
@@ -2106,6 +2304,45 @@ call_overlay_mod_hooks (list, overlay, start, end)
     }
   UNGCPRO;
 }
+
+/* Delete any zero-sized overlays at position POS, if the `evaporate'
+   property is set.  */
+void
+evaporate_overlays (pos)
+     int pos;
+{
+  Lisp_Object tail, overlay, hit_list;
+
+  hit_list = Qnil;
+  if (pos <= XFASTINT (current_buffer->overlay_center))
+    for (tail = current_buffer->overlays_before; CONSP (tail);
+        tail = XCONS (tail)->cdr)
+      {
+       int endpos;
+       overlay = XCONS (tail)->car;
+       endpos = OVERLAY_POSITION (OVERLAY_END (overlay));
+       if (endpos < pos)
+         break;
+       if (endpos == pos && OVERLAY_POSITION (OVERLAY_START (overlay)) == pos
+           && Foverlay_get (overlay, Qevaporate))
+         hit_list = Fcons (overlay, hit_list);
+      }
+  else
+    for (tail = current_buffer->overlays_after; CONSP (tail);
+        tail = XCONS (tail)->cdr)
+      {
+       int startpos;
+       overlay = XCONS (tail)->cdr;
+       startpos = OVERLAY_POSITION (OVERLAY_START (overlay));
+       if (startpos > pos)
+         break;
+       if (startpos == pos && OVERLAY_POSITION (OVERLAY_END (overlay)) == pos
+           && Foverlay_get (overlay, Qevaporate))
+         hit_list = Fcons (overlay, hit_list);
+      }
+  for (; CONSP (hit_list); hit_list = XCONS (hit_list)->cdr)
+    Fdelete_overlay (XCONS (hit_list)->car);
+}
 \f
 /* Somebody has tried to store NEWVAL into the buffer-local slot with
    offset XUINT (valcontents), and NEWVAL has an unacceptable type.  */
@@ -2142,7 +2379,9 @@ init_buffer_once ()
   /* Make sure all markable slots in buffer_defaults
      are initialized reasonably, so mark_buffer won't choke.  */
   reset_buffer (&buffer_defaults);
+  reset_buffer_local_variables (&buffer_defaults);
   reset_buffer (&buffer_local_symbols);
+  reset_buffer_local_variables (&buffer_local_symbols);
   XSET (Vbuffer_defaults, Lisp_Buffer, &buffer_defaults);
   XSET (Vbuffer_local_symbols, Lisp_Buffer, &buffer_local_symbols);
 
@@ -2172,7 +2411,7 @@ init_buffer_once ()
   buffer_defaults.ctl_arrow = Qt;
 
 #ifdef MSDOS
-  buffer_defaults.buffer_file_type = 0; /* TEXT */
+  buffer_defaults.buffer_file_type = Qnil; /* TEXT */
 #endif
   XFASTINT (buffer_defaults.fill_column) = 70;
   XFASTINT (buffer_defaults.left_margin) = 0;
@@ -2183,7 +2422,7 @@ init_buffer_once ()
      The local flag bits are in the local_var_flags slot of the buffer.  */
 
   /* Nothing can work if this isn't true */
-  if (sizeof (int) != sizeof (Lisp_Object)) abort ();
+  if (sizeof (EMACS_INT) != sizeof (Lisp_Object)) abort ();
 
   /* 0 means not a lisp var, -1 means always local, else mask */
   bzero (&buffer_local_flags, sizeof buffer_local_flags);
@@ -2291,6 +2530,8 @@ syms_of_buffer ()
   staticpro (&Qpermanent_local);
   staticpro (&Qkill_buffer_hook);
   staticpro (&Qoverlayp);
+  Qevaporate = intern ("evaporate");
+  staticpro (&Qevaporate);
   staticpro (&Qmodification_hooks);
   Qmodification_hooks = intern ("modification-hooks");
   staticpro (&Qinsert_in_front_hooks);
@@ -2387,10 +2628,12 @@ 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\
-  %* -- print *, % or hyphen.   %m -- print value of mode-name (obsolete).\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\
   %s -- print process status.   %l -- print the current line number.\n\
   %p -- print percent of buffer above top of window, or Top, Bot or All.\n\
-  %p -- print percent of buffer above bottom of window, perhaps plus Top,\n\
+  %P -- print percent of buffer above bottom of window, perhaps plus Top,\n\
         or print Bottom or All.\n\
   %n -- print Narrow if appropriate.\n\
   %t -- print T if files is text, B if binary.\n\
@@ -2557,10 +2800,13 @@ Two arguments are passed to the function: the positions of\n\
 the beginning and end of the range of old text to be changed.\n\
 \(For an insertion, the beginning and end are at the same place.)\n\
 No information is given about the length of the text after the change.\n\
-position of the change\n\
 \n\
-While executing the `before-change-function', changes to buffers do not\n\
-cause calls to any `before-change-function' or `after-change-function'.");
+Buffer changes made while executing the `before-change-function'\n\
+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.");
   Vbefore_change_function = Qnil;
 
   DEFVAR_LISP ("after-change-function", &Vafter_change_function,
@@ -2572,10 +2818,47 @@ and the length of the pre-change text replaced by that range.\n\
 for a deletion, that length is the number of characters deleted,\n\
 and the post-change beginning and end are at the same place.)\n\
 \n\
-While executing the `after-change-function', changes to buffers do not\n\
-cause calls to any `before-change-function' or `after-change-function'.");
+Buffer changes made while executing the `after-change-function'\n\
+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.");
   Vafter_change_function = Qnil;
 
+  DEFVAR_LISP ("before-change-functions", &Vbefore_change_functions,
+              "List of functions to call before each text change.\n\
+Two arguments are passed to each function: the positions of\n\
+the beginning and end of the range of old text to be changed.\n\
+\(For an insertion, the beginning and end are at the same place.)\n\
+No information is given about the length of the text after the change.\n\
+\n\
+Buffer changes made while executing the `before-change-functions'\n\
+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.");
+  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\
+\(For an insertion, the pre-change length is zero;\n\
+for a deletion, that length is the number of characters 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\
+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.");
+
+  Vafter_change_functions = Qnil;
+
   DEFVAR_LISP ("first-change-hook", &Vfirst_change_hook,
   "A list of functions to call before changing a buffer which is unmodified.\n\
 The functions are run using the `run-hooks' function.");