src/*.c: Remove some additional unused parameters.
[bpt/emacs.git] / src / editfns.c
index 1bd6682..b04414e 100644 (file)
@@ -1,8 +1,6 @@
 /* Lisp functions pertaining to editing.
 
-Copyright (C) 1985, 1986, 1987, 1989, 1993, 1994, 1995, 1996, 1997,
-  1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
-  2009, 2010 Free Software Foundation, Inc.
+Copyright (C) 1985-1987, 1989, 1993-2011 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -29,9 +27,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <pwd.h>
 #endif
 
-#ifdef HAVE_UNISTD_H
 #include <unistd.h>
-#endif
 
 #ifdef HAVE_SYS_UTSNAME_H
 #include <sys/utsname.h>
@@ -49,6 +45,9 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #endif
 
 #include <ctype.h>
+#include <limits.h>
+#include <intprops.h>
+#include <strftime.h>
 
 #include "intervals.h"
 #include "buffer.h"
@@ -86,46 +85,31 @@ extern char **environ;
     (1000 - TM_YEAR_BASE <= (tm_year) && (tm_year) <= 9999 - TM_YEAR_BASE)
 #endif
 
-extern size_t emacs_strftimeu (char *, size_t, const char *,
-                               const struct tm *, int);
-
 #ifdef WINDOWSNT
 extern Lisp_Object w32_get_internal_run_time (void);
 #endif
 
+static void time_overflow (void) NO_RETURN;
 static int tm_diff (struct tm *, struct tm *);
-static void find_field (Lisp_Object, Lisp_Object, Lisp_Object, int *, Lisp_Object, int *);
-static void update_buffer_properties (int, int);
+static void find_field (Lisp_Object, Lisp_Object, Lisp_Object,
+                       EMACS_INT *, Lisp_Object, EMACS_INT *);
+static void update_buffer_properties (EMACS_INT, EMACS_INT);
 static Lisp_Object region_limit (int);
-static size_t emacs_memftimeu (char *, size_t, const char *,
-                               size_t, const struct tm *, int);
-static void general_insert_function (void (*) (const unsigned char *, EMACS_INT),
+static size_t emacs_nmemftime (char *, size_t, const char *,
+                              size_t, const struct tm *, int, int);
+static void general_insert_function (void (*) (const char *, EMACS_INT),
                                     void (*) (Lisp_Object, EMACS_INT,
                                               EMACS_INT, EMACS_INT,
                                               EMACS_INT, int),
                                     int, int, Lisp_Object *);
 static Lisp_Object subst_char_in_region_unwind (Lisp_Object);
 static Lisp_Object subst_char_in_region_unwind_1 (Lisp_Object);
-static void transpose_markers (int, int, int, int, int, int, int, int);
+static void transpose_markers (EMACS_INT, EMACS_INT, EMACS_INT, EMACS_INT,
+                              EMACS_INT, EMACS_INT, EMACS_INT, EMACS_INT);
 
-Lisp_Object Vbuffer_access_fontify_functions;
 Lisp_Object Qbuffer_access_fontify_functions;
-Lisp_Object Vbuffer_access_fontified_property;
-
 Lisp_Object Fuser_full_name (Lisp_Object);
 
-/* Non-nil means don't stop at field boundary in text motion commands.  */
-
-Lisp_Object Vinhibit_field_text_motion;
-
-/* Some static data, and a function to initialize it for each run */
-
-Lisp_Object Vsystem_name;
-Lisp_Object Vuser_real_login_name;     /* login name of current user ID */
-Lisp_Object Vuser_full_name;           /* full name of current user */
-Lisp_Object Vuser_login_name;          /* user name from LOGNAME or USER */
-Lisp_Object Voperating_system_release;  /* Operating System Release */
-
 /* Symbol for the text property used to mark fields.  */
 
 Lisp_Object Qfield;
@@ -138,8 +122,8 @@ Lisp_Object Qboundary;
 void
 init_editfns (void)
 {
-  char *user_name;
-  register unsigned char *p;
+  const char *user_name;
+  register char *p;
   struct passwd *pw;   /* password entry for the current user */
   Lisp_Object tem;
 
@@ -152,7 +136,7 @@ init_editfns (void)
     return;
 #endif /* not CANNOT_DUMP */
 
-  pw = (struct passwd *) getpwuid (getuid ());
+  pw = getpwuid (getuid ());
 #ifdef MSDOS
   /* We let the real user name default to "root" because that's quite
      accurate on MSDOG and because it lets Emacs find the init file.
@@ -164,17 +148,17 @@ init_editfns (void)
 
   /* Get the effective user name, by consulting environment variables,
      or the effective uid if those are unset.  */
-  user_name = (char *) getenv ("LOGNAME");
+  user_name = getenv ("LOGNAME");
   if (!user_name)
 #ifdef WINDOWSNT
-    user_name = (char *) getenv ("USERNAME");  /* it's USERNAME on NT */
+    user_name = getenv ("USERNAME");   /* it's USERNAME on NT */
 #else  /* WINDOWSNT */
-    user_name = (char *) getenv ("USER");
+    user_name = getenv ("USER");
 #endif /* WINDOWSNT */
   if (!user_name)
     {
-      pw = (struct passwd *) getpwuid (geteuid ());
-      user_name = (char *) (pw ? pw->pw_name : "unknown");
+      pw = getpwuid (geteuid ());
+      user_name = pw ? pw->pw_name : "unknown";
     }
   Vuser_login_name = build_string (user_name);
 
@@ -184,7 +168,7 @@ init_editfns (void)
   Vuser_full_name = Fuser_full_name (NILP (tem)? make_number (geteuid())
                                     : Vuser_login_name);
 
-  p = (unsigned char *) getenv ("NAME");
+  p = getenv ("NAME");
   if (p)
     Vuser_full_name = build_string (p);
   else if (NILP (Vuser_full_name))
@@ -212,17 +196,19 @@ usage: (char-to-string CHAR)  */)
   CHECK_CHARACTER (character);
 
   len = CHAR_STRING (XFASTINT (character), str);
-  return make_string_from_bytes (str, 1, len);
+  return make_string_from_bytes ((char *) str, 1, len);
 }
 
 DEFUN ("byte-to-string", Fbyte_to_string, Sbyte_to_string, 1, 1, 0,
-       doc: /* Convert arg BYTE to a string containing that byte.  */)
+       doc: /* Convert arg BYTE to a unibyte string containing that byte.  */)
   (Lisp_Object byte)
 {
   unsigned char b;
   CHECK_NUMBER (byte);
+  if (XINT (byte) < 0 || XINT (byte) > 255)
+    error ("Invalid byte");
   b = XINT (byte);
-  return make_string_from_bytes (&b, 1, 1);
+  return make_string_from_bytes ((char *) &b, 1, 1);
 }
 
 DEFUN ("string-to-char", Fstring_to_char, Sstring_to_char, 1, 1, 0,
@@ -245,7 +231,7 @@ A multibyte character is handled correctly.  */)
 }
 \f
 static Lisp_Object
-buildmark (int charpos, int bytepos)
+buildmark (EMACS_INT charpos, EMACS_INT bytepos)
 {
   register Lisp_Object mark;
   mark = Fmake_marker ();
@@ -270,8 +256,8 @@ DEFUN ("point-marker", Fpoint_marker, Spoint_marker, 0, 0, 0,
   return buildmark (PT, PT_BYTE);
 }
 
-int
-clip_to_bounds (int lower, int num, int upper)
+EMACS_INT
+clip_to_bounds (EMACS_INT lower, EMACS_INT num, EMACS_INT upper)
 {
   if (num < lower)
     return lower;
@@ -288,7 +274,7 @@ Beginning of buffer is position (point-min), end is (point-max).
 The return value is POSITION.  */)
   (register Lisp_Object position)
 {
-  int pos;
+  EMACS_INT pos;
 
   if (MARKERP (position)
       && current_buffer == XMARKER (position)->buffer)
@@ -323,10 +309,10 @@ region_limit (int beginningp)
 
   if (!NILP (Vtransient_mark_mode)
       && NILP (Vmark_even_if_inactive)
-      && NILP (current_buffer->mark_active))
+      && NILP (BVAR (current_buffer, mark_active)))
     xsignal0 (Qmark_inactive);
 
-  m = Fmarker_position (current_buffer->mark);
+  m = Fmarker_position (BVAR (current_buffer, mark));
   if (NILP (m))
     error ("The mark is not set now, so there is no region");
 
@@ -355,7 +341,7 @@ Watch out!  Moving this marker changes the mark position.
 If you set the marker not to point anywhere, the buffer will have no mark.  */)
   (void)
 {
-  return current_buffer->mark;
+  return BVAR (current_buffer, mark);
 }
 
 \f
