]> code.delx.au - gnu-emacs/blobdiff - src/fileio.c
Make Fnext_read_file_uses_dialog_p compatible with recent DEFUN change.
[gnu-emacs] / src / fileio.c
index f0bd75b170efb6e1e63220488c96bd057d7fb10c..45a31c0e57331656de9ff6b1579177e662c0edff 100644 (file)
@@ -1,6 +1,6 @@
 /* File IO for GNU Emacs.
 
-Copyright (C) 1985-1988, 1993-2014 Free Software Foundation, Inc.
+Copyright (C) 1985-1988, 1993-2015 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -113,49 +113,10 @@ static bool auto_save_error_occurred;
 static bool valid_timestamp_file_system;
 static dev_t timestamp_file_system;
 
-/* The symbol bound to coding-system-for-read when
-   insert-file-contents is called for recovering a file.  This is not
-   an actual coding system name, but just an indicator to tell
-   insert-file-contents to use `emacs-mule' with a special flag for
-   auto saving and recovering a file.  */
-static Lisp_Object Qauto_save_coding;
-
-/* Property name of a file name handler,
-   which gives a list of operations it handles..  */
-static Lisp_Object Qoperations;
-
-/* Lisp functions for translating file formats.  */
-static Lisp_Object Qformat_decode, Qformat_annotate_function;
-
-/* Lisp function for setting buffer-file-coding-system and the
-   multibyteness of the current buffer after inserting a file.  */
-static Lisp_Object Qafter_insert_file_set_coding;
-
-static Lisp_Object Qwrite_region_annotate_functions;
 /* Each time an annotation function changes the buffer, the new buffer
    is added here.  */
 static Lisp_Object Vwrite_region_annotation_buffers;
 
-static Lisp_Object Qdelete_by_moving_to_trash;
-
-/* Lisp function for moving files to trash.  */
-static Lisp_Object Qmove_file_to_trash;
-
-/* Lisp function for recursively copying directories.  */
-static Lisp_Object Qcopy_directory;
-
-/* Lisp function for recursively deleting directories.  */
-static Lisp_Object Qdelete_directory;
-
-static Lisp_Object Qsubstitute_env_in_file_name;
-
-Lisp_Object Qfile_error, Qfile_notify_error;
-static Lisp_Object Qfile_already_exists, Qfile_date_error;
-static Lisp_Object Qexcl;
-Lisp_Object Qfile_name_history;
-
-static Lisp_Object Qcar_less_than_car;
-
 static bool a_write (int, Lisp_Object, ptrdiff_t, ptrdiff_t,
                     Lisp_Object *, struct coding_system *);
 static bool e_write (int, Lisp_Object, ptrdiff_t, ptrdiff_t,
@@ -197,7 +158,7 @@ check_writable (const char *filename, int amode)
   bool res = faccessat (AT_FDCWD, filename, amode, AT_EACCESS) == 0;
 #ifdef CYGWIN
   /* faccessat may have returned failure because Cygwin couldn't
-     determine the file's UID or GID; if so, we return success. */
+     determine the file's UID or GID; if so, we return success.  */
   if (!res)
     {
       int faccessat_errno = errno;
@@ -290,44 +251,6 @@ restore_point_unwind (Lisp_Object location)
 }
 
 \f
-static Lisp_Object Qexpand_file_name;
-static Lisp_Object Qsubstitute_in_file_name;
-static Lisp_Object Qdirectory_file_name;
-static Lisp_Object Qfile_name_directory;
-static Lisp_Object Qfile_name_nondirectory;
-static Lisp_Object Qunhandled_file_name_directory;
-static Lisp_Object Qfile_name_as_directory;
-static Lisp_Object Qcopy_file;
-static Lisp_Object Qmake_directory_internal;
-static Lisp_Object Qmake_directory;
-static Lisp_Object Qdelete_directory_internal;
-Lisp_Object Qdelete_file;
-static Lisp_Object Qrename_file;
-static Lisp_Object Qadd_name_to_file;
-static Lisp_Object Qmake_symbolic_link;
-Lisp_Object Qfile_exists_p;
-static Lisp_Object Qfile_executable_p;
-static Lisp_Object Qfile_readable_p;
-static Lisp_Object Qfile_writable_p;
-static Lisp_Object Qfile_symlink_p;
-static Lisp_Object Qaccess_file;
-Lisp_Object Qfile_directory_p;
-static Lisp_Object Qfile_regular_p;
-static Lisp_Object Qfile_accessible_directory_p;
-static Lisp_Object Qfile_modes;
-static Lisp_Object Qset_file_modes;
-static Lisp_Object Qset_file_times;
-static Lisp_Object Qfile_selinux_context;
-static Lisp_Object Qset_file_selinux_context;
-static Lisp_Object Qfile_acl;
-static Lisp_Object Qset_file_acl;
-static Lisp_Object Qfile_newer_than_file_p;
-Lisp_Object Qinsert_file_contents;
-static Lisp_Object Qchoose_write_coding_system;
-Lisp_Object Qwrite_region;
-static Lisp_Object Qverify_visited_file_modtime;
-static Lisp_Object Qset_visited_file_modtime;
-
 DEFUN ("find-file-name-handler", Ffind_file_name_handler,
        Sfind_file_name_handler, 2, 2, 0,
        doc: /* Return FILENAME's handler function for OPERATION, if it has one.
@@ -397,13 +320,6 @@ Otherwise return a directory name.
 Given a Unix syntax file name, returns a string ending in slash.  */)
   (Lisp_Object filename)
 {
-#ifndef DOS_NT
-  register const char *beg;
-#else
-  register char *beg;
-  Lisp_Object tem_fn;
-#endif
-  register const char *p;
   Lisp_Object handler;
 
   CHECK_STRING (filename);
@@ -418,12 +334,8 @@ Given a Unix syntax file name, returns a string ending in slash.  */)
       return STRINGP (handled_name) ? handled_name : Qnil;
     }
 
-#ifdef DOS_NT
-  beg = xlispstrdupa (filename);
-#else
-  beg = SSDATA (filename);
-#endif
-  p = beg + SBYTES (filename);
+  char *beg = SSDATA (filename);
+  char const *p = beg + SBYTES (filename);
 
   while (p != beg && !IS_DIRECTORY_SEP (p[-1])
 #ifdef DOS_NT
@@ -439,6 +351,11 @@ Given a Unix syntax file name, returns a string ending in slash.  */)
     return Qnil;
 #ifdef DOS_NT
   /* Expansion of "c:" to drive and default directory.  */
