]> code.delx.au - gnu-emacs/blobdiff - src/fileio.c
2002-08-10 Andrew Choi <akochoi@shaw.ca>
[gnu-emacs] / src / fileio.c
index 894189008d52b19895f0e3585a9913387a7ecc7e..f764bff2200cabe0239a973f6efe0fe604ccd432 100644 (file)
@@ -199,6 +199,12 @@ Lisp_Object Vwrite_region_annotations_so_far;
 /* File name in which we write a list of all our auto save files.  */
 Lisp_Object Vauto_save_list_file_name;
 
+/* Function to call to read a file name.  */
+Lisp_Object Vread_file_name_function; 
+
+/* Current predicate used by read_file_name_internal.  */
+Lisp_Object Vread_file_name_predicate;
+
 /* Nonzero means, when reading a filename in the minibuffer,
  start out by inserting the default directory into the minibuffer. */
 int insert_default_directory;
@@ -244,7 +250,7 @@ static int e_write P_ ((int, Lisp_Object, int, int, struct coding_system *));
 \f
 void
 report_file_error (string, data)
-     char *string;
+     const char *string;
      Lisp_Object data;
 {
   Lisp_Object errstring;
@@ -263,8 +269,8 @@ report_file_error (string, data)
       default:
        /* System error messages are capitalized.  Downcase the initial
           unless it is followed by a slash.  */
-       if (XSTRING (errstring)->data[1] != '/')
-         XSTRING (errstring)->data[0] = DOWNCASE (XSTRING (errstring)->data[0]);
+       if (SREF (errstring, 1) != '/')
+         SSET (errstring, 0, DOWNCASE (SREF (errstring, 0)));
 
        Fsignal (Qfile_error,
                 Fcons (build_string (string), Fcons (errstring, data)));
@@ -387,8 +393,8 @@ on VMS, perhaps instead a string ending in `:', `]' or `>'.  */)
      (filename)
      Lisp_Object filename;
 {
-  register unsigned char *beg;
-  register unsigned char *p;
+  register const unsigned char *beg;
+  register const unsigned char *p;
   Lisp_Object handler;
 
   CHECK_STRING (filename);
@@ -402,11 +408,11 @@ on VMS, perhaps instead a string ending in `:', `]' or `>'.  */)
 #ifdef FILE_SYSTEM_CASE
   filename = FILE_SYSTEM_CASE (filename);
 #endif
-  beg = XSTRING (filename)->data;
+  beg = SDATA (filename);
 #ifdef DOS_NT
   beg = strcpy (alloca (strlen (beg) + 1), beg);
 #endif
-  p = beg + STRING_BYTES (XSTRING (filename));
+  p = beg + SBYTES (filename);
 
   while (p != beg && !IS_DIRECTORY_SEP (p[-1])
 #ifdef VMS
@@ -463,7 +469,7 @@ or the entire name if it contains no slash.  */)
      (filename)
      Lisp_Object filename;
 {
-  register unsigned char *beg, *p, *end;
+  register const unsigned char *beg, *p, *end;
   Lisp_Object handler;
 
   CHECK_STRING (filename);
@@ -474,8 +480,8 @@ or the entire name if it contains no slash.  */)
   if (!NILP (handler))
     return call2 (handler, Qfile_name_nondirectory, filename);
 
-  beg = XSTRING (filename)->data;
-  end = p = beg + STRING_BYTES (XSTRING (filename));
+  beg = SDATA (filename);
+  end = p = beg + SBYTES (filename);
 
   while (p != beg && !IS_DIRECTORY_SEP (p[-1])
 #ifdef VMS
@@ -596,7 +602,8 @@ file_name_as_directory (out, in)
   /* For Unix syntax, Append a slash if necessary */
   if (!IS_DIRECTORY_SEP (out[size]))
     {
-      out[size + 1] = DIRECTORY_SEP;
+      /* Cannot use DIRECTORY_SEP, which could have any value */
+      out[size + 1] = '/';
       out[size + 2] = '\0';
     }
 #ifdef DOS_NT
@@ -608,7 +615,7 @@ file_name_as_directory (out, in)
 
 DEFUN ("file-name-as-directory", Ffile_name_as_directory,
        Sfile_name_as_directory, 1, 1, 0,
-       doc: /* Return a string representing file FILENAME interpreted as a directory.
+       doc: /* Return a string representing the file name FILE interpreted as a directory.
 This operation exists because a directory is also a file, but its name as
 a directory is different from its name as a file.
 The result can be used as the value of `default-directory'
@@ -631,8 +638,8 @@ On VMS, converts \"[X]FOO.DIR\" to \"[X.FOO]\", etc.  */)
   if (!NILP (handler))
     return call2 (handler, Qfile_name_as_directory, file);
 
-  buf = (char *) alloca (STRING_BYTES (XSTRING (file)) + 10);
-  return build_string (file_name_as_directory (buf, XSTRING (file)->data));
+  buf = (char *) alloca (SBYTES (file) + 10);
+  return build_string (file_name_as_directory (buf, SDATA (file)));
 }
 \f
 /*
@@ -827,11 +834,11 @@ it returns a file name such as \"[X]Y.DIR.1\".  */)
   /* 20 extra chars is insufficient for VMS, since we might perform a
      logical name translation. an equivalence string can be up to 255
      chars long, so grab that much extra space...  - sss */
-  buf = (char *) alloca (STRING_BYTES (XSTRING (directory)) + 20 + 255);
+  buf = (char *) alloca (SBYTES (directory) + 20 + 255);
 #else
-  buf = (char *) alloca (STRING_BYTES (XSTRING (directory)) + 20);
+  buf = (char *) alloca (SBYTES (directory) + 20);
 #endif
-  directory_file_name (XSTRING (directory)->data, buf);
+  directory_file_name (SDATA (directory), buf);
   return build_string (buf);
 }
 
@@ -906,10 +913,10 @@ make_temp_name (prefix, base64_p)
 #endif
     }
   
-  len = XSTRING (prefix)->size;
+  len = SCHARS (prefix);
   val = make_uninit_string (len + 3 + pidlen);
-  data = XSTRING (val)->data;
-  bcopy(XSTRING (prefix)->data, data, len);
+  data = SDATA (val);
+  bcopy(SDATA (prefix), data, len);
   p = data + len;
 
   bcopy (pidbuf, p, pidlen);
@@ -964,7 +971,7 @@ make_temp_name (prefix, base64_p)
     }
 
   error ("Cannot create temporary name for prefix `%s'",
-        XSTRING (prefix)->data);
+        SDATA (prefix));
   return Qnil;
 }
 
@@ -980,7 +987,11 @@ PREFIX should be an absolute file name.
 
 There is a race condition between calling `make-temp-name' and creating the
 file which opens all kinds of security holes.  For that reason, you should
-probably use `make-temp-file' instead.  */)
+probably use `make-temp-file' instead, except in three circumstances:
+
+* If you are creating the file in the user's home directory.
+* If you are creating a directory rather than an ordinary file.
+* If you are taking special precautions as `make-temp-file' does.  */)
      (prefix)
      Lisp_Object prefix;
 {
@@ -1063,7 +1074,7 @@ See also the function `substitute-in-file-name'.  */)
        return call3 (handler, Qexpand_file_name, name, default_directory);
     }
 
-  o = XSTRING (default_directory)->data;
+  o = SDATA (default_directory);
 
   /* Make sure DEFAULT_DIRECTORY is properly expanded.
      It would be better to do this down below where we actually use
@@ -1108,7 +1119,7 @@ See also the function `substitute-in-file-name'.  */)
   name = FILE_SYSTEM_CASE (name);
 #endif
 
-  nm = XSTRING (name)->data;
+  nm = SDATA (name);
 
 #ifdef DOS_NT
   /* We will force directory separators to be either all \ or /, so make
@@ -1282,21 +1293,21 @@ See also the function `substitute-in-file-name'.  */)
 #ifdef WINDOWSNT
          if (IS_DIRECTORY_SEP (nm[1]))
            {
-             if (strcmp (nm, XSTRING (name)->data) != 0)
+             if (strcmp (nm, SDATA (name)) != 0)
                name = build_string (nm);
            }
          else
 #endif
          /* drive must be set, so this is okay */
