(syms_of_dired): staticpro Qdirectory_files, Qfile_name_completion,
[bpt/emacs.git] / src / dired.c
index 834729a..a8468c0 100644 (file)
@@ -15,7 +15,8 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
+the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
 
 
 #include <config.h>
@@ -105,12 +106,13 @@ Lisp_Object Qfile_attributes;
 DEFUN ("directory-files", Fdirectory_files, Sdirectory_files, 1, 4, 0,
   "Return a list of names of files in DIRECTORY.\n\
 There are three optional arguments:\n\
-If FULL is non-nil, absolute pathnames of the files are returned.\n\
-If MATCH is non-nil, only pathnames containing that regexp are returned.\n\
+If FULL is non-nil, return absolute file names.  Otherwise return names\n\
+ that are relative to the specified directory.\n\
+If MATCH is non-nil, mention only file names that match the regexp MATCH.\n\
 If NOSORT is non-nil, the list is not sorted--its order is unpredictable.\n\
  NOSORT is useful if you plan to sort the result yourself.")
-  (dirname, full, match, nosort)
-     Lisp_Object dirname, full, match, nosort;
+  (directory, full, match, nosort)
+     Lisp_Object directory, full, match, nosort;
 {
   DIR *d;
   int dirnamelen;
@@ -120,14 +122,14 @@ If NOSORT is non-nil, the list is not sorted--its order is unpredictable.\n\
 
   /* If the file name has special constructs in it,
      call the corresponding file handler.  */
-  handler = Ffind_file_name_handler (dirname, Qdirectory_files);
+  handler = Ffind_file_name_handler (directory, Qdirectory_files);
   if (!NILP (handler))
     {
       Lisp_Object args[6];
 
       args[0] = handler;
       args[1] = Qdirectory_files;
-      args[2] = dirname;
+      args[2] = directory;
       args[3] = full;
       args[4] = match;
       args[5] = nosort;
@@ -140,10 +142,10 @@ If NOSORT is non-nil, the list is not sorted--its order is unpredictable.\n\
     /* Because of file name handlers, these functions might call
      Ffuncall, and cause a GC.  */
     GCPRO1 (match);
-    dirname = Fexpand_file_name (dirname, Qnil);
+    directory = Fexpand_file_name (directory, Qnil);
     UNGCPRO;
-    GCPRO2 (match, dirname);
-    dirfilename = Fdirectory_file_name (dirname);
+    GCPRO2 (match, directory);
+    dirfilename = Fdirectory_file_name (directory);
     UNGCPRO;
   }
 
@@ -152,7 +154,7 @@ If NOSORT is non-nil, the list is not sorted--its order is unpredictable.\n\
       CHECK_STRING (match, 3);
 
       /* MATCH might be a flawed regular expression.  Rather than
-        catching and signalling our own errors, we just call
+        catching and signaling our own errors, we just call
         compile_pattern to do the work for us.  */
 #ifdef VMS
       bufp = compile_pattern (match, 0,
@@ -166,15 +168,15 @@ If NOSORT is non-nil, the list is not sorted--its order is unpredictable.\n\
      which might compile a new regexp until we're done with the loop!  */
 
   /* Do this opendir after anything which might signal an error; if
-     an error is signalled while the directory stream is open, we
+     an error is signaled while the directory stream is open, we
      have to make sure it gets closed, and setting up an
      unwind_protect to do so would be a pain.  */
   d = opendir (XSTRING (dirfilename)->data);
   if (! d)
-    report_file_error ("Opening directory", Fcons (dirname, Qnil));
+    report_file_error ("Opening directory", Fcons (directory, Qnil));
 
   list = Qnil;
-  dirnamelen = XSTRING (dirname)->size;
+  dirnamelen = XSTRING (directory)->size;
 
   /* Loop reading blocks */
   while (1)
@@ -198,12 +200,12 @@ If NOSORT is non-nil, the list is not sorted--its order is unpredictable.\n\
                  /* Decide whether we need to add a directory separator.  */
 #ifndef VMS
                  if (dirnamelen == 0
-                     || !IS_ANY_SEP (XSTRING (dirname)->data[dirnamelen - 1]))
+                     || !IS_ANY_SEP (XSTRING (directory)->data[dirnamelen - 1]))
                    needsep = 1;
 #endif /* VMS */
 
                  name = make_uninit_string (total + needsep);
-                 bcopy (XSTRING (dirname)->data, XSTRING (name)->data,
+                 bcopy (XSTRING (directory)->data, XSTRING (name)->data,
                         dirnamelen);
                  if (needsep)
                    XSTRING (name)->data[afterdirindex++] = DIRECTORY_SEP;
@@ -226,41 +228,53 @@ Lisp_Object file_name_completion ();
 
 DEFUN ("file-name-completion", Ffile_name_completion, Sfile_name_completion,
   2, 2, 0,
-  "Complete file name FILE in directory DIR.\n\
+  "Complete file name FILE in directory DIRECTORY.\n\
 Returns the longest string\n\
-common to all filenames in DIR that start with FILE.\n\
+common to all file names in DIRECTORY that start with FILE.\n\
 If there is only one and FILE matches it exactly, returns t.\n\
 Returns nil if DIR contains no name starting with FILE.")
-  (file, dirname)
-     Lisp_Object file, dirname;
+  (file, directory)
+     Lisp_Object file, directory;
 {
   Lisp_Object handler;
 
+  /* If the directory name has special constructs in it,
+     call the corresponding file handler.  */
+  handler = Ffind_file_name_handler (directory, Qfile_name_completion);
+  if (!NILP (handler))
+    return call3 (handler, Qfile_name_completion, file, directory);
+
   /* If the file name has special constructs in it,
      call the corresponding file handler.  */
-  handler = Ffind_file_name_handler (dirname, Qfile_name_completion);
+  handler = Ffind_file_name_handler (file, Qfile_name_completion);
   if (!NILP (handler))
-    return call3 (handler, Qfile_name_completion, file, dirname);
+    return call3 (handler, Qfile_name_completion, file, directory);
 
-  return file_name_completion (file, dirname, 0, 0);
+  return file_name_completion (file, directory, 0, 0);
 }
 
 DEFUN ("file-name-all-completions", Ffile_name_all_completions,
   Sfile_name_all_completions, 2, 2, 0,
-  "Return a list of all completions of file name FILE in directory DIR.\n\
-These are all file names in directory DIR which begin with FILE.")
-  (file, dirname)
-     Lisp_Object file, dirname;
+  "Return a list of all completions of file name FILE in directory DIRECTORY.\n\
+These are all file names in directory DIRECTORY which begin with FILE.")
+  (file, directory)
+     Lisp_Object file, directory;
 {
   Lisp_Object handler;
 
+  /* If the directory name has special constructs in it,
+     call the corresponding file handler.  */
+  handler = Ffind_file_name_handler (directory, Qfile_name_all_completions);
+  if (!NILP (handler))
+    return call3 (handler, Qfile_name_all_completions, file, directory);
+
   /* If the file name has special constructs in it,
      call the corresponding file handler.  */
-  handler = Ffind_file_name_handler (dirname, Qfile_name_all_completions);
+  handler = Ffind_file_name_handler (file, Qfile_name_all_completions);
   if (!NILP (handler))
-    return call3 (handler, Qfile_name_all_completions, file, dirname);
+    return call3 (handler, Qfile_name_all_completions, file, directory);
 
-  return file_name_completion (file, dirname, 1, 0);
+  return file_name_completion (file, directory, 1, 0);
 }
 
 Lisp_Object
@@ -281,6 +295,19 @@ file_name_completion (file, dirname, all_flag, ver_flag)
   int count = specpdl_ptr - specpdl;
   struct gcpro gcpro1, gcpro2, gcpro3;
 
+#ifdef MSDOS
+#if __DJGPP__ > 1
+  /* Some fields of struct stat are *very* expensive to compute on MS-DOS,
+     but aren't required here.  Avoid computing the following fields:
+     st_inode, st_size and st_nlink for directories, and the execute bits
+     in st_mode for non-directory files with non-standard extensions.  */
+
+  unsigned short save_djstat_flags = _djstat_flags;
+
+  _djstat_flags = _STAT_INODE | _STAT_EXEC_MAGIC | _STAT_DIRSIZE;
+#endif
+#endif
+
 #ifdef VMS
   extern DIRENTRY * readdirver ();
 
@@ -439,6 +466,8 @@ file_name_completion (file, dirname, all_flag, ver_flag)
                     use it as the best match rather than one that is not
                     an exact match.  This way, we get the case pattern
                     of the actual match.  */
+                 /* This tests that the current file is an exact match
+                    but BESTMATCH is not (it is too long).  */
                  if ((matchsize == len
                       && matchsize + !!directoryp 
                          < XSTRING (bestmatch)->size)
@@ -446,13 +475,15 @@ file_name_completion (file, dirname, all_flag, ver_flag)
                      /* If there is no exact match ignoring case,
                         prefer a match that does not change the case
                         of the input.  */
+                     /* If there is more than one exact match aside from
+                        case, and one of them is exact including case,
+                        prefer that one.  */
+                     /* This == checks that, of current file and BESTMATCH,
+                        either both or neither are exact.  */
                      (((matchsize == len)
                        ==
                        (matchsize + !!directoryp 
                         == XSTRING (bestmatch)->size))
-                      /* If there is more than one exact match aside from
-                         case, and one of them is exact including case,
-                         prefer that one.  */
                       && !bcmp (p2, XSTRING (file)->data, XSTRING (file)->size)
                       && bcmp (p1, XSTRING (file)->data, XSTRING (file)->size)))
                    {
@@ -478,6 +509,12 @@ file_name_completion (file, dirname, all_flag, ver_flag)
   UNGCPRO;
   bestmatch = unbind_to (count, bestmatch);
 
+#ifdef MSDOS
+#if __DJGPP__ > 1
+  _djstat_flags = save_djstat_flags;
+#endif
+#endif
+
   if (all_flag || NILP (bestmatch))
     return bestmatch;
   if (matchcount == 1 && bestmatchsize == XSTRING (file)->size)
@@ -524,11 +561,11 @@ file_name_completion_stat (dirname, dp, st_addr)
 
 DEFUN ("file-name-all-versions", Ffile_name_all_versions,
   Sfile_name_all_versions, 2, 2, 0,
-  "Return a list of all versions of file name FILE in directory DIR.")
-  (file, dirname)
-     Lisp_Object file, dirname;
+  "Return a list of all versions of file name FILE in directory DIRECTORY.")
+  (file, directory)
+     Lisp_Object file, directory;
 {
-  return file_name_completion (file, dirname, 1, 1);
+  return file_name_completion (file, directory, 1, 1);
 }
 
 DEFUN ("file-version-limit", Ffile_version_limit, Sfile_version_limit, 1, 1, 0,
@@ -609,22 +646,6 @@ If file does not exist, returns nil.")
   if (lstat (XSTRING (filename)->data, &s) < 0)
     return Qnil;
 
-#ifdef MSDOS
-  {
-    char *tmpnam = XSTRING (Ffile_name_nondirectory (filename))->data;
-    int l = strlen (tmpnam);
-
-    if (l >= 5 
-       && S_ISREG (s.st_mode)
-       && (stricmp (&tmpnam[l - 4], ".com") == 0
-           || stricmp (&tmpnam[l - 4], ".exe") == 0
-           || stricmp (&tmpnam[l - 4], ".bat") == 0))
-      {
-       s.st_mode |= S_IEXEC;
-      }
-  }
-#endif /* MSDOS */
-
   switch (s.st_mode & S_IFMT)
     {
     default:
@@ -658,24 +679,13 @@ If file does not exist, returns nil.")
   else                                 /* if we can't tell, assume worst */
     values[9] = Qt;
 #else                                  /* file gid will be egid */
-#ifdef WINDOWSNT
-  values[9] = Qnil;    /* sorry, no group IDs on NT */
-#else  /* not WINDOWSNT */
   values[9] = (s.st_gid != getegid ()) ? Qt : Qnil;
-#endif /* not WINDOWSNT */
 #endif /* BSD4_2 (or BSD4_3) */
 #ifdef BSD4_3
 #undef BSD4_2 /* ok, you can look again without throwing up */
 #endif
-#ifdef WINDOWSNT
-  /* Fill in the inode and device values specially...see nt.c.  */
-  if (!get_inode_and_device_vals (filename, &values[10], &values[11])) {
-      return Qnil;
-  }
-#else  /* not WINDOWSNT */
   values[10] = make_number (s.st_ino);
   values[11] = make_number (s.st_dev);
-#endif /* not WINDOWSNT */
   return Flist (sizeof(values) / sizeof(values[0]), values);
 }
 \f
@@ -686,6 +696,11 @@ syms_of_dired ()
   Qfile_name_all_completions = intern ("file-name-all-completions");
   Qfile_attributes = intern ("file-attributes");
 
+  staticpro (&Qdirectory_files);
+  staticpro (&Qfile_name_completion);
+  staticpro (&Qfile_name_all_completions);
+  staticpro (&Qfile_attributes);
+
   defsubr (&Sdirectory_files);
   defsubr (&Sfile_name_completion);
 #ifdef VMS