+  Lisp_Object tem_fn;
+  USE_SAFE_ALLOCA;
+  SAFE_ALLOCA_STRING (beg, filename);
+  p = beg + (p - SSDATA (filename));
+
   if (p[-1] == ':')
     {
       /* MAXPATHLEN+1 is guaranteed to be enough space for getdefdir.  */
@@ -482,6 +399,7 @@ Given a Unix syntax file name, returns a string ending in slash.  */)
       dostounix_filename (beg);
       tem_fn = make_specified_string (beg, -1, p - beg, 0);
     }
+  SAFE_FREE ();
   return tem_fn;
 #else  /* DOS_NT */
   return make_specified_string (beg, -1, p - beg, STRING_MULTIBYTE (filename));
@@ -604,8 +522,6 @@ For a Unix-syntax file name, just appends a slash.  */)
   USE_SAFE_ALLOCA;
 
   CHECK_STRING (file);
-  if (NILP (file))
-    return Qnil;
 
   /* If the file name has special constructs in it,
      call the corresponding file handler.  */
@@ -673,9 +589,6 @@ In Unix-syntax, this function just removes the final slash.  */)
 
   CHECK_STRING (directory);
 
-  if (NILP (directory))
-    return Qnil;
-
   /* If the file name has special constructs in it,
      call the corresponding file handler.  */
   handler = Ffind_file_name_handler (directory, Qdirectory_file_name);
@@ -734,7 +647,7 @@ Lisp_Object
 make_temp_name (Lisp_Object prefix, bool base64_p)
 {
   Lisp_Object val, encoded_prefix;
-  int len;
+  ptrdiff_t len;
   printmax_t pid;
   char *p, *data;
   char pidbuf[INT_BUFSIZE_BOUND (printmax_t)];
@@ -848,8 +761,6 @@ probably use `make-temp-file' instead, except in three circumstances:
   return make_temp_name (prefix, 0);
 }
 
-
-\f
 DEFUN ("expand-file-name", Fexpand_file_name, Sexpand_file_name, 1, 2, 0,
        doc: /* Convert filename NAME to absolute, and canonicalize it.
 Second arg DEFAULT-DIRECTORY is directory to start with if NAME is relative
@@ -879,7 +790,9 @@ filesystem tree, not (expand-file-name ".."  dirname).  */)
   /* These point to SDATA and need to be careful with string-relocation
      during GC (via DECODE_FILE).  */
   char *nm;
+  char *nmlim;
   const char *newdir;
+  const char *newdirlim;
   /* This should only point to alloca'd data.  */
   char *target;
 
@@ -887,10 +800,10 @@ filesystem tree, not (expand-file-name ".."  dirname).  */)
   struct passwd *pw;
 #ifdef DOS_NT
   int drive = 0;
-  bool collapse_newdir = 1;
+  bool collapse_newdir = true;
   bool is_escaped = 0;
 #endif /* DOS_NT */
-  ptrdiff_t length;
+  ptrdiff_t length, nbytes;
   Lisp_Object handler, result, handled_name;
   bool multibyte;
   Lisp_Object hdir;
@@ -1019,8 +932,9 @@ filesystem tree, not (expand-file-name ".."  dirname).  */)
     default_directory = Fdowncase (default_directory);
 #endif
 
-  /* Make a local copy of nm[] to protect it from GC in DECODE_FILE below.  */
-  nm = xlispstrdupa (name);
+  /* Make a local copy of NAME to protect it from GC in DECODE_FILE below.  */
+  SAFE_ALLOCA_STRING (nm, name);
+  nmlim = nm + SBYTES (name);
 
 #ifdef DOS_NT
   /* Note if special escape prefix is present, but remove for now.  */
@@ -1105,7 +1019,7 @@ filesystem tree, not (expand-file-name ".."  dirname).  */)
          if (IS_DIRECTORY_SEP (nm[1]))
            {
              if (strcmp (nm, SSDATA (name)) != 0)
-               name = make_specified_string (nm, -1, strlen (nm), multibyte);
+               name = make_specified_string (nm, -1, nmlim - nm, multibyte);
            }
          else
 #endif
@@ -1116,18 +1030,19 @@ filesystem tree, not (expand-file-name ".."  dirname).  */)
 
              name = make_specified_string (nm, -1, p - nm, multibyte);
              temp[0] = DRIVE_LETTER (drive);
-             name = concat2 (build_string (temp), name);
+             AUTO_STRING (drive_prefix, temp);
+             name = concat2 (drive_prefix, name);
            }
 #ifdef WINDOWSNT
          if (!NILP (Vw32_downcase_file_names))
            name = Fdowncase (name);
 #endif
-         return name;
 #else /* not DOS_NT */
-         if (strcmp (nm, SSDATA (name)) == 0)
-           return name;
-         return make_specified_string (nm, -1, strlen (nm), multibyte);
+         if (strcmp (nm, SSDATA (name)) != 0)
+           name = make_specified_string (nm, -1, nmlim - nm, multibyte);
 #endif /* not DOS_NT */
+         SAFE_FREE ();
+         return name;
        }
     }
 
@@ -1147,7 +1062,7 @@ filesystem tree, not (expand-file-name ".."  dirname).  */)
      return an absolute name, if the final prefix is not absolute we
      append it to the current working directory.  */
 
-  newdir = 0;
+  newdir = newdirlim = 0;
 
   if (nm[0] == '~')            /* prefix ~ */
     {
@@ -1157,7 +1072,7 @@ filesystem tree, not (expand-file-name ".."  dirname).  */)
          Lisp_Object tem;
 
          if (!(newdir = egetenv ("HOME")))
-           newdir = "";
+           newdir = newdirlim = "";
          nm++;
          /* `egetenv' may return a unibyte string, which will bite us since
             we expect the directory to be multibyte.  */
@@ -1167,18 +1082,20 @@ filesystem tree, not (expand-file-name ".."  dirname).  */)
              char newdir_utf8[MAX_UTF8_PATH];
 
              filename_from_ansi (newdir, newdir_utf8);