-         if (strcmp (nm - 2, XSTRING (name)->data) != 0)
+         if (strcmp (nm - 2, SDATA (name)) != 0)
            {
              name = make_string (nm - 2, p - nm + 2);
-             XSTRING (name)->data[0] = DRIVE_LETTER (drive);
-             XSTRING (name)->data[1] = ':';
+             SSET (name, 0, DRIVE_LETTER (drive));
+             SSET (name, 1, ':');
            }
          return name;
 #else /* not DOS_NT */
-         if (nm == XSTRING (name)->data)
+         if (nm == SDATA (name))
            return name;
          return build_string (nm);
 #endif /* not DOS_NT */
@@ -1409,7 +1420,7 @@ See also the function `substitute-in-file-name'.  */)
 #endif
       && !newdir)
     {
-      newdir = XSTRING (default_directory)->data;
+      newdir = SDATA (default_directory);
 #ifdef DOS_NT
       /* Note if special escape prefix is present, but remove for now.  */
       if (newdir[0] == '/' && newdir[1] == ':')
@@ -1716,7 +1727,7 @@ See also the function `substitute-in-file-name'.")
   name = Fupcase (name);
 #endif
 
-  nm = XSTRING (name)->data;
+  nm = SDATA (name);
 
   /* If nm is absolute, flush ...// and detect /./ and /../.
      If no /./ or /../ we can return right away.  */
@@ -1825,7 +1836,7 @@ See also the function `substitute-in-file-name'.")
          if (index (nm, '/'))
            return build_string (sys_translate_unix (nm));
 #endif /* VMS */
-         if (nm == XSTRING (name)->data)
+         if (nm == SDATA (name))
            return name;
          return build_string (nm);
        }
@@ -1886,7 +1897,7 @@ See also the function `substitute-in-file-name'.")
       if (NILP (defalt))
        defalt = current_buffer->directory;
       CHECK_STRING (defalt);
-      newdir = XSTRING (defalt)->data;
+      newdir = SDATA (defalt);
     }
 
   /* Now concatenate the directory and name to new space in the stack frame */
@@ -2036,13 +2047,13 @@ duplicates what `expand-file-name' does.  */)
   if (!NILP (handler))
     return call2 (handler, Qsubstitute_in_file_name, filename);
 
-  nm = XSTRING (filename)->data;
+  nm = SDATA (filename);
 #ifdef DOS_NT
   nm = strcpy (alloca (strlen (nm) + 1), nm);
   CORRECT_DIR_SEPS (nm);
-  substituted = (strcmp (nm, XSTRING (filename)->data) != 0);
+  substituted = (strcmp (nm, SDATA (filename)) != 0);
 #endif
-  endp = nm + STRING_BYTES (XSTRING (filename));
+  endp = nm + SBYTES (filename);
 
   /* If /~ or // appears, discard everything through first slash.  */
 
@@ -2158,7 +2169,7 @@ duplicates what `expand-file-name' does.  */)
 
   /* If substitution required, recopy the string and do it */
   /* Make space in stack frame for the new copy */
-  xnm = (unsigned char *) alloca (STRING_BYTES (XSTRING (filename)) + total + 1);
+  xnm = (unsigned char *) alloca (SBYTES (filename) + total + 1);
   x = xnm;
 
   /* Copy the rest of the name through, replacing $ constructs with values */
@@ -2269,16 +2280,16 @@ expand_and_dir_to_file (filename, defdir)
   absname = Fexpand_file_name (filename, defdir);
 #ifdef VMS
   {
-    register int c = XSTRING (absname)->data[STRING_BYTES (XSTRING (absname)) - 1];
+    register int c = SREF (absname, SBYTES (absname) - 1);
     if (c == ':' || c == ']' || c == '>')
       absname = Fdirectory_file_name (absname);
   }
 #else
   /* Remove final slash, if any (unless this is the root dir).
      stat behaves differently depending!  */
-  if (XSTRING (absname)->size > 1
-      && IS_DIRECTORY_SEP (XSTRING (absname)->data[STRING_BYTES (XSTRING (absname)) - 1])
-      && !IS_DEVICE_SEP (XSTRING (absname)->data[STRING_BYTES (XSTRING (absname))-2]))
+  if (SCHARS (absname) > 1
+      && IS_DIRECTORY_SEP (SREF (absname, SBYTES (absname) - 1))
+      && !IS_DEVICE_SEP (SREF (absname, SBYTES (absname)-2)))
     /* We cannot take shortcuts; they might be wrong for magic file names.  */
     absname = Fdirectory_file_name (absname);
 #endif
@@ -2313,7 +2324,7 @@ barf_or_query_if_file_exists (absname, querystring, interactive, statptr, quick)
 
   /* stat is a good way to tell whether the file exists,
      regardless of what access permissions it has.  */
-  if (stat (XSTRING (encoded_filename)->data, &statbuf) >= 0)
+  if (stat (SDATA (encoded_filename), &statbuf) >= 0)
     {
       if (! interactive)
        Fsignal (Qfile_already_exists,
@@ -2321,7 +2332,7 @@ barf_or_query_if_file_exists (absname, querystring, interactive, statptr, quick)
                        Fcons (absname, Qnil)));
       GCPRO1 (absname);
       tem = format1 ("File %s already exists; %s anyway? ",
-                    XSTRING (absname)->data, querystring);
+                    SDATA (absname), querystring);
       if (quick)
        tem = Fy_or_n_p (tem);
       else
@@ -2361,7 +2372,7 @@ A prefix arg makes KEEP-TIME non-nil.  */)
   struct stat st, out_st;
   Lisp_Object handler;
   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
-  int count = specpdl_ptr - specpdl;
+  int count = SPECPDL_INDEX ();
   int input_file_statable_p;
   Lisp_Object encoded_file, encoded_newname;
 
@@ -2394,26 +2405,39 @@ A prefix arg makes KEEP-TIME non-nil.  */)
       || INTEGERP (ok_if_already_exists))
     barf_or_query_if_file_exists (encoded_newname, "copy to it",
                                  INTEGERP (ok_if_already_exists), &out_st, 0);
-  else if (stat (XSTRING (encoded_newname)->data, &out_st) < 0)
+  else if (stat (SDATA (encoded_newname), &out_st) < 0)
     out_st.st_mode = 0;
 
 #ifdef WINDOWSNT
