Add 2012 to FSF copyright years for Emacs files
[bpt/emacs.git] / src / buffer.c
index fd51f50..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.
 
@@ -94,6 +94,11 @@ static Lisp_Object Vbuffer_local_symbols;
 #define PER_BUFFER_SYMBOL(OFFSET) \
       (*(Lisp_Object *)((OFFSET) + (char *) &buffer_local_symbols))
 
+/* Maximum length of an overlay vector.  */
+#define OVERLAY_COUNT_MAX                                              \
+  ((ptrdiff_t) min (MOST_POSITIVE_FIXNUM,                              \
+                   min (PTRDIFF_MAX, SIZE_MAX) / sizeof (Lisp_Object)))
+
 /* Flags indicating which built-in buffer-local variables
    are permanent locals.  */
 static char buffer_permanent_local_flags[MAX_PER_BUFFER_VARS];
@@ -147,7 +152,7 @@ Lisp_Object Qmodification_hooks;
 Lisp_Object Qinsert_in_front_hooks;
 Lisp_Object Qinsert_behind_hooks;
 
-static void alloc_buffer_text (struct buffer *, size_t);
+static void alloc_buffer_text (struct buffer *, ptrdiff_t);
 static void free_buffer_text (struct buffer *b);
 static struct Lisp_Overlay * copy_overlays (struct buffer *, struct Lisp_Overlay *);
 static void modify_overlay (struct buffer *, EMACS_INT, EMACS_INT);
@@ -356,6 +361,7 @@ even if it is dead.  The return value is never nil.  */)
   BUF_END_UNCHANGED (b) = 0;
   BUF_BEG_UNCHANGED (b) = 0;
   *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'.  */
+  b->text->inhibit_shrinking = 0;
 
   b->newline_cache = 0;
   b->width_run_cache = 0;
@@ -466,8 +472,8 @@ clone_per_buffer_values (struct buffer *from, struct buffer *to)
 
   /* buffer-local Lisp variables start at `undo_list',
      tho only the ones from `name' on are GC'd normally.  */
-  for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
-       offset < sizeof *to;
+  for (offset = PER_BUFFER_VAR_OFFSET (FIRST_FIELD_PER_BUFFER);
+       offset <= PER_BUFFER_VAR_OFFSET (LAST_FIELD_PER_BUFFER);
        offset += sizeof (Lisp_Object))
     {
       Lisp_Object obj;
@@ -718,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;
@@ -825,8 +831,8 @@ reset_buffer_local_variables (register struct buffer *b, int permanent_too)
 
   /* buffer-local Lisp variables start at `undo_list',
      tho only the ones from `name' on are GC'd normally.  */
-  for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
-       offset < sizeof *b;
+  for (offset = PER_BUFFER_VAR_OFFSET (FIRST_FIELD_PER_BUFFER);
+       offset <= PER_BUFFER_VAR_OFFSET (LAST_FIELD_PER_BUFFER);
        offset += sizeof (Lisp_Object))
     {
       int idx = PER_BUFFER_IDX (offset);
@@ -1050,8 +1056,8 @@ No argument or nil as argument means use current buffer as BUFFER.  */)
 
     /* buffer-local Lisp variables start at `undo_list',
        tho only the ones from `name' on are GC'd normally.  */
-    for (offset = PER_BUFFER_VAR_OFFSET (undo_list);
-        offset < sizeof (struct buffer);
+    for (offset = PER_BUFFER_VAR_OFFSET (FIRST_FIELD_PER_BUFFER);
+        offset <= PER_BUFFER_VAR_OFFSET (LAST_FIELD_PER_BUFFER);
         /* sizeof EMACS_INT == sizeof Lisp_Object */
         offset += (sizeof (EMACS_INT)))
       {
@@ -1520,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)
@@ -1560,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);
@@ -1620,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);
@@ -1692,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;
@@ -1740,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,
@@ -2486,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:
@@ -2518,14 +2518,15 @@ swap_out_buffer_local_variables (struct buffer *b)
    *NEXT_PTR is guaranteed to be not equal to POS, unless it is the
    default (BEGV or ZV).  */
 