-             tem = build_string (newdir_utf8);
+             tem = make_unibyte_string (newdir_utf8, strlen (newdir_utf8));
            }
          else
 #endif
            tem = build_string (newdir);
+         newdirlim = newdir + SBYTES (tem);
          if (multibyte && !STRING_MULTIBYTE (tem))
            {
              hdir = DECODE_FILE (tem);
              newdir = SSDATA (hdir);
+             newdirlim = newdir + SBYTES (hdir);
            }
 #ifdef DOS_NT
-         collapse_newdir = 0;
+         collapse_newdir = false;
 #endif
        }
       else                     /* ~user/filename */
@@ -1201,15 +1118,17 @@ filesystem tree, not (expand-file-name ".."  dirname).  */)
              /* `getpwnam' may return a unibyte string, which will
                 bite us since we expect the directory to be
                 multibyte.  */
-             tem = build_string (newdir);
+             tem = make_unibyte_string (newdir, strlen (newdir));
+             newdirlim = newdir + SBYTES (tem);
              if (multibyte && !STRING_MULTIBYTE (tem))
                {
                  hdir = DECODE_FILE (tem);
                  newdir = SSDATA (hdir);
+                 newdirlim = newdir + SBYTES (hdir);
                }
              nm = p;
 #ifdef DOS_NT
-             collapse_newdir = 0;
+             collapse_newdir = false;
 #endif
            }
 
@@ -1235,8 +1154,11 @@ filesystem tree, not (expand-file-name ".."  dirname).  */)
              Lisp_Object tem = build_string (adir);
 
              tem = DECODE_FILE (tem);
+             newdirlim = adir + SBYTES (tem);
              memcpy (adir, SSDATA (tem), SBYTES (tem) + 1);
            }
+         else
+           newdirlim = adir + strlen (adir);
        }
       if (!adir)
        {
@@ -1246,6 +1168,7 @@ filesystem tree, not (expand-file-name ".."  dirname).  */)
          adir[1] = ':';
          adir[2] = '/';
          adir[3] = 0;
+         newdirlim = adir + 3;
        }
       newdir = adir;
     }
@@ -1266,6 +1189,7 @@ filesystem tree, not (expand-file-name ".."  dirname).  */)
       && !newdir)
     {
       newdir = SSDATA (default_directory);
+      newdirlim = newdir + SBYTES (default_directory);
 #ifdef DOS_NT
       /* Note if special escape prefix is present, but remove for now.  */
       if (newdir[0] == '/' && newdir[1] == ':')
@@ -1310,12 +1234,15 @@ filesystem tree, not (expand-file-name ".."  dirname).  */)
            }
          if (!IS_DIRECTORY_SEP (nm[0]))
            {
-             ptrdiff_t newlen = strlen (newdir);
-             char *tmp = alloca (newlen + file_name_as_directory_slop
-                                 + strlen (nm) + 1);
-             file_name_as_directory (tmp, newdir, newlen, multibyte);
-             strcat (tmp, nm);
+             ptrdiff_t nmlen = nmlim - nm;
+             ptrdiff_t newdirlen = newdirlim - newdir;
+             char *tmp = alloca (newdirlen + file_name_as_directory_slop
+                                 + nmlen + 1);
+             ptrdiff_t dlen = file_name_as_directory (tmp, newdir, newdirlen,
+                                                      multibyte);
+             memcpy (tmp + dlen, nm, nmlen + 1);
              nm = tmp;
+             nmlim = nm + dlen + nmlen;
            }
          adir = alloca (adir_size);
          if (drive)
@@ -1330,8 +1257,11 @@ filesystem tree, not (expand-file-name ".."  dirname).  */)
              Lisp_Object tem = build_string (adir);
 
              tem = DECODE_FILE (tem);
+             newdirlim = adir + SBYTES (tem);
              memcpy (adir, SSDATA (tem), SBYTES (tem) + 1);
            }
+         else
+           newdirlim = adir + strlen (adir);
          newdir = adir;
        }
 
@@ -1350,35 +1280,32 @@ filesystem tree, not (expand-file-name ".."  dirname).  */)
          if (IS_DIRECTORY_SEP (newdir[0]) && IS_DIRECTORY_SEP (newdir[1])
              && !IS_DIRECTORY_SEP (newdir[2]))
            {
-             char *adir = strcpy (alloca (strlen (newdir) + 1), newdir);
+             char *adir = strcpy (alloca (newdirlim - newdir + 1), newdir);
              char *p = adir + 2;
              while (*p && !IS_DIRECTORY_SEP (*p)) p++;
              p++;
              while (*p && !IS_DIRECTORY_SEP (*p)) p++;
              *p = 0;
              newdir = adir;
+             newdirlim = newdir + strlen (adir);
            }
          else
 #endif
-           newdir = "";
+           newdir = newdirlim = "";
        }
     }
 #endif /* DOS_NT */
 
-  if (newdir)
-    {
-      /* Ignore any slash at the end of newdir, unless newdir is
-        just "/" or "//".  */
-      length = strlen (newdir);
-      while (length > 1 && IS_DIRECTORY_SEP (newdir[length - 1])
-            && ! (length == 2 && IS_DIRECTORY_SEP (newdir[0])))
-       length--;
-    }
-  else
-    length = 0;
+  /* Ignore any slash at the end of newdir, unless newdir is
+     just "/" or "//".  */
+  length = newdirlim - newdir;
+  while (length > 1 && IS_DIRECTORY_SEP (newdir[length - 1])
+        && ! (length == 2 && IS_DIRECTORY_SEP (newdir[0])))
+    length--;
 
   /* Now concatenate the directory and name to new space in the stack frame.  */
-  tlen = length + file_name_as_directory_slop + strlen (nm) + 1;
+  tlen = length + file_name_as_directory_slop + (nmlim - nm) + 1;
+  eassert (tlen > file_name_as_directory_slop + 1);
 #ifdef DOS_NT
   /* Reserve space for drive specifier and escape prefix, since either
      or both may need to be inserted.  (The Microsoft x86 compiler
@@ -1389,6 +1316,7 @@ filesystem tree, not (expand-file-name ".."  dirname).  */)
   target = SAFE_ALLOCA (tlen);
 #endif /* not DOS_NT */
   *target = 0;
