Commit | Line | Data |
---|---|---|
570d7624 | 1 | /* File IO for GNU Emacs. |
854afafc | 2 | Copyright (C) 1985,86,87,88,93,94,95,96,97,98,99,2000,01,2003 |
43fb7d9a | 3 | Free Software Foundation, Inc. |
570d7624 JB |
4 | |
5 | This file is part of GNU Emacs. | |
6 | ||
7 | GNU Emacs is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
4746118a | 9 | the Free Software Foundation; either version 2, or (at your option) |
570d7624 JB |
10 | any later version. |
11 | ||
12 | GNU Emacs is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GNU Emacs; see the file COPYING. If not, write to | |
3b7ad313 EN |
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
20 | Boston, MA 02111-1307, USA. */ | |
570d7624 | 21 | |
18160b98 | 22 | #include <config.h> |
570d7624 | 23 | |
612221ab | 24 | #ifdef HAVE_FCNTL_H |
bb369dc6 RS |
25 | #include <fcntl.h> |
26 | #endif | |
27 | ||
1b335d29 | 28 | #include <stdio.h> |
570d7624 JB |
29 | #include <sys/types.h> |
30 | #include <sys/stat.h> | |
bfb61299 | 31 | |
29beb080 RS |
32 | #ifdef HAVE_UNISTD_H |
33 | #include <unistd.h> | |
34 | #endif | |
35 | ||
f73b0ada BF |
36 | #if !defined (S_ISLNK) && defined (S_IFLNK) |
37 | # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) | |
38 | #endif | |
39 | ||
bb369dc6 RS |
40 | #if !defined (S_ISFIFO) && defined (S_IFIFO) |
41 | # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO) | |
42 | #endif | |
43 | ||
f73b0ada BF |
44 | #if !defined (S_ISREG) && defined (S_IFREG) |
45 | # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) | |
46 | #endif | |
47 | ||
bfb61299 | 48 | #ifdef VMS |
de5bf5d3 | 49 | #include "vms-pwd.h" |
bfb61299 | 50 | #else |
570d7624 | 51 | #include <pwd.h> |
bfb61299 JB |
52 | #endif |
53 | ||
570d7624 | 54 | #include <ctype.h> |
bfb61299 JB |
55 | |
56 | #ifdef VMS | |
3d9f5ce2 | 57 | #include "vmsdir.h" |
bfb61299 JB |
58 | #include <perror.h> |
59 | #include <stddef.h> | |
60 | #include <string.h> | |
bfb61299 JB |
61 | #endif |
62 | ||
570d7624 JB |
63 | #include <errno.h> |
64 | ||
bfb61299 | 65 | #ifndef vax11c |
f12ef5eb | 66 | #ifndef USE_CRT_DLL |
570d7624 | 67 | extern int errno; |
570d7624 | 68 | #endif |
f12ef5eb | 69 | #endif |
570d7624 | 70 | |
570d7624 JB |
71 | #ifdef APOLLO |
72 | #include <sys/time.h> | |
73 | #endif | |
74 | ||
570d7624 | 75 | #include "lisp.h" |
8d4e077b | 76 | #include "intervals.h" |
570d7624 | 77 | #include "buffer.h" |
6fdaa9a0 KH |
78 | #include "charset.h" |
79 | #include "coding.h" | |
570d7624 JB |
80 | #include "window.h" |
81 | ||
5e570b75 RS |
82 | #ifdef WINDOWSNT |
83 | #define NOMINMAX 1 | |
84 | #include <windows.h> | |
85 | #include <stdlib.h> | |
86 | #include <fcntl.h> | |
87 | #endif /* not WINDOWSNT */ | |
88 | ||
7990d02a EZ |
89 | #ifdef MSDOS |
90 | #include "msdos.h" | |
91 | #include <sys/param.h> | |
92 | #if __DJGPP__ >= 2 | |
93 | #include <fcntl.h> | |
94 | #include <string.h> | |
95 | #endif | |
96 | #endif | |
97 | ||
199607e4 RS |
98 | #ifdef DOS_NT |
99 | #define CORRECT_DIR_SEPS(s) \ | |
100 | do { if ('/' == DIRECTORY_SEP) dostounix_filename (s); \ | |
101 | else unixtodos_filename (s); \ | |
102 | } while (0) | |
103 | /* On Windows, drive letters must be alphabetic - on DOS, the Netware | |
104 | redirector allows the six letters between 'Z' and 'a' as well. */ | |
105 | #ifdef MSDOS | |
106 | #define IS_DRIVE(x) ((x) >= 'A' && (x) <= 'z') | |
107 | #endif | |
108 | #ifdef WINDOWSNT | |
109 | #define IS_DRIVE(x) isalpha (x) | |
110 | #endif | |
f54b565c MB |
111 | /* Need to lower-case the drive letter, or else expanded |
112 | filenames will sometimes compare inequal, because | |
113 | `expand-file-name' doesn't always down-case the drive letter. */ | |
114 | #define DRIVE_LETTER(x) (tolower (x)) | |
199607e4 RS |
115 | #endif |
116 | ||
570d7624 | 117 | #ifdef VMS |
570d7624 JB |
118 | #include <file.h> |
119 | #include <rmsdef.h> | |
120 | #include <fab.h> | |
121 | #include <nam.h> | |
122 | #endif | |
123 | ||
de5bf5d3 | 124 | #include "systime.h" |
570d7624 JB |
125 | |
126 | #ifdef HPUX | |
127 | #include <netio.h> | |
9b7828a5 | 128 | #ifndef HPUX8 |
47e7b9e5 | 129 | #ifndef HPUX9 |
570d7624 JB |
130 | #include <errnet.h> |
131 | #endif | |
9b7828a5 | 132 | #endif |
47e7b9e5 | 133 | #endif |
570d7624 | 134 | |
9c856db9 GM |
135 | #include "commands.h" |
136 | extern int use_dialog_box; | |
137 | ||
570d7624 JB |
138 | #ifndef O_WRONLY |
139 | #define O_WRONLY 1 | |
140 | #endif | |
141 | ||
4018b5ef RS |
142 | #ifndef O_RDONLY |
143 | #define O_RDONLY 0 | |
144 | #endif | |
145 | ||
8f6df9b5 EZ |
146 | #ifndef S_ISLNK |
147 | # define lstat stat | |
148 | #endif | |
149 | ||
570d7624 JB |
150 | /* Nonzero during writing of auto-save files */ |
151 | int auto_saving; | |
152 | ||
153 | /* Set by auto_save_1 to mode of original file so Fwrite_region will create | |
154 | a new file with the same mode as the original */ | |
155 | int auto_save_mode_bits; | |
156 | ||
356a6224 KH |
157 | /* The symbol bound to coding-system-for-read when |
158 | insert-file-contents is called for recovering a file. This is not | |
159 | an actual coding system name, but just an indicator to tell | |
160 | insert-file-contents to use `emacs-mule' with a special flag for | |
161 | auto saving and recovering a file. */ | |
162 | Lisp_Object Qauto_save_coding; | |
163 | ||
b1d1b865 RS |
164 | /* Coding system for file names, or nil if none. */ |
165 | Lisp_Object Vfile_name_coding_system; | |
166 | ||
cd913586 KH |
167 | /* Coding system for file names used only when |
168 | Vfile_name_coding_system is nil. */ | |
169 | Lisp_Object Vdefault_file_name_coding_system; | |
170 | ||
199607e4 | 171 | /* Alist of elements (REGEXP . HANDLER) for file names |
32f4334d RS |
172 | whose I/O is done with a special handler. */ |
173 | Lisp_Object Vfile_name_handler_alist; | |
174 | ||
0d420e88 BG |
175 | /* Format for auto-save files */ |
176 | Lisp_Object Vauto_save_file_format; | |
177 | ||
178 | /* Lisp functions for translating file formats */ | |
179 | Lisp_Object Qformat_decode, Qformat_annotate_function; | |
180 | ||
c9e82392 | 181 | /* Function to be called to decide a coding system of a reading file. */ |
0414b394 | 182 | Lisp_Object Vset_auto_coding_function; |
c9e82392 | 183 | |
d6a3cc15 RS |
184 | /* Functions to be called to process text properties in inserted file. */ |
185 | Lisp_Object Vafter_insert_file_functions; | |
186 | ||
2080470e | 187 | /* Lisp function for setting buffer-file-coding-system and the |
b6426b03 | 188 | multibyteness of the current buffer after inserting a file. */ |
2080470e | 189 | Lisp_Object Qafter_insert_file_set_coding; |
b6426b03 | 190 | |
d6a3cc15 RS |
191 | /* Functions to be called to create text property annotations for file. */ |
192 | Lisp_Object Vwrite_region_annotate_functions; | |
bd235610 | 193 | Lisp_Object Qwrite_region_annotate_functions; |
d6a3cc15 | 194 | |
6fc6f94b RS |
195 | /* During build_annotations, each time an annotation function is called, |
196 | this holds the annotations made by the previous functions. */ | |
197 | Lisp_Object Vwrite_region_annotations_so_far; | |
198 | ||
e54d3b5d RS |
199 | /* File name in which we write a list of all our auto save files. */ |
200 | Lisp_Object Vauto_save_list_file_name; | |
201 | ||
59ffe07d | 202 | /* Function to call to read a file name. */ |
efdc16c9 | 203 | Lisp_Object Vread_file_name_function; |
59ffe07d KS |
204 | |
205 | /* Current predicate used by read_file_name_internal. */ | |
206 | Lisp_Object Vread_file_name_predicate; | |
207 | ||
570d7624 JB |
208 | /* Nonzero means, when reading a filename in the minibuffer, |
209 | start out by inserting the default directory into the minibuffer. */ | |
210 | int insert_default_directory; | |
211 | ||
212 | /* On VMS, nonzero means write new files with record format stmlf. | |
213 | Zero means use var format. */ | |
214 | int vms_stmlf_recfm; | |
215 | ||
199607e4 RS |
216 | /* On NT, specifies the directory separator character, used (eg.) when |
217 | expanding file names. This can be bound to / or \. */ | |
218 | Lisp_Object Vdirectory_sep_char; | |
219 | ||
84f6296a RS |
220 | extern Lisp_Object Vuser_login_name; |
221 | ||
c1c4693e RS |
222 | #ifdef WINDOWSNT |
223 | extern Lisp_Object Vw32_get_true_file_attributes; | |
224 | #endif | |
225 | ||
84f6296a RS |
226 | extern int minibuf_level; |
227 | ||
a8c828be RS |
228 | extern int minibuffer_auto_raise; |
229 | ||
a65970a0 RS |
230 | /* These variables describe handlers that have "already" had a chance |
231 | to handle the current operation. | |
232 | ||
233 | Vinhibit_file_name_handlers is a list of file name handlers. | |
234 | Vinhibit_file_name_operation is the operation being handled. | |
235 | If we try to handle that operation, we ignore those handlers. */ | |
236 | ||
82c2d839 | 237 | static Lisp_Object Vinhibit_file_name_handlers; |
a65970a0 | 238 | static Lisp_Object Vinhibit_file_name_operation; |
82c2d839 | 239 | |
c0b7b21c | 240 | Lisp_Object Qfile_error, Qfile_already_exists, Qfile_date_error; |
505ab9bc | 241 | Lisp_Object Qexcl; |
15c65264 RS |
242 | Lisp_Object Qfile_name_history; |
243 | ||
d6a3cc15 RS |
244 | Lisp_Object Qcar_less_than_car; |
245 | ||
ce51c54c | 246 | static int a_write P_ ((int, Lisp_Object, int, int, |
b9013200 | 247 | Lisp_Object *, struct coding_system *)); |
ce51c54c KH |
248 | static int e_write P_ ((int, Lisp_Object, int, int, struct coding_system *)); |
249 | ||
ec7adf26 | 250 | \f |
5d01d666 | 251 | void |
570d7624 | 252 | report_file_error (string, data) |
19290c65 | 253 | const char *string; |
570d7624 JB |
254 | Lisp_Object data; |
255 | { | |
256 | Lisp_Object errstring; | |
505ab9bc | 257 | int errorno = errno; |
570d7624 | 258 | |
ca9c0567 | 259 | synchronize_system_messages_locale (); |
68c45bf0 PE |
260 | errstring = code_convert_string_norecord (build_string (strerror (errorno)), |
261 | Vlocale_coding_system, 0); | |
262 | ||
570d7624 | 263 | while (1) |
505ab9bc RS |
264 | switch (errorno) |
265 | { | |
266 | case EEXIST: | |
267 | Fsignal (Qfile_already_exists, Fcons (errstring, data)); | |
268 | break; | |
269 | default: | |
270 | /* System error messages are capitalized. Downcase the initial | |
271 | unless it is followed by a slash. */ | |
d5db4077 | 272 | if (SREF (errstring, 1) != '/') |
942dc838 | 273 | SSET (errstring, 0, DOWNCASE (SREF (errstring, 0))); |
505ab9bc RS |
274 | |
275 | Fsignal (Qfile_error, | |
276 | Fcons (build_string (string), Fcons (errstring, data))); | |
277 | } | |
570d7624 | 278 | } |
b5148e85 | 279 | |
b27a1703 | 280 | Lisp_Object |
b5148e85 RS |
281 | close_file_unwind (fd) |
282 | Lisp_Object fd; | |
283 | { | |
68c45bf0 | 284 | emacs_close (XFASTINT (fd)); |
b27a1703 | 285 | return Qnil; |
b5148e85 | 286 | } |
a1d2b64a RS |
287 | |
288 | /* Restore point, having saved it as a marker. */ | |
289 | ||
ec7adf26 | 290 | static Lisp_Object |
a1d2b64a | 291 | restore_point_unwind (location) |
199607e4 | 292 | Lisp_Object location; |
a1d2b64a | 293 | { |
ec7adf26 | 294 | Fgoto_char (location); |
a1d2b64a | 295 | Fset_marker (location, Qnil, Qnil); |
b27a1703 | 296 | return Qnil; |
a1d2b64a | 297 | } |
570d7624 | 298 | \f |
0bf2eed2 | 299 | Lisp_Object Qexpand_file_name; |
273e0829 | 300 | Lisp_Object Qsubstitute_in_file_name; |
0bf2eed2 RS |
301 | Lisp_Object Qdirectory_file_name; |
302 | Lisp_Object Qfile_name_directory; | |
303 | Lisp_Object Qfile_name_nondirectory; | |
642ef245 | 304 | Lisp_Object Qunhandled_file_name_directory; |
0bf2eed2 | 305 | Lisp_Object Qfile_name_as_directory; |
32f4334d | 306 | Lisp_Object Qcopy_file; |
a6e6e718 | 307 | Lisp_Object Qmake_directory_internal; |
b272d624 | 308 | Lisp_Object Qmake_directory; |
32f4334d RS |
309 | Lisp_Object Qdelete_directory; |
310 | Lisp_Object Qdelete_file; | |
311 | Lisp_Object Qrename_file; | |
312 | Lisp_Object Qadd_name_to_file; | |
313 | Lisp_Object Qmake_symbolic_link; | |
314 | Lisp_Object Qfile_exists_p; | |
315 | Lisp_Object Qfile_executable_p; | |
316 | Lisp_Object Qfile_readable_p; | |
32f4334d | 317 | Lisp_Object Qfile_writable_p; |
1f8653eb RS |
318 | Lisp_Object Qfile_symlink_p; |
319 | Lisp_Object Qaccess_file; | |
32f4334d | 320 | Lisp_Object Qfile_directory_p; |
adedc71d | 321 | Lisp_Object Qfile_regular_p; |
32f4334d RS |
322 | Lisp_Object Qfile_accessible_directory_p; |
323 | Lisp_Object Qfile_modes; | |
324 | Lisp_Object Qset_file_modes; | |
325 | Lisp_Object Qfile_newer_than_file_p; | |
326 | Lisp_Object Qinsert_file_contents; | |
327 | Lisp_Object Qwrite_region; | |
328 | Lisp_Object Qverify_visited_file_modtime; | |
3ec46acd | 329 | Lisp_Object Qset_visited_file_modtime; |
32f4334d | 330 | |
49307295 | 331 | DEFUN ("find-file-name-handler", Ffind_file_name_handler, Sfind_file_name_handler, 2, 2, 0, |
8c1a1077 PJ |
332 | doc: /* Return FILENAME's handler function for OPERATION, if it has one. |
333 | Otherwise, return nil. | |
334 | A file name is handled if one of the regular expressions in | |
335 | `file-name-handler-alist' matches it. | |
336 | ||
337 | If OPERATION equals `inhibit-file-name-operation', then we ignore | |
338 | any handlers that are members of `inhibit-file-name-handlers', | |
339 | but we still do run any other handlers. This lets handlers | |
340 | use the standard functions without calling themselves recursively. */) | |
341 | (filename, operation) | |
342 | Lisp_Object filename, operation; | |
32f4334d | 343 | { |
642ef245 | 344 | /* This function must not munge the match data. */ |
204ee271 | 345 | Lisp_Object chain, inhibited_handlers, result; |
8d2ced53 | 346 | int pos = -1; |
642ef245 | 347 | |
204ee271 | 348 | result = Qnil; |
b7826503 | 349 | CHECK_STRING (filename); |
e4432095 | 350 | |
a65970a0 RS |
351 | if (EQ (operation, Vinhibit_file_name_operation)) |
352 | inhibited_handlers = Vinhibit_file_name_handlers; | |
353 | else | |
354 | inhibited_handlers = Qnil; | |
82c2d839 | 355 | |
93c30b5f | 356 | for (chain = Vfile_name_handler_alist; CONSP (chain); |
03699b14 | 357 | chain = XCDR (chain)) |
32f4334d RS |
358 | { |
359 | Lisp_Object elt; | |
03699b14 | 360 | elt = XCAR (chain); |
93c30b5f | 361 | if (CONSP (elt)) |
32f4334d RS |
362 | { |
363 | Lisp_Object string; | |
8d2ced53 | 364 | int match_pos; |
03699b14 | 365 | string = XCAR (elt); |
8d2ced53 SM |
366 | if (STRINGP (string) |
367 | && (match_pos = fast_string_match (string, filename)) > pos) | |
a65970a0 RS |
368 | { |
369 | Lisp_Object handler, tem; | |
370 | ||
03699b14 | 371 | handler = XCDR (elt); |
a65970a0 RS |
372 | tem = Fmemq (handler, inhibited_handlers); |
373 | if (NILP (tem)) | |
8d2ced53 SM |
374 | { |
375 | result = handler; | |
376 | pos = match_pos; | |
377 | } | |
a65970a0 | 378 | } |
32f4334d | 379 | } |
642ef245 JB |
380 | |
381 | QUIT; | |
32f4334d | 382 | } |
8d2ced53 | 383 | return result; |
32f4334d RS |
384 | } |
385 | \f | |
570d7624 | 386 | DEFUN ("file-name-directory", Ffile_name_directory, Sfile_name_directory, |
8c1a1077 PJ |
387 | 1, 1, 0, |
388 | doc: /* Return the directory component in file name FILENAME. | |
389 | Return nil if FILENAME does not include a directory. | |
390 | Otherwise return a directory spec. | |
391 | Given a Unix syntax file name, returns a string ending in slash; | |
392 | on VMS, perhaps instead a string ending in `:', `]' or `>'. */) | |
393 | (filename) | |
3b7f6e60 | 394 | Lisp_Object filename; |
570d7624 | 395 | { |
100c44b7 | 396 | #ifndef DOS_NT |
19290c65 | 397 | register const unsigned char *beg; |
100c44b7 EZ |
398 | #else |
399 | register unsigned char *beg; | |
400 | #endif | |
19290c65 | 401 | register const unsigned char *p; |
0bf2eed2 | 402 | Lisp_Object handler; |
570d7624 | 403 | |
b7826503 | 404 | CHECK_STRING (filename); |
570d7624 | 405 | |
0bf2eed2 RS |
406 | /* If the file name has special constructs in it, |
407 | call the corresponding file handler. */ | |
3b7f6e60 | 408 | handler = Ffind_file_name_handler (filename, Qfile_name_directory); |
0bf2eed2 | 409 | if (!NILP (handler)) |
3b7f6e60 | 410 | return call2 (handler, Qfile_name_directory, filename); |
0bf2eed2 | 411 | |
4c3c22f3 | 412 | #ifdef FILE_SYSTEM_CASE |
3b7f6e60 | 413 | filename = FILE_SYSTEM_CASE (filename); |
4c3c22f3 | 414 | #endif |
d5db4077 | 415 | beg = SDATA (filename); |
199607e4 RS |
416 | #ifdef DOS_NT |
417 | beg = strcpy (alloca (strlen (beg) + 1), beg); | |
418 | #endif | |
d5db4077 | 419 | p = beg + SBYTES (filename); |
570d7624 | 420 | |
199607e4 | 421 | while (p != beg && !IS_DIRECTORY_SEP (p[-1]) |
570d7624 JB |
422 | #ifdef VMS |
423 | && p[-1] != ':' && p[-1] != ']' && p[-1] != '>' | |
424 | #endif /* VMS */ | |
199607e4 | 425 | #ifdef DOS_NT |
ba14e174 RS |
426 | /* only recognise drive specifier at the beginning */ |
427 | && !(p[-1] == ':' | |
428 | /* handle the "/:d:foo" and "/:foo" cases correctly */ | |
429 | && ((p == beg + 2 && !IS_DIRECTORY_SEP (*beg)) | |
430 | || (p == beg + 4 && IS_DIRECTORY_SEP (*beg)))) | |
199607e4 | 431 | #endif |
570d7624 JB |
432 | ) p--; |
433 | ||
434 | if (p == beg) | |
435 | return Qnil; | |
5e570b75 | 436 | #ifdef DOS_NT |
4c3c22f3 | 437 | /* Expansion of "c:" to drive and default directory. */ |
ba14e174 | 438 | if (p[-1] == ':') |
4c3c22f3 | 439 | { |
4c3c22f3 | 440 | /* MAXPATHLEN+1 is guaranteed to be enough space for getdefdir. */ |
199607e4 | 441 | unsigned char *res = alloca (MAXPATHLEN + 1); |
ba14e174 RS |
442 | unsigned char *r = res; |
443 | ||
444 | if (p == beg + 4 && IS_DIRECTORY_SEP (*beg) && beg[1] == ':') | |
445 | { | |
446 | strncpy (res, beg, 2); | |
447 | beg += 2; | |
448 | r += 2; | |
449 | } | |
450 | ||
451 | if (getdefdir (toupper (*beg) - 'A' + 1, r)) | |
4c3c22f3 | 452 | { |
199607e4 | 453 | if (!IS_DIRECTORY_SEP (res[strlen (res) - 1])) |
4c3c22f3 RS |
454 | strcat (res, "/"); |
455 | beg = res; | |
456 | p = beg + strlen (beg); | |
457 | } | |
458 | } | |
199607e4 | 459 | CORRECT_DIR_SEPS (beg); |
5e570b75 | 460 | #endif /* DOS_NT */ |
60d67b83 | 461 | |
d7231f93 | 462 | return make_specified_string (beg, -1, p - beg, STRING_MULTIBYTE (filename)); |
570d7624 JB |
463 | } |
464 | ||
60d67b83 RS |
465 | DEFUN ("file-name-nondirectory", Ffile_name_nondirectory, |
466 | Sfile_name_nondirectory, 1, 1, 0, | |
8c1a1077 PJ |
467 | doc: /* Return file name FILENAME sans its directory. |
468 | For example, in a Unix-syntax file name, | |
469 | this is everything after the last slash, | |
470 | or the entire name if it contains no slash. */) | |
471 | (filename) | |
3b7f6e60 | 472 | Lisp_Object filename; |
570d7624 | 473 | { |
19290c65 | 474 | register const unsigned char *beg, *p, *end; |
0bf2eed2 | 475 | Lisp_Object handler; |
570d7624 | 476 | |
b7826503 | 477 | CHECK_STRING (filename); |
570d7624 | 478 | |
0bf2eed2 RS |
479 | /* If the file name has special constructs in it, |
480 | call the corresponding file handler. */ | |
3b7f6e60 | 481 | handler = Ffind_file_name_handler (filename, Qfile_name_nondirectory); |
0bf2eed2 | 482 | if (!NILP (handler)) |
3b7f6e60 | 483 | return call2 (handler, Qfile_name_nondirectory, filename); |
0bf2eed2 | 484 | |
d5db4077 KR |
485 | beg = SDATA (filename); |
486 | end = p = beg + SBYTES (filename); | |
570d7624 | 487 | |
199607e4 | 488 | while (p != beg && !IS_DIRECTORY_SEP (p[-1]) |
570d7624 JB |
489 | #ifdef VMS |
490 | && p[-1] != ':' && p[-1] != ']' && p[-1] != '>' | |
491 | #endif /* VMS */ | |
199607e4 RS |
492 | #ifdef DOS_NT |
493 | /* only recognise drive specifier at beginning */ | |
ba14e174 RS |
494 | && !(p[-1] == ':' |
495 | /* handle the "/:d:foo" case correctly */ | |
496 | && (p == beg + 2 || (p == beg + 4 && IS_DIRECTORY_SEP (*beg)))) | |
199607e4 | 497 | #endif |
60d67b83 RS |
498 | ) |
499 | p--; | |
570d7624 | 500 | |
d7231f93 | 501 | return make_specified_string (p, -1, end - p, STRING_MULTIBYTE (filename)); |
570d7624 | 502 | } |
642ef245 | 503 | |
60d67b83 RS |
504 | DEFUN ("unhandled-file-name-directory", Funhandled_file_name_directory, |
505 | Sunhandled_file_name_directory, 1, 1, 0, | |
8c1a1077 PJ |
506 | doc: /* Return a directly usable directory name somehow associated with FILENAME. |
507 | A `directly usable' directory name is one that may be used without the | |
508 | intervention of any file handler. | |
509 | If FILENAME is a directly usable file itself, return | |
510 | \(file-name-directory FILENAME). | |
511 | The `call-process' and `start-process' functions use this function to | |
512 | get a current directory to run processes in. */) | |
513 | (filename) | |
514 | Lisp_Object filename; | |
642ef245 JB |
515 | { |
516 | Lisp_Object handler; | |
517 | ||
518 | /* If the file name has special constructs in it, | |
519 | call the corresponding file handler. */ | |
49307295 | 520 | handler = Ffind_file_name_handler (filename, Qunhandled_file_name_directory); |
642ef245 JB |
521 | if (!NILP (handler)) |
522 | return call2 (handler, Qunhandled_file_name_directory, filename); | |
523 | ||
524 | return Ffile_name_directory (filename); | |
525 | } | |
526 | ||
570d7624 JB |
527 | \f |
528 | char * | |
529 | file_name_as_directory (out, in) | |
530 | char *out, *in; | |
531 | { | |
532 | int size = strlen (in) - 1; | |
533 | ||
534 | strcpy (out, in); | |
535 | ||
8aa3a244 RS |
536 | if (size < 0) |
537 | { | |
154a307d KH |
538 | out[0] = '.'; |
539 | out[1] = '/'; | |
540 | out[2] = 0; | |
8aa3a244 RS |
541 | return out; |
542 | } | |
543 | ||
570d7624 JB |
544 | #ifdef VMS |
545 | /* Is it already a directory string? */ | |
546 | if (in[size] == ':' || in[size] == ']' || in[size] == '>') | |
547 | return out; | |
548 | /* Is it a VMS directory file name? If so, hack VMS syntax. */ | |
549 | else if (! index (in, '/') | |
550 | && ((size > 3 && ! strcmp (&in[size - 3], ".DIR")) | |
551 | || (size > 3 && ! strcmp (&in[size - 3], ".dir")) | |
552 | || (size > 5 && (! strncmp (&in[size - 5], ".DIR", 4) | |
553 | || ! strncmp (&in[size - 5], ".dir", 4)) | |
554 | && (in[size - 1] == '.' || in[size - 1] == ';') | |
555 | && in[size] == '1'))) | |
556 | { | |
557 | register char *p, *dot; | |
558 | char brack; | |
559 | ||
560 | /* x.dir -> [.x] | |
561 | dir:x.dir --> dir:[x] | |
562 | dir:[x]y.dir --> dir:[x.y] */ | |
563 | p = in + size; | |
564 | while (p != in && *p != ':' && *p != '>' && *p != ']') p--; | |
565 | if (p != in) | |
566 | { | |
567 | strncpy (out, in, p - in); | |
568 | out[p - in] = '\0'; | |
569 | if (*p == ':') | |
570 | { | |
571 | brack = ']'; | |
572 | strcat (out, ":["); | |
573 | } | |
574 | else | |
575 | { | |
576 | brack = *p; | |
577 | strcat (out, "."); | |
578 | } | |
579 | p++; | |
580 | } | |
581 | else | |
582 | { | |
583 | brack = ']'; | |
584 | strcpy (out, "[."); | |
585 | } | |
bfb61299 JB |
586 | dot = index (p, '.'); |
587 | if (dot) | |
570d7624 JB |
588 | { |
589 | /* blindly remove any extension */ | |
590 | size = strlen (out) + (dot - p); | |
591 | strncat (out, p, dot - p); | |
592 | } | |
593 | else | |
594 | { | |
595 | strcat (out, p); | |
596 | size = strlen (out); | |
597 | } | |
598 | out[size++] = brack; | |
599 | out[size] = '\0'; | |
600 | } | |
601 | #else /* not VMS */ | |
602 | /* For Unix syntax, Append a slash if necessary */ | |
199607e4 | 603 | if (!IS_DIRECTORY_SEP (out[size])) |
5e570b75 | 604 | { |
8cfd6446 JB |
605 | /* Cannot use DIRECTORY_SEP, which could have any value */ |
606 | out[size + 1] = '/'; | |
5e570b75 RS |
607 | out[size + 2] = '\0'; |
608 | } | |
199607e4 RS |
609 | #ifdef DOS_NT |
610 | CORRECT_DIR_SEPS (out); | |
611 | #endif | |
570d7624 JB |
612 | #endif /* not VMS */ |
613 | return out; | |
614 | } | |
615 | ||
616 | DEFUN ("file-name-as-directory", Ffile_name_as_directory, | |
617 | Sfile_name_as_directory, 1, 1, 0, | |
0dac4f85 | 618 | doc: /* Return a string representing the file name FILE interpreted as a directory. |
8c1a1077 PJ |
619 | This operation exists because a directory is also a file, but its name as |
620 | a directory is different from its name as a file. | |
621 | The result can be used as the value of `default-directory' | |
622 | or passed as second argument to `expand-file-name'. | |
623 | For a Unix-syntax file name, just appends a slash. | |
624 | On VMS, converts \"[X]FOO.DIR\" to \"[X.FOO]\", etc. */) | |
625 | (file) | |
570d7624 JB |
626 | Lisp_Object file; |
627 | { | |
628 | char *buf; | |
0bf2eed2 | 629 | Lisp_Object handler; |
570d7624 | 630 | |
b7826503 | 631 | CHECK_STRING (file); |
265a9e55 | 632 | if (NILP (file)) |
570d7624 | 633 | return Qnil; |
0bf2eed2 RS |
634 | |
635 | /* If the file name has special constructs in it, | |
636 | call the corresponding file handler. */ | |
49307295 | 637 | handler = Ffind_file_name_handler (file, Qfile_name_as_directory); |
0bf2eed2 RS |
638 | if (!NILP (handler)) |
639 | return call2 (handler, Qfile_name_as_directory, file); | |
640 | ||
d5db4077 | 641 | buf = (char *) alloca (SBYTES (file) + 10); |
d7231f93 KH |
642 | file_name_as_directory (buf, SDATA (file)); |
643 | return make_specified_string (buf, -1, strlen (buf), | |
644 | STRING_MULTIBYTE (file)); | |
570d7624 JB |
645 | } |
646 | \f | |
647 | /* | |
648 | * Convert from directory name to filename. | |
649 | * On VMS: | |
650 | * xyzzy:[mukesh.emacs] => xyzzy:[mukesh]emacs.dir.1 | |
651 | * xyzzy:[mukesh] => xyzzy:[000000]mukesh.dir.1 | |
199607e4 | 652 | * On UNIX, it's simple: just make sure there isn't a terminating / |
570d7624 JB |
653 | |
654 | * Value is nonzero if the string output is different from the input. | |
655 | */ | |
656 | ||
dfcf069d | 657 | int |
570d7624 JB |
658 | directory_file_name (src, dst) |
659 | char *src, *dst; | |
660 | { | |
661 | long slen; | |
662 | #ifdef VMS | |
663 | long rlen; | |
664 | char * ptr, * rptr; | |
665 | char bracket; | |
666 | struct FAB fab = cc$rms_fab; | |
667 | struct NAM nam = cc$rms_nam; | |
668 | char esa[NAM$C_MAXRSS]; | |
669 | #endif /* VMS */ | |
670 | ||
671 | slen = strlen (src); | |
672 | #ifdef VMS | |
673 | if (! index (src, '/') | |
674 | && (src[slen - 1] == ']' | |
675 | || src[slen - 1] == ':' | |
676 | || src[slen - 1] == '>')) | |
677 | { | |
678 | /* VMS style - convert [x.y.z] to [x.y]z, [x] to [000000]x */ | |
679 | fab.fab$l_fna = src; | |
680 | fab.fab$b_fns = slen; | |
681 | fab.fab$l_nam = &nam; | |
682 | fab.fab$l_fop = FAB$M_NAM; | |
683 | ||
684 | nam.nam$l_esa = esa; | |
685 | nam.nam$b_ess = sizeof esa; | |
686 | nam.nam$b_nop |= NAM$M_SYNCHK; | |
687 | ||
688 | /* We call SYS$PARSE to handle such things as [--] for us. */ | |
199607e4 | 689 | if (SYS$PARSE (&fab, 0, 0) == RMS$_NORMAL) |
570d7624 JB |
690 | { |
691 | slen = nam.nam$b_esl; | |
692 | if (esa[slen - 1] == ';' && esa[slen - 2] == '.') | |
693 | slen -= 2; | |
694 | esa[slen] = '\0'; | |
695 | src = esa; | |
696 | } | |
697 | if (src[slen - 1] != ']' && src[slen - 1] != '>') | |
698 | { | |
699 | /* what about when we have logical_name:???? */ | |
700 | if (src[slen - 1] == ':') | |
5e570b75 | 701 | { /* Xlate logical name and see what we get */ |
570d7624 JB |
702 | ptr = strcpy (dst, src); /* upper case for getenv */ |
703 | while (*ptr) | |
704 | { | |
705 | if ('a' <= *ptr && *ptr <= 'z') | |
706 | *ptr -= 040; | |
707 | ptr++; | |
708 | } | |
5e570b75 | 709 | dst[slen - 1] = 0; /* remove colon */ |
570d7624 JB |
710 | if (!(src = egetenv (dst))) |
711 | return 0; | |
712 | /* should we jump to the beginning of this procedure? | |
713 | Good points: allows us to use logical names that xlate | |
714 | to Unix names, | |
715 | Bad points: can be a problem if we just translated to a device | |
716 | name... | |
717 | For now, I'll punt and always expect VMS names, and hope for | |
718 | the best! */ | |
719 | slen = strlen (src); | |
720 | if (src[slen - 1] != ']' && src[slen - 1] != '>') | |
721 | { /* no recursion here! */ | |
722 | strcpy (dst, src); | |
723 | return 0; | |
724 | } | |
725 | } | |
726 | else | |
5e570b75 | 727 | { /* not a directory spec */ |
570d7624 JB |
728 | strcpy (dst, src); |
729 | return 0; | |
730 | } | |
731 | } | |
732 | bracket = src[slen - 1]; | |
733 | ||
734 | /* If bracket is ']' or '>', bracket - 2 is the corresponding | |
735 | opening bracket. */ | |
bfb61299 JB |
736 | ptr = index (src, bracket - 2); |
737 | if (ptr == 0) | |
570d7624 JB |
738 | { /* no opening bracket */ |
739 | strcpy (dst, src); | |
740 | return 0; | |
741 | } | |
742 | if (!(rptr = rindex (src, '.'))) | |
743 | rptr = ptr; | |
744 | slen = rptr - src; | |
745 | strncpy (dst, src, slen); | |
746 | dst[slen] = '\0'; | |
747 | if (*rptr == '.') | |
748 | { | |
749 | dst[slen++] = bracket; | |
750 | dst[slen] = '\0'; | |
751 | } | |
752 | else | |
753 | { | |
754 | /* If we have the top-level of a rooted directory (i.e. xx:[000000]), | |
755 | then translate the device and recurse. */ | |
756 | if (dst[slen - 1] == ':' | |
5e570b75 | 757 | && dst[slen - 2] != ':' /* skip decnet nodes */ |
199607e4 | 758 | && strcmp (src + slen, "[000000]") == 0) |
570d7624 JB |
759 | { |
760 | dst[slen - 1] = '\0'; | |
761 | if ((ptr = egetenv (dst)) | |
762 | && (rlen = strlen (ptr) - 1) > 0 | |
763 | && (ptr[rlen] == ']' || ptr[rlen] == '>') | |
764 | && ptr[rlen - 1] == '.') | |
765 | { | |
72b21817 RS |
766 | char * buf = (char *) alloca (strlen (ptr) + 1); |
767 | strcpy (buf, ptr); | |
768 | buf[rlen - 1] = ']'; | |
769 | buf[rlen] = '\0'; | |
770 | return directory_file_name (buf, dst); | |
570d7624 JB |
771 | } |
772 | else | |
773 | dst[slen - 1] = ':'; | |
774 | } | |
775 | strcat (dst, "[000000]"); | |
776 | slen += 8; | |
777 | } | |
778 | rptr++; | |
779 | rlen = strlen (rptr) - 1; | |
780 | strncat (dst, rptr, rlen); | |
781 | dst[slen + rlen] = '\0'; | |
782 | strcat (dst, ".DIR.1"); | |
783 | return 1; | |
784 | } | |
785 | #endif /* VMS */ | |
786 | /* Process as Unix format: just remove any final slash. | |
787 | But leave "/" unchanged; do not change it to "". */ | |
788 | strcpy (dst, src); | |
125feee8 RS |
789 | #ifdef APOLLO |
790 | /* Handle // as root for apollo's. */ | |
791 | if ((slen > 2 && dst[slen - 1] == '/') | |
792 | || (slen > 1 && dst[0] != '/' && dst[slen - 1] == '/')) | |
793 | dst[slen - 1] = 0; | |
794 | #else | |
199607e4 | 795 | if (slen > 1 |
5e570b75 | 796 | && IS_DIRECTORY_SEP (dst[slen - 1]) |
4592782e RS |
797 | #ifdef DOS_NT |
798 | && !IS_ANY_SEP (dst[slen - 2]) | |
799 | #endif | |
800 | ) | |
570d7624 | 801 | dst[slen - 1] = 0; |
199607e4 RS |
802 | #endif |
803 | #ifdef DOS_NT | |
804 | CORRECT_DIR_SEPS (dst); | |
125feee8 | 805 | #endif |
570d7624 JB |
806 | return 1; |
807 | } | |
808 | ||
809 | DEFUN ("directory-file-name", Fdirectory_file_name, Sdirectory_file_name, | |
8c1a1077 PJ |
810 | 1, 1, 0, |
811 | doc: /* Returns the file name of the directory named DIRECTORY. | |
812 | This is the name of the file that holds the data for the directory DIRECTORY. | |
813 | This operation exists because a directory is also a file, but its name as | |
814 | a directory is different from its name as a file. | |
815 | In Unix-syntax, this function just removes the final slash. | |
816 | On VMS, given a VMS-syntax directory name such as \"[X.Y]\", | |
817 | it returns a file name such as \"[X]Y.DIR.1\". */) | |
818 | (directory) | |
570d7624 JB |
819 | Lisp_Object directory; |
820 | { | |
821 | char *buf; | |
0bf2eed2 | 822 | Lisp_Object handler; |
570d7624 | 823 | |
b7826503 | 824 | CHECK_STRING (directory); |
570d7624 | 825 | |
265a9e55 | 826 | if (NILP (directory)) |
570d7624 | 827 | return Qnil; |
0bf2eed2 RS |
828 | |
829 | /* If the file name has special constructs in it, | |
830 | call the corresponding file handler. */ | |
49307295 | 831 | handler = Ffind_file_name_handler (directory, Qdirectory_file_name); |
0bf2eed2 RS |
832 | if (!NILP (handler)) |
833 | return call2 (handler, Qdirectory_file_name, directory); | |
834 | ||
570d7624 JB |
835 | #ifdef VMS |
836 | /* 20 extra chars is insufficient for VMS, since we might perform a | |
837 | logical name translation. an equivalence string can be up to 255 | |
838 | chars long, so grab that much extra space... - sss */ | |
d5db4077 | 839 | buf = (char *) alloca (SBYTES (directory) + 20 + 255); |
570d7624 | 840 | #else |
d5db4077 | 841 | buf = (char *) alloca (SBYTES (directory) + 20); |
570d7624 | 842 | #endif |
d5db4077 | 843 | directory_file_name (SDATA (directory), buf); |
d7231f93 KH |
844 | return make_specified_string (buf, -1, strlen (buf), |
845 | STRING_MULTIBYTE (directory)); | |
570d7624 JB |
846 | } |
847 | ||
3ce839e4 RS |
848 | static char make_temp_name_tbl[64] = |
849 | { | |
850 | 'A','B','C','D','E','F','G','H', | |
851 | 'I','J','K','L','M','N','O','P', | |
852 | 'Q','R','S','T','U','V','W','X', | |
853 | 'Y','Z','a','b','c','d','e','f', | |
854 | 'g','h','i','j','k','l','m','n', | |
855 | 'o','p','q','r','s','t','u','v', | |
856 | 'w','x','y','z','0','1','2','3', | |
857 | '4','5','6','7','8','9','-','_' | |
858 | }; | |
cb613bb8 | 859 | |
3ce839e4 RS |
860 | static unsigned make_temp_name_count, make_temp_name_count_initialized_p; |
861 | ||
cb613bb8 | 862 | /* Value is a temporary file name starting with PREFIX, a string. |
efdc16c9 | 863 | |
cb613bb8 GM |
864 | The Emacs process number forms part of the result, so there is |
865 | no danger of generating a name being used by another process. | |
866 | In addition, this function makes an attempt to choose a name | |
867 | which has no existing file. To make this work, PREFIX should be | |
868 | an absolute file name. | |
efdc16c9 | 869 | |
cb613bb8 GM |
870 | BASE64_P non-zero means add the pid as 3 characters in base64 |
871 | encoding. In this case, 6 characters will be added to PREFIX to | |
872 | form the file name. Otherwise, if Emacs is running on a system | |
873 | with long file names, add the pid as a decimal number. | |
874 | ||
875 | This function signals an error if no unique file name could be | |
876 | generated. */ | |
877 | ||
878 | Lisp_Object | |
879 | make_temp_name (prefix, base64_p) | |
570d7624 | 880 | Lisp_Object prefix; |
cb613bb8 | 881 | int base64_p; |
570d7624 JB |
882 | { |
883 | Lisp_Object val; | |
3ce839e4 RS |
884 | int len; |
885 | int pid; | |
886 | unsigned char *p, *data; | |
887 | char pidbuf[20]; | |
888 | int pidlen; | |
efdc16c9 | 889 | |
b7826503 | 890 | CHECK_STRING (prefix); |
3ce839e4 RS |
891 | |
892 | /* VAL is created by adding 6 characters to PREFIX. The first | |
893 | three are the PID of this process, in base 64, and the second | |
894 | three are incremented if the file already exists. This ensures | |
895 | 262144 unique file names per PID per PREFIX. */ | |
896 | ||
897 | pid = (int) getpid (); | |
898 | ||
cb613bb8 GM |
899 | if (base64_p) |
900 | { | |
901 | pidbuf[0] = make_temp_name_tbl[pid & 63], pid >>= 6; | |
902 | pidbuf[1] = make_temp_name_tbl[pid & 63], pid >>= 6; | |
903 | pidbuf[2] = make_temp_name_tbl[pid & 63], pid >>= 6; | |
904 | pidlen = 3; | |
905 | } | |
906 | else | |
907 | { | |
3ce839e4 | 908 | #ifdef HAVE_LONG_FILE_NAMES |
cb613bb8 GM |
909 | sprintf (pidbuf, "%d", pid); |
910 | pidlen = strlen (pidbuf); | |
3a3bfb18 | 911 | #else |
cb613bb8 GM |
912 | pidbuf[0] = make_temp_name_tbl[pid & 63], pid >>= 6; |
913 | pidbuf[1] = make_temp_name_tbl[pid & 63], pid >>= 6; | |
914 | pidbuf[2] = make_temp_name_tbl[pid & 63], pid >>= 6; | |
915 | pidlen = 3; | |
3ce839e4 | 916 | #endif |
cb613bb8 | 917 | } |
efdc16c9 | 918 | |
d5db4077 | 919 | len = SCHARS (prefix); |
3ce839e4 | 920 | val = make_uninit_string (len + 3 + pidlen); |
d5db4077 KR |
921 | data = SDATA (val); |
922 | bcopy(SDATA (prefix), data, len); | |
3ce839e4 RS |
923 | p = data + len; |
924 | ||
925 | bcopy (pidbuf, p, pidlen); | |
926 | p += pidlen; | |
927 | ||
928 | /* Here we try to minimize useless stat'ing when this function is | |
929 | invoked many times successively with the same PREFIX. We achieve | |
930 | this by initializing count to a random value, and incrementing it | |
f6a492a9 RS |
931 | afterwards. |
932 | ||
933 | We don't want make-temp-name to be called while dumping, | |
934 | because then make_temp_name_count_initialized_p would get set | |
935 | and then make_temp_name_count would not be set when Emacs starts. */ | |
936 | ||
3ce839e4 RS |
937 | if (!make_temp_name_count_initialized_p) |
938 | { | |
939 | make_temp_name_count = (unsigned) time (NULL); | |
940 | make_temp_name_count_initialized_p = 1; | |
941 | } | |
942 | ||
943 | while (1) | |
944 | { | |
945 | struct stat ignored; | |
8a7777fc | 946 | unsigned num = make_temp_name_count; |
3ce839e4 RS |
947 | |
948 | p[0] = make_temp_name_tbl[num & 63], num >>= 6; | |
949 | p[1] = make_temp_name_tbl[num & 63], num >>= 6; | |
950 | p[2] = make_temp_name_tbl[num & 63], num >>= 6; | |
951 | ||
8a7777fc RS |
952 | /* Poor man's congruential RN generator. Replace with |
953 | ++make_temp_name_count for debugging. */ | |
954 | make_temp_name_count += 25229; | |
955 | make_temp_name_count %= 225307; | |
956 | ||
3ce839e4 RS |
957 | if (stat (data, &ignored) < 0) |
958 | { | |
959 | /* We want to return only if errno is ENOENT. */ | |
960 | if (errno == ENOENT) | |
961 | return val; | |
962 | else | |
963 | /* The error here is dubious, but there is little else we | |
964 | can do. The alternatives are to return nil, which is | |
965 | as bad as (and in many cases worse than) throwing the | |
966 | error, or to ignore the error, which will likely result | |
8a7777fc RS |
967 | in looping through 225307 stat's, which is not only |
968 | dog-slow, but also useless since it will fallback to | |
969 | the errow below, anyway. */ | |
9869bb0b | 970 | report_file_error ("Cannot create temporary name for prefix", |
3ce839e4 RS |
971 | Fcons (prefix, Qnil)); |
972 | /* not reached */ | |
973 | } | |
974 | } | |
975 | ||
976 | error ("Cannot create temporary name for prefix `%s'", | |
d5db4077 | 977 | SDATA (prefix)); |
3ce839e4 | 978 | return Qnil; |
570d7624 | 979 | } |
3ce839e4 | 980 | |
cb613bb8 GM |
981 | |
982 | DEFUN ("make-temp-name", Fmake_temp_name, Smake_temp_name, 1, 1, 0, | |
8c1a1077 PJ |
983 | doc: /* Generate temporary file name (string) starting with PREFIX (a string). |
984 | The Emacs process number forms part of the result, | |
985 | so there is no danger of generating a name being used by another process. | |
986 | ||
987 | In addition, this function makes an attempt to choose a name | |
988 | which has no existing file. To make this work, | |
989 | PREFIX should be an absolute file name. | |
990 | ||
991 | There is a race condition between calling `make-temp-name' and creating the | |
992 | file which opens all kinds of security holes. For that reason, you should | |
f9e6f049 RS |
993 | probably use `make-temp-file' instead, except in three circumstances: |
994 | ||
995 | * If you are creating the file in the user's home directory. | |
996 | * If you are creating a directory rather than an ordinary file. | |
997 | * If you are taking special precautions as `make-temp-file' does. */) | |
8c1a1077 | 998 | (prefix) |
cb613bb8 GM |
999 | Lisp_Object prefix; |
1000 | { | |
1001 | return make_temp_name (prefix, 0); | |
1002 | } | |
1003 | ||
1004 | ||
570d7624 JB |
1005 | \f |
1006 | DEFUN ("expand-file-name", Fexpand_file_name, Sexpand_file_name, 1, 2, 0, | |
8c1a1077 PJ |
1007 | doc: /* Convert filename NAME to absolute, and canonicalize it. |
1008 | Second arg DEFAULT-DIRECTORY is directory to start with if NAME is relative | |
1009 | (does not start with slash); if DEFAULT-DIRECTORY is nil or missing, | |
1010 | the current buffer's value of default-directory is used. | |
1011 | File name components that are `.' are removed, and | |
1012 | so are file name components followed by `..', along with the `..' itself; | |
1013 | note that these simplifications are done without checking the resulting | |
1014 | file names in the file system. | |
1015 | An initial `~/' expands to your home directory. | |
1016 | An initial `~USER/' expands to USER's home directory. | |
1017 | See also the function `substitute-in-file-name'. */) | |
1018 | (name, default_directory) | |
3b7f6e60 | 1019 | Lisp_Object name, default_directory; |
570d7624 JB |
1020 | { |
1021 | unsigned char *nm; | |
199607e4 | 1022 | |
570d7624 JB |
1023 | register unsigned char *newdir, *p, *o; |
1024 | int tlen; | |
1025 | unsigned char *target; | |
1026 | struct passwd *pw; | |
570d7624 JB |
1027 | #ifdef VMS |
1028 | unsigned char * colon = 0; | |
1029 | unsigned char * close = 0; | |
1030 | unsigned char * slash = 0; | |
1031 | unsigned char * brack = 0; | |
1032 | int lbrack = 0, rbrack = 0; | |
1033 | int dots = 0; | |
1034 | #endif /* VMS */ | |
5e570b75 | 1035 | #ifdef DOS_NT |
199607e4 | 1036 | int drive = 0; |
9a1dc3be | 1037 | int collapse_newdir = 1; |
f0f95d31 | 1038 | int is_escaped = 0; |
5e570b75 | 1039 | #endif /* DOS_NT */ |
199607e4 | 1040 | int length; |
beb402de | 1041 | Lisp_Object handler, result; |
199607e4 | 1042 | |
b7826503 | 1043 | CHECK_STRING (name); |
570d7624 | 1044 | |
0bf2eed2 RS |
1045 | /* If the file name has special constructs in it, |
1046 | call the corresponding file handler. */ | |
49307295 | 1047 | handler = Ffind_file_name_handler (name, Qexpand_file_name); |
0bf2eed2 | 1048 | if (!NILP (handler)) |
3b7f6e60 | 1049 | return call3 (handler, Qexpand_file_name, name, default_directory); |
58fc9587 | 1050 | |
3b7f6e60 EN |
1051 | /* Use the buffer's default-directory if DEFAULT_DIRECTORY is omitted. */ |
1052 | if (NILP (default_directory)) | |
1053 | default_directory = current_buffer->directory; | |
82330e7f | 1054 | if (! STRINGP (default_directory)) |
dd693537 EZ |
1055 | { |
1056 | #ifdef DOS_NT | |
1057 | /* "/" is not considered a root directory on DOS_NT, so using "/" | |
1058 | here causes an infinite recursion in, e.g., the following: | |
1059 | ||
1060 | (let (default-directory) | |
1061 | (expand-file-name "a")) | |
1062 | ||
1063 | To avoid this, we set default_directory to the root of the | |
1064 | current drive. */ | |
1065 | extern char *emacs_root_dir (void); | |
1066 | ||
1067 | default_directory = build_string (emacs_root_dir ()); | |
1068 | #else | |
1069 | default_directory = build_string ("/"); | |
1070 | #endif | |
1071 | } | |
58fc9587 | 1072 | |
3b7f6e60 | 1073 | if (!NILP (default_directory)) |
273e0829 | 1074 | { |
3b7f6e60 | 1075 | handler = Ffind_file_name_handler (default_directory, Qexpand_file_name); |
273e0829 | 1076 | if (!NILP (handler)) |
3b7f6e60 | 1077 | return call3 (handler, Qexpand_file_name, name, default_directory); |
273e0829 | 1078 | } |
0bf2eed2 | 1079 | |
d5db4077 | 1080 | o = SDATA (default_directory); |
5e570b75 | 1081 | |
3b7f6e60 | 1082 | /* Make sure DEFAULT_DIRECTORY is properly expanded. |
f14b1c68 | 1083 | It would be better to do this down below where we actually use |
3b7f6e60 | 1084 | default_directory. Unfortunately, calling Fexpand_file_name recursively |
f14b1c68 JB |
1085 | could invoke GC, and the strings might be relocated. This would |
1086 | be annoying because we have pointers into strings lying around | |
1087 | that would need adjusting, and people would add new pointers to | |
1088 | the code and forget to adjust them, resulting in intermittent bugs. | |
4ad827c5 RS |
1089 | Putting this call here avoids all that crud. |
1090 | ||
1091 | The EQ test avoids infinite recursion. */ | |
3b7f6e60 | 1092 | if (! NILP (default_directory) && !EQ (default_directory, name) |
199607e4 RS |
1093 | /* Save time in some common cases - as long as default_directory |
1094 | is not relative, it can be canonicalized with name below (if it | |
1095 | is needed at all) without requiring it to be expanded now. */ | |
01937013 | 1096 | #ifdef DOS_NT |
199607e4 | 1097 | /* Detect MSDOS file names with drive specifiers. */ |
f0f95d31 | 1098 | && ! (IS_DRIVE (o[0]) && IS_DEVICE_SEP (o[1]) && IS_DIRECTORY_SEP (o[2])) |
199607e4 RS |
1099 | #ifdef WINDOWSNT |
1100 | /* Detect Windows file names in UNC format. */ | |
1101 | && ! (IS_DIRECTORY_SEP (o[0]) && IS_DIRECTORY_SEP (o[1])) | |
01937013 | 1102 | #endif |
199607e4 RS |
1103 | #else /* not DOS_NT */ |
1104 | /* Detect Unix absolute file names (/... alone is not absolute on | |
1105 | DOS or Windows). */ | |
1106 | && ! (IS_DIRECTORY_SEP (o[0])) | |
1107 | #endif /* not DOS_NT */ | |
1108 | ) | |
f14b1c68 JB |
1109 | { |
1110 | struct gcpro gcpro1; | |
1111 | ||
1112 | GCPRO1 (name); | |
3b7f6e60 | 1113 | default_directory = Fexpand_file_name (default_directory, Qnil); |
f14b1c68 JB |
1114 | UNGCPRO; |
1115 | } | |
1116 | ||
570d7624 JB |
1117 | #ifdef VMS |
1118 | /* Filenames on VMS are always upper case. */ | |
1119 | name = Fupcase (name); | |
1120 | #endif | |
4c3c22f3 RS |
1121 | #ifdef FILE_SYSTEM_CASE |
1122 | name = FILE_SYSTEM_CASE (name); | |
1123 | #endif | |
570d7624 | 1124 | |
d5db4077 | 1125 | nm = SDATA (name); |
a5a1cc06 | 1126 | |
5e570b75 | 1127 | #ifdef DOS_NT |
199607e4 RS |
1128 | /* We will force directory separators to be either all \ or /, so make |
1129 | a local copy to modify, even if there ends up being no change. */ | |
1130 | nm = strcpy (alloca (strlen (nm) + 1), nm); | |
1131 | ||
f0f95d31 RS |
1132 | /* Note if special escape prefix is present, but remove for now. */ |
1133 | if (nm[0] == '/' && nm[1] == ':') | |
1134 | { | |
1135 | is_escaped = 1; | |
1136 | nm += 2; | |
1137 | } | |
1138 | ||
199607e4 | 1139 | /* Find and remove drive specifier if present; this makes nm absolute |
f0f95d31 RS |
1140 | even if the rest of the name appears to be relative. Only look for |
1141 | drive specifier at the beginning. */ | |
1142 | if (IS_DRIVE (nm[0]) && IS_DEVICE_SEP (nm[1])) | |
1143 | { | |
1144 | drive = nm[0]; | |
1145 | nm += 2; | |
1146 | } | |
bb1ff1f4 GV |
1147 | |
1148 | #ifdef WINDOWSNT | |
1149 | /* If we see "c://somedir", we want to strip the first slash after the | |
1150 | colon when stripping the drive letter. Otherwise, this expands to | |
1151 | "//somedir". */ | |
1152 | if (drive && IS_DIRECTORY_SEP (nm[0]) && IS_DIRECTORY_SEP (nm[1])) | |
1153 | nm++; | |
1154 | #endif /* WINDOWSNT */ | |
5e570b75 | 1155 | #endif /* DOS_NT */ |
4c3c22f3 | 1156 | |
199607e4 RS |
1157 | #ifdef WINDOWSNT |
1158 | /* Discard any previous drive specifier if nm is now in UNC format. */ | |
1159 | if (IS_DIRECTORY_SEP (nm[0]) && IS_DIRECTORY_SEP (nm[1])) | |
1160 | { | |
1161 | drive = 0; | |
1162 | } | |
1163 | #endif | |
1164 | ||
214378ec GM |
1165 | /* If nm is absolute, look for `/./' or `/../' or `//''sequences; if |
1166 | none are found, we can probably return right away. We will avoid | |
1167 | allocating a new string if name is already fully expanded. */ | |
570d7624 | 1168 | if ( |
5e570b75 | 1169 | IS_DIRECTORY_SEP (nm[0]) |
199607e4 | 1170 | #ifdef MSDOS |
f0f95d31 | 1171 | && drive && !is_escaped |
199607e4 RS |
1172 | #endif |
1173 | #ifdef WINDOWSNT | |
f0f95d31 | 1174 | && (drive || IS_DIRECTORY_SEP (nm[1])) && !is_escaped |
199607e4 | 1175 | #endif |
570d7624 JB |
1176 | #ifdef VMS |
1177 | || index (nm, ':') | |
1178 | #endif /* VMS */ | |
1179 | ) | |
1180 | { | |
f14b1c68 JB |
1181 | /* If it turns out that the filename we want to return is just a |
1182 | suffix of FILENAME, we don't need to go through and edit | |
1183 | things; we just need to construct a new string using data | |
1184 | starting at the middle of FILENAME. If we set lose to a | |
1185 | non-zero value, that means we've discovered that we can't do | |
1186 | that cool trick. */ | |
1187 | int lose = 0; | |
1188 | ||
570d7624 | 1189 | p = nm; |
570d7624 JB |
1190 | while (*p) |
1191 | { | |
199607e4 | 1192 | /* Since we know the name is absolute, we can assume that each |
c77d647e JB |
1193 | element starts with a "/". */ |
1194 | ||
c77d647e | 1195 | /* "." and ".." are hairy. */ |
5e570b75 | 1196 | if (IS_DIRECTORY_SEP (p[0]) |
c77d647e | 1197 | && p[1] == '.' |
5e570b75 | 1198 | && (IS_DIRECTORY_SEP (p[2]) |
c77d647e | 1199 | || p[2] == 0 |
5e570b75 | 1200 | || (p[2] == '.' && (IS_DIRECTORY_SEP (p[3]) |
c77d647e | 1201 | || p[3] == 0)))) |
570d7624 | 1202 | lose = 1; |
214378ec GM |
1203 | /* We want to replace multiple `/' in a row with a single |
1204 | slash. */ | |
1205 | else if (p > nm | |
1206 | && IS_DIRECTORY_SEP (p[0]) | |
1207 | && IS_DIRECTORY_SEP (p[1])) | |
1208 | lose = 1; | |
efdc16c9 | 1209 | |
570d7624 JB |
1210 | #ifdef VMS |
1211 | if (p[0] == '\\') | |
1212 | lose = 1; | |
1213 | if (p[0] == '/') { | |
1214 | /* if dev:[dir]/, move nm to / */ | |
1215 | if (!slash && p > nm && (brack || colon)) { | |
1216 | nm = (brack ? brack + 1 : colon + 1); | |
1217 | lbrack = rbrack = 0; | |
1218 | brack = 0; | |
1219 | colon = 0; | |
1220 | } | |
1221 | slash = p; | |
1222 | } | |
1223 | if (p[0] == '-') | |
1224 | #ifndef VMS4_4 | |
1225 | /* VMS pre V4.4,convert '-'s in filenames. */ | |
1226 | if (lbrack == rbrack) | |
1227 | { | |
5e570b75 | 1228 | if (dots < 2) /* this is to allow negative version numbers */ |
570d7624 JB |
1229 | p[0] = '_'; |
1230 | } | |
1231 | else | |
1232 | #endif /* VMS4_4 */ | |
1233 | if (lbrack > rbrack && | |
1234 | ((p[-1] == '.' || p[-1] == '[' || p[-1] == '<') && | |
1235 | (p[1] == '.' || p[1] == ']' || p[1] == '>'))) | |
1236 | lose = 1; | |
1237 | #ifndef VMS4_4 | |
1238 | else | |
1239 | p[0] = '_'; | |
1240 | #endif /* VMS4_4 */ | |
1241 | /* count open brackets, reset close bracket pointer */ | |
1242 | if (p[0] == '[' || p[0] == '<') | |
1243 | lbrack++, brack = 0; | |
1244 | /* count close brackets, set close bracket pointer */ | |
1245 | if (p[0] == ']' || p[0] == '>') | |
1246 | rbrack++, brack = p; | |
1247 | /* detect ][ or >< */ | |
1248 | if ((p[0] == ']' || p[0] == '>') && (p[1] == '[' || p[1] == '<')) | |
1249 | lose = 1; | |
1250 | if ((p[0] == ':' || p[0] == ']' || p[0] == '>') && p[1] == '~') | |
1251 | nm = p + 1, lose = 1; | |
1252 | if (p[0] == ':' && (colon || slash)) | |
1253 | /* if dev1:[dir]dev2:, move nm to dev2: */ | |
1254 | if (brack) | |
1255 | { | |
1256 | nm = brack + 1; | |
1257 | brack = 0; | |
1258 | } | |
199607e4 | 1259 | /* if /name/dev:, move nm to dev: */ |
570d7624 JB |
1260 | else if (slash) |
1261 | nm = slash + 1; | |
1262 | /* if node::dev:, move colon following dev */ | |
1263 | else if (colon && colon[-1] == ':') | |
1264 | colon = p; | |
1265 | /* if dev1:dev2:, move nm to dev2: */ | |
1266 | else if (colon && colon[-1] != ':') | |
1267 | { | |
1268 | nm = colon + 1; | |
1269 | colon = 0; | |
1270 | } | |
1271 | if (p[0] == ':' && !colon) | |
1272 | { | |
1273 | if (p[1] == ':') | |
1274 | p++; | |
1275 | colon = p; | |
1276 | } | |
1277 | if (lbrack == rbrack) | |
1278 | if (p[0] == ';') | |
1279 | dots = 2; | |
1280 | else if (p[0] == '.') | |
1281 | dots++; | |
1282 | #endif /* VMS */ | |
1283 | p++; | |
1284 | } | |
1285 | if (!lose) | |
1286 | { | |
1287 | #ifdef VMS | |
1288 | if (index (nm, '/')) | |
d7231f93 KH |
1289 | { |
1290 | nm = sys_translate_unix (nm); | |
1291 | return make_specified_string (nm, -1, strlen (nm), | |
1292 | STRING_MULTIBYTE (name)); | |
1293 | } | |
570d7624 | 1294 | #endif /* VMS */ |
199607e4 RS |
1295 | #ifdef DOS_NT |
1296 | /* Make sure directories are all separated with / or \ as | |
1297 | desired, but avoid allocation of a new string when not | |
1298 | required. */ | |
1299 | CORRECT_DIR_SEPS (nm); | |
1300 | #ifdef WINDOWSNT | |
1301 | if (IS_DIRECTORY_SEP (nm[1])) | |
1302 | { | |
d5db4077 | 1303 | if (strcmp (nm, SDATA (name)) != 0) |
3f817c73 KH |
1304 | name = make_specified_string (nm, -1, strlen (nm), |
1305 | STRING_MULTIBYTE (name)); | |
199607e4 RS |
1306 | } |
1307 | else | |
1308 | #endif | |
1309 | /* drive must be set, so this is okay */ | |
d5db4077 | 1310 | if (strcmp (nm - 2, SDATA (name)) != 0) |
199607e4 | 1311 | { |
3f817c73 KH |
1312 | char temp[] = " :"; |
1313 | ||
1314 | name = make_specified_string (nm, -1, p - nm, | |
1315 | STRING_MULTIBYTE (name)); | |
1316 | temp[0] = DRIVE_LETTER (drive); | |
1317 | name = concat2 (build_string (temp), name); | |
199607e4 RS |
1318 | } |
1319 | return name; | |
1320 | #else /* not DOS_NT */ | |
d5db4077 | 1321 | if (nm == SDATA (name)) |
570d7624 | 1322 | return name; |
d7231f93 KH |
1323 | return make_specified_string (nm, -1, strlen (nm), |
1324 | STRING_MULTIBYTE (name)); | |
5e570b75 | 1325 | #endif /* not DOS_NT */ |
570d7624 JB |
1326 | } |
1327 | } | |
1328 | ||
199607e4 RS |
1329 | /* At this point, nm might or might not be an absolute file name. We |
1330 | need to expand ~ or ~user if present, otherwise prefix nm with | |
1331 | default_directory if nm is not absolute, and finally collapse /./ | |
1332 | and /foo/../ sequences. | |
1333 | ||
1334 | We set newdir to be the appropriate prefix if one is needed: | |
1335 | - the relevant user directory if nm starts with ~ or ~user | |
1336 | - the specified drive's working dir (DOS/NT only) if nm does not | |
1337 | start with / | |
1338 | - the value of default_directory. | |
1339 | ||
1340 | Note that these prefixes are not guaranteed to be absolute (except | |
1341 | for the working dir of a drive). Therefore, to ensure we always | |
1342 | return an absolute name, if the final prefix is not absolute we | |
1343 | append it to the current working directory. */ | |
570d7624 JB |
1344 | |
1345 | newdir = 0; | |
1346 | ||
1347 | if (nm[0] == '~') /* prefix ~ */ | |
c77d647e | 1348 | { |
5e570b75 | 1349 | if (IS_DIRECTORY_SEP (nm[1]) |
570d7624 | 1350 | #ifdef VMS |
c77d647e | 1351 | || nm[1] == ':' |
5e570b75 | 1352 | #endif /* VMS */ |
c77d647e JB |
1353 | || nm[1] == 0) /* ~ by itself */ |
1354 | { | |
1355 | if (!(newdir = (unsigned char *) egetenv ("HOME"))) | |
1356 | newdir = (unsigned char *) ""; | |
199607e4 | 1357 | nm++; |
5e570b75 | 1358 | #ifdef DOS_NT |
9a1dc3be | 1359 | collapse_newdir = 0; |
4c3c22f3 | 1360 | #endif |
570d7624 | 1361 | #ifdef VMS |
c77d647e | 1362 | nm++; /* Don't leave the slash in nm. */ |
5e570b75 | 1363 | #endif /* VMS */ |
c77d647e JB |
1364 | } |
1365 | else /* ~user/filename */ | |
1366 | { | |
5e570b75 | 1367 | for (p = nm; *p && (!IS_DIRECTORY_SEP (*p) |
570d7624 | 1368 | #ifdef VMS |
c77d647e | 1369 | && *p != ':' |
5e570b75 | 1370 | #endif /* VMS */ |
c77d647e JB |
1371 | ); p++); |
1372 | o = (unsigned char *) alloca (p - nm + 1); | |
1373 | bcopy ((char *) nm, o, p - nm); | |
1374 | o [p - nm] = 0; | |
1375 | ||
1376 | pw = (struct passwd *) getpwnam (o + 1); | |
1377 | if (pw) | |
1378 | { | |
1379 | newdir = (unsigned char *) pw -> pw_dir; | |
570d7624 | 1380 | #ifdef VMS |
c77d647e | 1381 | nm = p + 1; /* skip the terminator */ |
570d7624 | 1382 | #else |
c77d647e | 1383 | nm = p; |
199607e4 | 1384 | #ifdef DOS_NT |
9a1dc3be | 1385 | collapse_newdir = 0; |
199607e4 | 1386 | #endif |
5e570b75 | 1387 | #endif /* VMS */ |
c77d647e | 1388 | } |
e5d77022 | 1389 | |
c77d647e JB |
1390 | /* If we don't find a user of that name, leave the name |
1391 | unchanged; don't move nm forward to p. */ | |
1392 | } | |
1393 | } | |
570d7624 | 1394 | |
5e570b75 | 1395 | #ifdef DOS_NT |
199607e4 RS |
1396 | /* On DOS and Windows, nm is absolute if a drive name was specified; |
1397 | use the drive's current directory as the prefix if needed. */ | |
1398 | if (!newdir && drive) | |
1399 | { | |
1400 | /* Get default directory if needed to make nm absolute. */ | |
1401 | if (!IS_DIRECTORY_SEP (nm[0])) | |
1402 | { | |
1403 | newdir = alloca (MAXPATHLEN + 1); | |
1404 | if (!getdefdir (toupper (drive) - 'A' + 1, newdir)) | |
1405 | newdir = NULL; | |
1406 | } | |
1407 | if (!newdir) | |
1408 | { | |
1409 | /* Either nm starts with /, or drive isn't mounted. */ | |
1410 | newdir = alloca (4); | |
f94988a7 | 1411 | newdir[0] = DRIVE_LETTER (drive); |
199607e4 RS |
1412 | newdir[1] = ':'; |
1413 | newdir[2] = '/'; | |
1414 | newdir[3] = 0; | |
1415 | } | |
1416 | } | |
5e570b75 | 1417 | #endif /* DOS_NT */ |
199607e4 RS |
1418 | |
1419 | /* Finally, if no prefix has been specified and nm is not absolute, | |
1420 | then it must be expanded relative to default_directory. */ | |
1421 | ||
34097368 | 1422 | if (1 |
199607e4 RS |
1423 | #ifndef DOS_NT |
1424 | /* /... alone is not absolute on DOS and Windows. */ | |
34097368 | 1425 | && !IS_DIRECTORY_SEP (nm[0]) |
199607e4 RS |
1426 | #endif |
1427 | #ifdef WINDOWSNT | |
34097368 | 1428 | && !(IS_DIRECTORY_SEP (nm[0]) && IS_DIRECTORY_SEP (nm[1])) |
199607e4 RS |
1429 | #endif |
1430 | #ifdef VMS | |
1431 | && !index (nm, ':') | |
1432 | #endif | |
570d7624 JB |
1433 | && !newdir) |
1434 | { | |
d5db4077 | 1435 | newdir = SDATA (default_directory); |
f0f95d31 RS |
1436 | #ifdef DOS_NT |
1437 | /* Note if special escape prefix is present, but remove for now. */ | |
1438 | if (newdir[0] == '/' && newdir[1] == ':') | |
1439 | { | |
1440 | is_escaped = 1; | |
1441 | newdir += 2; | |
1442 | } | |
1443 | #endif | |
570d7624 JB |
1444 | } |
1445 | ||
5e570b75 | 1446 | #ifdef DOS_NT |
199607e4 RS |
1447 | if (newdir) |
1448 | { | |
1449 | /* First ensure newdir is an absolute name. */ | |
1450 | if ( | |
1451 | /* Detect MSDOS file names with drive specifiers. */ | |
1452 | ! (IS_DRIVE (newdir[0]) | |
1453 | && IS_DEVICE_SEP (newdir[1]) && IS_DIRECTORY_SEP (newdir[2])) | |
1454 | #ifdef WINDOWSNT | |
1455 | /* Detect Windows file names in UNC format. */ | |
1456 | && ! (IS_DIRECTORY_SEP (newdir[0]) && IS_DIRECTORY_SEP (newdir[1])) | |
1457 | #endif | |
1458 | ) | |
1459 | { | |
1460 | /* Effectively, let newdir be (expand-file-name newdir cwd). | |
1461 | Because of the admonition against calling expand-file-name | |
1462 | when we have pointers into lisp strings, we accomplish this | |
1463 | indirectly by prepending newdir to nm if necessary, and using | |
1464 | cwd (or the wd of newdir's drive) as the new newdir. */ | |
1465 | ||
1466 | if (IS_DRIVE (newdir[0]) && newdir[1] == ':') | |
1467 | { | |
1468 | drive = newdir[0]; | |
1469 | newdir += 2; | |
1470 | } | |
1471 | if (!IS_DIRECTORY_SEP (nm[0])) | |
1472 | { | |
1473 | char * tmp = alloca (strlen (newdir) + strlen (nm) + 2); | |
1474 | file_name_as_directory (tmp, newdir); | |
1475 | strcat (tmp, nm); | |
1476 | nm = tmp; | |
1477 | } | |
1478 | newdir = alloca (MAXPATHLEN + 1); | |
1479 | if (drive) | |
1480 | { | |
1481 | if (!getdefdir (toupper (drive) - 'A' + 1, newdir)) | |
1482 | newdir = "/"; | |
1483 | } | |
1484 | else | |
1485 | getwd (newdir); | |
1486 | } | |
1487 | ||
1488 | /* Strip off drive name from prefix, if present. */ | |
1489 | if (IS_DRIVE (newdir[0]) && newdir[1] == ':') | |
1490 | { | |
1491 | drive = newdir[0]; | |
1492 | newdir += 2; | |
1493 | } | |
1494 | ||
1495 | /* Keep only a prefix from newdir if nm starts with slash | |
82330e7f | 1496 | (//server/share for UNC, nothing otherwise). */ |
9a1dc3be | 1497 | if (IS_DIRECTORY_SEP (nm[0]) && collapse_newdir) |
199607e4 RS |
1498 | { |
1499 | #ifdef WINDOWSNT | |
1500 | if (IS_DIRECTORY_SEP (newdir[0]) && IS_DIRECTORY_SEP (newdir[1])) | |
1501 | { | |
1502 | newdir = strcpy (alloca (strlen (newdir) + 1), newdir); | |
1503 | p = newdir + 2; | |
1504 | while (*p && !IS_DIRECTORY_SEP (*p)) p++; | |
1505 | p++; | |
1506 | while (*p && !IS_DIRECTORY_SEP (*p)) p++; | |
1507 | *p = 0; | |
1508 | } | |
1509 | else | |
1510 | #endif | |
1511 | newdir = ""; | |
1512 | } | |
1513 | } | |
5e570b75 | 1514 | #endif /* DOS_NT */ |
199607e4 RS |
1515 | |
1516 | if (newdir) | |
bfb61299 | 1517 | { |
57676091 | 1518 | /* Get rid of any slash at the end of newdir, unless newdir is |
f0f95d31 | 1519 | just / or // (an incomplete UNC name). */ |
199607e4 | 1520 | length = strlen (newdir); |
f0f95d31 | 1521 | if (length > 1 && IS_DIRECTORY_SEP (newdir[length - 1]) |
57676091 RS |
1522 | #ifdef WINDOWSNT |
1523 | && !(length == 2 && IS_DIRECTORY_SEP (newdir[0])) | |
1524 | #endif | |
1525 | ) | |
bfb61299 JB |
1526 | { |
1527 | unsigned char *temp = (unsigned char *) alloca (length); | |
1528 | bcopy (newdir, temp, length - 1); | |
1529 | temp[length - 1] = 0; | |
1530 | newdir = temp; | |
1531 | } | |
1532 | tlen = length + 1; | |
1533 | } | |
1534 | else | |
1535 | tlen = 0; | |
570d7624 | 1536 | |
bfb61299 JB |
1537 | /* Now concatenate the directory and name to new space in the stack frame */ |
1538 | tlen += strlen (nm) + 1; | |
5e570b75 | 1539 | #ifdef DOS_NT |
f0f95d31 RS |
1540 | /* Reserve space for drive specifier and escape prefix, since either |
1541 | or both may need to be inserted. (The Microsoft x86 compiler | |
5e570b75 | 1542 | produces incorrect code if the following two lines are combined.) */ |
f0f95d31 RS |
1543 | target = (unsigned char *) alloca (tlen + 4); |
1544 | target += 4; | |
5e570b75 | 1545 | #else /* not DOS_NT */ |
570d7624 | 1546 | target = (unsigned char *) alloca (tlen); |
5e570b75 | 1547 | #endif /* not DOS_NT */ |
570d7624 JB |
1548 | *target = 0; |
1549 | ||
1550 | if (newdir) | |
1551 | { | |
1552 | #ifndef VMS | |
5e570b75 | 1553 | if (nm[0] == 0 || IS_DIRECTORY_SEP (nm[0])) |
f5321b5c | 1554 | { |
3ea2d8b2 | 1555 | #ifdef DOS_NT |
f5321b5c RS |
1556 | /* If newdir is effectively "C:/", then the drive letter will have |
1557 | been stripped and newdir will be "/". Concatenating with an | |
1558 | absolute directory in nm produces "//", which will then be | |
1559 | incorrectly treated as a network share. Ignore newdir in | |
1560 | this case (keeping the drive letter). */ | |
efdc16c9 | 1561 | if (!(drive && nm[0] && IS_DIRECTORY_SEP (newdir[0]) |
f5321b5c RS |
1562 | && newdir[1] == '\0')) |
1563 | #endif | |
1564 | strcpy (target, newdir); | |
1565 | } | |
570d7624 JB |
1566 | else |
1567 | #endif | |
c77d647e | 1568 | file_name_as_directory (target, newdir); |
570d7624 JB |
1569 | } |
1570 | ||
1571 | strcat (target, nm); | |
1572 | #ifdef VMS | |
1573 | if (index (target, '/')) | |
1574 | strcpy (target, sys_translate_unix (target)); | |
1575 | #endif /* VMS */ | |
1576 | ||
199607e4 RS |
1577 | /* ASSERT (IS_DIRECTORY_SEP (target[0])) if not VMS */ |
1578 | ||
214378ec GM |
1579 | /* Now canonicalize by removing `//', `/.' and `/foo/..' if they |
1580 | appear. */ | |
570d7624 JB |
1581 | |
1582 | p = target; | |
1583 | o = target; | |
1584 | ||
1585 | while (*p) | |
1586 | { | |
1587 | #ifdef VMS | |
1588 | if (*p != ']' && *p != '>' && *p != '-') | |
1589 | { | |
1590 | if (*p == '\\') | |
1591 | p++; | |
1592 | *o++ = *p++; | |
1593 | } | |
1594 | else if ((p[0] == ']' || p[0] == '>') && p[0] == p[1] + 2) | |
1595 | /* brackets are offset from each other by 2 */ | |
1596 | { | |
1597 | p += 2; | |
1598 | if (*p != '.' && *p != '-' && o[-1] != '.') | |
1599 | /* convert [foo][bar] to [bar] */ | |
1600 | while (o[-1] != '[' && o[-1] != '<') | |
1601 | o--; | |
1602 | else if (*p == '-' && *o != '.') | |
1603 | *--p = '.'; | |
1604 | } | |
1605 | else if (p[0] == '-' && o[-1] == '.' && | |
1606 | (p[1] == '.' || p[1] == ']' || p[1] == '>')) | |
1607 | /* flush .foo.- ; leave - if stopped by '[' or '<' */ | |
1608 | { | |
1609 | do | |
1610 | o--; | |
1611 | while (o[-1] != '.' && o[-1] != '[' && o[-1] != '<'); | |
5e570b75 | 1612 | if (p[1] == '.') /* foo.-.bar ==> bar. */ |
570d7624 JB |
1613 | p += 2; |
1614 | else if (o[-1] == '.') /* '.foo.-]' ==> ']' */ | |
1615 | p++, o--; | |
1616 | /* else [foo.-] ==> [-] */ | |
1617 | } | |
1618 | else | |
1619 | { | |
1620 | #ifndef VMS4_4 | |
1621 | if (*p == '-' && | |
1622 | o[-1] != '[' && o[-1] != '<' && o[-1] != '.' && | |
1623 | p[1] != ']' && p[1] != '>' && p[1] != '.') | |
1624 | *p = '_'; | |
1625 | #endif /* VMS4_4 */ | |
1626 | *o++ = *p++; | |
1627 | } | |
1628 | #else /* not VMS */ | |
5e570b75 RS |
1629 | if (!IS_DIRECTORY_SEP (*p)) |
1630 | { | |
570d7624 JB |
1631 | *o++ = *p++; |
1632 | } | |
5e570b75 | 1633 | else if (IS_DIRECTORY_SEP (p[0]) |
c77d647e | 1634 | && p[1] == '.' |
5e570b75 | 1635 | && (IS_DIRECTORY_SEP (p[2]) |
c77d647e JB |
1636 | || p[2] == 0)) |
1637 | { | |
1638 | /* If "/." is the entire filename, keep the "/". Otherwise, | |
1639 | just delete the whole "/.". */ | |
1640 | if (o == target && p[2] == '\0') | |
1641 | *o++ = *p; | |
1642 | p += 2; | |
1643 | } | |
c0fa5a0b | 1644 | else if (IS_DIRECTORY_SEP (p[0]) && p[1] == '.' && p[2] == '.' |
570d7624 JB |
1645 | /* `/../' is the "superroot" on certain file systems. */ |
1646 | && o != target | |
5e570b75 | 1647 | && (IS_DIRECTORY_SEP (p[3]) || p[3] == 0)) |
570d7624 | 1648 | { |
5e570b75 | 1649 | while (o != target && (--o) && !IS_DIRECTORY_SEP (*o)) |
570d7624 | 1650 | ; |
795de720 RS |
1651 | /* Keep initial / only if this is the whole name. */ |
1652 | if (o == target && IS_ANY_SEP (*o) && p[3] == 0) | |
51b1d12e | 1653 | ++o; |
570d7624 JB |
1654 | p += 3; |
1655 | } | |
214378ec GM |
1656 | else if (p > target |
1657 | && IS_DIRECTORY_SEP (p[0]) && IS_DIRECTORY_SEP (p[1])) | |
1658 | { | |
1659 | /* Collapse multiple `/' in a row. */ | |
1660 | *o++ = *p++; | |
1661 | while (IS_DIRECTORY_SEP (*p)) | |
1662 | ++p; | |
1663 | } | |
570d7624 | 1664 | else |
5e570b75 | 1665 | { |
570d7624 JB |
1666 | *o++ = *p++; |
1667 | } | |
1668 | #endif /* not VMS */ | |
1669 | } | |
1670 | ||
5e570b75 | 1671 | #ifdef DOS_NT |
199607e4 | 1672 | /* At last, set drive name. */ |
5e570b75 | 1673 | #ifdef WINDOWSNT |
199607e4 RS |
1674 | /* Except for network file name. */ |
1675 | if (!(IS_DIRECTORY_SEP (target[0]) && IS_DIRECTORY_SEP (target[1]))) | |
5e570b75 | 1676 | #endif /* WINDOWSNT */ |
4c3c22f3 | 1677 | { |
199607e4 | 1678 | if (!drive) abort (); |
4c3c22f3 | 1679 | target -= 2; |
f94988a7 | 1680 | target[0] = DRIVE_LETTER (drive); |
4c3c22f3 RS |
1681 | target[1] = ':'; |
1682 | } | |
f0f95d31 RS |
1683 | /* Reinsert the escape prefix if required. */ |
1684 | if (is_escaped) | |
1685 | { | |
1686 | target -= 2; | |
1687 | target[0] = '/'; | |
1688 | target[1] = ':'; | |
1689 | } | |
199607e4 | 1690 | CORRECT_DIR_SEPS (target); |
5e570b75 | 1691 | #endif /* DOS_NT */ |
4c3c22f3 | 1692 | |
beb402de KG |
1693 | result = make_specified_string (target, -1, o - target, |
1694 | STRING_MULTIBYTE (name)); | |
1695 | ||
1696 | /* Again look to see if the file name has special constructs in it | |
1697 | and perhaps call the corresponding file handler. This is needed | |
1698 | for filenames such as "/foo/../user@host:/bar/../baz". Expanding | |
1699 | the ".." component gives us "/user@host:/bar/../baz" which needs | |
1700 | to be expanded again. */ | |
1701 | handler = Ffind_file_name_handler (result, Qexpand_file_name); | |
1702 | if (!NILP (handler)) | |
1703 | return call3 (handler, Qexpand_file_name, result, default_directory); | |
1704 | ||
1705 | return result; | |
570d7624 | 1706 | } |
5e570b75 | 1707 | |
4887597a EZ |
1708 | #if 0 |
1709 | /* PLEASE DO NOT DELETE THIS COMMENTED-OUT VERSION! | |
1710 | This is the old version of expand-file-name, before it was thoroughly | |
1711 | rewritten for Emacs 10.31. We leave this version here commented-out, | |
1712 | because the code is very complex and likely to have subtle bugs. If | |
1713 | bugs _are_ found, it might be of interest to look at the old code and | |
1714 | see what did it do in the relevant situation. | |
1715 | ||
1716 | Don't remove this code: it's true that it will be accessible via CVS, | |
1717 | but a few years from deletion, people will forget it is there. */ | |
1718 | ||
1719 | /* Changed this DEFUN to a DEAFUN, so as not to confuse `make-docfile'. */ | |
1720 | DEAFUN ("expand-file-name", Fexpand_file_name, Sexpand_file_name, 1, 2, 0, | |
1721 | "Convert FILENAME to absolute, and canonicalize it.\n\ | |
1722 | Second arg DEFAULT is directory to start with if FILENAME is relative\n\ | |
1723 | (does not start with slash); if DEFAULT is nil or missing,\n\ | |
1724 | the current buffer's value of default-directory is used.\n\ | |
1725 | Filenames containing `.' or `..' as components are simplified;\n\ | |
1726 | initial `~/' expands to your home directory.\n\ | |
1727 | See also the function `substitute-in-file-name'.") | |
1728 | (name, defalt) | |
1729 | Lisp_Object name, defalt; | |
1730 | { | |
1731 | unsigned char *nm; | |
1732 | ||
1733 | register unsigned char *newdir, *p, *o; | |
1734 | int tlen; | |
1735 | unsigned char *target; | |
1736 | struct passwd *pw; | |
1737 | int lose; | |
1738 | #ifdef VMS | |
1739 | unsigned char * colon = 0; | |
1740 | unsigned char * close = 0; | |
1741 | unsigned char * slash = 0; | |
1742 | unsigned char * brack = 0; | |
1743 | int lbrack = 0, rbrack = 0; | |
1744 | int dots = 0; | |
1745 | #endif /* VMS */ | |
1746 | ||
b7826503 | 1747 | CHECK_STRING (name); |
4887597a EZ |
1748 | |
1749 | #ifdef VMS | |
1750 | /* Filenames on VMS are always upper case. */ | |
1751 | name = Fupcase (name); | |
1752 | #endif | |
1753 | ||
d5db4077 | 1754 | nm = SDATA (name); |
4887597a EZ |
1755 | |
1756 | /* If nm is absolute, flush ...// and detect /./ and /../. | |
1757 | If no /./ or /../ we can return right away. */ | |
1758 | if ( | |
1759 | nm[0] == '/' | |
1760 | #ifdef VMS | |
1761 | || index (nm, ':') | |
1762 | #endif /* VMS */ | |
1763 | ) | |
1764 | { | |
1765 | p = nm; | |
1766 | lose = 0; | |
1767 | while (*p) | |
1768 | { | |
1769 | if (p[0] == '/' && p[1] == '/' | |
1770 | #ifdef APOLLO | |
1771 | /* // at start of filename is meaningful on Apollo system. */ | |
1772 | && nm != p | |
1773 | #endif /* APOLLO */ | |
1774 | ) | |
1775 | nm = p + 1; | |
1776 | if (p[0] == '/' && p[1] == '~') | |
1777 | nm = p + 1, lose = 1; | |
1778 | if (p[0] == '/' && p[1] == '.' | |
1779 | && (p[2] == '/' || p[2] == 0 | |
1780 | || (p[2] == '.' && (p[3] == '/' || p[3] == 0)))) | |
1781 | lose = 1; | |
1782 | #ifdef VMS | |
1783 | if (p[0] == '\\') | |
1784 | lose = 1; | |
1785 | if (p[0] == '/') { | |
1786 | /* if dev:[dir]/, move nm to / */ | |
1787 | if (!slash && p > nm && (brack || colon)) { | |
1788 | nm = (brack ? brack + 1 : colon + 1); | |
1789 | lbrack = rbrack = 0; | |
1790 | brack = 0; | |
1791 | colon = 0; | |
1792 | } | |
1793 | slash = p; | |
1794 | } | |
1795 | if (p[0] == '-') | |
1796 | #ifndef VMS4_4 | |
1797 | /* VMS pre V4.4,convert '-'s in filenames. */ | |
1798 | if (lbrack == rbrack) | |
1799 | { | |
1800 | if (dots < 2) /* this is to allow negative version numbers */ | |
1801 | p[0] = '_'; | |
1802 | } | |
1803 | else | |
1804 | #endif /* VMS4_4 */ | |
1805 | if (lbrack > rbrack && | |
1806 | ((p[-1] == '.' || p[-1] == '[' || p[-1] == '<') && | |
1807 | (p[1] == '.' || p[1] == ']' || p[1] == '>'))) | |
1808 | lose = 1; | |
1809 | #ifndef VMS4_4 | |
1810 | else | |
1811 | p[0] = '_'; | |
1812 | #endif /* VMS4_4 */ | |
1813 | /* count open brackets, reset close bracket pointer */ | |
1814 | if (p[0] == '[' || p[0] == '<') | |
1815 | lbrack++, brack = 0; | |
1816 | /* count close brackets, set close bracket pointer */ | |
1817 | if (p[0] == ']' || p[0] == '>') | |
1818 | rbrack++, brack = p; | |
1819 | /* detect ][ or >< */ | |
1820 | if ((p[0] == ']' || p[0] == '>') && (p[1] == '[' || p[1] == '<')) | |
1821 | lose = 1; | |
1822 | if ((p[0] == ':' || p[0] == ']' || p[0] == '>') && p[1] == '~') | |
1823 | nm = p + 1, lose = 1; | |
1824 | if (p[0] == ':' && (colon || slash)) | |
1825 | /* if dev1:[dir]dev2:, move nm to dev2: */ | |
1826 | if (brack) | |
1827 | { | |
1828 | nm = brack + 1; | |
1829 | brack = 0; | |
1830 | } | |
1831 | /* If /name/dev:, move nm to dev: */ | |
1832 | else if (slash) | |
1833 | nm = slash + 1; | |
1834 | /* If node::dev:, move colon following dev */ | |
1835 | else if (colon && colon[-1] == ':') | |
1836 | colon = p; | |
1837 | /* If dev1:dev2:, move nm to dev2: */ | |
1838 | else if (colon && colon[-1] != ':') | |
1839 | { | |
1840 | nm = colon + 1; | |
1841 | colon = 0; | |
1842 | } | |
1843 | if (p[0] == ':' && !colon) | |
1844 | { | |
1845 | if (p[1] == ':') | |
1846 | p++; | |
1847 | colon = p; | |
1848 | } | |
1849 | if (lbrack == rbrack) | |
1850 | if (p[0] == ';') | |
1851 | dots = 2; | |
1852 | else if (p[0] == '.') | |
1853 | dots++; | |
1854 | #endif /* VMS */ | |
1855 | p++; | |
1856 | } | |
1857 | if (!lose) | |
1858 | { | |
1859 | #ifdef VMS | |
1860 | if (index (nm, '/')) | |
1861 | return build_string (sys_translate_unix (nm)); | |
1862 | #endif /* VMS */ | |
d5db4077 | 1863 | if (nm == SDATA (name)) |
4887597a EZ |
1864 | return name; |
1865 | return build_string (nm); | |
1866 | } | |
1867 | } | |
1868 | ||
1869 | /* Now determine directory to start with and put it in NEWDIR */ | |
1870 | ||
1871 | newdir = 0; | |
1872 | ||
1873 | if (nm[0] == '~') /* prefix ~ */ | |
1874 | if (nm[1] == '/' | |
1875 | #ifdef VMS | |
1876 | || nm[1] == ':' | |
1877 | #endif /* VMS */ | |
1878 | || nm[1] == 0)/* ~/filename */ | |
1879 | { | |
1880 | if (!(newdir = (unsigned char *) egetenv ("HOME"))) | |
1881 | newdir = (unsigned char *) ""; | |
1882 | nm++; | |
1883 | #ifdef VMS | |
1884 | nm++; /* Don't leave the slash in nm. */ | |
1885 | #endif /* VMS */ | |
1886 | } | |
1887 | else /* ~user/filename */ | |
1888 | { | |
1889 | /* Get past ~ to user */ | |
1890 | unsigned char *user = nm + 1; | |
1891 | /* Find end of name. */ | |
1892 | unsigned char *ptr = (unsigned char *) index (user, '/'); | |
1893 | int len = ptr ? ptr - user : strlen (user); | |
1894 | #ifdef VMS | |
1895 | unsigned char *ptr1 = index (user, ':'); | |
1896 | if (ptr1 != 0 && ptr1 - user < len) | |
1897 | len = ptr1 - user; | |
1898 | #endif /* VMS */ | |
1899 | /* Copy the user name into temp storage. */ | |
1900 | o = (unsigned char *) alloca (len + 1); | |
1901 | bcopy ((char *) user, o, len); | |
1902 | o[len] = 0; | |
1903 | ||
1904 | /* Look up the user name. */ | |
1905 | pw = (struct passwd *) getpwnam (o + 1); | |
1906 | if (!pw) | |
1907 | error ("\"%s\" isn't a registered user", o + 1); | |
1908 | ||
1909 | newdir = (unsigned char *) pw->pw_dir; | |
1910 | ||
1911 | /* Discard the user name from NM. */ | |
1912 | nm += len; | |
1913 | } | |
1914 | ||
1915 | if (nm[0] != '/' | |
1916 | #ifdef VMS | |
1917 | && !index (nm, ':') | |
1918 | #endif /* not VMS */ | |
1919 | && !newdir) | |
1920 | { | |
1921 | if (NILP (defalt)) | |
1922 | defalt = current_buffer->directory; | |
b7826503 | 1923 | CHECK_STRING (defalt); |
d5db4077 | 1924 | newdir = SDATA (defalt); |
4887597a EZ |
1925 | } |
1926 | ||
1927 | /* Now concatenate the directory and name to new space in the stack frame */ | |
1928 | ||
1929 | tlen = (newdir ? strlen (newdir) + 1 : 0) + strlen (nm) + 1; | |
1930 | target = (unsigned char *) alloca (tlen); | |
1931 | *target = 0; | |
1932 | ||
1933 | if (newdir) | |
1934 | { | |
1935 | #ifndef VMS | |
1936 | if (nm[0] == 0 || nm[0] == '/') | |
1937 | strcpy (target, newdir); | |
1938 | else | |
1939 | #endif | |
1940 | file_name_as_directory (target, newdir); | |
1941 | } | |
1942 | ||
1943 | strcat (target, nm); | |
1944 | #ifdef VMS | |
1945 | if (index (target, '/')) | |
1946 | strcpy (target, sys_translate_unix (target)); | |
1947 | #endif /* VMS */ | |
1948 | ||
1949 | /* Now canonicalize by removing /. and /foo/.. if they appear */ | |
1950 | ||
1951 | p = target; | |
1952 | o = target; | |
1953 | ||
1954 | while (*p) | |
1955 | { | |
1956 | #ifdef VMS | |
1957 | if (*p != ']' && *p != '>' && *p != '-') | |
1958 | { | |
1959 | if (*p == '\\') | |
1960 | p++; | |
1961 | *o++ = *p++; | |
1962 | } | |
1963 | else if ((p[0] == ']' || p[0] == '>') && p[0] == p[1] + 2) | |
1964 | /* brackets are offset from each other by 2 */ | |
1965 | { | |
1966 | p += 2; | |
1967 | if (*p != '.' && *p != '-' && o[-1] != '.') | |
1968 | /* convert [foo][bar] to [bar] */ | |
1969 | while (o[-1] != '[' && o[-1] != '<') | |
1970 | o--; | |
1971 | else if (*p == '-' && *o != '.') | |
1972 | *--p = '.'; | |
1973 | } | |
1974 | else if (p[0] == '-' && o[-1] == '.' && | |
1975 | (p[1] == '.' || p[1] == ']' || p[1] == '>')) | |
1976 | /* flush .foo.- ; leave - if stopped by '[' or '<' */ | |
1977 | { | |
1978 | do | |
1979 | o--; | |
1980 | while (o[-1] != '.' && o[-1] != '[' && o[-1] != '<'); | |
1981 | if (p[1] == '.') /* foo.-.bar ==> bar. */ | |
1982 | p += 2; | |
1983 | else if (o[-1] == '.') /* '.foo.-]' ==> ']' */ | |
1984 | p++, o--; | |
1985 | /* else [foo.-] ==> [-] */ | |
1986 | } | |
1987 | else | |
1988 | { | |
1989 | #ifndef VMS4_4 | |
1990 | if (*p == '-' && | |
1991 | o[-1] != '[' && o[-1] != '<' && o[-1] != '.' && | |
1992 | p[1] != ']' && p[1] != '>' && p[1] != '.') | |
1993 | *p = '_'; | |
1994 | #endif /* VMS4_4 */ | |
1995 | *o++ = *p++; | |
1996 | } | |
1997 | #else /* not VMS */ | |
1998 | if (*p != '/') | |
1999 | { | |
2000 | *o++ = *p++; | |
2001 | } | |
2002 | else if (!strncmp (p, "//", 2) | |
2003 | #ifdef APOLLO | |
2004 | /* // at start of filename is meaningful in Apollo system. */ | |
2005 | && o != target | |
2006 | #endif /* APOLLO */ | |
2007 | ) | |
2008 | { | |
2009 | o = target; | |
2010 | p++; | |
2011 | } | |
2012 | else if (p[0] == '/' && p[1] == '.' && | |
2013 | (p[2] == '/' || p[2] == 0)) | |
2014 | p += 2; | |
2015 | else if (!strncmp (p, "/..", 3) | |
2016 | /* `/../' is the "superroot" on certain file systems. */ | |
2017 | && o != target | |
2018 | && (p[3] == '/' || p[3] == 0)) | |
2019 | { | |
2020 | while (o != target && *--o != '/') | |
2021 | ; | |
2022 | #ifdef APOLLO | |
2023 | if (o == target + 1 && o[-1] == '/' && o[0] == '/') | |
2024 | ++o; | |
2025 | else | |
2026 | #endif /* APOLLO */ | |
2027 | if (o == target && *o == '/') | |
2028 | ++o; | |
2029 | p += 3; | |
2030 | } | |
2031 | else | |
2032 | { | |
2033 | *o++ = *p++; | |
2034 | } | |
2035 | #endif /* not VMS */ | |
2036 | } | |
2037 | ||
2038 | return make_string (target, o - target); | |
2039 | } | |
2040 | #endif | |
570d7624 JB |
2041 | \f |
2042 | DEFUN ("substitute-in-file-name", Fsubstitute_in_file_name, | |
8c1a1077 PJ |
2043 | Ssubstitute_in_file_name, 1, 1, 0, |
2044 | doc: /* Substitute environment variables referred to in FILENAME. | |
2045 | `$FOO' where FOO is an environment variable name means to substitute | |
2046 | the value of that variable. The variable name should be terminated | |
2047 | with a character not a letter, digit or underscore; otherwise, enclose | |
2048 | the entire variable name in braces. | |
2049 | If `/~' appears, all of FILENAME through that `/' is discarded. | |
2050 | ||
2051 | On VMS, `$' substitution is not done; this function does little and only | |
2052 | duplicates what `expand-file-name' does. */) | |
2053 | (filename) | |
3b7f6e60 | 2054 | Lisp_Object filename; |
570d7624 JB |
2055 | { |
2056 | unsigned char *nm; | |
2057 | ||
2058 | register unsigned char *s, *p, *o, *x, *endp; | |
6bbd7a29 | 2059 | unsigned char *target = NULL; |
570d7624 JB |
2060 | int total = 0; |
2061 | int substituted = 0; | |
2062 | unsigned char *xnm; | |
dba493fe | 2063 | struct passwd *pw; |
8ce069f5 | 2064 | Lisp_Object handler; |
570d7624 | 2065 | |
b7826503 | 2066 | CHECK_STRING (filename); |
570d7624 | 2067 | |
8ce069f5 RS |
2068 | /* If the file name has special constructs in it, |
2069 | call the corresponding file handler. */ | |
3b7f6e60 | 2070 | handler = Ffind_file_name_handler (filename, Qsubstitute_in_file_name); |
8ce069f5 | 2071 | if (!NILP (handler)) |
3b7f6e60 | 2072 | return call2 (handler, Qsubstitute_in_file_name, filename); |
8ce069f5 | 2073 | |
d5db4077 | 2074 | nm = SDATA (filename); |
199607e4 RS |
2075 | #ifdef DOS_NT |
2076 | nm = strcpy (alloca (strlen (nm) + 1), nm); | |
2077 | CORRECT_DIR_SEPS (nm); | |
d5db4077 | 2078 | substituted = (strcmp (nm, SDATA (filename)) != 0); |
a5a1cc06 | 2079 | #endif |
d5db4077 | 2080 | endp = nm + SBYTES (filename); |
570d7624 | 2081 | |
82330e7f | 2082 | /* If /~ or // appears, discard everything through first slash. */ |
570d7624 JB |
2083 | |
2084 | for (p = nm; p != endp; p++) | |
2085 | { | |
199607e4 | 2086 | if ((p[0] == '~' |
c60ee5e7 JB |
2087 | #if defined (APOLLO) || defined (WINDOWSNT) || defined(CYGWIN) |
2088 | /* // at start of file name is meaningful in Apollo, | |
2089 | WindowsNT and Cygwin systems. */ | |
199607e4 | 2090 | || (IS_DIRECTORY_SEP (p[0]) && p - 1 != nm) |
c60ee5e7 | 2091 | #else /* not (APOLLO || WINDOWSNT || CYGWIN) */ |
199607e4 | 2092 | || IS_DIRECTORY_SEP (p[0]) |
c60ee5e7 | 2093 | #endif /* not (APOLLO || WINDOWSNT || CYGWIN) */ |
570d7624 | 2094 | ) |
5e570b75 RS |
2095 | && p != nm |
2096 | && (0 | |
570d7624 | 2097 | #ifdef VMS |
5e570b75 | 2098 | || p[-1] == ':' || p[-1] == ']' || p[-1] == '>' |
570d7624 | 2099 | #endif /* VMS */ |
5e570b75 | 2100 | || IS_DIRECTORY_SEP (p[-1]))) |
570d7624 | 2101 | { |
dba493fe EZ |
2102 | for (s = p; *s && (!IS_DIRECTORY_SEP (*s) |
2103 | #ifdef VMS | |
2104 | && *s != ':' | |
2105 | #endif /* VMS */ | |
2106 | ); s++); | |
b135bd4c | 2107 | if (p[0] == '~' && s > p + 1) /* we've got "/~something/" */ |
dba493fe EZ |
2108 | { |
2109 | o = (unsigned char *) alloca (s - p + 1); | |
2110 | bcopy ((char *) p, o, s - p); | |
2111 | o [s - p] = 0; | |
2112 | ||
2113 | pw = (struct passwd *) getpwnam (o + 1); | |
2114 | } | |
2115 | /* If we have ~/ or ~user and `user' exists, discard | |
2116 | everything up to ~. But if `user' does not exist, leave | |
2117 | ~user alone, it might be a literal file name. */ | |
b135bd4c | 2118 | if (IS_DIRECTORY_SEP (p[0]) || s == p + 1 || pw) |
dba493fe EZ |
2119 | { |
2120 | nm = p; | |
2121 | substituted = 1; | |
2122 | } | |
570d7624 | 2123 | } |
5e570b75 | 2124 | #ifdef DOS_NT |
199607e4 RS |
2125 | /* see comment in expand-file-name about drive specifiers */ |
2126 | else if (IS_DRIVE (p[0]) && p[1] == ':' | |
2127 | && p > nm && IS_DIRECTORY_SEP (p[-1])) | |
4c3c22f3 RS |
2128 | { |
2129 | nm = p; | |
2130 | substituted = 1; | |
2131 | } | |
5e570b75 | 2132 | #endif /* DOS_NT */ |
570d7624 JB |
2133 | } |
2134 | ||
2135 | #ifdef VMS | |
d7231f93 KH |
2136 | return make_specified_string (nm, -1, strlen (nm), |
2137 | STRING_MULTIBYTE (filename)); | |
570d7624 JB |
2138 | #else |
2139 | ||
2140 | /* See if any variables are substituted into the string | |
2141 | and find the total length of their values in `total' */ | |
2142 | ||
2143 | for (p = nm; p != endp;) | |
2144 | if (*p != '$') | |
2145 | p++; | |
2146 | else | |
2147 | { | |
2148 | p++; | |
2149 | if (p == endp) | |
2150 | goto badsubst; | |
2151 | else if (*p == '$') | |
2152 | { | |
2153 | /* "$$" means a single "$" */ | |
2154 | p++; | |
2155 | total -= 1; | |
2156 | substituted = 1; | |
2157 | continue; | |
2158 | } | |
2159 | else if (*p == '{') | |
2160 | { | |
2161 | o = ++p; | |
2162 | while (p != endp && *p != '}') p++; | |
2163 | if (*p != '}') goto missingclose; | |
2164 | s = p; | |
2165 | } | |
2166 | else | |
2167 | { | |
2168 | o = p; | |
2169 | while (p != endp && (isalnum (*p) || *p == '_')) p++; | |
2170 | s = p; | |
2171 | } | |
2172 | ||
2173 | /* Copy out the variable name */ | |
2174 | target = (unsigned char *) alloca (s - o + 1); | |
2175 | strncpy (target, o, s - o); | |
2176 | target[s - o] = 0; | |
5e570b75 | 2177 | #ifdef DOS_NT |
4c3c22f3 | 2178 | strupr (target); /* $home == $HOME etc. */ |
5e570b75 | 2179 | #endif /* DOS_NT */ |
570d7624 JB |
2180 | |
2181 | /* Get variable value */ | |
2182 | o = (unsigned char *) egetenv (target); | |
8d2ced53 SM |
2183 | if (o) |
2184 | { | |
2185 | total += strlen (o); | |
2186 | substituted = 1; | |
2187 | } | |
2188 | else if (*p == '}') | |
2189 | goto badvar; | |
570d7624 JB |
2190 | } |
2191 | ||
2192 | if (!substituted) | |
3b7f6e60 | 2193 | return filename; |
570d7624 JB |
2194 | |
2195 | /* If substitution required, recopy the string and do it */ | |
2196 | /* Make space in stack frame for the new copy */ | |
d5db4077 | 2197 | xnm = (unsigned char *) alloca (SBYTES (filename) + total + 1); |
570d7624 JB |
2198 | x = xnm; |
2199 | ||
2200 | /* Copy the rest of the name through, replacing $ constructs with values */ | |
2201 | for (p = nm; *p;) | |
2202 | if (*p != '$') | |
2203 | *x++ = *p++; | |
2204 | else | |
2205 | { | |
2206 | p++; | |
2207 | if (p == endp) | |
2208 | goto badsubst; | |
2209 | else if (*p == '$') | |
2210 | { | |
2211 | *x++ = *p++; | |
2212 | continue; | |
2213 | } | |
2214 | else if (*p == '{') | |
2215 | { | |
2216 | o = ++p; | |
2217 | while (p != endp && *p != '}') p++; | |
2218 | if (*p != '}') goto missingclose; | |
2219 | s = p++; | |
2220 | } | |
2221 | else | |
2222 | { | |
2223 | o = p; | |
2224 | while (p != endp && (isalnum (*p) || *p == '_')) p++; | |
2225 | s = p; | |
2226 | } | |
2227 | ||
2228 | /* Copy out the variable name */ | |
2229 | target = (unsigned char *) alloca (s - o + 1); | |
2230 | strncpy (target, o, s - o); | |
2231 | target[s - o] = 0; | |
5e570b75 | 2232 | #ifdef DOS_NT |
4c3c22f3 | 2233 | strupr (target); /* $home == $HOME etc. */ |
5e570b75 | 2234 | #endif /* DOS_NT */ |
570d7624 JB |
2235 | |
2236 | /* Get variable value */ | |
2237 | o = (unsigned char *) egetenv (target); | |
570d7624 | 2238 | if (!o) |
8d2ced53 SM |
2239 | { |
2240 | *x++ = '$'; | |
2241 | strcpy (x, target); x+= strlen (target); | |
2242 | } | |
2243 | else if (STRING_MULTIBYTE (filename)) | |
60d67b83 RS |
2244 | { |
2245 | /* If the original string is multibyte, | |
2246 | convert what we substitute into multibyte. */ | |
60d67b83 RS |
2247 | while (*o) |
2248 | { | |
ce51c54c KH |
2249 | int c = unibyte_char_to_multibyte (*o++); |
2250 | x += CHAR_STRING (c, x); | |
60d67b83 RS |
2251 | } |
2252 | } | |
2253 | else | |
2254 | { | |
2255 | strcpy (x, o); | |
2256 | x += strlen (o); | |
2257 | } | |
570d7624 JB |
2258 | } |
2259 | ||
2260 | *x = 0; | |
2261 | ||
82330e7f | 2262 | /* If /~ or // appears, discard everything through first slash. */ |
570d7624 JB |
2263 | |
2264 | for (p = xnm; p != x; p++) | |
5e570b75 | 2265 | if ((p[0] == '~' |
c60ee5e7 | 2266 | #if defined (APOLLO) || defined (WINDOWSNT) || defined(CYGWIN) |
5e570b75 | 2267 | || (IS_DIRECTORY_SEP (p[0]) && p - 1 != xnm) |
c60ee5e7 | 2268 | #else /* not (APOLLO || WINDOWSNT || CYGWIN) */ |
199607e4 | 2269 | || IS_DIRECTORY_SEP (p[0]) |
c60ee5e7 | 2270 | #endif /* not (APOLLO || WINDOWSNT || CYGWIN) */ |
570d7624 | 2271 | ) |
ac5b7072 | 2272 | && p != xnm && IS_DIRECTORY_SEP (p[-1])) |
570d7624 | 2273 | xnm = p; |
5e570b75 | 2274 | #ifdef DOS_NT |
199607e4 | 2275 | else if (IS_DRIVE (p[0]) && p[1] == ':' |
4488d7e1 | 2276 | && p > xnm && IS_DIRECTORY_SEP (p[-1])) |
199607e4 | 2277 | xnm = p; |
4c3c22f3 | 2278 | #endif |
570d7624 | 2279 | |
d7231f93 | 2280 | return make_specified_string (xnm, -1, x - xnm, STRING_MULTIBYTE (filename)); |
570d7624 JB |
2281 | |
2282 | badsubst: | |
2283 | error ("Bad format environment-variable substitution"); | |
2284 | missingclose: | |
2285 | error ("Missing \"}\" in environment-variable substitution"); | |
2286 | badvar: | |
2287 | error ("Substituting nonexistent environment variable \"%s\"", target); | |
2288 | ||
2289 | /* NOTREACHED */ | |
2290 | #endif /* not VMS */ | |
6bbd7a29 | 2291 | return Qnil; |
570d7624 JB |
2292 | } |
2293 | \f | |
067ffa38 | 2294 | /* A slightly faster and more convenient way to get |
298b760e | 2295 | (directory-file-name (expand-file-name FOO)). */ |
067ffa38 | 2296 | |
570d7624 JB |
2297 | Lisp_Object |
2298 | expand_and_dir_to_file (filename, defdir) | |
2299 | Lisp_Object filename, defdir; | |
2300 | { | |
199607e4 | 2301 | register Lisp_Object absname; |
570d7624 | 2302 | |
199607e4 | 2303 | absname = Fexpand_file_name (filename, defdir); |
570d7624 JB |
2304 | #ifdef VMS |
2305 | { | |
d5db4077 | 2306 | register int c = SREF (absname, SBYTES (absname) - 1); |
570d7624 | 2307 | if (c == ':' || c == ']' || c == '>') |
199607e4 | 2308 | absname = Fdirectory_file_name (absname); |
570d7624 JB |
2309 | } |
2310 | #else | |
199607e4 | 2311 | /* Remove final slash, if any (unless this is the root dir). |
570d7624 | 2312 | stat behaves differently depending! */ |
d5db4077 KR |
2313 | if (SCHARS (absname) > 1 |
2314 | && IS_DIRECTORY_SEP (SREF (absname, SBYTES (absname) - 1)) | |
2315 | && !IS_DEVICE_SEP (SREF (absname, SBYTES (absname)-2))) | |
ddc61f46 | 2316 | /* We cannot take shortcuts; they might be wrong for magic file names. */ |
199607e4 | 2317 | absname = Fdirectory_file_name (absname); |
570d7624 | 2318 | #endif |
199607e4 | 2319 | return absname; |
570d7624 JB |
2320 | } |
2321 | \f | |
3ed15d97 RS |
2322 | /* Signal an error if the file ABSNAME already exists. |
2323 | If INTERACTIVE is nonzero, ask the user whether to proceed, | |
2324 | and bypass the error if the user says to go ahead. | |
2325 | QUERYSTRING is a name for the action that is being considered | |
2326 | to alter the file. | |
de1d0127 | 2327 | |
3ed15d97 | 2328 | *STATPTR is used to store the stat information if the file exists. |
de1d0127 | 2329 | If the file does not exist, STATPTR->st_mode is set to 0. |
b8b29dc9 RS |
2330 | If STATPTR is null, we don't store into it. |
2331 | ||
2332 | If QUICK is nonzero, we ask for y or n, not yes or no. */ | |
3ed15d97 | 2333 | |
c4df73f9 | 2334 | void |
b8b29dc9 | 2335 | barf_or_query_if_file_exists (absname, querystring, interactive, statptr, quick) |
570d7624 JB |
2336 | Lisp_Object absname; |
2337 | unsigned char *querystring; | |
2338 | int interactive; | |
3ed15d97 | 2339 | struct stat *statptr; |
b8b29dc9 | 2340 | int quick; |
570d7624 | 2341 | { |
643c73b9 | 2342 | register Lisp_Object tem, encoded_filename; |
4018b5ef | 2343 | struct stat statbuf; |
570d7624 JB |
2344 | struct gcpro gcpro1; |
2345 | ||
643c73b9 RS |
2346 | encoded_filename = ENCODE_FILE (absname); |
2347 | ||
4018b5ef RS |
2348 | /* stat is a good way to tell whether the file exists, |
2349 | regardless of what access permissions it has. */ | |
d5db4077 | 2350 | if (stat (SDATA (encoded_filename), &statbuf) >= 0) |
570d7624 JB |
2351 | { |
2352 | if (! interactive) | |
2353 | Fsignal (Qfile_already_exists, | |
2354 | Fcons (build_string ("File already exists"), | |
2355 | Fcons (absname, Qnil))); | |
2356 | GCPRO1 (absname); | |
67e8e2b8 RS |
2357 | tem = format2 ("File %s already exists; %s anyway? ", |
2358 | absname, build_string (querystring)); | |
b8b29dc9 RS |
2359 | if (quick) |
2360 | tem = Fy_or_n_p (tem); | |
2361 | else | |
2362 | tem = do_yes_or_no_p (tem); | |
570d7624 | 2363 | UNGCPRO; |
265a9e55 | 2364 | if (NILP (tem)) |
570d7624 JB |
2365 | Fsignal (Qfile_already_exists, |
2366 | Fcons (build_string ("File already exists"), | |
2367 | Fcons (absname, Qnil))); | |
3ed15d97 RS |
2368 | if (statptr) |
2369 | *statptr = statbuf; | |
2370 | } | |
2371 | else | |
2372 | { | |
2373 | if (statptr) | |
2374 | statptr->st_mode = 0; | |
570d7624 JB |
2375 | } |
2376 | return; | |
2377 | } | |
2378 | ||
2379 | DEFUN ("copy-file", Fcopy_file, Scopy_file, 2, 4, | |
8c1a1077 PJ |
2380 | "fCopy file: \nFCopy %s to file: \np\nP", |
2381 | doc: /* Copy FILE to NEWNAME. Both args must be strings. | |
2382 | If NEWNAME names a directory, copy FILE there. | |
2383 | Signals a `file-already-exists' error if file NEWNAME already exists, | |
2384 | unless a third argument OK-IF-ALREADY-EXISTS is supplied and non-nil. | |
2385 | A number as third arg means request confirmation if NEWNAME already exists. | |
2386 | This is what happens in interactive use with M-x. | |
2387 | Fourth arg KEEP-TIME non-nil means give the new file the same | |
2388 | last-modified time as the old one. (This works on only some systems.) | |
b8f75eda KG |
2389 | A prefix arg makes KEEP-TIME non-nil. |
2390 | Also set the file modes of the target file to match the source file. */) | |
8c1a1077 | 2391 | (file, newname, ok_if_already_exists, keep_time) |
8ca6602c | 2392 | Lisp_Object file, newname, ok_if_already_exists, keep_time; |
570d7624 JB |
2393 | { |
2394 | int ifd, ofd, n; | |
2395 | char buf[16 * 1024]; | |
3ed15d97 | 2396 | struct stat st, out_st; |
32f4334d | 2397 | Lisp_Object handler; |
b1d1b865 | 2398 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; |
aed13378 | 2399 | int count = SPECPDL_INDEX (); |
f73b0ada | 2400 | int input_file_statable_p; |
b1d1b865 | 2401 | Lisp_Object encoded_file, encoded_newname; |
570d7624 | 2402 | |
b1d1b865 RS |
2403 | encoded_file = encoded_newname = Qnil; |
2404 | GCPRO4 (file, newname, encoded_file, encoded_newname); | |
b7826503 PJ |
2405 | CHECK_STRING (file); |
2406 | CHECK_STRING (newname); | |
b1d1b865 | 2407 | |
a9d14e54 GM |
2408 | if (!NILP (Ffile_directory_p (newname))) |
2409 | newname = Fexpand_file_name (file, newname); | |
2410 | else | |
2411 | newname = Fexpand_file_name (newname, Qnil); | |
2412 | ||
3b7f6e60 | 2413 | file = Fexpand_file_name (file, Qnil); |
32f4334d | 2414 | |
0bf2eed2 | 2415 | /* If the input file name has special constructs in it, |
32f4334d | 2416 | call the corresponding file handler. */ |
3b7f6e60 | 2417 | handler = Ffind_file_name_handler (file, Qcopy_file); |
0bf2eed2 | 2418 | /* Likewise for output file name. */ |
51cf6d37 | 2419 | if (NILP (handler)) |
49307295 | 2420 | handler = Ffind_file_name_handler (newname, Qcopy_file); |
32f4334d | 2421 | if (!NILP (handler)) |
3b7f6e60 | 2422 | RETURN_UNGCPRO (call5 (handler, Qcopy_file, file, newname, |
8ca6602c | 2423 | ok_if_already_exists, keep_time)); |
32f4334d | 2424 | |
b1d1b865 RS |
2425 | encoded_file = ENCODE_FILE (file); |
2426 | encoded_newname = ENCODE_FILE (newname); | |
2427 | ||
265a9e55 | 2428 | if (NILP (ok_if_already_exists) |
93c30b5f | 2429 | || INTEGERP (ok_if_already_exists)) |
b1d1b865 | 2430 | barf_or_query_if_file_exists (encoded_newname, "copy to it", |
b8b29dc9 | 2431 | INTEGERP (ok_if_already_exists), &out_st, 0); |
d5db4077 | 2432 | else if (stat (SDATA (encoded_newname), &out_st) < 0) |
3ed15d97 | 2433 | out_st.st_mode = 0; |
570d7624 | 2434 | |
e8691c59 | 2435 | #ifdef WINDOWSNT |
d5db4077 | 2436 | if (!CopyFile (SDATA (encoded_file), |
efdc16c9 | 2437 | SDATA (encoded_newname), |
e8691c59 GM |
2438 | FALSE)) |
2439 | report_file_error ("Copying file", Fcons (file, Fcons (newname, Qnil))); | |
8b53dc06 JR |
2440 | /* CopyFile retains the timestamp by default. */ |
2441 | else if (NILP (keep_time)) | |
e8691c59 GM |
2442 | { |
2443 | EMACS_TIME now; | |
ad497129 JR |
2444 | DWORD attributes; |
2445 | char * filename; | |
2446 | ||
e8691c59 | 2447 | EMACS_GET_TIME (now); |
d5db4077 | 2448 | filename = SDATA (encoded_newname); |
ad497129 JR |
2449 | |
2450 | /* Ensure file is writable while its modified time is set. */ | |
2451 | attributes = GetFileAttributes (filename); | |
02cca86b | 2452 | SetFileAttributes (filename, attributes & ~FILE_ATTRIBUTE_READONLY); |
ad497129 JR |
2453 | if (set_file_times (filename, now, now)) |
2454 | { | |
2455 | /* Restore original attributes. */ | |
2456 | SetFileAttributes (filename, attributes); | |
2457 | Fsignal (Qfile_date_error, | |
2458 | Fcons (build_string ("Cannot set file date"), | |
2459 | Fcons (newname, Qnil))); | |
2460 | } | |
2461 | /* Restore original attributes. */ | |
2462 | SetFileAttributes (filename, attributes); | |
e8691c59 GM |
2463 | } |
2464 | #else /* not WINDOWSNT */ | |
13336908 | 2465 | immediate_quit = 1; |
d5db4077 | 2466 | ifd = emacs_open (SDATA (encoded_file), O_RDONLY, 0); |
13336908 RS |
2467 | immediate_quit = 0; |
2468 | ||
570d7624 | 2469 | if (ifd < 0) |
3b7f6e60 | 2470 | report_file_error ("Opening input file", Fcons (file, Qnil)); |
570d7624 | 2471 | |
b5148e85 RS |
2472 | record_unwind_protect (close_file_unwind, make_number (ifd)); |
2473 | ||
f73b0ada BF |
2474 | /* We can only copy regular files and symbolic links. Other files are not |
2475 | copyable by us. */ | |
2476 | input_file_statable_p = (fstat (ifd, &st) >= 0); | |
2477 | ||
f9ba66ce | 2478 | #if !defined (DOS_NT) || __DJGPP__ > 1 |
3ed15d97 RS |
2479 | if (out_st.st_mode != 0 |
2480 | && st.st_dev == out_st.st_dev && st.st_ino == out_st.st_ino) | |
2481 | { | |
2482 | errno = 0; | |
2483 | report_file_error ("Input and output files are the same", | |
3b7f6e60 | 2484 | Fcons (file, Fcons (newname, Qnil))); |
3ed15d97 RS |
2485 | } |
2486 | #endif | |
2487 | ||
f73b0ada BF |
2488 | #if defined (S_ISREG) && defined (S_ISLNK) |
2489 | if (input_file_statable_p) | |
2490 | { | |
2491 | if (!(S_ISREG (st.st_mode)) && !(S_ISLNK (st.st_mode))) | |
2492 | { | |
2493 | #if defined (EISDIR) | |
2494 | /* Get a better looking error message. */ | |
2495 | errno = EISDIR; | |
2496 | #endif /* EISDIR */ | |
3b7f6e60 | 2497 | report_file_error ("Non-regular file", Fcons (file, Qnil)); |
f73b0ada BF |
2498 | } |
2499 | } | |
2500 | #endif /* S_ISREG && S_ISLNK */ | |
2501 | ||
570d7624 JB |
2502 | #ifdef VMS |
2503 | /* Create the copy file with the same record format as the input file */ | |
d5db4077 | 2504 | ofd = sys_creat (SDATA (encoded_newname), 0666, ifd); |
570d7624 | 2505 | #else |
4c3c22f3 RS |
2506 | #ifdef MSDOS |
2507 | /* System's default file type was set to binary by _fmode in emacs.c. */ | |
d5db4077 | 2508 | ofd = creat (SDATA (encoded_newname), S_IREAD | S_IWRITE); |
4c3c22f3 | 2509 | #else /* not MSDOS */ |
d5db4077 | 2510 | ofd = creat (SDATA (encoded_newname), 0666); |
4c3c22f3 | 2511 | #endif /* not MSDOS */ |
570d7624 JB |
2512 | #endif /* VMS */ |
2513 | if (ofd < 0) | |
3ed15d97 | 2514 | report_file_error ("Opening output file", Fcons (newname, Qnil)); |
b5148e85 RS |
2515 | |
2516 | record_unwind_protect (close_file_unwind, make_number (ofd)); | |
570d7624 | 2517 | |
b5148e85 RS |
2518 | immediate_quit = 1; |
2519 | QUIT; | |
68c45bf0 PE |
2520 | while ((n = emacs_read (ifd, buf, sizeof buf)) > 0) |
2521 | if (emacs_write (ofd, buf, n) != n) | |
3ed15d97 | 2522 | report_file_error ("I/O error", Fcons (newname, Qnil)); |
b5148e85 | 2523 | immediate_quit = 0; |
570d7624 | 2524 | |
5acac34e | 2525 | /* Closing the output clobbers the file times on some systems. */ |
68c45bf0 | 2526 | if (emacs_close (ofd) < 0) |
5acac34e RS |
2527 | report_file_error ("I/O error", Fcons (newname, Qnil)); |
2528 | ||
f73b0ada | 2529 | if (input_file_statable_p) |
570d7624 | 2530 | { |
8ca6602c | 2531 | if (!NILP (keep_time)) |
570d7624 | 2532 | { |
de5bf5d3 JB |
2533 | EMACS_TIME atime, mtime; |
2534 | EMACS_SET_SECS_USECS (atime, st.st_atime, 0); | |
2535 | EMACS_SET_SECS_USECS (mtime, st.st_mtime, 0); | |
d5db4077 | 2536 | if (set_file_times (SDATA (encoded_newname), |
b1d1b865 | 2537 | atime, mtime)) |
c0b7b21c | 2538 | Fsignal (Qfile_date_error, |
d1b9ed63 | 2539 | Fcons (build_string ("Cannot set file date"), |
3dbcf3f6 | 2540 | Fcons (newname, Qnil))); |
570d7624 | 2541 | } |
2dc3be7e | 2542 | #ifndef MSDOS |
d5db4077 | 2543 | chmod (SDATA (encoded_newname), st.st_mode & 07777); |
2dc3be7e RS |
2544 | #else /* MSDOS */ |
2545 | #if defined (__DJGPP__) && __DJGPP__ > 1 | |
2546 | /* In DJGPP v2.0 and later, fstat usually returns true file mode bits, | |
2547 | and if it can't, it tells so. Otherwise, under MSDOS we usually | |
2548 | get only the READ bit, which will make the copied file read-only, | |
2549 | so it's better not to chmod at all. */ | |
2550 | if ((_djstat_flags & _STFAIL_WRITEBIT) == 0) | |
d5db4077 | 2551 | chmod (SDATA (encoded_newname), st.st_mode & 07777); |
2dc3be7e RS |
2552 | #endif /* DJGPP version 2 or newer */ |
2553 | #endif /* MSDOS */ | |
570d7624 JB |
2554 | } |
2555 | ||
68c45bf0 | 2556 | emacs_close (ifd); |
e8691c59 | 2557 | #endif /* WINDOWSNT */ |
5acac34e | 2558 | |
b5148e85 RS |
2559 | /* Discard the unwind protects. */ |
2560 | specpdl_ptr = specpdl + count; | |
2561 | ||
570d7624 JB |
2562 | UNGCPRO; |
2563 | return Qnil; | |
2564 | } | |
385b6cc7 | 2565 | \f |
9bbe01fb | 2566 | DEFUN ("make-directory-internal", Fmake_directory_internal, |
353cfc19 | 2567 | Smake_directory_internal, 1, 1, 0, |
8c1a1077 PJ |
2568 | doc: /* Create a new directory named DIRECTORY. */) |
2569 | (directory) | |
3b7f6e60 | 2570 | Lisp_Object directory; |
570d7624 | 2571 | { |
19290c65 | 2572 | const unsigned char *dir; |
32f4334d | 2573 | Lisp_Object handler; |
b1d1b865 | 2574 | Lisp_Object encoded_dir; |
570d7624 | 2575 | |
b7826503 | 2576 | CHECK_STRING (directory); |
3b7f6e60 | 2577 | directory = Fexpand_file_name (directory, Qnil); |
32f4334d | 2578 | |
3b7f6e60 | 2579 | handler = Ffind_file_name_handler (directory, Qmake_directory_internal); |
32f4334d | 2580 | if (!NILP (handler)) |
3b7f6e60 | 2581 | return call2 (handler, Qmake_directory_internal, directory); |
9bbe01fb | 2582 | |
b1d1b865 RS |
2583 | encoded_dir = ENCODE_FILE (directory); |
2584 | ||
d5db4077 | 2585 | dir = SDATA (encoded_dir); |
570d7624 | 2586 | |
5e570b75 RS |
2587 | #ifdef WINDOWSNT |
2588 | if (mkdir (dir) != 0) | |
2589 | #else | |
570d7624 | 2590 | if (mkdir (dir, 0777) != 0) |
5e570b75 | 2591 | #endif |
3b7f6e60 | 2592 | report_file_error ("Creating directory", Flist (1, &directory)); |
570d7624 | 2593 | |
32f4334d | 2594 | return Qnil; |
570d7624 JB |
2595 | } |
2596 | ||
aa734e17 | 2597 | DEFUN ("delete-directory", Fdelete_directory, Sdelete_directory, 1, 1, "FDelete directory: ", |
efdc16c9 | 2598 | doc: /* Delete the directory named DIRECTORY. Does not follow symlinks. */) |
8c1a1077 | 2599 | (directory) |
3b7f6e60 | 2600 | Lisp_Object directory; |
570d7624 | 2601 | { |
19290c65 | 2602 | const unsigned char *dir; |
32f4334d | 2603 | Lisp_Object handler; |
b1d1b865 | 2604 | Lisp_Object encoded_dir; |
570d7624 | 2605 | |
b7826503 | 2606 | CHECK_STRING (directory); |
3b7f6e60 | 2607 | directory = Fdirectory_file_name (Fexpand_file_name (directory, Qnil)); |
570d7624 | 2608 | |
3b7f6e60 | 2609 | handler = Ffind_file_name_handler (directory, Qdelete_directory); |
32f4334d | 2610 | if (!NILP (handler)) |
3b7f6e60 | 2611 | return call2 (handler, Qdelete_directory, directory); |
32f4334d | 2612 | |
b1d1b865 RS |
2613 | encoded_dir = ENCODE_FILE (directory); |
2614 | ||
d5db4077 | 2615 | dir = SDATA (encoded_dir); |
b1d1b865 | 2616 | |
570d7624 | 2617 | if (rmdir (dir) != 0) |
3b7f6e60 | 2618 | report_file_error ("Removing directory", Flist (1, &directory)); |
570d7624 JB |
2619 | |
2620 | return Qnil; | |
2621 | } | |
2622 | ||
2623 | DEFUN ("delete-file", Fdelete_file, Sdelete_file, 1, 1, "fDelete file: ", | |
efdc16c9 | 2624 | doc: /* Delete file named FILENAME. If it is a symlink, remove the symlink. |
8c1a1077 PJ |
2625 | If file has multiple names, it continues to exist with the other names. */) |
2626 | (filename) | |
570d7624 JB |
2627 | Lisp_Object filename; |
2628 | { | |
32f4334d | 2629 | Lisp_Object handler; |
b1d1b865 | 2630 | Lisp_Object encoded_file; |
efdc16c9 | 2631 | struct gcpro gcpro1; |
b1d1b865 | 2632 | |
efdc16c9 | 2633 | GCPRO1 (filename); |
b4bd27c5 RS |
2634 | if (!NILP (Ffile_directory_p (filename)) |
2635 | && NILP (Ffile_symlink_p (filename))) | |
efdc16c9 FP |
2636 | Fsignal (Qfile_error, |
2637 | Fcons (build_string ("Removing old name: is a directory"), | |
2638 | Fcons (filename, Qnil))); | |
2639 | UNGCPRO; | |
570d7624 | 2640 | filename = Fexpand_file_name (filename, Qnil); |
32f4334d | 2641 | |
49307295 | 2642 | handler = Ffind_file_name_handler (filename, Qdelete_file); |
32f4334d | 2643 | if (!NILP (handler)) |
8a9b0da9 | 2644 | return call2 (handler, Qdelete_file, filename); |
32f4334d | 2645 | |
b1d1b865 RS |
2646 | encoded_file = ENCODE_FILE (filename); |
2647 | ||
d5db4077 | 2648 | if (0 > unlink (SDATA (encoded_file))) |
570d7624 | 2649 | report_file_error ("Removing old name", Flist (1, &filename)); |
8a9b0da9 | 2650 | return Qnil; |
570d7624 JB |
2651 | } |
2652 | ||
385b6cc7 RS |
2653 | static Lisp_Object |
2654 | internal_delete_file_1 (ignore) | |
2655 | Lisp_Object ignore; | |
2656 | { | |
2657 | return Qt; | |
2658 | } | |
2659 | ||
2660 | /* Delete file FILENAME, returning 1 if successful and 0 if failed. */ | |
2661 | ||
2662 | int | |
2663 | internal_delete_file (filename) | |
2664 | Lisp_Object filename; | |
2665 | { | |
2666 | return NILP (internal_condition_case_1 (Fdelete_file, filename, | |
2667 | Qt, internal_delete_file_1)); | |
2668 | } | |
2669 | \f | |
570d7624 | 2670 | DEFUN ("rename-file", Frename_file, Srename_file, 2, 3, |
8c1a1077 PJ |
2671 | "fRename file: \nFRename %s to file: \np", |
2672 | doc: /* Rename FILE as NEWNAME. Both args strings. | |
2673 | If file has names other than FILE, it continues to have those names. | |
2674 | Signals a `file-already-exists' error if a file NEWNAME already exists | |
2675 | unless optional third argument OK-IF-ALREADY-EXISTS is non-nil. | |
2676 | A number as third arg means request confirmation if NEWNAME already exists. | |
2677 | This is what happens in interactive use with M-x. */) | |
2678 | (file, newname, ok_if_already_exists) | |
3b7f6e60 | 2679 | Lisp_Object file, newname, ok_if_already_exists; |
570d7624 JB |
2680 | { |
2681 | #ifdef NO_ARG_ARRAY | |
2682 | Lisp_Object args[2]; | |
2683 | #endif | |
32f4334d | 2684 | Lisp_Object handler; |
b1d1b865 RS |
2685 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; |
2686 | Lisp_Object encoded_file, encoded_newname; | |
570d7624 | 2687 | |
b1d1b865 RS |
2688 | encoded_file = encoded_newname = Qnil; |
2689 | GCPRO4 (file, newname, encoded_file, encoded_newname); | |
b7826503 PJ |
2690 | CHECK_STRING (file); |
2691 | CHECK_STRING (newname); | |
3b7f6e60 | 2692 | file = Fexpand_file_name (file, Qnil); |
570d7624 | 2693 | newname = Fexpand_file_name (newname, Qnil); |
32f4334d RS |
2694 | |
2695 | /* If the file name has special constructs in it, | |
2696 | call the corresponding file handler. */ | |
3b7f6e60 | 2697 | handler = Ffind_file_name_handler (file, Qrename_file); |
51cf6d37 | 2698 | if (NILP (handler)) |
49307295 | 2699 | handler = Ffind_file_name_handler (newname, Qrename_file); |
32f4334d | 2700 | if (!NILP (handler)) |
36712b0a | 2701 | RETURN_UNGCPRO (call4 (handler, Qrename_file, |
3b7f6e60 | 2702 | file, newname, ok_if_already_exists)); |
32f4334d | 2703 | |
b1d1b865 RS |
2704 | encoded_file = ENCODE_FILE (file); |
2705 | encoded_newname = ENCODE_FILE (newname); | |
2706 | ||
bc77278f EZ |
2707 | #ifdef DOS_NT |
2708 | /* If the file names are identical but for the case, don't ask for | |
2709 | confirmation: they simply want to change the letter-case of the | |
2710 | file name. */ | |
2711 | if (NILP (Fstring_equal (Fdowncase (file), Fdowncase (newname)))) | |
2712 | #endif | |
265a9e55 | 2713 | if (NILP (ok_if_already_exists) |
93c30b5f | 2714 | || INTEGERP (ok_if_already_exists)) |
b1d1b865 | 2715 | barf_or_query_if_file_exists (encoded_newname, "rename to it", |
b8b29dc9 | 2716 | INTEGERP (ok_if_already_exists), 0, 0); |
570d7624 | 2717 | #ifndef BSD4_1 |
d5db4077 | 2718 | if (0 > rename (SDATA (encoded_file), SDATA (encoded_newname))) |
570d7624 | 2719 | #else |
d5db4077 KR |
2720 | if (0 > link (SDATA (encoded_file), SDATA (encoded_newname)) |
2721 | || 0 > unlink (SDATA (encoded_file))) | |
570d7624 JB |
2722 | #endif |
2723 | { | |
2724 | if (errno == EXDEV) | |
2725 | { | |
3b7f6e60 | 2726 | Fcopy_file (file, newname, |
d093c3ac RM |
2727 | /* We have already prompted if it was an integer, |
2728 | so don't have copy-file prompt again. */ | |
2729 | NILP (ok_if_already_exists) ? Qnil : Qt, Qt); | |
3b7f6e60 | 2730 | Fdelete_file (file); |
570d7624 JB |
2731 | } |
2732 | else | |
2733 | #ifdef NO_ARG_ARRAY | |
2734 | { | |
3b7f6e60 | 2735 | args[0] = file; |
570d7624 JB |
2736 | args[1] = newname; |
2737 | report_file_error ("Renaming", Flist (2, args)); | |
2738 | } | |
2739 | #else | |
3b7f6e60 | 2740 | report_file_error ("Renaming", Flist (2, &file)); |
570d7624 JB |
2741 | #endif |
2742 | } | |
2743 | UNGCPRO; | |
2744 | return Qnil; | |
2745 | } | |
2746 | ||
2747 | DEFUN ("add-name-to-file", Fadd_name_to_file, Sadd_name_to_file, 2, 3, | |
8c1a1077 PJ |
2748 | "fAdd name to file: \nFName to add to %s: \np", |
2749 | doc: /* Give FILE additional name NEWNAME. Both args strings. | |
2750 | Signals a `file-already-exists' error if a file NEWNAME already exists | |
2751 | unless optional third argument OK-IF-ALREADY-EXISTS is non-nil. | |
2752 | A number as third arg means request confirmation if NEWNAME already exists. | |
2753 | This is what happens in interactive use with M-x. */) | |
2754 | (file, newname, ok_if_already_exists) | |
3b7f6e60 | 2755 | Lisp_Object file, newname, ok_if_already_exists; |
570d7624 JB |
2756 | { |
2757 | #ifdef NO_ARG_ARRAY | |
2758 | Lisp_Object args[2]; | |
2759 | #endif | |
32f4334d | 2760 | Lisp_Object handler; |
b1d1b865 RS |
2761 | Lisp_Object encoded_file, encoded_newname; |
2762 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; | |
570d7624 | 2763 | |
b1d1b865 RS |
2764 | GCPRO4 (file, newname, encoded_file, encoded_newname); |
2765 | encoded_file = encoded_newname = Qnil; | |
b7826503 PJ |
2766 | CHECK_STRING (file); |
2767 | CHECK_STRING (newname); | |
3b7f6e60 | 2768 | file = Fexpand_file_name (file, Qnil); |
570d7624 | 2769 | newname = Fexpand_file_name (newname, Qnil); |
32f4334d RS |
2770 | |
2771 | /* If the file name has special constructs in it, | |
2772 | call the corresponding file handler. */ | |
3b7f6e60 | 2773 | handler = Ffind_file_name_handler (file, Qadd_name_to_file); |
32f4334d | 2774 | if (!NILP (handler)) |
3b7f6e60 | 2775 | RETURN_UNGCPRO (call4 (handler, Qadd_name_to_file, file, |
36712b0a | 2776 | newname, ok_if_already_exists)); |
32f4334d | 2777 | |
adc6741c RS |
2778 | /* If the new name has special constructs in it, |
2779 | call the corresponding file handler. */ | |
2780 | handler = Ffind_file_name_handler (newname, Qadd_name_to_file); | |
2781 | if (!NILP (handler)) | |
3b7f6e60 | 2782 | RETURN_UNGCPRO (call4 (handler, Qadd_name_to_file, file, |
adc6741c RS |
2783 | newname, ok_if_already_exists)); |
2784 | ||
b1d1b865 RS |
2785 | encoded_file = ENCODE_FILE (file); |
2786 | encoded_newname = ENCODE_FILE (newname); | |
2787 | ||
265a9e55 | 2788 | if (NILP (ok_if_already_exists) |
93c30b5f | 2789 | || INTEGERP (ok_if_already_exists)) |
b1d1b865 | 2790 | barf_or_query_if_file_exists (encoded_newname, "make it a new name", |
b8b29dc9 | 2791 | INTEGERP (ok_if_already_exists), 0, 0); |
5e570b75 | 2792 | |
d5db4077 KR |
2793 | unlink (SDATA (newname)); |
2794 | if (0 > link (SDATA (encoded_file), SDATA (encoded_newname))) | |
570d7624 JB |
2795 | { |
2796 | #ifdef NO_ARG_ARRAY | |
3b7f6e60 | 2797 | args[0] = file; |
570d7624 JB |
2798 | args[1] = newname; |
2799 | report_file_error ("Adding new name", Flist (2, args)); | |
2800 | #else | |
3b7f6e60 | 2801 | report_file_error ("Adding new name", Flist (2, &file)); |
570d7624 JB |
2802 | #endif |
2803 | } | |
2804 | ||
2805 | UNGCPRO; | |
2806 | return Qnil; | |
2807 | } | |
2808 | ||
2809 | #ifdef S_IFLNK | |
2810 | DEFUN ("make-symbolic-link", Fmake_symbolic_link, Smake_symbolic_link, 2, 3, | |
8c1a1077 PJ |
2811 | "FMake symbolic link to file: \nFMake symbolic link to file %s: \np", |
2812 | doc: /* Make a symbolic link to FILENAME, named LINKNAME. Both args strings. | |
2813 | Signals a `file-already-exists' error if a file LINKNAME already exists | |
2814 | unless optional third argument OK-IF-ALREADY-EXISTS is non-nil. | |
2815 | A number as third arg means request confirmation if LINKNAME already exists. | |
2816 | This happens for interactive use with M-x. */) | |
2817 | (filename, linkname, ok_if_already_exists) | |
e5d77022 | 2818 | Lisp_Object filename, linkname, ok_if_already_exists; |
570d7624 JB |
2819 | { |
2820 | #ifdef NO_ARG_ARRAY | |
2821 | Lisp_Object args[2]; | |
2822 | #endif | |
32f4334d | 2823 | Lisp_Object handler; |
b1d1b865 RS |
2824 | Lisp_Object encoded_filename, encoded_linkname; |
2825 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; | |
570d7624 | 2826 | |
b1d1b865 RS |
2827 | GCPRO4 (filename, linkname, encoded_filename, encoded_linkname); |
2828 | encoded_filename = encoded_linkname = Qnil; | |
b7826503 PJ |
2829 | CHECK_STRING (filename); |
2830 | CHECK_STRING (linkname); | |
d9bc1c99 RS |
2831 | /* If the link target has a ~, we must expand it to get |
2832 | a truly valid file name. Otherwise, do not expand; | |
2833 | we want to permit links to relative file names. */ | |
d5db4077 | 2834 | if (SREF (filename, 0) == '~') |
d9bc1c99 | 2835 | filename = Fexpand_file_name (filename, Qnil); |
e5d77022 | 2836 | linkname = Fexpand_file_name (linkname, Qnil); |
32f4334d RS |
2837 | |
2838 | /* If the file name has special constructs in it, | |
2839 | call the corresponding file handler. */ | |
49307295 | 2840 | handler = Ffind_file_name_handler (filename, Qmake_symbolic_link); |
32f4334d | 2841 | if (!NILP (handler)) |
36712b0a KH |
2842 | RETURN_UNGCPRO (call4 (handler, Qmake_symbolic_link, filename, |
2843 | linkname, ok_if_already_exists)); | |
32f4334d | 2844 | |
adc6741c RS |
2845 | /* If the new link name has special constructs in it, |
2846 | call the corresponding file handler. */ | |
2847 | handler = Ffind_file_name_handler (linkname, Qmake_symbolic_link); | |
2848 | if (!NILP (handler)) | |
2849 | RETURN_UNGCPRO (call4 (handler, Qmake_symbolic_link, filename, | |
2850 | linkname, ok_if_already_exists)); | |
2851 | ||
b1d1b865 RS |
2852 | encoded_filename = ENCODE_FILE (filename); |
2853 | encoded_linkname = ENCODE_FILE (linkname); | |
2854 | ||
265a9e55 | 2855 | if (NILP (ok_if_already_exists) |
93c30b5f | 2856 | || INTEGERP (ok_if_already_exists)) |
b1d1b865 | 2857 | barf_or_query_if_file_exists (encoded_linkname, "make it a link", |
b8b29dc9 | 2858 | INTEGERP (ok_if_already_exists), 0, 0); |
d5db4077 KR |
2859 | if (0 > symlink (SDATA (encoded_filename), |
2860 | SDATA (encoded_linkname))) | |
570d7624 JB |
2861 | { |
2862 | /* If we didn't complain already, silently delete existing file. */ | |
2863 | if (errno == EEXIST) | |
2864 | { | |
d5db4077 KR |
2865 | unlink (SDATA (encoded_linkname)); |
2866 | if (0 <= symlink (SDATA (encoded_filename), | |
2867 | SDATA (encoded_linkname))) | |
1a04498e KH |
2868 | { |
2869 | UNGCPRO; | |
2870 | return Qnil; | |
2871 | } | |
570d7624 JB |
2872 | } |
2873 | ||
2874 | #ifdef NO_ARG_ARRAY | |
2875 | args[0] = filename; | |
e5d77022 | 2876 | args[1] = linkname; |
570d7624 JB |
2877 | report_file_error ("Making symbolic link", Flist (2, args)); |
2878 | #else | |
2879 | report_file_error ("Making symbolic link", Flist (2, &filename)); | |
2880 | #endif | |
2881 | } | |
2882 | UNGCPRO; | |
2883 | return Qnil; | |
2884 | } | |
2885 | #endif /* S_IFLNK */ | |
2886 | ||
2887 | #ifdef VMS | |
2888 | ||
2889 | DEFUN ("define-logical-name", Fdefine_logical_name, Sdefine_logical_name, | |
2890 | 2, 2, "sDefine logical name: \nsDefine logical name %s as: ", | |
8c1a1077 PJ |
2891 | doc: /* Define the job-wide logical name NAME to have the value STRING. |
2892 | If STRING is nil or a null string, the logical name NAME is deleted. */) | |
2893 | (name, string) | |
3b7f6e60 | 2894 | Lisp_Object name; |
570d7624 JB |
2895 | Lisp_Object string; |
2896 | { | |
b7826503 | 2897 | CHECK_STRING (name); |
265a9e55 | 2898 | if (NILP (string)) |
d5db4077 | 2899 | delete_logical_name (SDATA (name)); |
570d7624 JB |
2900 | else |
2901 | { | |
b7826503 | 2902 | CHECK_STRING (string); |
570d7624 | 2903 | |
d5db4077 KR |
2904 | if (SCHARS (string) == 0) |
2905 | delete_logical_name (SDATA (name)); | |
570d7624 | 2906 | else |
d5db4077 | 2907 | define_logical_name (SDATA (name), SDATA (string)); |
570d7624 JB |
2908 | } |
2909 | ||
2910 | return string; | |
2911 | } | |
2912 | #endif /* VMS */ | |
2913 | ||
2914 | #ifdef HPUX_NET | |
2915 | ||
2916 | DEFUN ("sysnetunam", Fsysnetunam, Ssysnetunam, 2, 2, 0, | |
8c1a1077 | 2917 | doc: /* Open a network connection to PATH using LOGIN as the login string. */) |
570d7624 JB |
2918 | (path, login) |
2919 | Lisp_Object path, login; | |
2920 | { | |
2921 | int netresult; | |
199607e4 | 2922 | |
b7826503 PJ |
2923 | CHECK_STRING (path); |
2924 | CHECK_STRING (login); | |
199607e4 | 2925 | |
d5db4077 | 2926 | netresult = netunam (SDATA (path), SDATA (login)); |
570d7624 JB |
2927 | |
2928 | if (netresult == -1) | |
2929 | return Qnil; | |
2930 | else | |
2931 | return Qt; | |
2932 | } | |
2933 | #endif /* HPUX_NET */ | |
2934 | \f | |
2935 | DEFUN ("file-name-absolute-p", Ffile_name_absolute_p, Sfile_name_absolute_p, | |
2936 | 1, 1, 0, | |
8c1a1077 PJ |
2937 | doc: /* Return t if file FILENAME specifies an absolute file name. |
2938 | On Unix, this is a name starting with a `/' or a `~'. */) | |
570d7624 JB |
2939 | (filename) |
2940 | Lisp_Object filename; | |
2941 | { | |
19290c65 | 2942 | const unsigned char *ptr; |
570d7624 | 2943 | |
b7826503 | 2944 | CHECK_STRING (filename); |
d5db4077 | 2945 | ptr = SDATA (filename); |
5e570b75 | 2946 | if (IS_DIRECTORY_SEP (*ptr) || *ptr == '~' |
570d7624 JB |
2947 | #ifdef VMS |
2948 | /* ??? This criterion is probably wrong for '<'. */ | |
2949 | || index (ptr, ':') || index (ptr, '<') | |
2950 | || (*ptr == '[' && (ptr[1] != '-' || (ptr[2] != '.' && ptr[2] != ']')) | |
2951 | && ptr[1] != '.') | |
2952 | #endif /* VMS */ | |
5e570b75 | 2953 | #ifdef DOS_NT |
199607e4 | 2954 | || (IS_DRIVE (*ptr) && ptr[1] == ':' && IS_DIRECTORY_SEP (ptr[2])) |
4c3c22f3 | 2955 | #endif |
570d7624 JB |
2956 | ) |
2957 | return Qt; | |
2958 | else | |
2959 | return Qnil; | |
2960 | } | |
3beeedfe RS |
2961 | \f |
2962 | /* Return nonzero if file FILENAME exists and can be executed. */ | |
2963 | ||
2964 | static int | |
2965 | check_executable (filename) | |
2966 | char *filename; | |
2967 | { | |
3be3c08e RS |
2968 | #ifdef DOS_NT |
2969 | int len = strlen (filename); | |
2970 | char *suffix; | |
2971 | struct stat st; | |
2972 | if (stat (filename, &st) < 0) | |
2973 | return 0; | |
34ead71a | 2974 | #if defined (WINDOWSNT) || (defined (MSDOS) && __DJGPP__ > 1) |
199607e4 RS |
2975 | return ((st.st_mode & S_IEXEC) != 0); |
2976 | #else | |
3be3c08e RS |
2977 | return (S_ISREG (st.st_mode) |
2978 | && len >= 5 | |
2979 | && (stricmp ((suffix = filename + len-4), ".com") == 0 | |
2980 | || stricmp (suffix, ".exe") == 0 | |
2dc3be7e RS |
2981 | || stricmp (suffix, ".bat") == 0) |
2982 | || (st.st_mode & S_IFMT) == S_IFDIR); | |
199607e4 | 2983 | #endif /* not WINDOWSNT */ |
3be3c08e | 2984 | #else /* not DOS_NT */ |
de0be7dd RS |
2985 | #ifdef HAVE_EUIDACCESS |
2986 | return (euidaccess (filename, 1) >= 0); | |
3beeedfe RS |
2987 | #else |
2988 | /* Access isn't quite right because it uses the real uid | |
2989 | and we really want to test with the effective uid. | |
2990 | But Unix doesn't give us a right way to do it. */ | |
2991 | return (access (filename, 1) >= 0); | |
2992 | #endif | |
3be3c08e | 2993 | #endif /* not DOS_NT */ |
3beeedfe RS |
2994 | } |
2995 | ||
2996 | /* Return nonzero if file FILENAME exists and can be written. */ | |
2997 | ||
2998 | static int | |
2999 | check_writable (filename) | |
3000 | char *filename; | |
3001 | { | |
3be3c08e RS |
3002 | #ifdef MSDOS |
3003 | struct stat st; | |
3004 | if (stat (filename, &st) < 0) | |
3005 | return 0; | |
3006 | return (st.st_mode & S_IWRITE || (st.st_mode & S_IFMT) == S_IFDIR); | |
3007 | #else /* not MSDOS */ | |
41f3fb38 KH |
3008 | #ifdef HAVE_EUIDACCESS |
3009 | return (euidaccess (filename, 2) >= 0); | |
3beeedfe RS |
3010 | #else |
3011 | /* Access isn't quite right because it uses the real uid | |
3012 | and we really want to test with the effective uid. | |
3013 | But Unix doesn't give us a right way to do it. | |
3014 | Opening with O_WRONLY could work for an ordinary file, | |
3015 | but would lose for directories. */ | |
3016 | return (access (filename, 2) >= 0); | |
3017 | #endif | |
3be3c08e | 3018 | #endif /* not MSDOS */ |
3beeedfe | 3019 | } |
570d7624 JB |
3020 | |
3021 | DEFUN ("file-exists-p", Ffile_exists_p, Sfile_exists_p, 1, 1, 0, | |
8c1a1077 PJ |
3022 | doc: /* Return t if file FILENAME exists. (This does not mean you can read it.) |
3023 | See also `file-readable-p' and `file-attributes'. */) | |
3024 | (filename) | |
570d7624 JB |
3025 | Lisp_Object filename; |
3026 | { | |
199607e4 | 3027 | Lisp_Object absname; |
32f4334d | 3028 | Lisp_Object handler; |
4018b5ef | 3029 | struct stat statbuf; |
570d7624 | 3030 | |
b7826503 | 3031 | CHECK_STRING (filename); |
199607e4 | 3032 | absname = Fexpand_file_name (filename, Qnil); |
32f4334d RS |
3033 | |
3034 | /* If the file name has special constructs in it, | |
3035 | call the corresponding file handler. */ | |
199607e4 | 3036 | handler = Ffind_file_name_handler (absname, Qfile_exists_p); |
32f4334d | 3037 | if (!NILP (handler)) |
199607e4 | 3038 | return call2 (handler, Qfile_exists_p, absname); |
32f4334d | 3039 | |
b1d1b865 RS |
3040 | absname = ENCODE_FILE (absname); |
3041 | ||
d5db4077 | 3042 | return (stat (SDATA (absname), &statbuf) >= 0) ? Qt : Qnil; |
570d7624 JB |
3043 | } |
3044 | ||
3045 | DEFUN ("file-executable-p", Ffile_executable_p, Sfile_executable_p, 1, 1, 0, | |
8c1a1077 PJ |
3046 | doc: /* Return t if FILENAME can be executed by you. |
3047 | For a directory, this means you can access files in that directory. */) | |
3048 | (filename) | |
3049 | Lisp_Object filename; | |
570d7624 | 3050 | { |
199607e4 | 3051 | Lisp_Object absname; |
32f4334d | 3052 | Lisp_Object handler; |
570d7624 | 3053 | |
b7826503 | 3054 | CHECK_STRING (filename); |
199607e4 | 3055 | absname = Fexpand_file_name (filename, Qnil); |
32f4334d RS |
3056 | |
3057 | /* If the file name has special constructs in it, | |
3058 | call the corresponding file handler. */ | |
199607e4 | 3059 | handler = Ffind_file_name_handler (absname, Qfile_executable_p); |
32f4334d | 3060 | if (!NILP (handler)) |
199607e4 | 3061 | return call2 (handler, Qfile_executable_p, absname); |
32f4334d | 3062 | |
b1d1b865 RS |
3063 | absname = ENCODE_FILE (absname); |
3064 | ||
d5db4077 | 3065 | return (check_executable (SDATA (absname)) ? Qt : Qnil); |
570d7624 JB |
3066 | } |
3067 | ||
3068 | DEFUN ("file-readable-p", Ffile_readable_p, Sfile_readable_p, 1, 1, 0, | |
8c1a1077 PJ |
3069 | doc: /* Return t if file FILENAME exists and you can read it. |
3070 | See also `file-exists-p' and `file-attributes'. */) | |
3071 | (filename) | |
570d7624 JB |
3072 | Lisp_Object filename; |
3073 | { | |
199607e4 | 3074 | Lisp_Object absname; |
32f4334d | 3075 | Lisp_Object handler; |
4018b5ef | 3076 | int desc; |
bb369dc6 RS |
3077 | int flags; |
3078 | struct stat statbuf; | |
570d7624 | 3079 | |
b7826503 | 3080 | CHECK_STRING (filename); |
199607e4 | 3081 | absname = Fexpand_file_name (filename, Qnil); |
32f4334d RS |
3082 | |
3083 | /* If the file name has special constructs in it, | |
3084 | call the corresponding file handler. */ | |
199607e4 | 3085 | handler = Ffind_file_name_handler (absname, Qfile_readable_p); |
32f4334d | 3086 | if (!NILP (handler)) |
199607e4 | 3087 | return call2 (handler, Qfile_readable_p, absname); |
32f4334d | 3088 | |
b1d1b865 RS |
3089 | absname = ENCODE_FILE (absname); |
3090 | ||
fb4c6c96 AC |
3091 | #if defined(DOS_NT) || defined(macintosh) |
3092 | /* Under MS-DOS, Windows, and Macintosh, open does not work for | |
3093 | directories. */ | |
d5db4077 | 3094 | if (access (SDATA (absname), 0) == 0) |
a8a7d065 RS |
3095 | return Qt; |
3096 | return Qnil; | |
fb4c6c96 | 3097 | #else /* not DOS_NT and not macintosh */ |
bb369dc6 RS |
3098 | flags = O_RDONLY; |
3099 | #if defined (S_ISFIFO) && defined (O_NONBLOCK) | |
3100 | /* Opening a fifo without O_NONBLOCK can wait. | |
3101 | We don't want to wait. But we don't want to mess wth O_NONBLOCK | |
3102 | except in the case of a fifo, on a system which handles it. */ | |
d5db4077 | 3103 | desc = stat (SDATA (absname), &statbuf); |
bb369dc6 RS |
3104 | if (desc < 0) |
3105 | return Qnil; | |
3106 | if (S_ISFIFO (statbuf.st_mode)) | |
3107 | flags |= O_NONBLOCK; | |
3108 | #endif | |
d5db4077 | 3109 | desc = emacs_open (SDATA (absname), flags, 0); |
4018b5ef RS |
3110 | if (desc < 0) |
3111 | return Qnil; | |
68c45bf0 | 3112 | emacs_close (desc); |
4018b5ef | 3113 | return Qt; |
fb4c6c96 | 3114 | #endif /* not DOS_NT and not macintosh */ |
570d7624 JB |
3115 | } |
3116 | ||
f793dc6c RS |
3117 | /* Having this before file-symlink-p mysteriously caused it to be forgotten |
3118 | on the RT/PC. */ | |
3119 | DEFUN ("file-writable-p", Ffile_writable_p, Sfile_writable_p, 1, 1, 0, | |
8c1a1077 PJ |
3120 | doc: /* Return t if file FILENAME can be written or created by you. */) |
3121 | (filename) | |
f793dc6c RS |
3122 | Lisp_Object filename; |
3123 | { | |
b1d1b865 | 3124 | Lisp_Object absname, dir, encoded; |
f793dc6c RS |
3125 | Lisp_Object handler; |
3126 | struct stat statbuf; | |
3127 | ||
b7826503 | 3128 | CHECK_STRING (filename); |
199607e4 | 3129 | absname = Fexpand_file_name (filename, Qnil); |
f793dc6c RS |
3130 | |
3131 | /* If the file name has special constructs in it, | |
3132 | call the corresponding file handler. */ | |
199607e4 | 3133 | handler = Ffind_file_name_handler (absname, Qfile_writable_p); |
f793dc6c | 3134 | if (!NILP (handler)) |
199607e4 | 3135 | return call2 (handler, Qfile_writable_p, absname); |
f793dc6c | 3136 | |
b1d1b865 | 3137 | encoded = ENCODE_FILE (absname); |
d5db4077 KR |
3138 | if (stat (SDATA (encoded), &statbuf) >= 0) |
3139 | return (check_writable (SDATA (encoded)) | |
f793dc6c | 3140 | ? Qt : Qnil); |
b1d1b865 | 3141 | |
199607e4 | 3142 | dir = Ffile_name_directory (absname); |
f793dc6c RS |
3143 | #ifdef VMS |
3144 | if (!NILP (dir)) | |
3145 | dir = Fdirectory_file_name (dir); | |
3146 | #endif /* VMS */ | |
3147 | #ifdef MSDOS | |
3148 | if (!NILP (dir)) | |
3149 | dir = Fdirectory_file_name (dir); | |
3150 | #endif /* MSDOS */ | |
b1d1b865 RS |
3151 | |
3152 | dir = ENCODE_FILE (dir); | |
e3e8a75a GM |
3153 | #ifdef WINDOWSNT |
3154 | /* The read-only attribute of the parent directory doesn't affect | |
3155 | whether a file or directory can be created within it. Some day we | |
3156 | should check ACLs though, which do affect this. */ | |
d5db4077 | 3157 | if (stat (SDATA (dir), &statbuf) < 0) |
e3e8a75a GM |
3158 | return Qnil; |
3159 | return (statbuf.st_mode & S_IFMT) == S_IFDIR ? Qt : Qnil; | |
3160 | #else | |
d5db4077 | 3161 | return (check_writable (!NILP (dir) ? (char *) SDATA (dir) : "") |
f793dc6c | 3162 | ? Qt : Qnil); |
e3e8a75a | 3163 | #endif |
f793dc6c RS |
3164 | } |
3165 | \f | |
1f8653eb | 3166 | DEFUN ("access-file", Faccess_file, Saccess_file, 2, 2, 0, |
8c1a1077 PJ |
3167 | doc: /* Access file FILENAME, and get an error if that does not work. |
3168 | The second argument STRING is used in the error message. | |
3169 | If there is no error, we return nil. */) | |
3170 | (filename, string) | |
1f8653eb RS |
3171 | Lisp_Object filename, string; |
3172 | { | |
49475635 | 3173 | Lisp_Object handler, encoded_filename, absname; |
1f8653eb RS |
3174 | int fd; |
3175 | ||
b7826503 | 3176 | CHECK_STRING (filename); |
49475635 EZ |
3177 | absname = Fexpand_file_name (filename, Qnil); |
3178 | ||
b7826503 | 3179 | CHECK_STRING (string); |
1f8653eb RS |
3180 | |
3181 | /* If the file name has special constructs in it, | |
3182 | call the corresponding file handler. */ | |
49475635 | 3183 | handler = Ffind_file_name_handler (absname, Qaccess_file); |
1f8653eb | 3184 | if (!NILP (handler)) |
49475635 | 3185 | return call3 (handler, Qaccess_file, absname, string); |
1f8653eb | 3186 | |
49475635 | 3187 | encoded_filename = ENCODE_FILE (absname); |
b1d1b865 | 3188 | |
d5db4077 | 3189 | fd = emacs_open (SDATA (encoded_filename), O_RDONLY, 0); |
1f8653eb | 3190 | if (fd < 0) |
d5db4077 | 3191 | report_file_error (SDATA (string), Fcons (filename, Qnil)); |
68c45bf0 | 3192 | emacs_close (fd); |
1f8653eb RS |
3193 | |
3194 | return Qnil; | |
3195 | } | |
3196 | \f | |
570d7624 | 3197 | DEFUN ("file-symlink-p", Ffile_symlink_p, Sfile_symlink_p, 1, 1, 0, |
8c1a1077 | 3198 | doc: /* Return non-nil if file FILENAME is the name of a symbolic link. |
1c353c74 | 3199 | The value is the link target, as a string. |
8c1a1077 PJ |
3200 | Otherwise returns nil. */) |
3201 | (filename) | |
570d7624 JB |
3202 | Lisp_Object filename; |
3203 | { | |
32f4334d | 3204 | Lisp_Object handler; |
570d7624 | 3205 | |
b7826503 | 3206 | CHECK_STRING (filename); |
570d7624 JB |
3207 | filename = Fexpand_file_name (filename, Qnil); |
3208 | ||
32f4334d RS |
3209 | /* If the file name has special constructs in it, |
3210 | call the corresponding file handler. */ | |
49307295 | 3211 | handler = Ffind_file_name_handler (filename, Qfile_symlink_p); |
32f4334d RS |
3212 | if (!NILP (handler)) |
3213 | return call2 (handler, Qfile_symlink_p, filename); | |
3214 | ||
5adcec23 JR |
3215 | #ifdef S_IFLNK |
3216 | { | |
3217 | char *buf; | |
3218 | int bufsize; | |
3219 | int valsize; | |
3220 | Lisp_Object val; | |
3221 | ||
b1d1b865 RS |
3222 | filename = ENCODE_FILE (filename); |
3223 | ||
81c3310d GM |
3224 | bufsize = 50; |
3225 | buf = NULL; | |
3226 | do | |
570d7624 | 3227 | { |
81c3310d GM |
3228 | bufsize *= 2; |
3229 | buf = (char *) xrealloc (buf, bufsize); | |
570d7624 | 3230 | bzero (buf, bufsize); |
efdc16c9 | 3231 | |
81c3310d | 3232 | errno = 0; |
d5db4077 | 3233 | valsize = readlink (SDATA (filename), buf, bufsize); |
bcdd93b3 GM |
3234 | if (valsize == -1) |
3235 | { | |
81c3310d GM |
3236 | #ifdef ERANGE |
3237 | /* HP-UX reports ERANGE if buffer is too small. */ | |
bcdd93b3 GM |
3238 | if (errno == ERANGE) |
3239 | valsize = bufsize; | |
3240 | else | |
81c3310d | 3241 | #endif |
bcdd93b3 GM |
3242 | { |
3243 | xfree (buf); | |
3244 | return Qnil; | |
3245 | } | |
81c3310d | 3246 | } |
570d7624 | 3247 | } |
81c3310d | 3248 | while (valsize >= bufsize); |
efdc16c9 | 3249 | |
570d7624 | 3250 | val = make_string (buf, valsize); |
69ac1891 GM |
3251 | if (buf[0] == '/' && index (buf, ':')) |
3252 | val = concat2 (build_string ("/:"), val); | |
9ac0d9e0 | 3253 | xfree (buf); |
cd913586 KH |
3254 | val = DECODE_FILE (val); |
3255 | return val; | |
5adcec23 | 3256 | } |
570d7624 JB |
3257 | #else /* not S_IFLNK */ |
3258 | return Qnil; | |
3259 | #endif /* not S_IFLNK */ | |
3260 | } | |
3261 | ||
570d7624 | 3262 | DEFUN ("file-directory-p", Ffile_directory_p, Sfile_directory_p, 1, 1, 0, |
8c1a1077 PJ |
3263 | doc: /* Return t if FILENAME names an existing directory. |
3264 | Symbolic links to directories count as directories. | |
3265 | See `file-symlink-p' to distinguish symlinks. */) | |
3266 | (filename) | |
570d7624 JB |
3267 | Lisp_Object filename; |
3268 | { | |
199607e4 | 3269 | register Lisp_Object absname; |
570d7624 | 3270 | struct stat st; |
32f4334d | 3271 | Lisp_Object handler; |
570d7624 | 3272 | |
199607e4 | 3273 | absname = expand_and_dir_to_file (filename, current_buffer->directory); |
570d7624 | 3274 | |
32f4334d RS |
3275 | /* If the file name has special constructs in it, |
3276 | call the corresponding file handler. */ | |
199607e4 | 3277 | handler = Ffind_file_name_handler (absname, Qfile_directory_p); |
32f4334d | 3278 | if (!NILP (handler)) |
199607e4 | 3279 | return call2 (handler, Qfile_directory_p, absname); |
32f4334d | 3280 | |
b1d1b865 RS |
3281 | absname = ENCODE_FILE (absname); |
3282 | ||
d5db4077 | 3283 | if (stat (SDATA (absname), &st) < 0) |
570d7624 JB |
3284 | return Qnil; |
3285 | return (st.st_mode & S_IFMT) == S_IFDIR ? Qt : Qnil; | |
3286 | } | |
3287 | ||
b72dea2a | 3288 | DEFUN ("file-accessible-directory-p", Ffile_accessible_directory_p, Sfile_accessible_directory_p, 1, 1, 0, |
e385ec41 RS |
3289 | doc: /* Return t if file FILENAME names a directory you can open. |
3290 | For the value to be t, FILENAME must specify the name of a directory as a file, | |
3291 | and the directory must allow you to open files in it. In order to use a | |
8c1a1077 PJ |
3292 | directory as a buffer's current directory, this predicate must return true. |
3293 | A directory name spec may be given instead; then the value is t | |
3294 | if the directory so specified exists and really is a readable and | |
3295 | searchable directory. */) | |
3296 | (filename) | |
b72dea2a JB |
3297 | Lisp_Object filename; |
3298 | { | |
32f4334d | 3299 | Lisp_Object handler; |
1a04498e | 3300 | int tem; |
d26859eb | 3301 | struct gcpro gcpro1; |
32f4334d RS |
3302 | |
3303 | /* If the file name has special constructs in it, | |
3304 | call the corresponding file handler. */ | |
49307295 | 3305 | handler = Ffind_file_name_handler (filename, Qfile_accessible_directory_p); |
32f4334d RS |
3306 | if (!NILP (handler)) |
3307 | return call2 (handler, Qfile_accessible_directory_p, filename); | |
3308 | ||
d26859eb | 3309 | GCPRO1 (filename); |
1a04498e KH |
3310 | tem = (NILP (Ffile_directory_p (filename)) |
3311 | || NILP (Ffile_executable_p (filename))); | |
d26859eb | 3312 | UNGCPRO; |
1a04498e | 3313 | return tem ? Qnil : Qt; |
b72dea2a JB |
3314 | } |
3315 | ||
f793dc6c | 3316 | DEFUN ("file-regular-p", Ffile_regular_p, Sfile_regular_p, 1, 1, 0, |
8c1a1077 PJ |
3317 | doc: /* Return t if file FILENAME is the name of a regular file. |
3318 | This is the sort of file that holds an ordinary stream of data bytes. */) | |
3319 | (filename) | |
f793dc6c RS |
3320 | Lisp_Object filename; |
3321 | { | |
199607e4 | 3322 | register Lisp_Object absname; |
f793dc6c RS |
3323 | struct stat st; |
3324 | Lisp_Object handler; | |
3325 | ||
199607e4 | 3326 | absname = expand_and_dir_to_file (filename, current_buffer->directory); |
f793dc6c RS |
3327 | |
3328 | /* If the file name has special constructs in it, | |
3329 | call the corresponding file handler. */ | |
199607e4 | 3330 | handler = Ffind_file_name_handler (absname, Qfile_regular_p); |
f793dc6c | 3331 | if (!NILP (handler)) |
199607e4 | 3332 | return call2 (handler, Qfile_regular_p, absname); |
f793dc6c | 3333 | |
b1d1b865 RS |
3334 | absname = ENCODE_FILE (absname); |
3335 | ||
c1c4693e RS |
3336 | #ifdef WINDOWSNT |
3337 | { | |
3338 | int result; | |
3339 | Lisp_Object tem = Vw32_get_true_file_attributes; | |
3340 | ||
3341 | /* Tell stat to use expensive method to get accurate info. */ | |
3342 | Vw32_get_true_file_attributes = Qt; | |
d5db4077 | 3343 | result = stat (SDATA (absname), &st); |
c1c4693e RS |
3344 | Vw32_get_true_file_attributes = tem; |
3345 | ||
3346 | if (result < 0) | |
3347 | return Qnil; | |
3348 | return (st.st_mode & S_IFMT) == S_IFREG ? Qt : Qnil; | |
3349 | } | |
3350 | #else | |
d5db4077 | 3351 | if (stat (SDATA (absname), &st) < 0) |
f793dc6c RS |
3352 | return Qnil; |
3353 | return (st.st_mode & S_IFMT) == S_IFREG ? Qt : Qnil; | |
c1c4693e | 3354 | #endif |
f793dc6c RS |
3355 | } |
3356 | \f | |
570d7624 | 3357 | DEFUN ("file-modes", Ffile_modes, Sfile_modes, 1, 1, 0, |
8c1a1077 PJ |
3358 | doc: /* Return mode bits of file named FILENAME, as an integer. */) |
3359 | (filename) | |
570d7624 JB |
3360 | Lisp_Object filename; |
3361 | { | |
199607e4 | 3362 | Lisp_Object absname; |
570d7624 | 3363 | struct stat st; |
32f4334d | 3364 | Lisp_Object handler; |
570d7624 | 3365 | |
199607e4 | 3366 | absname = expand_and_dir_to_file (filename, current_buffer->directory); |
570d7624 | 3367 | |
32f4334d RS |
3368 | /* If the file name has special constructs in it, |
3369 | call the corresponding file handler. */ | |
199607e4 | 3370 | handler = Ffind_file_name_handler (absname, Qfile_modes); |
32f4334d | 3371 | if (!NILP (handler)) |
199607e4 | 3372 | return call2 (handler, Qfile_modes, absname); |
32f4334d | 3373 | |
b1d1b865 RS |
3374 | absname = ENCODE_FILE (absname); |
3375 | ||
d5db4077 | 3376 | if (stat (SDATA (absname), &st) < 0) |
570d7624 | 3377 | return Qnil; |
34ead71a | 3378 | #if defined (MSDOS) && __DJGPP__ < 2 |
d5db4077 | 3379 | if (check_executable (SDATA (absname))) |
3be3c08e | 3380 | st.st_mode |= S_IEXEC; |
34ead71a | 3381 | #endif /* MSDOS && __DJGPP__ < 2 */ |
3ace87e3 | 3382 | |
570d7624 JB |
3383 | return make_number (st.st_mode & 07777); |
3384 | } | |
3385 | ||
3386 | DEFUN ("set-file-modes", Fset_file_modes, Sset_file_modes, 2, 2, 0, | |
8c1a1077 PJ |
3387 | doc: /* Set mode bits of file named FILENAME to MODE (an integer). |
3388 | Only the 12 low bits of MODE are used. */) | |
570d7624 JB |
3389 | (filename, mode) |
3390 | Lisp_Object filename, mode; | |
3391 | { | |
b1d1b865 | 3392 | Lisp_Object absname, encoded_absname; |
32f4334d | 3393 | Lisp_Object handler; |
570d7624 | 3394 | |
199607e4 | 3395 | absname = Fexpand_file_name (filename, current_buffer->directory); |
b7826503 | 3396 | CHECK_NUMBER (mode); |
570d7624 | 3397 | |
32f4334d RS |
3398 | /* If the file name has special constructs in it, |
3399 | call the corresponding file handler. */ | |
199607e4 | 3400 | handler = Ffind_file_name_handler (absname, Qset_file_modes); |
32f4334d | 3401 | if (!NILP (handler)) |
199607e4 | 3402 | return call3 (handler, Qset_file_modes, absname, mode); |
32f4334d | 3403 | |
b1d1b865 RS |
3404 | encoded_absname = ENCODE_FILE (absname); |
3405 | ||
d5db4077 | 3406 | if (chmod (SDATA (encoded_absname), XINT (mode)) < 0) |
199607e4 | 3407 | report_file_error ("Doing chmod", Fcons (absname, Qnil)); |
570d7624 JB |
3408 | |
3409 | return Qnil; | |
3410 | } | |
3411 | ||
c24e9a53 | 3412 | DEFUN ("set-default-file-modes", Fset_default_file_modes, Sset_default_file_modes, 1, 1, 0, |
8c1a1077 PJ |
3413 | doc: /* Set the file permission bits for newly created files. |
3414 | The argument MODE should be an integer; only the low 9 bits are used. | |
3415 | This setting is inherited by subprocesses. */) | |
3416 | (mode) | |
5f85ea58 | 3417 | Lisp_Object mode; |
36a8c287 | 3418 | { |
b7826503 | 3419 | CHECK_NUMBER (mode); |
199607e4 | 3420 | |
5f85ea58 | 3421 | umask ((~ XINT (mode)) & 0777); |
36a8c287 JB |
3422 | |
3423 | return Qnil; | |
3424 | } | |
3425 | ||
c24e9a53 | 3426 | DEFUN ("default-file-modes", Fdefault_file_modes, Sdefault_file_modes, 0, 0, 0, |
8c1a1077 PJ |
3427 | doc: /* Return the default file protection for created files. |
3428 | The value is an integer. */) | |
3429 | () | |
36a8c287 | 3430 | { |
5f85ea58 RS |
3431 | int realmask; |
3432 | Lisp_Object value; | |
36a8c287 | 3433 | |
5f85ea58 RS |
3434 | realmask = umask (0); |
3435 | umask (realmask); | |
36a8c287 | 3436 | |
46283abe | 3437 | XSETINT (value, (~ realmask) & 0777); |
5f85ea58 | 3438 | return value; |
36a8c287 | 3439 | } |
5df5e07c | 3440 | |
f793dc6c | 3441 | \f |
5df5e07c GM |
3442 | #ifdef __NetBSD__ |
3443 | #define unix 42 | |
3444 | #endif | |
85ffea93 | 3445 | |
5df5e07c | 3446 | #ifdef unix |
85ffea93 | 3447 | DEFUN ("unix-sync", Funix_sync, Sunix_sync, 0, 0, "", |
8c1a1077 PJ |
3448 | doc: /* Tell Unix to finish all pending disk updates. */) |
3449 | () | |
85ffea93 RS |
3450 | { |
3451 | sync (); | |
3452 | return Qnil; | |
3453 | } | |
3454 | ||
3455 | #endif /* unix */ | |
3456 | ||
570d7624 | 3457 | DEFUN ("file-newer-than-file-p", Ffile_newer_than_file_p, Sfile_newer_than_file_p, 2, 2, 0, |
8c1a1077 PJ |
3458 | doc: /* Return t if file FILE1 is newer than file FILE2. |
3459 | If FILE1 does not exist, the answer is nil; | |
3460 | otherwise, if FILE2 does not exist, the answer is t. */) | |
3461 | (file1, file2) | |
570d7624 JB |
3462 | Lisp_Object file1, file2; |
3463 | { | |
199607e4 | 3464 | Lisp_Object absname1, absname2; |
570d7624 JB |
3465 | struct stat st; |
3466 | int mtime1; | |
32f4334d | 3467 | Lisp_Object handler; |
09121adc | 3468 | struct gcpro gcpro1, gcpro2; |
570d7624 | 3469 | |
b7826503 PJ |
3470 | CHECK_STRING (file1); |
3471 | CHECK_STRING (file2); | |
570d7624 | 3472 | |
199607e4 RS |
3473 | absname1 = Qnil; |
3474 | GCPRO2 (absname1, file2); | |
3475 | absname1 = expand_and_dir_to_file (file1, current_buffer->directory); | |
3476 | absname2 = expand_and_dir_to_file (file2, current_buffer->directory); | |
09121adc | 3477 | UNGCPRO; |
570d7624 | 3478 | |
32f4334d RS |
3479 | /* If the file name has special constructs in it, |
3480 | call the corresponding file handler. */ | |
199607e4 | 3481 | handler = Ffind_file_name_handler (absname1, Qfile_newer_than_file_p); |
51cf6d37 | 3482 | if (NILP (handler)) |
199607e4 | 3483 | handler = Ffind_file_name_handler (absname2, Qfile_newer_than_file_p); |
32f4334d | 3484 | if (!NILP (handler)) |
199607e4 | 3485 | return call3 (handler, Qfile_newer_than_file_p, absname1, absname2); |
32f4334d | 3486 | |
b1d1b865 RS |
3487 | GCPRO2 (absname1, absname2); |
3488 | absname1 = ENCODE_FILE (absname1); | |
3489 | absname2 = ENCODE_FILE (absname2); | |
3490 | UNGCPRO; | |
3491 | ||
d5db4077 | 3492 | if (stat (SDATA (absname1), &st) < 0) |
570d7624 JB |
3493 | return Qnil; |
3494 | ||
3495 | mtime1 = st.st_mtime; | |
3496 | ||
d5db4077 | 3497 | if (stat (SDATA (absname2), &st) < 0) |
570d7624 JB |
3498 | return Qt; |
3499 | ||
3500 | return (mtime1 > st.st_mtime) ? Qt : Qnil; | |
3501 | } | |
3502 | \f | |
5e570b75 | 3503 | #ifdef DOS_NT |
4c3c22f3 | 3504 | Lisp_Object Qfind_buffer_file_type; |
5e570b75 | 3505 | #endif /* DOS_NT */ |
4c3c22f3 | 3506 | |
6fdaa9a0 KH |
3507 | #ifndef READ_BUF_SIZE |
3508 | #define READ_BUF_SIZE (64 << 10) | |
3509 | #endif | |
3510 | ||
98a7d268 KH |
3511 | extern void adjust_markers_for_delete P_ ((int, int, int, int)); |
3512 | ||
3513 | /* This function is called after Lisp functions to decide a coding | |
3514 | system are called, or when they cause an error. Before they are | |
3515 | called, the current buffer is set unibyte and it contains only a | |
3516 | newly inserted text (thus the buffer was empty before the | |
3517 | insertion). | |
3518 | ||
3519 | The functions may set markers, overlays, text properties, or even | |
3520 | alter the buffer contents, change the current buffer. | |
3521 | ||
3522 | Here, we reset all those changes by: | |
3523 | o set back the current buffer. | |
3524 | o move all markers and overlays to BEG. | |
3525 | o remove all text properties. | |
3526 | o set back the buffer multibyteness. */ | |
f736ffbf KH |
3527 | |
3528 | static Lisp_Object | |
98a7d268 KH |
3529 | decide_coding_unwind (unwind_data) |
3530 | Lisp_Object unwind_data; | |
f736ffbf | 3531 | { |
98a7d268 | 3532 | Lisp_Object multibyte, undo_list, buffer; |
f736ffbf | 3533 | |
98a7d268 KH |
3534 | multibyte = XCAR (unwind_data); |
3535 | unwind_data = XCDR (unwind_data); | |
3536 | undo_list = XCAR (unwind_data); | |
3537 | buffer = XCDR (unwind_data); | |
3538 | ||
3539 | if (current_buffer != XBUFFER (buffer)) | |
3540 | set_buffer_internal (XBUFFER (buffer)); | |
3541 | adjust_markers_for_delete (BEG, BEG_BYTE, Z, Z_BYTE); | |
3542 | adjust_overlays_for_delete (BEG, Z - BEG); | |
3543 | BUF_INTERVALS (current_buffer) = 0; | |
3544 | TEMP_SET_PT_BOTH (BEG, BEG_BYTE); | |
3545 | ||
3546 | /* Now we are safe to change the buffer's multibyteness directly. */ | |
3547 | current_buffer->enable_multibyte_characters = multibyte; | |
3548 | current_buffer->undo_list = undo_list; | |
f736ffbf KH |
3549 | |
3550 | return Qnil; | |
3551 | } | |
3552 | ||
55587f8a | 3553 | |
1b978129 | 3554 | /* Used to pass values from insert-file-contents to read_non_regular. */ |
55587f8a | 3555 | |
1b978129 GM |
3556 | static int non_regular_fd; |
3557 | static int non_regular_inserted; | |
3558 | static int non_regular_nbytes; | |
55587f8a | 3559 | |
55587f8a | 3560 | |
1b978129 GM |
3561 | /* Read from a non-regular file. |
3562 | Read non_regular_trytry bytes max from non_regular_fd. | |
3563 | Non_regular_inserted specifies where to put the read bytes. | |
3564 | Value is the number of bytes read. */ | |
55587f8a GM |
3565 | |
3566 | static Lisp_Object | |
1b978129 | 3567 | read_non_regular () |
55587f8a | 3568 | { |
1b978129 | 3569 | int nbytes; |
efdc16c9 | 3570 | |
1b978129 GM |
3571 | immediate_quit = 1; |
3572 | QUIT; | |
3573 | nbytes = emacs_read (non_regular_fd, | |
28c3eb5a | 3574 | BEG_ADDR + PT_BYTE - BEG_BYTE + non_regular_inserted, |
1b978129 | 3575 | non_regular_nbytes); |
1b978129 GM |
3576 | immediate_quit = 0; |
3577 | return make_number (nbytes); | |
3578 | } | |
55587f8a | 3579 | |
d0e2444e | 3580 | |
1b978129 GM |
3581 | /* Condition-case handler used when reading from non-regular files |
3582 | in insert-file-contents. */ | |
3583 | ||
3584 | static Lisp_Object | |
3585 | read_non_regular_quit () | |
3586 | { | |
55587f8a GM |
3587 | return Qnil; |
3588 | } | |
3589 | ||
3590 | ||
570d7624 | 3591 | DEFUN ("insert-file-contents", Finsert_file_contents, Sinsert_file_contents, |
8c1a1077 PJ |
3592 | 1, 5, 0, |
3593 | doc: /* Insert contents of file FILENAME after point. | |
cf6d2357 | 3594 | Returns list of absolute file name and number of characters inserted. |
8c1a1077 PJ |
3595 | If second argument VISIT is non-nil, the buffer's visited filename |
3596 | and last save file modtime are set, and it is marked unmodified. | |
3597 | If visiting and the file does not exist, visiting is completed | |
3598 | before the error is signaled. | |
3599 | The optional third and fourth arguments BEG and END | |
3600 | specify what portion of the file to insert. | |
3601 | These arguments count bytes in the file, not characters in the buffer. | |
3602 | If VISIT is non-nil, BEG and END must be nil. | |
3603 | ||
3604 | If optional fifth argument REPLACE is non-nil, | |
3605 | it means replace the current buffer contents (in the accessible portion) | |
3606 | with the file contents. This is better than simply deleting and inserting | |
3607 | the whole thing because (1) it preserves some marker positions | |
3608 | and (2) it puts less data in the undo list. | |
3609 | When REPLACE is non-nil, the value is the number of characters actually read, | |
3610 | which is often less than the number of characters to be read. | |
3611 | ||
3612 | This does code conversion according to the value of | |
3613 | `coding-system-for-read' or `file-coding-system-alist', | |
3614 | and sets the variable `last-coding-system-used' to the coding system | |
3615 | actually used. */) | |
3616 | (filename, visit, beg, end, replace) | |
3d0387c0 | 3617 | Lisp_Object filename, visit, beg, end, replace; |
570d7624 JB |
3618 | { |
3619 | struct stat st; | |
3620 | register int fd; | |
ec7adf26 | 3621 | int inserted = 0; |
570d7624 | 3622 | register int how_much; |
6fdaa9a0 | 3623 | register int unprocessed; |
331379bf | 3624 | int count = SPECPDL_INDEX (); |
b1d1b865 RS |
3625 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; |
3626 | Lisp_Object handler, val, insval, orig_filename; | |
d6a3cc15 | 3627 | Lisp_Object p; |
6bbd7a29 | 3628 | int total = 0; |
53c34c46 | 3629 | int not_regular = 0; |
feb9dc27 | 3630 | unsigned char read_buf[READ_BUF_SIZE]; |
6fdaa9a0 | 3631 | struct coding_system coding; |
3dbcf3f6 | 3632 | unsigned char buffer[1 << 14]; |
727a0b4a | 3633 | int replace_handled = 0; |
ec7adf26 | 3634 | int set_coding_system = 0; |
f736ffbf | 3635 | int coding_system_decided = 0; |
1b978129 | 3636 | int read_quit = 0; |
32f4334d | 3637 | |
95385625 RS |
3638 | if (current_buffer->base_buffer && ! NILP (visit)) |
3639 | error ("Cannot do file visiting in an indirect buffer"); | |
3640 | ||
3641 | if (!NILP (current_buffer->read_only)) | |
3642 | Fbarf_if_buffer_read_only (); | |
3643 | ||
32f4334d | 3644 | val = Qnil; |
d6a3cc15 | 3645 | p = Qnil; |
b1d1b865 | 3646 | orig_filename = Qnil; |
32f4334d | 3647 | |
b1d1b865 | 3648 | GCPRO4 (filename, val, p, orig_filename); |
570d7624 | 3649 | |
b7826503 | 3650 | CHECK_STRING (filename); |
570d7624 JB |
3651 | filename = Fexpand_file_name (filename, Qnil); |
3652 | ||
32f4334d RS |
3653 | /* If the file name has special constructs in it, |
3654 | call the corresponding file handler. */ | |
49307295 | 3655 | handler = Ffind_file_name_handler (filename, Qinsert_file_contents); |
32f4334d RS |
3656 | if (!NILP (handler)) |
3657 | { | |
3d0387c0 RS |
3658 | val = call6 (handler, Qinsert_file_contents, filename, |
3659 | visit, beg, end, replace); | |
03699b14 KR |
3660 | if (CONSP (val) && CONSP (XCDR (val))) |
3661 | inserted = XINT (XCAR (XCDR (val))); | |
32f4334d RS |
3662 | goto handled; |
3663 | } | |
3664 | ||
b1d1b865 RS |
3665 | orig_filename = filename; |
3666 | filename = ENCODE_FILE (filename); | |
3667 | ||
570d7624 JB |
3668 | fd = -1; |
3669 | ||
c1c4693e RS |
3670 | #ifdef WINDOWSNT |
3671 | { | |
3672 | Lisp_Object tem = Vw32_get_true_file_attributes; | |
3673 | ||
3674 | /* Tell stat to use expensive method to get accurate info. */ | |
3675 | Vw32_get_true_file_attributes = Qt; | |
d5db4077 | 3676 | total = stat (SDATA (filename), &st); |
c1c4693e RS |
3677 | Vw32_get_true_file_attributes = tem; |
3678 | } | |
3679 | if (total < 0) | |
3680 | #else | |
570d7624 | 3681 | #ifndef APOLLO |
d5db4077 | 3682 | if (stat (SDATA (filename), &st) < 0) |
570d7624 | 3683 | #else |
d5db4077 | 3684 | if ((fd = emacs_open (SDATA (filename), O_RDONLY, 0)) < 0 |
570d7624 JB |
3685 | || fstat (fd, &st) < 0) |
3686 | #endif /* not APOLLO */ | |
c1c4693e | 3687 | #endif /* WINDOWSNT */ |
570d7624 | 3688 | { |
68c45bf0 | 3689 | if (fd >= 0) emacs_close (fd); |
99bc28f4 | 3690 | badopen: |
265a9e55 | 3691 | if (NILP (visit)) |
b1d1b865 | 3692 | report_file_error ("Opening input file", Fcons (orig_filename, Qnil)); |
570d7624 JB |
3693 | st.st_mtime = -1; |
3694 | how_much = 0; | |
0de6b8f4 | 3695 | if (!NILP (Vcoding_system_for_read)) |
22d92d6b | 3696 | Fset (Qbuffer_file_coding_system, Vcoding_system_for_read); |
570d7624 JB |
3697 | goto notfound; |
3698 | } | |
3699 | ||
99bc28f4 | 3700 | #ifdef S_IFREG |
be53b411 JB |
3701 | /* This code will need to be changed in order to work on named |
3702 | pipes, and it's probably just not worth it. So we should at | |
3703 | least signal an error. */ | |
99bc28f4 | 3704 | if (!S_ISREG (st.st_mode)) |
330bfe57 | 3705 | { |
d4b8687b RS |
3706 | not_regular = 1; |
3707 | ||
3708 | if (! NILP (visit)) | |
3709 | goto notfound; | |
3710 | ||
3711 | if (! NILP (replace) || ! NILP (beg) || ! NILP (end)) | |
330bfe57 RS |
3712 | Fsignal (Qfile_error, |
3713 | Fcons (build_string ("not a regular file"), | |
b1d1b865 | 3714 | Fcons (orig_filename, Qnil))); |
330bfe57 | 3715 | } |
be53b411 JB |
3716 | #endif |
3717 | ||
99bc28f4 | 3718 | if (fd < 0) |
d5db4077 | 3719 | if ((fd = emacs_open (SDATA (filename), O_RDONLY, 0)) < 0) |
99bc28f4 KH |
3720 | goto badopen; |
3721 | ||
3722 | /* Replacement should preserve point as it preserves markers. */ | |
3723 | if (!NILP (replace)) | |
3724 | record_unwind_protect (restore_point_unwind, Fpoint_marker ()); | |
3725 | ||
3726 | record_unwind_protect (close_file_unwind, make_number (fd)); | |
3727 | ||
570d7624 | 3728 | /* Supposedly happens on VMS. */ |
11d300db JR |
3729 | /* Can happen on any platform that uses long as type of off_t, but allows |
3730 | file sizes to exceed 2Gb. VMS is no longer officially supported, so | |
3731 | give a message suitable for the latter case. */ | |
d4b8687b | 3732 | if (! not_regular && st.st_size < 0) |
11d300db | 3733 | error ("Maximum buffer size exceeded"); |
be53b411 | 3734 | |
9c856db9 GM |
3735 | /* Prevent redisplay optimizations. */ |
3736 | current_buffer->clip_changed = 1; | |
3737 | ||
9f57b6b4 KH |
3738 | if (!NILP (visit)) |
3739 | { | |
3740 | if (!NILP (beg) || !NILP (end)) | |
3741 | error ("Attempt to visit less than an entire file"); | |
3742 | if (BEG < Z && NILP (replace)) | |
3743 | error ("Cannot do file visiting in a non-empty buffer"); | |
3744 | } | |
7fded690 JB |
3745 | |
3746 | if (!NILP (beg)) | |
b7826503 | 3747 | CHECK_NUMBER (beg); |
7fded690 | 3748 | else |
2acfd7ae | 3749 | XSETFASTINT (beg, 0); |
7fded690 JB |
3750 | |
3751 | if (!NILP (end)) | |
b7826503 | 3752 | CHECK_NUMBER (end); |
7fded690 JB |
3753 | else |
3754 | { | |
d4b8687b RS |
3755 | if (! not_regular) |
3756 | { | |
3757 | XSETINT (end, st.st_size); | |
68c45bf0 PE |
3758 | |
3759 | /* Arithmetic overflow can occur if an Emacs integer cannot | |
3760 | represent the file size, or if the calculations below | |
3761 | overflow. The calculations below double the file size | |
3762 | twice, so check that it can be multiplied by 4 safely. */ | |
3763 | if (XINT (end) != st.st_size | |
3764 | || ((int) st.st_size * 4) / 4 != st.st_size) | |
d4b8687b | 3765 | error ("Maximum buffer size exceeded"); |
d21dd12d GM |
3766 | |
3767 | /* The file size returned from stat may be zero, but data | |
3768 | may be readable nonetheless, for example when this is a | |
3769 | file in the /proc filesystem. */ | |
3770 | if (st.st_size == 0) | |
3771 | XSETINT (end, READ_BUF_SIZE); | |
d4b8687b | 3772 | } |
7fded690 JB |
3773 | } |
3774 | ||
356a6224 KH |
3775 | if (EQ (Vcoding_system_for_read, Qauto_save_coding)) |
3776 | { | |
3777 | /* We use emacs-mule for auto saving... */ | |
3778 | setup_coding_system (Qemacs_mule, &coding); | |
3779 | /* ... but with the special flag to indicate to read in a | |
3780 | multibyte sequence for eight-bit-control char as is. */ | |
3781 | coding.flags = 1; | |
3782 | coding.src_multibyte = 0; | |
3783 | coding.dst_multibyte | |
3784 | = !NILP (current_buffer->enable_multibyte_characters); | |
3785 | coding.eol_type = CODING_EOL_LF; | |
3786 | coding_system_decided = 1; | |
3787 | } | |
3788 | else if (BEG < Z) | |
f736ffbf KH |
3789 | { |
3790 | /* Decide the coding system to use for reading the file now | |
3791 | because we can't use an optimized method for handling | |
3792 | `coding:' tag if the current buffer is not empty. */ | |
3793 | Lisp_Object val; | |
3794 | val = Qnil; | |
feb9dc27 | 3795 | |
f736ffbf KH |
3796 | if (!NILP (Vcoding_system_for_read)) |
3797 | val = Vcoding_system_for_read; | |
3798 | else if (! NILP (replace)) | |
3799 | /* In REPLACE mode, we can use the same coding system | |
3800 | that was used to visit the file. */ | |
3801 | val = current_buffer->buffer_file_coding_system; | |
3802 | else | |
3803 | { | |
3804 | /* Don't try looking inside a file for a coding system | |
3805 | specification if it is not seekable. */ | |
3806 | if (! not_regular && ! NILP (Vset_auto_coding_function)) | |
3807 | { | |
3808 | /* Find a coding system specified in the heading two | |
3809 | lines or in the tailing several lines of the file. | |
3810 | We assume that the 1K-byte and 3K-byte for heading | |
003a7eaa | 3811 | and tailing respectively are sufficient for this |
f736ffbf | 3812 | purpose. */ |
07590973 | 3813 | int nread; |
f736ffbf KH |
3814 | |
3815 | if (st.st_size <= (1024 * 4)) | |
68c45bf0 | 3816 | nread = emacs_read (fd, read_buf, 1024 * 4); |
f736ffbf KH |
3817 | else |
3818 | { | |
68c45bf0 | 3819 | nread = emacs_read (fd, read_buf, 1024); |
f736ffbf KH |
3820 | if (nread >= 0) |
3821 | { | |
3822 | if (lseek (fd, st.st_size - (1024 * 3), 0) < 0) | |
3823 | report_file_error ("Setting file position", | |
3824 | Fcons (orig_filename, Qnil)); | |
68c45bf0 | 3825 | nread += emacs_read (fd, read_buf + nread, 1024 * 3); |
f736ffbf KH |
3826 | } |
3827 | } | |
feb9dc27 | 3828 | |
f736ffbf KH |
3829 | if (nread < 0) |
3830 | error ("IO error reading %s: %s", | |
d5db4077 | 3831 | SDATA (orig_filename), emacs_strerror (errno)); |
f736ffbf KH |
3832 | else if (nread > 0) |
3833 | { | |
f736ffbf | 3834 | struct buffer *prev = current_buffer; |
685fc579 RS |
3835 | Lisp_Object buffer; |
3836 | struct buffer *buf; | |
f736ffbf KH |
3837 | |
3838 | record_unwind_protect (Fset_buffer, Fcurrent_buffer ()); | |
1d92afcd | 3839 | |
685fc579 RS |
3840 | buffer = Fget_buffer_create (build_string (" *code-converting-work*")); |
3841 | buf = XBUFFER (buffer); | |
3842 | ||
29ea8ae9 | 3843 | delete_all_overlays (buf); |
685fc579 RS |
3844 | buf->directory = current_buffer->directory; |
3845 | buf->read_only = Qnil; | |
3846 | buf->filename = Qnil; | |
3847 | buf->undo_list = Qt; | |
29ea8ae9 SM |
3848 | eassert (buf->overlays_before == NULL); |
3849 | eassert (buf->overlays_after == NULL); | |
efdc16c9 | 3850 | |
685fc579 RS |
3851 | set_buffer_internal (buf); |
3852 | Ferase_buffer (); | |
3853 | buf->enable_multibyte_characters = Qnil; | |
3854 | ||
f736ffbf KH |
3855 | insert_1_both (read_buf, nread, nread, 0, 0, 0); |
3856 | TEMP_SET_PT_BOTH (BEG, BEG_BYTE); | |
1255deb9 KH |
3857 | val = call2 (Vset_auto_coding_function, |
3858 | filename, make_number (nread)); | |
f736ffbf | 3859 | set_buffer_internal (prev); |
efdc16c9 | 3860 | |
f736ffbf KH |
3861 | /* Discard the unwind protect for recovering the |
3862 | current buffer. */ | |
3863 | specpdl_ptr--; | |
3864 | ||
3865 | /* Rewind the file for the actual read done later. */ | |
3866 | if (lseek (fd, 0, 0) < 0) | |
3867 | report_file_error ("Setting file position", | |
3868 | Fcons (orig_filename, Qnil)); | |
3869 | } | |
3870 | } | |
feb9dc27 | 3871 | |
f736ffbf KH |
3872 | if (NILP (val)) |
3873 | { | |
3874 | /* If we have not yet decided a coding system, check | |
3875 | file-coding-system-alist. */ | |
3876 | Lisp_Object args[6], coding_systems; | |
3877 | ||
3878 | args[0] = Qinsert_file_contents, args[1] = orig_filename; | |
3879 | args[2] = visit, args[3] = beg, args[4] = end, args[5] = replace; | |
3880 | coding_systems = Ffind_operation_coding_system (6, args); | |
3881 | if (CONSP (coding_systems)) | |
03699b14 | 3882 | val = XCAR (coding_systems); |
f736ffbf KH |
3883 | } |
3884 | } | |
c9e82392 | 3885 | |
f736ffbf | 3886 | setup_coding_system (Fcheck_coding_system (val), &coding); |
f8569325 DL |
3887 | /* Ensure we set Vlast_coding_system_used. */ |
3888 | set_coding_system = 1; | |
c8a6d68a | 3889 | |
237a6fd2 RS |
3890 | if (NILP (current_buffer->enable_multibyte_characters) |
3891 | && ! NILP (val)) | |
3892 | /* We must suppress all character code conversion except for | |
3893 | end-of-line conversion. */ | |
57515cfe | 3894 | setup_raw_text_coding_system (&coding); |
54369368 | 3895 | |
8c3b9441 KH |
3896 | coding.src_multibyte = 0; |
3897 | coding.dst_multibyte | |
3898 | = !NILP (current_buffer->enable_multibyte_characters); | |
f736ffbf KH |
3899 | coding_system_decided = 1; |
3900 | } | |
6cf71bf1 | 3901 | |
3d0387c0 RS |
3902 | /* If requested, replace the accessible part of the buffer |
3903 | with the file contents. Avoid replacing text at the | |
3904 | beginning or end of the buffer that matches the file contents; | |
3dbcf3f6 RS |
3905 | that preserves markers pointing to the unchanged parts. |
3906 | ||
3907 | Here we implement this feature in an optimized way | |
3908 | for the case where code conversion is NOT needed. | |
3909 | The following if-statement handles the case of conversion | |
727a0b4a RS |
3910 | in a less optimal way. |
3911 | ||
3912 | If the code conversion is "automatic" then we try using this | |
3913 | method and hope for the best. | |
3914 | But if we discover the need for conversion, we give up on this method | |
3915 | and let the following if-statement handle the replace job. */ | |
3dbcf3f6 | 3916 | if (!NILP (replace) |
f736ffbf | 3917 | && BEGV < ZV |
8c3b9441 | 3918 | && !(coding.common_flags & CODING_REQUIRE_DECODING_MASK)) |
3d0387c0 | 3919 | { |
ec7adf26 RS |
3920 | /* same_at_start and same_at_end count bytes, |
3921 | because file access counts bytes | |
3922 | and BEG and END count bytes. */ | |
3923 | int same_at_start = BEGV_BYTE; | |
3924 | int same_at_end = ZV_BYTE; | |
9c28748f | 3925 | int overlap; |
6fdaa9a0 KH |
3926 | /* There is still a possibility we will find the need to do code |
3927 | conversion. If that happens, we set this variable to 1 to | |
727a0b4a | 3928 | give up on handling REPLACE in the optimized way. */ |
6fdaa9a0 | 3929 | int giveup_match_end = 0; |
9c28748f | 3930 | |
4d2a0879 RS |
3931 | if (XINT (beg) != 0) |
3932 | { | |
3933 | if (lseek (fd, XINT (beg), 0) < 0) | |
3934 | report_file_error ("Setting file position", | |
b1d1b865 | 3935 | Fcons (orig_filename, Qnil)); |
4d2a0879 RS |
3936 | } |
3937 | ||
3d0387c0 RS |
3938 | immediate_quit = 1; |
3939 | QUIT; | |
3940 | /* Count how many chars at the start of the file | |
3941 | match the text at the beginning of the buffer. */ | |
3942 | while (1) | |
3943 | { | |
3944 | int nread, bufpos; | |
3945 | ||
68c45bf0 | 3946 | nread = emacs_read (fd, buffer, sizeof buffer); |
3d0387c0 RS |
3947 | if (nread < 0) |
3948 | error ("IO error reading %s: %s", | |
d5db4077 | 3949 | SDATA (orig_filename), emacs_strerror (errno)); |
3d0387c0 RS |
3950 | else if (nread == 0) |
3951 | break; | |
6fdaa9a0 | 3952 | |
0ef69138 | 3953 | if (coding.type == coding_type_undecided) |
727a0b4a | 3954 | detect_coding (&coding, buffer, nread); |
8c3b9441 | 3955 | if (coding.common_flags & CODING_REQUIRE_DECODING_MASK) |
727a0b4a RS |
3956 | /* We found that the file should be decoded somehow. |
3957 | Let's give up here. */ | |
3958 | { | |
3959 | giveup_match_end = 1; | |
3960 | break; | |
3961 | } | |
3962 | ||
0ef69138 | 3963 | if (coding.eol_type == CODING_EOL_UNDECIDED) |
727a0b4a | 3964 | detect_eol (&coding, buffer, nread); |
1b335d29 | 3965 | if (coding.eol_type != CODING_EOL_UNDECIDED |
70ec4328 | 3966 | && coding.eol_type != CODING_EOL_LF) |
727a0b4a RS |
3967 | /* We found that the format of eol should be decoded. |
3968 | Let's give up here. */ | |
3969 | { | |
3970 | giveup_match_end = 1; | |
3971 | break; | |
3972 | } | |
3973 | ||
3d0387c0 | 3974 | bufpos = 0; |
ec7adf26 | 3975 | while (bufpos < nread && same_at_start < ZV_BYTE |
6fdaa9a0 | 3976 | && FETCH_BYTE (same_at_start) == buffer[bufpos]) |
3d0387c0 RS |
3977 | same_at_start++, bufpos++; |
3978 | /* If we found a discrepancy, stop the scan. | |
8e6208c5 | 3979 | Otherwise loop around and scan the next bufferful. */ |
3d0387c0 RS |
3980 | if (bufpos != nread) |
3981 | break; | |
3982 | } | |
3983 | immediate_quit = 0; | |
3984 | /* If the file matches the buffer completely, | |
3985 | there's no need to replace anything. */ | |
ec7adf26 | 3986 | if (same_at_start - BEGV_BYTE == XINT (end)) |
3d0387c0 | 3987 | { |
68c45bf0 | 3988 | emacs_close (fd); |
a1d2b64a | 3989 | specpdl_ptr--; |
1051b3b3 | 3990 | /* Truncate the buffer to the size of the file. */ |
7dae4502 | 3991 | del_range_1 (same_at_start, same_at_end, 0, 0); |
3d0387c0 RS |
3992 | goto handled; |
3993 | } | |
3994 | immediate_quit = 1; | |
3995 | QUIT; | |
3996 | /* Count how many chars at the end of the file | |
6fdaa9a0 KH |
3997 | match the text at the end of the buffer. But, if we have |
3998 | already found that decoding is necessary, don't waste time. */ | |
3999 | while (!giveup_match_end) | |
3d0387c0 RS |
4000 | { |
4001 | int total_read, nread, bufpos, curpos, trial; | |
4002 | ||
4003 | /* At what file position are we now scanning? */ | |
ec7adf26 | 4004 | curpos = XINT (end) - (ZV_BYTE - same_at_end); |
fc81fa9e KH |
4005 | /* If the entire file matches the buffer tail, stop the scan. */ |
4006 | if (curpos == 0) | |
4007 | break; | |
3d0387c0 RS |
4008 | /* How much can we scan in the next step? */ |
4009 | trial = min (curpos, sizeof buffer); | |
4010 | if (lseek (fd, curpos - trial, 0) < 0) | |
4011 | report_file_error ("Setting file position", | |
b1d1b865 | 4012 | Fcons (orig_filename, Qnil)); |
3d0387c0 | 4013 | |
b02439c8 | 4014 | total_read = nread = 0; |
3d0387c0 RS |
4015 | while (total_read < trial) |
4016 | { | |
68c45bf0 | 4017 | nread = emacs_read (fd, buffer + total_read, trial - total_read); |
2bd2273e | 4018 | if (nread < 0) |
3d0387c0 | 4019 | error ("IO error reading %s: %s", |
d5db4077 | 4020 | SDATA (orig_filename), emacs_strerror (errno)); |
2bd2273e GM |
4021 | else if (nread == 0) |
4022 | break; | |
3d0387c0 RS |
4023 | total_read += nread; |
4024 | } | |
efdc16c9 | 4025 | |
8e6208c5 | 4026 | /* Scan this bufferful from the end, comparing with |
3d0387c0 RS |
4027 | the Emacs buffer. */ |
4028 | bufpos = total_read; | |
efdc16c9 | 4029 | |
3d0387c0 RS |
4030 | /* Compare with same_at_start to avoid counting some buffer text |
4031 | as matching both at the file's beginning and at the end. */ | |
4032 | while (bufpos > 0 && same_at_end > same_at_start | |
6fdaa9a0 | 4033 | && FETCH_BYTE (same_at_end - 1) == buffer[bufpos - 1]) |
3d0387c0 | 4034 | same_at_end--, bufpos--; |
727a0b4a | 4035 | |
3d0387c0 | 4036 | /* If we found a discrepancy, stop the scan. |
8e6208c5 | 4037 | Otherwise loop around and scan the preceding bufferful. */ |
3d0387c0 | 4038 | if (bufpos != 0) |
727a0b4a RS |
4039 | { |
4040 | /* If this discrepancy is because of code conversion, | |
4041 | we cannot use this method; giveup and try the other. */ | |
4042 | if (same_at_end > same_at_start | |
4043 | && FETCH_BYTE (same_at_end - 1) >= 0200 | |
71312b68 | 4044 | && ! NILP (current_buffer->enable_multibyte_characters) |
c8a6d68a | 4045 | && (CODING_MAY_REQUIRE_DECODING (&coding))) |
727a0b4a RS |
4046 | giveup_match_end = 1; |
4047 | break; | |
4048 | } | |
b02439c8 GM |
4049 | |
4050 | if (nread == 0) | |
4051 | break; | |
3d0387c0 RS |
4052 | } |
4053 | immediate_quit = 0; | |
9c28748f | 4054 | |
727a0b4a RS |
4055 | if (! giveup_match_end) |
4056 | { | |
ec7adf26 RS |
4057 | int temp; |
4058 | ||
727a0b4a | 4059 | /* We win! We can handle REPLACE the optimized way. */ |
9c28748f | 4060 | |
20f6783d RS |
4061 | /* Extend the start of non-matching text area to multibyte |
4062 | character boundary. */ | |
4063 | if (! NILP (current_buffer->enable_multibyte_characters)) | |
4064 | while (same_at_start > BEGV_BYTE | |
4065 | && ! CHAR_HEAD_P (FETCH_BYTE (same_at_start))) | |
4066 | same_at_start--; | |
4067 | ||
4068 | /* Extend the end of non-matching text area to multibyte | |
71312b68 RS |
4069 | character boundary. */ |
4070 | if (! NILP (current_buffer->enable_multibyte_characters)) | |
ec7adf26 RS |
4071 | while (same_at_end < ZV_BYTE |
4072 | && ! CHAR_HEAD_P (FETCH_BYTE (same_at_end))) | |
71312b68 RS |
4073 | same_at_end++; |
4074 | ||
727a0b4a | 4075 | /* Don't try to reuse the same piece of text twice. */ |
ec7adf26 RS |
4076 | overlap = (same_at_start - BEGV_BYTE |
4077 | - (same_at_end + st.st_size - ZV)); | |
727a0b4a RS |
4078 | if (overlap > 0) |
4079 | same_at_end += overlap; | |
9c28748f | 4080 | |
727a0b4a | 4081 | /* Arrange to read only the nonmatching middle part of the file. */ |
ec7adf26 RS |
4082 | XSETFASTINT (beg, XINT (beg) + (same_at_start - BEGV_BYTE)); |
4083 | XSETFASTINT (end, XINT (end) - (ZV_BYTE - same_at_end)); | |
3dbcf3f6 | 4084 | |
ec7adf26 | 4085 | del_range_byte (same_at_start, same_at_end, 0); |
727a0b4a | 4086 | /* Insert from the file at the proper position. */ |
ec7adf26 RS |
4087 | temp = BYTE_TO_CHAR (same_at_start); |
4088 | SET_PT_BOTH (temp, same_at_start); | |
727a0b4a RS |
4089 | |
4090 | /* If display currently starts at beginning of line, | |
4091 | keep it that way. */ | |
4092 | if (XBUFFER (XWINDOW (selected_window)->buffer) == current_buffer) | |
4093 | XWINDOW (selected_window)->start_at_line_beg = Fbolp (); | |
4094 | ||
4095 | replace_handled = 1; | |
4096 | } | |
3dbcf3f6 RS |
4097 | } |
4098 | ||
4099 | /* If requested, replace the accessible part of the buffer | |
4100 | with the file contents. Avoid replacing text at the | |
4101 | beginning or end of the buffer that matches the file contents; | |
4102 | that preserves markers pointing to the unchanged parts. | |
4103 | ||
4104 | Here we implement this feature for the case where code conversion | |
4105 | is needed, in a simple way that needs a lot of memory. | |
4106 | The preceding if-statement handles the case of no conversion | |
4107 | in a more optimized way. */ | |
f736ffbf | 4108 | if (!NILP (replace) && ! replace_handled && BEGV < ZV) |
3dbcf3f6 | 4109 | { |
ec7adf26 RS |
4110 | int same_at_start = BEGV_BYTE; |
4111 | int same_at_end = ZV_BYTE; | |
3dbcf3f6 RS |
4112 | int overlap; |
4113 | int bufpos; | |
4114 | /* Make sure that the gap is large enough. */ | |
4115 | int bufsize = 2 * st.st_size; | |
b00ca0d7 | 4116 | unsigned char *conversion_buffer = (unsigned char *) xmalloc (bufsize); |
ec7adf26 | 4117 | int temp; |
3dbcf3f6 RS |
4118 | |
4119 | /* First read the whole file, performing code conversion into | |
4120 | CONVERSION_BUFFER. */ | |
4121 | ||
727a0b4a RS |
4122 | if (lseek (fd, XINT (beg), 0) < 0) |
4123 | { | |
68cfd853 | 4124 | xfree (conversion_buffer); |
727a0b4a | 4125 | report_file_error ("Setting file position", |
b1d1b865 | 4126 | Fcons (orig_filename, Qnil)); |
727a0b4a RS |
4127 | } |
4128 | ||
3dbcf3f6 RS |
4129 | total = st.st_size; /* Total bytes in the file. */ |
4130 | how_much = 0; /* Bytes read from file so far. */ | |
4131 | inserted = 0; /* Bytes put into CONVERSION_BUFFER so far. */ | |
4132 | unprocessed = 0; /* Bytes not processed in previous loop. */ | |
4133 | ||
4134 | while (how_much < total) | |
4135 | { | |
4136 | /* try is reserved in some compilers (Microsoft C) */ | |
4137 | int trytry = min (total - how_much, READ_BUF_SIZE - unprocessed); | |
cadf50ff | 4138 | unsigned char *destination = read_buf + unprocessed; |
3dbcf3f6 RS |
4139 | int this; |
4140 | ||
4141 | /* Allow quitting out of the actual I/O. */ | |
4142 | immediate_quit = 1; | |
4143 | QUIT; | |
68c45bf0 | 4144 | this = emacs_read (fd, destination, trytry); |
3dbcf3f6 RS |
4145 | immediate_quit = 0; |
4146 | ||
4147 | if (this < 0 || this + unprocessed == 0) | |
4148 | { | |
4149 | how_much = this; | |
4150 | break; | |
4151 | } | |
4152 | ||
4153 | how_much += this; | |
4154 | ||
c8a6d68a | 4155 | if (CODING_MAY_REQUIRE_DECODING (&coding)) |
3dbcf3f6 | 4156 | { |
c8a6d68a | 4157 | int require, result; |
3dbcf3f6 RS |
4158 | |
4159 | this += unprocessed; | |
4160 | ||
4161 | /* If we are using more space than estimated, | |
4162 | make CONVERSION_BUFFER bigger. */ | |
4163 | require = decoding_buffer_size (&coding, this); | |
4164 | if (inserted + require + 2 * (total - how_much) > bufsize) | |
4165 | { | |
4166 | bufsize = inserted + require + 2 * (total - how_much); | |
92cf1086 | 4167 | conversion_buffer = (unsigned char *) xrealloc (conversion_buffer, bufsize); |
3dbcf3f6 RS |
4168 | } |
4169 | ||
4170 | /* Convert this batch with results in CONVERSION_BUFFER. */ | |
4171 | if (how_much >= total) /* This is the last block. */ | |
c8a6d68a | 4172 | coding.mode |= CODING_MODE_LAST_BLOCK; |
1ddb09f5 GM |
4173 | if (coding.composing != COMPOSITION_DISABLED) |
4174 | coding_allocate_composition_data (&coding, BEGV); | |
c8a6d68a KH |
4175 | result = decode_coding (&coding, read_buf, |
4176 | conversion_buffer + inserted, | |
4177 | this, bufsize - inserted); | |
3dbcf3f6 RS |
4178 | |
4179 | /* Save for next iteration whatever we didn't convert. */ | |
c8a6d68a KH |
4180 | unprocessed = this - coding.consumed; |
4181 | bcopy (read_buf + coding.consumed, read_buf, unprocessed); | |
8c3b9441 KH |
4182 | if (!NILP (current_buffer->enable_multibyte_characters)) |
4183 | this = coding.produced; | |
4184 | else | |
4185 | this = str_as_unibyte (conversion_buffer + inserted, | |
4186 | coding.produced); | |
3dbcf3f6 RS |
4187 | } |
4188 | ||
4189 | inserted += this; | |
4190 | } | |
4191 | ||
c8a6d68a | 4192 | /* At this point, INSERTED is how many characters (i.e. bytes) |
3dbcf3f6 RS |
4193 | are present in CONVERSION_BUFFER. |
4194 | HOW_MUCH should equal TOTAL, | |
4195 | or should be <= 0 if we couldn't read the file. */ | |
4196 | ||
4197 | if (how_much < 0) | |
4198 | { | |
a36837e4 | 4199 | xfree (conversion_buffer); |
3dbcf3f6 RS |
4200 | |
4201 | if (how_much == -1) | |
4202 | error ("IO error reading %s: %s", | |
d5db4077 | 4203 | SDATA (orig_filename), emacs_strerror (errno)); |
3dbcf3f6 RS |
4204 | else if (how_much == -2) |
4205 | error ("maximum buffer size exceeded"); | |
4206 | } | |
4207 | ||
4208 | /* Compare the beginning of the converted file | |
4209 | with the buffer text. */ | |
4210 | ||
4211 | bufpos = 0; | |
4212 | while (bufpos < inserted && same_at_start < same_at_end | |
4213 | && FETCH_BYTE (same_at_start) == conversion_buffer[bufpos]) | |
4214 | same_at_start++, bufpos++; | |
4215 | ||
4216 | /* If the file matches the buffer completely, | |
4217 | there's no need to replace anything. */ | |
4218 | ||
4219 | if (bufpos == inserted) | |
4220 | { | |
a36837e4 | 4221 | xfree (conversion_buffer); |
68c45bf0 | 4222 | emacs_close (fd); |
3dbcf3f6 RS |
4223 | specpdl_ptr--; |
4224 | /* Truncate the buffer to the size of the file. */ | |
427f5aab KH |
4225 | del_range_byte (same_at_start, same_at_end, 0); |
4226 | inserted = 0; | |
3dbcf3f6 RS |
4227 | goto handled; |
4228 | } | |
4229 | ||
20f6783d RS |
4230 | /* Extend the start of non-matching text area to multibyte |
4231 | character boundary. */ | |
4232 | if (! NILP (current_buffer->enable_multibyte_characters)) | |
4233 | while (same_at_start > BEGV_BYTE | |
4234 | && ! CHAR_HEAD_P (FETCH_BYTE (same_at_start))) | |
4235 | same_at_start--; | |
4236 | ||
3dbcf3f6 RS |
4237 | /* Scan this bufferful from the end, comparing with |
4238 | the Emacs buffer. */ | |
4239 | bufpos = inserted; | |
4240 | ||
4241 | /* Compare with same_at_start to avoid counting some buffer text | |
4242 | as matching both at the file's beginning and at the end. */ | |
4243 | while (bufpos > 0 && same_at_end > same_at_start | |
4244 | && FETCH_BYTE (same_at_end - 1) == conversion_buffer[bufpos - 1]) | |
4245 | same_at_end--, bufpos--; | |
4246 | ||
20f6783d RS |
4247 | /* Extend the end of non-matching text area to multibyte |
4248 | character boundary. */ | |
4249 | if (! NILP (current_buffer->enable_multibyte_characters)) | |
4250 | while (same_at_end < ZV_BYTE | |
4251 | && ! CHAR_HEAD_P (FETCH_BYTE (same_at_end))) | |
4252 | same_at_end++; | |
4253 | ||
3dbcf3f6 | 4254 | /* Don't try to reuse the same piece of text twice. */ |
ec7adf26 | 4255 | overlap = same_at_start - BEGV_BYTE - (same_at_end + inserted - ZV_BYTE); |
3dbcf3f6 RS |
4256 | if (overlap > 0) |
4257 | same_at_end += overlap; | |
4258 | ||
727a0b4a RS |
4259 | /* If display currently starts at beginning of line, |
4260 | keep it that way. */ | |
4261 | if (XBUFFER (XWINDOW (selected_window)->buffer) == current_buffer) | |
4262 | XWINDOW (selected_window)->start_at_line_beg = Fbolp (); | |
4263 | ||
3dbcf3f6 RS |
4264 | /* Replace the chars that we need to replace, |
4265 | and update INSERTED to equal the number of bytes | |
4266 | we are taking from the file. */ | |
ec7adf26 | 4267 | inserted -= (Z_BYTE - same_at_end) + (same_at_start - BEG_BYTE); |
427f5aab | 4268 | |
643c73b9 | 4269 | if (same_at_end != same_at_start) |
427f5aab KH |
4270 | { |
4271 | del_range_byte (same_at_start, same_at_end, 0); | |
4272 | temp = GPT; | |
4273 | same_at_start = GPT_BYTE; | |
4274 | } | |
643c73b9 RS |
4275 | else |
4276 | { | |
643c73b9 | 4277 | temp = BYTE_TO_CHAR (same_at_start); |
643c73b9 | 4278 | } |
427f5aab KH |
4279 | /* Insert from the file at the proper position. */ |
4280 | SET_PT_BOTH (temp, same_at_start); | |
ec7adf26 RS |
4281 | insert_1 (conversion_buffer + same_at_start - BEG_BYTE, inserted, |
4282 | 0, 0, 0); | |
1ddb09f5 GM |
4283 | if (coding.cmp_data && coding.cmp_data->used) |
4284 | coding_restore_composition (&coding, Fcurrent_buffer ()); | |
4285 | coding_free_composition_data (&coding); | |
efdc16c9 | 4286 | |
427f5aab KH |
4287 | /* Set `inserted' to the number of inserted characters. */ |
4288 | inserted = PT - temp; | |
3dbcf3f6 | 4289 | |
93184560 | 4290 | xfree (conversion_buffer); |
68c45bf0 | 4291 | emacs_close (fd); |
3dbcf3f6 RS |
4292 | specpdl_ptr--; |
4293 | ||
3dbcf3f6 | 4294 | goto handled; |
3d0387c0 RS |
4295 | } |
4296 | ||
d4b8687b RS |
4297 | if (! not_regular) |
4298 | { | |
4299 | register Lisp_Object temp; | |
7fded690 | 4300 | |
d4b8687b | 4301 | total = XINT (end) - XINT (beg); |
570d7624 | 4302 | |
d4b8687b RS |
4303 | /* Make sure point-max won't overflow after this insertion. */ |
4304 | XSETINT (temp, total); | |
4305 | if (total != XINT (temp)) | |
4306 | error ("Maximum buffer size exceeded"); | |
4307 | } | |
4308 | else | |
4309 | /* For a special file, all we can do is guess. */ | |
4310 | total = READ_BUF_SIZE; | |
570d7624 | 4311 | |
57d8d468 | 4312 | if (NILP (visit) && total > 0) |
6c478ee2 | 4313 | prepare_to_modify_buffer (PT, PT, NULL); |
570d7624 | 4314 | |
7fe52289 | 4315 | move_gap (PT); |
7fded690 JB |
4316 | if (GAP_SIZE < total) |
4317 | make_gap (total - GAP_SIZE); | |
4318 | ||
a1d2b64a | 4319 | if (XINT (beg) != 0 || !NILP (replace)) |
7fded690 JB |
4320 | { |
4321 | if (lseek (fd, XINT (beg), 0) < 0) | |
b1d1b865 RS |
4322 | report_file_error ("Setting file position", |
4323 | Fcons (orig_filename, Qnil)); | |
7fded690 JB |
4324 | } |
4325 | ||
6fdaa9a0 | 4326 | /* In the following loop, HOW_MUCH contains the total bytes read so |
c8a6d68a KH |
4327 | far for a regular file, and not changed for a special file. But, |
4328 | before exiting the loop, it is set to a negative value if I/O | |
4329 | error occurs. */ | |
a1d2b64a | 4330 | how_much = 0; |
efdc16c9 | 4331 | |
6fdaa9a0 KH |
4332 | /* Total bytes inserted. */ |
4333 | inserted = 0; | |
efdc16c9 | 4334 | |
c8a6d68a KH |
4335 | /* Here, we don't do code conversion in the loop. It is done by |
4336 | code_convert_region after all data are read into the buffer. */ | |
1b978129 GM |
4337 | { |
4338 | int gap_size = GAP_SIZE; | |
efdc16c9 | 4339 | |
1b978129 GM |
4340 | while (how_much < total) |
4341 | { | |
5e570b75 | 4342 | /* try is reserved in some compilers (Microsoft C) */ |
1b978129 GM |
4343 | int trytry = min (total - how_much, READ_BUF_SIZE); |
4344 | int this; | |
570d7624 | 4345 | |
1b978129 GM |
4346 | if (not_regular) |
4347 | { | |
4348 | Lisp_Object val; | |
570d7624 | 4349 | |
1b978129 GM |
4350 | /* Maybe make more room. */ |
4351 | if (gap_size < trytry) | |
4352 | { | |
4353 | make_gap (total - gap_size); | |
4354 | gap_size = GAP_SIZE; | |
4355 | } | |
4356 | ||
4357 | /* Read from the file, capturing `quit'. When an | |
4358 | error occurs, end the loop, and arrange for a quit | |
4359 | to be signaled after decoding the text we read. */ | |
4360 | non_regular_fd = fd; | |
4361 | non_regular_inserted = inserted; | |
4362 | non_regular_nbytes = trytry; | |
4363 | val = internal_condition_case_1 (read_non_regular, Qnil, Qerror, | |
4364 | read_non_regular_quit); | |
4365 | if (NILP (val)) | |
4366 | { | |
4367 | read_quit = 1; | |
4368 | break; | |
4369 | } | |
4370 | ||
4371 | this = XINT (val); | |
4372 | } | |
4373 | else | |
4374 | { | |
4375 | /* Allow quitting out of the actual I/O. We don't make text | |
4376 | part of the buffer until all the reading is done, so a C-g | |
4377 | here doesn't do any harm. */ | |
4378 | immediate_quit = 1; | |
4379 | QUIT; | |
28c3eb5a | 4380 | this = emacs_read (fd, BEG_ADDR + PT_BYTE - BEG_BYTE + inserted, trytry); |
1b978129 GM |
4381 | immediate_quit = 0; |
4382 | } | |
efdc16c9 | 4383 | |
1b978129 GM |
4384 | if (this <= 0) |
4385 | { | |
4386 | how_much = this; | |
4387 | break; | |
4388 | } | |
4389 | ||
4390 | gap_size -= this; | |
4391 | ||
4392 | /* For a regular file, where TOTAL is the real size, | |
4393 | count HOW_MUCH to compare with it. | |
4394 | For a special file, where TOTAL is just a buffer size, | |
4395 | so don't bother counting in HOW_MUCH. | |
4396 | (INSERTED is where we count the number of characters inserted.) */ | |
4397 | if (! not_regular) | |
4398 | how_much += this; | |
4399 | inserted += this; | |
4400 | } | |
4401 | } | |
4402 | ||
4403 | /* Make the text read part of the buffer. */ | |
4404 | GAP_SIZE -= inserted; | |
4405 | GPT += inserted; | |
4406 | GPT_BYTE += inserted; | |
4407 | ZV += inserted; | |
4408 | ZV_BYTE += inserted; | |
4409 | Z += inserted; | |
4410 | Z_BYTE += inserted; | |
6fdaa9a0 | 4411 | |
c8a6d68a KH |
4412 | if (GAP_SIZE > 0) |
4413 | /* Put an anchor to ensure multi-byte form ends at gap. */ | |
4414 | *GPT_ADDR = 0; | |
d4b8687b | 4415 | |
68c45bf0 | 4416 | emacs_close (fd); |
6fdaa9a0 | 4417 | |
c8a6d68a KH |
4418 | /* Discard the unwind protect for closing the file. */ |
4419 | specpdl_ptr--; | |
6fdaa9a0 | 4420 | |
c8a6d68a KH |
4421 | if (how_much < 0) |
4422 | error ("IO error reading %s: %s", | |
d5db4077 | 4423 | SDATA (orig_filename), emacs_strerror (errno)); |
ec7adf26 | 4424 | |
f8569325 DL |
4425 | notfound: |
4426 | ||
2df42e09 | 4427 | if (! coding_system_decided) |
c8a6d68a | 4428 | { |
2df42e09 | 4429 | /* The coding system is not yet decided. Decide it by an |
dfe35e7b RS |
4430 | optimized method for handling `coding:' tag. |
4431 | ||
4432 | Note that we can get here only if the buffer was empty | |
4433 | before the insertion. */ | |
2df42e09 KH |
4434 | Lisp_Object val; |
4435 | val = Qnil; | |
f736ffbf | 4436 | |
2df42e09 KH |
4437 | if (!NILP (Vcoding_system_for_read)) |
4438 | val = Vcoding_system_for_read; | |
4439 | else | |
4440 | { | |
98a7d268 KH |
4441 | /* Since we are sure that the current buffer was empty |
4442 | before the insertion, we can toggle | |
4443 | enable-multibyte-characters directly here without taking | |
4444 | care of marker adjustment and byte combining problem. By | |
4445 | this way, we can run Lisp program safely before decoding | |
4446 | the inserted text. */ | |
4447 | Lisp_Object unwind_data; | |
aed13378 | 4448 | int count = SPECPDL_INDEX (); |
2df42e09 | 4449 | |
98a7d268 KH |
4450 | unwind_data = Fcons (current_buffer->enable_multibyte_characters, |
4451 | Fcons (current_buffer->undo_list, | |
4452 | Fcurrent_buffer ())); | |
2df42e09 | 4453 | current_buffer->enable_multibyte_characters = Qnil; |
98a7d268 KH |
4454 | current_buffer->undo_list = Qt; |
4455 | record_unwind_protect (decide_coding_unwind, unwind_data); | |
4456 | ||
4457 | if (inserted > 0 && ! NILP (Vset_auto_coding_function)) | |
4458 | { | |
1255deb9 KH |
4459 | val = call2 (Vset_auto_coding_function, |
4460 | filename, make_number (inserted)); | |
2df42e09 | 4461 | } |
f736ffbf | 4462 | |
2df42e09 KH |
4463 | if (NILP (val)) |
4464 | { | |
4465 | /* If the coding system is not yet decided, check | |
4466 | file-coding-system-alist. */ | |
4467 | Lisp_Object args[6], coding_systems; | |
f736ffbf | 4468 | |
2df42e09 KH |
4469 | args[0] = Qinsert_file_contents, args[1] = orig_filename; |
4470 | args[2] = visit, args[3] = beg, args[4] = end, args[5] = Qnil; | |
4471 | coding_systems = Ffind_operation_coding_system (6, args); | |
4472 | if (CONSP (coding_systems)) | |
03699b14 | 4473 | val = XCAR (coding_systems); |
f736ffbf | 4474 | } |
98a7d268 KH |
4475 | |
4476 | unbind_to (count, Qnil); | |
4477 | inserted = Z_BYTE - BEG_BYTE; | |
2df42e09 | 4478 | } |
f736ffbf | 4479 | |
2df42e09 KH |
4480 | /* The following kludgy code is to avoid some compiler bug. |
4481 | We can't simply do | |
4482 | setup_coding_system (val, &coding); | |
4483 | on some system. */ | |
4484 | { | |
4485 | struct coding_system temp_coding; | |
4486 | setup_coding_system (val, &temp_coding); | |
4487 | bcopy (&temp_coding, &coding, sizeof coding); | |
4488 | } | |
f8569325 DL |
4489 | /* Ensure we set Vlast_coding_system_used. */ |
4490 | set_coding_system = 1; | |
f736ffbf | 4491 | |
237a6fd2 RS |
4492 | if (NILP (current_buffer->enable_multibyte_characters) |
4493 | && ! NILP (val)) | |
4494 | /* We must suppress all character code conversion except for | |
2df42e09 KH |
4495 | end-of-line conversion. */ |
4496 | setup_raw_text_coding_system (&coding); | |
6db43875 KH |
4497 | coding.src_multibyte = 0; |
4498 | coding.dst_multibyte | |
4499 | = !NILP (current_buffer->enable_multibyte_characters); | |
2df42e09 | 4500 | } |
f736ffbf | 4501 | |
8c3b9441 | 4502 | if (!NILP (visit) |
24766480 GM |
4503 | /* Can't do this if part of the buffer might be preserved. */ |
4504 | && NILP (replace) | |
8c3b9441 KH |
4505 | && (coding.type == coding_type_no_conversion |
4506 | || coding.type == coding_type_raw_text)) | |
4507 | { | |
24766480 GM |
4508 | /* Visiting a file with these coding system makes the buffer |
4509 | unibyte. */ | |
4510 | current_buffer->enable_multibyte_characters = Qnil; | |
e1249666 | 4511 | coding.dst_multibyte = 0; |
8c3b9441 KH |
4512 | } |
4513 | ||
c91beee2 | 4514 | if (inserted > 0 || coding.type == coding_type_ccl) |
2df42e09 | 4515 | { |
c8a6d68a | 4516 | if (CODING_MAY_REQUIRE_DECODING (&coding)) |
64e0ae2a KH |
4517 | { |
4518 | code_convert_region (PT, PT_BYTE, PT + inserted, PT_BYTE + inserted, | |
4519 | &coding, 0, 0); | |
8c3b9441 | 4520 | inserted = coding.produced_char; |
f8198e19 | 4521 | } |
e9cea947 AS |
4522 | else |
4523 | adjust_after_insert (PT, PT_BYTE, PT + inserted, PT_BYTE + inserted, | |
8c3b9441 | 4524 | inserted); |
2df42e09 | 4525 | } |
570d7624 | 4526 | |
cf6d2357 RS |
4527 | /* Now INSERTED is measured in characters. */ |
4528 | ||
04e6f79c | 4529 | #ifdef DOS_NT |
2df42e09 KH |
4530 | /* Use the conversion type to determine buffer-file-type |
4531 | (find-buffer-file-type is now used to help determine the | |
4532 | conversion). */ | |
efdc16c9 | 4533 | if ((coding.eol_type == CODING_EOL_UNDECIDED |
2df42e09 KH |
4534 | || coding.eol_type == CODING_EOL_LF) |
4535 | && ! CODING_REQUIRE_DECODING (&coding)) | |
4536 | current_buffer->buffer_file_type = Qt; | |
4537 | else | |
4538 | current_buffer->buffer_file_type = Qnil; | |
04e6f79c | 4539 | #endif |
570d7624 | 4540 | |
32f4334d | 4541 | handled: |
570d7624 | 4542 | |
265a9e55 | 4543 | if (!NILP (visit)) |
570d7624 | 4544 | { |
cfadd376 RS |
4545 | if (!EQ (current_buffer->undo_list, Qt)) |
4546 | current_buffer->undo_list = Qnil; | |
570d7624 | 4547 | #ifdef APOLLO |
d5db4077 | 4548 | stat (SDATA (filename), &st); |
570d7624 | 4549 | #endif |
62bcf009 | 4550 | |
a7e82472 RS |
4551 | if (NILP (handler)) |
4552 | { | |
4553 | current_buffer->modtime = st.st_mtime; | |
b1d1b865 | 4554 | current_buffer->filename = orig_filename; |
a7e82472 | 4555 | } |
62bcf009 | 4556 | |
95385625 | 4557 | SAVE_MODIFF = MODIFF; |
570d7624 | 4558 | current_buffer->auto_save_modified = MODIFF; |
2acfd7ae | 4559 | XSETFASTINT (current_buffer->save_length, Z - BEG); |
570d7624 | 4560 | #ifdef CLASH_DETECTION |
32f4334d RS |
4561 | if (NILP (handler)) |
4562 | { | |
f471f4c2 RS |
4563 | if (!NILP (current_buffer->file_truename)) |
4564 | unlock_file (current_buffer->file_truename); | |
32f4334d RS |
4565 | unlock_file (filename); |
4566 | } | |
570d7624 | 4567 | #endif /* CLASH_DETECTION */ |
330bfe57 RS |
4568 | if (not_regular) |
4569 | Fsignal (Qfile_error, | |
4570 | Fcons (build_string ("not a regular file"), | |
b1d1b865 | 4571 | Fcons (orig_filename, Qnil))); |
570d7624 JB |
4572 | } |
4573 | ||
b6426b03 KH |
4574 | if (set_coding_system) |
4575 | Vlast_coding_system_used = coding.symbol; | |
4576 | ||
2080470e | 4577 | if (! NILP (Ffboundp (Qafter_insert_file_set_coding))) |
b6426b03 | 4578 | { |
2080470e | 4579 | insval = call1 (Qafter_insert_file_set_coding, make_number (inserted)); |
b6426b03 KH |
4580 | if (! NILP (insval)) |
4581 | { | |
4582 | CHECK_NUMBER (insval); | |
4583 | inserted = XFASTINT (insval); | |
4584 | } | |
4585 | } | |
4586 | ||
0d420e88 | 4587 | /* Decode file format */ |
c8a6d68a | 4588 | if (inserted > 0) |
0d420e88 | 4589 | { |
ed8e506f | 4590 | int empty_undo_list_p = 0; |
efdc16c9 | 4591 | |
ed8e506f GM |
4592 | /* If we're anyway going to discard undo information, don't |
4593 | record it in the first place. The buffer's undo list at this | |
4594 | point is either nil or t when visiting a file. */ | |
4595 | if (!NILP (visit)) | |
4596 | { | |
4597 | empty_undo_list_p = NILP (current_buffer->undo_list); | |
4598 | current_buffer->undo_list = Qt; | |
4599 | } | |
efdc16c9 | 4600 | |
199607e4 | 4601 | insval = call3 (Qformat_decode, |
c8a6d68a | 4602 | Qnil, make_number (inserted), visit); |
b7826503 | 4603 | CHECK_NUMBER (insval); |
c8a6d68a | 4604 | inserted = XFASTINT (insval); |
efdc16c9 | 4605 | |
ed8e506f GM |
4606 | if (!NILP (visit)) |
4607 | current_buffer->undo_list = empty_undo_list_p ? Qnil : Qt; | |
0d420e88 BG |
4608 | } |
4609 | ||
0342d8c5 RS |
4610 | /* Call after-change hooks for the inserted text, aside from the case |
4611 | of normal visiting (not with REPLACE), which is done in a new buffer | |
4612 | "before" the buffer is changed. */ | |
c8a6d68a | 4613 | if (inserted > 0 && total > 0 |
0342d8c5 | 4614 | && (NILP (visit) || !NILP (replace))) |
ce51c54c KH |
4615 | { |
4616 | signal_after_change (PT, 0, inserted); | |
4617 | update_compositions (PT, PT, CHECK_BORDER); | |
4618 | } | |
b56567b5 | 4619 | |
f8569325 | 4620 | p = Vafter_insert_file_functions; |
28c3eb5a | 4621 | while (CONSP (p)) |
d6a3cc15 | 4622 | { |
28c3eb5a | 4623 | insval = call1 (XCAR (p), make_number (inserted)); |
f8569325 | 4624 | if (!NILP (insval)) |
d6a3cc15 | 4625 | { |
b7826503 | 4626 | CHECK_NUMBER (insval); |
f8569325 | 4627 | inserted = XFASTINT (insval); |
d6a3cc15 | 4628 | } |
f8569325 | 4629 | QUIT; |
28c3eb5a | 4630 | p = XCDR (p); |
f8569325 DL |
4631 | } |
4632 | ||
4633 | if (!NILP (visit) | |
4634 | && current_buffer->modtime == -1) | |
4635 | { | |
4636 | /* If visiting nonexistent file, return nil. */ | |
4637 | report_file_error ("Opening input file", Fcons (orig_filename, Qnil)); | |
d6a3cc15 RS |
4638 | } |
4639 | ||
1b978129 GM |
4640 | if (read_quit) |
4641 | Fsignal (Qquit, Qnil); | |
4642 | ||
ec7adf26 | 4643 | /* ??? Retval needs to be dealt with in all cases consistently. */ |
a1d2b64a | 4644 | if (NILP (val)) |
b1d1b865 | 4645 | val = Fcons (orig_filename, |
a1d2b64a RS |
4646 | Fcons (make_number (inserted), |
4647 | Qnil)); | |
4648 | ||
4649 | RETURN_UNGCPRO (unbind_to (count, val)); | |
570d7624 | 4650 | } |
7fded690 | 4651 | \f |
236a12f2 SM |
4652 | static Lisp_Object build_annotations P_ ((Lisp_Object, Lisp_Object)); |
4653 | static Lisp_Object build_annotations_2 P_ ((Lisp_Object, Lisp_Object, | |
4654 | Lisp_Object, Lisp_Object)); | |
d6a3cc15 | 4655 | |
6fc6f94b | 4656 | /* If build_annotations switched buffers, switch back to BUF. |
6fdaa9a0 KH |
4657 | Kill the temporary buffer that was selected in the meantime. |
4658 | ||
4659 | Since this kill only the last temporary buffer, some buffers remain | |
4660 | not killed if build_annotations switched buffers more than once. | |
4661 | -- K.Handa */ | |
6fc6f94b | 4662 | |
199607e4 | 4663 | static Lisp_Object |
6fc6f94b RS |
4664 | build_annotations_unwind (buf) |
4665 | Lisp_Object buf; | |
4666 | { | |
4667 | Lisp_Object tembuf; | |
4668 | ||
4669 | if (XBUFFER (buf) == current_buffer) | |
4670 | return Qnil; | |
4671 | tembuf = Fcurrent_buffer (); | |
4672 | Fset_buffer (buf); | |
4673 | Fkill_buffer (tembuf); | |
4674 | return Qnil; | |
4675 | } | |
4676 | ||
7c82a4a9 SM |
4677 | /* Decide the coding-system to encode the data with. */ |
4678 | ||
4679 | void | |
4680 | choose_write_coding_system (start, end, filename, | |
4681 | append, visit, lockname, coding) | |
4682 | Lisp_Object start, end, filename, append, visit, lockname; | |
4683 | struct coding_system *coding; | |
4684 | { | |
4685 | Lisp_Object val; | |
4686 | ||
4687 | if (auto_saving) | |
356a6224 KH |
4688 | { |
4689 | /* We use emacs-mule for auto saving... */ | |
4690 | setup_coding_system (Qemacs_mule, coding); | |
4691 | /* ... but with the special flag to indicate not to strip off | |
4692 | leading code of eight-bit-control chars. */ | |
4693 | coding->flags = 1; | |
4694 | goto done_setup_coding; | |
4695 | } | |
7c82a4a9 | 4696 | else if (!NILP (Vcoding_system_for_write)) |
42b01e1e KH |
4697 | { |
4698 | val = Vcoding_system_for_write; | |
4699 | if (coding_system_require_warning | |
4700 | && !NILP (Ffboundp (Vselect_safe_coding_system_function))) | |
4701 | /* Confirm that VAL can surely encode the current region. */ | |
4702 | val = call5 (Vselect_safe_coding_system_function, | |
4703 | start, end, Fcons (Qt, Fcons (val, Qnil)), | |
4704 | Qnil, filename); | |
4705 | } | |
7c82a4a9 SM |
4706 | else |
4707 | { | |
4708 | /* If the variable `buffer-file-coding-system' is set locally, | |
4709 | it means that the file was read with some kind of code | |
4710 | conversion or the variable is explicitly set by users. We | |
4711 | had better write it out with the same coding system even if | |
4712 | `enable-multibyte-characters' is nil. | |
4713 | ||
4714 | If it is not set locally, we anyway have to convert EOL | |
4715 | format if the default value of `buffer-file-coding-system' | |
4716 | tells that it is not Unix-like (LF only) format. */ | |
4717 | int using_default_coding = 0; | |
4718 | int force_raw_text = 0; | |
4719 | ||
4720 | val = current_buffer->buffer_file_coding_system; | |
4721 | if (NILP (val) | |
4722 | || NILP (Flocal_variable_p (Qbuffer_file_coding_system, Qnil))) | |
4723 | { | |
4724 | val = Qnil; | |
4725 | if (NILP (current_buffer->enable_multibyte_characters)) | |
4726 | force_raw_text = 1; | |
4727 | } | |
efdc16c9 | 4728 | |
7c82a4a9 SM |
4729 | if (NILP (val)) |
4730 | { | |
4731 | /* Check file-coding-system-alist. */ | |
4732 | Lisp_Object args[7], coding_systems; | |
4733 | ||
4734 | args[0] = Qwrite_region; args[1] = start; args[2] = end; | |
4735 | args[3] = filename; args[4] = append; args[5] = visit; | |
4736 | args[6] = lockname; | |
4737 | coding_systems = Ffind_operation_coding_system (7, args); | |
4738 | if (CONSP (coding_systems) && !NILP (XCDR (coding_systems))) | |
4739 | val = XCDR (coding_systems); | |
4740 | } | |
4741 | ||
4742 | if (NILP (val) | |
4743 | && !NILP (current_buffer->buffer_file_coding_system)) | |
4744 | { | |
4745 | /* If we still have not decided a coding system, use the | |
4746 | default value of buffer-file-coding-system. */ | |
4747 | val = current_buffer->buffer_file_coding_system; | |
4748 | using_default_coding = 1; | |
4749 | } | |
efdc16c9 | 4750 | |
7c82a4a9 SM |
4751 | if (!force_raw_text |
4752 | && !NILP (Ffboundp (Vselect_safe_coding_system_function))) | |
4753 | /* Confirm that VAL can surely encode the current region. */ | |
905a4276 PJ |
4754 | val = call5 (Vselect_safe_coding_system_function, |
4755 | start, end, val, Qnil, filename); | |
7c82a4a9 SM |
4756 | |
4757 | setup_coding_system (Fcheck_coding_system (val), coding); | |
4758 | if (coding->eol_type == CODING_EOL_UNDECIDED | |
4759 | && !using_default_coding) | |
4760 | { | |
4761 | if (! EQ (default_buffer_file_coding.symbol, | |
4762 | buffer_defaults.buffer_file_coding_system)) | |
4763 | setup_coding_system (buffer_defaults.buffer_file_coding_system, | |
4764 | &default_buffer_file_coding); | |
4765 | if (default_buffer_file_coding.eol_type != CODING_EOL_UNDECIDED) | |
4766 | { | |
4767 | Lisp_Object subsidiaries; | |
4768 | ||
4769 | coding->eol_type = default_buffer_file_coding.eol_type; | |
4770 | subsidiaries = Fget (coding->symbol, Qeol_type); | |
4771 | if (VECTORP (subsidiaries) | |
4772 | && XVECTOR (subsidiaries)->size == 3) | |
4773 | coding->symbol | |
4774 | = XVECTOR (subsidiaries)->contents[coding->eol_type]; | |
4775 | } | |
4776 | } | |
4777 | ||
4778 | if (force_raw_text) | |
4779 | setup_raw_text_coding_system (coding); | |
4780 | goto done_setup_coding; | |
4781 | } | |
4782 | ||
4783 | setup_coding_system (Fcheck_coding_system (val), coding); | |
4784 | ||
4785 | done_setup_coding: | |
4786 | if (!STRINGP (start) && !NILP (current_buffer->selective_display)) | |
4787 | coding->mode |= CODING_MODE_SELECTIVE_DISPLAY; | |
4788 | } | |
4789 | ||
de1d0127 | 4790 | DEFUN ("write-region", Fwrite_region, Swrite_region, 3, 7, |
8c1a1077 PJ |
4791 | "r\nFWrite region to file: \ni\ni\ni\np", |
4792 | doc: /* Write current region into specified file. | |
c2efea25 RS |
4793 | When called from a program, requires three arguments: |
4794 | START, END and FILENAME. START and END are normally buffer positions | |
4795 | specifying the part of the buffer to write. | |
4796 | If START is nil, that means to use the entire buffer contents. | |
4797 | If START is a string, then output that string to the file | |
4798 | instead of any buffer contents; END is ignored. | |
4799 | ||
8c1a1077 PJ |
4800 | Optional fourth argument APPEND if non-nil means |
4801 | append to existing file contents (if any). If it is an integer, | |
4802 | seek to that offset in the file before writing. | |
4803 | Optional fifth argument VISIT if t means | |
4804 | set the last-save-file-modtime of buffer to this file's modtime | |
4805 | and mark buffer not modified. | |
4806 | If VISIT is a string, it is a second file name; | |
4807 | the output goes to FILENAME, but the buffer is marked as visiting VISIT. | |
4808 | VISIT is also the file name to lock and unlock for clash detection. | |
4809 | If VISIT is neither t nor nil nor a string, | |
5f4e6aa9 | 4810 | that means do not display the \"Wrote file\" message. |
8c1a1077 PJ |
4811 | The optional sixth arg LOCKNAME, if non-nil, specifies the name to |
4812 | use for locking and unlocking, overriding FILENAME and VISIT. | |
4813 | The optional seventh arg MUSTBENEW, if non-nil, insists on a check | |
4814 | for an existing file with the same name. If MUSTBENEW is `excl', | |
4815 | that means to get an error if the file already exists; never overwrite. | |
4816 | If MUSTBENEW is neither nil nor `excl', that means ask for | |
4817 | confirmation before overwriting, but do go ahead and overwrite the file | |
4818 | if the user confirms. | |
8c1a1077 PJ |
4819 | |
4820 | This does code conversion according to the value of | |
4821 | `coding-system-for-write', `buffer-file-coding-system', or | |
4822 | `file-coding-system-alist', and sets the variable | |
4823 | `last-coding-system-used' to the coding system actually used. */) | |
4824 | (start, end, filename, append, visit, lockname, mustbenew) | |
f7b4065f | 4825 | Lisp_Object start, end, filename, append, visit, lockname, mustbenew; |
570d7624 JB |
4826 | { |
4827 | register int desc; | |
4828 | int failure; | |
6bbd7a29 | 4829 | int save_errno = 0; |
19290c65 | 4830 | const unsigned char *fn; |
570d7624 | 4831 | struct stat st; |
c975dd7a | 4832 | int tem; |
aed13378 | 4833 | int count = SPECPDL_INDEX (); |
6fc6f94b | 4834 | int count1; |
570d7624 | 4835 | #ifdef VMS |
5e570b75 | 4836 | unsigned char *fname = 0; /* If non-0, original filename (must rename) */ |
570d7624 | 4837 | #endif /* VMS */ |
3eac9910 | 4838 | Lisp_Object handler; |
4ad827c5 | 4839 | Lisp_Object visit_file; |
65b7d3e7 | 4840 | Lisp_Object annotations; |
b1d1b865 | 4841 | Lisp_Object encoded_filename; |
d3a67486 SM |
4842 | int visiting = (EQ (visit, Qt) || STRINGP (visit)); |
4843 | int quietly = !NILP (visit); | |
7204a979 | 4844 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; |
6fc6f94b | 4845 | struct buffer *given_buffer; |
5e570b75 | 4846 | #ifdef DOS_NT |
fa228724 | 4847 | int buffer_file_type = O_BINARY; |
5e570b75 | 4848 | #endif /* DOS_NT */ |
6fdaa9a0 | 4849 | struct coding_system coding; |
570d7624 | 4850 | |
d3a67486 | 4851 | if (current_buffer->base_buffer && visiting) |
95385625 RS |
4852 | error ("Cannot do file visiting in an indirect buffer"); |
4853 | ||
561cb8e1 | 4854 | if (!NILP (start) && !STRINGP (start)) |
570d7624 JB |
4855 | validate_region (&start, &end); |
4856 | ||
59fac292 | 4857 | GCPRO5 (start, filename, visit, visit_file, lockname); |
b56567b5 | 4858 | |
570d7624 | 4859 | filename = Fexpand_file_name (filename, Qnil); |
de1d0127 | 4860 | |
236a12f2 | 4861 | if (!NILP (mustbenew) && !EQ (mustbenew, Qexcl)) |
b8b29dc9 | 4862 | barf_or_query_if_file_exists (filename, "overwrite", 1, 0, 1); |
de1d0127 | 4863 | |
561cb8e1 | 4864 | if (STRINGP (visit)) |
e5176bae | 4865 | visit_file = Fexpand_file_name (visit, Qnil); |
4ad827c5 RS |
4866 | else |
4867 | visit_file = filename; | |
4868 | ||
7204a979 RS |
4869 | if (NILP (lockname)) |
4870 | lockname = visit_file; | |
4871 | ||
65b7d3e7 RS |
4872 | annotations = Qnil; |
4873 | ||
32f4334d RS |
4874 | /* If the file name has special constructs in it, |
4875 | call the corresponding file handler. */ | |
49307295 | 4876 | handler = Ffind_file_name_handler (filename, Qwrite_region); |
b56ad927 | 4877 | /* If FILENAME has no handler, see if VISIT has one. */ |
93c30b5f | 4878 | if (NILP (handler) && STRINGP (visit)) |
199607e4 | 4879 | handler = Ffind_file_name_handler (visit, Qwrite_region); |
3eac9910 | 4880 | |
32f4334d RS |
4881 | if (!NILP (handler)) |
4882 | { | |
32f4334d | 4883 | Lisp_Object val; |
51cf6d37 RS |
4884 | val = call6 (handler, Qwrite_region, start, end, |
4885 | filename, append, visit); | |
32f4334d | 4886 | |
d6a3cc15 | 4887 | if (visiting) |
32f4334d | 4888 | { |
95385625 | 4889 | SAVE_MODIFF = MODIFF; |
2acfd7ae | 4890 | XSETFASTINT (current_buffer->save_length, Z - BEG); |
3b7792ed | 4891 | current_buffer->filename = visit_file; |
32f4334d | 4892 | } |
09121adc | 4893 | UNGCPRO; |
32f4334d RS |
4894 | return val; |
4895 | } | |
4896 | ||
4a38de71 KH |
4897 | record_unwind_protect (save_restriction_restore, save_restriction_save ()); |
4898 | ||
561cb8e1 RS |
4899 | /* Special kludge to simplify auto-saving. */ |
4900 | if (NILP (start)) | |
4901 | { | |
2acfd7ae KH |
4902 | XSETFASTINT (start, BEG); |
4903 | XSETFASTINT (end, Z); | |
4a38de71 | 4904 | Fwiden (); |
561cb8e1 RS |
4905 | } |
4906 | ||
6fc6f94b | 4907 | record_unwind_protect (build_annotations_unwind, Fcurrent_buffer ()); |
aed13378 | 4908 | count1 = SPECPDL_INDEX (); |
6fc6f94b RS |
4909 | |
4910 | given_buffer = current_buffer; | |
bf3428a1 RS |
4911 | |
4912 | if (!STRINGP (start)) | |
236a12f2 | 4913 | { |
bf3428a1 RS |
4914 | annotations = build_annotations (start, end); |
4915 | ||
4916 | if (current_buffer != given_buffer) | |
4917 | { | |
4918 | XSETFASTINT (start, BEGV); | |
4919 | XSETFASTINT (end, ZV); | |
4920 | } | |
236a12f2 SM |
4921 | } |
4922 | ||
4923 | UNGCPRO; | |
4924 | ||
4925 | GCPRO5 (start, filename, annotations, visit_file, lockname); | |
4926 | ||
59fac292 SM |
4927 | /* Decide the coding-system to encode the data with. |
4928 | We used to make this choice before calling build_annotations, but that | |
4929 | leads to problems when a write-annotate-function takes care of | |
4930 | unsavable chars (as was the case with X-Symbol). */ | |
4931 | choose_write_coding_system (start, end, filename, | |
4932 | append, visit, lockname, &coding); | |
4933 | Vlast_coding_system_used = coding.symbol; | |
4934 | ||
236a12f2 | 4935 | given_buffer = current_buffer; |
bf3428a1 | 4936 | if (! STRINGP (start)) |
6fc6f94b | 4937 | { |
bf3428a1 RS |
4938 | annotations = build_annotations_2 (start, end, |
4939 | coding.pre_write_conversion, annotations); | |
4940 | if (current_buffer != given_buffer) | |
4941 | { | |
4942 | XSETFASTINT (start, BEGV); | |
4943 | XSETFASTINT (end, ZV); | |
4944 | } | |
6fc6f94b | 4945 | } |
d6a3cc15 | 4946 | |
570d7624 JB |
4947 | #ifdef CLASH_DETECTION |
4948 | if (!auto_saving) | |
84f6296a | 4949 | { |
a9171faa | 4950 | #if 0 /* This causes trouble for GNUS. */ |
84f6296a RS |
4951 | /* If we've locked this file for some other buffer, |
4952 | query before proceeding. */ | |
4953 | if (!visiting && EQ (Ffile_locked_p (lockname), Qt)) | |
bffd00b0 | 4954 | call2 (intern ("ask-user-about-lock"), filename, Vuser_login_name); |
a9171faa | 4955 | #endif |
84f6296a RS |
4956 | |
4957 | lock_file (lockname); | |
4958 | } | |
570d7624 JB |
4959 | #endif /* CLASH_DETECTION */ |
4960 | ||
b1d1b865 RS |
4961 | encoded_filename = ENCODE_FILE (filename); |
4962 | ||
d5db4077 | 4963 | fn = SDATA (encoded_filename); |
570d7624 | 4964 | desc = -1; |
265a9e55 | 4965 | if (!NILP (append)) |
5e570b75 | 4966 | #ifdef DOS_NT |
68c45bf0 | 4967 | desc = emacs_open (fn, O_WRONLY | buffer_file_type, 0); |
5e570b75 | 4968 | #else /* not DOS_NT */ |
68c45bf0 | 4969 | desc = emacs_open (fn, O_WRONLY, 0); |
5e570b75 | 4970 | #endif /* not DOS_NT */ |
570d7624 | 4971 | |
b1d1b865 | 4972 | if (desc < 0 && (NILP (append) || errno == ENOENT)) |
570d7624 | 4973 | #ifdef VMS |
5e570b75 | 4974 | if (auto_saving) /* Overwrite any previous version of autosave file */ |
570d7624 | 4975 | { |
5e570b75 | 4976 | vms_truncate (fn); /* if fn exists, truncate to zero length */ |
68c45bf0 | 4977 | desc = emacs_open (fn, O_RDWR, 0); |
570d7624 | 4978 | if (desc < 0) |
561cb8e1 | 4979 | desc = creat_copy_attrs (STRINGP (current_buffer->filename) |
d5db4077 | 4980 | ? SDATA (current_buffer->filename) : 0, |
b72dea2a | 4981 | fn); |
570d7624 | 4982 | } |
5e570b75 | 4983 | else /* Write to temporary name and rename if no errors */ |
570d7624 JB |
4984 | { |
4985 | Lisp_Object temp_name; | |
4986 | temp_name = Ffile_name_directory (filename); | |
4987 | ||
265a9e55 | 4988 | if (!NILP (temp_name)) |
570d7624 JB |
4989 | { |
4990 | temp_name = Fmake_temp_name (concat2 (temp_name, | |
4991 | build_string ("$$SAVE$$"))); | |
d5db4077 KR |
4992 | fname = SDATA (filename); |
4993 | fn = SDATA (temp_name); | |
570d7624 JB |
4994 | desc = creat_copy_attrs (fname, fn); |
4995 | if (desc < 0) | |
4996 | { | |
4997 | /* If we can't open the temporary file, try creating a new | |
4998 | version of the original file. VMS "creat" creates a | |
4999 | new version rather than truncating an existing file. */ | |
5000 | fn = fname; | |
5001 | fname = 0; | |
5002 | desc = creat (fn, 0666); | |
5003 | #if 0 /* This can clobber an existing file and fail to replace it, | |
5004 | if the user runs out of space. */ | |
5005 | if (desc < 0) | |
5006 | { | |
5007 | /* We can't make a new version; | |
5008 | try to truncate and rewrite existing version if any. */ | |
5009 | vms_truncate (fn); | |
68c45bf0 | 5010 | desc = emacs_open (fn, O_RDWR, 0); |
570d7624 JB |
5011 | } |
5012 | #endif | |
5013 | } | |
5014 | } | |
5015 | else | |
5016 | desc = creat (fn, 0666); | |
5017 | } | |
5018 | #else /* not VMS */ | |
5e570b75 | 5019 | #ifdef DOS_NT |
68c45bf0 | 5020 | desc = emacs_open (fn, |
95522746 GM |
5021 | O_WRONLY | O_CREAT | buffer_file_type |
5022 | | (EQ (mustbenew, Qexcl) ? O_EXCL : O_TRUNC), | |
68c45bf0 | 5023 | S_IREAD | S_IWRITE); |
5e570b75 | 5024 | #else /* not DOS_NT */ |
68c45bf0 | 5025 | desc = emacs_open (fn, O_WRONLY | O_TRUNC | O_CREAT |
7c752c80 | 5026 | | (EQ (mustbenew, Qexcl) ? O_EXCL : 0), |
68c45bf0 | 5027 | auto_saving ? auto_save_mode_bits : 0666); |
5e570b75 | 5028 | #endif /* not DOS_NT */ |
570d7624 JB |
5029 | #endif /* not VMS */ |
5030 | ||
5031 | if (desc < 0) | |
5032 | { | |
5033 | #ifdef CLASH_DETECTION | |
5034 | save_errno = errno; | |
7204a979 | 5035 | if (!auto_saving) unlock_file (lockname); |
570d7624 JB |
5036 | errno = save_errno; |
5037 | #endif /* CLASH_DETECTION */ | |
43fb7d9a | 5038 | UNGCPRO; |
570d7624 JB |
5039 | report_file_error ("Opening output file", Fcons (filename, Qnil)); |
5040 | } | |
5041 | ||
5042 | record_unwind_protect (close_file_unwind, make_number (desc)); | |
5043 | ||
c1c4693e | 5044 | if (!NILP (append) && !NILP (Ffile_regular_p (filename))) |
43fb7d9a DL |
5045 | { |
5046 | long ret; | |
efdc16c9 | 5047 | |
43fb7d9a DL |
5048 | if (NUMBERP (append)) |
5049 | ret = lseek (desc, XINT (append), 1); | |
5050 | else | |
5051 | ret = lseek (desc, 0, 2); | |
5052 | if (ret < 0) | |
5053 | { | |
570d7624 | 5054 | #ifdef CLASH_DETECTION |
43fb7d9a | 5055 | if (!auto_saving) unlock_file (lockname); |
570d7624 | 5056 | #endif /* CLASH_DETECTION */ |
43fb7d9a DL |
5057 | UNGCPRO; |
5058 | report_file_error ("Lseek error", Fcons (filename, Qnil)); | |
5059 | } | |
5060 | } | |
efdc16c9 | 5061 | |
43fb7d9a | 5062 | UNGCPRO; |
570d7624 JB |
5063 | |
5064 | #ifdef VMS | |
5065 | /* | |
5066 | * Kludge Warning: The VMS C RTL likes to insert carriage returns | |
5067 | * if we do writes that don't end with a carriage return. Furthermore | |
5068 | * it cannot handle writes of more then 16K. The modified | |
5069 | * version of "sys_write" in SYSDEP.C (see comment there) copes with | |
5070 | * this EXCEPT for the last record (iff it doesn't end with a carriage | |
5071 | * return). This implies that if your buffer doesn't end with a carriage | |
5072 | * return, you get one free... tough. However it also means that if | |
5073 | * we make two calls to sys_write (a la the following code) you can | |
5074 | * get one at the gap as well. The easiest way to fix this (honest) | |
5075 | * is to move the gap to the next newline (or the end of the buffer). | |
5076 | * Thus this change. | |
5077 | * | |
5078 | * Yech! | |
5079 | */ | |
5080 | if (GPT > BEG && GPT_ADDR[-1] != '\n') | |
5081 | move_gap (find_next_newline (GPT, 1)); | |
cdfb0f1d KH |
5082 | #else |
5083 | /* Whether VMS or not, we must move the gap to the next of newline | |
5084 | when we must put designation sequences at beginning of line. */ | |
5085 | if (INTEGERP (start) | |
5086 | && coding.type == coding_type_iso2022 | |
5087 | && coding.flags & CODING_FLAG_ISO_DESIGNATE_AT_BOL | |
5088 | && GPT > BEG && GPT_ADDR[-1] != '\n') | |
ec7adf26 RS |
5089 | { |
5090 | int opoint = PT, opoint_byte = PT_BYTE; | |
5091 | scan_newline (PT, PT_BYTE, ZV, ZV_BYTE, 1, 0); | |
5092 | move_gap_both (PT, PT_BYTE); | |
5093 | SET_PT_BOTH (opoint, opoint_byte); | |
5094 | } | |
570d7624 JB |
5095 | #endif |
5096 | ||
5097 | failure = 0; | |
5098 | immediate_quit = 1; | |
5099 | ||
561cb8e1 | 5100 | if (STRINGP (start)) |
570d7624 | 5101 | { |
d5db4077 | 5102 | failure = 0 > a_write (desc, start, 0, SCHARS (start), |
ce51c54c | 5103 | &annotations, &coding); |
570d7624 JB |
5104 | save_errno = errno; |
5105 | } | |
5106 | else if (XINT (start) != XINT (end)) | |
5107 | { | |
ec7adf26 RS |
5108 | tem = CHAR_TO_BYTE (XINT (start)); |
5109 | ||
570d7624 JB |
5110 | if (XINT (start) < GPT) |
5111 | { | |
ce51c54c KH |
5112 | failure = 0 > a_write (desc, Qnil, XINT (start), |
5113 | min (GPT, XINT (end)) - XINT (start), | |
5114 | &annotations, &coding); | |
570d7624 JB |
5115 | save_errno = errno; |
5116 | } | |
5117 | ||
5118 | if (XINT (end) > GPT && !failure) | |
5119 | { | |
ce51c54c KH |
5120 | tem = max (XINT (start), GPT); |
5121 | failure = 0 > a_write (desc, Qnil, tem , XINT (end) - tem, | |
5122 | &annotations, &coding); | |
d6a3cc15 RS |
5123 | save_errno = errno; |
5124 | } | |
69f6e679 RS |
5125 | } |
5126 | else | |
5127 | { | |
5128 | /* If file was empty, still need to write the annotations */ | |
c8a6d68a | 5129 | coding.mode |= CODING_MODE_LAST_BLOCK; |
ce51c54c | 5130 | failure = 0 > a_write (desc, Qnil, XINT (end), 0, &annotations, &coding); |
6fdaa9a0 KH |
5131 | save_errno = errno; |
5132 | } | |
5133 | ||
c8a6d68a KH |
5134 | if (CODING_REQUIRE_FLUSHING (&coding) |
5135 | && !(coding.mode & CODING_MODE_LAST_BLOCK) | |
1354debd | 5136 | && ! failure) |
6fdaa9a0 KH |
5137 | { |
5138 | /* We have to flush out a data. */ | |
c8a6d68a | 5139 | coding.mode |= CODING_MODE_LAST_BLOCK; |
ce51c54c | 5140 | failure = 0 > e_write (desc, Qnil, 0, 0, &coding); |
69f6e679 | 5141 | save_errno = errno; |
570d7624 JB |
5142 | } |
5143 | ||
5144 | immediate_quit = 0; | |
5145 | ||
6e23c83e | 5146 | #ifdef HAVE_FSYNC |
570d7624 JB |
5147 | /* Note fsync appears to change the modtime on BSD4.2 (both vax and sun). |
5148 | Disk full in NFS may be reported here. */ | |
1daffa1c RS |
5149 | /* mib says that closing the file will try to write as fast as NFS can do |
5150 | it, and that means the fsync here is not crucial for autosave files. */ | |
5151 | if (!auto_saving && fsync (desc) < 0) | |
cb33c142 KH |
5152 | { |
5153 | /* If fsync fails with EINTR, don't treat that as serious. */ | |
5154 | if (errno != EINTR) | |
5155 | failure = 1, save_errno = errno; | |
5156 | } | |
570d7624 JB |
5157 | #endif |
5158 | ||
199607e4 | 5159 | /* Spurious "file has changed on disk" warnings have been |
570d7624 JB |
5160 | observed on Suns as well. |
5161 | It seems that `close' can change the modtime, under nfs. | |
5162 | ||
5163 | (This has supposedly been fixed in Sunos 4, | |
5164 | but who knows about all the other machines with NFS?) */ | |
5165 | #if 0 | |
5166 | ||
5167 | /* On VMS and APOLLO, must do the stat after the close | |
5168 | since closing changes the modtime. */ | |
5169 | #ifndef VMS | |
5170 | #ifndef APOLLO | |
5171 | /* Recall that #if defined does not work on VMS. */ | |
5172 | #define FOO | |
5173 | fstat (desc, &st); | |
5174 | #endif | |
5175 | #endif | |
5176 | #endif | |
5177 | ||
5178 | /* NFS can report a write failure now. */ | |
68c45bf0 | 5179 | if (emacs_close (desc) < 0) |
570d7624 JB |
5180 | failure = 1, save_errno = errno; |
5181 | ||
5182 | #ifdef VMS | |
5183 | /* If we wrote to a temporary name and had no errors, rename to real name. */ | |
5184 | if (fname) | |
5185 | { | |
5186 | if (!failure) | |
5187 | failure = (rename (fn, fname) != 0), save_errno = errno; | |
5188 | fn = fname; | |
5189 | } | |
5190 | #endif /* VMS */ | |
5191 | ||
5192 | #ifndef FOO | |
5193 | stat (fn, &st); | |
5194 | #endif | |
6fc6f94b RS |
5195 | /* Discard the unwind protect for close_file_unwind. */ |
5196 | specpdl_ptr = specpdl + count1; | |
5197 | /* Restore the original current buffer. */ | |
98295b48 | 5198 | visit_file = unbind_to (count, visit_file); |
570d7624 JB |
5199 | |
5200 | #ifdef CLASH_DETECTION | |
5201 | if (!auto_saving) | |
7204a979 | 5202 | unlock_file (lockname); |
570d7624 JB |
5203 | #endif /* CLASH_DETECTION */ |
5204 | ||
5205 | /* Do this before reporting IO error | |
5206 | to avoid a "file has changed on disk" warning on | |
5207 | next attempt to save. */ | |
d6a3cc15 | 5208 | if (visiting) |
570d7624 JB |
5209 | current_buffer->modtime = st.st_mtime; |
5210 | ||
5211 | if (failure) | |
d5db4077 | 5212 | error ("IO error writing %s: %s", SDATA (filename), |
68c45bf0 | 5213 | emacs_strerror (save_errno)); |
570d7624 | 5214 | |
d6a3cc15 | 5215 | if (visiting) |
570d7624 | 5216 | { |
95385625 | 5217 | SAVE_MODIFF = MODIFF; |
2acfd7ae | 5218 | XSETFASTINT (current_buffer->save_length, Z - BEG); |
3b7792ed | 5219 | current_buffer->filename = visit_file; |
f4226e89 | 5220 | update_mode_lines++; |
570d7624 | 5221 | } |
d6a3cc15 | 5222 | else if (quietly) |
570d7624 JB |
5223 | return Qnil; |
5224 | ||
5225 | if (!auto_saving) | |
463f5630 | 5226 | message_with_string ((! INTEGERP (append) |
0c328a0e RS |
5227 | ? "Updated %s" |
5228 | : ! NILP (append) | |
5229 | ? "Added to %s" | |
5230 | : "Wrote %s"), | |
5231 | visit_file, 1); | |
570d7624 JB |
5232 | |
5233 | return Qnil; | |
5234 | } | |
ec7adf26 | 5235 | \f |
d6a3cc15 RS |
5236 | Lisp_Object merge (); |
5237 | ||
5238 | DEFUN ("car-less-than-car", Fcar_less_than_car, Scar_less_than_car, 2, 2, 0, | |
8c1a1077 PJ |
5239 | doc: /* Return t if (car A) is numerically less than (car B). */) |
5240 | (a, b) | |
d6a3cc15 RS |
5241 | Lisp_Object a, b; |
5242 | { | |
5243 | return Flss (Fcar (a), Fcar (b)); | |
5244 | } | |
5245 | ||
5246 | /* Build the complete list of annotations appropriate for writing out | |
5247 | the text between START and END, by calling all the functions in | |
6fc6f94b RS |
5248 | write-region-annotate-functions and merging the lists they return. |
5249 | If one of these functions switches to a different buffer, we assume | |
5250 | that buffer contains altered text. Therefore, the caller must | |
5251 | make sure to restore the current buffer in all cases, | |
5252 | as save-excursion would do. */ | |
d6a3cc15 RS |
5253 | |
5254 | static Lisp_Object | |
236a12f2 SM |
5255 | build_annotations (start, end) |
5256 | Lisp_Object start, end; | |
d6a3cc15 RS |
5257 | { |
5258 | Lisp_Object annotations; | |
5259 | Lisp_Object p, res; | |
5260 | struct gcpro gcpro1, gcpro2; | |
0a20b684 | 5261 | Lisp_Object original_buffer; |
bd235610 | 5262 | int i, used_global = 0; |
0a20b684 RS |
5263 | |
5264 | XSETBUFFER (original_buffer, current_buffer); | |
d6a3cc15 RS |
5265 | |
5266 | annotations = Qnil; | |
5267 | p = Vwrite_region_annotate_functions; | |
5268 | GCPRO2 (annotations, p); | |
28c3eb5a | 5269 | while (CONSP (p)) |
d6a3cc15 | 5270 | { |
6fc6f94b | 5271 | struct buffer *given_buffer = current_buffer; |
bd235610 SM |
5272 | if (EQ (Qt, XCAR (p)) && !used_global) |
5273 | { /* Use the global value of the hook. */ | |
5274 | Lisp_Object arg[2]; | |
5275 | used_global = 1; | |
5276 | arg[0] = Fdefault_value (Qwrite_region_annotate_functions); | |
5277 | arg[1] = XCDR (p); | |
5278 | p = Fappend (2, arg); | |
5279 | continue; | |
5280 | } | |
6fc6f94b | 5281 | Vwrite_region_annotations_so_far = annotations; |
28c3eb5a | 5282 | res = call2 (XCAR (p), start, end); |
6fc6f94b RS |
5283 | /* If the function makes a different buffer current, |
5284 | assume that means this buffer contains altered text to be output. | |
5285 | Reset START and END from the buffer bounds | |
5286 | and discard all previous annotations because they should have | |
5287 | been dealt with by this function. */ | |
5288 | if (current_buffer != given_buffer) | |
5289 | { | |
3cf29f61 RS |
5290 | XSETFASTINT (start, BEGV); |
5291 | XSETFASTINT (end, ZV); | |
6fc6f94b RS |
5292 | annotations = Qnil; |
5293 | } | |
d6a3cc15 RS |
5294 | Flength (res); /* Check basic validity of return value */ |
5295 | annotations = merge (annotations, res, Qcar_less_than_car); | |
28c3eb5a | 5296 | p = XCDR (p); |
d6a3cc15 | 5297 | } |
0d420e88 BG |
5298 | |
5299 | /* Now do the same for annotation functions implied by the file-format */ | |
5300 | if (auto_saving && (!EQ (Vauto_save_file_format, Qt))) | |
5301 | p = Vauto_save_file_format; | |
5302 | else | |
5303 | p = current_buffer->file_format; | |
28c3eb5a | 5304 | for (i = 0; CONSP (p); p = XCDR (p), ++i) |
0d420e88 BG |
5305 | { |
5306 | struct buffer *given_buffer = current_buffer; | |
efdc16c9 | 5307 | |
0d420e88 | 5308 | Vwrite_region_annotations_so_far = annotations; |
532ed661 GM |
5309 | |
5310 | /* Value is either a list of annotations or nil if the function | |
5311 | has written annotations to a temporary buffer, which is now | |
5312 | current. */ | |
28c3eb5a | 5313 | res = call5 (Qformat_annotate_function, XCAR (p), start, end, |
532ed661 | 5314 | original_buffer, make_number (i)); |
0d420e88 BG |
5315 | if (current_buffer != given_buffer) |
5316 | { | |
3cf29f61 RS |
5317 | XSETFASTINT (start, BEGV); |
5318 | XSETFASTINT (end, ZV); | |
0d420e88 BG |
5319 | annotations = Qnil; |
5320 | } | |
efdc16c9 | 5321 | |
532ed661 GM |
5322 | if (CONSP (res)) |
5323 | annotations = merge (annotations, res, Qcar_less_than_car); | |
0d420e88 | 5324 | } |
6fdaa9a0 | 5325 | |
236a12f2 SM |
5326 | UNGCPRO; |
5327 | return annotations; | |
5328 | } | |
5329 | ||
5330 | static Lisp_Object | |
5331 | build_annotations_2 (start, end, pre_write_conversion, annotations) | |
5332 | Lisp_Object start, end, pre_write_conversion, annotations; | |
5333 | { | |
5334 | struct gcpro gcpro1; | |
5335 | Lisp_Object res; | |
5336 | ||
5337 | GCPRO1 (annotations); | |
6fdaa9a0 KH |
5338 | /* At last, do the same for the function PRE_WRITE_CONVERSION |
5339 | implied by the current coding-system. */ | |
5340 | if (!NILP (pre_write_conversion)) | |
5341 | { | |
5342 | struct buffer *given_buffer = current_buffer; | |
5343 | Vwrite_region_annotations_so_far = annotations; | |
5344 | res = call2 (pre_write_conversion, start, end); | |
6fdaa9a0 | 5345 | Flength (res); |
cdfb0f1d KH |
5346 | annotations = (current_buffer != given_buffer |
5347 | ? res | |
5348 | : merge (annotations, res, Qcar_less_than_car)); | |
6fdaa9a0 KH |
5349 | } |
5350 | ||
d6a3cc15 RS |
5351 | UNGCPRO; |
5352 | return annotations; | |
5353 | } | |
ec7adf26 | 5354 | \f |
ce51c54c KH |
5355 | /* Write to descriptor DESC the NCHARS chars starting at POS of STRING. |
5356 | If STRING is nil, POS is the character position in the current buffer. | |
d6a3cc15 | 5357 | Intersperse with them the annotations from *ANNOT |
ce51c54c | 5358 | which fall within the range of POS to POS + NCHARS, |
d6a3cc15 RS |
5359 | each at its appropriate position. |
5360 | ||
ec7adf26 RS |
5361 | We modify *ANNOT by discarding elements as we use them up. |
5362 | ||
d6a3cc15 RS |
5363 | The return value is negative in case of system call failure. */ |
5364 | ||
ec7adf26 | 5365 | static int |
ce51c54c | 5366 | a_write (desc, string, pos, nchars, annot, coding) |
d6a3cc15 | 5367 | int desc; |
ce51c54c KH |
5368 | Lisp_Object string; |
5369 | register int nchars; | |
5370 | int pos; | |
d6a3cc15 | 5371 | Lisp_Object *annot; |
6fdaa9a0 | 5372 | struct coding_system *coding; |
d6a3cc15 RS |
5373 | { |
5374 | Lisp_Object tem; | |
5375 | int nextpos; | |
ce51c54c | 5376 | int lastpos = pos + nchars; |
d6a3cc15 | 5377 | |
eb15aa18 | 5378 | while (NILP (*annot) || CONSP (*annot)) |
d6a3cc15 RS |
5379 | { |
5380 | tem = Fcar_safe (Fcar (*annot)); | |
ce51c54c | 5381 | nextpos = pos - 1; |
ec7adf26 | 5382 | if (INTEGERP (tem)) |
ce51c54c | 5383 | nextpos = XFASTINT (tem); |
ec7adf26 RS |
5384 | |
5385 | /* If there are no more annotations in this range, | |
5386 | output the rest of the range all at once. */ | |
ce51c54c KH |
5387 | if (! (nextpos >= pos && nextpos <= lastpos)) |
5388 | return e_write (desc, string, pos, lastpos, coding); | |
ec7adf26 RS |
5389 | |
5390 | /* Output buffer text up to the next annotation's position. */ | |
ce51c54c | 5391 | if (nextpos > pos) |
d6a3cc15 | 5392 | { |
055a28c9 | 5393 | if (0 > e_write (desc, string, pos, nextpos, coding)) |
d6a3cc15 | 5394 | return -1; |
ce51c54c | 5395 | pos = nextpos; |
d6a3cc15 | 5396 | } |
ec7adf26 | 5397 | /* Output the annotation. */ |
d6a3cc15 RS |
5398 | tem = Fcdr (Fcar (*annot)); |
5399 | if (STRINGP (tem)) | |
5400 | { | |
d5db4077 | 5401 | if (0 > e_write (desc, tem, 0, SCHARS (tem), coding)) |
d6a3cc15 RS |
5402 | return -1; |
5403 | } | |
5404 | *annot = Fcdr (*annot); | |
5405 | } | |
dfcf069d | 5406 | return 0; |
d6a3cc15 RS |
5407 | } |
5408 | ||
6fdaa9a0 KH |
5409 | #ifndef WRITE_BUF_SIZE |
5410 | #define WRITE_BUF_SIZE (16 * 1024) | |
5411 | #endif | |
5412 | ||
ce51c54c KH |
5413 | /* Write text in the range START and END into descriptor DESC, |
5414 | encoding them with coding system CODING. If STRING is nil, START | |
5415 | and END are character positions of the current buffer, else they | |
5416 | are indexes to the string STRING. */ | |
ec7adf26 RS |
5417 | |
5418 | static int | |
ce51c54c | 5419 | e_write (desc, string, start, end, coding) |
570d7624 | 5420 | int desc; |
ce51c54c KH |
5421 | Lisp_Object string; |
5422 | int start, end; | |
6fdaa9a0 | 5423 | struct coding_system *coding; |
570d7624 | 5424 | { |
ce51c54c KH |
5425 | register char *addr; |
5426 | register int nbytes; | |
6fdaa9a0 | 5427 | char buf[WRITE_BUF_SIZE]; |
ce51c54c KH |
5428 | int return_val = 0; |
5429 | ||
5430 | if (start >= end) | |
5431 | coding->composing = COMPOSITION_DISABLED; | |
5432 | if (coding->composing != COMPOSITION_DISABLED) | |
5433 | coding_save_composition (coding, start, end, string); | |
5434 | ||
5435 | if (STRINGP (string)) | |
5436 | { | |
d5db4077 KR |
5437 | addr = SDATA (string); |
5438 | nbytes = SBYTES (string); | |
8c3b9441 | 5439 | coding->src_multibyte = STRING_MULTIBYTE (string); |
ce51c54c KH |
5440 | } |
5441 | else if (start < end) | |
5442 | { | |
5443 | /* It is assured that the gap is not in the range START and END-1. */ | |
5444 | addr = CHAR_POS_ADDR (start); | |
5445 | nbytes = CHAR_TO_BYTE (end) - CHAR_TO_BYTE (start); | |
8c3b9441 KH |
5446 | coding->src_multibyte |
5447 | = !NILP (current_buffer->enable_multibyte_characters); | |
ce51c54c KH |
5448 | } |
5449 | else | |
5450 | { | |
5451 | addr = ""; | |
5452 | nbytes = 0; | |
8c3b9441 | 5453 | coding->src_multibyte = 1; |
ce51c54c | 5454 | } |
570d7624 | 5455 | |
6fdaa9a0 KH |
5456 | /* We used to have a code for handling selective display here. But, |
5457 | now it is handled within encode_coding. */ | |
5458 | while (1) | |
570d7624 | 5459 | { |
b4132433 KH |
5460 | int result; |
5461 | ||
5462 | result = encode_coding (coding, addr, buf, nbytes, WRITE_BUF_SIZE); | |
c8a6d68a | 5463 | if (coding->produced > 0) |
6fdaa9a0 | 5464 | { |
68c45bf0 | 5465 | coding->produced -= emacs_write (desc, buf, coding->produced); |
ce51c54c KH |
5466 | if (coding->produced) |
5467 | { | |
5468 | return_val = -1; | |
5469 | break; | |
5470 | } | |
570d7624 | 5471 | } |
ca91fb26 KH |
5472 | nbytes -= coding->consumed; |
5473 | addr += coding->consumed; | |
5474 | if (result == CODING_FINISH_INSUFFICIENT_SRC | |
5475 | && nbytes > 0) | |
b4132433 KH |
5476 | { |
5477 | /* The source text ends by an incomplete multibyte form. | |
5478 | There's no way other than write it out as is. */ | |
68c45bf0 | 5479 | nbytes -= emacs_write (desc, addr, nbytes); |
ce51c54c KH |
5480 | if (nbytes) |
5481 | { | |
5482 | return_val = -1; | |
5483 | break; | |
5484 | } | |
b4132433 | 5485 | } |
ec7adf26 | 5486 | if (nbytes <= 0) |
6fdaa9a0 | 5487 | break; |
ce51c54c KH |
5488 | start += coding->consumed_char; |
5489 | if (coding->cmp_data) | |
5490 | coding_adjust_composition_offset (coding, start); | |
570d7624 | 5491 | } |
0c41a39c KH |
5492 | |
5493 | if (coding->cmp_data) | |
5494 | coding_free_composition_data (coding); | |
5495 | ||
055a28c9 | 5496 | return return_val; |
570d7624 | 5497 | } |
ec7adf26 | 5498 | \f |
570d7624 | 5499 | DEFUN ("verify-visited-file-modtime", Fverify_visited_file_modtime, |
8c1a1077 PJ |
5500 | Sverify_visited_file_modtime, 1, 1, 0, |
5501 | doc: /* Return t if last mod time of BUF's visited file matches what BUF records. | |
5502 | This means that the file has not been changed since it was visited or saved. */) | |
5503 | (buf) | |
570d7624 JB |
5504 | Lisp_Object buf; |
5505 | { | |
5506 | struct buffer *b; | |
5507 | struct stat st; | |
32f4334d | 5508 | Lisp_Object handler; |
b1d1b865 | 5509 | Lisp_Object filename; |
570d7624 | 5510 | |
b7826503 | 5511 | CHECK_BUFFER (buf); |
570d7624 JB |
5512 | b = XBUFFER (buf); |
5513 | ||
93c30b5f | 5514 | if (!STRINGP (b->filename)) return Qt; |
570d7624 JB |
5515 | if (b->modtime == 0) return Qt; |
5516 | ||
32f4334d RS |
5517 | /* If the file name has special constructs in it, |
5518 | call the corresponding file handler. */ | |
49307295 KH |
5519 | handler = Ffind_file_name_handler (b->filename, |
5520 | Qverify_visited_file_modtime); | |
32f4334d | 5521 | if (!NILP (handler)) |
09121adc | 5522 | return call2 (handler, Qverify_visited_file_modtime, buf); |
32f4334d | 5523 | |
b1d1b865 RS |
5524 | filename = ENCODE_FILE (b->filename); |
5525 | ||
d5db4077 | 5526 | if (stat (SDATA (filename), &st) < 0) |
570d7624 JB |
5527 | { |
5528 | /* If the file doesn't exist now and didn't exist before, | |
5529 | we say that it isn't modified, provided the error is a tame one. */ | |
5530 | if (errno == ENOENT || errno == EACCES || errno == ENOTDIR) | |
5531 | st.st_mtime = -1; | |
5532 | else | |
5533 | st.st_mtime = 0; | |
5534 | } | |
5535 | if (st.st_mtime == b->modtime | |
5536 | /* If both are positive, accept them if they are off by one second. */ | |
5537 | || (st.st_mtime > 0 && b->modtime > 0 | |
5538 | && (st.st_mtime == b->modtime + 1 | |
5539 | || st.st_mtime == b->modtime - 1))) | |
5540 | return Qt; | |
5541 | return Qnil; | |
5542 | } | |
5543 | ||
5544 | DEFUN ("clear-visited-file-modtime", Fclear_visited_file_modtime, | |
8c1a1077 PJ |
5545 | Sclear_visited_file_modtime, 0, 0, 0, |
5546 | doc: /* Clear out records of last mod time of visited file. | |
5547 | Next attempt to save will certainly not complain of a discrepancy. */) | |
5548 | () | |
570d7624 JB |
5549 | { |
5550 | current_buffer->modtime = 0; | |
5551 | return Qnil; | |
5552 | } | |
5553 | ||
f5d5eccf | 5554 | DEFUN ("visited-file-modtime", Fvisited_file_modtime, |
8c1a1077 PJ |
5555 | Svisited_file_modtime, 0, 0, 0, |
5556 | doc: /* Return the current buffer's recorded visited file modification time. | |
5557 | The value is a list of the form (HIGH . LOW), like the time values | |
5558 | that `file-attributes' returns. */) | |
5559 | () | |
f5d5eccf | 5560 | { |
b50536bb | 5561 | return long_to_cons ((unsigned long) current_buffer->modtime); |
f5d5eccf RS |
5562 | } |
5563 | ||
570d7624 | 5564 | DEFUN ("set-visited-file-modtime", Fset_visited_file_modtime, |
8c1a1077 PJ |
5565 | Sset_visited_file_modtime, 0, 1, 0, |
5566 | doc: /* Update buffer's recorded modification time from the visited file's time. | |
5567 | Useful if the buffer was not read from the file normally | |
5568 | or if the file itself has been changed for some known benign reason. | |
5569 | An argument specifies the modification time value to use | |
5570 | \(instead of that of the visited file), in the form of a list | |
5571 | \(HIGH . LOW) or (HIGH LOW). */) | |
5572 | (time_list) | |
f5d5eccf | 5573 | Lisp_Object time_list; |
570d7624 | 5574 | { |
f5d5eccf RS |
5575 | if (!NILP (time_list)) |
5576 | current_buffer->modtime = cons_to_long (time_list); | |
5577 | else | |
5578 | { | |
5579 | register Lisp_Object filename; | |
5580 | struct stat st; | |
5581 | Lisp_Object handler; | |
570d7624 | 5582 | |
f5d5eccf | 5583 | filename = Fexpand_file_name (current_buffer->filename, Qnil); |
32f4334d | 5584 | |
f5d5eccf RS |
5585 | /* If the file name has special constructs in it, |
5586 | call the corresponding file handler. */ | |
49307295 | 5587 | handler = Ffind_file_name_handler (filename, Qset_visited_file_modtime); |
f5d5eccf | 5588 | if (!NILP (handler)) |
caf3c431 | 5589 | /* The handler can find the file name the same way we did. */ |
76c881b0 | 5590 | return call2 (handler, Qset_visited_file_modtime, Qnil); |
b1d1b865 RS |
5591 | |
5592 | filename = ENCODE_FILE (filename); | |
5593 | ||
d5db4077 | 5594 | if (stat (SDATA (filename), &st) >= 0) |
f5d5eccf RS |
5595 | current_buffer->modtime = st.st_mtime; |
5596 | } | |
570d7624 JB |
5597 | |
5598 | return Qnil; | |
5599 | } | |
5600 | \f | |
5601 | Lisp_Object | |
d7f31e22 GM |
5602 | auto_save_error (error) |
5603 | Lisp_Object error; | |
570d7624 | 5604 | { |
d7f31e22 GM |
5605 | Lisp_Object args[3], msg; |
5606 | int i, nbytes; | |
5607 | struct gcpro gcpro1; | |
efdc16c9 | 5608 | |
570d7624 | 5609 | ring_bell (); |
efdc16c9 | 5610 | |
d7f31e22 GM |
5611 | args[0] = build_string ("Auto-saving %s: %s"); |
5612 | args[1] = current_buffer->name; | |
5613 | args[2] = Ferror_message_string (error); | |
5614 | msg = Fformat (3, args); | |
5615 | GCPRO1 (msg); | |
d5db4077 | 5616 | nbytes = SBYTES (msg); |
d7f31e22 GM |
5617 | |
5618 | for (i = 0; i < 3; ++i) | |
5619 | { | |
5620 | if (i == 0) | |
d5db4077 | 5621 | message2 (SDATA (msg), nbytes, STRING_MULTIBYTE (msg)); |
d7f31e22 | 5622 | else |
d5db4077 | 5623 | message2_nolog (SDATA (msg), nbytes, STRING_MULTIBYTE (msg)); |
d7f31e22 GM |
5624 | Fsleep_for (make_number (1), Qnil); |
5625 | } | |
5626 | ||
5627 | UNGCPRO; | |
570d7624 JB |
5628 | return Qnil; |
5629 | } | |
5630 | ||
5631 | Lisp_Object | |
5632 | auto_save_1 () | |
5633 | { | |
570d7624 JB |
5634 | struct stat st; |
5635 | ||
5636 | /* Get visited file's mode to become the auto save file's mode. */ | |
8801a864 | 5637 | if (! NILP (current_buffer->filename) |
d5db4077 | 5638 | && stat (SDATA (current_buffer->filename), &st) >= 0) |
570d7624 JB |
5639 | /* But make sure we can overwrite it later! */ |
5640 | auto_save_mode_bits = st.st_mode | 0600; | |
5641 | else | |
5642 | auto_save_mode_bits = 0666; | |
5643 | ||
5644 | return | |
5645 | Fwrite_region (Qnil, Qnil, | |
5646 | current_buffer->auto_save_file_name, | |
de1d0127 | 5647 | Qnil, Qlambda, Qnil, Qnil); |
570d7624 JB |
5648 | } |
5649 | ||
e54d3b5d | 5650 | static Lisp_Object |
1b335d29 RS |
5651 | do_auto_save_unwind (stream) /* used as unwind-protect function */ |
5652 | Lisp_Object stream; | |
e54d3b5d | 5653 | { |
3be3c08e | 5654 | auto_saving = 0; |
1b335d29 | 5655 | if (!NILP (stream)) |
03699b14 KR |
5656 | fclose ((FILE *) (XFASTINT (XCAR (stream)) << 16 |
5657 | | XFASTINT (XCDR (stream)))); | |
e54d3b5d RS |
5658 | return Qnil; |
5659 | } | |
5660 | ||
a8c828be RS |
5661 | static Lisp_Object |
5662 | do_auto_save_unwind_1 (value) /* used as unwind-protect function */ | |
5663 | Lisp_Object value; | |
5664 | { | |
5665 | minibuffer_auto_raise = XINT (value); | |
5666 | return Qnil; | |
5667 | } | |
5668 | ||
5794dd61 RS |
5669 | static Lisp_Object |
5670 | do_auto_save_make_dir (dir) | |
5671 | Lisp_Object dir; | |
5672 | { | |
5673 | return call2 (Qmake_directory, dir, Qt); | |
5674 | } | |
5675 | ||
5676 | static Lisp_Object | |
5677 | do_auto_save_eh (ignore) | |
5678 | Lisp_Object ignore; | |
5679 | { | |
5680 | return Qnil; | |
5681 | } | |
5682 | ||
570d7624 | 5683 | DEFUN ("do-auto-save", Fdo_auto_save, Sdo_auto_save, 0, 2, "", |
8c1a1077 PJ |
5684 | doc: /* Auto-save all buffers that need it. |
5685 | This is all buffers that have auto-saving enabled | |
5686 | and are changed since last auto-saved. | |
5687 | Auto-saving writes the buffer into a file | |
5688 | so that your editing is not lost if the system crashes. | |
5689 | This file is not the file you visited; that changes only when you save. | |
5690 | Normally we run the normal hook `auto-save-hook' before saving. | |
5691 | ||
5692 | A non-nil NO-MESSAGE argument means do not print any message if successful. | |
5693 | A non-nil CURRENT-ONLY argument means save only current buffer. */) | |
5694 | (no_message, current_only) | |
17857782 | 5695 | Lisp_Object no_message, current_only; |
570d7624 JB |
5696 | { |
5697 | struct buffer *old = current_buffer, *b; | |
5698 | Lisp_Object tail, buf; | |
5699 | int auto_saved = 0; | |
f14b1c68 | 5700 | int do_handled_files; |
ff4c9993 | 5701 | Lisp_Object oquit; |
1b335d29 RS |
5702 | FILE *stream; |
5703 | Lisp_Object lispstream; | |
aed13378 | 5704 | int count = SPECPDL_INDEX (); |
a8c828be | 5705 | int orig_minibuffer_auto_raise = minibuffer_auto_raise; |
5794dd61 | 5706 | int old_message_p = 0; |
d57563b6 | 5707 | struct gcpro gcpro1, gcpro2; |
38da540d RS |
5708 | |
5709 | if (max_specpdl_size < specpdl_size + 40) | |
5710 | max_specpdl_size = specpdl_size + 40; | |
5711 | ||
5712 | if (minibuf_level) | |
5713 | no_message = Qt; | |
5714 | ||
5794dd61 RS |
5715 | if (NILP (no_message)) |
5716 | { | |
5717 | old_message_p = push_message (); | |
5718 | record_unwind_protect (pop_message_unwind, Qnil); | |
5719 | } | |
efdc16c9 | 5720 | |
ff4c9993 RS |
5721 | /* Ordinarily don't quit within this function, |
5722 | but don't make it impossible to quit (in case we get hung in I/O). */ | |
5723 | oquit = Vquit_flag; | |
5724 | Vquit_flag = Qnil; | |
570d7624 JB |
5725 | |
5726 | /* No GCPRO needed, because (when it matters) all Lisp_Object variables | |
5727 | point to non-strings reached from Vbuffer_alist. */ | |
5728 | ||
265a9e55 | 5729 | if (!NILP (Vrun_hooks)) |
570d7624 JB |
5730 | call1 (Vrun_hooks, intern ("auto-save-hook")); |
5731 | ||
e54d3b5d RS |
5732 | if (STRINGP (Vauto_save_list_file_name)) |
5733 | { | |
0894672f | 5734 | Lisp_Object listfile; |
efdc16c9 | 5735 | |
258fd2cb | 5736 | listfile = Fexpand_file_name (Vauto_save_list_file_name, Qnil); |
0894672f GM |
5737 | |
5738 | /* Don't try to create the directory when shutting down Emacs, | |
5739 | because creating the directory might signal an error, and | |
5740 | that would leave Emacs in a strange state. */ | |
5741 | if (!NILP (Vrun_hooks)) | |
5742 | { | |
5743 | Lisp_Object dir; | |
d57563b6 RS |
5744 | dir = Qnil; |
5745 | GCPRO2 (dir, listfile); | |
0894672f GM |
5746 | dir = Ffile_name_directory (listfile); |
5747 | if (NILP (Ffile_directory_p (dir))) | |
5794dd61 RS |
5748 | internal_condition_case_1 (do_auto_save_make_dir, |
5749 | dir, Fcons (Fcons (Qfile_error, Qnil), Qnil), | |
5750 | do_auto_save_eh); | |
d57563b6 | 5751 | UNGCPRO; |
0894672f | 5752 | } |
efdc16c9 | 5753 | |
d5db4077 | 5754 | stream = fopen (SDATA (listfile), "w"); |
0eff1f85 RS |
5755 | if (stream != NULL) |
5756 | { | |
5757 | /* Arrange to close that file whether or not we get an error. | |
5758 | Also reset auto_saving to 0. */ | |
5759 | lispstream = Fcons (Qnil, Qnil); | |
f3fbd155 KR |
5760 | XSETCARFASTINT (lispstream, (EMACS_UINT)stream >> 16); |
5761 | XSETCDRFASTINT (lispstream, (EMACS_UINT)stream & 0xffff); | |
0eff1f85 RS |
5762 | } |
5763 | else | |
5764 | lispstream = Qnil; | |
e54d3b5d RS |
5765 | } |
5766 | else | |
1b335d29 RS |
5767 | { |
5768 | stream = NULL; | |
5769 | lispstream = Qnil; | |
5770 | } | |
199607e4 | 5771 | |
1b335d29 | 5772 | record_unwind_protect (do_auto_save_unwind, lispstream); |
a8c828be RS |
5773 | record_unwind_protect (do_auto_save_unwind_1, |
5774 | make_number (minibuffer_auto_raise)); | |
5775 | minibuffer_auto_raise = 0; | |
3be3c08e RS |
5776 | auto_saving = 1; |
5777 | ||
f14b1c68 JB |
5778 | /* First, save all files which don't have handlers. If Emacs is |
5779 | crashing, the handlers may tweak what is causing Emacs to crash | |
5780 | in the first place, and it would be a shame if Emacs failed to | |
5781 | autosave perfectly ordinary files because it couldn't handle some | |
5782 | ange-ftp'd file. */ | |
5783 | for (do_handled_files = 0; do_handled_files < 2; do_handled_files++) | |
03699b14 | 5784 | for (tail = Vbuffer_alist; GC_CONSP (tail); tail = XCDR (tail)) |
f14b1c68 | 5785 | { |
03699b14 | 5786 | buf = XCDR (XCAR (tail)); |
f14b1c68 | 5787 | b = XBUFFER (buf); |
199607e4 | 5788 | |
e54d3b5d | 5789 | /* Record all the buffers that have auto save mode |
258fd2cb RS |
5790 | in the special file that lists them. For each of these buffers, |
5791 | Record visited name (if any) and auto save name. */ | |
93c30b5f | 5792 | if (STRINGP (b->auto_save_file_name) |
1b335d29 | 5793 | && stream != NULL && do_handled_files == 0) |
e54d3b5d | 5794 | { |
258fd2cb RS |
5795 | if (!NILP (b->filename)) |
5796 | { | |
d5db4077 KR |
5797 | fwrite (SDATA (b->filename), 1, |
5798 | SBYTES (b->filename), stream); | |
258fd2cb | 5799 | } |
1b335d29 | 5800 | putc ('\n', stream); |
d5db4077 KR |
5801 | fwrite (SDATA (b->auto_save_file_name), 1, |
5802 | SBYTES (b->auto_save_file_name), stream); | |
1b335d29 | 5803 | putc ('\n', stream); |
e54d3b5d | 5804 | } |
17857782 | 5805 | |
f14b1c68 JB |
5806 | if (!NILP (current_only) |
5807 | && b != current_buffer) | |
5808 | continue; | |
e54d3b5d | 5809 | |
95385625 RS |
5810 | /* Don't auto-save indirect buffers. |
5811 | The base buffer takes care of it. */ | |
5812 | if (b->base_buffer) | |
5813 | continue; | |
5814 | ||
f14b1c68 JB |
5815 | /* Check for auto save enabled |
5816 | and file changed since last auto save | |
5817 | and file changed since last real save. */ | |
93c30b5f | 5818 | if (STRINGP (b->auto_save_file_name) |
95385625 | 5819 | && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b) |
f14b1c68 | 5820 | && b->auto_save_modified < BUF_MODIFF (b) |
82c2d839 RS |
5821 | /* -1 means we've turned off autosaving for a while--see below. */ |
5822 | && XINT (b->save_length) >= 0 | |
f14b1c68 | 5823 | && (do_handled_files |
49307295 KH |
5824 | || NILP (Ffind_file_name_handler (b->auto_save_file_name, |
5825 | Qwrite_region)))) | |
f14b1c68 | 5826 | { |
b60247d9 RS |
5827 | EMACS_TIME before_time, after_time; |
5828 | ||
5829 | EMACS_GET_TIME (before_time); | |
5830 | ||
5831 | /* If we had a failure, don't try again for 20 minutes. */ | |
5832 | if (b->auto_save_failure_time >= 0 | |
5833 | && EMACS_SECS (before_time) - b->auto_save_failure_time < 1200) | |
5834 | continue; | |
5835 | ||
f14b1c68 JB |
5836 | if ((XFASTINT (b->save_length) * 10 |
5837 | > (BUF_Z (b) - BUF_BEG (b)) * 13) | |
5838 | /* A short file is likely to change a large fraction; | |
5839 | spare the user annoying messages. */ | |
5840 | && XFASTINT (b->save_length) > 5000 | |
5841 | /* These messages are frequent and annoying for `*mail*'. */ | |
5842 | && !EQ (b->filename, Qnil) | |
5843 | && NILP (no_message)) | |
5844 | { | |
5845 | /* It has shrunk too much; turn off auto-saving here. */ | |
a8c828be | 5846 | minibuffer_auto_raise = orig_minibuffer_auto_raise; |
fd91d0d4 | 5847 | message_with_string ("Buffer %s has shrunk a lot; auto save disabled in that buffer until next real save", |
60d67b83 | 5848 | b->name, 1); |
a8c828be | 5849 | minibuffer_auto_raise = 0; |
82c2d839 RS |
5850 | /* Turn off auto-saving until there's a real save, |
5851 | and prevent any more warnings. */ | |
46283abe | 5852 | XSETINT (b->save_length, -1); |
f14b1c68 JB |
5853 | Fsleep_for (make_number (1), Qnil); |
5854 | continue; | |
5855 | } | |
5856 | set_buffer_internal (b); | |
5857 | if (!auto_saved && NILP (no_message)) | |
5858 | message1 ("Auto-saving..."); | |
5859 | internal_condition_case (auto_save_1, Qt, auto_save_error); | |
5860 | auto_saved++; | |
5861 | b->auto_save_modified = BUF_MODIFF (b); | |
2acfd7ae | 5862 | XSETFASTINT (current_buffer->save_length, Z - BEG); |
f14b1c68 | 5863 | set_buffer_internal (old); |
b60247d9 RS |
5864 | |
5865 | EMACS_GET_TIME (after_time); | |
5866 | ||
5867 | /* If auto-save took more than 60 seconds, | |
5868 | assume it was an NFS failure that got a timeout. */ | |
5869 | if (EMACS_SECS (after_time) - EMACS_SECS (before_time) > 60) | |
5870 | b->auto_save_failure_time = EMACS_SECS (after_time); | |
f14b1c68 JB |
5871 | } |
5872 | } | |
570d7624 | 5873 | |
b67f2ca5 RS |
5874 | /* Prevent another auto save till enough input events come in. */ |
5875 | record_auto_save (); | |
570d7624 | 5876 | |
17857782 | 5877 | if (auto_saved && NILP (no_message)) |
f05b275b | 5878 | { |
5794dd61 | 5879 | if (old_message_p) |
31f3d831 | 5880 | { |
5794dd61 RS |
5881 | /* If we are going to restore an old message, |
5882 | give time to read ours. */ | |
22e59fa7 | 5883 | sit_for (1, 0, 0, 0, 0); |
c71106e5 | 5884 | restore_message (); |
31f3d831 | 5885 | } |
f05b275b | 5886 | else |
5794dd61 RS |
5887 | /* If we displayed a message and then restored a state |
5888 | with no message, leave a "done" message on the screen. */ | |
f05b275b KH |
5889 | message1 ("Auto-saving...done"); |
5890 | } | |
570d7624 | 5891 | |
ff4c9993 RS |
5892 | Vquit_flag = oquit; |
5893 | ||
5794dd61 | 5894 | /* This restores the message-stack status. */ |
e54d3b5d | 5895 | unbind_to (count, Qnil); |
570d7624 JB |
5896 | return Qnil; |
5897 | } | |
5898 | ||
5899 | DEFUN ("set-buffer-auto-saved", Fset_buffer_auto_saved, | |
8c1a1077 PJ |
5900 | Sset_buffer_auto_saved, 0, 0, 0, |
5901 | doc: /* Mark current buffer as auto-saved with its current text. | |
5902 | No auto-save file will be written until the buffer changes again. */) | |
5903 | () | |
570d7624 JB |
5904 | { |
5905 | current_buffer->auto_save_modified = MODIFF; | |
2acfd7ae | 5906 | XSETFASTINT (current_buffer->save_length, Z - BEG); |
b60247d9 RS |
5907 | current_buffer->auto_save_failure_time = -1; |
5908 | return Qnil; | |
5909 | } | |
5910 | ||
5911 | DEFUN ("clear-buffer-auto-save-failure", Fclear_buffer_auto_save_failure, | |
8c1a1077 PJ |
5912 | Sclear_buffer_auto_save_failure, 0, 0, 0, |
5913 | doc: /* Clear any record of a recent auto-save failure in the current buffer. */) | |
5914 | () | |
b60247d9 RS |
5915 | { |
5916 | current_buffer->auto_save_failure_time = -1; | |
570d7624 JB |
5917 | return Qnil; |
5918 | } | |
5919 | ||
5920 | DEFUN ("recent-auto-save-p", Frecent_auto_save_p, Srecent_auto_save_p, | |
8c1a1077 PJ |
5921 | 0, 0, 0, |
5922 | doc: /* Return t if buffer has been auto-saved since last read in or saved. */) | |
5923 | () | |
570d7624 | 5924 | { |
95385625 | 5925 | return (SAVE_MODIFF < current_buffer->auto_save_modified) ? Qt : Qnil; |
570d7624 JB |
5926 | } |
5927 | \f | |
5928 | /* Reading and completing file names */ | |
5929 | extern Lisp_Object Ffile_name_completion (), Ffile_name_all_completions (); | |
5930 | ||
6e710ae5 RS |
5931 | /* In the string VAL, change each $ to $$ and return the result. */ |
5932 | ||
5933 | static Lisp_Object | |
5934 | double_dollars (val) | |
5935 | Lisp_Object val; | |
5936 | { | |
19290c65 KR |
5937 | register const unsigned char *old; |
5938 | register unsigned char *new; | |
6e710ae5 RS |
5939 | register int n; |
5940 | int osize, count; | |
5941 | ||
d5db4077 | 5942 | osize = SBYTES (val); |
60d67b83 RS |
5943 | |
5944 | /* Count the number of $ characters. */ | |
d5db4077 | 5945 | for (n = osize, count = 0, old = SDATA (val); n > 0; n--) |
6e710ae5 RS |
5946 | if (*old++ == '$') count++; |
5947 | if (count > 0) | |
5948 | { | |
d5db4077 KR |
5949 | old = SDATA (val); |
5950 | val = make_uninit_multibyte_string (SCHARS (val) + count, | |
60d67b83 | 5951 | osize + count); |
d5db4077 | 5952 | new = SDATA (val); |
6e710ae5 RS |
5953 | for (n = osize; n > 0; n--) |
5954 | if (*old != '$') | |
5955 | *new++ = *old++; | |
5956 | else | |
5957 | { | |
5958 | *new++ = '$'; | |
5959 | *new++ = '$'; | |
5960 | old++; | |
5961 | } | |
5962 | } | |
5963 | return val; | |
5964 | } | |
5965 | ||
59ffe07d KS |
5966 | static Lisp_Object |
5967 | read_file_name_cleanup (arg) | |
5968 | Lisp_Object arg; | |
5969 | { | |
c4174fbb | 5970 | return (current_buffer->directory = arg); |
59ffe07d KS |
5971 | } |
5972 | ||
570d7624 | 5973 | DEFUN ("read-file-name-internal", Fread_file_name_internal, Sread_file_name_internal, |
8c1a1077 PJ |
5974 | 3, 3, 0, |
5975 | doc: /* Internal subroutine for read-file-name. Do not call this. */) | |
5976 | (string, dir, action) | |
570d7624 JB |
5977 | Lisp_Object string, dir, action; |
5978 | /* action is nil for complete, t for return list of completions, | |
5979 | lambda for verify final value */ | |
5980 | { | |
5981 | Lisp_Object name, specdir, realdir, val, orig_string; | |
09121adc | 5982 | int changed; |
8ce069f5 | 5983 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; |
09121adc | 5984 | |
b7826503 | 5985 | CHECK_STRING (string); |
58cc3710 | 5986 | |
09121adc RS |
5987 | realdir = dir; |
5988 | name = string; | |
5989 | orig_string = Qnil; | |
5990 | specdir = Qnil; | |
5991 | changed = 0; | |
5992 | /* No need to protect ACTION--we only compare it with t and nil. */ | |
8ce069f5 | 5993 | GCPRO5 (string, realdir, name, specdir, orig_string); |
570d7624 | 5994 | |
d5db4077 | 5995 | if (SCHARS (string) == 0) |
570d7624 | 5996 | { |
570d7624 | 5997 | if (EQ (action, Qlambda)) |
09121adc RS |
5998 | { |
5999 | UNGCPRO; | |
6000 | return Qnil; | |
6001 | } | |
570d7624 JB |
6002 | } |
6003 | else | |
6004 | { | |
6005 | orig_string = string; | |
6006 | string = Fsubstitute_in_file_name (string); | |
09121adc | 6007 | changed = NILP (Fstring_equal (string, orig_string)); |
570d7624 | 6008 | name = Ffile_name_nondirectory (string); |
09121adc RS |
6009 | val = Ffile_name_directory (string); |
6010 | if (! NILP (val)) | |
6011 | realdir = Fexpand_file_name (val, realdir); | |
570d7624 JB |
6012 | } |
6013 | ||
265a9e55 | 6014 | if (NILP (action)) |
570d7624 JB |
6015 | { |
6016 | specdir = Ffile_name_directory (string); | |
6017 | val = Ffile_name_completion (name, realdir); | |
09121adc | 6018 | UNGCPRO; |
93c30b5f | 6019 | if (!STRINGP (val)) |
570d7624 | 6020 | { |
09121adc | 6021 | if (changed) |
dbd04e01 | 6022 | return double_dollars (string); |
09121adc | 6023 | return val; |
570d7624 JB |
6024 | } |
6025 | ||
265a9e55 | 6026 | if (!NILP (specdir)) |
570d7624 JB |
6027 | val = concat2 (specdir, val); |
6028 | #ifndef VMS | |
6e710ae5 RS |
6029 | return double_dollars (val); |
6030 | #else /* not VMS */ | |
09121adc | 6031 | return val; |
6e710ae5 | 6032 | #endif /* not VMS */ |
570d7624 | 6033 | } |
09121adc | 6034 | UNGCPRO; |
570d7624 JB |
6035 | |
6036 | if (EQ (action, Qt)) | |
59ffe07d KS |
6037 | { |
6038 | Lisp_Object all = Ffile_name_all_completions (name, realdir); | |
6039 | Lisp_Object comp; | |
6040 | int count; | |
6041 | ||
6042 | if (NILP (Vread_file_name_predicate) | |
6043 | || EQ (Vread_file_name_predicate, Qfile_exists_p)) | |
6044 | return all; | |
da46f04f KS |
6045 | |
6046 | #ifndef VMS | |
6047 | if (EQ (Vread_file_name_predicate, Qfile_directory_p)) | |
6048 | { | |
efdc16c9 | 6049 | /* Brute-force speed up for directory checking: |
da46f04f KS |
6050 | Discard strings which don't end in a slash. */ |
6051 | for (comp = Qnil; CONSP (all); all = XCDR (all)) | |
6052 | { | |
6053 | Lisp_Object tem = XCAR (all); | |
6054 | int len; | |
6055 | if (STRINGP (tem) && | |
d5db4077 KR |
6056 | (len = SCHARS (tem), len > 0) && |
6057 | IS_DIRECTORY_SEP (SREF (tem, len-1))) | |
da46f04f KS |
6058 | comp = Fcons (tem, comp); |
6059 | } | |
6060 | } | |
6061 | else | |
6062 | #endif | |
6063 | { | |
6064 | /* Must do it the hard (and slow) way. */ | |
6065 | GCPRO3 (all, comp, specdir); | |
aed13378 | 6066 | count = SPECPDL_INDEX (); |
da46f04f KS |
6067 | record_unwind_protect (read_file_name_cleanup, current_buffer->directory); |
6068 | current_buffer->directory = realdir; | |
6069 | for (comp = Qnil; CONSP (all); all = XCDR (all)) | |
6070 | if (!NILP (call1 (Vread_file_name_predicate, XCAR (all)))) | |
6071 | comp = Fcons (XCAR (all), comp); | |
6072 | unbind_to (count, Qnil); | |
6073 | UNGCPRO; | |
6074 | } | |
59ffe07d KS |
6075 | return Fnreverse (comp); |
6076 | } | |
6077 | ||
570d7624 JB |
6078 | /* Only other case actually used is ACTION = lambda */ |
6079 | #ifdef VMS | |
6080 | /* Supposedly this helps commands such as `cd' that read directory names, | |
6081 | but can someone explain how it helps them? -- RMS */ | |
d5db4077 | 6082 | if (SCHARS (name) == 0) |
570d7624 JB |
6083 | return Qt; |
6084 | #endif /* VMS */ | |
59ffe07d KS |
6085 | if (!NILP (Vread_file_name_predicate)) |
6086 | return call1 (Vread_file_name_predicate, string); | |
570d7624 JB |
6087 | return Ffile_exists_p (string); |
6088 | } | |
6089 | ||
59ffe07d | 6090 | DEFUN ("read-file-name", Fread_file_name, Sread_file_name, 1, 6, 0, |
8c1a1077 PJ |
6091 | doc: /* Read file name, prompting with PROMPT and completing in directory DIR. |
6092 | Value is not expanded---you must call `expand-file-name' yourself. | |
6093 | Default name to DEFAULT-FILENAME if user enters a null string. | |
6094 | (If DEFAULT-FILENAME is omitted, the visited file name is used, | |
6095 | except that if INITIAL is specified, that combined with DIR is used.) | |
6096 | Fourth arg MUSTMATCH non-nil means require existing file's name. | |
6097 | Non-nil and non-t means also require confirmation after completion. | |
6098 | Fifth arg INITIAL specifies text to start with. | |
efdc16c9 | 6099 | If optional sixth arg PREDICATE is non-nil, possible completions and the |
59ffe07d | 6100 | resulting file name must satisfy (funcall PREDICATE NAME). |
8c1a1077 PJ |
6101 | DIR defaults to current buffer's directory default. |
6102 | ||
6103 | If this command was invoked with the mouse, use a file dialog box if | |
6104 | `use-dialog-box' is non-nil, and the window system or X toolkit in use | |
6105 | provides a file dialog box. */) | |
59ffe07d KS |
6106 | (prompt, dir, default_filename, mustmatch, initial, predicate) |
6107 | Lisp_Object prompt, dir, default_filename, mustmatch, initial, predicate; | |
570d7624 | 6108 | { |
8d6d9fef | 6109 | Lisp_Object val, insdef, tem; |
570d7624 JB |
6110 | struct gcpro gcpro1, gcpro2; |
6111 | register char *homedir; | |
d7231f93 | 6112 | Lisp_Object decoded_homedir; |
62f555a5 RS |
6113 | int replace_in_history = 0; |
6114 | int add_to_history = 0; | |
570d7624 JB |
6115 | int count; |
6116 | ||
265a9e55 | 6117 | if (NILP (dir)) |
570d7624 | 6118 | dir = current_buffer->directory; |
3b7f6e60 | 6119 | if (NILP (default_filename)) |
4a9f0fae SM |
6120 | default_filename = !NILP (initial) |
6121 | ? Fexpand_file_name (initial, dir) | |
6122 | : current_buffer->filename; | |
570d7624 JB |
6123 | |
6124 | /* If dir starts with user's homedir, change that to ~. */ | |
6125 | homedir = (char *) egetenv ("HOME"); | |
199607e4 | 6126 | #ifdef DOS_NT |
417c884a EZ |
6127 | /* homedir can be NULL in temacs, since Vprocess_environment is not |
6128 | yet set up. We shouldn't crash in that case. */ | |
6129 | if (homedir != 0) | |
6130 | { | |
6131 | homedir = strcpy (alloca (strlen (homedir) + 1), homedir); | |
6132 | CORRECT_DIR_SEPS (homedir); | |
6133 | } | |
199607e4 | 6134 | #endif |
d7231f93 KH |
6135 | if (homedir != 0) |
6136 | decoded_homedir | |
6137 | = DECODE_FILE (make_unibyte_string (homedir, strlen (homedir))); | |
570d7624 | 6138 | if (homedir != 0 |
93c30b5f | 6139 | && STRINGP (dir) |
d7231f93 KH |
6140 | && !strncmp (SDATA (decoded_homedir), SDATA (dir), |
6141 | SBYTES (decoded_homedir)) | |
6142 | && IS_DIRECTORY_SEP (SREF (dir, SBYTES (decoded_homedir)))) | |
570d7624 | 6143 | { |
60204046 | 6144 | dir = Fsubstring (dir, make_number (SCHARS (decoded_homedir)), Qnil); |
d7231f93 | 6145 | dir = concat2 (build_string ("~"), dir); |
570d7624 | 6146 | } |
8d6d9fef AS |
6147 | /* Likewise for default_filename. */ |
6148 | if (homedir != 0 | |
6149 | && STRINGP (default_filename) | |
d7231f93 KH |
6150 | && !strncmp (SDATA (decoded_homedir), SDATA (default_filename), |
6151 | SBYTES (decoded_homedir)) | |
6152 | && IS_DIRECTORY_SEP (SREF (default_filename, SBYTES (decoded_homedir)))) | |
8d6d9fef AS |
6153 | { |
6154 | default_filename | |
d7231f93 | 6155 | = Fsubstring (default_filename, |
60204046 | 6156 | make_number (SCHARS (decoded_homedir)), Qnil); |
d7231f93 | 6157 | default_filename = concat2 (build_string ("~"), default_filename); |
8d6d9fef AS |
6158 | } |
6159 | if (!NILP (default_filename)) | |
b537a6c7 | 6160 | { |
b7826503 | 6161 | CHECK_STRING (default_filename); |
b537a6c7 RS |
6162 | default_filename = double_dollars (default_filename); |
6163 | } | |
570d7624 | 6164 | |
58cc3710 | 6165 | if (insert_default_directory && STRINGP (dir)) |
570d7624 JB |
6166 | { |
6167 | insdef = dir; | |
265a9e55 | 6168 | if (!NILP (initial)) |
570d7624 | 6169 | { |
15c65264 | 6170 | Lisp_Object args[2], pos; |
570d7624 JB |
6171 | |
6172 | args[0] = insdef; | |
6173 | args[1] = initial; | |
6174 | insdef = Fconcat (2, args); | |
d5db4077 | 6175 | pos = make_number (SCHARS (double_dollars (dir))); |
8d6d9fef | 6176 | insdef = Fcons (double_dollars (insdef), pos); |
570d7624 | 6177 | } |
6e710ae5 | 6178 | else |
8d6d9fef | 6179 | insdef = double_dollars (insdef); |
570d7624 | 6180 | } |
58cc3710 | 6181 | else if (STRINGP (initial)) |
8d6d9fef | 6182 | insdef = Fcons (double_dollars (initial), make_number (0)); |
570d7624 | 6183 | else |
8d6d9fef | 6184 | insdef = Qnil; |
570d7624 | 6185 | |
59ffe07d KS |
6186 | if (!NILP (Vread_file_name_function)) |
6187 | { | |
6188 | Lisp_Object args[7]; | |
6189 | ||
6190 | GCPRO2 (insdef, default_filename); | |
6191 | args[0] = Vread_file_name_function; | |
6192 | args[1] = prompt; | |
6193 | args[2] = dir; | |
6194 | args[3] = default_filename; | |
6195 | args[4] = mustmatch; | |
6196 | args[5] = initial; | |
6197 | args[6] = predicate; | |
6198 | RETURN_UNGCPRO (Ffuncall (7, args)); | |
6199 | } | |
6200 | ||
aed13378 | 6201 | count = SPECPDL_INDEX (); |
a79485af | 6202 | #ifdef VMS |
570d7624 JB |
6203 | specbind (intern ("completion-ignore-case"), Qt); |
6204 | #endif | |
6205 | ||
a79485af | 6206 | specbind (intern ("minibuffer-completing-file-name"), Qt); |
efdc16c9 | 6207 | specbind (intern ("read-file-name-predicate"), |
59ffe07d | 6208 | (NILP (predicate) ? Qfile_exists_p : predicate)); |
a79485af | 6209 | |
3b7f6e60 | 6210 | GCPRO2 (insdef, default_filename); |
c60ee5e7 | 6211 | |
488dd4c4 | 6212 | #if defined (USE_MOTIF) || defined (HAVE_NTGUI) || defined (USE_GTK) |
9c856db9 GM |
6213 | if ((NILP (last_nonmenu_event) || CONSP (last_nonmenu_event)) |
6214 | && use_dialog_box | |
6215 | && have_menus_p ()) | |
6216 | { | |
9172b88d GM |
6217 | /* If DIR contains a file name, split it. */ |
6218 | Lisp_Object file; | |
6219 | file = Ffile_name_nondirectory (dir); | |
d5db4077 | 6220 | if (SCHARS (file) && NILP (default_filename)) |
9172b88d GM |
6221 | { |
6222 | default_filename = file; | |
6223 | dir = Ffile_name_directory (dir); | |
6224 | } | |
f73f57bd JR |
6225 | if (!NILP(default_filename)) |
6226 | default_filename = Fexpand_file_name (default_filename, dir); | |
9c856db9 GM |
6227 | val = Fx_file_dialog (prompt, dir, default_filename, mustmatch); |
6228 | add_to_history = 1; | |
6229 | } | |
6230 | else | |
6231 | #endif | |
6232 | val = Fcompleting_read (prompt, intern ("read-file-name-internal"), | |
6233 | dir, mustmatch, insdef, | |
6234 | Qfile_name_history, default_filename, Qnil); | |
62f555a5 RS |
6235 | |
6236 | tem = Fsymbol_value (Qfile_name_history); | |
03699b14 | 6237 | if (CONSP (tem) && EQ (XCAR (tem), val)) |
62f555a5 RS |
6238 | replace_in_history = 1; |
6239 | ||
6240 | /* If Fcompleting_read returned the inserted default string itself | |
a8c828be RS |
6241 | (rather than a new string with the same contents), |
6242 | it has to mean that the user typed RET with the minibuffer empty. | |
6243 | In that case, we really want to return "" | |
6244 | so that commands such as set-visited-file-name can distinguish. */ | |
6245 | if (EQ (val, default_filename)) | |
62f555a5 RS |
6246 | { |
6247 | /* In this case, Fcompleting_read has not added an element | |
6248 | to the history. Maybe we should. */ | |
6249 | if (! replace_in_history) | |
6250 | add_to_history = 1; | |
6251 | ||
4a9f0fae | 6252 | val = empty_string; |
62f555a5 | 6253 | } |
570d7624 | 6254 | |
570d7624 | 6255 | unbind_to (count, Qnil); |
570d7624 | 6256 | UNGCPRO; |
265a9e55 | 6257 | if (NILP (val)) |
570d7624 | 6258 | error ("No file name specified"); |
62f555a5 | 6259 | |
8d6d9fef | 6260 | tem = Fstring_equal (val, CONSP (insdef) ? XCAR (insdef) : insdef); |
62f555a5 | 6261 | |
3b7f6e60 | 6262 | if (!NILP (tem) && !NILP (default_filename)) |
62f555a5 | 6263 | val = default_filename; |
d5db4077 | 6264 | else if (SCHARS (val) == 0 && NILP (insdef)) |
d9bc1c99 | 6265 | { |
3b7f6e60 | 6266 | if (!NILP (default_filename)) |
62f555a5 | 6267 | val = default_filename; |
d9bc1c99 RS |
6268 | else |
6269 | error ("No default file name"); | |
6270 | } | |
62f555a5 | 6271 | val = Fsubstitute_in_file_name (val); |
570d7624 | 6272 | |
62f555a5 RS |
6273 | if (replace_in_history) |
6274 | /* Replace what Fcompleting_read added to the history | |
6275 | with what we will actually return. */ | |
f3fbd155 | 6276 | XSETCAR (Fsymbol_value (Qfile_name_history), double_dollars (val)); |
62f555a5 | 6277 | else if (add_to_history) |
570d7624 | 6278 | { |
62f555a5 RS |
6279 | /* Add the value to the history--but not if it matches |
6280 | the last value already there. */ | |
8d6d9fef | 6281 | Lisp_Object val1 = double_dollars (val); |
62f555a5 | 6282 | tem = Fsymbol_value (Qfile_name_history); |
03699b14 | 6283 | if (! CONSP (tem) || NILP (Fequal (XCAR (tem), val1))) |
62f555a5 | 6284 | Fset (Qfile_name_history, |
8d6d9fef | 6285 | Fcons (val1, tem)); |
570d7624 | 6286 | } |
efdc16c9 | 6287 | |
62f555a5 | 6288 | return val; |
570d7624 | 6289 | } |
9c856db9 | 6290 | |
570d7624 | 6291 | \f |
dbda5089 GV |
6292 | void |
6293 | init_fileio_once () | |
6294 | { | |
6295 | /* Must be set before any path manipulation is performed. */ | |
6296 | XSETFASTINT (Vdirectory_sep_char, '/'); | |
6297 | } | |
6298 | ||
9c856db9 | 6299 | \f |
dfcf069d | 6300 | void |
570d7624 JB |
6301 | syms_of_fileio () |
6302 | { | |
0bf2eed2 | 6303 | Qexpand_file_name = intern ("expand-file-name"); |
273e0829 | 6304 | Qsubstitute_in_file_name = intern ("substitute-in-file-name"); |
0bf2eed2 RS |
6305 | Qdirectory_file_name = intern ("directory-file-name"); |
6306 | Qfile_name_directory = intern ("file-name-directory"); | |
6307 | Qfile_name_nondirectory = intern ("file-name-nondirectory"); | |
642ef245 | 6308 | Qunhandled_file_name_directory = intern ("unhandled-file-name-directory"); |
0bf2eed2 | 6309 | Qfile_name_as_directory = intern ("file-name-as-directory"); |
32f4334d | 6310 | Qcopy_file = intern ("copy-file"); |
a6e6e718 | 6311 | Qmake_directory_internal = intern ("make-directory-internal"); |
b272d624 | 6312 | Qmake_directory = intern ("make-directory"); |
32f4334d RS |
6313 | Qdelete_directory = intern ("delete-directory"); |
6314 | Qdelete_file = intern ("delete-file"); | |
6315 | Qrename_file = intern ("rename-file"); | |
6316 | Qadd_name_to_file = intern ("add-name-to-file"); | |
6317 | Qmake_symbolic_link = intern ("make-symbolic-link"); | |
6318 | Qfile_exists_p = intern ("file-exists-p"); | |
6319 | Qfile_executable_p = intern ("file-executable-p"); | |
6320 | Qfile_readable_p = intern ("file-readable-p"); | |
32f4334d | 6321 | Qfile_writable_p = intern ("file-writable-p"); |
1f8653eb RS |
6322 | Qfile_symlink_p = intern ("file-symlink-p"); |
6323 | Qaccess_file = intern ("access-file"); | |
32f4334d | 6324 | Qfile_directory_p = intern ("file-directory-p"); |
adedc71d | 6325 | Qfile_regular_p = intern ("file-regular-p"); |
32f4334d RS |
6326 | Qfile_accessible_directory_p = intern ("file-accessible-directory-p"); |
6327 | Qfile_modes = intern ("file-modes"); | |
6328 | Qset_file_modes = intern ("set-file-modes"); | |
6329 | Qfile_newer_than_file_p = intern ("file-newer-than-file-p"); | |
6330 | Qinsert_file_contents = intern ("insert-file-contents"); | |
6331 | Qwrite_region = intern ("write-region"); | |
6332 | Qverify_visited_file_modtime = intern ("verify-visited-file-modtime"); | |
3ec46acd | 6333 | Qset_visited_file_modtime = intern ("set-visited-file-modtime"); |
356a6224 | 6334 | Qauto_save_coding = intern ("auto-save-coding"); |
32f4334d | 6335 | |
642ef245 | 6336 | staticpro (&Qexpand_file_name); |
273e0829 | 6337 | staticpro (&Qsubstitute_in_file_name); |
642ef245 JB |
6338 | staticpro (&Qdirectory_file_name); |
6339 | staticpro (&Qfile_name_directory); | |
6340 | staticpro (&Qfile_name_nondirectory); | |
6341 | staticpro (&Qunhandled_file_name_directory); | |
6342 | staticpro (&Qfile_name_as_directory); | |
15c65264 | 6343 | staticpro (&Qcopy_file); |
c34b559d | 6344 | staticpro (&Qmake_directory_internal); |
b272d624 | 6345 | staticpro (&Qmake_directory); |
15c65264 RS |
6346 | staticpro (&Qdelete_directory); |
6347 | staticpro (&Qdelete_file); | |
6348 | staticpro (&Qrename_file); | |
6349 | staticpro (&Qadd_name_to_file); | |
6350 | staticpro (&Qmake_symbolic_link); | |
6351 | staticpro (&Qfile_exists_p); | |
6352 | staticpro (&Qfile_executable_p); | |
6353 | staticpro (&Qfile_readable_p); | |
15c65264 | 6354 | staticpro (&Qfile_writable_p); |
1f8653eb RS |
6355 | staticpro (&Qaccess_file); |
6356 | staticpro (&Qfile_symlink_p); | |
15c65264 | 6357 | staticpro (&Qfile_directory_p); |
adedc71d | 6358 | staticpro (&Qfile_regular_p); |
15c65264 RS |
6359 | staticpro (&Qfile_accessible_directory_p); |
6360 | staticpro (&Qfile_modes); | |
6361 | staticpro (&Qset_file_modes); | |
6362 | staticpro (&Qfile_newer_than_file_p); | |
6363 | staticpro (&Qinsert_file_contents); | |
6364 | staticpro (&Qwrite_region); | |
6365 | staticpro (&Qverify_visited_file_modtime); | |
0a61794b | 6366 | staticpro (&Qset_visited_file_modtime); |
356a6224 | 6367 | staticpro (&Qauto_save_coding); |
642ef245 JB |
6368 | |
6369 | Qfile_name_history = intern ("file-name-history"); | |
6370 | Fset (Qfile_name_history, Qnil); | |
15c65264 RS |
6371 | staticpro (&Qfile_name_history); |
6372 | ||
570d7624 JB |
6373 | Qfile_error = intern ("file-error"); |
6374 | staticpro (&Qfile_error); | |
199607e4 | 6375 | Qfile_already_exists = intern ("file-already-exists"); |
570d7624 | 6376 | staticpro (&Qfile_already_exists); |
c0b7b21c RS |
6377 | Qfile_date_error = intern ("file-date-error"); |
6378 | staticpro (&Qfile_date_error); | |
505ab9bc RS |
6379 | Qexcl = intern ("excl"); |
6380 | staticpro (&Qexcl); | |
570d7624 | 6381 | |
5e570b75 | 6382 | #ifdef DOS_NT |
4c3c22f3 RS |
6383 | Qfind_buffer_file_type = intern ("find-buffer-file-type"); |
6384 | staticpro (&Qfind_buffer_file_type); | |
5e570b75 | 6385 | #endif /* DOS_NT */ |
4c3c22f3 | 6386 | |
b1d1b865 | 6387 | DEFVAR_LISP ("file-name-coding-system", &Vfile_name_coding_system, |
8c1a1077 | 6388 | doc: /* *Coding system for encoding file names. |
346ebf53 | 6389 | If it is nil, `default-file-name-coding-system' (which see) is used. */); |
b1d1b865 RS |
6390 | Vfile_name_coding_system = Qnil; |
6391 | ||
cd913586 KH |
6392 | DEFVAR_LISP ("default-file-name-coding-system", |
6393 | &Vdefault_file_name_coding_system, | |
8c1a1077 | 6394 | doc: /* Default coding system for encoding file names. |
346ebf53 | 6395 | This variable is used only when `file-name-coding-system' is nil. |
8c1a1077 | 6396 | |
346ebf53 | 6397 | This variable is set/changed by the command `set-language-environment'. |
8c1a1077 | 6398 | User should not set this variable manually, |
346ebf53 | 6399 | instead use `file-name-coding-system' to get a constant encoding |
8c1a1077 | 6400 | of file names regardless of the current language environment. */); |
cd913586 KH |
6401 | Vdefault_file_name_coding_system = Qnil; |
6402 | ||
0d420e88 | 6403 | DEFVAR_LISP ("auto-save-file-format", &Vauto_save_file_format, |
8c1a1077 PJ |
6404 | doc: /* *Format in which to write auto-save files. |
6405 | Should be a list of symbols naming formats that are defined in `format-alist'. | |
6406 | If it is t, which is the default, auto-save files are written in the | |
6407 | same format as a regular save would use. */); | |
0d420e88 BG |
6408 | Vauto_save_file_format = Qt; |
6409 | ||
6410 | Qformat_decode = intern ("format-decode"); | |
6411 | staticpro (&Qformat_decode); | |
6412 | Qformat_annotate_function = intern ("format-annotate-function"); | |
6413 | staticpro (&Qformat_annotate_function); | |
2080470e KH |
6414 | Qafter_insert_file_set_coding = intern ("after-insert-file-set-coding"); |
6415 | staticpro (&Qafter_insert_file_set_coding); | |
efdc16c9 | 6416 | |
d6a3cc15 RS |
6417 | Qcar_less_than_car = intern ("car-less-than-car"); |
6418 | staticpro (&Qcar_less_than_car); | |
6419 | ||
570d7624 JB |
6420 | Fput (Qfile_error, Qerror_conditions, |
6421 | Fcons (Qfile_error, Fcons (Qerror, Qnil))); | |
6422 | Fput (Qfile_error, Qerror_message, | |
6423 | build_string ("File error")); | |
6424 | ||
6425 | Fput (Qfile_already_exists, Qerror_conditions, | |
6426 | Fcons (Qfile_already_exists, | |
6427 | Fcons (Qfile_error, Fcons (Qerror, Qnil)))); | |
6428 | Fput (Qfile_already_exists, Qerror_message, | |
6429 | build_string ("File already exists")); | |
6430 | ||
c0b7b21c RS |
6431 | Fput (Qfile_date_error, Qerror_conditions, |
6432 | Fcons (Qfile_date_error, | |
6433 | Fcons (Qfile_error, Fcons (Qerror, Qnil)))); | |
6434 | Fput (Qfile_date_error, Qerror_message, | |
6435 | build_string ("Cannot set file date")); | |
6436 | ||
59ffe07d KS |
6437 | DEFVAR_LISP ("read-file-name-function", &Vread_file_name_function, |
6438 | doc: /* If this is non-nil, `read-file-name' does its work by calling this function. */); | |
6439 | Vread_file_name_function = Qnil; | |
6440 | ||
6441 | DEFVAR_LISP ("read-file-name-predicate", &Vread_file_name_predicate, | |
6442 | doc: /* Current predicate used by `read-file-name-internal'. */); | |
6443 | Vread_file_name_predicate = Qnil; | |
6444 | ||
570d7624 | 6445 | DEFVAR_BOOL ("insert-default-directory", &insert_default_directory, |
8c1a1077 | 6446 | doc: /* *Non-nil means when reading a filename start with default dir in minibuffer. */); |
570d7624 JB |
6447 | insert_default_directory = 1; |
6448 | ||
6449 | DEFVAR_BOOL ("vms-stmlf-recfm", &vms_stmlf_recfm, | |
8c1a1077 PJ |
6450 | doc: /* *Non-nil means write new files with record format `stmlf'. |
6451 | nil means use format `var'. This variable is meaningful only on VMS. */); | |
570d7624 JB |
6452 | vms_stmlf_recfm = 0; |
6453 | ||
199607e4 | 6454 | DEFVAR_LISP ("directory-sep-char", &Vdirectory_sep_char, |
8c1a1077 | 6455 | doc: /* Directory separator character for built-in functions that return file names. |
d57563b6 | 6456 | The value is always ?/. Don't use this variable, just use `/'. */); |
199607e4 | 6457 | |
1d1826db | 6458 | DEFVAR_LISP ("file-name-handler-alist", &Vfile_name_handler_alist, |
8c1a1077 PJ |
6459 | doc: /* *Alist of elements (REGEXP . HANDLER) for file names handled specially. |
6460 | If a file name matches REGEXP, then all I/O on that file is done by calling | |
6461 | HANDLER. | |
6462 | ||
6463 | The first argument given to HANDLER is the name of the I/O primitive | |
6464 | to be handled; the remaining arguments are the arguments that were | |
6465 | passed to that primitive. For example, if you do | |
6466 | (file-exists-p FILENAME) | |
6467 | and FILENAME is handled by HANDLER, then HANDLER is called like this: | |
6468 | (funcall HANDLER 'file-exists-p FILENAME) | |
6469 | The function `find-file-name-handler' checks this list for a handler | |
6470 | for its argument. */); | |
09121adc RS |
6471 | Vfile_name_handler_alist = Qnil; |
6472 | ||
0414b394 KH |
6473 | DEFVAR_LISP ("set-auto-coding-function", |
6474 | &Vset_auto_coding_function, | |
8c1a1077 PJ |
6475 | doc: /* If non-nil, a function to call to decide a coding system of file. |
6476 | Two arguments are passed to this function: the file name | |
6477 | and the length of a file contents following the point. | |
6478 | This function should return a coding system to decode the file contents. | |
6479 | It should check the file name against `auto-coding-alist'. | |
6480 | If no coding system is decided, it should check a coding system | |
6481 | specified in the heading lines with the format: | |
6482 | -*- ... coding: CODING-SYSTEM; ... -*- | |
6483 | or local variable spec of the tailing lines with `coding:' tag. */); | |
0414b394 | 6484 | Vset_auto_coding_function = Qnil; |
c9e82392 | 6485 | |
d6a3cc15 | 6486 | DEFVAR_LISP ("after-insert-file-functions", &Vafter_insert_file_functions, |
8c1a1077 | 6487 | doc: /* A list of functions to be called at the end of `insert-file-contents'. |
cf6d2357 RS |
6488 | Each is passed one argument, the number of characters inserted. |
6489 | It should return the new character count, and leave point the same. | |
6490 | If `insert-file-contents' is intercepted by a handler from | |
6491 | `file-name-handler-alist', that handler is responsible for calling the | |
6492 | functions in `after-insert-file-functions' if appropriate. */); | |
d6a3cc15 RS |
6493 | Vafter_insert_file_functions = Qnil; |
6494 | ||
6495 | DEFVAR_LISP ("write-region-annotate-functions", &Vwrite_region_annotate_functions, | |
8c1a1077 PJ |
6496 | doc: /* A list of functions to be called at the start of `write-region'. |
6497 | Each is passed two arguments, START and END as for `write-region'. | |
6498 | These are usually two numbers but not always; see the documentation | |
6499 | for `write-region'. The function should return a list of pairs | |
6500 | of the form (POSITION . STRING), consisting of strings to be effectively | |
6501 | inserted at the specified positions of the file being written (1 means to | |
6502 | insert before the first byte written). The POSITIONs must be sorted into | |
6503 | increasing order. If there are several functions in the list, the several | |
28c3eb5a | 6504 | lists are merged destructively. Alternatively, the function can return |
bd235610 SM |
6505 | with a different buffer current; in that case it should pay attention |
6506 | to the annotations returned by previous functions and listed in | |
6507 | `write-region-annotations-so-far'.*/); | |
d6a3cc15 | 6508 | Vwrite_region_annotate_functions = Qnil; |
bd235610 SM |
6509 | staticpro (&Qwrite_region_annotate_functions); |
6510 | Qwrite_region_annotate_functions | |
6511 | = intern ("write-region-annotate-functions"); | |
d6a3cc15 | 6512 | |
6fc6f94b RS |
6513 | DEFVAR_LISP ("write-region-annotations-so-far", |
6514 | &Vwrite_region_annotations_so_far, | |
8c1a1077 PJ |
6515 | doc: /* When an annotation function is called, this holds the previous annotations. |
6516 | These are the annotations made by other annotation functions | |
6517 | that were already called. See also `write-region-annotate-functions'. */); | |
6fc6f94b RS |
6518 | Vwrite_region_annotations_so_far = Qnil; |
6519 | ||
82c2d839 | 6520 | DEFVAR_LISP ("inhibit-file-name-handlers", &Vinhibit_file_name_handlers, |
8c1a1077 PJ |
6521 | doc: /* A list of file name handlers that temporarily should not be used. |
6522 | This applies only to the operation `inhibit-file-name-operation'. */); | |
82c2d839 RS |
6523 | Vinhibit_file_name_handlers = Qnil; |
6524 | ||
a65970a0 | 6525 | DEFVAR_LISP ("inhibit-file-name-operation", &Vinhibit_file_name_operation, |
8c1a1077 | 6526 | doc: /* The operation for which `inhibit-file-name-handlers' is applicable. */); |
a65970a0 RS |
6527 | Vinhibit_file_name_operation = Qnil; |
6528 | ||
e54d3b5d | 6529 | DEFVAR_LISP ("auto-save-list-file-name", &Vauto_save_list_file_name, |
8c1a1077 PJ |
6530 | doc: /* File name in which we write a list of all auto save file names. |
6531 | This variable is initialized automatically from `auto-save-list-file-prefix' | |
6532 | shortly after Emacs reads your `.emacs' file, if you have not yet given it | |
6533 | a non-nil value. */); | |
e54d3b5d RS |
6534 | Vauto_save_list_file_name = Qnil; |
6535 | ||
642ef245 | 6536 | defsubr (&Sfind_file_name_handler); |
570d7624 JB |
6537 | defsubr (&Sfile_name_directory); |
6538 | defsubr (&Sfile_name_nondirectory); | |
642ef245 | 6539 | defsubr (&Sunhandled_file_name_directory); |
570d7624 JB |
6540 | defsubr (&Sfile_name_as_directory); |
6541 | defsubr (&Sdirectory_file_name); | |
6542 | defsubr (&Smake_temp_name); | |
6543 | defsubr (&Sexpand_file_name); | |
6544 | defsubr (&Ssubstitute_in_file_name); | |
6545 | defsubr (&Scopy_file); | |
9bbe01fb | 6546 | defsubr (&Smake_directory_internal); |
aa734e17 | 6547 | defsubr (&Sdelete_directory); |
570d7624 JB |
6548 | defsubr (&Sdelete_file); |
6549 | defsubr (&Srename_file); | |
6550 | defsubr (&Sadd_name_to_file); | |
6551 | #ifdef S_IFLNK | |
6552 | defsubr (&Smake_symbolic_link); | |
6553 | #endif /* S_IFLNK */ | |
6554 | #ifdef VMS | |
6555 | defsubr (&Sdefine_logical_name); | |
6556 | #endif /* VMS */ | |
6557 | #ifdef HPUX_NET | |
6558 | defsubr (&Ssysnetunam); | |
6559 | #endif /* HPUX_NET */ | |
6560 | defsubr (&Sfile_name_absolute_p); | |
6561 | defsubr (&Sfile_exists_p); | |
6562 | defsubr (&Sfile_executable_p); | |
6563 | defsubr (&Sfile_readable_p); | |
6564 | defsubr (&Sfile_writable_p); | |
1f8653eb | 6565 | defsubr (&Saccess_file); |
570d7624 JB |
6566 | defsubr (&Sfile_symlink_p); |
6567 | defsubr (&Sfile_directory_p); | |
b72dea2a | 6568 | defsubr (&Sfile_accessible_directory_p); |
f793dc6c | 6569 | defsubr (&Sfile_regular_p); |
570d7624 JB |
6570 | defsubr (&Sfile_modes); |
6571 | defsubr (&Sset_file_modes); | |
c24e9a53 RS |
6572 | defsubr (&Sset_default_file_modes); |
6573 | defsubr (&Sdefault_file_modes); | |
570d7624 JB |
6574 | defsubr (&Sfile_newer_than_file_p); |
6575 | defsubr (&Sinsert_file_contents); | |
6576 | defsubr (&Swrite_region); | |
d6a3cc15 | 6577 | defsubr (&Scar_less_than_car); |
570d7624 JB |
6578 | defsubr (&Sverify_visited_file_modtime); |
6579 | defsubr (&Sclear_visited_file_modtime); | |
f5d5eccf | 6580 | defsubr (&Svisited_file_modtime); |
570d7624 JB |
6581 | defsubr (&Sset_visited_file_modtime); |
6582 | defsubr (&Sdo_auto_save); | |
6583 | defsubr (&Sset_buffer_auto_saved); | |
b60247d9 | 6584 | defsubr (&Sclear_buffer_auto_save_failure); |
570d7624 JB |
6585 | defsubr (&Srecent_auto_save_p); |
6586 | ||
6587 | defsubr (&Sread_file_name_internal); | |
6588 | defsubr (&Sread_file_name); | |
85ffea93 | 6589 | |
483a2e10 | 6590 | #ifdef unix |
85ffea93 | 6591 | defsubr (&Sunix_sync); |
483a2e10 | 6592 | #endif |
570d7624 | 6593 | } |