Add 2012 to FSF copyright years for Emacs files
[bpt/emacs.git] / src / buffer.c
index a40275d..0141895 100644 (file)
@@ -1,6 +1,6 @@
 /* Buffer manipulation primitives for GNU Emacs.
 
-Copyright (C) 1985-1989, 1993-1995, 1997-2011  Free Software Foundation, Inc.
+Copyright (C) 1985-1989, 1993-1995, 1997-2012  Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -724,7 +724,7 @@ reset_buffer (register struct buffer *b)
   b->prevent_redisplay_optimizations_p = 1;
   BVAR (b, backed_up) = Qnil;
   BUF_AUTOSAVE_MODIFF (b) = 0;
-  b->auto_save_failure_time = -1;
+  b->auto_save_failure_time = 0;
   BVAR (b, auto_save_file_name) = Qnil;
   BVAR (b, read_only) = Qnil;
   b->overlays_before = NULL;
@@ -1526,7 +1526,13 @@ with SIGHUP.  */)
       UNGCPRO;
     }
 
-  /* Make this buffer not be current.
+  /* Run replace_buffer_in_windows before making another buffer current
+     since set-window-buffer-start-and-point will refuse to make another
+     buffer current if the selected window does not show the current
+     buffer.  (Bug#10114) */
+  replace_buffer_in_windows (buffer);
+
+     /* Make this buffer not be current.
      In the process, notice if this is the sole visible buffer
      and give up if so.  */
   if (b == current_buffer)
@@ -1566,7 +1572,6 @@ with SIGHUP.  */)
 
   /* These may run Lisp code and into infinite loops (if someone
      insisted on circular lists) so allow quitting here.  */
-  replace_buffer_in_windows (buffer);
   frames_discard_buffer (buffer);
 
   clear_charpos_cache (b);
@@ -1626,7 +1631,7 @@ with SIGHUP.  */)
 
   /* Reset the local variables, so that this buffer's local values
      won't be protected from GC.  They would be protected
-     if they happened to remain encached in their symbols.
+     if they happened to remain cached in their symbols.
      This gets rid of them for certain.  */
   swap_out_buffer_local_variables (b);
   reset_buffer_local_variables (b, 1);
@@ -1698,27 +1703,16 @@ record_buffer (Lisp_Object buffer)
     call1 (Vrun_hooks, Qbuffer_list_update_hook);
 }
 
-DEFUN ("record-buffer", Frecord_buffer, Srecord_buffer, 1, 1, 0,
-       doc: /* Move BUFFER to the front of the buffer list.
-Return BUFFER.  */)
-  (Lisp_Object buffer)
-{
-  CHECK_BUFFER (buffer);
-
-  record_buffer (buffer);
-
-  return buffer;
-}
 
-  /* Move BUFFER to the end of the buffer (a)lists.  Do nothing if the
-     buffer is killed.  For the selected frame's buffer list this moves
-     BUFFER to its end even if it was never shown in that frame.  If
-     this happens we have a feature, hence `unrecord-buffer' should be
-     called only when BUFFER was shown in the selected frame.  */
+/* Move BUFFER to the end of the buffer (a)lists.  Do nothing if the
+   buffer is killed.  For the selected frame's buffer list this moves
+   BUFFER to its end even if it was never shown in that frame.  If
+   this happens we have a feature, hence `unrecord-buffer' should be
+   called only when BUFFER was shown in the selected frame.  */
 
-DEFUN ("unrecord-buffer", Funrecord_buffer, Sunrecord_buffer, 1, 1, 0,
-       doc: /* Move BUFFER to the end of the buffer list.
-Return BUFFER.  */)
+DEFUN ("bury-buffer-internal", Fbury_buffer_internal, Sbury_buffer_internal,
+       1, 1, 0,
+       doc: /* Move BUFFER to the end of the buffer list.  */)
   (Lisp_Object buffer)
 {
   Lisp_Object aelt, aelt_cons, tem;
@@ -1746,7 +1740,7 @@ Return BUFFER.  */)
   if (!NILP (Vrun_hooks))
     call1 (Vrun_hooks, Qbuffer_list_update_hook);
 