+  nbytes = 0;
 
   if (newdir)
     {
@@ -1406,13 +1334,14 @@ filesystem tree, not (expand-file-name ".."  dirname).  */)
            {
              memcpy (target, newdir, length);
              target[length] = 0;
+             nbytes = length;
            }
        }
       else
-       file_name_as_directory (target, newdir, length, multibyte);
+       nbytes = file_name_as_directory (target, newdir, length, multibyte);
     }
 
-  strcat (target, nm);
+  memcpy (target + nbytes, nm, nmlim - nm + 1);
 
   /* Now canonicalize by removing `//', `/.' and `/foo/..' if they
      appear.  */
@@ -1718,7 +1647,8 @@ search_embedded_absfilename (char *nm, char *endp)
          for (s = p; *s && !IS_DIRECTORY_SEP (*s); s++);
          if (p[0] == '~' && s > p + 1) /* We've got "/~something/".  */
            {
-             char *o = alloca (s - p + 1);
+             USE_SAFE_ALLOCA;
+             char *o = SAFE_ALLOCA (s - p + 1);
              struct passwd *pw;
              memcpy (o, p, s - p);
              o [s - p] = 0;
@@ -1729,6 +1659,7 @@ search_embedded_absfilename (char *nm, char *endp)
              block_input ();
              pw = getpwnam (o + 1);
              unblock_input ();
+             SAFE_FREE ();
              if (pw)
                return p;
            }
@@ -1777,7 +1708,8 @@ those `/' is discarded.  */)
   /* Always work on a copy of the string, in case GC happens during
      decode of environment variables, causing the original Lisp_String
      data to be relocated.  */
-  nm = xlispstrdupa (filename);
+  USE_SAFE_ALLOCA;
+  SAFE_ALLOCA_STRING (nm, filename);
 
 #ifdef DOS_NT
   dostounix_filename (nm);
@@ -1791,8 +1723,13 @@ those `/' is discarded.  */)
     /* Start over with the new string, so we check the file-name-handler
        again.  Important with filenames like "/home/foo//:/hello///there"
        which would substitute to "/:/hello///there" rather than "/there".  */
-    return Fsubstitute_in_file_name
-      (make_specified_string (p, -1, endp - p, multibyte));
+    {
+      Lisp_Object result
+       = (Fsubstitute_in_file_name
+          (make_specified_string (p, -1, endp - p, multibyte)));
+      SAFE_FREE ();
+      return result;
+    }
 
   /* See if any variables are substituted into the string.  */
 
@@ -1814,6 +1751,7 @@ those `/' is discarded.  */)
       if (!NILP (Vw32_downcase_file_names))
        filename = Fdowncase (filename);
 #endif
+      SAFE_FREE ();
       return filename;
     }
 
@@ -1832,14 +1770,14 @@ those `/' is discarded.  */)
     {
       Lisp_Object xname = make_specified_string (xnm, -1, x - xnm, multibyte);
 
-      xname = Fdowncase (xname);
-      return xname;
+      filename = Fdowncase (xname);
     }
   else
 #endif
-  return (xnm == SSDATA (filename)
-         ? filename
-         : make_specified_string (xnm, -1, x - xnm, multibyte));
+  if (xnm != SSDATA (filename))
+    filename = make_specified_string (xnm, -1, x - xnm, multibyte);
+  SAFE_FREE ();
+  return filename;
 }
 \f
 /* A slightly faster and more convenient way to get
@@ -2672,7 +2610,10 @@ emacs_readlinkat (int fd, char const *filename)
 
   val = build_unibyte_string (buf);
   if (buf[0] == '/' && strchr (buf, ':'))
-    val = concat2 (build_unibyte_string ("/:"), val);
+    {
+      AUTO_STRING (slash_colon, "/:");
+      val = concat2 (slash_colon, val);
+    }
   if (buf != readlink_buf)
     xfree (buf);
   val = DECODE_FILE (val);
@@ -2766,23 +2707,24 @@ searchable directory.  */)
     }
 
   absname = ENCODE_FILE (absname);
-  return file_accessible_directory_p (SSDATA (absname)) ? Qt : Qnil;
+  return file_accessible_directory_p (absname) ? Qt : Qnil;
 }
 
 /* If FILE is a searchable directory or a symlink to a
    searchable directory, return true.  Otherwise return
    false and set errno to an error number.  */
 bool
-file_accessible_directory_p (char const *file)
+file_accessible_directory_p (Lisp_Object file)
 {
 #ifdef DOS_NT
   /* There's no need to test whether FILE is searchable, as the
      searchable/executable bit is invented on DOS_NT platforms.  */
-  return file_directory_p (file);
+  return file_directory_p (SSDATA (file));
 #else
   /* On POSIXish platforms, use just one system call; this avoids a
      race and is typically faster.  */
-  ptrdiff_t len = strlen (file);
+  const char *data = SSDATA (file);
+  ptrdiff_t len = SBYTES (file);
   char const *dir;
   bool ok;
   int saved_errno;
@@ -2794,15 +2736,15 @@ file_accessible_directory_p (char const *file)
      "/" and "//" are distinct on some platforms, whereas "/", "///",
      "////", etc. are all equivalent.  */
   if (! len)
-    dir = file;
+    dir = data;
   else
     {
       /* Just check for trailing '/' when deciding whether to append '/'.
         That's simpler than testing the two special cases "/" and "//",
         and it's a safe optimization here.  */
       char *buf = SAFE_ALLOCA (len + 3);
-      memcpy (buf, file, len);
-      strcpy (buf + len, &"/."[file[len - 1] == '/']);
+      memcpy (buf, data, len);
+      strcpy (buf + len, &"/."[data[len - 1] == '/']);
       dir = buf;
     }
 
@@ -3387,6 +3329,56 @@ time_error_value (int errnum)
   return make_timespec (0, ns);
 }
 