-  if (!CopyFile (XSTRING (encoded_file)->data,
-                XSTRING (encoded_newname)->data
+  if (!CopyFile (SDATA (encoded_file),
+                SDATA (encoded_newname)
                 FALSE))
     report_file_error ("Copying file", Fcons (file, Fcons (newname, Qnil)));
   else if (NILP (keep_time))
     {
       EMACS_TIME now;
+      DWORD attributes;
+      char * filename;
+
       EMACS_GET_TIME (now);
-      if (set_file_times (XSTRING (encoded_newname)->data,
-                         now, now))
-       Fsignal (Qfile_date_error,
-                Fcons (build_string ("Cannot set file date"),
-                       Fcons (newname, Qnil)));
+      filename = SDATA (encoded_newname);
+
+      /* Ensure file is writable while its modified time is set.  */
+      attributes = GetFileAttributes (filename);
+      SetFileAttributes (filename, attributes & ~FILE_ATTRIBUTE_READONLY);
+      if (set_file_times (filename, now, now))
+       {
+         /* Restore original attributes.  */
+         SetFileAttributes (filename, attributes);
+         Fsignal (Qfile_date_error,
+                  Fcons (build_string ("Cannot set file date"),
+                         Fcons (newname, Qnil)));
+       }
+      /* Restore original attributes.  */
+      SetFileAttributes (filename, attributes);
     }
 #else /* not WINDOWSNT */
-  ifd = emacs_open (XSTRING (encoded_file)->data, O_RDONLY, 0);
+  ifd = emacs_open (SDATA (encoded_file), O_RDONLY, 0);
   if (ifd < 0)
     report_file_error ("Opening input file", Fcons (file, Qnil));
 
@@ -2449,13 +2473,13 @@ A prefix arg makes KEEP-TIME non-nil.  */)
 
 #ifdef VMS
   /* Create the copy file with the same record format as the input file */
-  ofd = sys_creat (XSTRING (encoded_newname)->data, 0666, ifd);
+  ofd = sys_creat (SDATA (encoded_newname), 0666, ifd);
 #else
 #ifdef MSDOS
   /* System's default file type was set to binary by _fmode in emacs.c.  */
-  ofd = creat (XSTRING (encoded_newname)->data, S_IREAD | S_IWRITE);
+  ofd = creat (SDATA (encoded_newname), S_IREAD | S_IWRITE);
 #else /* not MSDOS */
-  ofd = creat (XSTRING (encoded_newname)->data, 0666);
+  ofd = creat (SDATA (encoded_newname), 0666);
 #endif /* not MSDOS */
 #endif /* VMS */
   if (ofd < 0)
@@ -2481,14 +2505,14 @@ A prefix arg makes KEEP-TIME non-nil.  */)
          EMACS_TIME atime, mtime;
          EMACS_SET_SECS_USECS (atime, st.st_atime, 0);
          EMACS_SET_SECS_USECS (mtime, st.st_mtime, 0);
-         if (set_file_times (XSTRING (encoded_newname)->data,
+         if (set_file_times (SDATA (encoded_newname),
                              atime, mtime))
            Fsignal (Qfile_date_error,
                     Fcons (build_string ("Cannot set file date"),
                            Fcons (newname, Qnil)));
        }
 #ifndef MSDOS
-      chmod (XSTRING (encoded_newname)->data, st.st_mode & 07777);
+      chmod (SDATA (encoded_newname), st.st_mode & 07777);
 #else /* MSDOS */
 #if defined (__DJGPP__) && __DJGPP__ > 1
       /* In DJGPP v2.0 and later, fstat usually returns true file mode bits,
@@ -2496,7 +2520,7 @@ A prefix arg makes KEEP-TIME non-nil.  */)
          get only the READ bit, which will make the copied file read-only,
          so it's better not to chmod at all.  */
       if ((_djstat_flags & _STFAIL_WRITEBIT) == 0)
-       chmod (XSTRING (encoded_newname)->data, st.st_mode & 07777);
+       chmod (SDATA (encoded_newname), st.st_mode & 07777);
 #endif /* DJGPP version 2 or newer */
 #endif /* MSDOS */
     }
