annotate src/dired.c @ 377:d883f39b8495 r21-2b4

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