+static Lisp_Object
+get_window_points_and_markers (void)
+{
+  Lisp_Object pt_marker = Fpoint_marker ();
+  Lisp_Object windows
+    = call3 (Qget_buffer_window_list, Fcurrent_buffer (), Qnil, Qt);
+  Lisp_Object window_markers = windows;
+  /* Window markers (and point) are handled specially: rather than move to
+     just before or just after the modified text, we try to keep the
+     markers at the same distance (bug#19161).
+     In general, this is wrong, but for window-markers, this should be harmless
+     and is convenient for the end user when most of the file is unmodified,
+     except for a few minor details near the beginning and near the end.  */
+  for (; CONSP (windows); windows = XCDR (windows))
+    if (WINDOWP (XCAR (windows)))
+      {
+       Lisp_Object window_marker = XWINDOW (XCAR (windows))->pointm;
+       XSETCAR (windows,
+                Fcons (window_marker, Fmarker_position (window_marker)));
+      }
+  return Fcons (Fcons (pt_marker, Fpoint ()), window_markers);
+}
+
+static void
+restore_window_points (Lisp_Object window_markers, ptrdiff_t inserted,
+                      ptrdiff_t same_at_start, ptrdiff_t same_at_end)
+{
+  for (; CONSP (window_markers); window_markers = XCDR (window_markers))
+    if (CONSP (XCAR (window_markers)))
+      {
+       Lisp_Object car = XCAR (window_markers);
+       Lisp_Object marker = XCAR (car);
+       Lisp_Object oldpos = XCDR (car);
+       if (MARKERP (marker) && INTEGERP (oldpos)
+           && XINT (oldpos) > same_at_start
+           && XINT (oldpos) < same_at_end)
+         {
+           ptrdiff_t oldsize = same_at_end - same_at_start;
+           ptrdiff_t newsize = inserted;
+           double growth = newsize / (double)oldsize;
+           ptrdiff_t newpos
+             = same_at_start + growth * (XINT (oldpos) - same_at_start);
+           Fset_marker (marker, make_number (newpos), Qnil);
+         }
+      }
+}
+
+/* FIXME: insert-file-contents should be split with the top-level moved to
+   Elisp and only the core kept in C.  */
+
 DEFUN ("insert-file-contents", Finsert_file_contents, Sinsert_file_contents,
        1, 5, 0,
        doc: /* Insert contents of file FILENAME after point.
@@ -3431,24 +3423,32 @@ by calling `format-decode', which see.  */)
   int save_errno = 0;
   char read_buf[READ_BUF_SIZE];
   struct coding_system coding;
-  bool replace_handled = 0;
-  bool set_coding_system = 0;
+  bool replace_handled = false;
+  bool set_coding_system = false;
   Lisp_Object coding_system;
-  bool read_quit = 0;
+  bool read_quit = false;
   /* If the undo log only contains the insertion, there's no point
      keeping it.  It's typically when we first fill a file-buffer.  */
   bool empty_undo_list_p
     = (!NILP (visit) && NILP (BVAR (current_buffer, undo_list))
        && BEG == Z);
   Lisp_Object old_Vdeactivate_mark = Vdeactivate_mark;
-  bool we_locked_file = 0;
+  bool we_locked_file = false;
   ptrdiff_t fd_index;
+  Lisp_Object window_markers = Qnil;
+  /* same_at_start and same_at_end count bytes, because file access counts
+     bytes and BEG and END count bytes.  */
+  ptrdiff_t same_at_start = BEGV_BYTE;
+  ptrdiff_t same_at_end = ZV_BYTE;
+  /* SAME_AT_END_CHARPOS counts characters, because
+     restore_window_points needs the old character count.  */
+  ptrdiff_t same_at_end_charpos = ZV;
 
   if (current_buffer->base_buffer && ! NILP (visit))
     error ("Cannot do file visiting in an indirect buffer");
 
   if (!NILP (BVAR (current_buffer, read_only)))
-    Fbarf_if_buffer_read_only ();
+    Fbarf_if_buffer_read_only (Qnil);
 
   val = Qnil;
   p = Qnil;
@@ -3498,7 +3498,11 @@ by calling `format-decode', which see.  */)
 
   /* Replacement should preserve point as it preserves markers.  */
   if (!NILP (replace))
-    record_unwind_protect (restore_point_unwind, Fpoint_marker ());
+    {
+      window_markers = get_window_points_and_markers ();
+      record_unwind_protect (restore_point_unwind,
+                            XCAR (XCAR (window_markers)));
+    }
 
   if (fstat (fd, &st) != 0)
     report_file_error ("Input file status", orig_filename);
@@ -3576,14 +3580,14 @@ by calling `format-decode', which see.  */)
     }
 
   /* Prevent redisplay optimizations.  */
-  current_buffer->clip_changed = 1;
+  current_buffer->clip_changed = true;
 
   if (EQ (Vcoding_system_for_read, Qauto_save_coding))
     {
       coding_system = coding_inherit_eol_type (Qutf_8_emacs, Qunix);
       setup_coding_system (coding_system, &coding);
       /* Ensure we set Vlast_coding_system_used.  */
-      set_coding_system = 1;
+      set_coding_system = true;
     }
   else if (BEG < Z)
     {
@@ -3625,13 +3629,14 @@ by calling `format-decode', which see.  */)
                report_file_error ("Read error", orig_filename);
              else if (nread > 0)
                {
+                 AUTO_STRING (name, " *code-converting-work*");
                  struct buffer *prev = current_buffer;
                  Lisp_Object workbuf;
                  struct buffer *buf;
 
                  record_unwind_current_buffer ();
 
-                 workbuf = Fget_buffer_create (build_string (" *code-converting-work*"));
+                 workbuf = Fget_buffer_create (name);
                  buf = XBUFFER (workbuf);
 
                  delete_all_overlays (buf);
@@ -3688,7 +3693,7 @@ by calling `format-decode', which see.  */)
 
       setup_coding_system (coding_system, &coding);
       /* Ensure we set Vlast_coding_system_used.  */
-      set_coding_system = 1;
+      set_coding_system = true;
     }
 
   /* If requested, replace the accessible part of the buffer
@@ -3710,16 +3715,11 @@ by calling `format-decode', which see.  */)
       && (NILP (coding_system)
          || ! CODING_REQUIRE_DECODING (&coding)))
     {
-      /* same_at_start and same_at_end count bytes,
-        because file access counts bytes
-        and BEG and END count bytes.  */
-      ptrdiff_t same_at_start = BEGV_BYTE;
-      ptrdiff_t same_at_end = ZV_BYTE;
       ptrdiff_t overlap;
       /* There is still a possibility we will find the need to do code
         conversion.  If that happens, set this variable to
         give up on handling REPLACE in the optimized way.  */
-      bool giveup_match_end = 0;
+      bool giveup_match_end = false;
 
       if (beg_offset != 0)
        {
@@ -3753,7 +3753,7 @@ by calling `format-decode', which see.  */)
            /* We found that the file should be decoded somehow.
                Let's give up here.  */
            {
-             giveup_match_end = 1;
+             giveup_match_end = true;
              break;
            }
 
@@ -3766,7 +3766,7 @@ by calling `format-decode', which see.  */)
          if (bufpos != nread)
            break;
        }
-      immediate_quit = 0;
+      immediate_quit = false;
       /* If the file matches the buffer completely,
         there's no need to replace anything.  */
       if (same_at_start - BEGV_BYTE == end_offset - beg_offset)
@@ -3778,7 +3778,7 @@ by calling `format-decode', which see.  */)
          del_range_1 (same_at_start, same_at_end, 0, 0);
          goto handled;
        }
-      immediate_quit = 1;
+      immediate_quit = true;
       QUIT;
       /* Count how many chars at the end of the file
         match the text at the end of the buffer.  But, if we have
@@ -3829,7 +3829,7 @@ by calling `format-decode', which see.  */)
                  && FETCH_BYTE (same_at_end - 1) >= 0200
                  && ! NILP (BVAR (current_buffer, enable_multibyte_characters))
                  && (CODING_MAY_REQUIRE_DECODING (&coding)))