@@ -364,11 +350,11 @@ If you set the marker not to point anywhere, the buffer will have no mark.  */)
    of length LEN.  */
 
 static int
-overlays_around (int pos, Lisp_Object *vec, int len)
+overlays_around (EMACS_INT pos, Lisp_Object *vec, int len)
 {
   Lisp_Object overlay, start, end;
   struct Lisp_Overlay *tail;
-  int startpos, endpos;
+  EMACS_INT startpos, endpos;
   int idx = 0;
 
   for (tail = current_buffer->overlays_before; tail; tail = tail->next)
@@ -436,7 +422,7 @@ get_pos_property (Lisp_Object position, register Lisp_Object prop, Lisp_Object o
     return Fget_text_property (position, prop, object);
   else
     {
-      int posn = XINT (position);
+      EMACS_INT posn = XINT (position);
       int noverlays;
       Lisp_Object *overlay_vec, tem;
       struct buffer *obuf = current_buffer;
@@ -515,7 +501,9 @@ get_pos_property (Lisp_Object position, register Lisp_Object prop, Lisp_Object o
    is not stored.  */
 
 static void
-find_field (Lisp_Object pos, Lisp_Object merge_at_boundary, Lisp_Object beg_limit, int *beg, Lisp_Object end_limit, int *end)
+find_field (Lisp_Object pos, Lisp_Object merge_at_boundary,
+           Lisp_Object beg_limit,
+           EMACS_INT *beg, Lisp_Object end_limit, EMACS_INT *end)
 {
   /* Fields right before and after the point.  */
   Lisp_Object before_field, after_field;
@@ -631,7 +619,7 @@ A field is a region of text with the same `field' property.
 If POS is nil, the value of point is used for POS.  */)
   (Lisp_Object pos)
 {
-  int beg, end;
+  EMACS_INT beg, end;
   find_field (pos, Qnil, Qnil, &beg, Qnil, &end);
   if (beg != end)
     del_range (beg, end);
@@ -644,7 +632,7 @@ A field is a region of text with the same `field' property.
 If POS is nil, the value of point is used for POS.  */)
   (Lisp_Object pos)
 {
-  int beg, end;
+  EMACS_INT beg, end;
   find_field (pos, Qnil, Qnil, &beg, Qnil, &end);
   return make_buffer_string (beg, end, 1);
 }
@@ -655,7 +643,7 @@ A field is a region of text with the same `field' property.
 If POS is nil, the value of point is used for POS.  */)
   (Lisp_Object pos)
 {
-  int beg, end;
+  EMACS_INT beg, end;
   find_field (pos, Qnil, Qnil, &beg, Qnil, &end);
   return make_buffer_string (beg, end, 0);
 }
@@ -670,7 +658,7 @@ If LIMIT is non-nil, it is a buffer position; if the beginning of the field
 is before LIMIT, then LIMIT will be returned instead.  */)
   (Lisp_Object pos, Lisp_Object escape_from_edge, Lisp_Object limit)
 {
-  int beg;
+  EMACS_INT beg;
   find_field (pos, escape_from_edge, limit, &beg, Qnil, 0);
   return make_number (beg);
 }
@@ -685,7 +673,7 @@ If LIMIT is non-nil, it is a buffer position; if the end of the field
 is after LIMIT, then LIMIT will be returned instead.  */)
   (Lisp_Object pos, Lisp_Object escape_from_edge, Lisp_Object limit)
 {
-  int end;
+  EMACS_INT end;
   find_field (pos, escape_from_edge, Qnil, 0, limit, &end);
   return make_number (end);
 }
@@ -720,7 +708,7 @@ Field boundaries are not noticed if `inhibit-field-text-motion' is non-nil.  */)
   (Lisp_Object new_pos, Lisp_Object old_pos, Lisp_Object escape_from_edge, Lisp_Object only_in_line, Lisp_Object inhibit_capture_property)
 {
   /* If non-zero, then the original point, before re-positioning.  */
-  int orig_point = 0;
+  EMACS_INT orig_point = 0;
   int fwd;
   Lisp_Object prev_old, prev_new;
 
@@ -817,7 +805,7 @@ boundaries bind `inhibit-field-text-motion' to t.
 This function does not move point.  */)
   (Lisp_Object n)
 {
-  int orig, orig_byte, end;
+  EMACS_INT orig, orig_byte, end;
   int count = SPECPDL_INDEX ();
   specbind (Qinhibit_point_motion_hooks, Qt);
 
@@ -858,8 +846,8 @@ boundaries bind `inhibit-field-text-motion' to t.
 This function does not move point.  */)
   (Lisp_Object n)
 {
-  int end_pos;
-  int orig = PT;
+  EMACS_INT end_pos;
+  EMACS_INT orig = PT;
 
   if (NILP (n))
     XSETFASTINT (n, 1);
@@ -881,9 +869,9 @@ save_excursion_save (void)
                 == current_buffer);
 
   return Fcons (Fpoint_marker (),
-               Fcons (Fcopy_marker (current_buffer->mark, Qnil),
+               Fcons (Fcopy_marker (BVAR (current_buffer, mark), Qnil),
                       Fcons (visible ? Qt : Qnil,
-                             Fcons (current_buffer->mark_active,
+                             Fcons (BVAR (current_buffer, mark_active),
                                     selected_window))));
 }
 
@@ -915,8 +903,8 @@ save_excursion_restore (Lisp_Object info)
   /* Mark marker.  */
   info = XCDR (info);
   tem = XCAR (info);
-  omark = Fmarker_position (current_buffer->mark);
-  Fset_marker (current_buffer->mark, tem, Fcurrent_buffer ());
+  omark = Fmarker_position (BVAR (current_buffer, mark));
+  Fset_marker (BVAR (current_buffer, mark), tem, Fcurrent_buffer ());
   nmark = Fmarker_position (tem);
   unchain_marker (XMARKER (tem));
 
@@ -937,21 +925,24 @@ save_excursion_restore (Lisp_Object info)
   /* Mark active */
   info = XCDR (info);
   tem = XCAR (info);
-  tem1 = current_buffer->mark_active;
-  current_buffer->mark_active = tem;
+  tem1 = BVAR (current_buffer, mark_active);
+  BVAR (current_buffer, mark_active) = tem;
 
-  if (!NILP (Vrun_hooks))
+  /* If mark is active now, and either was not active
+     or was at a different place, run the activate hook.  */
+  if (! NILP (tem))
     {
-      /* If mark is active now, and either was not active
-        or was at a different place, run the activate hook.  */
-      if (! NILP (current_buffer->mark_active))
-       {
-         if (! EQ (omark, nmark))
-           call1 (Vrun_hooks, intern ("activate-mark-hook"));
-       }
-      /* If mark has ceased to be active, run deactivate hook.  */
-      else if (! NILP (tem1))
-       call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
+      if (! EQ (omark, nmark))
+        {
+          tem = intern ("activate-mark-hook");
+          Frun_hooks (1, &tem);
+        }
+    }
+  /* If mark has ceased to be active, run deactivate hook.  */
+  else if (! NILP (tem1))
+    {
+      tem = intern ("deactivate-mark-hook");
+      Frun_hooks (1, &tem);
     }
 
   /* If buffer was visible in a window, and a different window was
@@ -1129,9 +1120,9 @@ At the beginning of the buffer or accessible region, return 0.  */)
   Lisp_Object temp;
   if (PT <= BEGV)
     XSETFASTINT (temp, 0);
-  else if (!NILP (current_buffer->enable_multibyte_characters))
+  else if (!NILP (BVAR (current_buffer, enable_multibyte_characters)))
     {
-      int pos = PT_BYTE;
+      EMACS_INT pos = PT_BYTE;
       DEC_POS (pos);
       XSETFASTINT (temp, FETCH_CHAR (pos));
     }
@@ -1185,7 +1176,7 @@ POS is an integer or a marker and defaults to point.
 If POS is out of range, the value is nil.  */)
   (Lisp_Object pos)
 {
-  register int pos_byte;
+  register EMACS_INT pos_byte;
 
   if (NILP (pos))
     {
@@ -1218,7 +1209,7 @@ If POS is out of range, the value is nil.  */)
   (Lisp_Object pos)
 {
   register Lisp_Object val;
-  register int pos_byte;
+  register EMACS_INT pos_byte;
 
   if (NILP (pos))
     {
@@ -1243,7 +1234,7 @@ If POS is out of range, the value is nil.  */)
       pos_byte = CHAR_TO_BYTE (XINT (pos));
     }
 
-  if (!NILP (current_buffer->enable_multibyte_characters))
+  if (!NILP (BVAR (current_buffer, enable_multibyte_characters)))
     {
       DEC_POS (pos_byte);
       XSETFASTINT (val, FETCH_CHAR (pos_byte));
@@ -1278,9 +1269,9 @@ of the user with that uid, or nil if there is no such user.  */)
   if (NILP (uid))
     return Vuser_login_name;
 
-  id = (uid_t)XFLOATINT (uid);
+  id = XFLOATINT (uid);
   BLOCK_INPUT;
-  pw = (struct passwd *) getpwuid (id);
+  pw = getpwuid (id);
   UNBLOCK_INPUT;
   return (pw ? build_string (pw->pw_name) : Qnil);
 }
@@ -1312,7 +1303,7 @@ Value is an integer or a float, depending on the value.  */)
   /* Make sure we don't produce a negative UID due to signed integer
      overflow.  */
   if (euid < 0)
-    return make_float ((double)geteuid ());
+    return make_float (geteuid ());
   return make_fixnum_or_float (euid);
 }
 
@@ -1328,7 +1319,7 @@ Value is an integer or a float, depending on the value.  */)
   /* Make sure we don't produce a negative UID due to signed integer
      overflow.  */
   if (uid < 0)
