annotate src/dired.c @ 384:bbff43aa5eb7 r21-2-7

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