(X_IO_BUG): Defined.
[bpt/emacs.git] / src / buffer.c
index 1438a70..8ebfe98 100644 (file)
@@ -148,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;
 {
@@ -585,6 +581,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.
@@ -1635,6 +1635,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.")
@@ -1894,7 +1997,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;
 {
@@ -1937,7 +2041,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.")
@@ -2030,7 +2134,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)
@@ -2052,7 +2163,9 @@ verify_overlay_modification (start, end)
          if (!NILP (prop))
            {
              /* Copy TAIL in case the hook recenters the overlay lists.  */
-             tail = Fcopy_sequence (tail);
+             if (!tail_copied)
+               tail = Fcopy_sequence (tail);
+             tail_copied = 1;
              call_overlay_mod_hooks (prop, overlay, start, end);
            }
        }
@@ -2061,7 +2174,9 @@ verify_overlay_modification (start, end)
          prop = Foverlay_get (overlay, Qinsert_behind_hooks);
          if (!NILP (prop))
            {
-             tail = Fcopy_sequence (tail);
+             if (!tail_copied)
+               tail = Fcopy_sequence (tail);
+             tail_copied = 1;
              call_overlay_mod_hooks (prop, overlay, start, end);
            }
        }
@@ -2072,12 +2187,15 @@ verify_overlay_modification (start, end)
          prop = Foverlay_get (overlay, Qmodification_hooks);
          if (!NILP (prop))
            {
-             tail = Fcopy_sequence (tail);
+             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)
@@ -2098,7 +2216,9 @@ verify_overlay_modification (start, end)
          prop = Foverlay_get (overlay, Qinsert_in_front_hooks);
          if (!NILP (prop))
            {
-             tail = Fcopy_sequence (tail);
+             if (!tail_copied)
+               tail = Fcopy_sequence (tail);
+             tail_copied = 1;
              call_overlay_mod_hooks (prop, overlay, start, end);
            }
        }
@@ -2107,7 +2227,9 @@ verify_overlay_modification (start, end)
          prop = Foverlay_get (overlay, Qinsert_behind_hooks);
          if (!NILP (prop))
            {
-             tail = Fcopy_sequence (tail);
+             if (!tail_copied)
+               tail = Fcopy_sequence (tail);
+             tail_copied = 1;
              call_overlay_mod_hooks (prop, overlay, start, end);
            }
        }
@@ -2118,11 +2240,15 @@ verify_overlay_modification (start, end)
          prop = Foverlay_get (overlay, Qmodification_hooks);
          if (!NILP (prop))
            {
-             tail = Fcopy_sequence (tail);
+             if (!tail_copied)
+               tail = Fcopy_sequence (tail);
+             tail_copied = 1;
              call_overlay_mod_hooks (prop, overlay, start, end);
            }
        }
     }
+
+  UNGCPRO;
 }
 
 static void
@@ -2422,7 +2548,7 @@ A string is printed verbatim in the mode line except for %-constructs:\n\
   %* -- print *, % or hyphen.   %m -- print value of mode-name (obsolete).\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\
@@ -2589,10 +2715,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\
 Buffer changes made while executing the `before-change-function'\n\
-don't call any before-change or after-change functions.");
+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,
@@ -2605,7 +2734,11 @@ 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-function'\n\
-don't call any before-change or after-change functions.");
+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,
@@ -2614,10 +2747,13 @@ 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\
-position of the change\n\
 \n\
 Buffer changes made while executing the `before-change-functions'\n\
-don't call any before-change or after-change functions.");
+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,
@@ -2630,7 +2766,12 @@ 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.");
+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,