-    return make_float ((double)getuid ());
+    return make_float (getuid ());
   return make_fixnum_or_float (uid);
 }
 
@@ -1344,21 +1335,22 @@ name, or nil if there is no such user.  */)
   (Lisp_Object uid)
 {
   struct passwd *pw;
-  register unsigned char *p, *q;
+  register char *p, *q;
   Lisp_Object full;
 
   if (NILP (uid))
     return Vuser_full_name;
   else if (NUMBERP (uid))
     {
+      uid_t u = XFLOATINT (uid);
       BLOCK_INPUT;
-      pw = (struct passwd *) getpwuid ((uid_t) XFLOATINT (uid));
+      pw = getpwuid (u);
       UNBLOCK_INPUT;
     }
   else if (STRINGP (uid))
     {
       BLOCK_INPUT;
-      pw = (struct passwd *) getpwnam (SDATA (uid));
+      pw = getpwnam (SSDATA (uid));
       UNBLOCK_INPUT;
     }
   else
@@ -1367,26 +1359,26 @@ name, or nil if there is no such user.  */)
   if (!pw)
     return Qnil;
 
-  p = (unsigned char *) USER_FULL_NAME;
+  p = USER_FULL_NAME;
   /* Chop off everything after the first comma. */
-  q = (unsigned char *) strchr (p, ',');
+  q = strchr (p, ',');
   full = make_string (p, q ? q - p : strlen (p));
 
 #ifdef AMPERSAND_FULL_NAME
-  p = SDATA (full);
-  q = (unsigned char *) strchr (p, '&');
+  p = SSDATA (full);
+  q = strchr (p, '&');
   /* Substitute the login name for the &, upcasing the first character.  */
   if (q)
     {
-      register unsigned char *r;
+      register char *r;
       Lisp_Object login;
 
       login = Fuser_login_name (make_number (pw->pw_uid));
-      r = (unsigned char *) alloca (strlen (p) + SCHARS (login) + 1);
+      r = (char *) alloca (strlen (p) + SCHARS (login) + 1);
       memcpy (r, p, q - p);
       r[q - p] = 0;
-      strcat (r, SDATA (login));
-      r[q - p] = UPCASE (r[q - p]);
+      strcat (r, SSDATA (login));
+      r[q - p] = upcase ((unsigned char) r[q - p]);
       strcat (r, q + 1);
       full = build_string (r);
     }
@@ -1402,13 +1394,11 @@ DEFUN ("system-name", Fsystem_name, Ssystem_name, 0, 0, 0,
   return Vsystem_name;
 }
 
-/* For the benefit of callers who don't want to include lisp.h */
-
 const char *
 get_system_name (void)
 {
   if (STRINGP (Vsystem_name))
-    return (const char *) SDATA (Vsystem_name);
+    return SSDATA (Vsystem_name);
   else
     return "";
 }
@@ -1417,7 +1407,7 @@ const char *
 get_operating_system_release (void)
 {
   if (STRINGP (Voperating_system_release))
-    return (char *) SDATA (Voperating_system_release);
+    return SSDATA (Voperating_system_release);
   else
     return "";
 }
@@ -1429,6 +1419,49 @@ DEFUN ("emacs-pid", Femacs_pid, Semacs_pid, 0, 0, 0,
   return make_number (getpid ());
 }
 
+\f
+
+#ifndef TIME_T_MIN
+# define TIME_T_MIN TYPE_MINIMUM (time_t)
+#endif
+#ifndef TIME_T_MAX
+# define TIME_T_MAX TYPE_MAXIMUM (time_t)
+#endif
+
+/* Report that a time value is out of range for Emacs.  */
+static void
+time_overflow (void)
+{
+  error ("Specified time is not representable");
+}
+
+/* Return the upper part of the time T (everything but the bottom 16 bits),
+   making sure that it is representable.  */
+static EMACS_INT
+hi_time (time_t t)
+{
+  time_t hi = t >> 16;
+
+  /* Check for overflow, helping the compiler for common cases where
+     no runtime check is needed, and taking care not to convert
+     negative numbers to unsigned before comparing them.  */
+  if (! ((! TYPE_SIGNED (time_t)
+         || MOST_NEGATIVE_FIXNUM <= TIME_T_MIN >> 16
+         || MOST_NEGATIVE_FIXNUM <= hi)
+        && (TIME_T_MAX >> 16 <= MOST_POSITIVE_FIXNUM
+            || hi <= MOST_POSITIVE_FIXNUM)))
+    time_overflow ();
+
+  return hi;
+}
+
+/* Return the bottom 16 bits of the time T.  */
+static EMACS_INT
+lo_time (time_t t)
+{
+  return t & ((1 << 16) - 1);
+}
+
 DEFUN ("current-time", Fcurrent_time, Scurrent_time, 0, 0, 0,
        doc: /* Return the current time, as the number of seconds since 1970-01-01 00:00:00.
 The time is returned as a list of three integers.  The first has the
@@ -1443,8 +1476,8 @@ resolution finer than a second.  */)
   EMACS_TIME t;
 
   EMACS_GET_TIME (t);
-  return list3 (make_number ((EMACS_SECS (t) >> 16) & 0xffff),
-               make_number ((EMACS_SECS (t) >> 0)  & 0xffff),
+  return list3 (make_number (hi_time (EMACS_SECS (t))),
+               make_number (lo_time (EMACS_SECS (t))),
                make_number (EMACS_USECS (t)));
 }
 
@@ -1463,7 +1496,8 @@ on systems that do not provide resolution finer than a second.  */)
 {
 #ifdef HAVE_GETRUSAGE
   struct rusage usage;
-  int secs, usecs;
+  time_t secs;
+  int usecs;
 
   if (getrusage (RUSAGE_SELF, &usage) < 0)
     /* This shouldn't happen.  What action is appropriate?  */
@@ -1478,8 +1512,8 @@ on systems that do not provide resolution finer than a second.  */)
       secs++;
     }
 
-  return list3 (make_number ((secs >> 16) & 0xffff),
-               make_number ((secs >> 0)  & 0xffff),
+  return list3 (make_number (hi_time (secs)),
+               make_number (lo_time (secs)),
                make_number (usecs));
 #else /* ! HAVE_GETRUSAGE  */
 #ifdef WINDOWSNT
@@ -1491,6 +1525,19 @@ on systems that do not provide resolution finer than a second.  */)
 }
 \f
 