-int
-overlays_at (EMACS_INT pos, int extend, Lisp_Object **vec_ptr, int *len_ptr,
+ptrdiff_t
+overlays_at (EMACS_INT pos, int extend, Lisp_Object **vec_ptr,
+            ptrdiff_t *len_ptr,
             EMACS_INT *next_ptr, EMACS_INT *prev_ptr, int change_req)
 {
   Lisp_Object overlay, start, end;
   struct Lisp_Overlay *tail;
-  int idx = 0;
-  int len = *len_ptr;
+  ptrdiff_t idx = 0;
+  ptrdiff_t len = *len_ptr;
   Lisp_Object *vec = *vec_ptr;
   EMACS_INT next = ZV;
   EMACS_INT prev = BEGV;
@@ -2561,13 +2562,10 @@ overlays_at (EMACS_INT pos, int extend, Lisp_Object **vec_ptr, int *len_ptr,
                 Either make it bigger, or don't store any more in it.  */
              if (extend)
                {
-                 /* 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 = xpalloc (vec, len_ptr, 1, OVERLAY_COUNT_MAX,
+                                sizeof *vec);
                  *vec_ptr = vec;
+                 len = *len_ptr;
                }
              else
                inhibit_storing = 1;
@@ -2604,13 +2602,10 @@ overlays_at (EMACS_INT pos, int extend, Lisp_Object **vec_ptr, int *len_ptr,
            {
              if (extend)
                {
-                 /* 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 = xpalloc (vec, len_ptr, 1, OVERLAY_COUNT_MAX,
+                                sizeof *vec);
                  *vec_ptr = vec;
+                 len = *len_ptr;
                }
              else
                inhibit_storing = 1;
@@ -2657,15 +2652,15 @@ overlays_at (EMACS_INT pos, int extend, Lisp_Object **vec_ptr, int *len_ptr,
    and we store only as many overlays as will fit.
    But we still return the total number of overlays.  */
 
-static int
+static ptrdiff_t
 overlays_in (EMACS_INT beg, EMACS_INT end, int extend,
-            Lisp_Object **vec_ptr, int *len_ptr,
+            Lisp_Object **vec_ptr, ptrdiff_t *len_ptr,
             EMACS_INT *next_ptr, EMACS_INT *prev_ptr)
 {
   Lisp_Object overlay, ostart, oend;
   struct Lisp_Overlay *tail;
-  int idx = 0;
-  int len = *len_ptr;
+  ptrdiff_t idx = 0;
+  ptrdiff_t len = *len_ptr;
   Lisp_Object *vec = *vec_ptr;
   EMACS_INT next = ZV;
   EMACS_INT prev = BEGV;
@@ -2701,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)
                {
-                 /* 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 = xpalloc (vec, len_ptr, 1, OVERLAY_COUNT_MAX,
+                                sizeof *vec);
                  *vec_ptr = vec;
+                 len = *len_ptr;
                }
              else
                inhibit_storing = 1;
@@ -2749,13 +2741,10 @@ overlays_in (EMACS_INT beg, EMACS_INT end, int extend,
            {
              if (extend)
                {
-                 /* 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 = xpalloc (vec, len_ptr, 1, OVERLAY_COUNT_MAX,
+                                sizeof *vec);
                  *vec_ptr = vec;
+                 len = *len_ptr;
                }
              else
                inhibit_storing = 1;
@@ -2785,7 +2774,7 @@ mouse_face_overlay_overlaps (Lisp_Object overlay)
 {
   EMACS_INT start = OVERLAY_POSITION (OVERLAY_START (overlay));
   EMACS_INT end = OVERLAY_POSITION (OVERLAY_END (overlay));
-  int n, i, size;
+  ptrdiff_t n, i, size;
   Lisp_Object *v, tem;
 
   size = 10;
@@ -2871,10 +2860,10 @@ compare_overlays (const void *v1, const void *v2)
 /* Sort an array of overlays by priority.  The array is modified in place.
    The return value is the new size; this may be smaller than the original
    size if some of the overlays were invalid or were window-specific.  */
-int
-sort_overlays (Lisp_Object *overlay_vec, int noverlays, struct window *w)
+ptrdiff_t
+sort_overlays (Lisp_Object *overlay_vec, ptrdiff_t noverlays, struct window *w)
 {
-  int i, j;
+  ptrdiff_t i, j;
   struct sortvec *sortvec;
   sortvec = (struct sortvec *) alloca (noverlays * sizeof (struct sortvec));
 
@@ -2937,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
@@ -2948,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
@@ -2970,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;
@@ -2995,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))
@@ -3007,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;
     }
 }
@@ -3100,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;)
        {
@@ -3880,9 +3864,8 @@ DEFUN ("overlays-at", Foverlays_at, Soverlays_at, 1, 1, 0,
        doc: /* Return a list of the overlays that contain the character at POS.  */)
   (Lisp_Object pos)
 {
-  int noverlays;
+  ptrdiff_t len, noverlays;
   Lisp_Object *overlay_vec;
-  int len;
   Lisp_Object result;
 
   CHECK_NUMBER_COERCE_MARKER (pos);
@@ -3912,9 +3895,8 @@ between BEG and END, or at END provided END denotes the position at the
 end of the buffer.  */)
   (Lisp_Object beg, Lisp_Object end)
 {
-  int noverlays;
+  ptrdiff_t len, noverlays;
   Lisp_Object *overlay_vec;
-  int len;
   Lisp_Object result;
 
   CHECK_NUMBER_COERCE_MARKER (beg);
@@ -3942,11 +3924,9 @@ If there are no overlay boundaries from POS to (point-max),
 the value is (point-max).  */)
   (Lisp_Object pos)
 {
-  int noverlays;
+  ptrdiff_t i, len, noverlays;
   EMACS_INT endpos;
   Lisp_Object *overlay_vec;
-  int len;
-  int i;
 
   CHECK_NUMBER_COERCE_MARKER (pos);
 
@@ -3985,7 +3965,7 @@ the value is (point-min).  */)
 {
   EMACS_INT prevpos;
   Lisp_Object *overlay_vec;
-  int len;
+  ptrdiff_t len;
 
   CHECK_NUMBER_COERCE_MARKER (pos);
 
@@ -4054,7 +4034,8 @@ DEFUN ("overlay-get", Foverlay_get, Soverlay_get, 2, 2, 0,
 }
 
 DEFUN ("overlay-put", Foverlay_put, Soverlay_put, 3, 3, 0,
-       doc: /* Set one property of overlay OVERLAY: give property PROP value VALUE.  */)
+       doc: /* Set one property of overlay OVERLAY: give property PROP value VALUE.
+VALUE will be returned.*/)
   (Lisp_Object overlay, Lisp_Object prop, Lisp_Object value)
 {
   Lisp_Object tail, buffer;
@@ -4424,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;
 
@@ -4455,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.  */
 
@@ -4463,24 +4444,40 @@ static int mmap_initialized_p;
 #define MMAP_ALLOCATED_P(start, end) 1
 #endif
 
-/* Function prototypes.  */
+/* Perform necessary initializations for the use of mmap.  */
 
-static int mmap_free_1 (struct mmap_region *);
-static int mmap_enlarge (struct mmap_region *, int);
-static struct mmap_region *mmap_find (POINTER_TYPE *, POINTER_TYPE *);
-static POINTER_TYPE *mmap_alloc (POINTER_TYPE **, size_t);
-static POINTER_TYPE *mmap_realloc (POINTER_TYPE **, size_t);
-static void mmap_free (POINTER_TYPE **ptr);
-static void mmap_init (void);
+static void
+mmap_init (void)
+{
+#if MAP_ANON == 0
+  /* The value of mmap_fd is initially 0 in temacs, and -1
+     in a dumped Emacs.  */
+  if (mmap_fd <= 0)
+    {
+      /* No anonymous mmap -- we need the file descriptor.  */
+      mmap_fd = open ("/dev/zero", O_RDONLY);
+      if (mmap_fd == -1)
+       fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
+    }
+#endif /* MAP_ANON == 0 */
 
+  if (mmap_initialized_p)
+    return;
+  mmap_initialized_p = 1;
+
+#if MAP_ANON != 0
+  mmap_fd = -1;
+#endif
+
+  mmap_page_size = getpagesize ();
+}
 
 /* Return a region overlapping address range START...END, or null if
    none.  END is not including, i.e. the last byte in the range
    is at END - 1.  */
 
 static struct mmap_region *
-mmap_find (start, end)
-     POINTER_TYPE *start, *end;
+mmap_find (POINTER_TYPE *start, POINTER_TYPE *end)
 {
   struct mmap_region *r;
   char *s = (char *) start, *e = (char *) end;
@@ -4509,8 +4506,7 @@ mmap_find (start, end)
    the region.  Value is non-zero if successful.  */
 
 static int
-mmap_free_1 (r)
-     struct mmap_region *r;
+mmap_free_1 (struct mmap_region *r)
 {
   if (r->next)
     r->next->prev = r->prev;
@@ -4533,9 +4529,7 @@ mmap_free_1 (r)
    Value is non-zero if successful.  */
 
 static int
-mmap_enlarge (r, npages)
-     struct mmap_region *r;
-     int npages;
+mmap_enlarge (struct mmap_region *r, int npages)
 {
   char *region_end = (char *) r + r->nbytes_mapped;
   size_t nbytes;
@@ -4599,8 +4593,7 @@ mmap_enlarge (r, npages)
    when Emacs starts.  */
 
 void
-mmap_set_vars (restore_p)
-     int restore_p;
+mmap_set_vars (int restore_p)
 {
   struct mmap_region *r;
 
@@ -4633,9 +4626,7 @@ mmap_set_vars (restore_p)
    return null.  */
 
 static POINTER_TYPE *
-mmap_alloc (var, nbytes)
-     POINTER_TYPE **var;
-     size_t nbytes;
+mmap_alloc (POINTER_TYPE **var, size_t nbytes)
 {
   void *p;
   size_t map;
@@ -4672,15 +4663,29 @@ mmap_alloc (var, nbytes)
 }
 
 
+/* Free a block of relocatable storage whose data is pointed to by
+   PTR.  Store 0 in *PTR to show there's no block allocated.  */
+
+static void
+mmap_free (POINTER_TYPE **var)
+{
+  mmap_init ();
+
+  if (*var)
+    {
+      mmap_free_1 (MMAP_REGION (*var));
+      *var = NULL;
+    }
+}
+
+
 /* Given a pointer at address VAR to data allocated with mmap_alloc,
    resize it to size NBYTES.  Change *VAR to reflect the new block,
    and return this value.  If more memory cannot be allocated, then
    leave *VAR unchanged, and return null.  */
 
 static POINTER_TYPE *
-mmap_realloc (var, nbytes)
-     POINTER_TYPE **var;
-     size_t nbytes;
+mmap_realloc (POINTER_TYPE **var, size_t nbytes)
 {
   POINTER_TYPE *result;
 
@@ -4750,51 +4755,6 @@ mmap_realloc (var, nbytes)
 }
 
 
-/* Free a block of relocatable storage whose data is pointed to by
-   PTR.  Store 0 in *PTR to show there's no block allocated.  */
-
-static void
-mmap_free (var)
-     POINTER_TYPE **var;
-{
-  mmap_init ();
-
-  if (*var)
-    {
-      mmap_free_1 (MMAP_REGION (*var));
-      *var = NULL;
-    }
-}
-
-
-/* Perform necessary intializations for the use of mmap.  */
-
-static void
-mmap_init ()
-{
-#if MAP_ANON == 0
-  /* The value of mmap_fd is initially 0 in temacs, and -1
-     in a dumped Emacs.  */
-  if (mmap_fd <= 0)
-    {
-      /* No anonymous mmap -- we need the file descriptor.  */
-      mmap_fd = open ("/dev/zero", O_RDONLY);
-      if (mmap_fd == -1)
-       fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
-    }
-#endif /* MAP_ANON == 0 */
-
-  if (mmap_initialized_p)
-    return;
-  mmap_initialized_p = 1;
-
-#if MAP_ANON != 0
-  mmap_fd = -1;
-#endif
-
-  mmap_page_size = getpagesize ();
-}
-
 #endif /* USE_MMAP_FOR_BUFFERS */
 
 
@@ -4813,7 +4773,7 @@ extern void r_alloc_free (POINTER_TYPE **ptr);
 /* Allocate NBYTES bytes for buffer B's text buffer.  */
 
 static void
-alloc_buffer_text (struct buffer *b, size_t nbytes)
+alloc_buffer_text (struct buffer *b, ptrdiff_t nbytes)
 {
   POINTER_TYPE *p;
 
@@ -4843,8 +4803,8 @@ void
 enlarge_buffer_text (struct buffer *b, EMACS_INT delta)
 {
   POINTER_TYPE *p;
-  size_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1
-                  + delta);
+  ptrdiff_t nbytes = (BUF_Z_BYTE (b) - BUF_BEG_BYTE (b) + BUF_GAP_SIZE (b) + 1
+                     + delta);
   BLOCK_INPUT;
 #if defined USE_MMAP_FOR_BUFFERS
   p = mmap_realloc ((POINTER_TYPE **) &b->text->beg, nbytes);
@@ -4943,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;
@@ -5083,7 +5043,7 @@ init_buffer (void)
 {
   char *pwd;
   Lisp_Object temp;
-  int len;
+  ptrdiff_t len;
 
 #ifdef USE_MMAP_FOR_BUFFERS
  {
@@ -5207,39 +5167,26 @@ syms_of_buffer (void)
   staticpro (&Vbuffer_alist);
   staticpro (&Qprotected_field);
   staticpro (&Qpermanent_local);
-  Qpermanent_local_hook = intern_c_string ("permanent-local-hook");
-  staticpro (&Qpermanent_local_hook);
   staticpro (&Qkill_buffer_hook);
-  Qoverlayp = intern_c_string ("overlayp");
-  staticpro (&Qoverlayp);
-  Qevaporate = intern_c_string ("evaporate");
-  staticpro (&Qevaporate);
-  Qmodification_hooks = intern_c_string ("modification-hooks");
-  staticpro (&Qmodification_hooks);
-  Qinsert_in_front_hooks = intern_c_string ("insert-in-front-hooks");
-  staticpro (&Qinsert_in_front_hooks);
-  Qinsert_behind_hooks = intern_c_string ("insert-behind-hooks");
-  staticpro (&Qinsert_behind_hooks);
-  Qget_file_buffer = intern_c_string ("get-file-buffer");
-  staticpro (&Qget_file_buffer);
-  Qpriority = intern_c_string ("priority");
-  staticpro (&Qpriority);
-  Qbefore_string = intern_c_string ("before-string");
-  staticpro (&Qbefore_string);
-  Qafter_string = intern_c_string ("after-string");
-  staticpro (&Qafter_string);
-  Qfirst_change_hook = intern_c_string ("first-change-hook");
-  staticpro (&Qfirst_change_hook);
-  Qbefore_change_functions = intern_c_string ("before-change-functions");
-  staticpro (&Qbefore_change_functions);
-  Qafter_change_functions = intern_c_string ("after-change-functions");
-  staticpro (&Qafter_change_functions);
+
+  DEFSYM (Qpermanent_local_hook, "permanent-local-hook");
+  DEFSYM (Qoverlayp, "overlayp");
+  DEFSYM (Qevaporate, "evaporate");
+  DEFSYM (Qmodification_hooks, "modification-hooks");
+  DEFSYM (Qinsert_in_front_hooks, "insert-in-front-hooks");
+  DEFSYM (Qinsert_behind_hooks, "insert-behind-hooks");
+  DEFSYM (Qget_file_buffer, "get-file-buffer");
+  DEFSYM (Qpriority, "priority");
+  DEFSYM (Qbefore_string, "before-string");
+  DEFSYM (Qafter_string, "after-string");
+  DEFSYM (Qfirst_change_hook, "first-change-hook");
+  DEFSYM (Qbefore_change_functions, "before-change-functions");
+  DEFSYM (Qafter_change_functions, "after-change-functions");
+  DEFSYM (Qkill_buffer_query_functions, "kill-buffer-query-functions");
+
   /* The next one is initialized in init_buffer_once.  */
   staticpro (&Qucs_set_table_for_input);
 
-  Qkill_buffer_query_functions = intern_c_string ("kill-buffer-query-functions");
-  staticpro (&Qkill_buffer_query_functions);
-
   Fput (Qprotected_field, Qerror_conditions,
        pure_cons (Qprotected_field, pure_cons (Qerror, Qnil)));
   Fput (Qprotected_field, Qerror_message,
@@ -5506,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",
@@ -5551,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.
@@ -6033,8 +5980,7 @@ If any of them returns nil, the buffer is not killed.  */);
               doc: /* Normal hook run before changing the major mode of a buffer.
 The function `kill-all-local-variables' runs this before doing anything else.  */);
   Vchange_major_mode_hook = Qnil;
-  Qchange_major_mode_hook = intern_c_string ("change-major-mode-hook");
-  staticpro (&Qchange_major_mode_hook);
+  DEFSYM (Qchange_major_mode_hook, "change-major-mode-hook");
 
   DEFVAR_LISP ("buffer-list-update-hook", Vbuffer_list_update_hook,
               doc: /* Hook run when the buffer list changes.
@@ -6042,8 +5988,7 @@ Functions running this hook are `get-buffer-create',
 `make-indirect-buffer', `rename-buffer', `kill-buffer',
 `record-buffer' and `unrecord-buffer'.  */);
   Vbuffer_list_update_hook = Qnil;
-  Qbuffer_list_update_hook = intern_c_string ("buffer-list-update-hook");
-  staticpro (&Qbuffer_list_update_hook);
+  DEFSYM (Qbuffer_list_update_hook, "buffer-list-update-hook");
 
   defsubr (&Sbuffer_live_p);
   defsubr (&Sbuffer_list);
@@ -6066,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);