@@ -2517,7 +2541,7 @@ DEFUN ("make-directory-internal", Fmake_directory_internal,
      (directory)
      Lisp_Object directory;
 {
-  unsigned char *dir;
+  const unsigned char *dir;
   Lisp_Object handler;
   Lisp_Object encoded_dir;
 
@@ -2530,7 +2554,7 @@ DEFUN ("make-directory-internal", Fmake_directory_internal,
 
   encoded_dir = ENCODE_FILE (directory);
 
-  dir = XSTRING (encoded_dir)->data;
+  dir = SDATA (encoded_dir);
 
 #ifdef WINDOWSNT
   if (mkdir (dir) != 0)
@@ -2547,7 +2571,7 @@ DEFUN ("delete-directory", Fdelete_directory, Sdelete_directory, 1, 1, "FDelete
      (directory)
      Lisp_Object directory;
 {
-  unsigned char *dir;
+  const unsigned char *dir;
   Lisp_Object handler;
   Lisp_Object encoded_dir;
 
@@ -2560,7 +2584,7 @@ DEFUN ("delete-directory", Fdelete_directory, Sdelete_directory, 1, 1, "FDelete
 
   encoded_dir = ENCODE_FILE (directory);
 
-  dir = XSTRING (encoded_dir)->data;
+  dir = SDATA (encoded_dir);
 
   if (rmdir (dir) != 0)
     report_file_error ("Removing directory", Flist (1, &directory));
@@ -2586,7 +2610,7 @@ If file has multiple names, it continues to exist with the other names.  */)
 
   encoded_file = ENCODE_FILE (filename);
 
-  if (0 > unlink (XSTRING (encoded_file)->data))
+  if (0 > unlink (SDATA (encoded_file)))
     report_file_error ("Removing old name", Flist (1, &filename));
   return Qnil;
 }
@@ -2656,10 +2680,10 @@ This is what happens in interactive use with M-x.  */)
     barf_or_query_if_file_exists (encoded_newname, "rename to it",
                                  INTEGERP (ok_if_already_exists), 0, 0);
 #ifndef BSD4_1
-  if (0 > rename (XSTRING (encoded_file)->data, XSTRING (encoded_newname)->data))
+  if (0 > rename (SDATA (encoded_file), SDATA (encoded_newname)))
 #else
-  if (0 > link (XSTRING (encoded_file)->data, XSTRING (encoded_newname)->data)
-      || 0 > unlink (XSTRING (encoded_file)->data))
+  if (0 > link (SDATA (encoded_file), SDATA (encoded_newname))
+      || 0 > unlink (SDATA (encoded_file)))
 #endif
     {
       if (errno == EXDEV)
@@ -2731,8 +2755,8 @@ This is what happens in interactive use with M-x.  */)
     barf_or_query_if_file_exists (encoded_newname, "make it a new name",
                                  INTEGERP (ok_if_already_exists), 0, 0);
 
-  unlink (XSTRING (newname)->data);
-  if (0 > link (XSTRING (encoded_file)->data, XSTRING (encoded_newname)->data))
+  unlink (SDATA (newname));
+  if (0 > link (SDATA (encoded_file), SDATA (encoded_newname)))
     {
 #ifdef NO_ARG_ARRAY
       args[0] = file;
@@ -2772,7 +2796,7 @@ This happens for interactive use with M-x.  */)
   /* If the link target has a ~, we must expand it to get
      a truly valid file name.  Otherwise, do not expand;
      we want to permit links to relative file names.  */
-  if (XSTRING (filename)->data[0] == '~')
+  if (SREF (filename, 0) == '~')
     filename = Fexpand_file_name (filename, Qnil);
   linkname = Fexpand_file_name (linkname, Qnil);
 
@@ -2797,15 +2821,15 @@ This happens for interactive use with M-x.  */)
       || INTEGERP (ok_if_already_exists))
     barf_or_query_if_file_exists (encoded_linkname, "make it a link",
                                  INTEGERP (ok_if_already_exists), 0, 0);
-  if (0 > symlink (XSTRING (encoded_filename)->data,
-                  XSTRING (encoded_linkname)->data))
+  if (0 > symlink (SDATA (encoded_filename),
+                  SDATA (encoded_linkname)))
     {
       /* If we didn't complain already, silently delete existing file.  */
       if (errno == EEXIST)
        {
-         unlink (XSTRING (encoded_linkname)->data);
-         if (0 <= symlink (XSTRING (encoded_filename)->data,
-                           XSTRING (encoded_linkname)->data))
+         unlink (SDATA (encoded_linkname));
+         if (0 <= symlink (SDATA (encoded_filename),
+                           SDATA (encoded_linkname)))
            {
              UNGCPRO;
              return Qnil;
@@ -2837,15 +2861,15 @@ If STRING is nil or a null string, the logical name NAME is deleted.  */)
 {
   CHECK_STRING (name);
   if (NILP (string))
-    delete_logical_name (XSTRING (name)->data);
+    delete_logical_name (SDATA (name));
   else
     {
       CHECK_STRING (string);
 
-      if (XSTRING (string)->size == 0)
-       delete_logical_name (XSTRING (name)->data);
+      if (SCHARS (string) == 0)
+       delete_logical_name (SDATA (name));
       else
-       define_logical_name (XSTRING (name)->data, XSTRING (string)->data);
+       define_logical_name (SDATA (name), SDATA (string));
     }
 
   return string;
@@ -2864,7 +2888,7 @@ DEFUN ("sysnetunam", Fsysnetunam, Ssysnetunam, 2, 2, 0,
   CHECK_STRING (path);
   CHECK_STRING (login);
 
-  netresult = netunam (XSTRING (path)->data, XSTRING (login)->data);
+  netresult = netunam (SDATA (path), SDATA (login));
 
   if (netresult == -1)
     return Qnil;
@@ -2880,10 +2904,10 @@ On Unix, this is a name starting with a `/' or a `~'.  */)
      (filename)
      Lisp_Object filename;
 {
-  unsigned char *ptr;
+  const unsigned char *ptr;
 
   CHECK_STRING (filename);
-  ptr = XSTRING (filename)->data;
+  ptr = SDATA (filename);
   if (IS_DIRECTORY_SEP (*ptr) || *ptr == '~'
 #ifdef VMS
 /* ??? This criterion is probably wrong for '<'.  */
@@ -2980,7 +3004,7 @@ See also `file-readable-p' and `file-attributes'.  */)
 
   absname = ENCODE_FILE (absname);
 
-  return (stat (XSTRING (absname)->data, &statbuf) >= 0) ? Qt : Qnil;
+  return (stat (SDATA (absname), &statbuf) >= 0) ? Qt : Qnil;
 }
 
 DEFUN ("file-executable-p", Ffile_executable_p, Sfile_executable_p, 1, 1, 0,
@@ -3003,7 +3027,7 @@ For a directory, this means you can access files in that directory.  */)
 
   absname = ENCODE_FILE (absname);
 
-  return (check_executable (XSTRING (absname)->data) ? Qt : Qnil);
+  return (check_executable (SDATA (absname)) ? Qt : Qnil);
 }
 
 DEFUN ("file-readable-p", Ffile_readable_p, Sfile_readable_p, 1, 1, 0,
@@ -3032,7 +3056,7 @@ See also `file-exists-p' and `file-attributes'.  */)
 #if defined(DOS_NT) || defined(macintosh)
   /* Under MS-DOS, Windows, and Macintosh, open does not work for
      directories.  */
-  if (access (XSTRING (absname)->data, 0) == 0)
+  if (access (SDATA (absname), 0) == 0)
     return Qt;
   return Qnil;
 #else /* not DOS_NT and not macintosh */
@@ -3041,13 +3065,13 @@ See also `file-exists-p' and `file-attributes'.  */)
   /* Opening a fifo without O_NONBLOCK can wait.
      We don't want to wait.  But we don't want to mess wth O_NONBLOCK
      except in the case of a fifo, on a system which handles it.  */
-  desc = stat (XSTRING (absname)->data, &statbuf);
+  desc = stat (SDATA (absname), &statbuf);
   if (desc < 0)
     return Qnil;
   if (S_ISFIFO (statbuf.st_mode))
     flags |= O_NONBLOCK;
 #endif
-  desc = emacs_open (XSTRING (absname)->data, flags, 0);
+  desc = emacs_open (SDATA (absname), flags, 0);
   if (desc < 0)
     return Qnil;
   emacs_close (desc);
@@ -3076,8 +3100,8 @@ DEFUN ("file-writable-p", Ffile_writable_p, Sfile_writable_p, 1, 1, 0,
     return call2 (handler, Qfile_writable_p, absname);
 
   encoded = ENCODE_FILE (absname);
-  if (stat (XSTRING (encoded)->data, &statbuf) >= 0)
-    return (check_writable (XSTRING (encoded)->data)
+  if (stat (SDATA (encoded), &statbuf) >= 0)
+    return (check_writable (SDATA (encoded))
            ? Qt : Qnil);
 
   dir = Ffile_name_directory (absname);
@@ -3095,11 +3119,11 @@ DEFUN ("file-writable-p", Ffile_writable_p, Sfile_writable_p, 1, 1, 0,
   /* The read-only attribute of the parent directory doesn't affect
      whether a file or directory can be created within it.  Some day we
      should check ACLs though, which do affect this.  */
-  if (stat (XSTRING (dir)->data, &statbuf) < 0)
+  if (stat (SDATA (dir), &statbuf) < 0)
     return Qnil;
   return (statbuf.st_mode & S_IFMT) == S_IFDIR ? Qt : Qnil;
 #else
-  return (check_writable (!NILP (dir) ? (char *) XSTRING (dir)->data : "")
+  return (check_writable (!NILP (dir) ? (char *) SDATA (dir) : "")
          ? Qt : Qnil);
 #endif
 }
@@ -3127,9 +3151,9 @@ If there is no error, we return nil.  */)
 
   encoded_filename = ENCODE_FILE (absname);
 
-  fd = emacs_open (XSTRING (encoded_filename)->data, O_RDONLY, 0);
+  fd = emacs_open (SDATA (encoded_filename), O_RDONLY, 0);
   if (fd < 0)
-    report_file_error (XSTRING (string)->data, Fcons (filename, Qnil));
+    report_file_error (SDATA (string), Fcons (filename, Qnil));
   emacs_close (fd);
 
   return Qnil;
@@ -3169,7 +3193,7 @@ Otherwise returns nil.  */)
       bzero (buf, bufsize);
       
       errno = 0;
-      valsize = readlink (XSTRING (filename)->data, buf, bufsize);
+      valsize = readlink (SDATA (filename), buf, bufsize);
       if (valsize == -1)
        {
 #ifdef ERANGE
@@ -3218,7 +3242,7 @@ See `file-symlink-p' to distinguish symlinks.  */)
 
   absname = ENCODE_FILE (absname);
 
-  if (stat (XSTRING (absname)->data, &st) < 0)
+  if (stat (SDATA (absname), &st) < 0)
     return Qnil;
   return (st.st_mode & S_IFMT) == S_IFDIR ? Qt : Qnil;
 }
@@ -3284,7 +3308,7 @@ This is the sort of file that holds an ordinary stream of data bytes.  */)
 
     /* Tell stat to use expensive method to get accurate info.  */
     Vw32_get_true_file_attributes = Qt;
-    result = stat (XSTRING (absname)->data, &st);
+    result = stat (SDATA (absname), &st);
     Vw32_get_true_file_attributes = tem;
 
     if (result < 0)
@@ -3292,7 +3316,7 @@ This is the sort of file that holds an ordinary stream of data bytes.  */)
     return (st.st_mode & S_IFMT) == S_IFREG ? Qt : Qnil;
   }
 #else
-  if (stat (XSTRING (absname)->data, &st) < 0)
+  if (stat (SDATA (absname), &st) < 0)
     return Qnil;
   return (st.st_mode & S_IFMT) == S_IFREG ? Qt : Qnil;
 #endif
@@ -3317,10 +3341,10 @@ DEFUN ("file-modes", Ffile_modes, Sfile_modes, 1, 1, 0,
 
   absname = ENCODE_FILE (absname);
 
-  if (stat (XSTRING (absname)->data, &st) < 0)
+  if (stat (SDATA (absname), &st) < 0)
     return Qnil;
 #if defined (MSDOS) && __DJGPP__ < 2
-  if (check_executable (XSTRING (absname)->data))
+  if (check_executable (SDATA (absname)))
     st.st_mode |= S_IEXEC;
 #endif /* MSDOS && __DJGPP__ < 2 */
 
@@ -3347,7 +3371,7 @@ Only the 12 low bits of MODE are used.  */)
 
   encoded_absname = ENCODE_FILE (absname);
 
-  if (chmod (XSTRING (encoded_absname)->data, XINT (mode)) < 0)
+  if (chmod (SDATA (encoded_absname), XINT (mode)) < 0)
     report_file_error ("Doing chmod", Fcons (absname, Qnil));
 
   return Qnil;
@@ -3433,12 +3457,12 @@ otherwise, if FILE2 does not exist, the answer is t.  */)
   absname2 = ENCODE_FILE (absname2);
   UNGCPRO;
 
-  if (stat (XSTRING (absname1)->data, &st) < 0)
+  if (stat (SDATA (absname1), &st) < 0)
     return Qnil;
 
   mtime1 = st.st_mtime;
 
-  if (stat (XSTRING (absname2)->data, &st) < 0)
+  if (stat (SDATA (absname2), &st) < 0)
     return Qt;
 
   return (mtime1 > st.st_mtime) ? Qt : Qnil;
@@ -3515,7 +3539,7 @@ read_non_regular ()
   immediate_quit = 1;
   QUIT;
   nbytes = emacs_read (non_regular_fd,
-                      BEG_ADDR + PT_BYTE - 1 + non_regular_inserted,
+                      BEG_ADDR + PT_BYTE - BEG_BYTE + non_regular_inserted,
                       non_regular_nbytes);
   immediate_quit = 0;
   return make_number (nbytes);
@@ -3565,7 +3589,7 @@ actually used.  */)
   int inserted = 0;
   register int how_much;
   register int unprocessed;
-  int count = BINDING_STACK_SIZE ();
+  int count = SPECPDL_INDEX ();
   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
   Lisp_Object handler, val, insval, orig_filename;
   Lisp_Object p;
@@ -3617,15 +3641,15 @@ actually used.  */)
 
     /* Tell stat to use expensive method to get accurate info.  */
     Vw32_get_true_file_attributes = Qt;
-    total = stat (XSTRING (filename)->data, &st);
+    total = stat (SDATA (filename), &st);
     Vw32_get_true_file_attributes = tem;
   }
   if (total < 0)
 #else
 #ifndef APOLLO
-  if (stat (XSTRING (filename)->data, &st) < 0)
+  if (stat (SDATA (filename), &st) < 0)
 #else
-  if ((fd = emacs_open (XSTRING (filename)->data, O_RDONLY, 0)) < 0
+  if ((fd = emacs_open (SDATA (filename), O_RDONLY, 0)) < 0
       || fstat (fd, &st) < 0)
 #endif /* not APOLLO */
 #endif /* WINDOWSNT */
@@ -3660,7 +3684,7 @@ actually used.  */)
 #endif
 
   if (fd < 0)
-    if ((fd = emacs_open (XSTRING (filename)->data, O_RDONLY, 0)) < 0)
+    if ((fd = emacs_open (SDATA (filename), O_RDONLY, 0)) < 0)
       goto badopen;
 
   /* Replacement should preserve point as it preserves markers.  */
@@ -3756,29 +3780,34 @@ actually used.  */)
 
              if (nread < 0)
                error ("IO error reading %s: %s",
-                      XSTRING (orig_filename)->data, emacs_strerror (errno));
+                      SDATA (orig_filename), emacs_strerror (errno));
              else if (nread > 0)
                {
                  struct buffer *prev = current_buffer;
-                 int count1;
+                 Lisp_Object buffer;
+                 struct buffer *buf;
 
                  record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
 
-                 /* The call to temp_output_buffer_setup binds
-                    standard-output.  */
-                 count1 = specpdl_ptr - specpdl;
-                 temp_output_buffer_setup (" *code-converting-work*");
+                 buffer = Fget_buffer_create (build_string (" *code-converting-work*"));
+                 buf = XBUFFER (buffer);
+
+                 buf->directory = current_buffer->directory;
+                 buf->read_only = Qnil;
+                 buf->filename = Qnil;
+                 buf->undo_list = Qt;
+                 buf->overlays_before = Qnil;
+                 buf->overlays_after = Qnil;
                  
-                 set_buffer_internal (XBUFFER (Vstandard_output));
-                 current_buffer->enable_multibyte_characters = Qnil;
+                 set_buffer_internal (buf);
+                 Ferase_buffer ();
+                 buf->enable_multibyte_characters = Qnil;
+
                  insert_1_both (read_buf, nread, nread, 0, 0, 0);
                  TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
                  val = call2 (Vset_auto_coding_function,
                               filename, make_number (nread));
                  set_buffer_internal (prev);
-
-                 /* Remove the binding for standard-output.  */
-                 unbind_to (count1, Qnil);
                  
                  /* Discard the unwind protect for recovering the
                      current buffer.  */
@@ -3868,7 +3897,7 @@ actually used.  */)
          nread = emacs_read (fd, buffer, sizeof buffer);
          if (nread < 0)
            error ("IO error reading %s: %s",
-                  XSTRING (orig_filename)->data, emacs_strerror (errno));
+                  SDATA (orig_filename), emacs_strerror (errno));
          else if (nread == 0)
            break;
 
@@ -3939,7 +3968,7 @@ actually used.  */)
              nread = emacs_read (fd, buffer + total_read, trial - total_read);
              if (nread < 0)
                error ("IO error reading %s: %s",
-                      XSTRING (orig_filename)->data, emacs_strerror (errno));
+                      SDATA (orig_filename), emacs_strerror (errno));
              else if (nread == 0)
                break;
              total_read += nread;
@@ -4122,7 +4151,7 @@ actually used.  */)
 
          if (how_much == -1)
            error ("IO error reading %s: %s",
-                  XSTRING (orig_filename)->data, emacs_strerror (errno));
+                  SDATA (orig_filename), emacs_strerror (errno));
          else if (how_much == -2)
            error ("maximum buffer size exceeded");
        }
@@ -4299,7 +4328,7 @@ actually used.  */)
               here doesn't do any harm.  */
            immediate_quit = 1;
            QUIT;
-           this = emacs_read (fd, BEG_ADDR + PT_BYTE - 1 + inserted, trytry);
+           this = emacs_read (fd, BEG_ADDR + PT_BYTE - BEG_BYTE + inserted, trytry);
            immediate_quit = 0;
          }
       
@@ -4342,7 +4371,7 @@ actually used.  */)
 
   if (how_much < 0)
     error ("IO error reading %s: %s",
-          XSTRING (orig_filename)->data, emacs_strerror (errno));
+          SDATA (orig_filename), emacs_strerror (errno));
 
  notfound:
 
@@ -4367,7 +4396,7 @@ actually used.  */)
             this way, we can run Lisp program safely before decoding
             the inserted text.  */
          Lisp_Object unwind_data;
-             int count = specpdl_ptr - specpdl;
+             int count = SPECPDL_INDEX ();
 
          unwind_data = Fcons (current_buffer->enable_multibyte_characters,
                               Fcons (current_buffer->undo_list,
@@ -4465,7 +4494,7 @@ actually used.  */)
       if (!EQ (current_buffer->undo_list, Qt))
        current_buffer->undo_list = Qnil;
 #ifdef APOLLO
-      stat (XSTRING (filename)->data, &st);
+      stat (SDATA (filename), &st);
 #endif
 
       if (NILP (handler))
@@ -4528,16 +4557,16 @@ actually used.  */)
     }
 
   p = Vafter_insert_file_functions;
-  while (!NILP (p))
+  while (CONSP (p))
     {
-      insval = call1 (Fcar (p), make_number (inserted));
+      insval = call1 (XCAR (p), make_number (inserted));
       if (!NILP (insval))
        {
          CHECK_NUMBER (insval);
          inserted = XFASTINT (insval);
        }
       QUIT;
-      p = Fcdr (p);
+      p = XCDR (p);
     }
 
   if (!NILP (visit)
@@ -4646,7 +4675,8 @@ choose_write_coding_system (start, end, filename,
       if (!force_raw_text
          && !NILP (Ffboundp (Vselect_safe_coding_system_function)))
        /* Confirm that VAL can surely encode the current region.  */
-       val = call3 (Vselect_safe_coding_system_function, start, end, val);
+       val = call5 (Vselect_safe_coding_system_function,
+                    start, end, val, Qnil, filename);
 
       setup_coding_system (Fcheck_coding_system (val), coding);
       if (coding->eol_type == CODING_EOL_UNDECIDED
@@ -4701,7 +4731,7 @@ 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 print the \"Wrote file\" message.
+  that means 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
@@ -4721,10 +4751,10 @@ This does code conversion according to the value of
   register int desc;
   int failure;
   int save_errno = 0;
-  unsigned char *fn;
+  const unsigned char *fn;
   struct stat st;
   int tem;
-  int count = specpdl_ptr - specpdl;
+  int count = SPECPDL_INDEX ();
   int count1;
 #ifdef VMS
   unsigned char *fname = 0;     /* If non-0, original filename (must rename) */
@@ -4796,14 +4826,19 @@ This does code conversion according to the value of
     }
 
   record_unwind_protect (build_annotations_unwind, Fcurrent_buffer ());
-  count1 = specpdl_ptr - specpdl;
+  count1 = SPECPDL_INDEX ();
 
   given_buffer = current_buffer;
-  annotations = build_annotations (start, end);
-  if (current_buffer != given_buffer)
+
+  if (!STRINGP (start))
     {
-      XSETFASTINT (start, BEGV);
-      XSETFASTINT (end, ZV);
+      annotations = build_annotations (start, end);
+
+      if (current_buffer != given_buffer)
+       {
+         XSETFASTINT (start, BEGV);
+         XSETFASTINT (end, ZV);
+       }
     }
 
   UNGCPRO;
@@ -4819,12 +4854,15 @@ This does code conversion according to the value of
   Vlast_coding_system_used = coding.symbol;
 
   given_buffer = current_buffer;
-  annotations = build_annotations_2 (start, end,
-                                    coding.pre_write_conversion, annotations);
-  if (current_buffer != given_buffer)
+  if (! STRINGP (start))
     {
-      XSETFASTINT (start, BEGV);
-      XSETFASTINT (end, ZV);
+      annotations = build_annotations_2 (start, end,
+                                        coding.pre_write_conversion, annotations);
+      if (current_buffer != given_buffer)
+       {
+         XSETFASTINT (start, BEGV);
+         XSETFASTINT (end, ZV);
+       }
     }
 
 #ifdef CLASH_DETECTION
@@ -4843,7 +4881,7 @@ This does code conversion according to the value of
 
   encoded_filename = ENCODE_FILE (filename);
 
-  fn = XSTRING (encoded_filename)->data;
+  fn = SDATA (encoded_filename);
   desc = -1;
   if (!NILP (append))
 #ifdef DOS_NT
@@ -4860,7 +4898,7 @@ This does code conversion according to the value of
        desc = emacs_open (fn, O_RDWR, 0);
        if (desc < 0)
          desc = creat_copy_attrs (STRINGP (current_buffer->filename)
-                                  ? XSTRING (current_buffer->filename)->data : 0,
+                                  ? SDATA (current_buffer->filename) : 0,
                                   fn);
       }
     else                /* Write to temporary name and rename if no errors */
@@ -4872,8 +4910,8 @@ This does code conversion according to the value of
          {
            temp_name = Fmake_temp_name (concat2 (temp_name,
                                                  build_string ("$$SAVE$$")));
-           fname = XSTRING (filename)->data;
-           fn = XSTRING (temp_name)->data;
+           fname = SDATA (filename);
+           fn = SDATA (temp_name);
            desc = creat_copy_attrs (fname, fn);
            if (desc < 0)
              {
@@ -4982,7 +5020,7 @@ This does code conversion according to the value of
 
   if (STRINGP (start))
     {
-      failure = 0 > a_write (desc, start, 0, XSTRING (start)->size,
+      failure = 0 > a_write (desc, start, 0, SCHARS (start),
                             &annotations, &coding);
       save_errno = errno;
     }
@@ -5092,7 +5130,7 @@ This does code conversion according to the value of
     current_buffer->modtime = st.st_mtime;
 
   if (failure)
-    error ("IO error writing %s: %s", XSTRING (filename)->data,
+    error ("IO error writing %s: %s", SDATA (filename),
           emacs_strerror (save_errno));
 
   if (visiting)
@@ -5144,11 +5182,11 @@ build_annotations (start, end)
   annotations = Qnil;
   p = Vwrite_region_annotate_functions;
   GCPRO2 (annotations, p);
-  while (!NILP (p))
+  while (CONSP (p))
     {
       struct buffer *given_buffer = current_buffer;
       Vwrite_region_annotations_so_far = annotations;
-      res = call2 (Fcar (p), start, end);
+      res = call2 (XCAR (p), start, end);
       /* If the function makes a different buffer current,
         assume that means this buffer contains altered text to be output.
         Reset START and END from the buffer bounds
@@ -5162,7 +5200,7 @@ build_annotations (start, end)
        }
       Flength (res);   /* Check basic validity of return value */
       annotations = merge (annotations, res, Qcar_less_than_car);
-      p = Fcdr (p);
+      p = XCDR (p);
     }
 
   /* Now do the same for annotation functions implied by the file-format */
@@ -5170,7 +5208,7 @@ build_annotations (start, end)
     p = Vauto_save_file_format;
   else
     p = current_buffer->file_format;
-  for (i = 0; !NILP (p); p = Fcdr (p), ++i)
+  for (i = 0; CONSP (p); p = XCDR (p), ++i)
     {
       struct buffer *given_buffer = current_buffer;
       
@@ -5179,7 +5217,7 @@ build_annotations (start, end)
       /* Value is either a list of annotations or nil if the function
          has written annotations to a temporary buffer, which is now
          current.  */
-      res = call5 (Qformat_annotate_function, Fcar (p), start, end,
+      res = call5 (Qformat_annotate_function, XCAR (p), start, end,
                   original_buffer, make_number (i));
       if (current_buffer != given_buffer)
        {
@@ -5267,7 +5305,7 @@ a_write (desc, string, pos, nchars, annot, coding)
       tem = Fcdr (Fcar (*annot));
       if (STRINGP (tem))
        {
-         if (0 > e_write (desc, tem, 0, XSTRING (tem)->size, coding))
+         if (0 > e_write (desc, tem, 0, SCHARS (tem), coding))
            return -1;
        }
       *annot = Fcdr (*annot);
@@ -5303,8 +5341,8 @@ e_write (desc, string, start, end, coding)
 
   if (STRINGP (string))
     {
-      addr = XSTRING (string)->data;
-      nbytes = STRING_BYTES (XSTRING (string));
+      addr = SDATA (string);
+      nbytes = SBYTES (string);
       coding->src_multibyte = STRING_MULTIBYTE (string);
     }
   else if (start < end)
@@ -5392,7 +5430,7 @@ This means that the file has not been changed since it was visited or saved.  */
 
   filename = ENCODE_FILE (b->filename);
 
-  if (stat (XSTRING (filename)->data, &st) < 0)
+  if (stat (SDATA (filename), &st) < 0)
     {
       /* If the file doesn't exist now and didn't exist before,
         we say that it isn't modified, provided the error is a tame one.  */
@@ -5460,7 +5498,7 @@ An argument specifies the modification time value to use
 
       filename = ENCODE_FILE (filename);
 
-      if (stat (XSTRING (filename)->data, &st) >= 0)
+      if (stat (SDATA (filename), &st) >= 0)
        current_buffer->modtime = st.st_mtime;
     }
 
@@ -5482,14 +5520,14 @@ auto_save_error (error)
   args[2] = Ferror_message_string (error);
   msg = Fformat (3, args);
   GCPRO1 (msg);
-  nbytes = STRING_BYTES (XSTRING (msg));
+  nbytes = SBYTES (msg);
 
   for (i = 0; i < 3; ++i)
     {
       if (i == 0)
-       message2 (XSTRING (msg)->data, nbytes, STRING_MULTIBYTE (msg));
+       message2 (SDATA (msg), nbytes, STRING_MULTIBYTE (msg));
       else
-       message2_nolog (XSTRING (msg)->data, nbytes, STRING_MULTIBYTE (msg));
+       message2_nolog (SDATA (msg), nbytes, STRING_MULTIBYTE (msg));
       Fsleep_for (make_number (1), Qnil);
     }
 
@@ -5504,7 +5542,7 @@ auto_save_1 ()
 
   /* Get visited file's mode to become the auto save file's mode.  */
   if (! NILP (current_buffer->filename)
-      && stat (XSTRING (current_buffer->filename)->data, &st) >= 0)
+      && stat (SDATA (current_buffer->filename), &st) >= 0)
     /* But make sure we can overwrite it later!  */
     auto_save_mode_bits = st.st_mode | 0600;
   else
@@ -5557,7 +5595,7 @@ A non-nil CURRENT-ONLY argument means save only current buffer.  */)
   Lisp_Object oquit;
   FILE *stream;
   Lisp_Object lispstream;
-  int count = specpdl_ptr - specpdl;
+  int count = SPECPDL_INDEX ();
   int orig_minibuffer_auto_raise = minibuffer_auto_raise;
   int message_p = 0;
 
@@ -5598,7 +5636,7 @@ A non-nil CURRENT-ONLY argument means save only current buffer.  */)
            call2 (Qmake_directory, dir, Qt);
        }
       
-      stream = fopen (XSTRING (listfile)->data, "w");
+      stream = fopen (SDATA (listfile), "w");
       if (stream != NULL)
        {
          /* Arrange to close that file whether or not we get an error.
@@ -5641,12 +5679,12 @@ A non-nil CURRENT-ONLY argument means save only current buffer.  */)
          {
            if (!NILP (b->filename))
              {
-               fwrite (XSTRING (b->filename)->data, 1,
-                       STRING_BYTES (XSTRING (b->filename)), stream);
+               fwrite (SDATA (b->filename), 1,
+                       SBYTES (b->filename), stream);
              }
            putc ('\n', stream);
-           fwrite (XSTRING (b->auto_save_file_name)->data, 1,
-                   STRING_BYTES (XSTRING (b->auto_save_file_name)), stream);
+           fwrite (SDATA (b->auto_save_file_name), 1,
+                   SBYTES (b->auto_save_file_name), stream);
            putc ('\n', stream);
          }
 
@@ -5776,21 +5814,22 @@ static Lisp_Object
 double_dollars (val)
      Lisp_Object val;
 {
-  register unsigned char *old, *new;
+  register const unsigned char *old;
+  register unsigned char *new;
   register int n;
   int osize, count;
 
-  osize = STRING_BYTES (XSTRING (val));
+  osize = SBYTES (val);
 
   /* Count the number of $ characters.  */
-  for (n = osize, count = 0, old = XSTRING (val)->data; n > 0; n--)
+  for (n = osize, count = 0, old = SDATA (val); n > 0; n--)
     if (*old++ == '$') count++;
   if (count > 0)
     {
-      old = XSTRING (val)->data;
-      val = make_uninit_multibyte_string (XSTRING (val)->size + count,
+      old = SDATA (val);
+      val = make_uninit_multibyte_string (SCHARS (val) + count,
                                          osize + count);
-      new = XSTRING (val)->data;
+      new = SDATA (val);
       for (n = osize; n > 0; n--)
        if (*old != '$')
          *new++ = *old++;
@@ -5804,6 +5843,13 @@ double_dollars (val)
   return val;
 }
 
+static Lisp_Object
+read_file_name_cleanup (arg)
+     Lisp_Object arg;
+{
+  return (current_buffer->directory = arg);
+}
+
 DEFUN ("read-file-name-internal", Fread_file_name_internal, Sread_file_name_internal,
        3, 3, 0,
        doc: /* Internal subroutine for read-file-name.  Do not call this.  */)
@@ -5826,7 +5872,7 @@ DEFUN ("read-file-name-internal", Fread_file_name_internal, Sread_file_name_inte
   /* No need to protect ACTION--we only compare it with t and nil.  */
   GCPRO5 (string, realdir, name, specdir, orig_string);
 
-  if (XSTRING (string)->size == 0)
+  if (SCHARS (string) == 0)
     {
       if (EQ (action, Qlambda))
        {
@@ -5868,18 +5914,60 @@ DEFUN ("read-file-name-internal", Fread_file_name_internal, Sread_file_name_inte
   UNGCPRO;
 
   if (EQ (action, Qt))
-    return Ffile_name_all_completions (name, realdir);
+    {
+      Lisp_Object all = Ffile_name_all_completions (name, realdir);
+      Lisp_Object comp;
+      int count;
+
+      if (NILP (Vread_file_name_predicate)
+         || EQ (Vread_file_name_predicate, Qfile_exists_p))
+       return all;
+
+#ifndef VMS
+      if (EQ (Vread_file_name_predicate, Qfile_directory_p))
+       {
+         /* Brute-force speed up for directory checking: 
+            Discard strings which don't end in a slash.  */
+         for (comp = Qnil; CONSP (all); all = XCDR (all))
+           {
+             Lisp_Object tem = XCAR (all);
+             int len;
+             if (STRINGP (tem) &&
+                 (len = SCHARS (tem), len > 0) &&
+                 IS_DIRECTORY_SEP (SREF (tem, len-1)))
+               comp = Fcons (tem, comp);
+           }
+       }
+      else
+#endif
+       {
+         /* Must do it the hard (and slow) way.  */
+         GCPRO3 (all, comp, specdir);
+         count = SPECPDL_INDEX ();
+         record_unwind_protect (read_file_name_cleanup, current_buffer->directory);
+         current_buffer->directory = realdir;
+         for (comp = Qnil; CONSP (all); all = XCDR (all))
+           if (!NILP (call1 (Vread_file_name_predicate, XCAR (all))))
+             comp = Fcons (XCAR (all), comp);
+         unbind_to (count, Qnil);
+         UNGCPRO;
+       }
+      return Fnreverse (comp);
+    }
+
   /* Only other case actually used is ACTION = lambda */
 #ifdef VMS
   /* Supposedly this helps commands such as `cd' that read directory names,
      but can someone explain how it helps them? -- RMS */
-  if (XSTRING (name)->size == 0)
+  if (SCHARS (name) == 0)
     return Qt;
 #endif /* VMS */
+  if (!NILP (Vread_file_name_predicate))
+    return call1 (Vread_file_name_predicate, string);
   return Ffile_exists_p (string);
 }
 
-DEFUN ("read-file-name", Fread_file_name, Sread_file_name, 1, 5, 0,
+DEFUN ("read-file-name", Fread_file_name, Sread_file_name, 1, 6, 0,
        doc: /* Read file name, prompting with PROMPT and completing in directory DIR.
 Value is not expanded---you must call `expand-file-name' yourself.
 Default name to DEFAULT-FILENAME if user enters a null string.
@@ -5888,13 +5976,15 @@ Default name to DEFAULT-FILENAME if user enters a null string.
 Fourth arg MUSTMATCH non-nil means require existing file's name.
  Non-nil and non-t means also require confirmation after completion.
 Fifth arg INITIAL specifies text to start with.
+If optional sixth arg PREDICATE is non-nil, possible completions and the 
+resulting file name must satisfy (funcall PREDICATE NAME).
 DIR defaults to current buffer's directory default.
 
 If this command was invoked with the mouse, use a file dialog box if
 `use-dialog-box' is non-nil, and the window system or X toolkit in use
 provides a file dialog box.  */)
-     (prompt, dir, default_filename, mustmatch, initial)
-     Lisp_Object prompt, dir, default_filename, mustmatch, initial;
+     (prompt, dir, default_filename, mustmatch, initial, predicate)
+     Lisp_Object prompt, dir, default_filename, mustmatch, initial, predicate;
 {
   Lisp_Object val, insdef, tem;
   struct gcpro gcpro1, gcpro2;
@@ -5926,23 +6016,23 @@ provides a file dialog box.  */)
 #endif
   if (homedir != 0
       && STRINGP (dir)
-      && !strncmp (homedir, XSTRING (dir)->data, strlen (homedir))
-      && IS_DIRECTORY_SEP (XSTRING (dir)->data[strlen (homedir)]))
+      && !strncmp (homedir, SDATA (dir), strlen (homedir))
+      && IS_DIRECTORY_SEP (SREF (dir, strlen (homedir))))
     {
-      dir = make_string (XSTRING (dir)->data + strlen (homedir) - 1,
-                        STRING_BYTES (XSTRING (dir)) - strlen (homedir) + 1);
-      XSTRING (dir)->data[0] = '~';
+      dir = make_string (SDATA (dir) + strlen (homedir) - 1,
+                        SBYTES (dir) - strlen (homedir) + 1);
+      SSET (dir, 0, '~');
     }
   /* Likewise for default_filename.  */
   if (homedir != 0
       && STRINGP (default_filename)
-      && !strncmp (homedir, XSTRING (default_filename)->data, strlen (homedir))
-      && IS_DIRECTORY_SEP (XSTRING (default_filename)->data[strlen (homedir)]))
+      && !strncmp (homedir, SDATA (default_filename), strlen (homedir))
+      && IS_DIRECTORY_SEP (SREF (default_filename, strlen (homedir))))
     {
       default_filename
-       = make_string (XSTRING (default_filename)->data + strlen (homedir) - 1,
-                      STRING_BYTES (XSTRING (default_filename)) - strlen (homedir) + 1);
-      XSTRING (default_filename)->data[0] = '~';
+       = make_string (SDATA (default_filename) + strlen (homedir) - 1,
+                      SBYTES (default_filename) - strlen (homedir) + 1);
+      SSET (default_filename, 0, '~');
     }
   if (!NILP (default_filename))
     {
@@ -5960,7 +6050,7 @@ provides a file dialog box.  */)
          args[0] = insdef;
          args[1] = initial;
          insdef = Fconcat (2, args);
-         pos = make_number (XSTRING (double_dollars (dir))->size);
+         pos = make_number (SCHARS (double_dollars (dir)));
          insdef = Fcons (double_dollars (insdef), pos);
        }
       else
@@ -5971,12 +6061,29 @@ provides a file dialog box.  */)
   else
     insdef = Qnil;
 
-  count = specpdl_ptr - specpdl;
+  if (!NILP (Vread_file_name_function))
+    {
+      Lisp_Object args[7];
+
+      GCPRO2 (insdef, default_filename);
+      args[0] = Vread_file_name_function;
+      args[1] = prompt;
+      args[2] = dir;
+      args[3] = default_filename;
+      args[4] = mustmatch;
+      args[5] = initial;
+      args[6] = predicate;
+      RETURN_UNGCPRO (Ffuncall (7, args));
+    }
+
+  count = SPECPDL_INDEX ();
 #ifdef VMS
   specbind (intern ("completion-ignore-case"), Qt);
 #endif
 
   specbind (intern ("minibuffer-completing-file-name"), Qt);
+  specbind (intern ("read-file-name-predicate"), 
+           (NILP (predicate) ? Qfile_exists_p : predicate));
 
   GCPRO2 (insdef, default_filename);
   
@@ -5988,7 +6095,7 @@ provides a file dialog box.  */)
       /* If DIR contains a file name, split it.  */
       Lisp_Object file;
       file = Ffile_name_nondirectory (dir);
-      if (XSTRING (file)->size && NILP (default_filename))
+      if (SCHARS (file) && NILP (default_filename))
        {
          default_filename = file;
          dir = Ffile_name_directory (dir);
@@ -6032,7 +6139,7 @@ provides a file dialog box.  */)
 
   if (!NILP (tem) && !NILP (default_filename))
     val = default_filename;
-  else if (XSTRING (val)->size == 0 && NILP (insdef))
+  else if (SCHARS (val) == 0 && NILP (insdef))
     {
       if (!NILP (default_filename))
        val = default_filename;
@@ -6201,6 +6308,14 @@ same format as a regular save would use.  */);
   Fput (Qfile_date_error, Qerror_message,
        build_string ("Cannot set file date"));
 
+  DEFVAR_LISP ("read-file-name-function", &Vread_file_name_function,
+              doc: /* If this is non-nil, `read-file-name' does its work by calling this function.  */);
+  Vread_file_name_function = Qnil;
+
+  DEFVAR_LISP ("read-file-name-predicate", &Vread_file_name_predicate,
+              doc: /* Current predicate used by `read-file-name-internal'.  */);
+  Vread_file_name_predicate = Qnil;
+
   DEFVAR_BOOL ("insert-default-directory", &insert_default_directory,
               doc: /* *Non-nil means when reading a filename start with default dir in minibuffer.  */);
   insert_default_directory = 1;
@@ -6215,10 +6330,7 @@ nil means use format `var'.  This variable is meaningful only on VMS.  */);
 The value should be either ?/ or ?\\ (any other value is treated as ?\\).
 This variable affects the built-in functions only on Windows,
 on other platforms, it is initialized so that Lisp code can find out
-what the normal separator is.
-
-WARNING: This variable is deprecated and will be removed in the near
-future.  DO NOT USE IT.  */);
+what the normal separator is.  */);
 
   DEFVAR_LISP ("file-name-handler-alist", &Vfile_name_handler_alist,
               doc: /* *Alist of elements (REGEXP . HANDLER) for file names handled specially.
@@ -6265,7 +6377,8 @@ of the form (POSITION . STRING), consisting of strings to be effectively
 inserted at the specified positions of the file being written (1 means to
 insert before the first byte written).  The POSITIONs must be sorted into
 increasing order.  If there are several functions in the list, the several
-lists are merged destructively.  */);
+lists are merged destructively.  Alternatively, the function can return
+with a different buffer current and value nil.*/);
   Vwrite_region_annotate_functions = Qnil;
 
   DEFVAR_LISP ("write-region-annotations-so-far",