+/* Make a Lisp list that represents the time T.  */
+Lisp_Object
+make_time (time_t t)
+{
+  return list2 (make_number (hi_time (t)),
+               make_number (lo_time (t)));
+}
+
+/* Decode a Lisp list SPECIFIED_TIME that represents a time.
+   If SPECIFIED_TIME is nil, use the current time.
+   Set *RESULT to seconds since the Epoch.
+   If USEC is not null, set *USEC to the microseconds component.
+   Return nonzero if successful.  */
 int
 lisp_time_argument (Lisp_Object specified_time, time_t *result, int *usec)
 {
@@ -1511,6 +1558,7 @@ lisp_time_argument (Lisp_Object specified_time, time_t *result, int *usec)
   else
     {
       Lisp_Object high, low;
+      EMACS_INT hi;
       high = Fcar (specified_time);
       CHECK_NUMBER (high);
       low = Fcdr (specified_time);
@@ -1534,8 +1582,21 @@ lisp_time_argument (Lisp_Object specified_time, time_t *result, int *usec)
       else if (usec)
         *usec = 0;
       CHECK_NUMBER (low);
-      *result = (XINT (high) << 16) + (XINT (low) & 0xffff);
-      return *result >> 16 == XINT (high);
+      hi = XINT (high);
+
+      /* Check for overflow, helping the compiler for common cases
+        where no runtime check is needed, and taking care not to
+        convert negative numbers to unsigned before comparing them.  */
+      if (! ((TYPE_SIGNED (time_t)
+             ? (TIME_T_MIN >> 16 <= MOST_NEGATIVE_FIXNUM
+                || TIME_T_MIN >> 16 <= hi)
+             : 0 <= hi)
+            && (MOST_POSITIVE_FIXNUM <= TIME_T_MAX >> 16
+                || hi <= TIME_T_MAX >> 16)))
+       return 0;
+
+      *result = (hi << 16) + (XINT (low) & 0xffff);
+      return 1;
     }
 }
 
@@ -1564,22 +1625,24 @@ or (if you need time as a string) `format-time-string'.  */)
 /* Write information into buffer S of size MAXSIZE, according to the
    FORMAT of length FORMAT_LEN, using time information taken from *TP.
    Default to Universal Time if UT is nonzero, local time otherwise.
+   Use NS as the number of nanoseconds in the %N directive.
    Return the number of bytes written, not including the terminating
    '\0'.  If S is NULL, nothing will be written anywhere; so to
    determine how many bytes would be written, use NULL for S and
    ((size_t) -1) for MAXSIZE.
 
-   This function behaves like emacs_strftimeu, except it allows null
-   bytes in FORMAT.  */
+   This function behaves like nstrftime, except it allows null
+   bytes in FORMAT and it does not support nanoseconds.  */
 static size_t
-emacs_memftimeu (char *s, size_t maxsize, const char *format, size_t format_len, const struct tm *tp, int ut)
+emacs_nmemftime (char *s, size_t maxsize, const char *format,
+                size_t format_len, const struct tm *tp, int ut, int ns)
 {
   size_t total = 0;
 
   /* Loop through all the null-terminated strings in the format
      argument.  Normally there's just one null-terminated string, but
      there can be arbitrarily many, concatenated together, if the
-     format contains '\0' bytes.  emacs_strftimeu stops at the first
+     format contains '\0' bytes.  nstrftime stops at the first
      '\0' byte so we must invoke it separately for each such string.  */
   for (;;)
     {
@@ -1589,7 +1652,7 @@ emacs_memftimeu (char *s, size_t maxsize, const char *format, size_t format_len,
       if (s)
        s[0] = '\1';
 
-      result = emacs_strftimeu (s, maxsize, format, tp, ut);
+      result = nstrftime (s, maxsize, format, tp, ut, ns);
 
       if (s)
        {
@@ -1635,6 +1698,7 @@ by text that describes the specified date and time in TIME:
 %p is the locale's equivalent of either AM or PM.
 %M is the minute.
 %S is the second.
+%N is the nanosecond, %6N the microsecond, %3N the millisecond, etc.
 %Z is the time zone name, %z is the numeric form.
 %s is the number of seconds since 1970-01-01 00:00:00 +0000.
 
@@ -1660,17 +1724,21 @@ The modifiers are `E' and `O'.  For certain characters X,
 %OX is like %X, but uses the locale's number symbols.
 
 For example, to produce full ISO 8601 format, use "%Y-%m-%dT%T%z".  */)
-  (Lisp_Object format_string, Lisp_Object time, Lisp_Object universal)
+  (Lisp_Object format_string, Lisp_Object timeval, Lisp_Object universal)
 {
   time_t value;
   int size;
+  int usec;
+  int ns;
   struct tm *tm;
   int ut = ! NILP (universal);
 
   CHECK_STRING (format_string);
 
-  if (! lisp_time_argument (time, &value, NULL))
+  if (! (lisp_time_argument (timeval, &value, &usec)
+        && 0 <= usec && usec < 1000000))
     error ("Invalid time specification");
+  ns = usec * 1000;
 
   format_string = code_convert_string_norecord (format_string,
                                                Vlocale_coding_system, 1);
@@ -1682,7 +1750,7 @@ For example, to produce full ISO 8601 format, use "%Y-%m-%dT%T%z".  */)
   tm = ut ? gmtime (&value) : localtime (&value);
   UNBLOCK_INPUT;
   if (! tm)
-    error ("Specified time is not representable");
+    time_overflow ();
 
   synchronize_system_time_locale ();
 
@@ -1693,9 +1761,9 @@ For example, to produce full ISO 8601 format, use "%Y-%m-%dT%T%z".  */)
 
       buf[0] = '\1';
       BLOCK_INPUT;
-      result = emacs_memftimeu (buf, size, SDATA (format_string),
+      result = emacs_nmemftime (buf, size, SSDATA (format_string),
                                SBYTES (format_string),
-                               tm, ut);
+                               tm, ut, ns);
       UNBLOCK_INPUT;
       if ((result > 0 && result < size) || (result == 0 && buf[0] == '\0'))
        return code_convert_string_norecord (make_unibyte_string (buf, result),
@@ -1703,10 +1771,10 @@ For example, to produce full ISO 8601 format, use "%Y-%m-%dT%T%z".  */)
 
       /* If buffer was too small, make it bigger and try again.  */
       BLOCK_INPUT;
-      result = emacs_memftimeu (NULL, (size_t) -1,
-                               SDATA (format_string),
+      result = emacs_nmemftime (NULL, (size_t) -1,
+                               SSDATA (format_string),
                                SBYTES (format_string),
-                               tm, ut);
+                               tm, ut, ns);
       UNBLOCK_INPUT;
       size = result + 1;
     }
@@ -1740,8 +1808,10 @@ DOW and ZONE.)  */)
   BLOCK_INPUT;
   decoded_time = localtime (&time_spec);
   UNBLOCK_INPUT;
-  if (! decoded_time)
-    error ("Specified time is not representable");
+  if (! (decoded_time
+        && MOST_NEGATIVE_FIXNUM - TM_YEAR_BASE <= decoded_time->tm_year
+        && decoded_time->tm_year <= MOST_POSITIVE_FIXNUM - TM_YEAR_BASE))
+    time_overflow ();
   XSETFASTINT (list_args[0], decoded_time->tm_sec);
   XSETFASTINT (list_args[1], decoded_time->tm_min);
   XSETFASTINT (list_args[2], decoded_time->tm_hour);
@@ -1765,6 +1835,20 @@ DOW and ZONE.)  */)
   return Flist (9, list_args);
 }
 