-  return buffer;
+  return Qnil;
 }
 
 DEFUN ("set-buffer-major-mode", Fset_buffer_major_mode, Sset_buffer_major_mode, 1, 1, 0,
@@ -2492,7 +2486,7 @@ swap_out_buffer_local_variables (struct buffer *b)
       Lisp_Object sym = XCAR (XCAR (alist));
       eassert (XSYMBOL (sym)->redirect == SYMBOL_LOCALIZED);
       /* Need not do anything if some other buffer's binding is
-        now encached.  */
+        now cached.  */
       if (EQ (SYMBOL_BLV (XSYMBOL (sym))->where, buffer))
        {
          /* Symbol is set up for this buffer's old local value:
@@ -2568,13 +2562,10 @@ overlays_at (EMACS_INT pos, int extend, Lisp_Object **vec_ptr,
                 Either make it bigger, or don't store any more in it.  */
              if (extend)
                {
-                 if ((OVERLAY_COUNT_MAX - 4) / 2 < len)
-                   memory_full (SIZE_MAX);
-                 /* Make it work with an initial len == 0.  */
-                 len = len * 2 + 4;
-                 *len_ptr = len;
-                 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
+                 vec = xpalloc (vec, len_ptr, 1, OVERLAY_COUNT_MAX,
+                                sizeof *vec);
                  *vec_ptr = vec;
+                 len = *len_ptr;
                }
              else
                inhibit_storing = 1;
@@ -2611,13 +2602,10 @@ overlays_at (EMACS_INT pos, int extend, Lisp_Object **vec_ptr,
            {
              if (extend)
                {
-                 if ((OVERLAY_COUNT_MAX - 4) / 2 < len)
-                   memory_full (SIZE_MAX);
-                 /* Make it work with an initial len == 0.  */
-                 len = len * 2 + 4;
-                 *len_ptr = len;
-                 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
+                 vec = xpalloc (vec, len_ptr, 1, OVERLAY_COUNT_MAX,
+                                sizeof *vec);
                  *vec_ptr = vec;
+                 len = *len_ptr;
                }
              else
                inhibit_storing = 1;
@@ -2708,13 +2696,10 @@ overlays_in (EMACS_INT beg, EMACS_INT end, int extend,
                 Either make it bigger, or don't store any more in it.  */
              if (extend)
                {
-                 if ((OVERLAY_COUNT_MAX - 4) / 2 < len)
-                   memory_full (SIZE_MAX);
-                 /* Make it work with an initial len == 0.  */
-                 len = len * 2 + 4;
-                 *len_ptr = len;
-                 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
+                 vec = xpalloc (vec, len_ptr, 1, OVERLAY_COUNT_MAX,
+                                sizeof *vec);
                  *vec_ptr = vec;
+                 len = *len_ptr;
                }
              else
                inhibit_storing = 1;
@@ -2756,13 +2741,10 @@ overlays_in (EMACS_INT beg, EMACS_INT end, int extend,
            {
              if (extend)
                {
-                 if ((OVERLAY_COUNT_MAX - 4) / 2 < len)
-                   memory_full (SIZE_MAX);
-                 /* Make it work with an initial len == 0.  */
-                 len = len * 2 + 4;
-                 *len_ptr = len;
-                 vec = (Lisp_Object *) xrealloc (vec, len * sizeof (Lisp_Object));
+                 vec = xpalloc (vec, len_ptr, 1, OVERLAY_COUNT_MAX,
+                                sizeof *vec);
                  *vec_ptr = vec;
+                 len = *len_ptr;
                }
              else
                inhibit_storing = 1;
@@ -2944,7 +2926,7 @@ struct sortstrlist
   struct sortstr *buf; /* An array that expands as needed; never freed.  */
   ptrdiff_t size;      /* Allocated length of that array.  */
   ptrdiff_t used;      /* How much of the array is currently in use.  */
-  EMACS_INT bytes;             /* Total length of the strings in buf.  */
+  ptrdiff_t bytes;     /* Total length of the strings in buf.  */
 };
 
 /* Buffers for storing information about the overlays touching a given
@@ -2955,7 +2937,7 @@ static struct sortstrlist overlay_heads, overlay_tails;
 static unsigned char *overlay_str_buf;
 
 /* Allocated length of overlay_str_buf.  */
-static EMACS_INT overlay_str_len;
+static ptrdiff_t overlay_str_len;
 
 /* A comparison function suitable for passing to qsort.  */
 static int
@@ -2977,17 +2959,7 @@ record_overlay_string (struct sortstrlist *ssl, Lisp_Object str,
   EMACS_INT nbytes;
 
   if (ssl->used == ssl->size)
-    {
-      if (min (PTRDIFF_MAX, SIZE_MAX) / (sizeof (struct sortstr) * 2)
-         < ssl->size)
-       memory_full (SIZE_MAX);
-      else if (0 < ssl->size)
-       ssl->size *= 2;
-      else
-       ssl->size = 5;
-      ssl->buf = ((struct sortstr *)
-                 xrealloc (ssl->buf, ssl->size * sizeof (struct sortstr)));
-    }
+    ssl->buf = xpalloc (ssl->buf, &ssl->size, 5, -1, sizeof *ssl->buf);
   ssl->buf[ssl->used].string = str;
   ssl->buf[ssl->used].string2 = str2;
   ssl->buf[ssl->used].size = size;
@@ -3002,6 +2974,8 @@ record_overlay_string (struct sortstrlist *ssl, Lisp_Object str,
   else
     nbytes = SBYTES (str);
 
+  if (INT_ADD_OVERFLOW (ssl->bytes, nbytes))
+    memory_full (SIZE_MAX);
   ssl->bytes += nbytes;
 
   if (STRINGP (str2))
@@ -3014,6 +2988,8 @@ record_overlay_string (struct sortstrlist *ssl, Lisp_Object str,
       else
        nbytes = SBYTES (str2);
 
+      if (INT_ADD_OVERFLOW (ssl->bytes, nbytes))
+       memory_full (SIZE_MAX);
       ssl->bytes += nbytes;
     }
 }
@@ -3107,14 +3083,15 @@ overlay_strings (EMACS_INT pos, struct window *w, unsigned char **pstr)
       Lisp_Object tem;
       EMACS_INT i;
       unsigned char *p;
-      EMACS_INT total = overlay_heads.bytes + overlay_tails.bytes;
+      ptrdiff_t total;
 
+      if (INT_ADD_OVERFLOW (overlay_heads.bytes, overlay_tails.bytes))
+       memory_full (SIZE_MAX);
+      total = overlay_heads.bytes + overlay_tails.bytes;
       if (total > overlay_str_len)
-       {
-         overlay_str_len = total;
-         overlay_str_buf = (unsigned char *)xrealloc (overlay_str_buf,
-                                                      total);
-       }
+       overlay_str_buf = xpalloc (overlay_str_buf, &overlay_str_len,
+                                  total - overlay_str_len, -1, 1);
+
       p = overlay_str_buf;
       for (i = overlay_tails.used; --i >= 0;)
        {
@@ -4428,7 +4405,7 @@ static int mmap_fd_1;
 
 static int mmap_page_size;
 
-/* 1 means mmap has been intialized.  */
+/* 1 means mmap has been initialized.  */
 
 static int mmap_initialized_p;
 
@@ -4459,7 +4436,7 @@ static int mmap_initialized_p;
    is currently mapped.  Used to prevent overwriting an existing
    memory mapping.
 
-   Default is to conservativly assume the address range is occupied by
+   Default is to conservatively assume the address range is occupied by
    something else.  This can be overridden by system configuration
    files if system-specific means to determine this exists.  */
 
@@ -4467,7 +4444,7 @@ static int mmap_initialized_p;
 #define MMAP_ALLOCATED_P(start, end) 1
 #endif
 
-/* Perform necessary intializations for the use of mmap.  */
+/* Perform necessary initializations for the use of mmap.  */
 
 static void
 mmap_init (void)
@@ -4926,7 +4903,7 @@ init_buffer_once (void)
   BVAR (&buffer_defaults, truncate_lines) = Qnil;
   BVAR (&buffer_defaults, word_wrap) = Qnil;
   BVAR (&buffer_defaults, ctl_arrow) = Qt;
-  BVAR (&buffer_defaults, bidi_display_reordering) = Qnil;
+  BVAR (&buffer_defaults, bidi_display_reordering) = Qt;
   BVAR (&buffer_defaults, bidi_paragraph_direction) = Qnil;
   BVAR (&buffer_defaults, cursor_type) = Qt;
   BVAR (&buffer_defaults, extra_line_spacing) = Qnil;
@@ -5476,9 +5453,7 @@ file I/O and the behavior of various editing commands.
 
 This variable is buffer-local but you cannot set it directly;
 use the function `set-buffer-multibyte' to change a buffer's representation.
-Changing its default value with `setq-default' is supported.
-See also variable `default-enable-multibyte-characters' and Info node
-`(elisp)Text Representations'.  */);
+See also Info node `(elisp)Text Representations'.  */);
   XSYMBOL (intern_c_string ("enable-multibyte-characters"))->constant = 1;
 
   DEFVAR_PER_BUFFER ("buffer-file-coding-system",
@@ -5521,7 +5496,9 @@ Instead, give each line of text just one screen line.
 
 Note that this is overridden by the variable
 `truncate-partial-width-windows' if that variable is non-nil
-and this buffer is not full-frame width.  */);
+and this buffer is not full-frame width.
+
+Minibuffers set this variable to nil.  */);
 
   DEFVAR_PER_BUFFER ("word-wrap", &BVAR (current_buffer, word_wrap), Qnil,
                     doc: /* *Non-nil means to use word-wrapping for continuation lines.
@@ -6034,8 +6011,7 @@ Functions running this hook are `get-buffer-create',
   defsubr (&Sother_buffer);
   defsubr (&Sbuffer_enable_undo);
   defsubr (&Skill_buffer);
-  defsubr (&Srecord_buffer);
-  defsubr (&Sunrecord_buffer);
+  defsubr (&Sbury_buffer_internal);
   defsubr (&Sset_buffer_major_mode);
   defsubr (&Scurrent_buffer);
   defsubr (&Sset_buffer);