-               giveup_match_end = 1;
+               giveup_match_end = true;
              break;
            }
 
@@ -3864,6 +3864,7 @@ by calling `format-decode', which see.  */)
                        + (! NILP (end) ? end_offset : st.st_size) - ZV_BYTE));
          if (overlap > 0)
            same_at_end += overlap;
+         same_at_end_charpos = BYTE_TO_CHAR (same_at_end);
 
          /* Arrange to read only the nonmatching middle part of the file.  */
          beg_offset += same_at_start - BEGV_BYTE;
@@ -3871,7 +3872,7 @@ by calling `format-decode', which see.  */)
 
          invalidate_buffer_caches (current_buffer,
                                    BYTE_TO_CHAR (same_at_start),
-                                   BYTE_TO_CHAR (same_at_end));
+                                   same_at_end_charpos);
          del_range_byte (same_at_start, same_at_end, 0);
          /* Insert from the file at the proper position.  */
          temp = BYTE_TO_CHAR (same_at_start);
@@ -3882,7 +3883,7 @@ by calling `format-decode', which see.  */)
          if (XBUFFER (XWINDOW (selected_window)->contents) == current_buffer)
            XWINDOW (selected_window)->start_at_line_beg = !NILP (Fbolp ());
 
-         replace_handled = 1;
+         replace_handled = true;
        }
     }
 
@@ -3897,8 +3898,6 @@ by calling `format-decode', which see.  */)
      in a more optimized way.  */
   if (!NILP (replace) && ! replace_handled && BEGV < ZV)
     {
-      ptrdiff_t same_at_start = BEGV_BYTE;
-      ptrdiff_t same_at_end = ZV_BYTE;
       ptrdiff_t same_at_start_charpos;
       ptrdiff_t inserted_chars;
       ptrdiff_t overlap;
@@ -3962,7 +3961,7 @@ by calling `format-decode', which see.  */)
        }
 
       coding_system = CODING_ID_NAME (coding.id);
-      set_coding_system = 1;
+      set_coding_system = true;
       decoded = BUF_BEG_ADDR (XBUFFER (conversion_buffer));
       inserted = (BUF_Z_BYTE (XBUFFER (conversion_buffer))
                  - BUF_BEG_BYTE (XBUFFER (conversion_buffer)));
@@ -4022,6 +4021,7 @@ by calling `format-decode', which see.  */)
       overlap = same_at_start - BEGV_BYTE - (same_at_end + inserted - ZV_BYTE);
       if (overlap > 0)
        same_at_end += overlap;
+      same_at_end_charpos = BYTE_TO_CHAR (same_at_end);
 
       /* If display currently starts at beginning of line,
         keep it that way.  */
@@ -4037,7 +4037,7 @@ by calling `format-decode', which see.  */)
        {
          invalidate_buffer_caches (current_buffer,
                                    BYTE_TO_CHAR (same_at_start),
-                                   BYTE_TO_CHAR (same_at_end));
+                                   same_at_end_charpos);
          del_range_byte (same_at_start, same_at_end, 0);
          temp = GPT;
          eassert (same_at_start == GPT_BYTE);
@@ -4045,7 +4045,7 @@ by calling `format-decode', which see.  */)
        }
       else
        {
-         temp = BYTE_TO_CHAR (same_at_start);
+         temp = same_at_end_charpos;
        }
       /* Insert from the file at the proper position.  */
       SET_PT_BOTH (temp, same_at_start);
@@ -4087,7 +4087,7 @@ by calling `format-decode', which see.  */)
          /* Make binding buffer-file-name to nil effective.  */
          && !NILP (BVAR (current_buffer, filename))
          && SAVE_MODIFF >= MODIFF)
-       we_locked_file = 1;
+       we_locked_file = true;
       prepare_to_modify_buffer (PT, PT, NULL);
     }
 
@@ -4117,7 +4117,7 @@ by calling `format-decode', which see.  */)
 
     while (how_much < total)
       {
-       /* try is reserved in some compilers (Microsoft C) */
+       /* `try' is reserved in some compilers (Microsoft C).  */
        ptrdiff_t trytry = min (total - how_much, READ_BUF_SIZE);
        ptrdiff_t this;
 
@@ -4142,7 +4142,7 @@ by calling `format-decode', which see.  */)
 
            if (NILP (nbytes))
              {
-               read_quit = 1;
+               read_quit = true;
                break;
              }
 
@@ -4275,7 +4275,7 @@ by calling `format-decode', which see.  */)
        coding_system = raw_text_coding_system (coding_system);
       setup_coding_system (coding_system, &coding);
       /* Ensure we set Vlast_coding_system_used.  */
-      set_coding_system = 1;
+      set_coding_system = true;
     }
 
   if (!NILP (visit))