+/* Return OBJ - OFFSET, checking that OBJ is a valid fixnum and that
+   the result is representable as an int.  Assume OFFSET is small and
+   nonnegative.  */
+static int
+check_tm_member (Lisp_Object obj, int offset)
+{
+  EMACS_INT n;
+  CHECK_NUMBER (obj);
+  n = XINT (obj);
+  if (! (INT_MIN + offset <= n && n - offset <= INT_MAX))
+    time_overflow ();
+  return n - offset;
+}
+
 DEFUN ("encode-time", Fencode_time, Sencode_time, 6, MANY, 0,
        doc: /* Convert SECOND, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time.
 This is the reverse operation of `decode-time', which see.
@@ -1789,23 +1873,16 @@ year values as low as 1901 do work.
 usage: (encode-time SECOND MINUTE HOUR DAY MONTH YEAR &optional ZONE)  */)
   (int nargs, register Lisp_Object *args)
 {
-  time_t time;
+  time_t value;
   struct tm tm;
   Lisp_Object zone = (nargs > 6 ? args[nargs - 1] : Qnil);
 
-  CHECK_NUMBER (args[0]);      /* second */
-  CHECK_NUMBER (args[1]);      /* minute */
-  CHECK_NUMBER (args[2]);      /* hour */
-  CHECK_NUMBER (args[3]);      /* day */
-  CHECK_NUMBER (args[4]);      /* month */
-  CHECK_NUMBER (args[5]);      /* year */
-
-  tm.tm_sec = XINT (args[0]);
-  tm.tm_min = XINT (args[1]);
-  tm.tm_hour = XINT (args[2]);
-  tm.tm_mday = XINT (args[3]);
-  tm.tm_mon = XINT (args[4]) - 1;
-  tm.tm_year = XINT (args[5]) - TM_YEAR_BASE;
+  tm.tm_sec  = check_tm_member (args[0], 0);
+  tm.tm_min  = check_tm_member (args[1], 0);
+  tm.tm_hour = check_tm_member (args[2], 0);
+  tm.tm_mday = check_tm_member (args[3], 0);
+  tm.tm_mon  = check_tm_member (args[4], 1);
+  tm.tm_year = check_tm_member (args[5], TM_YEAR_BASE);
   tm.tm_isdst = -1;
 
   if (CONSP (zone))
@@ -1813,7 +1890,7 @@ usage: (encode-time SECOND MINUTE HOUR DAY MONTH YEAR &optional ZONE)  */)
   if (NILP (zone))
     {
       BLOCK_INPUT;
-      time = mktime (&tm);
+      value = mktime (&tm);
       UNBLOCK_INPUT;
     }
   else
@@ -1825,7 +1902,7 @@ usage: (encode-time SECOND MINUTE HOUR DAY MONTH YEAR &optional ZONE)  */)
       if (EQ (zone, Qt))
        tzstring = "UTC0";
       else if (STRINGP (zone))
-       tzstring = (char *) SDATA (zone);
+       tzstring = SSDATA (zone);
       else if (INTEGERP (zone))
        {
          int abszone = eabs (XINT (zone));
@@ -1841,7 +1918,7 @@ usage: (encode-time SECOND MINUTE HOUR DAY MONTH YEAR &optional ZONE)  */)
       set_time_zone_rule (tzstring);
 
       BLOCK_INPUT;
-      time = mktime (&tm);
+      value = mktime (&tm);
       UNBLOCK_INPUT;
 
       /* Restore TZ to previous value.  */
@@ -1853,10 +1930,10 @@ usage: (encode-time SECOND MINUTE HOUR DAY MONTH YEAR &optional ZONE)  */)
 #endif
     }
 
-  if (time == (time_t) -1)
-    error ("Specified time is not representable");
+  if (value == (time_t) -1)
+    time_overflow ();
 
-  return make_time (time);
+  return make_time (value);
 }
 
 DEFUN ("current-time-string", Fcurrent_time_string, Scurrent_time_string, 0, 1, 0,
@@ -1889,7 +1966,7 @@ but this is considered obsolete.  */)
   tm = localtime (&value);
   UNBLOCK_INPUT;
   if (! (tm && TM_YEAR_IN_ASCTIME_RANGE (tm->tm_year) && (tem = asctime (tm))))
-    error ("Specified time is not representable");
+    time_overflow ();
 
   /* Remove the trailing newline.  */
   tem[strlen (tem) - 1] = '\0';
@@ -2014,7 +2091,7 @@ If TZ is t, use Universal Time.  */)
   else
     {
       CHECK_STRING (tz);
-      tzstring = (char *) SDATA (tz);
+      tzstring = SSDATA (tz);
     }
 
   set_time_zone_rule (tzstring);
@@ -2126,7 +2203,7 @@ set_time_zone_rule (const char *tzstring)
 
 static void
 general_insert_function (void (*insert_func)
-                             (const unsigned char *, EMACS_INT),
+                             (const char *, EMACS_INT),
                         void (*insert_from_string_func)
                              (Lisp_Object, EMACS_INT, EMACS_INT,
                               EMACS_INT, EMACS_INT, int),
@@ -2143,16 +2220,16 @@ general_insert_function (void (*insert_func)
          unsigned char str[MAX_MULTIBYTE_LENGTH];
          int len;
 
-         if (!NILP (current_buffer->enable_multibyte_characters))
+         if (!NILP (BVAR (current_buffer, enable_multibyte_characters)))
            len = CHAR_STRING (XFASTINT (val), str);
          else
            {
              str[0] = (ASCII_CHAR_P (XINT (val))
                        ? XINT (val)
-                       : multibyte_char_to_unibyte (XINT (val), Qnil));
+                       : multibyte_char_to_unibyte (XINT (val)));
              len = 1;
            }
-         (*insert_func) (str, len);
+         (*insert_func) ((char *) str, len);
        }
       else if (STRINGP (val))
        {
@@ -2265,34 +2342,37 @@ The optional third arg INHERIT, if non-nil, says to inherit text properties
 from adjoining text, if those properties are sticky.  */)
   (Lisp_Object character, Lisp_Object count, Lisp_Object inherit)
 {
-  register unsigned char *string;
-  register int strlen;
-  register int i, n;
+  register char *string;
+  register EMACS_INT stringlen;
+  register int i;
+  register EMACS_INT n;
   int len;
   unsigned char str[MAX_MULTIBYTE_LENGTH];
 
   CHECK_NUMBER (character);
   CHECK_NUMBER (count);
 
-  if (!NILP (current_buffer->enable_multibyte_characters))
+  if (!NILP (BVAR (current_buffer, enable_multibyte_characters)))
     len = CHAR_STRING (XFASTINT (character), str);
   else
     str[0] = XFASTINT (character), len = 1;
+  if (MOST_POSITIVE_FIXNUM / len < XINT (count))
+    error ("Maximum buffer size would be exceeded");
   n = XINT (count) * len;
   if (n <= 0)
     return Qnil;
-  strlen = min (n, 256 * len);
-  string = (unsigned char *) alloca (strlen);
-  for (i = 0; i < strlen; i++)
+  stringlen = min (n, 256 * len);
+  string = (char *) alloca (stringlen);
+  for (i = 0; i < stringlen; i++)
     string[i] = str[i % len];
-  while (n >= strlen)
+  while (n >= stringlen)
     {
       QUIT;
       if (!NILP (inherit))
-       insert_and_inherit (string, strlen);
+       insert_and_inherit (string, stringlen);
       else
-       insert (string, strlen);
-      n -= strlen;
+       insert (string, stringlen);
+      n -= stringlen;
     }
   if (n > 0)
     {
@@ -2321,7 +2401,7 @@ from adjoining text, if those properties are sticky.  */)
   if (XINT (byte) < 0 || XINT (byte) > 255)
     args_out_of_range_3 (byte, make_number (0), make_number (255));
   if (XINT (byte) >= 128
-      && ! NILP (current_buffer->enable_multibyte_characters))
+      && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
     XSETFASTINT (byte, BYTE8_TO_CHAR (XINT (byte)));
   return Finsert_char (byte, count, inherit);
 }
@@ -2343,10 +2423,10 @@ from adjoining text, if those properties are sticky.  */)
    buffer substrings.  */
 
 Lisp_Object
-make_buffer_string (int start, int end, int props)
+make_buffer_string (EMACS_INT start, EMACS_INT end, int props)
 {
-  int start_byte = CHAR_TO_BYTE (start);
-  int end_byte = CHAR_TO_BYTE (end);
+  EMACS_INT start_byte = CHAR_TO_BYTE (start);
+  EMACS_INT end_byte = CHAR_TO_BYTE (end);
 
   return make_buffer_string_both (start, start_byte, end, end_byte, props);
 }
@@ -2367,14 +2447,15 @@ make_buffer_string (int start, int end, int props)
    buffer substrings.  */
 
 Lisp_Object
