annotate src/dired.c @ 498:88e7e9bafe19

[xemacs-hg @ 2001-05-02 16:08:10 by james] Correctly order SET_FACE_PROPERTY parameters.
author james
date Wed, 02 May 2001 16:08:11 +0000
parents 223736d75acb
children ef4d2466a29c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1 /* Lisp functions for making directory listings.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2 Copyright (C) 1985, 1986, 1992, 1993, 1994 Free Software Foundation, Inc.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
3
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
4 This file is part of XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
5
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
6 XEmacs is free software; you can redistribute it and/or modify it
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7 under the terms of the GNU General Public License as published by the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
8 Free Software Foundation; either version 2, or (at your option) any
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
9 later version.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
10
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
14 for more details.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
15
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
17 along with XEmacs; see the file COPYING. If not, write to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
19 Boston, MA 02111-1307, USA. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
20
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
21 /* Synched up with: FSF 19.30. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
23 #include <config.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24 #include "lisp.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
25
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
26 #include "sysfile.h"
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
27 #include "sysdir.h"
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
28 #include "systime.h"
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
29 #include "sysdep.h"
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
30 #include "syspwd.h"
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
31 #include "buffer.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
32 #include "commands.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
33 #include "elhash.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
34 #include "regex.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
35 #include "opaque.h"
460
223736d75acb Import from CVS: tag r21-2-45
cvs
parents: 446
diff changeset
36 #include "syntax.h"
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
37
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
38 Lisp_Object Vcompletion_ignored_extensions;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39 Lisp_Object Qdirectory_files;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40 Lisp_Object Qfile_name_completion;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41 Lisp_Object Qfile_name_all_completions;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42 Lisp_Object Qfile_attributes;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
43
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
44 static Lisp_Object
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
45 close_directory_unwind (Lisp_Object unwind_obj)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
46 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
47 DIR *d = (DIR *)get_opaque_ptr (unwind_obj);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
48 closedir (d);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
49 free_opaque_ptr (unwind_obj);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
50 return Qnil;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
51 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
52
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53 DEFUN ("directory-files", Fdirectory_files, 1, 5, 0, /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
54 Return a list of names of files in DIRECTORY.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55 There are four optional arguments:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
56 If FULL is non-nil, absolute pathnames of the files are returned.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
57 If MATCH is non-nil, only pathnames containing that regexp are returned.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
58 If NOSORT is non-nil, the list is not sorted--its order is unpredictable.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
59 NOSORT is useful if you plan to sort the result yourself.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60 If FILES-ONLY is the symbol t, then only the "files" in the directory
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
61 will be returned; subdirectories will be excluded. If FILES-ONLY is not
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
62 nil and not t, then only the subdirectories will be returned. Otherwise,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
63 if FILES-ONLY is nil (the default) then both files and subdirectories will
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
64 be returned.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
65 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
66 (directory, full, match, nosort, files_only))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
67 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68 /* This function can GC */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
69 DIR *d;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
70 Lisp_Object list = Qnil;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
71 Bytecount directorylen;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
72 Lisp_Object handler;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
73 struct re_pattern_buffer *bufp = NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
74 int speccount = specpdl_depth ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
75 char *statbuf, *statbuf_tail;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
76
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
77 struct gcpro gcpro1, gcpro2;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
78 GCPRO2 (directory, list);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
79
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
80 /* If the file name has special constructs in it,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
81 call the corresponding file handler. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
82 handler = Ffind_file_name_handler (directory, Qdirectory_files);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
83 if (!NILP (handler))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
84 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
85 UNGCPRO;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
86 if (!NILP (files_only))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
87 return call6 (handler, Qdirectory_files, directory, full, match,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
88 nosort, files_only);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
89 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
90 return call5 (handler, Qdirectory_files, directory, full, match,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
91 nosort);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
92 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
93
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
94 /* #### why do we do Fexpand_file_name after file handlers here,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
95 but earlier everywhere else? */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
96 directory = Fexpand_file_name (directory, Qnil);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
97 directory = Ffile_name_as_directory (directory);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
98 directorylen = XSTRING_LENGTH (directory);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
99
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
100 statbuf = (char *)alloca (directorylen + MAXNAMLEN + 1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
101 memcpy (statbuf, XSTRING_DATA (directory), directorylen);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
102 statbuf_tail = statbuf + directorylen;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
103
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
104 /* XEmacs: this should come after Ffile_name_as_directory() to avoid
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
105 potential regexp cache smashage. It comes before the opendir()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
106 because it might signal an error. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
107 if (!NILP (match))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
108 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
109 CHECK_STRING (match);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
110
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
111 /* MATCH might be a flawed regular expression. Rather than
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
112 catching and signalling our own errors, we just call
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
113 compile_pattern to do the work for us. */
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 444
diff changeset
114 bufp = compile_pattern (match, 0, Qnil, 0, ERROR_ME);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
115 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
116
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
117 /* Now *bufp is the compiled form of MATCH; don't call anything
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
118 which might compile a new regexp until we're done with the loop! */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
119
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
120 /* Do this opendir after anything which might signal an error.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
121 NOTE: the above comment is old; previously, there was no
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
122 unwind-protection in case of error, but now there is. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
123 d = opendir ((char *) XSTRING_DATA (directory));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
124 if (!d)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
125 report_file_error ("Opening directory", list1 (directory));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
126
460
223736d75acb Import from CVS: tag r21-2-45
cvs
parents: 446
diff changeset
127 regex_match_object = Qt;
223736d75acb Import from CVS: tag r21-2-45
cvs
parents: 446
diff changeset
128 regex_emacs_buffer = current_buffer;
223736d75acb Import from CVS: tag r21-2-45
cvs
parents: 446
diff changeset
129
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
130 record_unwind_protect (close_directory_unwind, make_opaque_ptr ((void *)d));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
131
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
132 /* Loop reading blocks */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
133 while (1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
134 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
135 DIRENTRY *dp = readdir (d);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
136 int len;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
137
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
138 if (!dp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
139 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
140 len = NAMLEN (dp);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
141 if (DIRENTRY_NONEMPTY (dp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
142 && (NILP (match)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
143 || (0 <= re_search (bufp, dp->d_name, len, 0, len, 0))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
144 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
145 if (!NILP (files_only))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
146 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
147 struct stat st;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
148 int dir_p = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
149
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
150 memcpy (statbuf_tail, dp->d_name, len);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
151 statbuf_tail[len] = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
152
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
153 if (xemacs_stat (statbuf, &st) == 0
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
154 && (st.st_mode & S_IFMT) == S_IFDIR)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
155 dir_p = 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
156
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
157 if (EQ (files_only, Qt) && dir_p)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
158 continue;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
159 else if (!EQ (files_only, Qt) && !dir_p)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
160 continue;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
161 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
162
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
163 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
164 Lisp_Object name =
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
165 make_string ((Bufbyte *)dp->d_name, len);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
166 if (!NILP (full))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
167 name = concat2 (directory, name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
168
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
169 list = Fcons (name, list);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
170 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
171 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
172 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
173 unbind_to (speccount, Qnil); /* This will close the dir */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
174
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
175 if (NILP (nosort))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
176 list = Fsort (Fnreverse (list), Qstring_lessp);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
177
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
178 RETURN_UNGCPRO (list);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
179 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
180
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
181 static Lisp_Object file_name_completion (Lisp_Object file,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
182 Lisp_Object directory,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
183 int all_flag, int ver_flag);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
184
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
185 DEFUN ("file-name-completion", Ffile_name_completion, 2, 2, 0, /*
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
186 Complete file name PARTIAL-FILENAME in directory DIRECTORY.
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
187 Return the longest prefix common to all file names in DIRECTORY
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
188 that start with PARTIAL-FILENAME.
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
189 If there is only one and PARTIAL-FILENAME matches it exactly, return t.
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
190 Return nil if DIRECTORY contains no name starting with PARTIAL-FILENAME.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
191
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
192 File names which end with any member of `completion-ignored-extensions'
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
193 are not considered as possible completions for PARTIAL-FILENAME unless
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
194 there is no other possible completion. `completion-ignored-extensions'
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
195 is not applied to the names of directories.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
196 */
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
197 (partial_filename, directory))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
198 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
199 /* This function can GC. GC checked 1996.04.06. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
200 Lisp_Object handler;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
201
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
202 /* If the directory name has special constructs in it,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
203 call the corresponding file handler. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
204 handler = Ffind_file_name_handler (directory, Qfile_name_completion);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
205 if (!NILP (handler))
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
206 return call3 (handler, Qfile_name_completion, partial_filename, directory);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
207
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
208 /* If the file name has special constructs in it,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
209 call the corresponding file handler. */
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
210 handler = Ffind_file_name_handler (partial_filename, Qfile_name_completion);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
211 if (!NILP (handler))
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
212 return call3 (handler, Qfile_name_completion, partial_filename, directory);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
213
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
214 return file_name_completion (partial_filename, directory, 0, 0);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
215 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
216
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
217 DEFUN ("file-name-all-completions", Ffile_name_all_completions, 2, 2, 0, /*
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
218 Return a list of all completions of PARTIAL-FILENAME in DIRECTORY.
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
219 These are all file names in DIRECTORY which begin with PARTIAL-FILENAME.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
220
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
221 File names which end with any member of `completion-ignored-extensions'
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
222 are not considered as possible completions for PARTIAL-FILENAME unless
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
223 there is no other possible completion. `completion-ignored-extensions'
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
224 is not applied to the names of directories.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
225 */
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
226 (partial_filename, directory))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
227 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
228 /* This function can GC. GC checked 1997.06.04. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
229 Lisp_Object handler;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
230 struct gcpro gcpro1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
231
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
232 GCPRO1 (directory);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
233 directory = Fexpand_file_name (directory, Qnil);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
234 /* If the file name has special constructs in it,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
235 call the corresponding file handler. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
236 handler = Ffind_file_name_handler (directory, Qfile_name_all_completions);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
237 UNGCPRO;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
238 if (!NILP (handler))
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
239 return call3 (handler, Qfile_name_all_completions, partial_filename,
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
240 directory);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
241
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
242 return file_name_completion (partial_filename, directory, 1, 0);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
243 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
244
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
245 static int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
246 file_name_completion_stat (Lisp_Object directory, DIRENTRY *dp,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
247 struct stat *st_addr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
248 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
249 Bytecount len = NAMLEN (dp);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
250 Bytecount pos = XSTRING_LENGTH (directory);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
251 int value;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
252 char *fullname = (char *) alloca (len + pos + 2);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
253
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
254 memcpy (fullname, XSTRING_DATA (directory), pos);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
255 if (!IS_DIRECTORY_SEP (fullname[pos - 1]))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
256 fullname[pos++] = DIRECTORY_SEP;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
257
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
258 memcpy (fullname + pos, dp->d_name, len);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
259 fullname[pos + len] = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
260
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
261 #ifdef S_IFLNK
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
262 /* We want to return success if a link points to a nonexistent file,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
263 but we want to return the status for what the link points to,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
264 in case it is a directory. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
265 value = lstat (fullname, st_addr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
266 if (S_ISLNK (st_addr->st_mode))
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
267 xemacs_stat (fullname, st_addr);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
268 #else
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
269 value = xemacs_stat (fullname, st_addr);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
270 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
271 return value;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
272 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
273
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
274 static Lisp_Object
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
275 file_name_completion_unwind (Lisp_Object locative)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
276 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
277 DIR *d;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
278 Lisp_Object obj = XCAR (locative);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
279
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
280 if (!NILP (obj))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
281 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
282 d = (DIR *)get_opaque_ptr (obj);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
283 closedir (d);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
284 free_opaque_ptr (obj);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
285 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
286 free_cons (XCONS (locative));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
287 return Qnil;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
288 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
289
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
290 static Lisp_Object
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
291 file_name_completion (Lisp_Object file, Lisp_Object directory, int all_flag,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
292 int ver_flag)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
293 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
294 /* This function can GC */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
295 DIR *d = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
296 int matchcount = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
297 Lisp_Object bestmatch = Qnil;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
298 Charcount bestmatchsize = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
299 struct stat st;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
300 int passcount;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
301 int speccount = specpdl_depth ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
302 Charcount file_name_length;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
303 Lisp_Object locative;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
304 struct gcpro gcpro1, gcpro2, gcpro3;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
305
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
306 GCPRO3 (file, directory, bestmatch);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
307
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
308 CHECK_STRING (file);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
309
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
310 #ifdef WIN32_NATIVE
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
311 /* Filename completion on Windows ignores case, since Windows
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
312 filesystems do. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
313 specbind (Qcompletion_ignore_case, Qt);
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
314 #endif /* WIN32_NATIVE */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
315
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
316 #ifdef FILE_SYSTEM_CASE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
317 file = FILE_SYSTEM_CASE (file);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
318 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
319 directory = Fexpand_file_name (directory, Qnil);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
320 file_name_length = XSTRING_CHAR_LENGTH (file);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
321
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
322 /* With passcount = 0, ignore files that end in an ignored extension.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
323 If nothing found then try again with passcount = 1, don't ignore them.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
324 If looking for all completions, start with passcount = 1,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
325 so always take even the ignored ones.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
326
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
327 ** It would not actually be helpful to the user to ignore any possible
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
328 completions when making a list of them.** */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
329
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
330 /* We cannot use close_directory_unwind() because we change the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
331 directory. The old code used to just avoid signaling errors, and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
332 call closedir, but it was wrong, because it made sane handling of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
333 QUIT impossible and, besides, various utility functions like
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
334 regexp_ignore_completion_p can signal errors. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
335 locative = noseeum_cons (Qnil, Qnil);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
336 record_unwind_protect (file_name_completion_unwind, locative);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
337
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
338 for (passcount = !!all_flag; NILP (bestmatch) && passcount < 2; passcount++)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
339 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
340 d = opendir ((char *) XSTRING_DATA (Fdirectory_file_name (directory)));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
341 if (!d)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
342 report_file_error ("Opening directory", list1 (directory));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
343 XCAR (locative) = make_opaque_ptr ((void *)d);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
344
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
345 /* Loop reading blocks */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
346 while (1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
347 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
348 DIRENTRY *dp;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
349 Bytecount len;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
350 /* scmp() works in characters, not bytes, so we have to compute
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
351 this value: */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
352 Charcount cclen;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
353 int directoryp;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
354 int ignored_extension_p = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
355 Bufbyte *d_name;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
356
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
357 dp = readdir (d);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
358 if (!dp) break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
359
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
360 /* Cast to Bufbyte* is OK, as readdir() Mule-encapsulates. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
361 d_name = (Bufbyte *) dp->d_name;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
362 len = NAMLEN (dp);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
363 cclen = bytecount_to_charcount (d_name, len);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
364
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
365 QUIT;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
366
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
367 if (! DIRENTRY_NONEMPTY (dp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
368 || cclen < file_name_length
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
369 || 0 <= scmp (d_name, XSTRING_DATA (file), file_name_length))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
370 continue;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
371
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
372 if (file_name_completion_stat (directory, dp, &st) < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
373 continue;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
374
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
375 directoryp = ((st.st_mode & S_IFMT) == S_IFDIR);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
376 if (directoryp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
377 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
378 #ifndef TRIVIAL_DIRECTORY_ENTRY
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
379 #define TRIVIAL_DIRECTORY_ENTRY(n) (!strcmp (n, ".") || !strcmp (n, ".."))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
380 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
381 /* "." and ".." are never interesting as completions, but are
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
382 actually in the way in a directory containing only one file. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
383 if (!passcount && TRIVIAL_DIRECTORY_ENTRY (dp->d_name))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
384 continue;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
385 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
386 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
387 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
388 /* Compare extensions-to-be-ignored against end of this file name */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
389 /* if name is not an exact match against specified string. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
390 if (!passcount && cclen > file_name_length)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
391 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
392 Lisp_Object tem;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
393 /* and exit this for loop if a match is found */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
394 EXTERNAL_LIST_LOOP (tem, Vcompletion_ignored_extensions)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
395 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
396 Lisp_Object elt = XCAR (tem);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
397 Charcount skip;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
398
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
399 CHECK_STRING (elt);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
400
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
401 skip = cclen - XSTRING_CHAR_LENGTH (elt);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
402 if (skip < 0) continue;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
403
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
404 if (0 > scmp (charptr_n_addr (d_name, skip),
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
405 XSTRING_DATA (elt),
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
406 XSTRING_CHAR_LENGTH (elt)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
407 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
408 ignored_extension_p = 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
409 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
410 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
411 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
412 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
413 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
414
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
415 /* If an ignored-extensions match was found,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
416 don't process this name as a completion. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
417 if (!passcount && ignored_extension_p)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
418 continue;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
419
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
420 if (!passcount && regexp_ignore_completion_p (d_name, Qnil, 0, cclen))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
421 continue;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
422
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
423 /* Update computation of how much all possible completions match */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
424 matchcount++;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
425
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
426 if (all_flag || NILP (bestmatch))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
427 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
428 Lisp_Object name = Qnil;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
429 struct gcpro ngcpro1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
430 NGCPRO1 (name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
431 /* This is a possible completion */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
432 name = make_string (d_name, len);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
433 if (directoryp) /* Completion is a directory; end it with '/' */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
434 name = Ffile_name_as_directory (name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
435 if (all_flag)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
436 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
437 bestmatch = Fcons (name, bestmatch);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
438 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
439 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
440 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
441 bestmatch = name;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
442 bestmatchsize = XSTRING_CHAR_LENGTH (name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
443 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
444 NUNGCPRO;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
445 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
446 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
447 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
448 Charcount compare = min (bestmatchsize, cclen);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
449 Bufbyte *p1 = XSTRING_DATA (bestmatch);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
450 Bufbyte *p2 = d_name;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
451 Charcount matchsize = scmp (p1, p2, compare);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
452
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
453 if (matchsize < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
454 matchsize = compare;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
455 if (completion_ignore_case)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
456 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
457 /* If this is an exact match except for case,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
458 use it as the best match rather than one that is not
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
459 an exact match. This way, we get the case pattern
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
460 of the actual match. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
461 if ((matchsize == cclen
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
462 && matchsize + !!directoryp
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
463 < XSTRING_CHAR_LENGTH (bestmatch))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
464 ||
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
465 /* If there is no exact match ignoring case,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
466 prefer a match that does not change the case
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
467 of the input. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
468 (((matchsize == cclen)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
469 ==
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
470 (matchsize + !!directoryp
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
471 == XSTRING_CHAR_LENGTH (bestmatch)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
472 /* If there is more than one exact match aside from
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
473 case, and one of them is exact including case,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
474 prefer that one. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
475 && 0 > scmp_1 (p2, XSTRING_DATA (file),
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
476 file_name_length, 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
477 && 0 <= scmp_1 (p1, XSTRING_DATA (file),
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
478 file_name_length, 0)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
479 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
480 bestmatch = make_string (d_name, len);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
481 if (directoryp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
482 bestmatch = Ffile_name_as_directory (bestmatch);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
483 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
484 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
485
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
486 /* If this directory all matches,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
487 see if implicit following slash does too. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
488 if (directoryp
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
489 && compare == matchsize
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
490 && bestmatchsize > matchsize
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
491 && IS_ANY_SEP (charptr_emchar_n (p1, matchsize)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
492 matchsize++;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
493 bestmatchsize = matchsize;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
494 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
495 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
496 closedir (d);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
497 free_opaque_ptr (XCAR (locative));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
498 XCAR (locative) = Qnil;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
499 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
500
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
501 unbind_to (speccount, Qnil);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
502
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
503 UNGCPRO;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
504
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
505 if (all_flag || NILP (bestmatch))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
506 return bestmatch;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
507 if (matchcount == 1 && bestmatchsize == file_name_length)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
508 return Qt;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
509 return Fsubstring (bestmatch, Qzero, make_int (bestmatchsize));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
510 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
511
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
512
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
513
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
514 /* The *pwent() functions do not exist on NT. #### The NT equivalent
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
515 is NetUserEnum(), and rewriting to use it is not hard.*/
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
516 #ifndef WIN32_NATIVE
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
517
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
518 static Lisp_Object user_name_completion (Lisp_Object user,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
519 int all_flag,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
520 int *uniq);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
521
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
522 DEFUN ("user-name-completion", Fuser_name_completion, 1, 1, 0, /*
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
523 Complete user name from PARTIAL-USERNAME.
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
524 Return the longest prefix common to all user names starting with
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
525 PARTIAL-USERNAME. If there is only one and PARTIAL-USERNAME matches
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
526 it exactly, returns t. Return nil if there is no user name starting
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
527 with PARTIAL-USERNAME.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
528 */
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
529 (partial_username))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
530 {
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
531 return user_name_completion (partial_username, 0, NULL);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
532 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
533
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
534 DEFUN ("user-name-completion-1", Fuser_name_completion_1, 1, 1, 0, /*
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
535 Complete user name from PARTIAL-USERNAME.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
536
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
537 This function is identical to `user-name-completion', except that
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
538 the cons of the completion and an indication of whether the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
539 completion was unique is returned.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
540
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
541 The car of the returned value is the longest prefix common to all user
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
542 names that start with PARTIAL-USERNAME. If there is only one and
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
543 PARTIAL-USERNAME matches it exactly, the car is t. The car is nil if
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
544 there is no user name starting with PARTIAL-USERNAME. The cdr of the
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
545 result is non-nil if and only if the completion returned in the car
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
546 was unique.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
547 */
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
548 (partial_username))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
549 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
550 int uniq;
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
551 Lisp_Object completed = user_name_completion (partial_username, 0, &uniq);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
552 return Fcons (completed, uniq ? Qt : Qnil);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
553 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
554
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
555 DEFUN ("user-name-all-completions", Fuser_name_all_completions, 1, 1, 0, /*
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
556 Return a list of all user name completions from PARTIAL-USERNAME.
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
557 These are all the user names which begin with PARTIAL-USERNAME.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
558 */
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
559 (partial_username))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
560 {
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
561 return user_name_completion (partial_username, 1, NULL);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
562 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
563
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
564 struct user_name
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
565 {
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
566 Bufbyte *ptr;
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
567 size_t len;
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
568 };
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
569
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
570 struct user_cache
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
571 {
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
572 struct user_name *user_names;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
573 int length;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
574 int size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
575 EMACS_TIME last_rebuild_time;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
576 };
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
577 static struct user_cache user_cache;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
578
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
579 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
580 free_user_cache (struct user_cache *cache)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
581 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
582 int i;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
583 for (i = 0; i < cache->length; i++)
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
584 xfree (cache->user_names[i].ptr);
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
585 xfree (cache->user_names);
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
586 xzero (*cache);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
587 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
588
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
589 static Lisp_Object
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
590 user_name_completion_unwind (Lisp_Object cache_incomplete_p)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
591 {
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
592 endpwent ();
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
593 speed_up_interrupts ();
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
594
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
595 if (! NILP (XCAR (cache_incomplete_p)))
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
596 free_user_cache (&user_cache);
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
597
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
598 free_cons (XCONS (cache_incomplete_p));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
599
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
600 return Qnil;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
601 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
602
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
603 #define USER_CACHE_TTL (24*60*60) /* Time to live: 1 day, in seconds */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
604
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
605 static Lisp_Object
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
606 user_name_completion (Lisp_Object user, int all_flag, int *uniq)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
607 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
608 /* This function can GC */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
609 int matchcount = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
610 Lisp_Object bestmatch = Qnil;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
611 Charcount bestmatchsize = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
612 Charcount user_name_length;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
613 EMACS_TIME t;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
614 int i;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
615 struct gcpro gcpro1, gcpro2;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
616
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
617 GCPRO2 (user, bestmatch);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
618
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
619 CHECK_STRING (user);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
620
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
621 user_name_length = XSTRING_CHAR_LENGTH (user);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
622
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
623 /* Cache user name lookups because it tends to be quite slow.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
624 * Rebuild the cache occasionally to catch changes */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
625 EMACS_GET_TIME (t);
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
626 if (user_cache.user_names &&
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
627 (EMACS_SECS (t) - EMACS_SECS (user_cache.last_rebuild_time)
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
628 > USER_CACHE_TTL))
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
629 free_user_cache (&user_cache);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
630
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
631 if (!user_cache.user_names)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
632 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
633 struct passwd *pwd;
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
634 Lisp_Object cache_incomplete_p = noseeum_cons (Qt, Qnil);
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
635 int speccount = specpdl_depth ();
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
636
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
637 slow_down_interrupts ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
638 setpwent ();
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
639 record_unwind_protect (user_name_completion_unwind, cache_incomplete_p);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
640 while ((pwd = getpwent ()))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
641 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
642 QUIT;
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
643 DO_REALLOC (user_cache.user_names, user_cache.size,
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
644 user_cache.length + 1, struct user_name);
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
645 TO_INTERNAL_FORMAT (C_STRING, pwd->pw_name,
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
646 MALLOC,
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
647 (user_cache.user_names[user_cache.length].ptr,
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
648 user_cache.user_names[user_cache.length].len),
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
649 Qnative);
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
650 user_cache.length++;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
651 }
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
652 XCAR (cache_incomplete_p) = Qnil;
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
653 unbind_to (speccount, Qnil);
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
654
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
655 EMACS_GET_TIME (user_cache.last_rebuild_time);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
656 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
657
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
658 for (i = 0; i < user_cache.length; i++)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
659 {
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
660 Bufbyte *u_name = user_cache.user_names[i].ptr;
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
661 Bytecount len = user_cache.user_names[i].len;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
662 /* scmp() works in chars, not bytes, so we have to compute this: */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
663 Charcount cclen = bytecount_to_charcount (u_name, len);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
664
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
665 QUIT;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
666
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
667 if (cclen < user_name_length
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
668 || 0 <= scmp_1 (u_name, XSTRING_DATA (user), user_name_length, 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
669 continue;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
670
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
671 matchcount++; /* count matching completions */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
672
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
673 if (all_flag || NILP (bestmatch))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
674 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
675 Lisp_Object name = Qnil;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
676 struct gcpro ngcpro1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
677 NGCPRO1 (name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
678 /* This is a possible completion */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
679 name = make_string (u_name, len);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
680 if (all_flag)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
681 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
682 bestmatch = Fcons (name, bestmatch);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
683 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
684 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
685 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
686 bestmatch = name;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
687 bestmatchsize = XSTRING_CHAR_LENGTH (name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
688 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
689 NUNGCPRO;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
690 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
691 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
692 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
693 Charcount compare = min (bestmatchsize, cclen);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
694 Bufbyte *p1 = XSTRING_DATA (bestmatch);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
695 Bufbyte *p2 = u_name;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
696 Charcount matchsize = scmp_1 (p1, p2, compare, 0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
697
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
698 if (matchsize < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
699 matchsize = compare;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
700
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
701 bestmatchsize = matchsize;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
702 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
703 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
704
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
705 UNGCPRO;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
706
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
707 if (uniq)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
708 *uniq = (matchcount == 1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
709
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
710 if (all_flag || NILP (bestmatch))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
711 return bestmatch;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
712 if (matchcount == 1 && bestmatchsize == user_name_length)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
713 return Qt;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
714 return Fsubstring (bestmatch, Qzero, make_int (bestmatchsize));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
715 }
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
716 #endif /* ! defined WIN32_NATIVE */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
717
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
718
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
719 Lisp_Object
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
720 make_directory_hash_table (const char *path)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
721 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
722 DIR *d;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
723 if ((d = opendir (path)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
724 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
725 DIRENTRY *dp;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
726 Lisp_Object hash =
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
727 make_lisp_hash_table (20, HASH_TABLE_NON_WEAK, HASH_TABLE_EQUAL);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
728
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
729 while ((dp = readdir (d)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
730 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
731 Bytecount len = NAMLEN (dp);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
732 if (DIRENTRY_NONEMPTY (dp))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
733 /* Cast to Bufbyte* is OK, as readdir() Mule-encapsulates. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
734 Fputhash (make_string ((Bufbyte *) dp->d_name, len), Qt, hash);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
735 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
736 closedir (d);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
737 return hash;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
738 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
739 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
740 return Qnil;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
741 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
742
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
743 Lisp_Object
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
744 wasteful_word_to_lisp (unsigned int item)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
745 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
746 /* Compatibility: in other versions, file-attributes returns a LIST
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
747 of two 16 bit integers... */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
748 Lisp_Object cons = word_to_lisp (item);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
749 XCDR (cons) = Fcons (XCDR (cons), Qnil);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
750 return cons;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
751 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
752
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
753 DEFUN ("file-attributes", Ffile_attributes, 1, 1, 0, /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
754 Return a list of attributes of file FILENAME.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
755 Value is nil if specified file cannot be opened.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
756 Otherwise, list elements are:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
757 0. t for directory, string (name linked to) for symbolic link, or nil.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
758 1. Number of links to file.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
759 2. File uid.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
760 3. File gid.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
761 4. Last access time, as a list of two integers.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
762 First integer has high-order 16 bits of time, second has low 16 bits.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
763 5. Last modification time, likewise.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
764 6. Last status change time, likewise.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
765 7. Size in bytes. (-1, if number is out of range).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
766 8. File modes, as a string of ten letters or dashes as in ls -l.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
767 9. t iff file's gid would change if file were deleted and recreated.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
768 10. inode number.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
769 11. Device number.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
770
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
771 If file does not exist, returns nil.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
772 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
773 (filename))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
774 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
775 /* This function can GC. GC checked 1997.06.04. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
776 Lisp_Object values[12];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
777 Lisp_Object directory = Qnil;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
778 struct stat s;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
779 char modes[10];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
780 Lisp_Object handler;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
781 struct gcpro gcpro1, gcpro2;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
782
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
783 GCPRO2 (filename, directory);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
784 filename = Fexpand_file_name (filename, Qnil);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
785
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
786 /* If the file name has special constructs in it,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
787 call the corresponding file handler. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
788 handler = Ffind_file_name_handler (filename, Qfile_attributes);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
789 if (!NILP (handler))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
790 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
791 UNGCPRO;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
792 return call2 (handler, Qfile_attributes, filename);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
793 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
794
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
795 if (lstat ((char *) XSTRING_DATA (filename), &s) < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
796 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
797 UNGCPRO;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
798 return Qnil;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
799 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
800
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
801 #ifdef BSD4_2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
802 directory = Ffile_name_directory (filename);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
803 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
804
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
805 #if 0 /* #### shouldn't this apply to WIN32_NATIVE and maybe CYGWIN? */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
806 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
807 char *tmpnam = (char *) XSTRING_DATA (Ffile_name_nondirectory (filename));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
808 int l = strlen (tmpnam);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
809
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
810 if (l >= 5
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
811 && S_ISREG (s.st_mode)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
812 && (stricmp (&tmpnam[l - 4], ".com") == 0 ||
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
813 stricmp (&tmpnam[l - 4], ".exe") == 0 ||
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
814 stricmp (&tmpnam[l - 4], ".bat") == 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
815 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
816 s.st_mode |= S_IEXEC;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
817 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
818 }
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
819 #endif
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
820
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
821 switch (s.st_mode & S_IFMT)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
822 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
823 default:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
824 values[0] = Qnil;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
825 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
826 case S_IFDIR:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
827 values[0] = Qt;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
828 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
829 #ifdef S_IFLNK
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
830 case S_IFLNK:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
831 values[0] = Ffile_symlink_p (filename);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
832 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
833 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
834 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
835 values[1] = make_int (s.st_nlink);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
836 values[2] = make_int (s.st_uid);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
837 values[3] = make_int (s.st_gid);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
838 values[4] = wasteful_word_to_lisp (s.st_atime);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
839 values[5] = wasteful_word_to_lisp (s.st_mtime);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
840 values[6] = wasteful_word_to_lisp (s.st_ctime);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
841 values[7] = make_int ((EMACS_INT) s.st_size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
842 /* If the size is out of range, give back -1. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
843 /* #### Fix when Emacs gets bignums! */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
844 if (XINT (values[7]) != s.st_size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
845 values[7] = make_int (-1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
846 filemodestring (&s, modes);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
847 values[8] = make_string ((Bufbyte *) modes, 10);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
848 #if defined (BSD4_2) || defined (BSD4_3) /* file gid will be dir gid */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
849 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
850 struct stat sdir;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
851
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
852 if (!NILP (directory) && xemacs_stat ((char *) XSTRING_DATA (directory), &sdir) == 0)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
853 values[9] = (sdir.st_gid != s.st_gid) ? Qt : Qnil;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
854 else /* if we can't tell, assume worst */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
855 values[9] = Qt;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
856 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
857 #else /* file gid will be egid */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
858 values[9] = (s.st_gid != getegid ()) ? Qt : Qnil;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
859 #endif /* BSD4_2 or BSD4_3 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
860 values[10] = make_int (s.st_ino);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
861 values[11] = make_int (s.st_dev);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
862 UNGCPRO;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
863 return Flist (countof (values), values);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
864 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
865
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
866
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
867 /************************************************************************/
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
868 /* initialization */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
869 /************************************************************************/
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
870
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
871 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
872 syms_of_dired (void)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
873 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
874 defsymbol (&Qdirectory_files, "directory-files");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
875 defsymbol (&Qfile_name_completion, "file-name-completion");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
876 defsymbol (&Qfile_name_all_completions, "file-name-all-completions");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
877 defsymbol (&Qfile_attributes, "file-attributes");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
878
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
879 DEFSUBR (Fdirectory_files);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
880 DEFSUBR (Ffile_name_completion);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
881 DEFSUBR (Ffile_name_all_completions);
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
882 #ifndef WIN32_NATIVE
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
883 DEFSUBR (Fuser_name_completion);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
884 DEFSUBR (Fuser_name_completion_1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
885 DEFSUBR (Fuser_name_all_completions);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
886 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
887 DEFSUBR (Ffile_attributes);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
888 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
889
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
890 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
891 vars_of_dired (void)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
892 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
893 DEFVAR_LISP ("completion-ignored-extensions", &Vcompletion_ignored_extensions /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
894 *Completion ignores filenames ending in any string in this list.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
895 This variable does not affect lists of possible completions,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
896 but does affect the commands that actually do completions.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
897 It is used by the functions `file-name-completion' and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
898 `file-name-all-completions'.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
899 */ );
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
900 Vcompletion_ignored_extensions = Qnil;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
901 }