@@ -4286,7 +4286,7 @@ by calling `format-decode', which see.  */)
          /* Can't do this if part of the buffer might be preserved.  */
          && NILP (replace))
        /* Visiting a file with these coding system makes the buffer
-          unibyte. */
+          unibyte.  */
        bset_enable_multibyte_characters (current_buffer, Qnil);
     }
 
@@ -4305,8 +4305,11 @@ by calling `format-decode', which see.  */)
       coding_system = CODING_ID_NAME (coding.id);
     }
   else if (inserted > 0)
-    adjust_after_insert (PT, PT_BYTE, PT + inserted, PT_BYTE + inserted,
-                        inserted);
+    {
+      invalidate_buffer_caches (current_buffer, PT, PT + inserted);
+      adjust_after_insert (PT, PT_BYTE, PT + inserted, PT_BYTE + inserted,
+                          inserted);
+    }
 
   /* Call after-change hooks for the inserted text, aside from the case
      of normal visiting (not with REPLACE), which is done in a new buffer
@@ -4322,6 +4325,11 @@ by calling `format-decode', which see.  */)
 
  handled:
 
+  if (inserted > 0)
+    restore_window_points (window_markers, inserted,
+                          BYTE_TO_CHAR (same_at_start),
+                          same_at_end_charpos);
+
   if (!NILP (visit))
     {
       if (empty_undo_list_p)
@@ -4525,24 +4533,14 @@ build_annotations_unwind (Lisp_Object arg)
 
 /* Decide the coding-system to encode the data with.  */
 
-DEFUN ("choose-write-coding-system", Fchoose_write_coding_system,
-       Schoose_write_coding_system, 3, 6, 0,
-       doc: /* Choose the coding system for writing a file.
-Arguments are as for `write-region'.
-This function is for internal use only.  It may prompt the user.  */ )
-  (Lisp_Object start, Lisp_Object end, Lisp_Object filename,
-   Lisp_Object append, Lisp_Object visit, Lisp_Object lockname)
+static Lisp_Object
+choose_write_coding_system (Lisp_Object start, Lisp_Object end, Lisp_Object filename,
+                           Lisp_Object append, Lisp_Object visit, Lisp_Object lockname,
+                           struct coding_system *coding)
 {
   Lisp_Object val;
   Lisp_Object eol_parent = Qnil;
 
-  /* Mimic write-region behavior.  */
-  if (NILP (start))
-    {
-      XSETFASTINT (start, BEGV);
-      XSETFASTINT (end, ZV);
-    }
-
   if (auto_saving
       && NILP (Fstring_equal (BVAR (current_buffer, filename),
                              BVAR (current_buffer, auto_save_file_name))))
@@ -4635,6 +4633,10 @@ This function is for internal use only.  It may prompt the user.  */ )
     }
 
   val = coding_inherit_eol_type (val, eol_parent);
+  setup_coding_system (val, coding);
+
+  if (!STRINGP (start) && !NILP (BVAR (current_buffer, selective_display)))
+    coding->mode |= CODING_MODE_SELECTIVE_DISPLAY;
   return val;
 }
 
@@ -4657,8 +4659,8 @@ Optional fifth argument VISIT, if t or a string, means
 If VISIT is a string, it is a second file name;
   the output goes to FILENAME, but the buffer is marked as visiting VISIT.
   VISIT is also the file name to lock and unlock for clash detection.
-If VISIT is neither t nor nil nor a string,
-  that means do not display the \"Wrote file\" message.
+If VISIT is neither t nor nil nor a string, or if Emacs is in batch mode,
+  do not display the \"Wrote file\" message.
 The optional sixth arg LOCKNAME, if non-nil, specifies the name to
   use for locking and unlocking, overriding FILENAME and VISIT.
 The optional seventh arg MUSTBENEW, if non-nil, insists on a check