-make_buffer_string_both (int start, int start_byte, int end, int end_byte, int props)
+make_buffer_string_both (EMACS_INT start, EMACS_INT start_byte,
+                        EMACS_INT end, EMACS_INT end_byte, int props)
 {
   Lisp_Object result, tem, tem1;
 
   if (start < GPT && GPT < end)
     move_gap (start);
 
-  if (! NILP (current_buffer->enable_multibyte_characters))
+  if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
     result = make_uninit_multibyte_string (end - start, end_byte - start_byte);
   else
     result = make_uninit_string (end - start);
@@ -2400,7 +2481,7 @@ make_buffer_string_both (int start, int start_byte, int end, int end_byte, int p
    in the current buffer, if necessary.  */
 
 static void
-update_buffer_properties (int start, int end)
+update_buffer_properties (EMACS_INT start, EMACS_INT end)
 {
   /* If this buffer has some access functions,
      call them, specifying the range of the buffer being accessed.  */
@@ -2439,7 +2520,7 @@ into the result string; if you don't want the text properties,
 use `buffer-substring-no-properties' instead.  */)
   (Lisp_Object start, Lisp_Object end)
 {
-  register int b, e;
+  register EMACS_INT b, e;
 
   validate_region (&start, &end);
   b = XINT (start);
@@ -2455,7 +2536,7 @@ The two arguments START and END are character positions;
 they can be in either order.  */)
   (Lisp_Object start, Lisp_Object end)
 {
-  register int b, e;
+  register EMACS_INT b, e;
 
   validate_region (&start, &end);
   b = XINT (start);
@@ -2481,7 +2562,7 @@ Arguments START and END are character positions specifying the substring.
 They default to the values of (point-min) and (point-max) in BUFFER.  */)
   (Lisp_Object buffer, Lisp_Object start, Lisp_Object end)
 {
-  register int b, e, temp;
+  register EMACS_INT b, e, temp;
   register struct buffer *bp, *obuf;
   Lisp_Object buf;
 
@@ -2489,7 +2570,7 @@ They default to the values of (point-min) and (point-max) in BUFFER.  */)
   if (NILP (buf))
     nsberror (buffer);
   bp = XBUFFER (buf);
-  if (NILP (bp->name))
+  if (NILP (BVAR (bp, name)))
     error ("Selecting deleted buffer");
 
   if (NILP (start))
@@ -2534,13 +2615,13 @@ The value of `case-fold-search' in the current buffer
 determines whether case is significant or ignored.  */)
   (Lisp_Object buffer1, Lisp_Object start1, Lisp_Object end1, Lisp_Object buffer2, Lisp_Object start2, Lisp_Object end2)
 {
-  register int begp1, endp1, begp2, endp2, temp;
+  register EMACS_INT begp1, endp1, begp2, endp2, temp;
   register struct buffer *bp1, *bp2;
   register Lisp_Object trt
-    = (!NILP (current_buffer->case_fold_search)
-       ? current_buffer->case_canon_table : Qnil);
-  int chars = 0;
-  int i1, i2, i1_byte, i2_byte;
+    = (!NILP (BVAR (current_buffer, case_fold_search))
+       ? BVAR (current_buffer, case_canon_table) : Qnil);
+  EMACS_INT chars = 0;
+  EMACS_INT i1, i2, i1_byte, i2_byte;
 
   /* Find the first buffer and its substring.  */
 
@@ -2553,7 +2634,7 @@ determines whether case is significant or ignored.  */)
       if (NILP (buf1))
        nsberror (buffer1);
       bp1 = XBUFFER (buf1);
-      if (NILP (bp1->name))
+      if (NILP (BVAR (bp1, name)))
        error ("Selecting deleted buffer");
     }
 
@@ -2591,7 +2672,7 @@ determines whether case is significant or ignored.  */)
       if (NILP (buf2))
        nsberror (buffer2);
       bp2 = XBUFFER (buf2);
-      if (NILP (bp2->name))
+      if (NILP (BVAR (bp2, name)))
        error ("Selecting deleted buffer");
     }
 
@@ -2631,7 +2712,7 @@ determines whether case is significant or ignored.  */)
 
       QUIT;
 
-      if (! NILP (bp1->enable_multibyte_characters))
+      if (! NILP (BVAR (bp1, enable_multibyte_characters)))
        {
          c1 = BUF_FETCH_MULTIBYTE_CHAR (bp1, i1_byte);
          BUF_INC_POS (bp1, i1_byte);
@@ -2644,7 +2725,7 @@ determines whether case is significant or ignored.  */)
          i1++;
        }
 
-      if (! NILP (bp2->enable_multibyte_characters))
+      if (! NILP (BVAR (bp2, enable_multibyte_characters)))
        {
          c2 = BUF_FETCH_MULTIBYTE_CHAR (bp2, i2_byte);
          BUF_INC_POS (bp2, i2_byte);
@@ -2684,13 +2765,13 @@ determines whether case is significant or ignored.  */)
 static Lisp_Object
 subst_char_in_region_unwind (Lisp_Object arg)
 {
-  return current_buffer->undo_list = arg;
+  return BVAR (current_buffer, undo_list) = arg;
 }
 
 static Lisp_Object
 subst_char_in_region_unwind_1 (Lisp_Object arg)
 {
-  return current_buffer->filename = arg;
+  return BVAR (current_buffer, filename) = arg;
 }
 
 DEFUN ("subst-char-in-region", Fsubst_char_in_region,
@@ -2701,12 +2782,12 @@ and don't mark the buffer as really changed.
 Both characters must have the same length of multi-byte form.  */)
   (Lisp_Object start, Lisp_Object end, Lisp_Object fromchar, Lisp_Object tochar, Lisp_Object noundo)
 {
-  register int pos, pos_byte, stop, i, len, end_byte;
+  register EMACS_INT pos, pos_byte, stop, i, len, end_byte;
   /* Keep track of the first change in the buffer:
      if 0 we haven't found it yet.
      if < 0 we've found it and we've run the before-change-function.
      if > 0 we've actually performed it and the value is its position.  */
-  int changed = 0;
+  EMACS_INT changed = 0;
   unsigned char fromstr[MAX_MULTIBYTE_LENGTH], tostr[MAX_MULTIBYTE_LENGTH];
   unsigned char *p;
   int count = SPECPDL_INDEX ();
@@ -2715,8 +2796,8 @@ Both characters must have the same length of multi-byte form.  */)
 #define COMBINING_AFTER  2
 #define COMBINING_BOTH (COMBINING_BEFORE | COMBINING_AFTER)
   int maybe_byte_combining = COMBINING_NO;
-  int last_changed = 0;
-  int multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
+  EMACS_INT last_changed = 0;
+  int multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
 
  restart:
 
@@ -2760,19 +2841,19 @@ Both characters must have the same length of multi-byte form.  */)
   if (!changed && !NILP (noundo))
     {
       record_unwind_protect (subst_char_in_region_unwind,
-                            current_buffer->undo_list);
-      current_buffer->undo_list = Qt;
+                            BVAR (current_buffer, undo_list));
+      BVAR (current_buffer, undo_list) = Qt;
       /* Don't do file-locking.  */
       record_unwind_protect (subst_char_in_region_unwind_1,
-                            current_buffer->filename);
-      current_buffer->filename = Qnil;
+                            BVAR (current_buffer, filename));
+      BVAR (current_buffer, filename) = Qnil;
     }
 
   if (pos_byte < GPT_BYTE)
     stop = min (stop, GPT_BYTE);
   while (1)
     {
-      int pos_byte_next = pos_byte;
+      EMACS_INT pos_byte_next = pos_byte;
 
       if (pos_byte >= stop)
        {
@@ -2828,11 +2909,11 @@ Both characters must have the same length of multi-byte form.  */)
 
              struct gcpro gcpro1;
 
-             tem = current_buffer->undo_list;
+             tem = BVAR (current_buffer, undo_list);
              GCPRO1 (tem);
 
              /* Make a multibyte string containing this single character.  */
-             string = make_multibyte_string (tostr, 1, len);
+             string = make_multibyte_string ((char *) tostr, 1, len);
              /* replace_range is less efficient, because it moves the gap,
                 but it handles combining correctly.  */
              replace_range (pos, pos + 1, string,
@@ -2847,7 +2928,7 @@ Both characters must have the same length of multi-byte form.  */)
                INC_POS (pos_byte_next);
 
              if (! NILP (noundo))
-               current_buffer->undo_list = tem;
+               BVAR (current_buffer, undo_list) = tem;
 
              UNGCPRO;
            }
@@ -2875,7 +2956,8 @@ Both characters must have the same length of multi-byte form.  */)
 }
 
 
-static Lisp_Object check_translation (int, int, int, Lisp_Object);
+static Lisp_Object check_translation (EMACS_INT, EMACS_INT, EMACS_INT,
+                                     Lisp_Object);
 
 /* Helper function for Ftranslate_region_internal.
 
@@ -2884,7 +2966,8 @@ static Lisp_Object check_translation (int, int, int, Lisp_Object);
    element is found, return it.  Otherwise return Qnil.  */
 
 static Lisp_Object
-check_translation (int pos, int pos_byte, int end, Lisp_Object val)
+check_translation (EMACS_INT pos, EMACS_INT pos_byte, EMACS_INT end,
+                  Lisp_Object val)
 {
   int buf_size = 16, buf_used = 0;
   int *buf = alloca (sizeof (int) * buf_size);
@@ -2892,7 +2975,7 @@ check_translation (int pos, int pos_byte, int end, Lisp_Object val)
   for (; CONSP (val); val = XCDR (val))
     {
       Lisp_Object elt;
-      int len, i;
+      EMACS_INT len, i;
 
       elt = XCAR (val);
       if (! CONSP (elt))
@@ -2908,7 +2991,7 @@ check_translation (int pos, int pos_byte, int end, Lisp_Object val)
              if (buf_used <= i)
                {
                  unsigned char *p = BYTE_POS_ADDR (pos_byte);
-                 int len;
+                 int len1;
 
                  if (buf_used == buf_size)
                    {
@@ -2919,8 +3002,8 @@ check_translation (int pos, int pos_byte, int end, Lisp_Object val)
                      memcpy (newbuf, buf, sizeof (int) * buf_used);
                      buf = newbuf;
                    }
-                 buf[buf_used++] = STRING_CHAR_AND_LENGTH (p, len);
-                 pos_byte += len;
+                 buf[buf_used++] = STRING_CHAR_AND_LENGTH (p, len1);
+                 pos_byte += len1;
                }
              if (XINT (AREF (elt, i)) != buf[i])
                break;
@@ -2945,11 +3028,10 @@ It returns the number of characters changed.  */)
   register unsigned char *tt;  /* Trans table. */
   register int nc;             /* New character. */
   int cnt;                     /* Number of changes made. */
-  int size;                    /* Size of translate table. */
-  int pos, pos_byte, end_pos;
-  int multibyte = !NILP (current_buffer->enable_multibyte_characters);
-  int string_multibyte;
-  Lisp_Object val;
+  EMACS_INT size;              /* Size of translate table. */
+  EMACS_INT pos, pos_byte, end_pos;
+  int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
+  int string_multibyte IF_LINT (= 0);
 
   validate_region (&start, &end);
   if (CHAR_TABLE_P (table))
@@ -3016,7 +3098,7 @@ It returns the number of characters changed.  */)
            }
          else
            {
-             int c;
+             EMACS_INT c;
 
              nc = oc;
              val = CHAR_TABLE_REF (table, oc);
@@ -3044,7 +3126,7 @@ It returns the number of characters changed.  */)
 
                  /* This is less efficient, because it moves the gap,
                     but it should handle multibyte characters correctly.  */
-                 string = make_multibyte_string (str, 1, str_len);
+                 string = make_multibyte_string ((char *) str, 1, str_len);
                  replace_range (pos, pos + 1, string, 1, 0, 1);
                  len = str_len;
                }
@@ -3208,7 +3290,7 @@ save_restriction_restore (Lisp_Object data)
                        ? XMARKER (XCAR (data))->buffer
                        : XBUFFER (data));
 
-  if (buf && buf != current_buffer && !NILP (buf->pt_marker))
+  if (buf && buf != current_buffer && !NILP (BVAR (buf, pt_marker)))
     { /* If `buf' uses markers to keep track of PT, BEGV, and ZV (as
         is the case if it is or has an indirect buffer), then make
         sure it is current before we update BEGV, so
@@ -3229,7 +3311,7 @@ save_restriction_restore (Lisp_Object data)
        /* The restriction has changed from the saved one, so restore
           the saved restriction.  */
        {
-         int pt = BUF_PT (buf);
+         EMACS_INT pt = BUF_PT (buf);
 
          SET_BUF_BEGV_BOTH (buf, beg->charpos, beg->bytepos);
          SET_BUF_ZV_BOTH (buf, end->charpos, end->bytepos);
@@ -3258,6 +3340,9 @@ save_restriction_restore (Lisp_Object data)
        }
     }
 
+  /* Changing the buffer bounds invalidates any recorded current column.  */
+  invalidate_current_column ();
+
   if (cur)
     set_buffer_internal (cur);
 
@@ -3508,21 +3593,21 @@ usage: (format STRING &rest OBJECTS)  */)
   (int nargs, register Lisp_Object *args)
 {
   register int n;              /* The number of the next arg to substitute */
-  register int total;          /* An estimate of the final length */
+  register EMACS_INT total;    /* An estimate of the final length */
   char *buf, *p;
-  register unsigned char *format, *end, *format_start;
+  register char *format, *end, *format_start;
   int nchars;
   /* Nonzero if the output should be a multibyte string,
      which is true if any of the inputs is one.  */
   int multibyte = 0;
   /* When we make a multibyte string, we must pay attention to the
      byte combining problem, i.e., a byte may be combined with a
-     multibyte charcter of the previous string.  This flag tells if we
+     multibyte character of the previous string.  This flag tells if we
      must consider such a situation or not.  */
   int maybe_combine_byte;
-  unsigned char *this_format;
+  char *this_format;
   /* Precision for each spec, or -1, a flag value meaning no precision
-     was given in that spec.  Element 0, corresonding to the format
+     was given in that spec.  Element 0, corresponding to the format
      string itself, will not be used.  Element NARGS, corresponding to
      no argument, *will* be assigned to in the case that a `%' and `.'
      occur after the final format specifier.  */
@@ -3574,7 +3659,7 @@ usage: (format STRING &rest OBJECTS)  */)
      That can only happen from the first large while loop below.  */
  retry:
 
-  format = SDATA (args[0]);
+  format = SSDATA (args[0]);
   format_start = format;
   end = format + SBYTES (args[0]);
   longest_format = 0;
@@ -3602,9 +3687,9 @@ usage: (format STRING &rest OBJECTS)  */)
   while (format != end)
     if (*format++ == '%')
       {
-       int thissize = 0;
-       int actual_width = 0;
-       unsigned char *this_format_start = format - 1;
+       EMACS_INT thissize = 0;
+       EMACS_INT actual_width = 0;
+       char *this_format_start = format - 1;
        int field_width = 0;
 
        /* General format specifications look like
@@ -3784,7 +3869,7 @@ usage: (format STRING &rest OBJECTS)  */)
   /* Now we can no longer jump to retry.
      TOTAL and LONGEST_FORMAT are known for certain.  */
 
-  this_format = (unsigned char *) alloca (longest_format + 1);
+  this_format = (char *) alloca (longest_format + 1);
 
   /* Allocate the space for the result.
      Note that TOTAL is an overestimate.  */
@@ -3795,7 +3880,7 @@ usage: (format STRING &rest OBJECTS)  */)
   n = 0;
 
   /* Scan the format and store result in BUF.  */
-  format = SDATA (args[0]);
+  format = SSDATA (args[0]);
   format_start = format;
   end = format + SBYTES (args[0]);
   maybe_combine_byte = 0;
@@ -3805,7 +3890,7 @@ usage: (format STRING &rest OBJECTS)  */)
        {
          int minlen;
          int negative = 0;
-         unsigned char *this_format_start = format;
+         char *this_format_start = format;
 
          discarded[format - format_start] = 1;
          format++;
@@ -3845,8 +3930,8 @@ usage: (format STRING &rest OBJECTS)  */)
              /* handle case (precision[n] >= 0) */
 
              int width, padding;
-             int nbytes, start, end;
-             int nchars_string;
+             EMACS_INT nbytes, start;
+             EMACS_INT nchars_string;
 
              /* lisp_string_width ignores a precision of 0, but GNU
                 libc functions print 0 characters when the precision
@@ -3857,7 +3942,8 @@ usage: (format STRING &rest OBJECTS)  */)
              if (precision[n] == 0)
                width = nchars_string = nbytes = 0;
              else if (precision[n] > 0)