@@ -4803,14 +4805,9 @@ write_region (Lisp_Object start, Lisp_Object end, Lisp_Object filename,
      We used to make this choice before calling build_annotations, but that
      leads to problems when a write-annotate-function takes care of
      unsavable chars (as was the case with X-Symbol).  */
-  Vlast_coding_system_used =
-    Fchoose_write_coding_system (start, end, filename,
-                                 append, visit, lockname);
-
-  setup_coding_system (Vlast_coding_system_used, &coding);
-
-  if (!STRINGP (start) && !NILP (BVAR (current_buffer, selective_display)))
-    coding.mode |= CODING_MODE_SELECTIVE_DISPLAY;
+  Vlast_coding_system_used
+    = choose_write_coding_system (start, end, filename,
+                                 append, visit, lockname, &coding);
 
   if (open_and_close_file && !auto_saving)
     {
@@ -5030,7 +5027,7 @@ write_region (Lisp_Object start, Lisp_Object end, Lisp_Object filename,
       return Qnil;
     }
 
-  if (!auto_saving)
+  if (!auto_saving && !noninteractive)
     message_with_string ((NUMBERP (append)
                          ? "Updated %s"
                          : ! NILP (append)
@@ -5304,20 +5301,12 @@ If BUF is omitted or nil, it defaults to the current buffer.
 See Info node `(elisp)Modification Time' for more details.  */)
   (Lisp_Object buf)
 {
-  struct buffer *b;
+  struct buffer *b = decode_buffer (buf);
   struct stat st;
   Lisp_Object handler;
   Lisp_Object filename;
   struct timespec mtime;
 
-  if (NILP (buf))
-    b = current_buffer;
-  else
-    {
-      CHECK_BUFFER (buf);
-      b = XBUFFER (buf);
-    }
-
   if (!STRINGP (BVAR (b, filename))) return Qt;
   if (b->modtime.tv_nsec == UNKNOWN_MODTIME_NSECS) return Qt;
 
@@ -5411,7 +5400,7 @@ An argument specifies the modification time value to use
 static Lisp_Object
 auto_save_error (Lisp_Object error_val)
 {
-  Lisp_Object args[3], msg;
+  Lisp_Object msg;
   int i;
   struct gcpro gcpro1;
 
@@ -5419,10 +5408,10 @@ auto_save_error (Lisp_Object error_val)
 
   ring_bell (XFRAME (selected_frame));
 
-  args[0] = build_string ("Auto-saving %s: %s");
-  args[1] = BVAR (current_buffer, name);
-  args[2] = Ferror_message_string (error_val);
-  msg = Fformat (3, args);
+  AUTO_STRING (format, "Auto-saving %s: %s");
+  msg = Fformat (3, ((Lisp_Object [])
+                    {format, BVAR (current_buffer, name),
+                     Ferror_message_string (error_val)}));
   GCPRO1 (msg);
 
   for (i = 0; i < 3; ++i)
@@ -5744,25 +5733,35 @@ then any auto-save counts as "recent".  */)
      they're never autosaved.  */
   return (SAVE_MODIFF < BUF_AUTOSAVE_MODIFF (current_buffer) ? Qt : Qnil);
 }
-\f
-/* Reading and completing file names */
+
+/* We want Fnext_read_file_uses_dialog_p to have ATTRIBUTE_CONST
+   regardless of #ifdefs, so there is a trivial workaround.  See
+   http://lists.gnu.org/archive/html/emacs-devel/2015-01/msg00289.html.  */
+
+static bool
+next_read_file_uses_dialog_p (void)
+{
+#if defined (USE_MOTIF) || defined (HAVE_NTGUI) || defined (USE_GTK) \
+  || defined (HAVE_NS)
+  return ((NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
+         && use_dialog_box
+         && use_file_dialog
+         && window_system_available (SELECTED_FRAME ()));
+#endif
+  return false;
+}
+
+/* Reading and completing file names.  */
 
 DEFUN ("next-read-file-uses-dialog-p", Fnext_read_file_uses_dialog_p,
        Snext_read_file_uses_dialog_p, 0, 0, 0,
        doc: /* Return t if a call to `read-file-name' will use a dialog.
 The return value is only relevant for a call to `read-file-name' that happens
-before any other event (mouse or keypress) is handled.  */)
+before any other event (mouse or keypress) is handled.  */
+       attributes: const)
   (void)
 {
-#if defined (USE_MOTIF) || defined (HAVE_NTGUI) || defined (USE_GTK) \
-  || defined (HAVE_NS)
-  if ((NILP (last_nonmenu_event) || CONSP (last_nonmenu_event))
-      && use_dialog_box
-      && use_file_dialog
-      && window_system_available (SELECTED_FRAME ()))
-    return Qt;
-#endif
-  return Qnil;
+  return next_read_file_uses_dialog_p () ? Qt : Qnil;
 }
 
 void
@@ -5795,7 +5794,10 @@ init_fileio (void)
 void
 syms_of_fileio (void)
 {
+  /* Property name of a file name handler,
+     which gives a list of operations it handles.  */
   DEFSYM (Qoperations, "operations");
+
   DEFSYM (Qexpand_file_name, "expand-file-name");
   DEFSYM (Qsubstitute_in_file_name, "substitute-in-file-name");
   DEFSYM (Qdirectory_file_name, "directory-file-name");
@@ -5829,10 +5831,15 @@ syms_of_fileio (void)
   DEFSYM (Qset_file_acl, "set-file-acl");
   DEFSYM (Qfile_newer_than_file_p, "file-newer-than-file-p");
   DEFSYM (Qinsert_file_contents, "insert-file-contents");
-  DEFSYM (Qchoose_write_coding_system, "choose-write-coding-system");
   DEFSYM (Qwrite_region, "write-region");
   DEFSYM (Qverify_visited_file_modtime, "verify-visited-file-modtime");
   DEFSYM (Qset_visited_file_modtime, "set-visited-file-modtime");
+
+  /* The symbol bound to coding-system-for-read when
+     insert-file-contents is called for recovering a file.  This is not
+     an actual coding system name, but just an indicator to tell
+     insert-file-contents to use `emacs-mule' with a special flag for
+     auto saving and recovering a file.  */
   DEFSYM (Qauto_save_coding, "auto-save-coding");
 
   DEFSYM (Qfile_name_history, "file-name-history");
@@ -5868,9 +5875,14 @@ On MS-Windows, the value of this variable is largely ignored if
 behaves as if file names were encoded in `utf-8'.  */);
   Vdefault_file_name_coding_system = Qnil;
 
+  /* Lisp functions for translating file formats.  */
   DEFSYM (Qformat_decode, "format-decode");
   DEFSYM (Qformat_annotate_function, "format-annotate-function");
+
+  /* Lisp function for setting buffer-file-coding-system and the
+     multibyteness of the current buffer after inserting a file.  */
   DEFSYM (Qafter_insert_file_set_coding, "after-insert-file-set-coding");
+
   DEFSYM (Qcar_less_than_car, "car-less-than-car");
 
   Fput (Qfile_error, Qerror_conditions,
@@ -6024,12 +6036,19 @@ When non-nil, certain file deletion commands use the function
 This includes interactive calls to `delete-file' and
 `delete-directory' and the Dired deletion commands.  */);
   delete_by_moving_to_trash = 0;
-  Qdelete_by_moving_to_trash = intern_c_string ("delete-by-moving-to-trash");
+  DEFSYM (Qdelete_by_moving_to_trash, "delete-by-moving-to-trash");
 
+  /* Lisp function for moving files to trash.  */
   DEFSYM (Qmove_file_to_trash, "move-file-to-trash");
+
+  /* Lisp function for recursively copying directories.  */
   DEFSYM (Qcopy_directory, "copy-directory");
+
+  /* Lisp function for recursively deleting directories.  */
   DEFSYM (Qdelete_directory, "delete-directory");
+
   DEFSYM (Qsubstitute_env_in_file_name, "substitute-env-in-file-name");
+  DEFSYM (Qget_buffer_window_list, "get-buffer-window-list");
 
   defsubr (&Sfind_file_name_handler);
   defsubr (&Sfile_name_directory);
@@ -6068,7 +6087,6 @@ This includes interactive calls to `delete-file' and
   defsubr (&Sdefault_file_modes);
   defsubr (&Sfile_newer_than_file_p);
   defsubr (&Sinsert_file_contents);
-  defsubr (&Schoose_write_coding_system);
   defsubr (&Swrite_region);
   defsubr (&Scar_less_than_car);
   defsubr (&Sverify_visited_file_modtime);