-               width = lisp_string_width (args[n], precision[n], &nchars_string, &nbytes);
+               width = lisp_string_width (args[n], precision[n],
+                                          &nchars_string, &nbytes);
              else
                {               /* no precision spec given for this argument */
                  width = lisp_string_width (args[n], -1, NULL, NULL);
@@ -3876,7 +3962,6 @@ usage: (format STRING &rest OBJECTS)  */)
 
              info[n].start = start = nchars;
              nchars += nchars_string;
-             end = nchars;
 
              if (p > buf
                  && multibyte
@@ -3885,7 +3970,7 @@ usage: (format STRING &rest OBJECTS)  */)
                  && !CHAR_HEAD_P (SREF (args[n], 0)))
                maybe_combine_byte = 1;
 
-             p += copy_text (SDATA (args[n]), p,
+             p += copy_text (SDATA (args[n]), (unsigned char *) p,
                              nbytes,
                              STRING_MULTIBYTE (args[n]), multibyte);
 
@@ -3953,7 +4038,8 @@ usage: (format STRING &rest OBJECTS)  */)
                maybe_combine_byte = 1;
              this_nchars = strlen (p);
              if (multibyte)
-               p += str_to_multibyte (p, buf + total - 1 - p, this_nchars);
+               p += str_to_multibyte ((unsigned char *) p,
+                                      buf + total - 1 - p, this_nchars);
              else
                p += this_nchars;
              nchars += this_nchars;
@@ -3980,7 +4066,8 @@ usage: (format STRING &rest OBJECTS)  */)
       else if (multibyte)
        {
          /* Convert a single-byte character to multibyte.  */
-         int len = copy_text (format, p, 1, 0, 1);
+         int len = copy_text ((unsigned char *) format, (unsigned char *) p,
+                              1, 0, 1);
 
          p += len;
          format++;
@@ -3994,7 +4081,7 @@ usage: (format STRING &rest OBJECTS)  */)
     abort ();
 
   if (maybe_combine_byte)
-    nchars = multibyte_chars_in_text (buf, p - buf);
+    nchars = multibyte_chars_in_text ((unsigned char *) buf, p - buf);
   val = make_specified_string (buf, nchars, p - buf, multibyte);
 
   /* If we allocated BUF with malloc, free it too.  */
@@ -4016,7 +4103,8 @@ usage: (format STRING &rest OBJECTS)  */)
 
       if (CONSP (props))
        {
-         int bytepos = 0, position = 0, translated = 0, argn = 1;
+         EMACS_INT bytepos = 0, position = 0, translated = 0;
+         int argn = 1;
          Lisp_Object list;
 
          /* Adjust the bounds of each text property
@@ -4034,7 +4122,7 @@ usage: (format STRING &rest OBJECTS)  */)
          for (list = props; CONSP (list); list = XCDR (list))
            {
              Lisp_Object item;
-             int pos;
+             EMACS_INT pos;
 
              item = XCAR (list);
 
@@ -4125,33 +4213,28 @@ Case is ignored if `case-fold-search' is non-nil in the current buffer.  */)
 {
   int i1, i2;
   /* Check they're chars, not just integers, otherwise we could get array
-     bounds violations in DOWNCASE.  */
+     bounds violations in downcase.  */
   CHECK_CHARACTER (c1);
   CHECK_CHARACTER (c2);
 
   if (XINT (c1) == XINT (c2))
     return Qt;
-  if (NILP (current_buffer->case_fold_search))
+  if (NILP (BVAR (current_buffer, case_fold_search)))
     return Qnil;
 
-  /* Do these in separate statements,
-     then compare the variables.
-     because of the way DOWNCASE uses temp variables.  */
   i1 = XFASTINT (c1);
-  if (NILP (current_buffer->enable_multibyte_characters)
+  if (NILP (BVAR (current_buffer, enable_multibyte_characters))
       && ! ASCII_CHAR_P (i1))
     {
       MAKE_CHAR_MULTIBYTE (i1);
     }
   i2 = XFASTINT (c2);
-  if (NILP (current_buffer->enable_multibyte_characters)
+  if (NILP (BVAR (current_buffer, enable_multibyte_characters))
       && ! ASCII_CHAR_P (i2))
     {
       MAKE_CHAR_MULTIBYTE (i2);
     }
-  i1 = DOWNCASE (i1);
-  i2 = DOWNCASE (i2);
-  return (i1 == i2 ? Qt :  Qnil);
+  return (downcase (i1) == downcase (i2) ? Qt :  Qnil);
 }
 \f
 /* Transpose the markers in two regions of the current buffer, and
@@ -4170,11 +4253,12 @@ Case is ignored if `case-fold-search' is non-nil in the current buffer.  */)
    It's the caller's job to ensure that START1 <= END1 <= START2 <= END2.  */
 
 static void
-transpose_markers (int start1, int end1, int start2, int end2,
-                  int start1_byte, int end1_byte,
-                  int start2_byte, int end2_byte)
+transpose_markers (EMACS_INT start1, EMACS_INT end1,
+                  EMACS_INT start2, EMACS_INT end2,
+                  EMACS_INT start1_byte, EMACS_INT end1_byte,
+                  EMACS_INT start2_byte, EMACS_INT end2_byte)
 {
-  register int amt1, amt1_byte, amt2, amt2_byte, diff, diff_byte, mpos;
+  register EMACS_INT amt1, amt1_byte, amt2, amt2_byte, diff, diff_byte, mpos;
   register struct Lisp_Marker *marker;
 
   /* Update point as if it were a marker.  */
@@ -4271,7 +4355,7 @@ Transposing beyond buffer boundaries is an error.  */)
   /* Swap the regions if they're reversed.  */
   if (start2 < end1)
     {
-      register int glumph = start1;
+      register EMACS_INT glumph = start1;
       start1 = start2;
       start2 = glumph;
       glumph = end1;
@@ -4542,12 +4626,12 @@ syms_of_editfns (void)
     = intern_c_string ("buffer-access-fontify-functions");
   staticpro (&Qbuffer_access_fontify_functions);
 
-  DEFVAR_LISP ("inhibit-field-text-motion", &Vinhibit_field_text_motion,
+  DEFVAR_LISP ("inhibit-field-text-motion", Vinhibit_field_text_motion,
               doc: /* Non-nil means text motion commands don't notice fields.  */);
   Vinhibit_field_text_motion = Qnil;
 
   DEFVAR_LISP ("buffer-access-fontify-functions",
-              &Vbuffer_access_fontify_functions,
+              Vbuffer_access_fontify_functions,
               doc: /* List of functions called by `buffer-substring' to fontify if necessary.
 Each function is called with two arguments which specify the range
 of the buffer being accessed.  */);
@@ -4565,25 +4649,25 @@ of the buffer being accessed.  */);
   }
 
   DEFVAR_LISP ("buffer-access-fontified-property",
-              &Vbuffer_access_fontified_property,
+              Vbuffer_access_fontified_property,
               doc: /* Property which (if non-nil) indicates text has been fontified.
 `buffer-substring' need not call the `buffer-access-fontify-functions'
 functions if all the text being accessed has this property.  */);
   Vbuffer_access_fontified_property = Qnil;
 
-  DEFVAR_LISP ("system-name", &Vsystem_name,
+  DEFVAR_LISP ("system-name", Vsystem_name,
               doc: /* The host name of the machine Emacs is running on.  */);
 
-  DEFVAR_LISP ("user-full-name", &Vuser_full_name,
+  DEFVAR_LISP ("user-full-name", Vuser_full_name,
               doc: /* The full name of the user logged in.  */);
 
-  DEFVAR_LISP ("user-login-name", &Vuser_login_name,
+  DEFVAR_LISP ("user-login-name", Vuser_login_name,
               doc: /* The user's name, taken from environment variables if possible.  */);
 
-  DEFVAR_LISP ("user-real-login-name", &Vuser_real_login_name,
+  DEFVAR_LISP ("user-real-login-name", Vuser_real_login_name,
               doc: /* The user's name, based upon the real uid only.  */);
 
-  DEFVAR_LISP ("operating-system-release", &Voperating_system_release,
+  DEFVAR_LISP ("operating-system-release", Voperating_system_release,
               doc: /* The release of the operating system Emacs is running on.  */);
 
   defsubr (&Spropertize);
@@ -4679,6 +4763,3 @@ functions if all the text being accessed has this property.  */);
   defsubr (&Ssave_restriction);
   defsubr (&Stranspose_regions);
 }
-
-/* arch-tag: fc3827d8-6f60-4067-b11e-c3218031b018
-   (do not change this comment) */