Commit | Line | Data |
---|---|---|
570d7624 | 1 | /* File IO for GNU Emacs. |
e9bffc61 | 2 | |
24c51a09 | 3 | Copyright (C) 1985-1988, 1993-2012 Free Software Foundation, Inc. |
570d7624 JB |
4 | |
5 | This file is part of GNU Emacs. | |
6 | ||
9ec0b715 | 7 | GNU Emacs is free software: you can redistribute it and/or modify |
570d7624 | 8 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
9 | the Free Software Foundation, either version 3 of the License, or |
10 | (at your option) any later version. | |
570d7624 JB |
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 | |
9ec0b715 | 18 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
570d7624 | 19 | |
18160b98 | 20 | #include <config.h> |
77a28bbf | 21 | #include <limits.h> |
bb369dc6 | 22 | #include <fcntl.h> |
1b335d29 | 23 | #include <stdio.h> |
570d7624 JB |
24 | #include <sys/types.h> |
25 | #include <sys/stat.h> | |
29beb080 | 26 | #include <unistd.h> |
29beb080 | 27 | |
5b9c0a1d | 28 | #ifdef HAVE_PWD_H |
570d7624 | 29 | #include <pwd.h> |
bfb61299 JB |
30 | #endif |
31 | ||
570d7624 JB |
32 | #include <errno.h> |
33 | ||
574c05e2 KK |
34 | #ifdef HAVE_LIBSELINUX |
35 | #include <selinux/selinux.h> | |
36 | #include <selinux/context.h> | |
37 | #endif | |
38 | ||
620f13b0 PE |
39 | #include <c-ctype.h> |
40 | ||
570d7624 | 41 | #include "lisp.h" |
8d4e077b | 42 | #include "intervals.h" |
db327c7e | 43 | #include "character.h" |
e5560ff7 | 44 | #include "buffer.h" |
6fdaa9a0 | 45 | #include "coding.h" |
570d7624 | 46 | #include "window.h" |
67c08d6c | 47 | #include "blockinput.h" |
385ed61f KL |
48 | #include "frame.h" |
49 | #include "dispextern.h" | |
570d7624 | 50 | |
5e570b75 RS |
51 | #ifdef WINDOWSNT |
52 | #define NOMINMAX 1 | |
53 | #include <windows.h> | |
5e570b75 | 54 | #include <fcntl.h> |
bb385a92 | 55 | #include <sys/file.h> |
5e570b75 RS |
56 | #endif /* not WINDOWSNT */ |
57 | ||
7990d02a EZ |
58 | #ifdef MSDOS |
59 | #include "msdos.h" | |
60 | #include <sys/param.h> | |
7990d02a | 61 | #include <fcntl.h> |
7990d02a | 62 | #endif |
7990d02a | 63 | |
199607e4 | 64 | #ifdef DOS_NT |
199607e4 | 65 | /* On Windows, drive letters must be alphabetic - on DOS, the Netware |
a5bb9bd3 | 66 | redirector allows the six letters between 'Z' and 'a' as well. */ |
199607e4 RS |
67 | #ifdef MSDOS |
68 | #define IS_DRIVE(x) ((x) >= 'A' && (x) <= 'z') | |
69 | #endif | |
70 | #ifdef WINDOWSNT | |
620f13b0 | 71 | #define IS_DRIVE(x) c_isalpha (x) |
199607e4 | 72 | #endif |
f54b565c | 73 | /* Need to lower-case the drive letter, or else expanded |
53964682 | 74 | filenames will sometimes compare unequal, because |
f54b565c | 75 | `expand-file-name' doesn't always down-case the drive letter. */ |
620f13b0 | 76 | #define DRIVE_LETTER(x) c_tolower (x) |
199607e4 RS |
77 | #endif |
78 | ||
de5bf5d3 | 79 | #include "systime.h" |
d35af63c | 80 | #include <stat-time.h> |
570d7624 JB |
81 | |
82 | #ifdef HPUX | |
83 | #include <netio.h> | |
47e7b9e5 | 84 | #endif |
570d7624 | 85 | |
9c856db9 | 86 | #include "commands.h" |
9c856db9 | 87 | |
f75d7a91 PE |
88 | /* True during writing of auto-save files. */ |
89 | static bool auto_saving; | |
570d7624 | 90 | |
71873e2b | 91 | /* Nonzero umask during creation of auto-save directories. */ |
f75d7a91 | 92 | static mode_t auto_saving_dir_umask; |
09450bae | 93 | |
570d7624 | 94 | /* Set by auto_save_1 to mode of original file so Fwrite_region will create |
71873e2b | 95 | a new file with the same mode as the original. */ |
f75d7a91 | 96 | static mode_t auto_save_mode_bits; |
570d7624 | 97 | |
71873e2b | 98 | /* Set by auto_save_1 if an error occurred during the last auto-save. */ |
f75d7a91 | 99 | static bool auto_save_error_occurred; |
ca730bf0 | 100 | |
356a6224 KH |
101 | /* The symbol bound to coding-system-for-read when |
102 | insert-file-contents is called for recovering a file. This is not | |
103 | an actual coding system name, but just an indicator to tell | |
104 | insert-file-contents to use `emacs-mule' with a special flag for | |
105 | auto saving and recovering a file. */ | |
955cbe7b | 106 | static Lisp_Object Qauto_save_coding; |
356a6224 | 107 | |
f6c9b683 RS |
108 | /* Property name of a file name handler, |
109 | which gives a list of operations it handles.. */ | |
955cbe7b | 110 | static Lisp_Object Qoperations; |
f6c9b683 | 111 | |
71873e2b | 112 | /* Lisp functions for translating file formats. */ |
955cbe7b | 113 | static Lisp_Object Qformat_decode, Qformat_annotate_function; |
0d420e88 | 114 | |
2080470e | 115 | /* Lisp function for setting buffer-file-coding-system and the |
b6426b03 | 116 | multibyteness of the current buffer after inserting a file. */ |
955cbe7b | 117 | static Lisp_Object Qafter_insert_file_set_coding; |
b6426b03 | 118 | |
955cbe7b | 119 | static Lisp_Object Qwrite_region_annotate_functions; |
67fbc0cb CY |
120 | /* Each time an annotation function changes the buffer, the new buffer |
121 | is added here. */ | |
5045092b | 122 | static Lisp_Object Vwrite_region_annotation_buffers; |
67fbc0cb | 123 | |
ccf61795 | 124 | #ifdef HAVE_FSYNC |
ccf61795 RF |
125 | #endif |
126 | ||
955cbe7b | 127 | static Lisp_Object Qdelete_by_moving_to_trash; |
d2b66acf | 128 | |
6cf29fe8 | 129 | /* Lisp function for moving files to trash. */ |
955cbe7b | 130 | static Lisp_Object Qmove_file_to_trash; |
6cf29fe8 | 131 | |
8719abec | 132 | /* Lisp function for recursively copying directories. */ |
955cbe7b | 133 | static Lisp_Object Qcopy_directory; |
8719abec CY |
134 | |
135 | /* Lisp function for recursively deleting directories. */ | |
955cbe7b | 136 | static Lisp_Object Qdelete_directory; |
8719abec | 137 | |
c1c4693e | 138 | #ifdef WINDOWSNT |
c1c4693e RS |
139 | #endif |
140 | ||
955cbe7b PE |
141 | Lisp_Object Qfile_error; |
142 | static Lisp_Object Qfile_already_exists, Qfile_date_error; | |
143 | static Lisp_Object Qexcl; | |
15c65264 RS |
144 | Lisp_Object Qfile_name_history; |
145 | ||
955cbe7b | 146 | static Lisp_Object Qcar_less_than_car; |
d6a3cc15 | 147 | |
f75d7a91 PE |
148 | static bool a_write (int, Lisp_Object, ptrdiff_t, ptrdiff_t, |
149 | Lisp_Object *, struct coding_system *); | |
150 | static bool e_write (int, Lisp_Object, ptrdiff_t, ptrdiff_t, | |
151 | struct coding_system *); | |
ce51c54c | 152 | |
ec7adf26 | 153 | \f |
5d01d666 | 154 | void |
971de7fb | 155 | report_file_error (const char *string, Lisp_Object data) |
570d7624 JB |
156 | { |
157 | Lisp_Object errstring; | |
505ab9bc | 158 | int errorno = errno; |
d7259fdb | 159 | char *str; |
570d7624 | 160 | |
ca9c0567 | 161 | synchronize_system_messages_locale (); |
d7259fdb | 162 | str = strerror (errorno); |
d7ea76b4 | 163 | errstring = code_convert_string_norecord (build_unibyte_string (str), |
68c45bf0 PE |
164 | Vlocale_coding_system, 0); |
165 | ||
570d7624 | 166 | while (1) |
505ab9bc RS |
167 | switch (errorno) |
168 | { | |
169 | case EEXIST: | |
24b1ddad | 170 | xsignal (Qfile_already_exists, Fcons (errstring, data)); |
505ab9bc RS |
171 | break; |
172 | default: | |
173 | /* System error messages are capitalized. Downcase the initial | |
39824137 EZ |
174 | unless it is followed by a slash. (The slash case caters to |
175 | error messages that begin with "I/O" or, in German, "E/A".) */ | |
d5443ffd KH |
176 | if (STRING_MULTIBYTE (errstring) |
177 | && ! EQ (Faref (errstring, make_number (1)), make_number ('/'))) | |
6c0969ca KH |
178 | { |
179 | int c; | |
180 | ||
51b59d79 | 181 | str = SSDATA (errstring); |
5976c3fe | 182 | c = STRING_CHAR ((unsigned char *) str); |
5da9919f | 183 | Faset (errstring, make_number (0), make_number (downcase (c))); |
6c0969ca | 184 | } |
505ab9bc | 185 | |
24b1ddad | 186 | xsignal (Qfile_error, |
505ab9bc RS |
187 | Fcons (build_string (string), Fcons (errstring, data))); |
188 | } | |
570d7624 | 189 | } |
b5148e85 | 190 | |
b27a1703 | 191 | Lisp_Object |
971de7fb | 192 | close_file_unwind (Lisp_Object fd) |
b5148e85 | 193 | { |
68c45bf0 | 194 | emacs_close (XFASTINT (fd)); |
b27a1703 | 195 | return Qnil; |
b5148e85 | 196 | } |
a1d2b64a RS |
197 | |
198 | /* Restore point, having saved it as a marker. */ | |
199 | ||
5dcde606 | 200 | Lisp_Object |
971de7fb | 201 | restore_point_unwind (Lisp_Object location) |
a1d2b64a | 202 | { |
ec7adf26 | 203 | Fgoto_char (location); |
a1d2b64a | 204 | Fset_marker (location, Qnil, Qnil); |
b27a1703 | 205 | return Qnil; |
a1d2b64a | 206 | } |
59c94f03 | 207 | |
570d7624 | 208 | \f |
955cbe7b PE |
209 | static Lisp_Object Qexpand_file_name; |
210 | static Lisp_Object Qsubstitute_in_file_name; | |
211 | static Lisp_Object Qdirectory_file_name; | |
212 | static Lisp_Object Qfile_name_directory; | |
213 | static Lisp_Object Qfile_name_nondirectory; | |
214 | static Lisp_Object Qunhandled_file_name_directory; | |
215 | static Lisp_Object Qfile_name_as_directory; | |
216 | static Lisp_Object Qcopy_file; | |
217 | static Lisp_Object Qmake_directory_internal; | |
218 | static Lisp_Object Qmake_directory; | |
219 | static Lisp_Object Qdelete_directory_internal; | |
32f4334d | 220 | Lisp_Object Qdelete_file; |
955cbe7b PE |
221 | static Lisp_Object Qrename_file; |
222 | static Lisp_Object Qadd_name_to_file; | |
223 | static Lisp_Object Qmake_symbolic_link; | |
32f4334d | 224 | Lisp_Object Qfile_exists_p; |
955cbe7b PE |
225 | static Lisp_Object Qfile_executable_p; |
226 | static Lisp_Object Qfile_readable_p; | |
227 | static Lisp_Object Qfile_writable_p; | |
228 | static Lisp_Object Qfile_symlink_p; | |
229 | static Lisp_Object Qaccess_file; | |
32f4334d | 230 | Lisp_Object Qfile_directory_p; |
955cbe7b PE |
231 | static Lisp_Object Qfile_regular_p; |
232 | static Lisp_Object Qfile_accessible_directory_p; | |
233 | static Lisp_Object Qfile_modes; | |
234 | static Lisp_Object Qset_file_modes; | |
235 | static Lisp_Object Qset_file_times; | |
236 | static Lisp_Object Qfile_selinux_context; | |
237 | static Lisp_Object Qset_file_selinux_context; | |
238 | static Lisp_Object Qfile_newer_than_file_p; | |
32f4334d RS |
239 | Lisp_Object Qinsert_file_contents; |
240 | Lisp_Object Qwrite_region; | |
955cbe7b PE |
241 | static Lisp_Object Qverify_visited_file_modtime; |
242 | static Lisp_Object Qset_visited_file_modtime; | |
32f4334d | 243 | |
a7ca3326 | 244 | DEFUN ("find-file-name-handler", Ffind_file_name_handler, |
16a97296 | 245 | Sfind_file_name_handler, 2, 2, 0, |
8c1a1077 PJ |
246 | doc: /* Return FILENAME's handler function for OPERATION, if it has one. |
247 | Otherwise, return nil. | |
248 | A file name is handled if one of the regular expressions in | |
249 | `file-name-handler-alist' matches it. | |
250 | ||
251 | If OPERATION equals `inhibit-file-name-operation', then we ignore | |
252 | any handlers that are members of `inhibit-file-name-handlers', | |
253 | but we still do run any other handlers. This lets handlers | |
254 | use the standard functions without calling themselves recursively. */) | |
5842a27b | 255 | (Lisp_Object filename, Lisp_Object operation) |
32f4334d | 256 | { |
642ef245 | 257 | /* This function must not munge the match data. */ |
204ee271 | 258 | Lisp_Object chain, inhibited_handlers, result; |
d311d28c | 259 | ptrdiff_t pos = -1; |
642ef245 | 260 | |
204ee271 | 261 | result = Qnil; |
b7826503 | 262 | CHECK_STRING (filename); |
e4432095 | 263 | |
a65970a0 RS |
264 | if (EQ (operation, Vinhibit_file_name_operation)) |
265 | inhibited_handlers = Vinhibit_file_name_handlers; | |
266 | else | |
267 | inhibited_handlers = Qnil; | |
82c2d839 | 268 | |
93c30b5f | 269 | for (chain = Vfile_name_handler_alist; CONSP (chain); |
03699b14 | 270 | chain = XCDR (chain)) |
32f4334d RS |
271 | { |
272 | Lisp_Object elt; | |
03699b14 | 273 | elt = XCAR (chain); |
93c30b5f | 274 | if (CONSP (elt)) |
32f4334d | 275 | { |
f6c9b683 | 276 | Lisp_Object string = XCAR (elt); |
d311d28c | 277 | ptrdiff_t match_pos; |
f6c9b683 | 278 | Lisp_Object handler = XCDR (elt); |
68780e2a RS |
279 | Lisp_Object operations = Qnil; |
280 | ||
281 | if (SYMBOLP (handler)) | |
282 | operations = Fget (handler, Qoperations); | |
f6c9b683 | 283 | |
8d2ced53 | 284 | if (STRINGP (string) |
f6c9b683 RS |
285 | && (match_pos = fast_string_match (string, filename)) > pos |
286 | && (NILP (operations) || ! NILP (Fmemq (operation, operations)))) | |
a65970a0 | 287 | { |
f6c9b683 | 288 | Lisp_Object tem; |
a65970a0 | 289 | |
03699b14 | 290 | handler = XCDR (elt); |
a65970a0 RS |
291 | tem = Fmemq (handler, inhibited_handlers); |
292 | if (NILP (tem)) | |
8d2ced53 SM |
293 | { |
294 | result = handler; | |
295 | pos = match_pos; | |
296 | } | |
a65970a0 | 297 | } |
32f4334d | 298 | } |
642ef245 JB |
299 | |
300 | QUIT; | |
32f4334d | 301 | } |
8d2ced53 | 302 | return result; |
32f4334d RS |
303 | } |
304 | \f | |
a7ca3326 | 305 | DEFUN ("file-name-directory", Ffile_name_directory, Sfile_name_directory, |
8c1a1077 PJ |
306 | 1, 1, 0, |
307 | doc: /* Return the directory component in file name FILENAME. | |
308 | Return nil if FILENAME does not include a directory. | |
78379264 | 309 | Otherwise return a directory name. |
7c2fb837 | 310 | Given a Unix syntax file name, returns a string ending in slash. */) |
5842a27b | 311 | (Lisp_Object filename) |
570d7624 | 312 | { |
100c44b7 | 313 | #ifndef DOS_NT |
5976c3fe | 314 | register const char *beg; |
100c44b7 | 315 | #else |
5976c3fe | 316 | register char *beg; |
100c44b7 | 317 | #endif |
5976c3fe | 318 | register const char *p; |
0bf2eed2 | 319 | Lisp_Object handler; |
570d7624 | 320 | |
b7826503 | 321 | CHECK_STRING (filename); |
570d7624 | 322 | |
0bf2eed2 RS |
323 | /* If the file name has special constructs in it, |
324 | call the corresponding file handler. */ | |
3b7f6e60 | 325 | handler = Ffind_file_name_handler (filename, Qfile_name_directory); |
0bf2eed2 | 326 | if (!NILP (handler)) |
2375c96a CY |
327 | { |
328 | Lisp_Object handled_name = call2 (handler, Qfile_name_directory, | |
329 | filename); | |
330 | return STRINGP (handled_name) ? handled_name : Qnil; | |
331 | } | |
0bf2eed2 | 332 | |
199607e4 | 333 | #ifdef DOS_NT |
38182d90 | 334 | beg = alloca (SBYTES (filename) + 1); |
5976c3fe | 335 | memcpy (beg, SSDATA (filename), SBYTES (filename) + 1); |
0f3f018c | 336 | #else |
5976c3fe | 337 | beg = SSDATA (filename); |
199607e4 | 338 | #endif |
d5db4077 | 339 | p = beg + SBYTES (filename); |
570d7624 | 340 | |
199607e4 | 341 | while (p != beg && !IS_DIRECTORY_SEP (p[-1]) |
199607e4 | 342 | #ifdef DOS_NT |
e1dbe924 | 343 | /* only recognize drive specifier at the beginning */ |
ba14e174 RS |
344 | && !(p[-1] == ':' |
345 | /* handle the "/:d:foo" and "/:foo" cases correctly */ | |
346 | && ((p == beg + 2 && !IS_DIRECTORY_SEP (*beg)) | |
347 | || (p == beg + 4 && IS_DIRECTORY_SEP (*beg)))) | |
199607e4 | 348 | #endif |
570d7624 JB |
349 | ) p--; |
350 | ||
351 | if (p == beg) | |
352 | return Qnil; | |
5e570b75 | 353 | #ifdef DOS_NT |
4c3c22f3 | 354 | /* Expansion of "c:" to drive and default directory. */ |
ba14e174 | 355 | if (p[-1] == ':') |
4c3c22f3 | 356 | { |
4c3c22f3 | 357 | /* MAXPATHLEN+1 is guaranteed to be enough space for getdefdir. */ |
5976c3fe PE |
358 | char *res = alloca (MAXPATHLEN + 1); |
359 | char *r = res; | |
ba14e174 RS |
360 | |
361 | if (p == beg + 4 && IS_DIRECTORY_SEP (*beg) && beg[1] == ':') | |
362 | { | |
e99a530f | 363 | memcpy (res, beg, 2); |
ba14e174 RS |
364 | beg += 2; |
365 | r += 2; | |
366 | } | |
367 | ||
620f13b0 | 368 | if (getdefdir (c_toupper (*beg) - 'A' + 1, r)) |
4c3c22f3 | 369 | { |
199607e4 | 370 | if (!IS_DIRECTORY_SEP (res[strlen (res) - 1])) |
4c3c22f3 RS |
371 | strcat (res, "/"); |
372 | beg = res; | |
373 | p = beg + strlen (beg); | |
374 | } | |
375 | } | |
087fc47a | 376 | dostounix_filename (beg); |
5e570b75 | 377 | #endif /* DOS_NT */ |
60d67b83 | 378 | |
d7231f93 | 379 | return make_specified_string (beg, -1, p - beg, STRING_MULTIBYTE (filename)); |
570d7624 JB |
380 | } |
381 | ||
a7ca3326 | 382 | DEFUN ("file-name-nondirectory", Ffile_name_nondirectory, |
60d67b83 | 383 | Sfile_name_nondirectory, 1, 1, 0, |
8c1a1077 PJ |
384 | doc: /* Return file name FILENAME sans its directory. |
385 | For example, in a Unix-syntax file name, | |
386 | this is everything after the last slash, | |
387 | or the entire name if it contains no slash. */) | |
5842a27b | 388 | (Lisp_Object filename) |
570d7624 | 389 | { |
5976c3fe | 390 | register const char *beg, *p, *end; |
0bf2eed2 | 391 | Lisp_Object handler; |
570d7624 | 392 | |
b7826503 | 393 | CHECK_STRING (filename); |
570d7624 | 394 | |
0bf2eed2 RS |
395 | /* If the file name has special constructs in it, |
396 | call the corresponding file handler. */ | |
3b7f6e60 | 397 | handler = Ffind_file_name_handler (filename, Qfile_name_nondirectory); |
0bf2eed2 | 398 | if (!NILP (handler)) |
2375c96a CY |
399 | { |
400 | Lisp_Object handled_name = call2 (handler, Qfile_name_nondirectory, | |
401 | filename); | |
402 | if (STRINGP (handled_name)) | |
403 | return handled_name; | |
404 | error ("Invalid handler in `file-name-handler-alist'"); | |
405 | } | |
0bf2eed2 | 406 | |
5976c3fe | 407 | beg = SSDATA (filename); |
d5db4077 | 408 | end = p = beg + SBYTES (filename); |
570d7624 | 409 | |
199607e4 | 410 | while (p != beg && !IS_DIRECTORY_SEP (p[-1]) |
199607e4 | 411 | #ifdef DOS_NT |
e1dbe924 | 412 | /* only recognize drive specifier at beginning */ |
ba14e174 RS |
413 | && !(p[-1] == ':' |
414 | /* handle the "/:d:foo" case correctly */ | |
415 | && (p == beg + 2 || (p == beg + 4 && IS_DIRECTORY_SEP (*beg)))) | |
199607e4 | 416 | #endif |
60d67b83 RS |
417 | ) |
418 | p--; | |
570d7624 | 419 | |
d7231f93 | 420 | return make_specified_string (p, -1, end - p, STRING_MULTIBYTE (filename)); |
570d7624 | 421 | } |
642ef245 | 422 | |
a7ca3326 | 423 | DEFUN ("unhandled-file-name-directory", Funhandled_file_name_directory, |
60d67b83 | 424 | Sunhandled_file_name_directory, 1, 1, 0, |
8c1a1077 PJ |
425 | doc: /* Return a directly usable directory name somehow associated with FILENAME. |
426 | A `directly usable' directory name is one that may be used without the | |
427 | intervention of any file handler. | |
428 | If FILENAME is a directly usable file itself, return | |
429 | \(file-name-directory FILENAME). | |
ca319910 SM |
430 | If FILENAME refers to a file which is not accessible from a local process, |
431 | then this should return nil. | |
8c1a1077 PJ |
432 | The `call-process' and `start-process' functions use this function to |
433 | get a current directory to run processes in. */) | |
5842a27b | 434 | (Lisp_Object filename) |
642ef245 JB |
435 | { |
436 | Lisp_Object handler; | |
437 | ||
438 | /* If the file name has special constructs in it, | |
439 | call the corresponding file handler. */ | |
49307295 | 440 | handler = Ffind_file_name_handler (filename, Qunhandled_file_name_directory); |
642ef245 | 441 | if (!NILP (handler)) |
2375c96a CY |
442 | { |
443 | Lisp_Object handled_name = call2 (handler, Qunhandled_file_name_directory, | |
444 | filename); | |
445 | return STRINGP (handled_name) ? handled_name : Qnil; | |
446 | } | |
642ef245 JB |
447 | |
448 | return Ffile_name_directory (filename); | |
449 | } | |
450 | ||
c293e30c DA |
451 | /* Convert from file name SRC of length SRCLEN to directory name |
452 | in DST. On UNIX, just make sure there is a terminating /. | |
453 | Return the length of DST. */ | |
570d7624 | 454 | |
c293e30c DA |
455 | static ptrdiff_t |
456 | file_name_as_directory (char *dst, const char *src, ptrdiff_t srclen) | |
457 | { | |
458 | if (srclen == 0) | |
8aa3a244 | 459 | { |
c293e30c DA |
460 | dst[0] = '.'; |
461 | dst[1] = '/'; | |
462 | dst[2] = '\0'; | |
463 | return 2; | |
8aa3a244 RS |
464 | } |
465 | ||
c293e30c | 466 | strcpy (dst, src); |
93f4cf88 | 467 | |
c293e30c | 468 | if (!IS_DIRECTORY_SEP (dst[srclen - 1])) |
5e570b75 | 469 | { |
c293e30c DA |
470 | dst[srclen] = DIRECTORY_SEP; |
471 | dst[srclen + 1] = '\0'; | |
472 | srclen++; | |
5e570b75 | 473 | } |
199607e4 | 474 | #ifdef DOS_NT |
c293e30c | 475 | dostounix_filename (dst); |
199607e4 | 476 | #endif |
c293e30c | 477 | return srclen; |
570d7624 JB |
478 | } |
479 | ||
a7ca3326 | 480 | DEFUN ("file-name-as-directory", Ffile_name_as_directory, |
570d7624 | 481 | Sfile_name_as_directory, 1, 1, 0, |
0dac4f85 | 482 | doc: /* Return a string representing the file name FILE interpreted as a directory. |
8c1a1077 PJ |
483 | This operation exists because a directory is also a file, but its name as |
484 | a directory is different from its name as a file. | |
485 | The result can be used as the value of `default-directory' | |
486 | or passed as second argument to `expand-file-name'. | |
7c2fb837 | 487 | For a Unix-syntax file name, just appends a slash. */) |
5842a27b | 488 | (Lisp_Object file) |
570d7624 JB |
489 | { |
490 | char *buf; | |
c293e30c | 491 | ptrdiff_t length; |
0bf2eed2 | 492 | Lisp_Object handler; |
570d7624 | 493 | |
b7826503 | 494 | CHECK_STRING (file); |
265a9e55 | 495 | if (NILP (file)) |
570d7624 | 496 | return Qnil; |
0bf2eed2 RS |
497 | |
498 | /* If the file name has special constructs in it, | |
499 | call the corresponding file handler. */ | |
49307295 | 500 | handler = Ffind_file_name_handler (file, Qfile_name_as_directory); |
0bf2eed2 | 501 | if (!NILP (handler)) |
2375c96a CY |
502 | { |
503 | Lisp_Object handled_name = call2 (handler, Qfile_name_as_directory, | |
504 | file); | |
505 | if (STRINGP (handled_name)) | |
506 | return handled_name; | |
507 | error ("Invalid handler in `file-name-handler-alist'"); | |
508 | } | |
0bf2eed2 | 509 | |
38182d90 | 510 | buf = alloca (SBYTES (file) + 10); |
c293e30c DA |
511 | length = file_name_as_directory (buf, SSDATA (file), SBYTES (file)); |
512 | return make_specified_string (buf, -1, length, STRING_MULTIBYTE (file)); | |
570d7624 JB |
513 | } |
514 | \f | |
c293e30c DA |
515 | /* Convert from directory name SRC of length SRCLEN to |
516 | file name in DST. On UNIX, just make sure there isn't | |
517 | a terminating /. Return the length of DST. */ | |
570d7624 | 518 | |
c293e30c DA |
519 | static ptrdiff_t |
520 | directory_file_name (char *dst, char *src, ptrdiff_t srclen) | |
570d7624 | 521 | { |
570d7624 JB |
522 | /* Process as Unix format: just remove any final slash. |
523 | But leave "/" unchanged; do not change it to "". */ | |
524 | strcpy (dst, src); | |
c293e30c DA |
525 | if (srclen > 1 |
526 | && IS_DIRECTORY_SEP (dst[srclen - 1]) | |
4592782e | 527 | #ifdef DOS_NT |
c293e30c | 528 | && !IS_ANY_SEP (dst[srclen - 2]) |
4592782e RS |
529 | #endif |
530 | ) | |
c293e30c DA |
531 | { |
532 | dst[srclen - 1] = 0; | |
533 | srclen--; | |
534 | } | |
199607e4 | 535 | #ifdef DOS_NT |
087fc47a | 536 | dostounix_filename (dst); |
125feee8 | 537 | #endif |
c293e30c | 538 | return srclen; |
570d7624 JB |
539 | } |
540 | ||
a7ca3326 | 541 | DEFUN ("directory-file-name", Fdirectory_file_name, Sdirectory_file_name, |
8c1a1077 PJ |
542 | 1, 1, 0, |
543 | doc: /* Returns the file name of the directory named DIRECTORY. | |
544 | This is the name of the file that holds the data for the directory DIRECTORY. | |
545 | This operation exists because a directory is also a file, but its name as | |
546 | a directory is different from its name as a file. | |
7c2fb837 | 547 | In Unix-syntax, this function just removes the final slash. */) |
5842a27b | 548 | (Lisp_Object directory) |
570d7624 JB |
549 | { |
550 | char *buf; | |
c293e30c | 551 | ptrdiff_t length; |
0bf2eed2 | 552 | Lisp_Object handler; |
570d7624 | 553 | |
b7826503 | 554 | CHECK_STRING (directory); |
570d7624 | 555 | |
265a9e55 | 556 | if (NILP (directory)) |
570d7624 | 557 | return Qnil; |
0bf2eed2 RS |
558 | |
559 | /* If the file name has special constructs in it, | |
560 | call the corresponding file handler. */ | |
49307295 | 561 | handler = Ffind_file_name_handler (directory, Qdirectory_file_name); |
0bf2eed2 | 562 | if (!NILP (handler)) |
2375c96a CY |
563 | { |
564 | Lisp_Object handled_name = call2 (handler, Qdirectory_file_name, | |
565 | directory); | |
566 | if (STRINGP (handled_name)) | |
567 | return handled_name; | |
568 | error ("Invalid handler in `file-name-handler-alist'"); | |
569 | } | |
0bf2eed2 | 570 | |
38182d90 | 571 | buf = alloca (SBYTES (directory) + 20); |
c293e30c DA |
572 | length = directory_file_name (buf, SSDATA (directory), SBYTES (directory)); |
573 | return make_specified_string (buf, -1, length, STRING_MULTIBYTE (directory)); | |
570d7624 JB |
574 | } |
575 | ||
91433552 | 576 | static const char make_temp_name_tbl[64] = |
3ce839e4 RS |
577 | { |
578 | 'A','B','C','D','E','F','G','H', | |
579 | 'I','J','K','L','M','N','O','P', | |
580 | 'Q','R','S','T','U','V','W','X', | |
581 | 'Y','Z','a','b','c','d','e','f', | |
582 | 'g','h','i','j','k','l','m','n', | |
583 | 'o','p','q','r','s','t','u','v', | |
584 | 'w','x','y','z','0','1','2','3', | |
585 | '4','5','6','7','8','9','-','_' | |
586 | }; | |
cb613bb8 | 587 | |
3ce839e4 RS |
588 | static unsigned make_temp_name_count, make_temp_name_count_initialized_p; |
589 | ||
cb613bb8 | 590 | /* Value is a temporary file name starting with PREFIX, a string. |
efdc16c9 | 591 | |
cb613bb8 GM |
592 | The Emacs process number forms part of the result, so there is |
593 | no danger of generating a name being used by another process. | |
594 | In addition, this function makes an attempt to choose a name | |
595 | which has no existing file. To make this work, PREFIX should be | |
596 | an absolute file name. | |
efdc16c9 | 597 | |
f75d7a91 | 598 | BASE64_P means add the pid as 3 characters in base64 |
cb613bb8 GM |
599 | encoding. In this case, 6 characters will be added to PREFIX to |
600 | form the file name. Otherwise, if Emacs is running on a system | |
601 | with long file names, add the pid as a decimal number. | |
602 | ||
603 | This function signals an error if no unique file name could be | |
604 | generated. */ | |
605 | ||
606 | Lisp_Object | |
f75d7a91 | 607 | make_temp_name (Lisp_Object prefix, bool base64_p) |
570d7624 JB |
608 | { |
609 | Lisp_Object val; | |
0cedd530 | 610 | int len, clen; |
a81d11a3 | 611 | printmax_t pid; |
5976c3fe | 612 | char *p, *data; |
a81d11a3 | 613 | char pidbuf[INT_BUFSIZE_BOUND (printmax_t)]; |
3ce839e4 | 614 | int pidlen; |
efdc16c9 | 615 | |
b7826503 | 616 | CHECK_STRING (prefix); |
3ce839e4 RS |
617 | |
618 | /* VAL is created by adding 6 characters to PREFIX. The first | |
619 | three are the PID of this process, in base 64, and the second | |
620 | three are incremented if the file already exists. This ensures | |
621 | 262144 unique file names per PID per PREFIX. */ | |
622 | ||
93f4cf88 | 623 | pid = getpid (); |
3ce839e4 | 624 | |
cb613bb8 GM |
625 | if (base64_p) |
626 | { | |
627 | pidbuf[0] = make_temp_name_tbl[pid & 63], pid >>= 6; | |
628 | pidbuf[1] = make_temp_name_tbl[pid & 63], pid >>= 6; | |
629 | pidbuf[2] = make_temp_name_tbl[pid & 63], pid >>= 6; | |
630 | pidlen = 3; | |
631 | } | |
632 | else | |
633 | { | |
3ce839e4 | 634 | #ifdef HAVE_LONG_FILE_NAMES |
a81d11a3 | 635 | pidlen = sprintf (pidbuf, "%"pMd, pid); |
3a3bfb18 | 636 | #else |
cb613bb8 GM |
637 | pidbuf[0] = make_temp_name_tbl[pid & 63], pid >>= 6; |
638 | pidbuf[1] = make_temp_name_tbl[pid & 63], pid >>= 6; | |
639 | pidbuf[2] = make_temp_name_tbl[pid & 63], pid >>= 6; | |
640 | pidlen = 3; | |
3ce839e4 | 641 | #endif |
cb613bb8 | 642 | } |
efdc16c9 | 643 | |
0cedd530 SM |
644 | len = SBYTES (prefix); clen = SCHARS (prefix); |
645 | val = make_uninit_multibyte_string (clen + 3 + pidlen, len + 3 + pidlen); | |
646 | if (!STRING_MULTIBYTE (prefix)) | |
647 | STRING_SET_UNIBYTE (val); | |
5976c3fe PE |
648 | data = SSDATA (val); |
649 | memcpy (data, SSDATA (prefix), len); | |
3ce839e4 RS |
650 | p = data + len; |
651 | ||
72af86bd | 652 | memcpy (p, pidbuf, pidlen); |
3ce839e4 RS |
653 | p += pidlen; |
654 | ||
655 | /* Here we try to minimize useless stat'ing when this function is | |
656 | invoked many times successively with the same PREFIX. We achieve | |
657 | this by initializing count to a random value, and incrementing it | |
f6a492a9 RS |
658 | afterwards. |
659 | ||
660 | We don't want make-temp-name to be called while dumping, | |
661 | because then make_temp_name_count_initialized_p would get set | |
662 | and then make_temp_name_count would not be set when Emacs starts. */ | |
663 | ||
3ce839e4 RS |
664 | if (!make_temp_name_count_initialized_p) |
665 | { | |
327eeec8 | 666 | make_temp_name_count = time (NULL); |
3ce839e4 RS |
667 | make_temp_name_count_initialized_p = 1; |
668 | } | |
669 | ||
670 | while (1) | |
671 | { | |
8a7777fc | 672 | unsigned num = make_temp_name_count; |
3ce839e4 RS |
673 | |
674 | p[0] = make_temp_name_tbl[num & 63], num >>= 6; | |
675 | p[1] = make_temp_name_tbl[num & 63], num >>= 6; | |
676 | p[2] = make_temp_name_tbl[num & 63], num >>= 6; | |
677 | ||
8a7777fc RS |
678 | /* Poor man's congruential RN generator. Replace with |
679 | ++make_temp_name_count for debugging. */ | |
680 | make_temp_name_count += 25229; | |
681 | make_temp_name_count %= 225307; | |
682 | ||
bb385a92 | 683 | if (!check_existing (data)) |
3ce839e4 RS |
684 | { |
685 | /* We want to return only if errno is ENOENT. */ | |
686 | if (errno == ENOENT) | |
687 | return val; | |
688 | else | |
689 | /* The error here is dubious, but there is little else we | |
690 | can do. The alternatives are to return nil, which is | |
691 | as bad as (and in many cases worse than) throwing the | |
692 | error, or to ignore the error, which will likely result | |
8a7777fc | 693 | in looping through 225307 stat's, which is not only |
410ed5c3 PE |
694 | dog-slow, but also useless since eventually nil would |
695 | have to be returned anyway. */ | |
9869bb0b | 696 | report_file_error ("Cannot create temporary name for prefix", |
3ce839e4 RS |
697 | Fcons (prefix, Qnil)); |
698 | /* not reached */ | |
699 | } | |
700 | } | |
570d7624 | 701 | } |
3ce839e4 | 702 | |
cb613bb8 GM |
703 | |
704 | DEFUN ("make-temp-name", Fmake_temp_name, Smake_temp_name, 1, 1, 0, | |
8c1a1077 PJ |
705 | doc: /* Generate temporary file name (string) starting with PREFIX (a string). |
706 | The Emacs process number forms part of the result, | |
707 | so there is no danger of generating a name being used by another process. | |
708 | ||
709 | In addition, this function makes an attempt to choose a name | |
710 | which has no existing file. To make this work, | |
711 | PREFIX should be an absolute file name. | |
712 | ||
713 | There is a race condition between calling `make-temp-name' and creating the | |
714 | file which opens all kinds of security holes. For that reason, you should | |
f9e6f049 RS |
715 | probably use `make-temp-file' instead, except in three circumstances: |
716 | ||
717 | * If you are creating the file in the user's home directory. | |
718 | * If you are creating a directory rather than an ordinary file. | |
719 | * If you are taking special precautions as `make-temp-file' does. */) | |
5842a27b | 720 | (Lisp_Object prefix) |
cb613bb8 GM |
721 | { |
722 | return make_temp_name (prefix, 0); | |
723 | } | |
724 | ||
725 | ||
570d7624 | 726 | \f |
a7ca3326 | 727 | DEFUN ("expand-file-name", Fexpand_file_name, Sexpand_file_name, 1, 2, 0, |
8c1a1077 PJ |
728 | doc: /* Convert filename NAME to absolute, and canonicalize it. |
729 | Second arg DEFAULT-DIRECTORY is directory to start with if NAME is relative | |
78379264 | 730 | \(does not start with slash or tilde); if DEFAULT-DIRECTORY is nil or missing, |
a4e03fe5 | 731 | the current buffer's value of `default-directory' is used. |
1b2a627f EZ |
732 | NAME should be a string that is a valid file name for the underlying |
733 | filesystem. | |
8c1a1077 PJ |
734 | File name components that are `.' are removed, and |
735 | so are file name components followed by `..', along with the `..' itself; | |
736 | note that these simplifications are done without checking the resulting | |
737 | file names in the file system. | |
15579471 EZ |
738 | Multiple consecutive slashes are collapsed into a single slash, |
739 | except at the beginning of the file name when they are significant (e.g., | |
740 | UNC file names on MS-Windows.) | |
8c1a1077 PJ |
741 | An initial `~/' expands to your home directory. |
742 | An initial `~USER/' expands to USER's home directory. | |
07114296 CY |
743 | See also the function `substitute-in-file-name'. |
744 | ||
745 | For technical reasons, this function can return correct but | |
746 | non-intuitive results for the root directory; for instance, | |
747 | \(expand-file-name ".." "/") returns "/..". For this reason, use | |
15579471 | 748 | \(directory-file-name (file-name-directory dirname)) to traverse a |
07114296 | 749 | filesystem tree, not (expand-file-name ".." dirname). */) |
5842a27b | 750 | (Lisp_Object name, Lisp_Object default_directory) |
570d7624 | 751 | { |
5b5a2ea1 SM |
752 | /* These point to SDATA and need to be careful with string-relocation |
753 | during GC (via DECODE_FILE). */ | |
2893f146 PE |
754 | char *nm; |
755 | const char *newdir; | |
5b5a2ea1 | 756 | /* This should only point to alloca'd data. */ |
5976c3fe | 757 | char *target; |
199607e4 | 758 | |
93f4cf88 | 759 | ptrdiff_t tlen; |
570d7624 | 760 | struct passwd *pw; |
5e570b75 | 761 | #ifdef DOS_NT |
199607e4 | 762 | int drive = 0; |
f75d7a91 PE |
763 | bool collapse_newdir = 1; |
764 | bool is_escaped = 0; | |
5e570b75 | 765 | #endif /* DOS_NT */ |
93f4cf88 | 766 | ptrdiff_t length; |
2375c96a | 767 | Lisp_Object handler, result, handled_name; |
fce31d69 | 768 | bool multibyte; |
9c06a1f3 | 769 | Lisp_Object hdir; |
199607e4 | 770 | |
b7826503 | 771 | CHECK_STRING (name); |
570d7624 | 772 | |
0bf2eed2 RS |
773 | /* If the file name has special constructs in it, |
774 | call the corresponding file handler. */ | |
49307295 | 775 | handler = Ffind_file_name_handler (name, Qexpand_file_name); |
0bf2eed2 | 776 | if (!NILP (handler)) |
2375c96a CY |
777 | { |
778 | handled_name = call3 (handler, Qexpand_file_name, | |
779 | name, default_directory); | |
780 | if (STRINGP (handled_name)) | |
781 | return handled_name; | |
782 | error ("Invalid handler in `file-name-handler-alist'"); | |
783 | } | |
784 | ||
58fc9587 | 785 | |
3b7f6e60 EN |
786 | /* Use the buffer's default-directory if DEFAULT_DIRECTORY is omitted. */ |
787 | if (NILP (default_directory)) | |
4b4deea2 | 788 | default_directory = BVAR (current_buffer, directory); |
82330e7f | 789 | if (! STRINGP (default_directory)) |
dd693537 EZ |
790 | { |
791 | #ifdef DOS_NT | |
792 | /* "/" is not considered a root directory on DOS_NT, so using "/" | |
793 | here causes an infinite recursion in, e.g., the following: | |
794 | ||
795 | (let (default-directory) | |
796 | (expand-file-name "a")) | |
797 | ||
798 | To avoid this, we set default_directory to the root of the | |
799 | current drive. */ | |
dd693537 EZ |
800 | default_directory = build_string (emacs_root_dir ()); |
801 | #else | |
802 | default_directory = build_string ("/"); | |
803 | #endif | |
804 | } | |
58fc9587 | 805 | |
3b7f6e60 | 806 | if (!NILP (default_directory)) |
273e0829 | 807 | { |
3b7f6e60 | 808 | handler = Ffind_file_name_handler (default_directory, Qexpand_file_name); |
273e0829 | 809 | if (!NILP (handler)) |
2375c96a CY |
810 | { |
811 | handled_name = call3 (handler, Qexpand_file_name, | |
812 | name, default_directory); | |
813 | if (STRINGP (handled_name)) | |
814 | return handled_name; | |
815 | error ("Invalid handler in `file-name-handler-alist'"); | |
816 | } | |
273e0829 | 817 | } |
0bf2eed2 | 818 | |
5b5a2ea1 | 819 | { |
5976c3fe | 820 | char *o = SSDATA (default_directory); |
5b5a2ea1 SM |
821 | |
822 | /* Make sure DEFAULT_DIRECTORY is properly expanded. | |
823 | It would be better to do this down below where we actually use | |
824 | default_directory. Unfortunately, calling Fexpand_file_name recursively | |
825 | could invoke GC, and the strings might be relocated. This would | |
826 | be annoying because we have pointers into strings lying around | |
827 | that would need adjusting, and people would add new pointers to | |
828 | the code and forget to adjust them, resulting in intermittent bugs. | |
829 | Putting this call here avoids all that crud. | |
830 | ||
831 | The EQ test avoids infinite recursion. */ | |
832 | if (! NILP (default_directory) && !EQ (default_directory, name) | |
833 | /* Save time in some common cases - as long as default_directory | |
834 | is not relative, it can be canonicalized with name below (if it | |
835 | is needed at all) without requiring it to be expanded now. */ | |
01937013 | 836 | #ifdef DOS_NT |
5b5a2ea1 SM |
837 | /* Detect MSDOS file names with drive specifiers. */ |
838 | && ! (IS_DRIVE (o[0]) && IS_DEVICE_SEP (o[1]) | |
839 | && IS_DIRECTORY_SEP (o[2])) | |
199607e4 | 840 | #ifdef WINDOWSNT |
5b5a2ea1 SM |
841 | /* Detect Windows file names in UNC format. */ |
842 | && ! (IS_DIRECTORY_SEP (o[0]) && IS_DIRECTORY_SEP (o[1])) | |
01937013 | 843 | #endif |
199607e4 RS |
844 | #else /* not DOS_NT */ |
845 | /* Detect Unix absolute file names (/... alone is not absolute on | |
846 | DOS or Windows). */ | |
5b5a2ea1 | 847 | && ! (IS_DIRECTORY_SEP (o[0])) |
199607e4 | 848 | #endif /* not DOS_NT */ |
5b5a2ea1 SM |
849 | ) |
850 | { | |
851 | struct gcpro gcpro1; | |
f14b1c68 | 852 | |
5b5a2ea1 SM |
853 | GCPRO1 (name); |
854 | default_directory = Fexpand_file_name (default_directory, Qnil); | |
855 | UNGCPRO; | |
856 | } | |
857 | } | |
2b046a72 | 858 | multibyte = STRING_MULTIBYTE (name); |
6d060996 KH |
859 | if (multibyte != STRING_MULTIBYTE (default_directory)) |
860 | { | |
861 | if (multibyte) | |
862 | default_directory = string_to_multibyte (default_directory); | |
863 | else | |
864 | { | |
865 | name = string_to_multibyte (name); | |
866 | multibyte = 1; | |
867 | } | |
868 | } | |
869 | ||
e8d32c7e | 870 | /* Make a local copy of nm[] to protect it from GC in DECODE_FILE below. */ |
38182d90 | 871 | nm = alloca (SBYTES (name) + 1); |
5976c3fe | 872 | memcpy (nm, SSDATA (name), SBYTES (name) + 1); |
199607e4 | 873 | |
565f0b98 | 874 | #ifdef DOS_NT |
f0f95d31 RS |
875 | /* Note if special escape prefix is present, but remove for now. */ |
876 | if (nm[0] == '/' && nm[1] == ':') | |
877 | { | |
878 | is_escaped = 1; | |
879 | nm += 2; | |
880 | } | |
881 | ||
199607e4 | 882 | /* Find and remove drive specifier if present; this makes nm absolute |
f0f95d31 RS |
883 | even if the rest of the name appears to be relative. Only look for |
884 | drive specifier at the beginning. */ | |
885 | if (IS_DRIVE (nm[0]) && IS_DEVICE_SEP (nm[1])) | |
886 | { | |
5976c3fe | 887 | drive = (unsigned char) nm[0]; |
f0f95d31 RS |
888 | nm += 2; |
889 | } | |
bb1ff1f4 GV |
890 | |
891 | #ifdef WINDOWSNT | |
892 | /* If we see "c://somedir", we want to strip the first slash after the | |
893 | colon when stripping the drive letter. Otherwise, this expands to | |
894 | "//somedir". */ | |
895 | if (drive && IS_DIRECTORY_SEP (nm[0]) && IS_DIRECTORY_SEP (nm[1])) | |
896 | nm++; | |
4c3c22f3 | 897 | |
e8d32c7e | 898 | /* Discard any previous drive specifier if nm is now in UNC format. */ |
199607e4 RS |
899 | if (IS_DIRECTORY_SEP (nm[0]) && IS_DIRECTORY_SEP (nm[1])) |
900 | { | |
901 | drive = 0; | |
902 | } | |
5b5a2ea1 SM |
903 | #endif /* WINDOWSNT */ |
904 | #endif /* DOS_NT */ | |
199607e4 | 905 | |
214378ec GM |
906 | /* If nm is absolute, look for `/./' or `/../' or `//''sequences; if |
907 | none are found, we can probably return right away. We will avoid | |
908 | allocating a new string if name is already fully expanded. */ | |
570d7624 | 909 | if ( |
5e570b75 | 910 | IS_DIRECTORY_SEP (nm[0]) |
199607e4 | 911 | #ifdef MSDOS |
f0f95d31 | 912 | && drive && !is_escaped |
199607e4 RS |
913 | #endif |
914 | #ifdef WINDOWSNT | |
f0f95d31 | 915 | && (drive || IS_DIRECTORY_SEP (nm[1])) && !is_escaped |
199607e4 | 916 | #endif |
570d7624 JB |
917 | ) |
918 | { | |
f14b1c68 JB |
919 | /* If it turns out that the filename we want to return is just a |
920 | suffix of FILENAME, we don't need to go through and edit | |
921 | things; we just need to construct a new string using data | |
f75d7a91 PE |
922 | starting at the middle of FILENAME. If we set LOSE, that |
923 | means we've discovered that we can't do that cool trick. */ | |
924 | bool lose = 0; | |
5976c3fe | 925 | char *p = nm; |
f14b1c68 | 926 | |
570d7624 JB |
927 | while (*p) |
928 | { | |
199607e4 | 929 | /* Since we know the name is absolute, we can assume that each |
c77d647e JB |
930 | element starts with a "/". */ |
931 | ||
c77d647e | 932 | /* "." and ".." are hairy. */ |
5e570b75 | 933 | if (IS_DIRECTORY_SEP (p[0]) |
c77d647e | 934 | && p[1] == '.' |
5e570b75 | 935 | && (IS_DIRECTORY_SEP (p[2]) |
c77d647e | 936 | || p[2] == 0 |
5e570b75 | 937 | || (p[2] == '.' && (IS_DIRECTORY_SEP (p[3]) |
c77d647e | 938 | || p[3] == 0)))) |
570d7624 | 939 | lose = 1; |
214378ec GM |
940 | /* We want to replace multiple `/' in a row with a single |
941 | slash. */ | |
942 | else if (p > nm | |
943 | && IS_DIRECTORY_SEP (p[0]) | |
944 | && IS_DIRECTORY_SEP (p[1])) | |
945 | lose = 1; | |
570d7624 JB |
946 | p++; |
947 | } | |
948 | if (!lose) | |
949 | { | |
199607e4 | 950 | #ifdef DOS_NT |
087fc47a JB |
951 | /* Make sure directories are all separated with /, but |
952 | avoid allocation of a new string when not required. */ | |
953 | dostounix_filename (nm); | |
199607e4 RS |
954 | #ifdef WINDOWSNT |
955 | if (IS_DIRECTORY_SEP (nm[1])) | |
956 | { | |
42a5b22f | 957 | if (strcmp (nm, SSDATA (name)) != 0) |
2b046a72 | 958 | name = make_specified_string (nm, -1, strlen (nm), multibyte); |
199607e4 RS |
959 | } |
960 | else | |
961 | #endif | |
e8d32c7e | 962 | /* Drive must be set, so this is okay. */ |
5976c3fe | 963 | if (strcmp (nm - 2, SSDATA (name)) != 0) |
199607e4 | 964 | { |
3f817c73 KH |
965 | char temp[] = " :"; |
966 | ||
2b046a72 | 967 | name = make_specified_string (nm, -1, p - nm, multibyte); |
3f817c73 KH |
968 | temp[0] = DRIVE_LETTER (drive); |
969 | name = concat2 (build_string (temp), name); | |
199607e4 RS |
970 | } |
971 | return name; | |
972 | #else /* not DOS_NT */ | |
42a5b22f | 973 | if (strcmp (nm, SSDATA (name)) == 0) |
570d7624 | 974 | return name; |
2b046a72 | 975 | return make_specified_string (nm, -1, strlen (nm), multibyte); |
5e570b75 | 976 | #endif /* not DOS_NT */ |
570d7624 JB |
977 | } |
978 | } | |
979 | ||
199607e4 RS |
980 | /* At this point, nm might or might not be an absolute file name. We |
981 | need to expand ~ or ~user if present, otherwise prefix nm with | |
982 | default_directory if nm is not absolute, and finally collapse /./ | |
983 | and /foo/../ sequences. | |
984 | ||
985 | We set newdir to be the appropriate prefix if one is needed: | |
986 | - the relevant user directory if nm starts with ~ or ~user | |
987 | - the specified drive's working dir (DOS/NT only) if nm does not | |
988 | start with / | |
989 | - the value of default_directory. | |
990 | ||
991 | Note that these prefixes are not guaranteed to be absolute (except | |
992 | for the working dir of a drive). Therefore, to ensure we always | |
993 | return an absolute name, if the final prefix is not absolute we | |
994 | append it to the current working directory. */ | |
570d7624 JB |
995 | |
996 | newdir = 0; | |
997 | ||
998 | if (nm[0] == '~') /* prefix ~ */ | |
c77d647e | 999 | { |
5e570b75 | 1000 | if (IS_DIRECTORY_SEP (nm[1]) |
c77d647e JB |
1001 | || nm[1] == 0) /* ~ by itself */ |
1002 | { | |
6d557778 EZ |
1003 | Lisp_Object tem; |
1004 | ||
5976c3fe PE |
1005 | if (!(newdir = egetenv ("HOME"))) |
1006 | newdir = ""; | |
199607e4 | 1007 | nm++; |
e8d32c7e | 1008 | /* `egetenv' may return a unibyte string, which will bite us since |
6d557778 | 1009 | we expect the directory to be multibyte. */ |
9c06a1f3 EZ |
1010 | tem = build_string (newdir); |
1011 | if (!STRING_MULTIBYTE (tem)) | |
1012 | { | |
1013 | hdir = DECODE_FILE (tem); | |
5976c3fe | 1014 | newdir = SSDATA (hdir); |
9c06a1f3 | 1015 | } |
5e570b75 | 1016 | #ifdef DOS_NT |
9a1dc3be | 1017 | collapse_newdir = 0; |
4c3c22f3 | 1018 | #endif |
c77d647e JB |
1019 | } |
1020 | else /* ~user/filename */ | |
1021 | { | |
5976c3fe | 1022 | char *o, *p; |
7c2fb837 | 1023 | for (p = nm; *p && (!IS_DIRECTORY_SEP (*p)); p++); |
5b5a2ea1 | 1024 | o = alloca (p - nm + 1); |
72af86bd | 1025 | memcpy (o, nm, p - nm); |
c77d647e JB |
1026 | o [p - nm] = 0; |
1027 | ||
4d7e6e51 | 1028 | block_input (); |
c77d647e | 1029 | pw = (struct passwd *) getpwnam (o + 1); |
4d7e6e51 | 1030 | unblock_input (); |
c77d647e JB |
1031 | if (pw) |
1032 | { | |
5976c3fe | 1033 | newdir = pw->pw_dir; |
c77d647e | 1034 | nm = p; |
199607e4 | 1035 | #ifdef DOS_NT |
9a1dc3be | 1036 | collapse_newdir = 0; |
199607e4 | 1037 | #endif |
c77d647e | 1038 | } |
e5d77022 | 1039 | |
c77d647e JB |
1040 | /* If we don't find a user of that name, leave the name |
1041 | unchanged; don't move nm forward to p. */ | |
1042 | } | |
1043 | } | |
570d7624 | 1044 | |
5e570b75 | 1045 | #ifdef DOS_NT |
199607e4 RS |
1046 | /* On DOS and Windows, nm is absolute if a drive name was specified; |
1047 | use the drive's current directory as the prefix if needed. */ | |
1048 | if (!newdir && drive) | |
1049 | { | |
e8d32c7e | 1050 | /* Get default directory if needed to make nm absolute. */ |
2893f146 | 1051 | char *adir = NULL; |
199607e4 RS |
1052 | if (!IS_DIRECTORY_SEP (nm[0])) |
1053 | { | |
2893f146 | 1054 | adir = alloca (MAXPATHLEN + 1); |
620f13b0 | 1055 | if (!getdefdir (c_toupper (drive) - 'A' + 1, adir)) |
2893f146 | 1056 | adir = NULL; |
199607e4 | 1057 | } |
2893f146 | 1058 | if (!adir) |
199607e4 | 1059 | { |
e8d32c7e | 1060 | /* Either nm starts with /, or drive isn't mounted. */ |
2893f146 PE |
1061 | adir = alloca (4); |
1062 | adir[0] = DRIVE_LETTER (drive); | |
1063 | adir[1] = ':'; | |
1064 | adir[2] = '/'; | |
1065 | adir[3] = 0; | |
199607e4 | 1066 | } |
2893f146 | 1067 | newdir = adir; |
199607e4 | 1068 | } |
5e570b75 | 1069 | #endif /* DOS_NT */ |
199607e4 RS |
1070 | |
1071 | /* Finally, if no prefix has been specified and nm is not absolute, | |
e8d32c7e | 1072 | then it must be expanded relative to default_directory. */ |
199607e4 | 1073 | |
34097368 | 1074 | if (1 |
199607e4 | 1075 | #ifndef DOS_NT |
e8d32c7e | 1076 | /* /... alone is not absolute on DOS and Windows. */ |
34097368 | 1077 | && !IS_DIRECTORY_SEP (nm[0]) |
199607e4 RS |
1078 | #endif |
1079 | #ifdef WINDOWSNT | |
34097368 | 1080 | && !(IS_DIRECTORY_SEP (nm[0]) && IS_DIRECTORY_SEP (nm[1])) |
199607e4 | 1081 | #endif |
570d7624 JB |
1082 | && !newdir) |
1083 | { | |
5976c3fe | 1084 | newdir = SSDATA (default_directory); |
f0f95d31 RS |
1085 | #ifdef DOS_NT |
1086 | /* Note if special escape prefix is present, but remove for now. */ | |
1087 | if (newdir[0] == '/' && newdir[1] == ':') | |
1088 | { | |
1089 | is_escaped = 1; | |
1090 | newdir += 2; | |
1091 | } | |
1092 | #endif | |
570d7624 JB |
1093 | } |
1094 | ||
5e570b75 | 1095 | #ifdef DOS_NT |
199607e4 RS |
1096 | if (newdir) |
1097 | { | |
e8d32c7e | 1098 | /* First ensure newdir is an absolute name. */ |
199607e4 RS |
1099 | if ( |
1100 | /* Detect MSDOS file names with drive specifiers. */ | |
1101 | ! (IS_DRIVE (newdir[0]) | |
1102 | && IS_DEVICE_SEP (newdir[1]) && IS_DIRECTORY_SEP (newdir[2])) | |
1103 | #ifdef WINDOWSNT | |
1104 | /* Detect Windows file names in UNC format. */ | |
1105 | && ! (IS_DIRECTORY_SEP (newdir[0]) && IS_DIRECTORY_SEP (newdir[1])) | |
1106 | #endif | |
1107 | ) | |
1108 | { | |
1109 | /* Effectively, let newdir be (expand-file-name newdir cwd). | |
1110 | Because of the admonition against calling expand-file-name | |
1111 | when we have pointers into lisp strings, we accomplish this | |
1112 | indirectly by prepending newdir to nm if necessary, and using | |
e8d32c7e | 1113 | cwd (or the wd of newdir's drive) as the new newdir. */ |
2893f146 | 1114 | char *adir; |
c70a4df6 | 1115 | if (IS_DRIVE (newdir[0]) && IS_DEVICE_SEP (newdir[1])) |
199607e4 | 1116 | { |
5976c3fe | 1117 | drive = (unsigned char) newdir[0]; |
199607e4 RS |
1118 | newdir += 2; |
1119 | } | |
1120 | if (!IS_DIRECTORY_SEP (nm[0])) | |
1121 | { | |
c293e30c DA |
1122 | ptrdiff_t newlen = strlen (newdir); |
1123 | char *tmp = alloca (newlen + strlen (nm) + 2); | |
1124 | file_name_as_directory (tmp, newdir, newlen); | |
199607e4 RS |
1125 | strcat (tmp, nm); |
1126 | nm = tmp; | |
1127 | } | |
2893f146 | 1128 | adir = alloca (MAXPATHLEN + 1); |
199607e4 RS |
1129 | if (drive) |
1130 | { | |
620f13b0 | 1131 | if (!getdefdir (c_toupper (drive) - 'A' + 1, adir)) |
199607e4 RS |
1132 | newdir = "/"; |
1133 | } | |
1134 | else | |
2893f146 PE |
1135 | getwd (adir); |
1136 | newdir = adir; | |
199607e4 RS |
1137 | } |
1138 | ||
e8d32c7e | 1139 | /* Strip off drive name from prefix, if present. */ |
c70a4df6 | 1140 | if (IS_DRIVE (newdir[0]) && IS_DEVICE_SEP (newdir[1])) |
199607e4 RS |
1141 | { |
1142 | drive = newdir[0]; | |
1143 | newdir += 2; | |
1144 | } | |
1145 | ||
1146 | /* Keep only a prefix from newdir if nm starts with slash | |
82330e7f | 1147 | (//server/share for UNC, nothing otherwise). */ |
9a1dc3be | 1148 | if (IS_DIRECTORY_SEP (nm[0]) && collapse_newdir) |
199607e4 RS |
1149 | { |
1150 | #ifdef WINDOWSNT | |
1151 | if (IS_DIRECTORY_SEP (newdir[0]) && IS_DIRECTORY_SEP (newdir[1])) | |
1152 | { | |
2893f146 PE |
1153 | char *adir = strcpy (alloca (strlen (newdir) + 1), newdir); |
1154 | char *p = adir + 2; | |
199607e4 RS |
1155 | while (*p && !IS_DIRECTORY_SEP (*p)) p++; |
1156 | p++; | |
1157 | while (*p && !IS_DIRECTORY_SEP (*p)) p++; | |
1158 | *p = 0; | |
2893f146 | 1159 | newdir = adir; |
199607e4 RS |
1160 | } |
1161 | else | |
1162 | #endif | |
1163 | newdir = ""; | |
1164 | } | |
1165 | } | |
5e570b75 | 1166 | #endif /* DOS_NT */ |
199607e4 RS |
1167 | |
1168 | if (newdir) | |
bfb61299 | 1169 | { |
57676091 | 1170 | /* Get rid of any slash at the end of newdir, unless newdir is |
f0f95d31 | 1171 | just / or // (an incomplete UNC name). */ |
199607e4 | 1172 | length = strlen (newdir); |
c293e30c | 1173 | tlen = length + 1; |
f0f95d31 | 1174 | if (length > 1 && IS_DIRECTORY_SEP (newdir[length - 1]) |
57676091 RS |
1175 | #ifdef WINDOWSNT |
1176 | && !(length == 2 && IS_DIRECTORY_SEP (newdir[0])) | |
1177 | #endif | |
1178 | ) | |
bfb61299 | 1179 | { |
38182d90 | 1180 | char *temp = alloca (length); |
72af86bd | 1181 | memcpy (temp, newdir, length - 1); |
bfb61299 | 1182 | temp[length - 1] = 0; |
c293e30c | 1183 | length--; |
bfb61299 JB |
1184 | newdir = temp; |
1185 | } | |
bfb61299 JB |
1186 | } |
1187 | else | |
c293e30c DA |
1188 | { |
1189 | length = 0; | |
1190 | tlen = 0; | |
1191 | } | |
570d7624 | 1192 | |
e8d32c7e | 1193 | /* Now concatenate the directory and name to new space in the stack frame. */ |
bfb61299 | 1194 | tlen += strlen (nm) + 1; |
5e570b75 | 1195 | #ifdef DOS_NT |
f0f95d31 RS |
1196 | /* Reserve space for drive specifier and escape prefix, since either |
1197 | or both may need to be inserted. (The Microsoft x86 compiler | |
5e570b75 | 1198 | produces incorrect code if the following two lines are combined.) */ |
38182d90 | 1199 | target = alloca (tlen + 4); |
f0f95d31 | 1200 | target += 4; |
5e570b75 | 1201 | #else /* not DOS_NT */ |
38182d90 | 1202 | target = alloca (tlen); |
5e570b75 | 1203 | #endif /* not DOS_NT */ |
570d7624 JB |
1204 | *target = 0; |
1205 | ||
1206 | if (newdir) | |
1207 | { | |
5e570b75 | 1208 | if (nm[0] == 0 || IS_DIRECTORY_SEP (nm[0])) |
f5321b5c | 1209 | { |
3ea2d8b2 | 1210 | #ifdef DOS_NT |
f5321b5c RS |
1211 | /* If newdir is effectively "C:/", then the drive letter will have |
1212 | been stripped and newdir will be "/". Concatenating with an | |
1213 | absolute directory in nm produces "//", which will then be | |
1214 | incorrectly treated as a network share. Ignore newdir in | |
1215 | this case (keeping the drive letter). */ | |
efdc16c9 | 1216 | if (!(drive && nm[0] && IS_DIRECTORY_SEP (newdir[0]) |
f5321b5c RS |
1217 | && newdir[1] == '\0')) |
1218 | #endif | |
1219 | strcpy (target, newdir); | |
1220 | } | |
570d7624 | 1221 | else |
c293e30c | 1222 | file_name_as_directory (target, newdir, length); |
570d7624 JB |
1223 | } |
1224 | ||
1225 | strcat (target, nm); | |
199607e4 | 1226 | |
214378ec GM |
1227 | /* Now canonicalize by removing `//', `/.' and `/foo/..' if they |
1228 | appear. */ | |
5b5a2ea1 | 1229 | { |
5976c3fe PE |
1230 | char *p = target; |
1231 | char *o = target; | |
570d7624 | 1232 | |
5b5a2ea1 SM |
1233 | while (*p) |
1234 | { | |
5b5a2ea1 SM |
1235 | if (!IS_DIRECTORY_SEP (*p)) |
1236 | { | |
1237 | *o++ = *p++; | |
1238 | } | |
1239 | else if (p[1] == '.' | |
1240 | && (IS_DIRECTORY_SEP (p[2]) | |
1241 | || p[2] == 0)) | |
1242 | { | |
1243 | /* If "/." is the entire filename, keep the "/". Otherwise, | |
1244 | just delete the whole "/.". */ | |
1245 | if (o == target && p[2] == '\0') | |
1246 | *o++ = *p; | |
1247 | p += 2; | |
1248 | } | |
1249 | else if (p[1] == '.' && p[2] == '.' | |
1250 | /* `/../' is the "superroot" on certain file systems. | |
1251 | Turned off on DOS_NT systems because they have no | |
1252 | "superroot" and because this causes us to produce | |
1253 | file names like "d:/../foo" which fail file-related | |
1254 | functions of the underlying OS. (To reproduce, try a | |
1255 | long series of "../../" in default_directory, longer | |
1256 | than the number of levels from the root.) */ | |
aa4060b9 | 1257 | #ifndef DOS_NT |
5b5a2ea1 | 1258 | && o != target |
aa4060b9 | 1259 | #endif |
5b5a2ea1 SM |
1260 | && (IS_DIRECTORY_SEP (p[3]) || p[3] == 0)) |
1261 | { | |
972ed246 | 1262 | #ifdef WINDOWSNT |
5976c3fe | 1263 | char *prev_o = o; |
972ed246 | 1264 | #endif |
5b5a2ea1 SM |
1265 | while (o != target && (--o) && !IS_DIRECTORY_SEP (*o)) |
1266 | ; | |
972ed246 JR |
1267 | #ifdef WINDOWSNT |
1268 | /* Don't go below server level in UNC filenames. */ | |
1269 | if (o == target + 1 && IS_DIRECTORY_SEP (*o) | |
1270 | && IS_DIRECTORY_SEP (*target)) | |
1271 | o = prev_o; | |
1272 | else | |
1273 | #endif | |
5b5a2ea1 SM |
1274 | /* Keep initial / only if this is the whole name. */ |
1275 | if (o == target && IS_ANY_SEP (*o) && p[3] == 0) | |
1276 | ++o; | |
1277 | p += 3; | |
1278 | } | |
1279 | else if (p > target && IS_DIRECTORY_SEP (p[1])) | |
1280 | /* Collapse multiple `/' in a row. */ | |
1281 | p++; | |
1282 | else | |
1283 | { | |
1284 | *o++ = *p++; | |
1285 | } | |
5b5a2ea1 | 1286 | } |
570d7624 | 1287 | |
5e570b75 | 1288 | #ifdef DOS_NT |
e8d32c7e | 1289 | /* At last, set drive name. */ |
5e570b75 | 1290 | #ifdef WINDOWSNT |
5b5a2ea1 SM |
1291 | /* Except for network file name. */ |
1292 | if (!(IS_DIRECTORY_SEP (target[0]) && IS_DIRECTORY_SEP (target[1]))) | |
5e570b75 | 1293 | #endif /* WINDOWSNT */ |
5b5a2ea1 | 1294 | { |
1088b922 | 1295 | if (!drive) emacs_abort (); |
5b5a2ea1 SM |
1296 | target -= 2; |
1297 | target[0] = DRIVE_LETTER (drive); | |
1298 | target[1] = ':'; | |
1299 | } | |
1300 | /* Reinsert the escape prefix if required. */ | |
1301 | if (is_escaped) | |
1302 | { | |
1303 | target -= 2; | |
1304 | target[0] = '/'; | |
1305 | target[1] = ':'; | |
1306 | } | |
087fc47a | 1307 | dostounix_filename (target); |
5e570b75 | 1308 | #endif /* DOS_NT */ |
4c3c22f3 | 1309 | |
5b5a2ea1 SM |
1310 | result = make_specified_string (target, -1, o - target, multibyte); |
1311 | } | |
beb402de KG |
1312 | |
1313 | /* Again look to see if the file name has special constructs in it | |
1314 | and perhaps call the corresponding file handler. This is needed | |
1315 | for filenames such as "/foo/../user@host:/bar/../baz". Expanding | |
1316 | the ".." component gives us "/user@host:/bar/../baz" which needs | |
e8d32c7e | 1317 | to be expanded again. */ |
beb402de KG |
1318 | handler = Ffind_file_name_handler (result, Qexpand_file_name); |
1319 | if (!NILP (handler)) | |
2375c96a CY |
1320 | { |
1321 | handled_name = call3 (handler, Qexpand_file_name, | |
1322 | result, default_directory); | |
1323 | if (STRINGP (handled_name)) | |
1324 | return handled_name; | |
1325 | error ("Invalid handler in `file-name-handler-alist'"); | |
1326 | } | |
beb402de KG |
1327 | |
1328 | return result; | |
570d7624 | 1329 | } |
5e570b75 | 1330 | |
4887597a EZ |
1331 | #if 0 |
1332 | /* PLEASE DO NOT DELETE THIS COMMENTED-OUT VERSION! | |
1333 | This is the old version of expand-file-name, before it was thoroughly | |
1334 | rewritten for Emacs 10.31. We leave this version here commented-out, | |
1335 | because the code is very complex and likely to have subtle bugs. If | |
1336 | bugs _are_ found, it might be of interest to look at the old code and | |
1337 | see what did it do in the relevant situation. | |
1338 | ||
30f50381 GM |
1339 | Don't remove this code: it's true that it will be accessible |
1340 | from the repository, but a few years from deletion, people will | |
1341 | forget it is there. */ | |
4887597a EZ |
1342 | |
1343 | /* Changed this DEFUN to a DEAFUN, so as not to confuse `make-docfile'. */ | |
1344 | DEAFUN ("expand-file-name", Fexpand_file_name, Sexpand_file_name, 1, 2, 0, | |
1345 | "Convert FILENAME to absolute, and canonicalize it.\n\ | |
1346 | Second arg DEFAULT is directory to start with if FILENAME is relative\n\ | |
c70a4df6 | 1347 | \(does not start with slash); if DEFAULT is nil or missing,\n\ |
4887597a EZ |
1348 | the current buffer's value of default-directory is used.\n\ |
1349 | Filenames containing `.' or `..' as components are simplified;\n\ | |
1350 | initial `~/' expands to your home directory.\n\ | |
1351 | See also the function `substitute-in-file-name'.") | |
1352 | (name, defalt) | |
1353 | Lisp_Object name, defalt; | |
1354 | { | |
1355 | unsigned char *nm; | |
1356 | ||
1357 | register unsigned char *newdir, *p, *o; | |
93f4cf88 | 1358 | ptrdiff_t tlen; |
4887597a EZ |
1359 | unsigned char *target; |
1360 | struct passwd *pw; | |
4887597a | 1361 | |
b7826503 | 1362 | CHECK_STRING (name); |
d5db4077 | 1363 | nm = SDATA (name); |
4887597a EZ |
1364 | |
1365 | /* If nm is absolute, flush ...// and detect /./ and /../. | |
1366 | If no /./ or /../ we can return right away. */ | |
7c2fb837 | 1367 | if (nm[0] == '/') |
4887597a | 1368 | { |
f75d7a91 | 1369 | bool lose = 0; |
4887597a | 1370 | p = nm; |
4887597a EZ |
1371 | while (*p) |
1372 | { | |
1373 | if (p[0] == '/' && p[1] == '/' | |
4887597a EZ |
1374 | ) |
1375 | nm = p + 1; | |
1376 | if (p[0] == '/' && p[1] == '~') | |
1377 | nm = p + 1, lose = 1; | |
1378 | if (p[0] == '/' && p[1] == '.' | |
1379 | && (p[2] == '/' || p[2] == 0 | |
1380 | || (p[2] == '.' && (p[3] == '/' || p[3] == 0)))) | |
1381 | lose = 1; | |
4887597a EZ |
1382 | p++; |
1383 | } | |
1384 | if (!lose) | |
1385 | { | |
d5db4077 | 1386 | if (nm == SDATA (name)) |
4887597a EZ |
1387 | return name; |
1388 | return build_string (nm); | |
1389 | } | |
1390 | } | |
1391 | ||
e8d32c7e | 1392 | /* Now determine directory to start with and put it in NEWDIR. */ |
4887597a EZ |
1393 | |
1394 | newdir = 0; | |
1395 | ||
1396 | if (nm[0] == '~') /* prefix ~ */ | |
7c2fb837 | 1397 | if (nm[1] == '/' || nm[1] == 0)/* ~/filename */ |
4887597a EZ |
1398 | { |
1399 | if (!(newdir = (unsigned char *) egetenv ("HOME"))) | |
1400 | newdir = (unsigned char *) ""; | |
1401 | nm++; | |
4887597a EZ |
1402 | } |
1403 | else /* ~user/filename */ | |
1404 | { | |
e8d32c7e | 1405 | /* Get past ~ to user. */ |
4887597a | 1406 | unsigned char *user = nm + 1; |
e8d32c7e | 1407 | /* Find end of name. */ |
8966b757 | 1408 | unsigned char *ptr = (unsigned char *) strchr (user, '/'); |
93f4cf88 | 1409 | ptrdiff_t len = ptr ? ptr - user : strlen (user); |
e8d32c7e | 1410 | /* Copy the user name into temp storage. */ |
38182d90 | 1411 | o = alloca (len + 1); |
72af86bd | 1412 | memcpy (o, user, len); |
4887597a EZ |
1413 | o[len] = 0; |
1414 | ||
e8d32c7e | 1415 | /* Look up the user name. */ |
4d7e6e51 | 1416 | block_input (); |
4887597a | 1417 | pw = (struct passwd *) getpwnam (o + 1); |
4d7e6e51 | 1418 | unblock_input (); |
4887597a EZ |
1419 | if (!pw) |
1420 | error ("\"%s\" isn't a registered user", o + 1); | |
1421 | ||
1422 | newdir = (unsigned char *) pw->pw_dir; | |
1423 | ||
1424 | /* Discard the user name from NM. */ | |
1425 | nm += len; | |
1426 | } | |
1427 | ||
7c2fb837 | 1428 | if (nm[0] != '/' && !newdir) |
4887597a EZ |
1429 | { |
1430 | if (NILP (defalt)) | |
1431 | defalt = current_buffer->directory; | |
b7826503 | 1432 | CHECK_STRING (defalt); |
d5db4077 | 1433 | newdir = SDATA (defalt); |
4887597a EZ |
1434 | } |
1435 | ||
e8d32c7e | 1436 | /* Now concatenate the directory and name to new space in the stack frame. */ |
4887597a EZ |
1437 | |
1438 | tlen = (newdir ? strlen (newdir) + 1 : 0) + strlen (nm) + 1; | |
38182d90 | 1439 | target = alloca (tlen); |
4887597a EZ |
1440 | *target = 0; |
1441 | ||
1442 | if (newdir) | |
1443 | { | |
4887597a EZ |
1444 | if (nm[0] == 0 || nm[0] == '/') |
1445 | strcpy (target, newdir); | |
1446 | else | |
4887597a EZ |
1447 | file_name_as_directory (target, newdir); |
1448 | } | |
1449 | ||
1450 | strcat (target, nm); | |
4887597a | 1451 | |
e8d32c7e | 1452 | /* Now canonicalize by removing /. and /foo/.. if they appear. */ |
4887597a EZ |
1453 | |
1454 | p = target; | |
1455 | o = target; | |
1456 | ||
1457 | while (*p) | |
1458 | { | |
4887597a EZ |
1459 | if (*p != '/') |
1460 | { | |
1461 | *o++ = *p++; | |
1462 | } | |
1463 | else if (!strncmp (p, "//", 2) | |
4887597a EZ |
1464 | ) |
1465 | { | |
1466 | o = target; | |
1467 | p++; | |
1468 | } | |
994a7262 RS |
1469 | else if (p[0] == '/' && p[1] == '.' |
1470 | && (p[2] == '/' || p[2] == 0)) | |
4887597a EZ |
1471 | p += 2; |
1472 | else if (!strncmp (p, "/..", 3) | |
1473 | /* `/../' is the "superroot" on certain file systems. */ | |
1474 | && o != target | |
1475 | && (p[3] == '/' || p[3] == 0)) | |
1476 | { | |
1477 | while (o != target && *--o != '/') | |
1478 | ; | |
4887597a EZ |
1479 | if (o == target && *o == '/') |
1480 | ++o; | |
1481 | p += 3; | |
1482 | } | |
1483 | else | |
1484 | { | |
1485 | *o++ = *p++; | |
1486 | } | |
4887597a EZ |
1487 | } |
1488 | ||
1489 | return make_string (target, o - target); | |
1490 | } | |
1491 | #endif | |
570d7624 | 1492 | \f |
c70a4df6 | 1493 | /* If /~ or // appears, discard everything through first slash. */ |
f75d7a91 | 1494 | static bool |
5976c3fe | 1495 | file_name_absolute_p (const char *filename) |
c70a4df6 SM |
1496 | { |
1497 | return | |
1498 | (IS_DIRECTORY_SEP (*filename) || *filename == '~' | |
c70a4df6 SM |
1499 | #ifdef DOS_NT |
1500 | || (IS_DRIVE (*filename) && IS_DEVICE_SEP (filename[1]) | |
1501 | && IS_DIRECTORY_SEP (filename[2])) | |
1502 | #endif | |
1503 | ); | |
1504 | } | |
1505 | ||
5976c3fe PE |
1506 | static char * |
1507 | search_embedded_absfilename (char *nm, char *endp) | |
c70a4df6 | 1508 | { |
5976c3fe | 1509 | char *p, *s; |
c70a4df6 SM |
1510 | |
1511 | for (p = nm + 1; p < endp; p++) | |
1512 | { | |
1513 | if ((0 | |
c70a4df6 SM |
1514 | || IS_DIRECTORY_SEP (p[-1])) |
1515 | && file_name_absolute_p (p) | |
5e617bc2 | 1516 | #if defined (WINDOWSNT) || defined (CYGWIN) |
c70a4df6 SM |
1517 | /* // at start of file name is meaningful in Apollo, |
1518 | WindowsNT and Cygwin systems. */ | |
f34574c6 | 1519 | && !(IS_DIRECTORY_SEP (p[0]) && p - 1 == nm) |
e39a993c | 1520 | #endif /* not (WINDOWSNT || CYGWIN) */ |
c70a4df6 SM |
1521 | ) |
1522 | { | |
7c2fb837 | 1523 | for (s = p; *s && (!IS_DIRECTORY_SEP (*s)); s++); |
e8d32c7e | 1524 | if (p[0] == '~' && s > p + 1) /* We've got "/~something/". */ |
c70a4df6 | 1525 | { |
5976c3fe | 1526 | char *o = alloca (s - p + 1); |
c70a4df6 | 1527 | struct passwd *pw; |
72af86bd | 1528 | memcpy (o, p, s - p); |
c70a4df6 SM |
1529 | o [s - p] = 0; |
1530 | ||
1531 | /* If we have ~user and `user' exists, discard | |
1532 | everything up to ~. But if `user' does not exist, leave | |
1533 | ~user alone, it might be a literal file name. */ | |
4d7e6e51 | 1534 | block_input (); |
67c08d6c | 1535 | pw = getpwnam (o + 1); |
4d7e6e51 | 1536 | unblock_input (); |
67c08d6c | 1537 | if (pw) |
c70a4df6 | 1538 | return p; |
c70a4df6 SM |
1539 | } |
1540 | else | |
1541 | return p; | |
1542 | } | |
1543 | } | |
1544 | return NULL; | |
1545 | } | |
1546 | ||
a7ca3326 | 1547 | DEFUN ("substitute-in-file-name", Fsubstitute_in_file_name, |
8c1a1077 PJ |
1548 | Ssubstitute_in_file_name, 1, 1, 0, |
1549 | doc: /* Substitute environment variables referred to in FILENAME. | |
1550 | `$FOO' where FOO is an environment variable name means to substitute | |
1551 | the value of that variable. The variable name should be terminated | |
1552 | with a character not a letter, digit or underscore; otherwise, enclose | |
1553 | the entire variable name in braces. | |
c68845e0 GM |
1554 | |
1555 | If `/~' appears, all of FILENAME through that `/' is discarded. | |
1556 | If `//' appears, everything up to and including the first of | |
1557 | those `/' is discarded. */) | |
5842a27b | 1558 | (Lisp_Object filename) |
570d7624 | 1559 | { |
f75d7a91 | 1560 | char *nm, *s, *p, *o, *x, *endp; |
5976c3fe | 1561 | char *target = NULL; |
570d7624 | 1562 | int total = 0; |
f75d7a91 | 1563 | bool substituted = 0; |
fce31d69 | 1564 | bool multibyte; |
5976c3fe | 1565 | char *xnm; |
8ce069f5 | 1566 | Lisp_Object handler; |
570d7624 | 1567 | |
b7826503 | 1568 | CHECK_STRING (filename); |
570d7624 | 1569 | |
58aec0d6 JR |
1570 | multibyte = STRING_MULTIBYTE (filename); |
1571 | ||
8ce069f5 RS |
1572 | /* If the file name has special constructs in it, |
1573 | call the corresponding file handler. */ | |
3b7f6e60 | 1574 | handler = Ffind_file_name_handler (filename, Qsubstitute_in_file_name); |
8ce069f5 | 1575 | if (!NILP (handler)) |
2375c96a CY |
1576 | { |
1577 | Lisp_Object handled_name = call2 (handler, Qsubstitute_in_file_name, | |
1578 | filename); | |
1579 | if (STRINGP (handled_name)) | |
1580 | return handled_name; | |
1581 | error ("Invalid handler in `file-name-handler-alist'"); | |
1582 | } | |
8ce069f5 | 1583 | |
58aec0d6 JR |
1584 | /* Always work on a copy of the string, in case GC happens during |
1585 | decode of environment variables, causing the original Lisp_String | |
1586 | data to be relocated. */ | |
38182d90 | 1587 | nm = alloca (SBYTES (filename) + 1); |
72af86bd | 1588 | memcpy (nm, SDATA (filename), SBYTES (filename) + 1); |
0f3f018c | 1589 | |
58aec0d6 | 1590 | #ifdef DOS_NT |
087fc47a | 1591 | dostounix_filename (nm); |
d5db4077 | 1592 | substituted = (strcmp (nm, SDATA (filename)) != 0); |
a5a1cc06 | 1593 | #endif |
d5db4077 | 1594 | endp = nm + SBYTES (filename); |
570d7624 | 1595 | |
82330e7f | 1596 | /* If /~ or // appears, discard everything through first slash. */ |
c70a4df6 SM |
1597 | p = search_embedded_absfilename (nm, endp); |
1598 | if (p) | |
1599 | /* Start over with the new string, so we check the file-name-handler | |
1600 | again. Important with filenames like "/home/foo//:/hello///there" | |
ee7683eb | 1601 | which would substitute to "/:/hello///there" rather than "/there". */ |
c70a4df6 | 1602 | return Fsubstitute_in_file_name |
58aec0d6 | 1603 | (make_specified_string (p, -1, endp - p, multibyte)); |
570d7624 JB |
1604 | |
1605 | /* See if any variables are substituted into the string | |
e8d32c7e | 1606 | and find the total length of their values in `total'. */ |
570d7624 JB |
1607 | |
1608 | for (p = nm; p != endp;) | |
1609 | if (*p != '$') | |
1610 | p++; | |
1611 | else | |
1612 | { | |
1613 | p++; | |
1614 | if (p == endp) | |
1615 | goto badsubst; | |
1616 | else if (*p == '$') | |
1617 | { | |
e8d32c7e | 1618 | /* "$$" means a single "$". */ |
570d7624 JB |
1619 | p++; |
1620 | total -= 1; | |
1621 | substituted = 1; | |
1622 | continue; | |
1623 | } | |
1624 | else if (*p == '{') | |
1625 | { | |
1626 | o = ++p; | |
1627 | while (p != endp && *p != '}') p++; | |
1628 | if (*p != '}') goto missingclose; | |
1629 | s = p; | |
1630 | } | |
1631 | else | |
1632 | { | |
1633 | o = p; | |
620f13b0 | 1634 | while (p != endp && (c_isalnum (*p) || *p == '_')) p++; |
570d7624 JB |
1635 | s = p; |
1636 | } | |
1637 | ||
e8d32c7e | 1638 | /* Copy out the variable name. */ |
38182d90 | 1639 | target = alloca (s - o + 1); |
e99a530f | 1640 | memcpy (target, o, s - o); |
570d7624 | 1641 | target[s - o] = 0; |
5e570b75 | 1642 | #ifdef DOS_NT |
4c3c22f3 | 1643 | strupr (target); /* $home == $HOME etc. */ |
5e570b75 | 1644 | #endif /* DOS_NT */ |
570d7624 | 1645 | |
e8d32c7e | 1646 | /* Get variable value. */ |
5976c3fe | 1647 | o = egetenv (target); |
8d2ced53 | 1648 | if (o) |
58aec0d6 JR |
1649 | { |
1650 | /* Don't try to guess a maximum length - UTF8 can use up to | |
1651 | four bytes per character. This code is unlikely to run | |
1652 | in a situation that requires performance, so decoding the | |
1653 | env variables twice should be acceptable. Note that | |
1654 | decoding may cause a garbage collect. */ | |
1655 | Lisp_Object orig, decoded; | |
d7ea76b4 | 1656 | orig = build_unibyte_string (o); |
58aec0d6 JR |
1657 | decoded = DECODE_FILE (orig); |
1658 | total += SBYTES (decoded); | |
8d2ced53 SM |
1659 | substituted = 1; |
1660 | } | |
1661 | else if (*p == '}') | |
1662 | goto badvar; | |
570d7624 JB |
1663 | } |
1664 | ||
1665 | if (!substituted) | |
3b7f6e60 | 1666 | return filename; |
570d7624 | 1667 | |
e8d32c7e SM |
1668 | /* If substitution required, recopy the string and do it. */ |
1669 | /* Make space in stack frame for the new copy. */ | |
38182d90 | 1670 | xnm = alloca (SBYTES (filename) + total + 1); |
570d7624 JB |
1671 | x = xnm; |
1672 | ||
e8d32c7e | 1673 | /* Copy the rest of the name through, replacing $ constructs with values. */ |
570d7624 JB |
1674 | for (p = nm; *p;) |
1675 | if (*p != '$') | |
1676 | *x++ = *p++; | |
1677 | else | |
1678 | { | |
1679 | p++; | |
1680 | if (p == endp) | |
1681 | goto badsubst; | |
1682 | else if (*p == '$') | |
1683 | { | |
1684 | *x++ = *p++; | |
1685 | continue; | |
1686 | } | |
1687 | else if (*p == '{') | |
1688 | { | |
1689 | o = ++p; | |
1690 | while (p != endp && *p != '}') p++; | |
1691 | if (*p != '}') goto missingclose; | |
1692 | s = p++; | |
1693 | } | |
1694 | else | |
1695 | { | |
1696 | o = p; | |
620f13b0 | 1697 | while (p != endp && (c_isalnum (*p) || *p == '_')) p++; |
570d7624 JB |
1698 | s = p; |
1699 | } | |
1700 | ||
e8d32c7e | 1701 | /* Copy out the variable name. */ |
38182d90 | 1702 | target = alloca (s - o + 1); |
e99a530f | 1703 | memcpy (target, o, s - o); |
570d7624 | 1704 | target[s - o] = 0; |
5e570b75 | 1705 | #ifdef DOS_NT |
4c3c22f3 | 1706 | strupr (target); /* $home == $HOME etc. */ |
5e570b75 | 1707 | #endif /* DOS_NT */ |
570d7624 | 1708 | |
e8d32c7e | 1709 | /* Get variable value. */ |
5976c3fe | 1710 | o = egetenv (target); |
570d7624 | 1711 | if (!o) |
8d2ced53 SM |
1712 | { |
1713 | *x++ = '$'; | |
1714 | strcpy (x, target); x+= strlen (target); | |
1715 | } | |
60d67b83 RS |
1716 | else |
1717 | { | |
58aec0d6 | 1718 | Lisp_Object orig, decoded; |
93f4cf88 | 1719 | ptrdiff_t orig_length, decoded_length; |
58aec0d6 JR |
1720 | orig_length = strlen (o); |
1721 | orig = make_unibyte_string (o, orig_length); | |
1722 | decoded = DECODE_FILE (orig); | |
1723 | decoded_length = SBYTES (decoded); | |
e99a530f | 1724 | memcpy (x, SDATA (decoded), decoded_length); |
58aec0d6 JR |
1725 | x += decoded_length; |
1726 | ||
1727 | /* If environment variable needed decoding, return value | |
1728 | needs to be multibyte. */ | |
1729 | if (decoded_length != orig_length | |
e99a530f | 1730 | || memcmp (SDATA (decoded), o, orig_length)) |
58aec0d6 | 1731 | multibyte = 1; |
60d67b83 | 1732 | } |
570d7624 JB |
1733 | } |
1734 | ||
1735 | *x = 0; | |
1736 | ||
82330e7f | 1737 | /* If /~ or // appears, discard everything through first slash. */ |
c70a4df6 SM |
1738 | while ((p = search_embedded_absfilename (xnm, x))) |
1739 | /* This time we do not start over because we've already expanded envvars | |
1740 | and replaced $$ with $. Maybe we should start over as well, but we'd | |
1741 | need to quote some $ to $$ first. */ | |
1742 | xnm = p; | |
570d7624 | 1743 | |
58aec0d6 | 1744 | return make_specified_string (xnm, -1, x - xnm, multibyte); |
570d7624 JB |
1745 | |
1746 | badsubst: | |
1747 | error ("Bad format environment-variable substitution"); | |
1748 | missingclose: | |
1749 | error ("Missing \"}\" in environment-variable substitution"); | |
1750 | badvar: | |
1751 | error ("Substituting nonexistent environment variable \"%s\"", target); | |
1752 | ||
1753 | /* NOTREACHED */ | |
6bbd7a29 | 1754 | return Qnil; |
570d7624 JB |
1755 | } |
1756 | \f | |
067ffa38 | 1757 | /* A slightly faster and more convenient way to get |
298b760e | 1758 | (directory-file-name (expand-file-name FOO)). */ |
067ffa38 | 1759 | |
570d7624 | 1760 | Lisp_Object |
971de7fb | 1761 | expand_and_dir_to_file (Lisp_Object filename, Lisp_Object defdir) |
570d7624 | 1762 | { |
199607e4 | 1763 | register Lisp_Object absname; |
570d7624 | 1764 | |
199607e4 | 1765 | absname = Fexpand_file_name (filename, defdir); |
7c2fb837 | 1766 | |
199607e4 | 1767 | /* Remove final slash, if any (unless this is the root dir). |
570d7624 | 1768 | stat behaves differently depending! */ |
d5db4077 KR |
1769 | if (SCHARS (absname) > 1 |
1770 | && IS_DIRECTORY_SEP (SREF (absname, SBYTES (absname) - 1)) | |
e8d32c7e | 1771 | && !IS_DEVICE_SEP (SREF (absname, SBYTES (absname) - 2))) |
ddc61f46 | 1772 | /* We cannot take shortcuts; they might be wrong for magic file names. */ |
199607e4 | 1773 | absname = Fdirectory_file_name (absname); |
199607e4 | 1774 | return absname; |
570d7624 JB |
1775 | } |
1776 | \f | |
3ed15d97 | 1777 | /* Signal an error if the file ABSNAME already exists. |
f75d7a91 | 1778 | If INTERACTIVE, ask the user whether to proceed, |
3ed15d97 RS |
1779 | and bypass the error if the user says to go ahead. |
1780 | QUERYSTRING is a name for the action that is being considered | |
1781 | to alter the file. | |
de1d0127 | 1782 | |
3ed15d97 | 1783 | *STATPTR is used to store the stat information if the file exists. |
de1d0127 | 1784 | If the file does not exist, STATPTR->st_mode is set to 0. |
b8b29dc9 RS |
1785 | If STATPTR is null, we don't store into it. |
1786 | ||
f75d7a91 | 1787 | If QUICK, ask for y or n, not yes or no. */ |
3ed15d97 | 1788 | |
f14b7e14 | 1789 | static void |
5976c3fe | 1790 | barf_or_query_if_file_exists (Lisp_Object absname, const char *querystring, |
f75d7a91 PE |
1791 | bool interactive, struct stat *statptr, |
1792 | bool quick) | |
570d7624 | 1793 | { |
f75d7a91 | 1794 | Lisp_Object tem, encoded_filename; |
4018b5ef | 1795 | struct stat statbuf; |
570d7624 JB |
1796 | struct gcpro gcpro1; |
1797 | ||
643c73b9 RS |
1798 | encoded_filename = ENCODE_FILE (absname); |
1799 | ||
e8d32c7e | 1800 | /* `stat' is a good way to tell whether the file exists, |
4018b5ef | 1801 | regardless of what access permissions it has. */ |
42a5b22f | 1802 | if (lstat (SSDATA (encoded_filename), &statbuf) >= 0) |
570d7624 | 1803 | { |
6e9b2be9 | 1804 | if (S_ISDIR (statbuf.st_mode)) |
c4f2d8d4 | 1805 | xsignal2 (Qfile_error, |
ad637907 | 1806 | build_string ("File is a directory"), absname); |
c4f2d8d4 | 1807 | |
570d7624 | 1808 | if (! interactive) |
24b1ddad KS |
1809 | xsignal2 (Qfile_already_exists, |
1810 | build_string ("File already exists"), absname); | |
570d7624 | 1811 | GCPRO1 (absname); |
67e8e2b8 RS |
1812 | tem = format2 ("File %s already exists; %s anyway? ", |
1813 | absname, build_string (querystring)); | |
b8b29dc9 | 1814 | if (quick) |
5616cc54 | 1815 | tem = call1 (intern ("y-or-n-p"), tem); |
b8b29dc9 RS |
1816 | else |
1817 | tem = do_yes_or_no_p (tem); | |
570d7624 | 1818 | UNGCPRO; |
265a9e55 | 1819 | if (NILP (tem)) |
24b1ddad KS |
1820 | xsignal2 (Qfile_already_exists, |
1821 | build_string ("File already exists"), absname); | |
3ed15d97 RS |
1822 | if (statptr) |
1823 | *statptr = statbuf; | |
1824 | } | |
1825 | else | |
1826 | { | |
1827 | if (statptr) | |
1828 | statptr->st_mode = 0; | |
570d7624 JB |
1829 | } |
1830 | return; | |
1831 | } | |
1832 | ||
574c05e2 | 1833 | DEFUN ("copy-file", Fcopy_file, Scopy_file, 2, 6, |
7c0f6118 | 1834 | "fCopy file: \nGCopy %s to file: \np\nP", |
8c1a1077 PJ |
1835 | doc: /* Copy FILE to NEWNAME. Both args must be strings. |
1836 | If NEWNAME names a directory, copy FILE there. | |
795c20df CY |
1837 | |
1838 | This function always sets the file modes of the output file to match | |
1839 | the input file. | |
1840 | ||
1841 | The optional third argument OK-IF-ALREADY-EXISTS specifies what to do | |
1842 | if file NEWNAME already exists. If OK-IF-ALREADY-EXISTS is nil, we | |
1843 | signal a `file-already-exists' error without overwriting. If | |
1844 | OK-IF-ALREADY-EXISTS is a number, we request confirmation from the user | |
1845 | about overwriting; this is what happens in interactive use with M-x. | |
1846 | Any other value for OK-IF-ALREADY-EXISTS means to overwrite the | |
1847 | existing file. | |
c50f15d0 | 1848 | |
7f08b80e | 1849 | Fourth arg KEEP-TIME non-nil means give the output file the same |
8c1a1077 | 1850 | last-modified time as the old one. (This works on only some systems.) |
c50f15d0 | 1851 | |
7fce7dfe EZ |
1852 | A prefix arg makes KEEP-TIME non-nil. |
1853 | ||
586702ce | 1854 | If PRESERVE-UID-GID is non-nil, we try to transfer the |
574c05e2 KK |
1855 | uid and gid of FILE to NEWNAME. |
1856 | ||
f5783df3 | 1857 | If PRESERVE-SELINUX-CONTEXT is non-nil and SELinux is enabled |
574c05e2 | 1858 | on the system, we copy the SELinux context of FILE to NEWNAME. */) |
5842a27b | 1859 | (Lisp_Object file, Lisp_Object newname, Lisp_Object ok_if_already_exists, Lisp_Object keep_time, Lisp_Object preserve_uid_gid, Lisp_Object preserve_selinux_context) |
570d7624 | 1860 | { |
8a2cbd72 | 1861 | int ifd, ofd; |
d311d28c | 1862 | int n; |
570d7624 | 1863 | char buf[16 * 1024]; |
3ed15d97 | 1864 | struct stat st, out_st; |
32f4334d | 1865 | Lisp_Object handler; |
b1d1b865 | 1866 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; |
d311d28c | 1867 | ptrdiff_t count = SPECPDL_INDEX (); |
f75d7a91 | 1868 | bool input_file_statable_p; |
b1d1b865 | 1869 | Lisp_Object encoded_file, encoded_newname; |
574c05e2 KK |
1870 | #if HAVE_LIBSELINUX |
1871 | security_context_t con; | |
f75d7a91 | 1872 | int conlength = 0; |
574c05e2 | 1873 | #endif |
570d7624 | 1874 | |
b1d1b865 RS |
1875 | encoded_file = encoded_newname = Qnil; |
1876 | GCPRO4 (file, newname, encoded_file, encoded_newname); | |
b7826503 PJ |
1877 | CHECK_STRING (file); |
1878 | CHECK_STRING (newname); | |
b1d1b865 | 1879 | |
a9d14e54 | 1880 | if (!NILP (Ffile_directory_p (newname))) |
6b61353c | 1881 | newname = Fexpand_file_name (Ffile_name_nondirectory (file), newname); |
a9d14e54 GM |
1882 | else |
1883 | newname = Fexpand_file_name (newname, Qnil); | |
1884 | ||
3b7f6e60 | 1885 | file = Fexpand_file_name (file, Qnil); |
32f4334d | 1886 | |
0bf2eed2 | 1887 | /* If the input file name has special constructs in it, |
32f4334d | 1888 | call the corresponding file handler. */ |
3b7f6e60 | 1889 | handler = Ffind_file_name_handler (file, Qcopy_file); |
0bf2eed2 | 1890 | /* Likewise for output file name. */ |
51cf6d37 | 1891 | if (NILP (handler)) |
49307295 | 1892 | handler = Ffind_file_name_handler (newname, Qcopy_file); |
32f4334d | 1893 | if (!NILP (handler)) |
574c05e2 KK |
1894 | RETURN_UNGCPRO (call7 (handler, Qcopy_file, file, newname, |
1895 | ok_if_already_exists, keep_time, preserve_uid_gid, | |
1896 | preserve_selinux_context)); | |
32f4334d | 1897 | |
b1d1b865 RS |
1898 | encoded_file = ENCODE_FILE (file); |
1899 | encoded_newname = ENCODE_FILE (newname); | |
1900 | ||
265a9e55 | 1901 | if (NILP (ok_if_already_exists) |
93c30b5f | 1902 | || INTEGERP (ok_if_already_exists)) |
2f868094 | 1903 | barf_or_query_if_file_exists (newname, "copy to it", |
b8b29dc9 | 1904 | INTEGERP (ok_if_already_exists), &out_st, 0); |
42a5b22f | 1905 | else if (stat (SSDATA (encoded_newname), &out_st) < 0) |
3ed15d97 | 1906 | out_st.st_mode = 0; |
570d7624 | 1907 | |
e8691c59 | 1908 | #ifdef WINDOWSNT |
d5db4077 | 1909 | if (!CopyFile (SDATA (encoded_file), |
efdc16c9 | 1910 | SDATA (encoded_newname), |
e8691c59 GM |
1911 | FALSE)) |
1912 | report_file_error ("Copying file", Fcons (file, Fcons (newname, Qnil))); | |
8b53dc06 JR |
1913 | /* CopyFile retains the timestamp by default. */ |
1914 | else if (NILP (keep_time)) | |
e8691c59 GM |
1915 | { |
1916 | EMACS_TIME now; | |
ad497129 JR |
1917 | DWORD attributes; |
1918 | char * filename; | |
1919 | ||
d5db4077 | 1920 | filename = SDATA (encoded_newname); |
ad497129 JR |
1921 | |
1922 | /* Ensure file is writable while its modified time is set. */ | |
1923 | attributes = GetFileAttributes (filename); | |
02cca86b | 1924 | SetFileAttributes (filename, attributes & ~FILE_ATTRIBUTE_READONLY); |
e9a9ae03 | 1925 | now = current_emacs_time (); |
d35af63c | 1926 | if (set_file_times (-1, filename, now, now)) |
ad497129 JR |
1927 | { |
1928 | /* Restore original attributes. */ | |
1929 | SetFileAttributes (filename, attributes); | |
24b1ddad KS |
1930 | xsignal2 (Qfile_date_error, |
1931 | build_string ("Cannot set file date"), newname); | |
ad497129 JR |
1932 | } |
1933 | /* Restore original attributes. */ | |
1934 | SetFileAttributes (filename, attributes); | |
e8691c59 GM |
1935 | } |
1936 | #else /* not WINDOWSNT */ | |
13336908 | 1937 | immediate_quit = 1; |
42a5b22f | 1938 | ifd = emacs_open (SSDATA (encoded_file), O_RDONLY, 0); |
13336908 RS |
1939 | immediate_quit = 0; |
1940 | ||
570d7624 | 1941 | if (ifd < 0) |
3b7f6e60 | 1942 | report_file_error ("Opening input file", Fcons (file, Qnil)); |
570d7624 | 1943 | |
b5148e85 RS |
1944 | record_unwind_protect (close_file_unwind, make_number (ifd)); |
1945 | ||
f73b0ada BF |
1946 | /* We can only copy regular files and symbolic links. Other files are not |
1947 | copyable by us. */ | |
1948 | input_file_statable_p = (fstat (ifd, &st) >= 0); | |
1949 | ||
574c05e2 KK |
1950 | #if HAVE_LIBSELINUX |
1951 | if (!NILP (preserve_selinux_context) && is_selinux_enabled ()) | |
1952 | { | |
1953 | conlength = fgetfilecon (ifd, &con); | |
1954 | if (conlength == -1) | |
1955 | report_file_error ("Doing fgetfilecon", Fcons (file, Qnil)); | |
1956 | } | |
1957 | #endif | |
1958 | ||
3ed15d97 RS |
1959 | if (out_st.st_mode != 0 |
1960 | && st.st_dev == out_st.st_dev && st.st_ino == out_st.st_ino) | |
1961 | { | |
1962 | errno = 0; | |
1963 | report_file_error ("Input and output files are the same", | |
3b7f6e60 | 1964 | Fcons (file, Fcons (newname, Qnil))); |
3ed15d97 | 1965 | } |
3ed15d97 | 1966 | |
f73b0ada BF |
1967 | if (input_file_statable_p) |
1968 | { | |
1969 | if (!(S_ISREG (st.st_mode)) && !(S_ISLNK (st.st_mode))) | |
1970 | { | |
1971 | #if defined (EISDIR) | |
1972 | /* Get a better looking error message. */ | |
1973 | errno = EISDIR; | |
1974 | #endif /* EISDIR */ | |
3b7f6e60 | 1975 | report_file_error ("Non-regular file", Fcons (file, Qnil)); |
f73b0ada BF |
1976 | } |
1977 | } | |
f73b0ada | 1978 | |
4c3c22f3 RS |
1979 | #ifdef MSDOS |
1980 | /* System's default file type was set to binary by _fmode in emacs.c. */ | |
c50f15d0 | 1981 | ofd = emacs_open (SDATA (encoded_newname), |
7fce7dfe | 1982 | O_WRONLY | O_TRUNC | O_CREAT |
795c20df | 1983 | | (NILP (ok_if_already_exists) ? O_EXCL : 0), |
c50f15d0 RS |
1984 | S_IREAD | S_IWRITE); |
1985 | #else /* not MSDOS */ | |
15e3a074 | 1986 | { |
f75d7a91 | 1987 | mode_t new_mask = 0666; |
15e3a074 PE |
1988 | if (input_file_statable_p) |
1989 | { | |
1990 | if (!NILP (preserve_uid_gid)) | |
1991 | new_mask = 0600; | |
1992 | new_mask &= st.st_mode; | |
1993 | } | |
1994 | ofd = emacs_open (SSDATA (encoded_newname), | |
1995 | (O_WRONLY | O_TRUNC | O_CREAT | |
1996 | | (NILP (ok_if_already_exists) ? O_EXCL : 0)), | |
1997 | new_mask); | |
1998 | } | |
4c3c22f3 | 1999 | #endif /* not MSDOS */ |
570d7624 | 2000 | if (ofd < 0) |
3ed15d97 | 2001 | report_file_error ("Opening output file", Fcons (newname, Qnil)); |
b5148e85 RS |
2002 | |
2003 | record_unwind_protect (close_file_unwind, make_number (ofd)); | |
570d7624 | 2004 | |
b5148e85 RS |
2005 | immediate_quit = 1; |
2006 | QUIT; | |
68c45bf0 PE |
2007 | while ((n = emacs_read (ifd, buf, sizeof buf)) > 0) |
2008 | if (emacs_write (ofd, buf, n) != n) | |
3ed15d97 | 2009 | report_file_error ("I/O error", Fcons (newname, Qnil)); |
b5148e85 | 2010 | immediate_quit = 0; |
570d7624 | 2011 | |
b016179b EZ |
2012 | #ifndef MSDOS |
2013 | /* Preserve the original file modes, and if requested, also its | |
2014 | owner and group. */ | |
586702ce RS |
2015 | if (input_file_statable_p) |
2016 | { | |
f75d7a91 | 2017 | mode_t mode_mask = 07777; |
a8031457 | 2018 | if (!NILP (preserve_uid_gid)) |
b59b67c5 PE |
2019 | { |
2020 | /* Attempt to change owner and group. If that doesn't work | |
2021 | attempt to change just the group, as that is sometimes allowed. | |
2022 | Adjust the mode mask to eliminate setuid or setgid bits | |
2023 | that are inappropriate if the owner and group are wrong. */ | |
2024 | if (fchown (ofd, st.st_uid, st.st_gid) != 0) | |
2025 | { | |
2026 | mode_mask &= ~06000; | |
2027 | if (fchown (ofd, -1, st.st_gid) == 0) | |
2028 | mode_mask |= 02000; | |
2029 | } | |
2030 | } | |
2031 | if (fchmod (ofd, st.st_mode & mode_mask) != 0) | |
81e56e61 | 2032 | report_file_error ("Doing chmod", Fcons (newname, Qnil)); |
586702ce | 2033 | } |
b016179b | 2034 | #endif /* not MSDOS */ |
586702ce | 2035 | |
574c05e2 KK |
2036 | #if HAVE_LIBSELINUX |
2037 | if (conlength > 0) | |
2038 | { | |
24c51a09 | 2039 | /* Set the modified context back to the file. */ |
f75d7a91 | 2040 | bool fail = fsetfilecon (ofd, con) != 0; |
24c51a09 GM |
2041 | /* See http://debbugs.gnu.org/11245 for ENOTSUP. */ |
2042 | if (fail && errno != ENOTSUP) | |
574c05e2 KK |
2043 | report_file_error ("Doing fsetfilecon", Fcons (newname, Qnil)); |
2044 | ||
2045 | freecon (con); | |
2046 | } | |
2047 | #endif | |
2048 | ||
f73b0ada | 2049 | if (input_file_statable_p) |
570d7624 | 2050 | { |
8ca6602c | 2051 | if (!NILP (keep_time)) |
570d7624 | 2052 | { |
d35af63c PE |
2053 | EMACS_TIME atime = get_stat_atime (&st); |
2054 | EMACS_TIME mtime = get_stat_mtime (&st); | |
2055 | if (set_file_times (ofd, SSDATA (encoded_newname), atime, mtime)) | |
24b1ddad KS |
2056 | xsignal2 (Qfile_date_error, |
2057 | build_string ("Cannot set file date"), newname); | |
570d7624 | 2058 | } |
570d7624 JB |
2059 | } |
2060 | ||
d35af63c PE |
2061 | if (emacs_close (ofd) < 0) |
2062 | report_file_error ("I/O error", Fcons (newname, Qnil)); | |
2063 | ||
68c45bf0 | 2064 | emacs_close (ifd); |
b016179b | 2065 | |
ed68db4d | 2066 | #ifdef MSDOS |
b016179b EZ |
2067 | if (input_file_statable_p) |
2068 | { | |
2069 | /* In DJGPP v2.0 and later, fstat usually returns true file mode bits, | |
2070 | and if it can't, it tells so. Otherwise, under MSDOS we usually | |
2071 | get only the READ bit, which will make the copied file read-only, | |
2072 | so it's better not to chmod at all. */ | |
2073 | if ((_djstat_flags & _STFAIL_WRITEBIT) == 0) | |
2074 | chmod (SDATA (encoded_newname), st.st_mode & 07777); | |
2075 | } | |
ed68db4d | 2076 | #endif /* MSDOS */ |
b016179b | 2077 | #endif /* not WINDOWSNT */ |
5acac34e | 2078 | |
b5148e85 RS |
2079 | /* Discard the unwind protects. */ |
2080 | specpdl_ptr = specpdl + count; | |
2081 | ||
570d7624 JB |
2082 | UNGCPRO; |
2083 | return Qnil; | |
2084 | } | |
385b6cc7 | 2085 | \f |
9bbe01fb | 2086 | DEFUN ("make-directory-internal", Fmake_directory_internal, |
353cfc19 | 2087 | Smake_directory_internal, 1, 1, 0, |
8c1a1077 | 2088 | doc: /* Create a new directory named DIRECTORY. */) |
5842a27b | 2089 | (Lisp_Object directory) |
570d7624 | 2090 | { |
5976c3fe | 2091 | const char *dir; |
32f4334d | 2092 | Lisp_Object handler; |
b1d1b865 | 2093 | Lisp_Object encoded_dir; |
570d7624 | 2094 | |
b7826503 | 2095 | CHECK_STRING (directory); |
3b7f6e60 | 2096 | directory = Fexpand_file_name (directory, Qnil); |
32f4334d | 2097 | |
3b7f6e60 | 2098 | handler = Ffind_file_name_handler (directory, Qmake_directory_internal); |
32f4334d | 2099 | if (!NILP (handler)) |
3b7f6e60 | 2100 | return call2 (handler, Qmake_directory_internal, directory); |
9bbe01fb | 2101 | |
b1d1b865 RS |
2102 | encoded_dir = ENCODE_FILE (directory); |
2103 | ||
5976c3fe | 2104 | dir = SSDATA (encoded_dir); |
570d7624 | 2105 | |
5e570b75 RS |
2106 | #ifdef WINDOWSNT |
2107 | if (mkdir (dir) != 0) | |
2108 | #else | |
09450bae | 2109 | if (mkdir (dir, 0777 & ~auto_saving_dir_umask) != 0) |
5e570b75 | 2110 | #endif |
a9f2aeae | 2111 | report_file_error ("Creating directory", list1 (directory)); |
570d7624 | 2112 | |
32f4334d | 2113 | return Qnil; |
570d7624 JB |
2114 | } |
2115 | ||
9d8f3bd9 MA |
2116 | DEFUN ("delete-directory-internal", Fdelete_directory_internal, |
2117 | Sdelete_directory_internal, 1, 1, 0, | |
efdc16c9 | 2118 | doc: /* Delete the directory named DIRECTORY. Does not follow symlinks. */) |
5842a27b | 2119 | (Lisp_Object directory) |
570d7624 | 2120 | { |
5976c3fe | 2121 | const char *dir; |
b1d1b865 | 2122 | Lisp_Object encoded_dir; |
570d7624 | 2123 | |
b7826503 | 2124 | CHECK_STRING (directory); |
3b7f6e60 | 2125 | directory = Fdirectory_file_name (Fexpand_file_name (directory, Qnil)); |
b1d1b865 | 2126 | encoded_dir = ENCODE_FILE (directory); |
5976c3fe | 2127 | dir = SSDATA (encoded_dir); |
b1d1b865 | 2128 | |
570d7624 | 2129 | if (rmdir (dir) != 0) |
a9f2aeae | 2130 | report_file_error ("Removing directory", list1 (directory)); |
570d7624 JB |
2131 | |
2132 | return Qnil; | |
2133 | } | |
2134 | ||
2e2bbddb | 2135 | DEFUN ("delete-file", Fdelete_file, Sdelete_file, 1, 2, |
f1a5d776 CY |
2136 | "(list (read-file-name \ |
2137 | (if (and delete-by-moving-to-trash (null current-prefix-arg)) \ | |
2138 | \"Move file to trash: \" \"Delete file: \") \ | |
2139 | nil default-directory (confirm-nonexistent-file-or-buffer)) \ | |
2140 | (null current-prefix-arg))", | |
efdc16c9 | 2141 | doc: /* Delete file named FILENAME. If it is a symlink, remove the symlink. |
53967e09 | 2142 | If file has multiple names, it continues to exist with the other names. |
f1a5d776 CY |
2143 | TRASH non-nil means to trash the file instead of deleting, provided |
2144 | `delete-by-moving-to-trash' is non-nil. | |
53967e09 | 2145 | |
f1a5d776 CY |
2146 | When called interactively, TRASH is t if no prefix argument is given. |
2147 | With a prefix argument, TRASH is nil. */) | |
5842a27b | 2148 | (Lisp_Object filename, Lisp_Object trash) |
570d7624 | 2149 | { |
32f4334d | 2150 | Lisp_Object handler; |
b1d1b865 | 2151 | Lisp_Object encoded_file; |
efdc16c9 | 2152 | struct gcpro gcpro1; |
b1d1b865 | 2153 | |
efdc16c9 | 2154 | GCPRO1 (filename); |
b4bd27c5 RS |
2155 | if (!NILP (Ffile_directory_p (filename)) |
2156 | && NILP (Ffile_symlink_p (filename))) | |
24b1ddad KS |
2157 | xsignal2 (Qfile_error, |
2158 | build_string ("Removing old name: is a directory"), | |
2159 | filename); | |
efdc16c9 | 2160 | UNGCPRO; |
570d7624 | 2161 | filename = Fexpand_file_name (filename, Qnil); |
32f4334d | 2162 | |
49307295 | 2163 | handler = Ffind_file_name_handler (filename, Qdelete_file); |
32f4334d | 2164 | if (!NILP (handler)) |
f5783df3 | 2165 | return call3 (handler, Qdelete_file, filename, trash); |
32f4334d | 2166 | |
f1a5d776 | 2167 | if (delete_by_moving_to_trash && !NILP (trash)) |
6cf29fe8 JR |
2168 | return call1 (Qmove_file_to_trash, filename); |
2169 | ||
b1d1b865 RS |
2170 | encoded_file = ENCODE_FILE (filename); |
2171 | ||
42a5b22f | 2172 | if (0 > unlink (SSDATA (encoded_file))) |
a9f2aeae | 2173 | report_file_error ("Removing old name", list1 (filename)); |
8a9b0da9 | 2174 | return Qnil; |
570d7624 JB |
2175 | } |
2176 | ||
385b6cc7 | 2177 | static Lisp_Object |
971de7fb | 2178 | internal_delete_file_1 (Lisp_Object ignore) |
385b6cc7 RS |
2179 | { |
2180 | return Qt; | |
2181 | } | |
2182 | ||
f75d7a91 | 2183 | /* Delete file FILENAME. |
f1a5d776 | 2184 | This ignores `delete-by-moving-to-trash'. */ |
385b6cc7 | 2185 | |
f75d7a91 | 2186 | void |
f1a5d776 | 2187 | internal_delete_file (Lisp_Object filename) |
385b6cc7 | 2188 | { |
f75d7a91 PE |
2189 | internal_condition_case_2 (Fdelete_file, filename, Qnil, |
2190 | Qt, internal_delete_file_1); | |
385b6cc7 RS |
2191 | } |
2192 | \f | |
570d7624 | 2193 | DEFUN ("rename-file", Frename_file, Srename_file, 2, 3, |
7c0f6118 | 2194 | "fRename file: \nGRename %s to file: \np", |
a4e03fe5 | 2195 | doc: /* Rename FILE as NEWNAME. Both args must be strings. |
8c1a1077 PJ |
2196 | If file has names other than FILE, it continues to have those names. |
2197 | Signals a `file-already-exists' error if a file NEWNAME already exists | |
2198 | unless optional third argument OK-IF-ALREADY-EXISTS is non-nil. | |
2199 | A number as third arg means request confirmation if NEWNAME already exists. | |
2200 | This is what happens in interactive use with M-x. */) | |
5842a27b | 2201 | (Lisp_Object file, Lisp_Object newname, Lisp_Object ok_if_already_exists) |
570d7624 | 2202 | { |
32f4334d | 2203 | Lisp_Object handler; |
f72b5416 JD |
2204 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; |
2205 | Lisp_Object encoded_file, encoded_newname, symlink_target; | |
570d7624 | 2206 | |
f72b5416 JD |
2207 | symlink_target = encoded_file = encoded_newname = Qnil; |
2208 | GCPRO5 (file, newname, encoded_file, encoded_newname, symlink_target); | |
b7826503 PJ |
2209 | CHECK_STRING (file); |
2210 | CHECK_STRING (newname); | |
3b7f6e60 | 2211 | file = Fexpand_file_name (file, Qnil); |
1ffc5c90 | 2212 | |
f83caf70 EZ |
2213 | if ((!NILP (Ffile_directory_p (newname))) |
2214 | #ifdef DOS_NT | |
2215 | /* If the file names are identical but for the case, | |
2216 | don't attempt to move directory to itself. */ | |
2217 | && (NILP (Fstring_equal (Fdowncase (file), Fdowncase (newname)))) | |
2218 | #endif | |
2219 | ) | |
8719abec CY |
2220 | { |
2221 | Lisp_Object fname = NILP (Ffile_directory_p (file)) | |
2222 | ? file : Fdirectory_file_name (file); | |
2223 | newname = Fexpand_file_name (Ffile_name_nondirectory (fname), newname); | |
2224 | } | |
1ffc5c90 RS |
2225 | else |
2226 | newname = Fexpand_file_name (newname, Qnil); | |
32f4334d RS |
2227 | |
2228 | /* If the file name has special constructs in it, | |
2229 | call the corresponding file handler. */ | |
3b7f6e60 | 2230 | handler = Ffind_file_name_handler (file, Qrename_file); |
51cf6d37 | 2231 | if (NILP (handler)) |
49307295 | 2232 | handler = Ffind_file_name_handler (newname, Qrename_file); |
32f4334d | 2233 | if (!NILP (handler)) |
36712b0a | 2234 | RETURN_UNGCPRO (call4 (handler, Qrename_file, |
3b7f6e60 | 2235 | file, newname, ok_if_already_exists)); |
32f4334d | 2236 | |
b1d1b865 RS |
2237 | encoded_file = ENCODE_FILE (file); |
2238 | encoded_newname = ENCODE_FILE (newname); | |
2239 | ||
bc77278f EZ |
2240 | #ifdef DOS_NT |
2241 | /* If the file names are identical but for the case, don't ask for | |
2242 | confirmation: they simply want to change the letter-case of the | |
2243 | file name. */ | |
2244 | if (NILP (Fstring_equal (Fdowncase (file), Fdowncase (newname)))) | |
2245 | #endif | |
265a9e55 | 2246 | if (NILP (ok_if_already_exists) |
93c30b5f | 2247 | || INTEGERP (ok_if_already_exists)) |
2f868094 | 2248 | barf_or_query_if_file_exists (newname, "rename to it", |
b8b29dc9 | 2249 | INTEGERP (ok_if_already_exists), 0, 0); |
42a5b22f | 2250 | if (0 > rename (SSDATA (encoded_file), SSDATA (encoded_newname))) |
570d7624 JB |
2251 | { |
2252 | if (errno == EXDEV) | |
2253 | { | |
d311d28c | 2254 | ptrdiff_t count; |
f72b5416 | 2255 | symlink_target = Ffile_symlink_p (file); |
440c7d00 JD |
2256 | if (! NILP (symlink_target)) |
2257 | Fmake_symbolic_link (symlink_target, newname, | |
f59abab9 | 2258 | NILP (ok_if_already_exists) ? Qnil : Qt); |
ae0d7250 | 2259 | else if (!NILP (Ffile_directory_p (file))) |
8719abec CY |
2260 | call4 (Qcopy_directory, file, newname, Qt, Qnil); |
2261 | else | |
2262 | /* We have already prompted if it was an integer, so don't | |
2263 | have copy-file prompt again. */ | |
586702ce | 2264 | Fcopy_file (file, newname, |
586702ce | 2265 | NILP (ok_if_already_exists) ? Qnil : Qt, |
574c05e2 | 2266 | Qt, Qt, Qt); |
d550c425 | 2267 | |
6bddfc97 | 2268 | count = SPECPDL_INDEX (); |
d2b66acf | 2269 | specbind (Qdelete_by_moving_to_trash, Qnil); |
8fab2362 | 2270 | |
ae0d7250 | 2271 | if (!NILP (Ffile_directory_p (file)) && NILP (symlink_target)) |
8719abec CY |
2272 | call2 (Qdelete_directory, file, Qt); |
2273 | else | |
f1a5d776 | 2274 | Fdelete_file (file, Qnil); |
6bddfc97 | 2275 | unbind_to (count, Qnil); |
570d7624 JB |
2276 | } |
2277 | else | |
a9f2aeae | 2278 | report_file_error ("Renaming", list2 (file, newname)); |
570d7624 JB |
2279 | } |
2280 | UNGCPRO; | |
2281 | return Qnil; | |
2282 | } | |
2283 | ||
2284 | DEFUN ("add-name-to-file", Fadd_name_to_file, Sadd_name_to_file, 2, 3, | |
7c0f6118 | 2285 | "fAdd name to file: \nGName to add to %s: \np", |
a4e03fe5 | 2286 | doc: /* Give FILE additional name NEWNAME. Both args must be strings. |
8c1a1077 PJ |
2287 | Signals a `file-already-exists' error if a file NEWNAME already exists |
2288 | unless optional third argument OK-IF-ALREADY-EXISTS is non-nil. | |
2289 | A number as third arg means request confirmation if NEWNAME already exists. | |
2290 | This is what happens in interactive use with M-x. */) | |
5842a27b | 2291 | (Lisp_Object file, Lisp_Object newname, Lisp_Object ok_if_already_exists) |
570d7624 | 2292 | { |
32f4334d | 2293 | Lisp_Object handler; |
b1d1b865 RS |
2294 | Lisp_Object encoded_file, encoded_newname; |
2295 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; | |
570d7624 | 2296 | |
b1d1b865 RS |
2297 | GCPRO4 (file, newname, encoded_file, encoded_newname); |
2298 | encoded_file = encoded_newname = Qnil; | |
b7826503 PJ |
2299 | CHECK_STRING (file); |
2300 | CHECK_STRING (newname); | |
3b7f6e60 | 2301 | file = Fexpand_file_name (file, Qnil); |
1ffc5c90 RS |
2302 | |
2303 | if (!NILP (Ffile_directory_p (newname))) | |
2304 | newname = Fexpand_file_name (Ffile_name_nondirectory (file), newname); | |
2305 | else | |
2306 | newname = Fexpand_file_name (newname, Qnil); | |
32f4334d RS |
2307 | |
2308 | /* If the file name has special constructs in it, | |
2309 | call the corresponding file handler. */ | |
3b7f6e60 | 2310 | handler = Ffind_file_name_handler (file, Qadd_name_to_file); |
32f4334d | 2311 | if (!NILP (handler)) |
3b7f6e60 | 2312 | RETURN_UNGCPRO (call4 (handler, Qadd_name_to_file, file, |
36712b0a | 2313 | newname, ok_if_already_exists)); |
32f4334d | 2314 | |
adc6741c RS |
2315 | /* If the new name has special constructs in it, |
2316 | call the corresponding file handler. */ | |
2317 | handler = Ffind_file_name_handler (newname, Qadd_name_to_file); | |
2318 | if (!NILP (handler)) | |
3b7f6e60 | 2319 | RETURN_UNGCPRO (call4 (handler, Qadd_name_to_file, file, |
adc6741c RS |
2320 | newname, ok_if_already_exists)); |
2321 | ||
b1d1b865 RS |
2322 | encoded_file = ENCODE_FILE (file); |
2323 | encoded_newname = ENCODE_FILE (newname); | |
2324 | ||
265a9e55 | 2325 | if (NILP (ok_if_already_exists) |
93c30b5f | 2326 | || INTEGERP (ok_if_already_exists)) |
2f868094 | 2327 | barf_or_query_if_file_exists (newname, "make it a new name", |
b8b29dc9 | 2328 | INTEGERP (ok_if_already_exists), 0, 0); |
5e570b75 | 2329 | |
42a5b22f PE |
2330 | unlink (SSDATA (newname)); |
2331 | if (0 > link (SSDATA (encoded_file), SSDATA (encoded_newname))) | |
a9f2aeae | 2332 | report_file_error ("Adding new name", list2 (file, newname)); |
570d7624 JB |
2333 | |
2334 | UNGCPRO; | |
2335 | return Qnil; | |
2336 | } | |
2337 | ||
570d7624 | 2338 | DEFUN ("make-symbolic-link", Fmake_symbolic_link, Smake_symbolic_link, 2, 3, |
7c0f6118 | 2339 | "FMake symbolic link to file: \nGMake symbolic link to file %s: \np", |
68780e2a RS |
2340 | doc: /* Make a symbolic link to FILENAME, named LINKNAME. |
2341 | Both args must be strings. | |
8c1a1077 PJ |
2342 | Signals a `file-already-exists' error if a file LINKNAME already exists |
2343 | unless optional third argument OK-IF-ALREADY-EXISTS is non-nil. | |
2344 | A number as third arg means request confirmation if LINKNAME already exists. | |
2345 | This happens for interactive use with M-x. */) | |
5842a27b | 2346 | (Lisp_Object filename, Lisp_Object linkname, Lisp_Object ok_if_already_exists) |
570d7624 | 2347 | { |
32f4334d | 2348 | Lisp_Object handler; |
b1d1b865 RS |
2349 | Lisp_Object encoded_filename, encoded_linkname; |
2350 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; | |
570d7624 | 2351 | |
b1d1b865 RS |
2352 | GCPRO4 (filename, linkname, encoded_filename, encoded_linkname); |
2353 | encoded_filename = encoded_linkname = Qnil; | |
b7826503 PJ |
2354 | CHECK_STRING (filename); |
2355 | CHECK_STRING (linkname); | |
d9bc1c99 RS |
2356 | /* If the link target has a ~, we must expand it to get |
2357 | a truly valid file name. Otherwise, do not expand; | |
2358 | we want to permit links to relative file names. */ | |
d5db4077 | 2359 | if (SREF (filename, 0) == '~') |
d9bc1c99 | 2360 | filename = Fexpand_file_name (filename, Qnil); |
1ffc5c90 RS |
2361 | |
2362 | if (!NILP (Ffile_directory_p (linkname))) | |
dac24db4 | 2363 | linkname = Fexpand_file_name (Ffile_name_nondirectory (filename), linkname); |
1ffc5c90 RS |
2364 | else |
2365 | linkname = Fexpand_file_name (linkname, Qnil); | |
32f4334d RS |
2366 | |
2367 | /* If the file name has special constructs in it, | |
2368 | call the corresponding file handler. */ | |
49307295 | 2369 | handler = Ffind_file_name_handler (filename, Qmake_symbolic_link); |
32f4334d | 2370 | if (!NILP (handler)) |
36712b0a KH |
2371 | RETURN_UNGCPRO (call4 (handler, Qmake_symbolic_link, filename, |
2372 | linkname, ok_if_already_exists)); | |
32f4334d | 2373 | |
adc6741c RS |
2374 | /* If the new link name has special constructs in it, |
2375 | call the corresponding file handler. */ | |
2376 | handler = Ffind_file_name_handler (linkname, Qmake_symbolic_link); | |
2377 | if (!NILP (handler)) | |
2378 | RETURN_UNGCPRO (call4 (handler, Qmake_symbolic_link, filename, | |
2379 | linkname, ok_if_already_exists)); | |
2380 | ||
b1d1b865 RS |
2381 | encoded_filename = ENCODE_FILE (filename); |
2382 | encoded_linkname = ENCODE_FILE (linkname); | |
2383 | ||
265a9e55 | 2384 | if (NILP (ok_if_already_exists) |
93c30b5f | 2385 | || INTEGERP (ok_if_already_exists)) |
2f868094 | 2386 | barf_or_query_if_file_exists (linkname, "make it a link", |
b8b29dc9 | 2387 | INTEGERP (ok_if_already_exists), 0, 0); |
42a5b22f PE |
2388 | if (0 > symlink (SSDATA (encoded_filename), |
2389 | SSDATA (encoded_linkname))) | |
570d7624 JB |
2390 | { |
2391 | /* If we didn't complain already, silently delete existing file. */ | |
2392 | if (errno == EEXIST) | |
2393 | { | |
42a5b22f PE |
2394 | unlink (SSDATA (encoded_linkname)); |
2395 | if (0 <= symlink (SSDATA (encoded_filename), | |
2396 | SSDATA (encoded_linkname))) | |
1a04498e KH |
2397 | { |
2398 | UNGCPRO; | |
2399 | return Qnil; | |
2400 | } | |
570d7624 | 2401 | } |
d9d0d182 PE |
2402 | if (errno == ENOSYS) |
2403 | { | |
2404 | UNGCPRO; | |
2405 | xsignal1 (Qfile_error, | |
2406 | build_string ("Symbolic links are not supported")); | |
2407 | } | |
570d7624 | 2408 | |
a9f2aeae | 2409 | report_file_error ("Making symbolic link", list2 (filename, linkname)); |
570d7624 JB |
2410 | } |
2411 | UNGCPRO; | |
2412 | return Qnil; | |
e89b536d | 2413 | } |
570d7624 | 2414 | |
570d7624 | 2415 | \f |
a7ca3326 | 2416 | DEFUN ("file-name-absolute-p", Ffile_name_absolute_p, Sfile_name_absolute_p, |
570d7624 | 2417 | 1, 1, 0, |
8c1a1077 PJ |
2418 | doc: /* Return t if file FILENAME specifies an absolute file name. |
2419 | On Unix, this is a name starting with a `/' or a `~'. */) | |
5842a27b | 2420 | (Lisp_Object filename) |
570d7624 | 2421 | { |
b7826503 | 2422 | CHECK_STRING (filename); |
5976c3fe | 2423 | return file_name_absolute_p (SSDATA (filename)) ? Qt : Qnil; |
570d7624 | 2424 | } |
3beeedfe | 2425 | \f |
bb385a92 EZ |
2426 | /* Return true if FILENAME exists. */ |
2427 | bool | |
2428 | check_existing (const char *filename) | |
2429 | { | |
2430 | #ifdef DOS_NT | |
2431 | /* The full emulation of Posix 'stat' is too expensive on | |
2432 | DOS/Windows, when all we want to know is whether the file exists. | |
2433 | So we use 'access' instead, which is much more lightweight. */ | |
2434 | return (access (filename, F_OK) >= 0); | |
2435 | #else | |
2436 | struct stat st; | |
2437 | return (stat (filename, &st) >= 0); | |
2438 | #endif | |
2439 | } | |
2440 | ||
f75d7a91 | 2441 | /* Return true if file FILENAME exists and can be executed. */ |
3beeedfe | 2442 | |
f75d7a91 | 2443 | static bool |
971de7fb | 2444 | check_executable (char *filename) |
3beeedfe | 2445 | { |
3be3c08e | 2446 | #ifdef DOS_NT |
3be3c08e RS |
2447 | struct stat st; |
2448 | if (stat (filename, &st) < 0) | |
2449 | return 0; | |
199607e4 | 2450 | return ((st.st_mode & S_IEXEC) != 0); |
3be3c08e | 2451 | #else /* not DOS_NT */ |
de0be7dd RS |
2452 | #ifdef HAVE_EUIDACCESS |
2453 | return (euidaccess (filename, 1) >= 0); | |
3beeedfe RS |
2454 | #else |
2455 | /* Access isn't quite right because it uses the real uid | |
2456 | and we really want to test with the effective uid. | |
2457 | But Unix doesn't give us a right way to do it. */ | |
2458 | return (access (filename, 1) >= 0); | |
2459 | #endif | |
3be3c08e | 2460 | #endif /* not DOS_NT */ |
3beeedfe RS |
2461 | } |
2462 | ||
f75d7a91 | 2463 | /* Return true if file FILENAME exists and can be written. */ |
3beeedfe | 2464 | |
f75d7a91 | 2465 | static bool |
8ea90aa3 | 2466 | check_writable (const char *filename) |
3beeedfe | 2467 | { |
3be3c08e RS |
2468 | #ifdef MSDOS |
2469 | struct stat st; | |
2470 | if (stat (filename, &st) < 0) | |
2471 | return 0; | |
f68c809d | 2472 | return (st.st_mode & S_IWRITE || S_ISDIR (st.st_mode)); |
3be3c08e | 2473 | #else /* not MSDOS */ |
41f3fb38 | 2474 | #ifdef HAVE_EUIDACCESS |
f75d7a91 | 2475 | bool res = (euidaccess (filename, 2) >= 0); |
e1b01a3a KB |
2476 | #ifdef CYGWIN |
2477 | /* euidaccess may have returned failure because Cygwin couldn't | |
2478 | determine the file's UID or GID; if so, we return success. */ | |
2479 | if (!res) | |
2480 | { | |
2481 | struct stat st; | |
2482 | if (stat (filename, &st) < 0) | |
2483 | return 0; | |
2484 | res = (st.st_uid == -1 || st.st_gid == -1); | |
2485 | } | |
2486 | #endif /* CYGWIN */ | |
2487 | return res; | |
2488 | #else /* not HAVE_EUIDACCESS */ | |
3beeedfe RS |
2489 | /* Access isn't quite right because it uses the real uid |
2490 | and we really want to test with the effective uid. | |
2491 | But Unix doesn't give us a right way to do it. | |
2492 | Opening with O_WRONLY could work for an ordinary file, | |
2493 | but would lose for directories. */ | |
2494 | return (access (filename, 2) >= 0); | |
e1b01a3a | 2495 | #endif /* not HAVE_EUIDACCESS */ |
3be3c08e | 2496 | #endif /* not MSDOS */ |
3beeedfe | 2497 | } |
570d7624 | 2498 | |
a7ca3326 | 2499 | DEFUN ("file-exists-p", Ffile_exists_p, Sfile_exists_p, 1, 1, 0, |
68780e2a RS |
2500 | doc: /* Return t if file FILENAME exists (whether or not you can read it.) |
2501 | See also `file-readable-p' and `file-attributes'. | |
2502 | This returns nil for a symlink to a nonexistent file. | |
2503 | Use `file-symlink-p' to test for such links. */) | |
5842a27b | 2504 | (Lisp_Object filename) |
570d7624 | 2505 | { |
199607e4 | 2506 | Lisp_Object absname; |
32f4334d | 2507 | Lisp_Object handler; |
570d7624 | 2508 | |
b7826503 | 2509 | CHECK_STRING (filename); |
199607e4 | 2510 | absname = Fexpand_file_name (filename, Qnil); |
32f4334d RS |
2511 | |
2512 | /* If the file name has special constructs in it, | |
2513 | call the corresponding file handler. */ | |
199607e4 | 2514 | handler = Ffind_file_name_handler (absname, Qfile_exists_p); |
32f4334d | 2515 | if (!NILP (handler)) |
199607e4 | 2516 | return call2 (handler, Qfile_exists_p, absname); |
32f4334d | 2517 | |
b1d1b865 RS |
2518 | absname = ENCODE_FILE (absname); |
2519 | ||
bb385a92 | 2520 | return (check_existing (SSDATA (absname))) ? Qt : Qnil; |
570d7624 JB |
2521 | } |
2522 | ||
2523 | DEFUN ("file-executable-p", Ffile_executable_p, Sfile_executable_p, 1, 1, 0, | |
8c1a1077 PJ |
2524 | doc: /* Return t if FILENAME can be executed by you. |
2525 | For a directory, this means you can access files in that directory. */) | |
5842a27b | 2526 | (Lisp_Object filename) |
570d7624 | 2527 | { |
199607e4 | 2528 | Lisp_Object absname; |
32f4334d | 2529 | Lisp_Object handler; |
570d7624 | 2530 | |
b7826503 | 2531 | CHECK_STRING (filename); |
199607e4 | 2532 | absname = Fexpand_file_name (filename, Qnil); |
32f4334d RS |
2533 | |
2534 | /* If the file name has special constructs in it, | |
2535 | call the corresponding file handler. */ | |
199607e4 | 2536 | handler = Ffind_file_name_handler (absname, Qfile_executable_p); |
32f4334d | 2537 | if (!NILP (handler)) |
199607e4 | 2538 | return call2 (handler, Qfile_executable_p, absname); |
32f4334d | 2539 | |
b1d1b865 RS |
2540 | absname = ENCODE_FILE (absname); |
2541 | ||
42a5b22f | 2542 | return (check_executable (SSDATA (absname)) ? Qt : Qnil); |
570d7624 JB |
2543 | } |
2544 | ||
a7ca3326 | 2545 | DEFUN ("file-readable-p", Ffile_readable_p, Sfile_readable_p, 1, 1, 0, |
8c1a1077 PJ |
2546 | doc: /* Return t if file FILENAME exists and you can read it. |
2547 | See also `file-exists-p' and `file-attributes'. */) | |
5842a27b | 2548 | (Lisp_Object filename) |
570d7624 | 2549 | { |
199607e4 | 2550 | Lisp_Object absname; |
32f4334d | 2551 | Lisp_Object handler; |
4018b5ef | 2552 | int desc; |
bb369dc6 RS |
2553 | int flags; |
2554 | struct stat statbuf; | |
570d7624 | 2555 | |
b7826503 | 2556 | CHECK_STRING (filename); |
199607e4 | 2557 | absname = Fexpand_file_name (filename, Qnil); |
32f4334d RS |
2558 | |
2559 | /* If the file name has special constructs in it, | |
2560 | call the corresponding file handler. */ | |
199607e4 | 2561 | handler = Ffind_file_name_handler (absname, Qfile_readable_p); |
32f4334d | 2562 | if (!NILP (handler)) |
199607e4 | 2563 | return call2 (handler, Qfile_readable_p, absname); |
32f4334d | 2564 | |
b1d1b865 RS |
2565 | absname = ENCODE_FILE (absname); |
2566 | ||
5e617bc2 | 2567 | #if defined (DOS_NT) || defined (macintosh) |
fb4c6c96 AC |
2568 | /* Under MS-DOS, Windows, and Macintosh, open does not work for |
2569 | directories. */ | |
d5db4077 | 2570 | if (access (SDATA (absname), 0) == 0) |
a8a7d065 RS |
2571 | return Qt; |
2572 | return Qnil; | |
fb4c6c96 | 2573 | #else /* not DOS_NT and not macintosh */ |
bb369dc6 | 2574 | flags = O_RDONLY; |
ae0d7250 | 2575 | #ifdef O_NONBLOCK |
bb369dc6 RS |
2576 | /* Opening a fifo without O_NONBLOCK can wait. |
2577 | We don't want to wait. But we don't want to mess wth O_NONBLOCK | |
2578 | except in the case of a fifo, on a system which handles it. */ | |
42a5b22f | 2579 | desc = stat (SSDATA (absname), &statbuf); |
bb369dc6 RS |
2580 | if (desc < 0) |
2581 | return Qnil; | |
2582 | if (S_ISFIFO (statbuf.st_mode)) | |
2583 | flags |= O_NONBLOCK; | |
2584 | #endif | |
42a5b22f | 2585 | desc = emacs_open (SSDATA (absname), flags, 0); |
4018b5ef RS |
2586 | if (desc < 0) |
2587 | return Qnil; | |
68c45bf0 | 2588 | emacs_close (desc); |
4018b5ef | 2589 | return Qt; |
fb4c6c96 | 2590 | #endif /* not DOS_NT and not macintosh */ |
570d7624 JB |
2591 | } |
2592 | ||
f793dc6c RS |
2593 | /* Having this before file-symlink-p mysteriously caused it to be forgotten |
2594 | on the RT/PC. */ | |
2595 | DEFUN ("file-writable-p", Ffile_writable_p, Sfile_writable_p, 1, 1, 0, | |
8c1a1077 | 2596 | doc: /* Return t if file FILENAME can be written or created by you. */) |
5842a27b | 2597 | (Lisp_Object filename) |
f793dc6c | 2598 | { |
b1d1b865 | 2599 | Lisp_Object absname, dir, encoded; |
f793dc6c | 2600 | Lisp_Object handler; |
f793dc6c | 2601 | |
b7826503 | 2602 | CHECK_STRING (filename); |
199607e4 | 2603 | absname = Fexpand_file_name (filename, Qnil); |
f793dc6c RS |
2604 | |
2605 | /* If the file name has special constructs in it, | |
2606 | call the corresponding file handler. */ | |
199607e4 | 2607 | handler = Ffind_file_name_handler (absname, Qfile_writable_p); |
f793dc6c | 2608 | if (!NILP (handler)) |
199607e4 | 2609 | return call2 (handler, Qfile_writable_p, absname); |
f793dc6c | 2610 | |
b1d1b865 | 2611 | encoded = ENCODE_FILE (absname); |
bb385a92 | 2612 | if (check_existing (SSDATA (encoded))) |
42a5b22f | 2613 | return (check_writable (SSDATA (encoded)) |
f793dc6c | 2614 | ? Qt : Qnil); |
b1d1b865 | 2615 | |
199607e4 | 2616 | dir = Ffile_name_directory (absname); |
f793dc6c RS |
2617 | #ifdef MSDOS |
2618 | if (!NILP (dir)) | |
2619 | dir = Fdirectory_file_name (dir); | |
2620 | #endif /* MSDOS */ | |
b1d1b865 RS |
2621 | |
2622 | dir = ENCODE_FILE (dir); | |
e3e8a75a GM |
2623 | #ifdef WINDOWSNT |
2624 | /* The read-only attribute of the parent directory doesn't affect | |
2625 | whether a file or directory can be created within it. Some day we | |
2626 | should check ACLs though, which do affect this. */ | |
bb385a92 | 2627 | return (access (SDATA (dir), D_OK) < 0) ? Qnil : Qt; |
e3e8a75a | 2628 | #else |
51b59d79 | 2629 | return (check_writable (!NILP (dir) ? SSDATA (dir) : "") |
f793dc6c | 2630 | ? Qt : Qnil); |
e3e8a75a | 2631 | #endif |
f793dc6c RS |
2632 | } |
2633 | \f | |
1f8653eb | 2634 | DEFUN ("access-file", Faccess_file, Saccess_file, 2, 2, 0, |
8c1a1077 PJ |
2635 | doc: /* Access file FILENAME, and get an error if that does not work. |
2636 | The second argument STRING is used in the error message. | |
a4e03fe5 | 2637 | If there is no error, returns nil. */) |
5842a27b | 2638 | (Lisp_Object filename, Lisp_Object string) |
1f8653eb | 2639 | { |
49475635 | 2640 | Lisp_Object handler, encoded_filename, absname; |
1f8653eb RS |
2641 | int fd; |
2642 | ||
b7826503 | 2643 | CHECK_STRING (filename); |
49475635 EZ |
2644 | absname = Fexpand_file_name (filename, Qnil); |
2645 | ||
b7826503 | 2646 | CHECK_STRING (string); |
1f8653eb RS |
2647 | |
2648 | /* If the file name has special constructs in it, | |
2649 | call the corresponding file handler. */ | |
49475635 | 2650 | handler = Ffind_file_name_handler (absname, Qaccess_file); |
1f8653eb | 2651 | if (!NILP (handler)) |
49475635 | 2652 | return call3 (handler, Qaccess_file, absname, string); |
1f8653eb | 2653 | |
49475635 | 2654 | encoded_filename = ENCODE_FILE (absname); |
b1d1b865 | 2655 | |
42a5b22f | 2656 | fd = emacs_open (SSDATA (encoded_filename), O_RDONLY, 0); |
1f8653eb | 2657 | if (fd < 0) |
42a5b22f | 2658 | report_file_error (SSDATA (string), Fcons (filename, Qnil)); |
68c45bf0 | 2659 | emacs_close (fd); |
1f8653eb RS |
2660 | |
2661 | return Qnil; | |
2662 | } | |
2663 | \f | |
a7ca3326 | 2664 | DEFUN ("file-symlink-p", Ffile_symlink_p, Sfile_symlink_p, 1, 1, 0, |
8c1a1077 | 2665 | doc: /* Return non-nil if file FILENAME is the name of a symbolic link. |
1c353c74 | 2666 | The value is the link target, as a string. |
68780e2a RS |
2667 | Otherwise it returns nil. |
2668 | ||
2669 | This function returns t when given the name of a symlink that | |
2670 | points to a nonexistent file. */) | |
5842a27b | 2671 | (Lisp_Object filename) |
570d7624 | 2672 | { |
32f4334d | 2673 | Lisp_Object handler; |
ae0d7250 | 2674 | char *buf; |
ae0d7250 | 2675 | Lisp_Object val; |
d1fdcab7 | 2676 | char readlink_buf[READLINK_BUFSIZE]; |
570d7624 | 2677 | |
b7826503 | 2678 | CHECK_STRING (filename); |
570d7624 JB |
2679 | filename = Fexpand_file_name (filename, Qnil); |
2680 | ||
32f4334d RS |
2681 | /* If the file name has special constructs in it, |
2682 | call the corresponding file handler. */ | |
49307295 | 2683 | handler = Ffind_file_name_handler (filename, Qfile_symlink_p); |
32f4334d RS |
2684 | if (!NILP (handler)) |
2685 | return call2 (handler, Qfile_symlink_p, filename); | |
2686 | ||
b1d1b865 RS |
2687 | filename = ENCODE_FILE (filename); |
2688 | ||
d1fdcab7 PE |
2689 | buf = emacs_readlink (SSDATA (filename), readlink_buf); |
2690 | if (! buf) | |
2691 | return Qnil; | |
efdc16c9 | 2692 | |
d1fdcab7 | 2693 | val = build_string (buf); |
8966b757 | 2694 | if (buf[0] == '/' && strchr (buf, ':')) |
69ac1891 | 2695 | val = concat2 (build_string ("/:"), val); |
d1fdcab7 PE |
2696 | if (buf != readlink_buf) |
2697 | xfree (buf); | |
cd913586 KH |
2698 | val = DECODE_FILE (val); |
2699 | return val; | |
570d7624 JB |
2700 | } |
2701 | ||
a7ca3326 | 2702 | DEFUN ("file-directory-p", Ffile_directory_p, Sfile_directory_p, 1, 1, 0, |
8c1a1077 PJ |
2703 | doc: /* Return t if FILENAME names an existing directory. |
2704 | Symbolic links to directories count as directories. | |
2705 | See `file-symlink-p' to distinguish symlinks. */) | |
5842a27b | 2706 | (Lisp_Object filename) |
570d7624 | 2707 | { |
199607e4 | 2708 | register Lisp_Object absname; |
570d7624 | 2709 | struct stat st; |
32f4334d | 2710 | Lisp_Object handler; |
570d7624 | 2711 | |
4b4deea2 | 2712 | absname = expand_and_dir_to_file (filename, BVAR (current_buffer, directory)); |
570d7624 | 2713 | |
32f4334d RS |
2714 | /* If the file name has special constructs in it, |
2715 | call the corresponding file handler. */ | |
199607e4 | 2716 | handler = Ffind_file_name_handler (absname, Qfile_directory_p); |
32f4334d | 2717 | if (!NILP (handler)) |
199607e4 | 2718 | return call2 (handler, Qfile_directory_p, absname); |
32f4334d | 2719 | |
b1d1b865 RS |
2720 | absname = ENCODE_FILE (absname); |
2721 | ||
42a5b22f | 2722 | if (stat (SSDATA (absname), &st) < 0) |
570d7624 | 2723 | return Qnil; |
f68c809d | 2724 | return S_ISDIR (st.st_mode) ? Qt : Qnil; |
570d7624 JB |
2725 | } |
2726 | ||
a7ca3326 | 2727 | DEFUN ("file-accessible-directory-p", Ffile_accessible_directory_p, |
16a97296 | 2728 | Sfile_accessible_directory_p, 1, 1, 0, |
e385ec41 RS |
2729 | doc: /* Return t if file FILENAME names a directory you can open. |
2730 | For the value to be t, FILENAME must specify the name of a directory as a file, | |
2731 | and the directory must allow you to open files in it. In order to use a | |
8c1a1077 PJ |
2732 | directory as a buffer's current directory, this predicate must return true. |
2733 | A directory name spec may be given instead; then the value is t | |
2734 | if the directory so specified exists and really is a readable and | |
2735 | searchable directory. */) | |
5842a27b | 2736 | (Lisp_Object filename) |
b72dea2a | 2737 | { |
32f4334d | 2738 | Lisp_Object handler; |
f75d7a91 | 2739 | bool tem; |
d26859eb | 2740 | struct gcpro gcpro1; |
32f4334d RS |
2741 | |
2742 | /* If the file name has special constructs in it, | |
2743 | call the corresponding file handler. */ | |
49307295 | 2744 | handler = Ffind_file_name_handler (filename, Qfile_accessible_directory_p); |
32f4334d RS |
2745 | if (!NILP (handler)) |
2746 | return call2 (handler, Qfile_accessible_directory_p, filename); | |
2747 | ||
d26859eb | 2748 | GCPRO1 (filename); |
1a04498e KH |
2749 | tem = (NILP (Ffile_directory_p (filename)) |
2750 | || NILP (Ffile_executable_p (filename))); | |
d26859eb | 2751 | UNGCPRO; |
1a04498e | 2752 | return tem ? Qnil : Qt; |
b72dea2a JB |
2753 | } |
2754 | ||
f793dc6c | 2755 | DEFUN ("file-regular-p", Ffile_regular_p, Sfile_regular_p, 1, 1, 0, |
19a9c3b7 LH |
2756 | doc: /* Return t if FILENAME names a regular file. |
2757 | This is the sort of file that holds an ordinary stream of data bytes. | |
2758 | Symbolic links to regular files count as regular files. | |
2759 | See `file-symlink-p' to distinguish symlinks. */) | |
5842a27b | 2760 | (Lisp_Object filename) |
f793dc6c | 2761 | { |
199607e4 | 2762 | register Lisp_Object absname; |
f793dc6c RS |
2763 | struct stat st; |
2764 | Lisp_Object handler; | |
2765 | ||
4b4deea2 | 2766 | absname = expand_and_dir_to_file (filename, BVAR (current_buffer, directory)); |
f793dc6c RS |
2767 | |
2768 | /* If the file name has special constructs in it, | |
2769 | call the corresponding file handler. */ | |
199607e4 | 2770 | handler = Ffind_file_name_handler (absname, Qfile_regular_p); |
f793dc6c | 2771 | if (!NILP (handler)) |
199607e4 | 2772 | return call2 (handler, Qfile_regular_p, absname); |
f793dc6c | 2773 | |
b1d1b865 RS |
2774 | absname = ENCODE_FILE (absname); |
2775 | ||
c1c4693e RS |
2776 | #ifdef WINDOWSNT |
2777 | { | |
2778 | int result; | |
2779 | Lisp_Object tem = Vw32_get_true_file_attributes; | |
2780 | ||
2781 | /* Tell stat to use expensive method to get accurate info. */ | |
2782 | Vw32_get_true_file_attributes = Qt; | |
d5db4077 | 2783 | result = stat (SDATA (absname), &st); |
c1c4693e RS |
2784 | Vw32_get_true_file_attributes = tem; |
2785 | ||
2786 | if (result < 0) | |
2787 | return Qnil; | |
f68c809d | 2788 | return S_ISREG (st.st_mode) ? Qt : Qnil; |
c1c4693e RS |
2789 | } |
2790 | #else | |
42a5b22f | 2791 | if (stat (SSDATA (absname), &st) < 0) |
f793dc6c | 2792 | return Qnil; |
f68c809d | 2793 | return S_ISREG (st.st_mode) ? Qt : Qnil; |
c1c4693e | 2794 | #endif |
f793dc6c RS |
2795 | } |
2796 | \f | |
574c05e2 KK |
2797 | DEFUN ("file-selinux-context", Ffile_selinux_context, |
2798 | Sfile_selinux_context, 1, 1, 0, | |
fa74b241 CY |
2799 | doc: /* Return SELinux context of file named FILENAME. |
2800 | The return value is a list (USER ROLE TYPE RANGE), where the list | |
2801 | elements are strings naming the user, role, type, and range of the | |
2802 | file's SELinux security context. | |
2803 | ||
2804 | Return (nil nil nil nil) if the file is nonexistent or inaccessible, | |
2805 | or if SELinux is disabled, or if Emacs lacks SELinux support. */) | |
5842a27b | 2806 | (Lisp_Object filename) |
574c05e2 KK |
2807 | { |
2808 | Lisp_Object absname; | |
2809 | Lisp_Object values[4]; | |
2810 | Lisp_Object handler; | |
2811 | #if HAVE_LIBSELINUX | |
2812 | security_context_t con; | |
2813 | int conlength; | |
2814 | context_t context; | |
2815 | #endif | |
2816 | ||
4b4deea2 | 2817 | absname = expand_and_dir_to_file (filename, BVAR (current_buffer, directory)); |
574c05e2 KK |
2818 | |
2819 | /* If the file name has special constructs in it, | |
2820 | call the corresponding file handler. */ | |
2821 | handler = Ffind_file_name_handler (absname, Qfile_selinux_context); | |
2822 | if (!NILP (handler)) | |
2823 | return call2 (handler, Qfile_selinux_context, absname); | |
2824 | ||
2825 | absname = ENCODE_FILE (absname); | |
2826 | ||
2827 | values[0] = Qnil; | |
2828 | values[1] = Qnil; | |
2829 | values[2] = Qnil; | |
2830 | values[3] = Qnil; | |
2831 | #if HAVE_LIBSELINUX | |
2832 | if (is_selinux_enabled ()) | |
2833 | { | |
b14aac08 | 2834 | conlength = lgetfilecon (SSDATA (absname), &con); |
574c05e2 KK |
2835 | if (conlength > 0) |
2836 | { | |
2837 | context = context_new (con); | |
45841e65 KK |
2838 | if (context_user_get (context)) |
2839 | values[0] = build_string (context_user_get (context)); | |
2840 | if (context_role_get (context)) | |
2841 | values[1] = build_string (context_role_get (context)); | |
2842 | if (context_type_get (context)) | |
2843 | values[2] = build_string (context_type_get (context)); | |
2844 | if (context_range_get (context)) | |
2845 | values[3] = build_string (context_range_get (context)); | |
574c05e2 | 2846 | context_free (context); |
c6ba4138 | 2847 | freecon (con); |
574c05e2 | 2848 | } |
574c05e2 KK |
2849 | } |
2850 | #endif | |
2851 | ||
5e617bc2 | 2852 | return Flist (sizeof (values) / sizeof (values[0]), values); |
574c05e2 KK |
2853 | } |
2854 | \f | |
2855 | DEFUN ("set-file-selinux-context", Fset_file_selinux_context, | |
2856 | Sset_file_selinux_context, 2, 2, 0, | |
fa74b241 CY |
2857 | doc: /* Set SELinux context of file named FILENAME to CONTEXT. |
2858 | CONTEXT should be a list (USER ROLE TYPE RANGE), where the list | |
2859 | elements are strings naming the components of a SELinux context. | |
2860 | ||
2861 | This function does nothing if SELinux is disabled, or if Emacs was not | |
2862 | compiled with SELinux support. */) | |
5842a27b | 2863 | (Lisp_Object filename, Lisp_Object context) |
574c05e2 | 2864 | { |
0f4a96b5 | 2865 | Lisp_Object absname; |
574c05e2 | 2866 | Lisp_Object handler; |
0f4a96b5 JB |
2867 | #if HAVE_LIBSELINUX |
2868 | Lisp_Object encoded_absname; | |
574c05e2 KK |
2869 | Lisp_Object user = CAR_SAFE (context); |
2870 | Lisp_Object role = CAR_SAFE (CDR_SAFE (context)); | |
2871 | Lisp_Object type = CAR_SAFE (CDR_SAFE (CDR_SAFE (context))); | |
2872 | Lisp_Object range = CAR_SAFE (CDR_SAFE (CDR_SAFE (CDR_SAFE (context)))); | |
574c05e2 | 2873 | security_context_t con; |
f75d7a91 PE |
2874 | bool fail; |
2875 | int conlength; | |
574c05e2 KK |
2876 | context_t parsed_con; |
2877 | #endif | |
2878 | ||
4b4deea2 | 2879 | absname = Fexpand_file_name (filename, BVAR (current_buffer, directory)); |
574c05e2 KK |
2880 | |
2881 | /* If the file name has special constructs in it, | |
2882 | call the corresponding file handler. */ | |
2883 | handler = Ffind_file_name_handler (absname, Qset_file_selinux_context); | |
2884 | if (!NILP (handler)) | |
2885 | return call3 (handler, Qset_file_selinux_context, absname, context); | |
2886 | ||
0f4a96b5 | 2887 | #if HAVE_LIBSELINUX |
574c05e2 KK |
2888 | if (is_selinux_enabled ()) |
2889 | { | |
2890 | /* Get current file context. */ | |
a9b53ad3 | 2891 | encoded_absname = ENCODE_FILE (absname); |
b14aac08 | 2892 | conlength = lgetfilecon (SSDATA (encoded_absname), &con); |
574c05e2 KK |
2893 | if (conlength > 0) |
2894 | { | |
2895 | parsed_con = context_new (con); | |
2896 | /* Change the parts defined in the parameter.*/ | |
2897 | if (STRINGP (user)) | |
2898 | { | |
b14aac08 | 2899 | if (context_user_set (parsed_con, SSDATA (user))) |
574c05e2 KK |
2900 | error ("Doing context_user_set"); |
2901 | } | |
2902 | if (STRINGP (role)) | |
2903 | { | |
b14aac08 | 2904 | if (context_role_set (parsed_con, SSDATA (role))) |
574c05e2 KK |
2905 | error ("Doing context_role_set"); |
2906 | } | |
2907 | if (STRINGP (type)) | |
2908 | { | |
b14aac08 | 2909 | if (context_type_set (parsed_con, SSDATA (type))) |
574c05e2 KK |
2910 | error ("Doing context_type_set"); |
2911 | } | |
2912 | if (STRINGP (range)) | |
2913 | { | |
b14aac08 | 2914 | if (context_range_set (parsed_con, SSDATA (range))) |
574c05e2 KK |
2915 | error ("Doing context_range_set"); |
2916 | } | |
2917 | ||
24c51a09 | 2918 | /* Set the modified context back to the file. */ |
f75d7a91 PE |
2919 | fail = (lsetfilecon (SSDATA (encoded_absname), |
2920 | context_str (parsed_con)) | |
2921 | != 0); | |
24c51a09 GM |
2922 | /* See http://debbugs.gnu.org/11245 for ENOTSUP. */ |
2923 | if (fail && errno != ENOTSUP) | |
574c05e2 KK |
2924 | report_file_error ("Doing lsetfilecon", Fcons (absname, Qnil)); |
2925 | ||
2926 | context_free (parsed_con); | |
c6ba4138 | 2927 | freecon (con); |
574c05e2 KK |
2928 | } |
2929 | else | |
5e617bc2 | 2930 | report_file_error ("Doing lgetfilecon", Fcons (absname, Qnil)); |
574c05e2 KK |
2931 | } |
2932 | #endif | |
2933 | ||
2934 | return Qnil; | |
2935 | } | |
2936 | \f | |
570d7624 | 2937 | DEFUN ("file-modes", Ffile_modes, Sfile_modes, 1, 1, 0, |
d4a42098 KS |
2938 | doc: /* Return mode bits of file named FILENAME, as an integer. |
2939 | Return nil, if file does not exist or is not accessible. */) | |
5842a27b | 2940 | (Lisp_Object filename) |
570d7624 | 2941 | { |
199607e4 | 2942 | Lisp_Object absname; |
570d7624 | 2943 | struct stat st; |
32f4334d | 2944 | Lisp_Object handler; |
570d7624 | 2945 | |
4b4deea2 | 2946 | absname = expand_and_dir_to_file (filename, BVAR (current_buffer, directory)); |
570d7624 | 2947 | |
32f4334d RS |
2948 | /* If the file name has special constructs in it, |
2949 | call the corresponding file handler. */ | |
199607e4 | 2950 | handler = Ffind_file_name_handler (absname, Qfile_modes); |
32f4334d | 2951 | if (!NILP (handler)) |
199607e4 | 2952 | return call2 (handler, Qfile_modes, absname); |
32f4334d | 2953 | |
b1d1b865 RS |
2954 | absname = ENCODE_FILE (absname); |
2955 | ||
42a5b22f | 2956 | if (stat (SSDATA (absname), &st) < 0) |
570d7624 | 2957 | return Qnil; |
3ace87e3 | 2958 | |
570d7624 JB |
2959 | return make_number (st.st_mode & 07777); |
2960 | } | |
2961 | ||
09fbdf6c MC |
2962 | DEFUN ("set-file-modes", Fset_file_modes, Sset_file_modes, 2, 2, |
2963 | "(let ((file (read-file-name \"File: \"))) \ | |
2964 | (list file (read-file-modes nil file)))", | |
8c1a1077 | 2965 | doc: /* Set mode bits of file named FILENAME to MODE (an integer). |
ea429250 EZ |
2966 | Only the 12 low bits of MODE are used. |
2967 | ||
2968 | Interactively, mode bits are read by `read-file-modes', which accepts | |
712adc82 | 2969 | symbolic notation, like the `chmod' command from GNU Coreutils. */) |
5842a27b | 2970 | (Lisp_Object filename, Lisp_Object mode) |
570d7624 | 2971 | { |
b1d1b865 | 2972 | Lisp_Object absname, encoded_absname; |
32f4334d | 2973 | Lisp_Object handler; |
570d7624 | 2974 | |
4b4deea2 | 2975 | absname = Fexpand_file_name (filename, BVAR (current_buffer, directory)); |
b7826503 | 2976 | CHECK_NUMBER (mode); |
570d7624 | 2977 | |
32f4334d RS |
2978 | /* If the file name has special constructs in it, |
2979 | call the corresponding file handler. */ | |
199607e4 | 2980 | handler = Ffind_file_name_handler (absname, Qset_file_modes); |
32f4334d | 2981 | if (!NILP (handler)) |
199607e4 | 2982 | return call3 (handler, Qset_file_modes, absname, mode); |
32f4334d | 2983 | |
b1d1b865 RS |
2984 | encoded_absname = ENCODE_FILE (absname); |
2985 | ||
18c52557 | 2986 | if (chmod (SSDATA (encoded_absname), XINT (mode) & 07777) < 0) |
199607e4 | 2987 | report_file_error ("Doing chmod", Fcons (absname, Qnil)); |
570d7624 JB |
2988 | |
2989 | return Qnil; | |
2990 | } | |
2991 | ||
c24e9a53 | 2992 | DEFUN ("set-default-file-modes", Fset_default_file_modes, Sset_default_file_modes, 1, 1, 0, |
8c1a1077 PJ |
2993 | doc: /* Set the file permission bits for newly created files. |
2994 | The argument MODE should be an integer; only the low 9 bits are used. | |
2995 | This setting is inherited by subprocesses. */) | |
5842a27b | 2996 | (Lisp_Object mode) |
36a8c287 | 2997 | { |
b7826503 | 2998 | CHECK_NUMBER (mode); |
199607e4 | 2999 | |
5f85ea58 | 3000 | umask ((~ XINT (mode)) & 0777); |
36a8c287 JB |
3001 | |
3002 | return Qnil; | |
3003 | } | |
3004 | ||
c24e9a53 | 3005 | DEFUN ("default-file-modes", Fdefault_file_modes, Sdefault_file_modes, 0, 0, 0, |
8c1a1077 PJ |
3006 | doc: /* Return the default file protection for created files. |
3007 | The value is an integer. */) | |
5842a27b | 3008 | (void) |
36a8c287 | 3009 | { |
f75d7a91 | 3010 | mode_t realmask; |
5f85ea58 | 3011 | Lisp_Object value; |
36a8c287 | 3012 | |
4d7e6e51 | 3013 | block_input (); |
5f85ea58 RS |
3014 | realmask = umask (0); |
3015 | umask (realmask); | |
4d7e6e51 | 3016 | unblock_input (); |
36a8c287 | 3017 | |
46283abe | 3018 | XSETINT (value, (~ realmask) & 0777); |
5f85ea58 | 3019 | return value; |
36a8c287 | 3020 | } |
819da85b | 3021 | \f |
819da85b EZ |
3022 | |
3023 | DEFUN ("set-file-times", Fset_file_times, Sset_file_times, 1, 2, 0, | |
f839df0c | 3024 | doc: /* Set times of file FILENAME to TIMESTAMP. |
819da85b EZ |
3025 | Set both access and modification times. |
3026 | Return t on success, else nil. | |
f839df0c | 3027 | Use the current time if TIMESTAMP is nil. TIMESTAMP is in the format of |
819da85b | 3028 | `current-time'. */) |
f839df0c | 3029 | (Lisp_Object filename, Lisp_Object timestamp) |
819da85b EZ |
3030 | { |
3031 | Lisp_Object absname, encoded_absname; | |
3032 | Lisp_Object handler; | |
31571fd7 | 3033 | EMACS_TIME t = lisp_time_argument (timestamp); |
819da85b | 3034 | |
4b4deea2 | 3035 | absname = Fexpand_file_name (filename, BVAR (current_buffer, directory)); |
819da85b EZ |
3036 | |
3037 | /* If the file name has special constructs in it, | |
3038 | call the corresponding file handler. */ | |
3039 | handler = Ffind_file_name_handler (absname, Qset_file_times); | |
3040 | if (!NILP (handler)) | |
f839df0c | 3041 | return call3 (handler, Qset_file_times, absname, timestamp); |
819da85b EZ |
3042 | |
3043 | encoded_absname = ENCODE_FILE (absname); | |
5df5e07c | 3044 | |
819da85b | 3045 | { |
d35af63c | 3046 | if (set_file_times (-1, SSDATA (encoded_absname), t, t)) |
819da85b | 3047 | { |
0d9f584b | 3048 | #ifdef MSDOS |
819da85b EZ |
3049 | struct stat st; |
3050 | ||
3051 | /* Setting times on a directory always fails. */ | |
f68c809d | 3052 | if (stat (SSDATA (encoded_absname), &st) == 0 && S_ISDIR (st.st_mode)) |
819da85b EZ |
3053 | return Qnil; |
3054 | #endif | |
3055 | report_file_error ("Setting file times", Fcons (absname, Qnil)); | |
3056 | return Qnil; | |
3057 | } | |
3058 | } | |
5df5e07c | 3059 | |
819da85b EZ |
3060 | return Qt; |
3061 | } | |
f793dc6c | 3062 | \f |
697c17a2 | 3063 | #ifdef HAVE_SYNC |
85ffea93 | 3064 | DEFUN ("unix-sync", Funix_sync, Sunix_sync, 0, 0, "", |
8c1a1077 | 3065 | doc: /* Tell Unix to finish all pending disk updates. */) |
5842a27b | 3066 | (void) |
85ffea93 RS |
3067 | { |
3068 | sync (); | |
3069 | return Qnil; | |
3070 | } | |
3071 | ||
697c17a2 | 3072 | #endif /* HAVE_SYNC */ |
85ffea93 | 3073 | |
570d7624 | 3074 | DEFUN ("file-newer-than-file-p", Ffile_newer_than_file_p, Sfile_newer_than_file_p, 2, 2, 0, |
8c1a1077 PJ |
3075 | doc: /* Return t if file FILE1 is newer than file FILE2. |
3076 | If FILE1 does not exist, the answer is nil; | |
3077 | otherwise, if FILE2 does not exist, the answer is t. */) | |
5842a27b | 3078 | (Lisp_Object file1, Lisp_Object file2) |
570d7624 | 3079 | { |
199607e4 | 3080 | Lisp_Object absname1, absname2; |
39adff0d | 3081 | struct stat st1, st2; |
32f4334d | 3082 | Lisp_Object handler; |
09121adc | 3083 | struct gcpro gcpro1, gcpro2; |
570d7624 | 3084 | |
b7826503 PJ |
3085 | CHECK_STRING (file1); |
3086 | CHECK_STRING (file2); | |
570d7624 | 3087 | |
199607e4 RS |
3088 | absname1 = Qnil; |
3089 | GCPRO2 (absname1, file2); | |
4b4deea2 TT |
3090 | absname1 = expand_and_dir_to_file (file1, BVAR (current_buffer, directory)); |
3091 | absname2 = expand_and_dir_to_file (file2, BVAR (current_buffer, directory)); | |
09121adc | 3092 | UNGCPRO; |
570d7624 | 3093 | |
32f4334d RS |
3094 | /* If the file name has special constructs in it, |
3095 | call the corresponding file handler. */ | |
199607e4 | 3096 | handler = Ffind_file_name_handler (absname1, Qfile_newer_than_file_p); |
51cf6d37 | 3097 | if (NILP (handler)) |
199607e4 | 3098 | handler = Ffind_file_name_handler (absname2, Qfile_newer_than_file_p); |
32f4334d | 3099 | if (!NILP (handler)) |
199607e4 | 3100 | return call3 (handler, Qfile_newer_than_file_p, absname1, absname2); |
32f4334d | 3101 | |
b1d1b865 RS |
3102 | GCPRO2 (absname1, absname2); |
3103 | absname1 = ENCODE_FILE (absname1); | |
3104 | absname2 = ENCODE_FILE (absname2); | |
3105 | UNGCPRO; | |
3106 | ||
39adff0d | 3107 | if (stat (SSDATA (absname1), &st1) < 0) |
570d7624 JB |
3108 | return Qnil; |
3109 | ||
39adff0d | 3110 | if (stat (SSDATA (absname2), &st2) < 0) |
570d7624 JB |
3111 | return Qt; |
3112 | ||
39adff0d PE |
3113 | return (EMACS_TIME_GT (get_stat_mtime (&st1), get_stat_mtime (&st2)) |
3114 | ? Qt : Qnil); | |
570d7624 JB |
3115 | } |
3116 | \f | |
6fdaa9a0 KH |
3117 | #ifndef READ_BUF_SIZE |
3118 | #define READ_BUF_SIZE (64 << 10) | |
3119 | #endif | |
d311d28c PE |
3120 | /* Some buffer offsets are stored in 'int' variables. */ |
3121 | verify (READ_BUF_SIZE <= INT_MAX); | |
6fdaa9a0 | 3122 | |
98a7d268 KH |
3123 | /* This function is called after Lisp functions to decide a coding |
3124 | system are called, or when they cause an error. Before they are | |
3125 | called, the current buffer is set unibyte and it contains only a | |
3126 | newly inserted text (thus the buffer was empty before the | |
3127 | insertion). | |
3128 | ||
3129 | The functions may set markers, overlays, text properties, or even | |
3130 | alter the buffer contents, change the current buffer. | |
3131 | ||
3132 | Here, we reset all those changes by: | |
3133 | o set back the current buffer. | |
3134 | o move all markers and overlays to BEG. | |
3135 | o remove all text properties. | |
3136 | o set back the buffer multibyteness. */ | |
f736ffbf KH |
3137 | |
3138 | static Lisp_Object | |
971de7fb | 3139 | decide_coding_unwind (Lisp_Object unwind_data) |
f736ffbf | 3140 | { |
98a7d268 | 3141 | Lisp_Object multibyte, undo_list, buffer; |
f736ffbf | 3142 | |
98a7d268 KH |
3143 | multibyte = XCAR (unwind_data); |
3144 | unwind_data = XCDR (unwind_data); | |
3145 | undo_list = XCAR (unwind_data); | |
3146 | buffer = XCDR (unwind_data); | |
3147 | ||
a3d794a1 | 3148 | set_buffer_internal (XBUFFER (buffer)); |
98a7d268 KH |
3149 | adjust_markers_for_delete (BEG, BEG_BYTE, Z, Z_BYTE); |
3150 | adjust_overlays_for_delete (BEG, Z - BEG); | |
0c94c8d6 | 3151 | set_buffer_intervals (current_buffer, NULL); |
98a7d268 KH |
3152 | TEMP_SET_PT_BOTH (BEG, BEG_BYTE); |
3153 | ||
3154 | /* Now we are safe to change the buffer's multibyteness directly. */ | |
39eb03f1 PE |
3155 | bset_enable_multibyte_characters (current_buffer, multibyte); |
3156 | bset_undo_list (current_buffer, undo_list); | |
f736ffbf KH |
3157 | |
3158 | return Qnil; | |
3159 | } | |
3160 | ||
55587f8a | 3161 | |
1b978129 | 3162 | /* Used to pass values from insert-file-contents to read_non_regular. */ |
55587f8a | 3163 | |
1b978129 | 3164 | static int non_regular_fd; |
d311d28c PE |
3165 | static ptrdiff_t non_regular_inserted; |
3166 | static int non_regular_nbytes; | |
55587f8a | 3167 | |
55587f8a | 3168 | |
1b978129 | 3169 | /* Read from a non-regular file. |
438105ed | 3170 | Read non_regular_nbytes bytes max from non_regular_fd. |
1b978129 GM |
3171 | Non_regular_inserted specifies where to put the read bytes. |
3172 | Value is the number of bytes read. */ | |
55587f8a GM |
3173 | |
3174 | static Lisp_Object | |
9c8a2331 | 3175 | read_non_regular (Lisp_Object ignore) |
55587f8a | 3176 | { |
d311d28c | 3177 | int nbytes; |
efdc16c9 | 3178 | |
1b978129 GM |
3179 | immediate_quit = 1; |
3180 | QUIT; | |
3181 | nbytes = emacs_read (non_regular_fd, | |
5976c3fe PE |
3182 | ((char *) BEG_ADDR + PT_BYTE - BEG_BYTE |
3183 | + non_regular_inserted), | |
1b978129 | 3184 | non_regular_nbytes); |
1b978129 GM |
3185 | immediate_quit = 0; |
3186 | return make_number (nbytes); | |
3187 | } | |
55587f8a | 3188 | |
d0e2444e | 3189 | |
1b978129 GM |
3190 | /* Condition-case handler used when reading from non-regular files |
3191 | in insert-file-contents. */ | |
3192 | ||
3193 | static Lisp_Object | |
9c8a2331 | 3194 | read_non_regular_quit (Lisp_Object ignore) |
1b978129 | 3195 | { |
55587f8a GM |
3196 | return Qnil; |
3197 | } | |
3198 | ||
e69dafad PE |
3199 | /* Reposition FD to OFFSET, based on WHENCE. This acts like lseek |
3200 | except that it also tests for OFFSET being out of lseek's range. */ | |
3201 | static off_t | |
3202 | emacs_lseek (int fd, EMACS_INT offset, int whence) | |
3203 | { | |
096a9774 PE |
3204 | /* Use "&" rather than "&&" to suppress a bogus GCC warning; see |
3205 | <http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43772>. */ | |
3206 | if (! ((TYPE_MINIMUM (off_t) <= offset) & (offset <= TYPE_MAXIMUM (off_t)))) | |
e69dafad PE |
3207 | { |
3208 | errno = EINVAL; | |
3209 | return -1; | |
3210 | } | |
3211 | return lseek (fd, offset, whence); | |
3212 | } | |
3213 | ||
f0941253 | 3214 | /* Return a special time value indicating the error number ERRNUM. */ |
dbeed9a6 | 3215 | static EMACS_TIME |
f0941253 | 3216 | time_error_value (int errnum) |
dbeed9a6 | 3217 | { |
0497dc44 | 3218 | int ns = (errnum == ENOENT || errnum == EACCES || errnum == ENOTDIR |
dbeed9a6 PE |
3219 | ? NONEXISTENT_MODTIME_NSECS |
3220 | : UNKNOWN_MODTIME_NSECS); | |
e9a9ae03 | 3221 | return make_emacs_time (0, ns); |
dbeed9a6 | 3222 | } |
55587f8a | 3223 | |
570d7624 | 3224 | DEFUN ("insert-file-contents", Finsert_file_contents, Sinsert_file_contents, |
8c1a1077 PJ |
3225 | 1, 5, 0, |
3226 | doc: /* Insert contents of file FILENAME after point. | |
cf6d2357 | 3227 | Returns list of absolute file name and number of characters inserted. |
6f2528d8 MR |
3228 | If second argument VISIT is non-nil, the buffer's visited filename and |
3229 | last save file modtime are set, and it is marked unmodified. If | |
3230 | visiting and the file does not exist, visiting is completed before the | |
3231 | error is signaled. | |
3232 | ||
3233 | The optional third and fourth arguments BEG and END specify what portion | |
3234 | of the file to insert. These arguments count bytes in the file, not | |
3235 | characters in the buffer. If VISIT is non-nil, BEG and END must be nil. | |
3236 | ||
3237 | If optional fifth argument REPLACE is non-nil, replace the current | |
3238 | buffer contents (in the accessible portion) with the file contents. | |
3239 | This is better than simply deleting and inserting the whole thing | |
3240 | because (1) it preserves some marker positions and (2) it puts less data | |
3241 | in the undo list. When REPLACE is non-nil, the second return value is | |
3242 | the number of characters that replace previous buffer contents. | |
3243 | ||
3244 | This function does code conversion according to the value of | |
3245 | `coding-system-for-read' or `file-coding-system-alist', and sets the | |
3246 | variable `last-coding-system-used' to the coding system actually used. */) | |
5842a27b | 3247 | (Lisp_Object filename, Lisp_Object visit, Lisp_Object beg, Lisp_Object end, Lisp_Object replace) |
570d7624 JB |
3248 | { |
3249 | struct stat st; | |
dbeed9a6 PE |
3250 | int file_status; |
3251 | EMACS_TIME mtime; | |
f75d7a91 | 3252 | int fd; |
d311d28c | 3253 | ptrdiff_t inserted = 0; |
f75d7a91 PE |
3254 | bool nochange = 0; |
3255 | ptrdiff_t how_much; | |
728f8f0a | 3256 | off_t beg_offset, end_offset; |
f75d7a91 | 3257 | int unprocessed; |
d311d28c | 3258 | ptrdiff_t count = SPECPDL_INDEX (); |
6f2528d8 MR |
3259 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; |
3260 | Lisp_Object handler, val, insval, orig_filename, old_undo; | |
d6a3cc15 | 3261 | Lisp_Object p; |
d311d28c | 3262 | ptrdiff_t total = 0; |
f75d7a91 | 3263 | bool not_regular = 0; |
e5e9d610 | 3264 | int save_errno = 0; |
5976c3fe | 3265 | char read_buf[READ_BUF_SIZE]; |
6fdaa9a0 | 3266 | struct coding_system coding; |
5976c3fe | 3267 | char buffer[1 << 14]; |
f75d7a91 PE |
3268 | bool replace_handled = 0; |
3269 | bool set_coding_system = 0; | |
db327c7e | 3270 | Lisp_Object coding_system; |
f75d7a91 | 3271 | bool read_quit = 0; |
490ee853 | 3272 | Lisp_Object old_Vdeactivate_mark = Vdeactivate_mark; |
f75d7a91 PE |
3273 | bool we_locked_file = 0; |
3274 | bool deferred_remove_unwind_protect = 0; | |
32f4334d | 3275 | |
95385625 RS |
3276 | if (current_buffer->base_buffer && ! NILP (visit)) |
3277 | error ("Cannot do file visiting in an indirect buffer"); | |
3278 | ||
4b4deea2 | 3279 | if (!NILP (BVAR (current_buffer, read_only))) |
95385625 RS |
3280 | Fbarf_if_buffer_read_only (); |
3281 | ||
32f4334d | 3282 | val = Qnil; |
d6a3cc15 | 3283 | p = Qnil; |
b1d1b865 | 3284 | orig_filename = Qnil; |
6f2528d8 | 3285 | old_undo = Qnil; |
32f4334d | 3286 | |
6f2528d8 | 3287 | GCPRO5 (filename, val, p, orig_filename, old_undo); |
570d7624 | 3288 | |
b7826503 | 3289 | CHECK_STRING (filename); |
570d7624 JB |
3290 | filename = Fexpand_file_name (filename, Qnil); |
3291 | ||
1c157f8d KH |
3292 | /* The value Qnil means that the coding system is not yet |
3293 | decided. */ | |
3294 | coding_system = Qnil; | |
3295 | ||
32f4334d RS |
3296 | /* If the file name has special constructs in it, |
3297 | call the corresponding file handler. */ | |
49307295 | 3298 | handler = Ffind_file_name_handler (filename, Qinsert_file_contents); |
32f4334d RS |
3299 | if (!NILP (handler)) |
3300 | { | |
3d0387c0 RS |
3301 | val = call6 (handler, Qinsert_file_contents, filename, |
3302 | visit, beg, end, replace); | |
d311d28c PE |
3303 | if (CONSP (val) && CONSP (XCDR (val)) |
3304 | && RANGED_INTEGERP (0, XCAR (XCDR (val)), ZV - PT)) | |
03699b14 | 3305 | inserted = XINT (XCAR (XCDR (val))); |
32f4334d RS |
3306 | goto handled; |
3307 | } | |
3308 | ||
b1d1b865 RS |
3309 | orig_filename = filename; |
3310 | filename = ENCODE_FILE (filename); | |
3311 | ||
570d7624 JB |
3312 | fd = -1; |
3313 | ||
c1c4693e RS |
3314 | #ifdef WINDOWSNT |
3315 | { | |
3316 | Lisp_Object tem = Vw32_get_true_file_attributes; | |
3317 | ||
3318 | /* Tell stat to use expensive method to get accurate info. */ | |
3319 | Vw32_get_true_file_attributes = Qt; | |
dbeed9a6 | 3320 | file_status = stat (SSDATA (filename), &st); |
c1c4693e RS |
3321 | Vw32_get_true_file_attributes = tem; |
3322 | } | |
c1c4693e | 3323 | #else |
dbeed9a6 | 3324 | file_status = stat (SSDATA (filename), &st); |
c1c4693e | 3325 | #endif /* WINDOWSNT */ |
dbeed9a6 PE |
3326 | |
3327 | if (file_status == 0) | |
3328 | mtime = get_stat_mtime (&st); | |
3329 | else | |
570d7624 | 3330 | { |
99bc28f4 | 3331 | badopen: |
e5e9d610 | 3332 | save_errno = errno; |
265a9e55 | 3333 | if (NILP (visit)) |
b1d1b865 | 3334 | report_file_error ("Opening input file", Fcons (orig_filename, Qnil)); |
f0941253 | 3335 | mtime = time_error_value (save_errno); |
b885bf36 | 3336 | st.st_size = -1; |
570d7624 | 3337 | how_much = 0; |
0de6b8f4 | 3338 | if (!NILP (Vcoding_system_for_read)) |
22d92d6b | 3339 | Fset (Qbuffer_file_coding_system, Vcoding_system_for_read); |
570d7624 JB |
3340 | goto notfound; |
3341 | } | |
3342 | ||
be53b411 JB |
3343 | /* This code will need to be changed in order to work on named |
3344 | pipes, and it's probably just not worth it. So we should at | |
3345 | least signal an error. */ | |
99bc28f4 | 3346 | if (!S_ISREG (st.st_mode)) |
330bfe57 | 3347 | { |
d4b8687b RS |
3348 | not_regular = 1; |
3349 | ||
3350 | if (! NILP (visit)) | |
3351 | goto notfound; | |
3352 | ||
3353 | if (! NILP (replace) || ! NILP (beg) || ! NILP (end)) | |
24b1ddad KS |
3354 | xsignal2 (Qfile_error, |
3355 | build_string ("not a regular file"), orig_filename); | |
330bfe57 | 3356 | } |
be53b411 | 3357 | |
99bc28f4 | 3358 | if (fd < 0) |
42a5b22f | 3359 | if ((fd = emacs_open (SSDATA (filename), O_RDONLY, 0)) < 0) |
99bc28f4 KH |
3360 | goto badopen; |
3361 | ||
3362 | /* Replacement should preserve point as it preserves markers. */ | |
3363 | if (!NILP (replace)) | |
3364 | record_unwind_protect (restore_point_unwind, Fpoint_marker ()); | |
3365 | ||
3366 | record_unwind_protect (close_file_unwind, make_number (fd)); | |
3367 | ||
2a47c44d | 3368 | |
9f57b6b4 KH |
3369 | if (!NILP (visit)) |
3370 | { | |
3371 | if (!NILP (beg) || !NILP (end)) | |
3372 | error ("Attempt to visit less than an entire file"); | |
3373 | if (BEG < Z && NILP (replace)) | |
3374 | error ("Cannot do file visiting in a non-empty buffer"); | |
3375 | } | |
7fded690 JB |
3376 | |
3377 | if (!NILP (beg)) | |
728f8f0a PE |
3378 | { |
3379 | if (! (RANGED_INTEGERP (0, beg, TYPE_MAXIMUM (off_t)))) | |
3380 | wrong_type_argument (intern ("file-offset"), beg); | |
3381 | beg_offset = XFASTINT (beg); | |
3382 | } | |
7fded690 | 3383 | else |
728f8f0a | 3384 | beg_offset = 0; |
7fded690 JB |
3385 | |
3386 | if (!NILP (end)) | |
728f8f0a PE |
3387 | { |
3388 | if (! (RANGED_INTEGERP (0, end, TYPE_MAXIMUM (off_t)))) | |
3389 | wrong_type_argument (intern ("file-offset"), end); | |
3390 | end_offset = XFASTINT (end); | |
3391 | } | |
7fded690 JB |
3392 | else |
3393 | { | |
728f8f0a PE |
3394 | if (not_regular) |
3395 | end_offset = TYPE_MAXIMUM (off_t); | |
3396 | else | |
d4b8687b | 3397 | { |
728f8f0a PE |
3398 | end_offset = st.st_size; |
3399 | ||
3400 | /* A negative size can happen on a platform that allows file | |
3401 | sizes greater than the maximum off_t value. */ | |
3402 | if (end_offset < 0) | |
3403 | buffer_overflow (); | |
68c45bf0 | 3404 | |
d21dd12d GM |
3405 | /* The file size returned from stat may be zero, but data |
3406 | may be readable nonetheless, for example when this is a | |
3407 | file in the /proc filesystem. */ | |
728f8f0a PE |
3408 | if (end_offset == 0) |
3409 | end_offset = READ_BUF_SIZE; | |
d4b8687b | 3410 | } |
7fded690 JB |
3411 | } |
3412 | ||
728f8f0a PE |
3413 | /* Check now whether the buffer will become too large, |
3414 | in the likely case where the file's length is not changing. | |
3415 | This saves a lot of needless work before a buffer overflow. */ | |
3416 | if (! not_regular) | |
3417 | { | |
3418 | /* The likely offset where we will stop reading. We could read | |
3419 | more (or less), if the file grows (or shrinks) as we read it. */ | |
3420 | off_t likely_end = min (end_offset, st.st_size); | |
3421 | ||
3422 | if (beg_offset < likely_end) | |
3423 | { | |
3424 | ptrdiff_t buf_bytes = | |
3425 | Z_BYTE - (!NILP (replace) ? ZV_BYTE - BEGV_BYTE : 0); | |
3426 | ptrdiff_t buf_growth_max = BUF_BYTES_MAX - buf_bytes; | |
3427 | off_t likely_growth = likely_end - beg_offset; | |
3428 | if (buf_growth_max < likely_growth) | |
3429 | buffer_overflow (); | |
3430 | } | |
3431 | } | |
3432 | ||
3433 | /* Prevent redisplay optimizations. */ | |
3434 | current_buffer->clip_changed = 1; | |
3435 | ||
356a6224 | 3436 | if (EQ (Vcoding_system_for_read, Qauto_save_coding)) |
5560aecd | 3437 | { |
75421805 | 3438 | coding_system = coding_inherit_eol_type (Qutf_8_emacs, Qunix); |
5560aecd KH |
3439 | setup_coding_system (coding_system, &coding); |
3440 | /* Ensure we set Vlast_coding_system_used. */ | |
3441 | set_coding_system = 1; | |
3442 | } | |
356a6224 | 3443 | else if (BEG < Z) |
f736ffbf KH |
3444 | { |
3445 | /* Decide the coding system to use for reading the file now | |
3446 | because we can't use an optimized method for handling | |
3447 | `coding:' tag if the current buffer is not empty. */ | |
f736ffbf | 3448 | if (!NILP (Vcoding_system_for_read)) |
db327c7e | 3449 | coding_system = Vcoding_system_for_read; |
f736ffbf KH |
3450 | else |
3451 | { | |
3452 | /* Don't try looking inside a file for a coding system | |
3453 | specification if it is not seekable. */ | |
3454 | if (! not_regular && ! NILP (Vset_auto_coding_function)) | |
3455 | { | |
3456 | /* Find a coding system specified in the heading two | |
3457 | lines or in the tailing several lines of the file. | |
3458 | We assume that the 1K-byte and 3K-byte for heading | |
003a7eaa | 3459 | and tailing respectively are sufficient for this |
f736ffbf | 3460 | purpose. */ |
d311d28c | 3461 | int nread; |
f736ffbf KH |
3462 | |
3463 | if (st.st_size <= (1024 * 4)) | |
68c45bf0 | 3464 | nread = emacs_read (fd, read_buf, 1024 * 4); |
f736ffbf KH |
3465 | else |
3466 | { | |
68c45bf0 | 3467 | nread = emacs_read (fd, read_buf, 1024); |
f736ffbf KH |
3468 | if (nread >= 0) |
3469 | { | |
e69dafad | 3470 | if (lseek (fd, st.st_size - (1024 * 3), SEEK_SET) < 0) |
f736ffbf KH |
3471 | report_file_error ("Setting file position", |
3472 | Fcons (orig_filename, Qnil)); | |
68c45bf0 | 3473 | nread += emacs_read (fd, read_buf + nread, 1024 * 3); |
f736ffbf KH |
3474 | } |
3475 | } | |
feb9dc27 | 3476 | |
f736ffbf KH |
3477 | if (nread < 0) |
3478 | error ("IO error reading %s: %s", | |
d5db4077 | 3479 | SDATA (orig_filename), emacs_strerror (errno)); |
f736ffbf KH |
3480 | else if (nread > 0) |
3481 | { | |
f736ffbf | 3482 | struct buffer *prev = current_buffer; |
f839df0c | 3483 | Lisp_Object workbuf; |
685fc579 | 3484 | struct buffer *buf; |
f736ffbf | 3485 | |
66322887 | 3486 | record_unwind_current_buffer (); |
1d92afcd | 3487 | |
f839df0c PE |
3488 | workbuf = Fget_buffer_create (build_string (" *code-converting-work*")); |
3489 | buf = XBUFFER (workbuf); | |
685fc579 | 3490 | |
29ea8ae9 | 3491 | delete_all_overlays (buf); |
39eb03f1 PE |
3492 | bset_directory (buf, BVAR (current_buffer, directory)); |
3493 | bset_read_only (buf, Qnil); | |
3494 | bset_filename (buf, Qnil); | |
3495 | bset_undo_list (buf, Qt); | |
fd318b54 DA |
3496 | eassert (buf->overlays_before == NULL); |
3497 | eassert (buf->overlays_after == NULL); | |
efdc16c9 | 3498 | |
685fc579 RS |
3499 | set_buffer_internal (buf); |
3500 | Ferase_buffer (); | |
39eb03f1 | 3501 | bset_enable_multibyte_characters (buf, Qnil); |
685fc579 | 3502 | |
b68864e5 | 3503 | insert_1_both ((char *) read_buf, nread, nread, 0, 0, 0); |
f736ffbf | 3504 | TEMP_SET_PT_BOTH (BEG, BEG_BYTE); |
db327c7e | 3505 | coding_system = call2 (Vset_auto_coding_function, |
8f924df7 | 3506 | filename, make_number (nread)); |
f736ffbf | 3507 | set_buffer_internal (prev); |
efdc16c9 | 3508 | |
f736ffbf KH |
3509 | /* Discard the unwind protect for recovering the |
3510 | current buffer. */ | |
3511 | specpdl_ptr--; | |
3512 | ||
3513 | /* Rewind the file for the actual read done later. */ | |
e69dafad | 3514 | if (lseek (fd, 0, SEEK_SET) < 0) |
f736ffbf KH |
3515 | report_file_error ("Setting file position", |
3516 | Fcons (orig_filename, Qnil)); | |
3517 | } | |
3518 | } | |
feb9dc27 | 3519 | |
db327c7e | 3520 | if (NILP (coding_system)) |
f736ffbf KH |
3521 | { |
3522 | /* If we have not yet decided a coding system, check | |
3523 | file-coding-system-alist. */ | |
8f924df7 | 3524 | Lisp_Object args[6]; |
f736ffbf KH |
3525 | |
3526 | args[0] = Qinsert_file_contents, args[1] = orig_filename; | |
3527 | args[2] = visit, args[3] = beg, args[4] = end, args[5] = replace; | |
8f924df7 KH |
3528 | coding_system = Ffind_operation_coding_system (6, args); |
3529 | if (CONSP (coding_system)) | |
3530 | coding_system = XCAR (coding_system); | |
f736ffbf KH |
3531 | } |
3532 | } | |
c9e82392 | 3533 | |
db327c7e KH |
3534 | if (NILP (coding_system)) |
3535 | coding_system = Qundecided; | |
3536 | else | |
3537 | CHECK_CODING_SYSTEM (coding_system); | |
c8a6d68a | 3538 | |
4b4deea2 | 3539 | if (NILP (BVAR (current_buffer, enable_multibyte_characters))) |
237a6fd2 RS |
3540 | /* We must suppress all character code conversion except for |
3541 | end-of-line conversion. */ | |
db327c7e | 3542 | coding_system = raw_text_coding_system (coding_system); |
54369368 | 3543 | |
db327c7e KH |
3544 | setup_coding_system (coding_system, &coding); |
3545 | /* Ensure we set Vlast_coding_system_used. */ | |
3546 | set_coding_system = 1; | |
f736ffbf | 3547 | } |
6cf71bf1 | 3548 | |
3d0387c0 RS |
3549 | /* If requested, replace the accessible part of the buffer |
3550 | with the file contents. Avoid replacing text at the | |
3551 | beginning or end of the buffer that matches the file contents; | |
3dbcf3f6 RS |
3552 | that preserves markers pointing to the unchanged parts. |
3553 | ||
3554 | Here we implement this feature in an optimized way | |
3555 | for the case where code conversion is NOT needed. | |
3556 | The following if-statement handles the case of conversion | |
727a0b4a RS |
3557 | in a less optimal way. |
3558 | ||
3559 | If the code conversion is "automatic" then we try using this | |
3560 | method and hope for the best. | |
3561 | But if we discover the need for conversion, we give up on this method | |
3562 | and let the following if-statement handle the replace job. */ | |
3dbcf3f6 | 3563 | if (!NILP (replace) |
f736ffbf | 3564 | && BEGV < ZV |
db327c7e KH |
3565 | && (NILP (coding_system) |
3566 | || ! CODING_REQUIRE_DECODING (&coding))) | |
3d0387c0 | 3567 | { |
ec7adf26 RS |
3568 | /* same_at_start and same_at_end count bytes, |
3569 | because file access counts bytes | |
3570 | and BEG and END count bytes. */ | |
d311d28c PE |
3571 | ptrdiff_t same_at_start = BEGV_BYTE; |
3572 | ptrdiff_t same_at_end = ZV_BYTE; | |
3573 | ptrdiff_t overlap; | |
6fdaa9a0 | 3574 | /* There is still a possibility we will find the need to do code |
f75d7a91 | 3575 | conversion. If that happens, set this variable to |
727a0b4a | 3576 | give up on handling REPLACE in the optimized way. */ |
f75d7a91 | 3577 | bool giveup_match_end = 0; |
9c28748f | 3578 | |
728f8f0a | 3579 | if (beg_offset != 0) |
4d2a0879 | 3580 | { |
728f8f0a | 3581 | if (lseek (fd, beg_offset, SEEK_SET) < 0) |
4d2a0879 | 3582 | report_file_error ("Setting file position", |
b1d1b865 | 3583 | Fcons (orig_filename, Qnil)); |
4d2a0879 RS |
3584 | } |
3585 | ||
3d0387c0 RS |
3586 | immediate_quit = 1; |
3587 | QUIT; | |
3588 | /* Count how many chars at the start of the file | |
3589 | match the text at the beginning of the buffer. */ | |
3590 | while (1) | |
3591 | { | |
d311d28c | 3592 | int nread, bufpos; |
3d0387c0 | 3593 | |
68c45bf0 | 3594 | nread = emacs_read (fd, buffer, sizeof buffer); |
3d0387c0 RS |
3595 | if (nread < 0) |
3596 | error ("IO error reading %s: %s", | |
5976c3fe | 3597 | SSDATA (orig_filename), emacs_strerror (errno)); |
3d0387c0 RS |
3598 | else if (nread == 0) |
3599 | break; | |
6fdaa9a0 | 3600 | |
db327c7e | 3601 | if (CODING_REQUIRE_DETECTION (&coding)) |
727a0b4a | 3602 | { |
5976c3fe PE |
3603 | coding_system = detect_coding_system ((unsigned char *) buffer, |
3604 | nread, nread, 1, 0, | |
db327c7e KH |
3605 | coding_system); |
3606 | setup_coding_system (coding_system, &coding); | |
727a0b4a RS |
3607 | } |
3608 | ||
db327c7e KH |
3609 | if (CODING_REQUIRE_DECODING (&coding)) |
3610 | /* We found that the file should be decoded somehow. | |
727a0b4a RS |
3611 | Let's give up here. */ |
3612 | { | |
3613 | giveup_match_end = 1; | |
3614 | break; | |
3615 | } | |
3616 | ||
3d0387c0 | 3617 | bufpos = 0; |
ec7adf26 | 3618 | while (bufpos < nread && same_at_start < ZV_BYTE |
6fdaa9a0 | 3619 | && FETCH_BYTE (same_at_start) == buffer[bufpos]) |
3d0387c0 RS |
3620 | same_at_start++, bufpos++; |
3621 | /* If we found a discrepancy, stop the scan. | |
8e6208c5 | 3622 | Otherwise loop around and scan the next bufferful. */ |
3d0387c0 RS |
3623 | if (bufpos != nread) |
3624 | break; | |
3625 | } | |
3626 | immediate_quit = 0; | |
3627 | /* If the file matches the buffer completely, | |
3628 | there's no need to replace anything. */ | |
2ba8e008 | 3629 | if (same_at_start - BEGV_BYTE == end_offset - beg_offset) |
3d0387c0 | 3630 | { |
68c45bf0 | 3631 | emacs_close (fd); |
a1d2b64a | 3632 | specpdl_ptr--; |
1051b3b3 | 3633 | /* Truncate the buffer to the size of the file. */ |
7dae4502 | 3634 | del_range_1 (same_at_start, same_at_end, 0, 0); |
3d0387c0 RS |
3635 | goto handled; |
3636 | } | |
3637 | immediate_quit = 1; | |
3638 | QUIT; | |
3639 | /* Count how many chars at the end of the file | |
6fdaa9a0 KH |
3640 | match the text at the end of the buffer. But, if we have |
3641 | already found that decoding is necessary, don't waste time. */ | |
3642 | while (!giveup_match_end) | |
3d0387c0 | 3643 | { |
728f8f0a PE |
3644 | int total_read, nread, bufpos, trial; |
3645 | off_t curpos; | |
3d0387c0 RS |
3646 | |
3647 | /* At what file position are we now scanning? */ | |
728f8f0a | 3648 | curpos = end_offset - (ZV_BYTE - same_at_end); |
fc81fa9e KH |
3649 | /* If the entire file matches the buffer tail, stop the scan. */ |
3650 | if (curpos == 0) | |
3651 | break; | |
3d0387c0 RS |
3652 | /* How much can we scan in the next step? */ |
3653 | trial = min (curpos, sizeof buffer); | |
728f8f0a | 3654 | if (lseek (fd, curpos - trial, SEEK_SET) < 0) |
3d0387c0 | 3655 | report_file_error ("Setting file position", |
b1d1b865 | 3656 | Fcons (orig_filename, Qnil)); |
3d0387c0 | 3657 | |
b02439c8 | 3658 | total_read = nread = 0; |
3d0387c0 RS |
3659 | while (total_read < trial) |
3660 | { | |
68c45bf0 | 3661 | nread = emacs_read (fd, buffer + total_read, trial - total_read); |
2bd2273e | 3662 | if (nread < 0) |
3d0387c0 | 3663 | error ("IO error reading %s: %s", |
d5db4077 | 3664 | SDATA (orig_filename), emacs_strerror (errno)); |
2bd2273e GM |
3665 | else if (nread == 0) |
3666 | break; | |
3d0387c0 RS |
3667 | total_read += nread; |
3668 | } | |
efdc16c9 | 3669 | |
8e6208c5 | 3670 | /* Scan this bufferful from the end, comparing with |
3d0387c0 RS |
3671 | the Emacs buffer. */ |
3672 | bufpos = total_read; | |
efdc16c9 | 3673 | |
3d0387c0 RS |
3674 | /* Compare with same_at_start to avoid counting some buffer text |
3675 | as matching both at the file's beginning and at the end. */ | |
3676 | while (bufpos > 0 && same_at_end > same_at_start | |
6fdaa9a0 | 3677 | && FETCH_BYTE (same_at_end - 1) == buffer[bufpos - 1]) |
3d0387c0 | 3678 | same_at_end--, bufpos--; |
727a0b4a | 3679 | |
3d0387c0 | 3680 | /* If we found a discrepancy, stop the scan. |
8e6208c5 | 3681 | Otherwise loop around and scan the preceding bufferful. */ |
3d0387c0 | 3682 | if (bufpos != 0) |
727a0b4a RS |
3683 | { |
3684 | /* If this discrepancy is because of code conversion, | |
3685 | we cannot use this method; giveup and try the other. */ | |
3686 | if (same_at_end > same_at_start | |
3687 | && FETCH_BYTE (same_at_end - 1) >= 0200 | |
4b4deea2 | 3688 | && ! NILP (BVAR (current_buffer, enable_multibyte_characters)) |
c8a6d68a | 3689 | && (CODING_MAY_REQUIRE_DECODING (&coding))) |
727a0b4a RS |
3690 | giveup_match_end = 1; |
3691 | break; | |
3692 | } | |
b02439c8 GM |
3693 | |
3694 | if (nread == 0) | |
3695 | break; | |
3d0387c0 RS |
3696 | } |
3697 | immediate_quit = 0; | |
9c28748f | 3698 | |
727a0b4a RS |
3699 | if (! giveup_match_end) |
3700 | { | |
d311d28c | 3701 | ptrdiff_t temp; |
ec7adf26 | 3702 | |
727a0b4a | 3703 | /* We win! We can handle REPLACE the optimized way. */ |
9c28748f | 3704 | |
20f6783d RS |
3705 | /* Extend the start of non-matching text area to multibyte |
3706 | character boundary. */ | |
4b4deea2 | 3707 | if (! NILP (BVAR (current_buffer, enable_multibyte_characters))) |
20f6783d RS |
3708 | while (same_at_start > BEGV_BYTE |
3709 | && ! CHAR_HEAD_P (FETCH_BYTE (same_at_start))) | |
3710 | same_at_start--; | |
3711 | ||
3712 | /* Extend the end of non-matching text area to multibyte | |
71312b68 | 3713 | character boundary. */ |
4b4deea2 | 3714 | if (! NILP (BVAR (current_buffer, enable_multibyte_characters))) |
ec7adf26 RS |
3715 | while (same_at_end < ZV_BYTE |
3716 | && ! CHAR_HEAD_P (FETCH_BYTE (same_at_end))) | |
71312b68 RS |
3717 | same_at_end++; |
3718 | ||
727a0b4a | 3719 | /* Don't try to reuse the same piece of text twice. */ |
ec7adf26 | 3720 | overlap = (same_at_start - BEGV_BYTE |
728f8f0a PE |
3721 | - (same_at_end |
3722 | + (! NILP (end) ? end_offset : st.st_size) - ZV_BYTE)); | |
727a0b4a RS |
3723 | if (overlap > 0) |
3724 | same_at_end += overlap; | |
9c28748f | 3725 | |
727a0b4a | 3726 | /* Arrange to read only the nonmatching middle part of the file. */ |
728f8f0a PE |
3727 | beg_offset += same_at_start - BEGV_BYTE; |
3728 | end_offset -= ZV_BYTE - same_at_end; | |
3dbcf3f6 | 3729 | |
ec7adf26 | 3730 | del_range_byte (same_at_start, same_at_end, 0); |
727a0b4a | 3731 | /* Insert from the file at the proper position. */ |
ec7adf26 RS |
3732 | temp = BYTE_TO_CHAR (same_at_start); |
3733 | SET_PT_BOTH (temp, same_at_start); | |
727a0b4a RS |
3734 | |
3735 | /* If display currently starts at beginning of line, | |
3736 | keep it that way. */ | |
d3d50620 | 3737 | if (XBUFFER (XWINDOW (selected_window)->buffer) == current_buffer) |
c98ff5dd | 3738 | XWINDOW (selected_window)->start_at_line_beg = !NILP (Fbolp ()); |
727a0b4a RS |
3739 | |
3740 | replace_handled = 1; | |
3741 | } | |
3dbcf3f6 RS |
3742 | } |
3743 | ||
3744 | /* If requested, replace the accessible part of the buffer | |
3745 | with the file contents. Avoid replacing text at the | |
3746 | beginning or end of the buffer that matches the file contents; | |
3747 | that preserves markers pointing to the unchanged parts. | |
3748 | ||
3749 | Here we implement this feature for the case where code conversion | |
3750 | is needed, in a simple way that needs a lot of memory. | |
3751 | The preceding if-statement handles the case of no conversion | |
3752 | in a more optimized way. */ | |
f736ffbf | 3753 | if (!NILP (replace) && ! replace_handled && BEGV < ZV) |
3dbcf3f6 | 3754 | { |
d311d28c PE |
3755 | ptrdiff_t same_at_start = BEGV_BYTE; |
3756 | ptrdiff_t same_at_end = ZV_BYTE; | |
3757 | ptrdiff_t same_at_start_charpos; | |
3758 | ptrdiff_t inserted_chars; | |
3759 | ptrdiff_t overlap; | |
3760 | ptrdiff_t bufpos; | |
db327c7e | 3761 | unsigned char *decoded; |
d311d28c PE |
3762 | ptrdiff_t temp; |
3763 | ptrdiff_t this = 0; | |
3764 | ptrdiff_t this_count = SPECPDL_INDEX (); | |
f10fe38f PE |
3765 | bool multibyte |
3766 | = ! NILP (BVAR (current_buffer, enable_multibyte_characters)); | |
5b359650 | 3767 | Lisp_Object conversion_buffer; |
83aca1cb | 3768 | struct gcpro gcpro1; |
db327c7e | 3769 | |
5b359650 | 3770 | conversion_buffer = code_conversion_save (1, multibyte); |
3dbcf3f6 RS |
3771 | |
3772 | /* First read the whole file, performing code conversion into | |
3773 | CONVERSION_BUFFER. */ | |
3774 | ||
728f8f0a | 3775 | if (lseek (fd, beg_offset, SEEK_SET) < 0) |
8f924df7 KH |
3776 | report_file_error ("Setting file position", |
3777 | Fcons (orig_filename, Qnil)); | |
727a0b4a | 3778 | |
3dbcf3f6 RS |
3779 | total = st.st_size; /* Total bytes in the file. */ |
3780 | how_much = 0; /* Bytes read from file so far. */ | |
3781 | inserted = 0; /* Bytes put into CONVERSION_BUFFER so far. */ | |
3782 | unprocessed = 0; /* Bytes not processed in previous loop. */ | |
3783 | ||
2ba48777 | 3784 | GCPRO1 (conversion_buffer); |
3dbcf3f6 RS |
3785 | while (how_much < total) |
3786 | { | |
db327c7e KH |
3787 | /* We read one bunch by one (READ_BUF_SIZE bytes) to allow |
3788 | quitting while reading a huge while. */ | |
a5bb9bd3 | 3789 | /* `try'' is reserved in some compilers (Microsoft C). */ |
d311d28c | 3790 | int trytry = min (total - how_much, READ_BUF_SIZE - unprocessed); |
3dbcf3f6 RS |
3791 | |
3792 | /* Allow quitting out of the actual I/O. */ | |
3793 | immediate_quit = 1; | |
3794 | QUIT; | |
db327c7e | 3795 | this = emacs_read (fd, read_buf + unprocessed, trytry); |
3dbcf3f6 RS |
3796 | immediate_quit = 0; |
3797 | ||
db327c7e | 3798 | if (this <= 0) |
43aae36e | 3799 | break; |
3dbcf3f6 RS |
3800 | |
3801 | how_much += this; | |
3802 | ||
bf1c0f27 SM |
3803 | BUF_TEMP_SET_PT (XBUFFER (conversion_buffer), |
3804 | BUF_Z (XBUFFER (conversion_buffer))); | |
5976c3fe PE |
3805 | decode_coding_c_string (&coding, (unsigned char *) read_buf, |
3806 | unprocessed + this, conversion_buffer); | |
db327c7e KH |
3807 | unprocessed = coding.carryover_bytes; |
3808 | if (coding.carryover_bytes > 0) | |
72af86bd | 3809 | memcpy (read_buf, coding.carryover, unprocessed); |
3dbcf3f6 | 3810 | } |
2ba48777 | 3811 | UNGCPRO; |
db327c7e | 3812 | emacs_close (fd); |
3dbcf3f6 | 3813 | |
db65a627 CY |
3814 | /* We should remove the unwind_protect calling |
3815 | close_file_unwind, but other stuff has been added the stack, | |
3816 | so defer the removal till we reach the `handled' label. */ | |
3817 | deferred_remove_unwind_protect = 1; | |
3818 | ||
db327c7e KH |
3819 | /* At this point, HOW_MUCH should equal TOTAL, or should be <= 0 |
3820 | if we couldn't read the file. */ | |
3dbcf3f6 | 3821 | |
43aae36e | 3822 | if (this < 0) |
4ed925c6 MB |
3823 | error ("IO error reading %s: %s", |
3824 | SDATA (orig_filename), emacs_strerror (errno)); | |
3dbcf3f6 | 3825 | |
db327c7e KH |
3826 | if (unprocessed > 0) |
3827 | { | |
3828 | coding.mode |= CODING_MODE_LAST_BLOCK; | |
5976c3fe PE |
3829 | decode_coding_c_string (&coding, (unsigned char *) read_buf, |
3830 | unprocessed, conversion_buffer); | |
db327c7e KH |
3831 | coding.mode &= ~CODING_MODE_LAST_BLOCK; |
3832 | } | |
3833 | ||
50b06221 | 3834 | coding_system = CODING_ID_NAME (coding.id); |
f6a07420 | 3835 | set_coding_system = 1; |
db327c7e | 3836 | decoded = BUF_BEG_ADDR (XBUFFER (conversion_buffer)); |
50342b35 KH |
3837 | inserted = (BUF_Z_BYTE (XBUFFER (conversion_buffer)) |
3838 | - BUF_BEG_BYTE (XBUFFER (conversion_buffer))); | |
db327c7e KH |
3839 | |
3840 | /* Compare the beginning of the converted string with the buffer | |
3841 | text. */ | |
3dbcf3f6 RS |
3842 | |
3843 | bufpos = 0; | |
3844 | while (bufpos < inserted && same_at_start < same_at_end | |
db327c7e | 3845 | && FETCH_BYTE (same_at_start) == decoded[bufpos]) |
3dbcf3f6 RS |
3846 | same_at_start++, bufpos++; |
3847 | ||
db327c7e | 3848 | /* If the file matches the head of buffer completely, |
3dbcf3f6 RS |
3849 | there's no need to replace anything. */ |
3850 | ||
3851 | if (bufpos == inserted) | |
3852 | { | |
3dbcf3f6 | 3853 | /* Truncate the buffer to the size of the file. */ |
18a9f8d9 SM |
3854 | if (same_at_start == same_at_end) |
3855 | nochange = 1; | |
3856 | else | |
3857 | del_range_byte (same_at_start, same_at_end, 0); | |
427f5aab | 3858 | inserted = 0; |
e8553dd1 KH |
3859 | |
3860 | unbind_to (this_count, Qnil); | |
3dbcf3f6 RS |
3861 | goto handled; |
3862 | } | |
3863 | ||
db327c7e KH |
3864 | /* Extend the start of non-matching text area to the previous |
3865 | multibyte character boundary. */ | |
4b4deea2 | 3866 | if (! NILP (BVAR (current_buffer, enable_multibyte_characters))) |
20f6783d RS |
3867 | while (same_at_start > BEGV_BYTE |
3868 | && ! CHAR_HEAD_P (FETCH_BYTE (same_at_start))) | |
3869 | same_at_start--; | |
3870 | ||
3dbcf3f6 RS |
3871 | /* Scan this bufferful from the end, comparing with |
3872 | the Emacs buffer. */ | |
3873 | bufpos = inserted; | |
3874 | ||
3875 | /* Compare with same_at_start to avoid counting some buffer text | |
3876 | as matching both at the file's beginning and at the end. */ | |
3877 | while (bufpos > 0 && same_at_end > same_at_start | |
db327c7e | 3878 | && FETCH_BYTE (same_at_end - 1) == decoded[bufpos - 1]) |
3dbcf3f6 RS |
3879 | same_at_end--, bufpos--; |
3880 | ||
db327c7e KH |
3881 | /* Extend the end of non-matching text area to the next |
3882 | multibyte character boundary. */ | |
4b4deea2 | 3883 | if (! NILP (BVAR (current_buffer, enable_multibyte_characters))) |
20f6783d RS |
3884 | while (same_at_end < ZV_BYTE |
3885 | && ! CHAR_HEAD_P (FETCH_BYTE (same_at_end))) | |
3886 | same_at_end++; | |
3887 | ||
3dbcf3f6 | 3888 | /* Don't try to reuse the same piece of text twice. */ |
ec7adf26 | 3889 | overlap = same_at_start - BEGV_BYTE - (same_at_end + inserted - ZV_BYTE); |
3dbcf3f6 RS |
3890 | if (overlap > 0) |
3891 | same_at_end += overlap; | |
3892 | ||
727a0b4a RS |
3893 | /* If display currently starts at beginning of line, |
3894 | keep it that way. */ | |
d3d50620 | 3895 | if (XBUFFER (XWINDOW (selected_window)->buffer) == current_buffer) |
c98ff5dd | 3896 | XWINDOW (selected_window)->start_at_line_beg = !NILP (Fbolp ()); |
727a0b4a | 3897 | |
3dbcf3f6 RS |
3898 | /* Replace the chars that we need to replace, |
3899 | and update INSERTED to equal the number of bytes | |
db327c7e | 3900 | we are taking from the decoded string. */ |
4b70e2c9 | 3901 | inserted -= (ZV_BYTE - same_at_end) + (same_at_start - BEGV_BYTE); |
427f5aab | 3902 | |
643c73b9 | 3903 | if (same_at_end != same_at_start) |
427f5aab KH |
3904 | { |
3905 | del_range_byte (same_at_start, same_at_end, 0); | |
3906 | temp = GPT; | |
3907 | same_at_start = GPT_BYTE; | |
3908 | } | |
643c73b9 RS |
3909 | else |
3910 | { | |
643c73b9 | 3911 | temp = BYTE_TO_CHAR (same_at_start); |
643c73b9 | 3912 | } |
427f5aab KH |
3913 | /* Insert from the file at the proper position. */ |
3914 | SET_PT_BOTH (temp, same_at_start); | |
50342b35 KH |
3915 | same_at_start_charpos |
3916 | = buf_bytepos_to_charpos (XBUFFER (conversion_buffer), | |
7f5d2c72 SM |
3917 | same_at_start - BEGV_BYTE |
3918 | + BUF_BEG_BYTE (XBUFFER (conversion_buffer))); | |
50342b35 KH |
3919 | inserted_chars |
3920 | = (buf_bytepos_to_charpos (XBUFFER (conversion_buffer), | |
7f5d2c72 SM |
3921 | same_at_start + inserted - BEGV_BYTE |
3922 | + BUF_BEG_BYTE (XBUFFER (conversion_buffer))) | |
50342b35 | 3923 | - same_at_start_charpos); |
d07af40d KH |
3924 | /* This binding is to avoid ask-user-about-supersession-threat |
3925 | being called in insert_from_buffer (via in | |
3926 | prepare_to_modify_buffer). */ | |
3927 | specbind (intern ("buffer-file-name"), Qnil); | |
db327c7e | 3928 | insert_from_buffer (XBUFFER (conversion_buffer), |
50342b35 | 3929 | same_at_start_charpos, inserted_chars, 0); |
427f5aab KH |
3930 | /* Set `inserted' to the number of inserted characters. */ |
3931 | inserted = PT - temp; | |
77343e1d KH |
3932 | /* Set point before the inserted characters. */ |
3933 | SET_PT_BOTH (temp, same_at_start); | |
3dbcf3f6 | 3934 | |
db327c7e | 3935 | unbind_to (this_count, Qnil); |
3dbcf3f6 | 3936 | |
3dbcf3f6 | 3937 | goto handled; |
3d0387c0 RS |
3938 | } |
3939 | ||
d4b8687b | 3940 | if (! not_regular) |
728f8f0a | 3941 | total = end_offset - beg_offset; |
d4b8687b RS |
3942 | else |
3943 | /* For a special file, all we can do is guess. */ | |
3944 | total = READ_BUF_SIZE; | |
570d7624 | 3945 | |
8b913b57 | 3946 | if (NILP (visit) && total > 0) |
68780e2a RS |
3947 | { |
3948 | #ifdef CLASH_DETECTION | |
4b4deea2 | 3949 | if (!NILP (BVAR (current_buffer, file_truename)) |
68780e2a | 3950 | /* Make binding buffer-file-name to nil effective. */ |
4b4deea2 | 3951 | && !NILP (BVAR (current_buffer, filename)) |
68780e2a RS |
3952 | && SAVE_MODIFF >= MODIFF) |
3953 | we_locked_file = 1; | |
3954 | #endif /* CLASH_DETECTION */ | |
3955 | prepare_to_modify_buffer (GPT, GPT, NULL); | |
3956 | } | |
570d7624 | 3957 | |
7fe52289 | 3958 | move_gap (PT); |
7fded690 JB |
3959 | if (GAP_SIZE < total) |
3960 | make_gap (total - GAP_SIZE); | |
3961 | ||
728f8f0a | 3962 | if (beg_offset != 0 || !NILP (replace)) |
7fded690 | 3963 | { |
728f8f0a | 3964 | if (lseek (fd, beg_offset, SEEK_SET) < 0) |
b1d1b865 RS |
3965 | report_file_error ("Setting file position", |
3966 | Fcons (orig_filename, Qnil)); | |
7fded690 JB |
3967 | } |
3968 | ||
6fdaa9a0 | 3969 | /* In the following loop, HOW_MUCH contains the total bytes read so |
c8a6d68a KH |
3970 | far for a regular file, and not changed for a special file. But, |
3971 | before exiting the loop, it is set to a negative value if I/O | |
3972 | error occurs. */ | |
a1d2b64a | 3973 | how_much = 0; |
efdc16c9 | 3974 | |
6fdaa9a0 KH |
3975 | /* Total bytes inserted. */ |
3976 | inserted = 0; | |
efdc16c9 | 3977 | |
c8a6d68a | 3978 | /* Here, we don't do code conversion in the loop. It is done by |
db327c7e | 3979 | decode_coding_gap after all data are read into the buffer. */ |
1b978129 | 3980 | { |
d311d28c | 3981 | ptrdiff_t gap_size = GAP_SIZE; |
efdc16c9 | 3982 | |
1b978129 GM |
3983 | while (how_much < total) |
3984 | { | |
5e570b75 | 3985 | /* try is reserved in some compilers (Microsoft C) */ |
d311d28c PE |
3986 | int trytry = min (total - how_much, READ_BUF_SIZE); |
3987 | ptrdiff_t this; | |
570d7624 | 3988 | |
1b978129 GM |
3989 | if (not_regular) |
3990 | { | |
f839df0c | 3991 | Lisp_Object nbytes; |
570d7624 | 3992 | |
1b978129 GM |
3993 | /* Maybe make more room. */ |
3994 | if (gap_size < trytry) | |
3995 | { | |
3996 | make_gap (total - gap_size); | |
3997 | gap_size = GAP_SIZE; | |
3998 | } | |
3999 | ||
4000 | /* Read from the file, capturing `quit'. When an | |
4001 | error occurs, end the loop, and arrange for a quit | |
4002 | to be signaled after decoding the text we read. */ | |
4003 | non_regular_fd = fd; | |
4004 | non_regular_inserted = inserted; | |
4005 | non_regular_nbytes = trytry; | |
f839df0c PE |
4006 | nbytes = internal_condition_case_1 (read_non_regular, |
4007 | Qnil, Qerror, | |
4008 | read_non_regular_quit); | |
4009 | if (NILP (nbytes)) | |
1b978129 GM |
4010 | { |
4011 | read_quit = 1; | |
4012 | break; | |
4013 | } | |
4014 | ||
f839df0c | 4015 | this = XINT (nbytes); |
1b978129 GM |
4016 | } |
4017 | else | |
4018 | { | |
4019 | /* Allow quitting out of the actual I/O. We don't make text | |
4020 | part of the buffer until all the reading is done, so a C-g | |
4021 | here doesn't do any harm. */ | |
4022 | immediate_quit = 1; | |
4023 | QUIT; | |
5976c3fe PE |
4024 | this = emacs_read (fd, |
4025 | ((char *) BEG_ADDR + PT_BYTE - BEG_BYTE | |
4026 | + inserted), | |
4027 | trytry); | |
1b978129 GM |
4028 | immediate_quit = 0; |
4029 | } | |
efdc16c9 | 4030 | |
1b978129 GM |
4031 | if (this <= 0) |
4032 | { | |
4033 | how_much = this; | |
4034 | break; | |
4035 | } | |
4036 | ||
4037 | gap_size -= this; | |
4038 | ||
4039 | /* For a regular file, where TOTAL is the real size, | |
4040 | count HOW_MUCH to compare with it. | |
4041 | For a special file, where TOTAL is just a buffer size, | |
4042 | so don't bother counting in HOW_MUCH. | |
4043 | (INSERTED is where we count the number of characters inserted.) */ | |
4044 | if (! not_regular) | |
4045 | how_much += this; | |
4046 | inserted += this; | |
4047 | } | |
4048 | } | |
4049 | ||
68780e2a RS |
4050 | /* Now we have read all the file data into the gap. |
4051 | If it was empty, undo marking the buffer modified. */ | |
4052 | ||
4053 | if (inserted == 0) | |
4054 | { | |
6840d350 | 4055 | #ifdef CLASH_DETECTION |
68780e2a | 4056 | if (we_locked_file) |
4b4deea2 | 4057 | unlock_file (BVAR (current_buffer, file_truename)); |
6840d350 | 4058 | #endif |
68780e2a RS |
4059 | Vdeactivate_mark = old_Vdeactivate_mark; |
4060 | } | |
83c1cf6d RS |
4061 | else |
4062 | Vdeactivate_mark = Qt; | |
68780e2a | 4063 | |
1b978129 GM |
4064 | /* Make the text read part of the buffer. */ |
4065 | GAP_SIZE -= inserted; | |
4066 | GPT += inserted; | |
4067 | GPT_BYTE += inserted; | |
4068 | ZV += inserted; | |
4069 | ZV_BYTE += inserted; | |
4070 | Z += inserted; | |
4071 | Z_BYTE += inserted; | |
6fdaa9a0 | 4072 | |
c8a6d68a KH |
4073 | if (GAP_SIZE > 0) |
4074 | /* Put an anchor to ensure multi-byte form ends at gap. */ | |
4075 | *GPT_ADDR = 0; | |
d4b8687b | 4076 | |
68c45bf0 | 4077 | emacs_close (fd); |
6fdaa9a0 | 4078 | |
c8a6d68a KH |
4079 | /* Discard the unwind protect for closing the file. */ |
4080 | specpdl_ptr--; | |
6fdaa9a0 | 4081 | |
c8a6d68a KH |
4082 | if (how_much < 0) |
4083 | error ("IO error reading %s: %s", | |
d5db4077 | 4084 | SDATA (orig_filename), emacs_strerror (errno)); |
ec7adf26 | 4085 | |
f8569325 DL |
4086 | notfound: |
4087 | ||
db327c7e | 4088 | if (NILP (coding_system)) |
c8a6d68a | 4089 | { |
2df42e09 | 4090 | /* The coding system is not yet decided. Decide it by an |
dfe35e7b RS |
4091 | optimized method for handling `coding:' tag. |
4092 | ||
4093 | Note that we can get here only if the buffer was empty | |
4094 | before the insertion. */ | |
f736ffbf | 4095 | |
2df42e09 | 4096 | if (!NILP (Vcoding_system_for_read)) |
db327c7e | 4097 | coding_system = Vcoding_system_for_read; |
2df42e09 KH |
4098 | else |
4099 | { | |
98a7d268 KH |
4100 | /* Since we are sure that the current buffer was empty |
4101 | before the insertion, we can toggle | |
4102 | enable-multibyte-characters directly here without taking | |
9a7f80aa KH |
4103 | care of marker adjustment. By this way, we can run Lisp |
4104 | program safely before decoding the inserted text. */ | |
98a7d268 | 4105 | Lisp_Object unwind_data; |
d311d28c | 4106 | ptrdiff_t count1 = SPECPDL_INDEX (); |
2df42e09 | 4107 | |
4b4deea2 TT |
4108 | unwind_data = Fcons (BVAR (current_buffer, enable_multibyte_characters), |
4109 | Fcons (BVAR (current_buffer, undo_list), | |
98a7d268 | 4110 | Fcurrent_buffer ())); |
39eb03f1 PE |
4111 | bset_enable_multibyte_characters (current_buffer, Qnil); |
4112 | bset_undo_list (current_buffer, Qt); | |
98a7d268 KH |
4113 | record_unwind_protect (decide_coding_unwind, unwind_data); |
4114 | ||
4115 | if (inserted > 0 && ! NILP (Vset_auto_coding_function)) | |
4116 | { | |
db327c7e | 4117 | coding_system = call2 (Vset_auto_coding_function, |
8f924df7 | 4118 | filename, make_number (inserted)); |
2df42e09 | 4119 | } |
f736ffbf | 4120 | |
db327c7e | 4121 | if (NILP (coding_system)) |
2df42e09 KH |
4122 | { |
4123 | /* If the coding system is not yet decided, check | |
4124 | file-coding-system-alist. */ | |
8f924df7 | 4125 | Lisp_Object args[6]; |
f736ffbf | 4126 | |
2df42e09 KH |
4127 | args[0] = Qinsert_file_contents, args[1] = orig_filename; |
4128 | args[2] = visit, args[3] = beg, args[4] = end, args[5] = Qnil; | |
8f924df7 KH |
4129 | coding_system = Ffind_operation_coding_system (6, args); |
4130 | if (CONSP (coding_system)) | |
4131 | coding_system = XCAR (coding_system); | |
f736ffbf | 4132 | } |
f839df0c | 4133 | unbind_to (count1, Qnil); |
98a7d268 | 4134 | inserted = Z_BYTE - BEG_BYTE; |
2df42e09 | 4135 | } |
f736ffbf | 4136 | |
db327c7e KH |
4137 | if (NILP (coding_system)) |
4138 | coding_system = Qundecided; | |
4139 | else | |
4140 | CHECK_CODING_SYSTEM (coding_system); | |
f736ffbf | 4141 | |
4b4deea2 | 4142 | if (NILP (BVAR (current_buffer, enable_multibyte_characters))) |
237a6fd2 | 4143 | /* We must suppress all character code conversion except for |
2df42e09 | 4144 | end-of-line conversion. */ |
db327c7e | 4145 | coding_system = raw_text_coding_system (coding_system); |
db327c7e KH |
4146 | setup_coding_system (coding_system, &coding); |
4147 | /* Ensure we set Vlast_coding_system_used. */ | |
4148 | set_coding_system = 1; | |
2df42e09 | 4149 | } |
f736ffbf | 4150 | |
db327c7e | 4151 | if (!NILP (visit)) |
8c3b9441 | 4152 | { |
db327c7e | 4153 | /* When we visit a file by raw-text, we change the buffer to |
9a7f80aa | 4154 | unibyte. */ |
db327c7e KH |
4155 | if (CODING_FOR_UNIBYTE (&coding) |
4156 | /* Can't do this if part of the buffer might be preserved. */ | |
4157 | && NILP (replace)) | |
4158 | /* Visiting a file with these coding system makes the buffer | |
4159 | unibyte. */ | |
39eb03f1 | 4160 | bset_enable_multibyte_characters (current_buffer, Qnil); |
8c3b9441 KH |
4161 | } |
4162 | ||
4b4deea2 | 4163 | coding.dst_multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters)); |
5b359650 | 4164 | if (CODING_MAY_REQUIRE_DECODING (&coding) |
1c157f8d | 4165 | && (inserted > 0 || CODING_REQUIRE_FLUSHING (&coding))) |
2df42e09 | 4166 | { |
db327c7e KH |
4167 | move_gap_both (PT, PT_BYTE); |
4168 | GAP_SIZE += inserted; | |
4169 | ZV_BYTE -= inserted; | |
4170 | Z_BYTE -= inserted; | |
4171 | ZV -= inserted; | |
4172 | Z -= inserted; | |
4173 | decode_coding_gap (&coding, inserted, inserted); | |
4174 | inserted = coding.produced_char; | |
5b359650 | 4175 | coding_system = CODING_ID_NAME (coding.id); |
2df42e09 | 4176 | } |
db327c7e KH |
4177 | else if (inserted > 0) |
4178 | adjust_after_insert (PT, PT_BYTE, PT + inserted, PT_BYTE + inserted, | |
4179 | inserted); | |
570d7624 | 4180 | |
0068070e SM |
4181 | /* Call after-change hooks for the inserted text, aside from the case |
4182 | of normal visiting (not with REPLACE), which is done in a new buffer | |
4183 | "before" the buffer is changed. */ | |
4184 | if (inserted > 0 && total > 0 | |
4185 | && (NILP (visit) || !NILP (replace))) | |
4186 | { | |
4187 | signal_after_change (PT, 0, inserted); | |
4188 | update_compositions (PT, PT, CHECK_BORDER); | |
4189 | } | |
4190 | ||
cf6d2357 RS |
4191 | /* Now INSERTED is measured in characters. */ |
4192 | ||
32f4334d | 4193 | handled: |
570d7624 | 4194 | |
db65a627 CY |
4195 | if (deferred_remove_unwind_protect) |
4196 | /* If requested above, discard the unwind protect for closing the | |
4197 | file. */ | |
4198 | specpdl_ptr--; | |
4199 | ||
265a9e55 | 4200 | if (!NILP (visit)) |
570d7624 | 4201 | { |
4b4deea2 | 4202 | if (!EQ (BVAR (current_buffer, undo_list), Qt) && !nochange) |
39eb03f1 | 4203 | bset_undo_list (current_buffer, Qnil); |
62bcf009 | 4204 | |
a7e82472 RS |
4205 | if (NILP (handler)) |
4206 | { | |
dbeed9a6 | 4207 | current_buffer->modtime = mtime; |
58b963f7 | 4208 | current_buffer->modtime_size = st.st_size; |
39eb03f1 | 4209 | bset_filename (current_buffer, orig_filename); |
a7e82472 | 4210 | } |
62bcf009 | 4211 | |
95385625 | 4212 | SAVE_MODIFF = MODIFF; |
0b5397c2 | 4213 | BUF_AUTOSAVE_MODIFF (current_buffer) = MODIFF; |
4b4deea2 | 4214 | XSETFASTINT (BVAR (current_buffer, save_length), Z - BEG); |
570d7624 | 4215 | #ifdef CLASH_DETECTION |
32f4334d RS |
4216 | if (NILP (handler)) |
4217 | { | |
4b4deea2 TT |
4218 | if (!NILP (BVAR (current_buffer, file_truename))) |
4219 | unlock_file (BVAR (current_buffer, file_truename)); | |
32f4334d RS |
4220 | unlock_file (filename); |
4221 | } | |
570d7624 | 4222 | #endif /* CLASH_DETECTION */ |
330bfe57 | 4223 | if (not_regular) |
24b1ddad KS |
4224 | xsignal2 (Qfile_error, |
4225 | build_string ("not a regular file"), orig_filename); | |
570d7624 JB |
4226 | } |
4227 | ||
b6426b03 | 4228 | if (set_coding_system) |
8f924df7 | 4229 | Vlast_coding_system_used = coding_system; |
b6426b03 | 4230 | |
2080470e | 4231 | if (! NILP (Ffboundp (Qafter_insert_file_set_coding))) |
b6426b03 | 4232 | { |
37a3c774 KH |
4233 | insval = call2 (Qafter_insert_file_set_coding, make_number (inserted), |
4234 | visit); | |
b6426b03 KH |
4235 | if (! NILP (insval)) |
4236 | { | |
d311d28c PE |
4237 | if (! RANGED_INTEGERP (0, insval, ZV - PT)) |
4238 | wrong_type_argument (intern ("inserted-chars"), insval); | |
b6426b03 KH |
4239 | inserted = XFASTINT (insval); |
4240 | } | |
4241 | } | |
4242 | ||
6420e80c | 4243 | /* Decode file format. */ |
c8a6d68a | 4244 | if (inserted > 0) |
0d420e88 | 4245 | { |
6420e80c | 4246 | /* Don't run point motion or modification hooks when decoding. */ |
d311d28c PE |
4247 | ptrdiff_t count1 = SPECPDL_INDEX (); |
4248 | ptrdiff_t old_inserted = inserted; | |
6f2528d8 MR |
4249 | specbind (Qinhibit_point_motion_hooks, Qt); |
4250 | specbind (Qinhibit_modification_hooks, Qt); | |
4251 | ||
6420e80c | 4252 | /* Save old undo list and don't record undo for decoding. */ |
4b4deea2 | 4253 | old_undo = BVAR (current_buffer, undo_list); |
39eb03f1 | 4254 | bset_undo_list (current_buffer, Qt); |
efdc16c9 | 4255 | |
6f2528d8 | 4256 | if (NILP (replace)) |
ed8e506f | 4257 | { |
6f2528d8 MR |
4258 | insval = call3 (Qformat_decode, |
4259 | Qnil, make_number (inserted), visit); | |
d311d28c PE |
4260 | if (! RANGED_INTEGERP (0, insval, ZV - PT)) |
4261 | wrong_type_argument (intern ("inserted-chars"), insval); | |
6f2528d8 MR |
4262 | inserted = XFASTINT (insval); |
4263 | } | |
4264 | else | |
4265 | { | |
4266 | /* If REPLACE is non-nil and we succeeded in not replacing the | |
6420e80c AS |
4267 | beginning or end of the buffer text with the file's contents, |
4268 | call format-decode with `point' positioned at the beginning | |
fa463103 | 4269 | of the buffer and `inserted' equaling the number of |
6420e80c AS |
4270 | characters in the buffer. Otherwise, format-decode might |
4271 | fail to correctly analyze the beginning or end of the buffer. | |
4272 | Hence we temporarily save `point' and `inserted' here and | |
4273 | restore `point' iff format-decode did not insert or delete | |
4274 | any text. Otherwise we leave `point' at point-min. */ | |
d311d28c PE |
4275 | ptrdiff_t opoint = PT; |
4276 | ptrdiff_t opoint_byte = PT_BYTE; | |
4277 | ptrdiff_t oinserted = ZV - BEGV; | |
4278 | EMACS_INT ochars_modiff = CHARS_MODIFF; | |
1f163f28 MA |
4279 | |
4280 | TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE); | |
6f2528d8 MR |
4281 | insval = call3 (Qformat_decode, |
4282 | Qnil, make_number (oinserted), visit); | |
d311d28c PE |
4283 | if (! RANGED_INTEGERP (0, insval, ZV - PT)) |
4284 | wrong_type_argument (intern ("inserted-chars"), insval); | |
cac4219c MR |
4285 | if (ochars_modiff == CHARS_MODIFF) |
4286 | /* format_decode didn't modify buffer's characters => move | |
4287 | point back to position before inserted text and leave | |
6420e80c | 4288 | value of inserted alone. */ |
6f2528d8 | 4289 | SET_PT_BOTH (opoint, opoint_byte); |
cac4219c MR |
4290 | else |
4291 | /* format_decode modified buffer's characters => consider | |
6420e80c | 4292 | entire buffer changed and leave point at point-min. */ |
cac4219c | 4293 | inserted = XFASTINT (insval); |
ed8e506f | 4294 | } |
efdc16c9 | 4295 | |
6f2528d8 | 4296 | /* For consistency with format-decode call these now iff inserted > 0 |
6420e80c | 4297 | (martin 2007-06-28). */ |
6f2528d8 MR |
4298 | p = Vafter_insert_file_functions; |
4299 | while (CONSP (p)) | |
4300 | { | |
4301 | if (NILP (replace)) | |
4302 | { | |
4303 | insval = call1 (XCAR (p), make_number (inserted)); | |
4304 | if (!NILP (insval)) | |
4305 | { | |
d311d28c PE |
4306 | if (! RANGED_INTEGERP (0, insval, ZV - PT)) |
4307 | wrong_type_argument (intern ("inserted-chars"), insval); | |
6f2528d8 MR |
4308 | inserted = XFASTINT (insval); |
4309 | } | |
4310 | } | |
4311 | else | |
4312 | { | |
6420e80c AS |
4313 | /* For the rationale of this see the comment on |
4314 | format-decode above. */ | |
d311d28c PE |
4315 | ptrdiff_t opoint = PT; |
4316 | ptrdiff_t opoint_byte = PT_BYTE; | |
4317 | ptrdiff_t oinserted = ZV - BEGV; | |
4318 | EMACS_INT ochars_modiff = CHARS_MODIFF; | |
1f163f28 | 4319 | |
6f2528d8 MR |
4320 | TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE); |
4321 | insval = call1 (XCAR (p), make_number (oinserted)); | |
4322 | if (!NILP (insval)) | |
4323 | { | |
d311d28c PE |
4324 | if (! RANGED_INTEGERP (0, insval, ZV - PT)) |
4325 | wrong_type_argument (intern ("inserted-chars"), insval); | |
cac4219c MR |
4326 | if (ochars_modiff == CHARS_MODIFF) |
4327 | /* after_insert_file_functions didn't modify | |
4328 | buffer's characters => move point back to | |
4329 | position before inserted text and leave value of | |
6420e80c | 4330 | inserted alone. */ |
6f2528d8 | 4331 | SET_PT_BOTH (opoint, opoint_byte); |
cac4219c MR |
4332 | else |
4333 | /* after_insert_file_functions did modify buffer's | |
4334 | characters => consider entire buffer changed and | |
6420e80c | 4335 | leave point at point-min. */ |
cac4219c | 4336 | inserted = XFASTINT (insval); |
6f2528d8 MR |
4337 | } |
4338 | } | |
4339 | ||
4340 | QUIT; | |
4341 | p = XCDR (p); | |
ed8e506f | 4342 | } |
efdc16c9 | 4343 | |
6f2528d8 MR |
4344 | if (NILP (visit)) |
4345 | { | |
39eb03f1 | 4346 | bset_undo_list (current_buffer, old_undo); |
6420e80c | 4347 | if (CONSP (old_undo) && inserted != old_inserted) |
6f2528d8 | 4348 | { |
6420e80c AS |
4349 | /* Adjust the last undo record for the size change during |
4350 | the format conversion. */ | |
6f2528d8 | 4351 | Lisp_Object tem = XCAR (old_undo); |
6420e80c AS |
4352 | if (CONSP (tem) && INTEGERP (XCAR (tem)) |
4353 | && INTEGERP (XCDR (tem)) | |
4354 | && XFASTINT (XCDR (tem)) == PT + old_inserted) | |
4355 | XSETCDR (tem, make_number (PT + inserted)); | |
6f2528d8 MR |
4356 | } |
4357 | } | |
6f2528d8 | 4358 | else |
1bc99c9c | 4359 | /* If undo_list was Qt before, keep it that way. |
6420e80c | 4360 | Otherwise start with an empty undo_list. */ |
39eb03f1 | 4361 | bset_undo_list (current_buffer, EQ (old_undo, Qt) ? Qt : Qnil); |
efdc16c9 | 4362 | |
f839df0c | 4363 | unbind_to (count1, Qnil); |
0d420e88 BG |
4364 | } |
4365 | ||
f8569325 | 4366 | if (!NILP (visit) |
d35af63c | 4367 | && EMACS_NSECS (current_buffer->modtime) == NONEXISTENT_MODTIME_NSECS) |
f8569325 DL |
4368 | { |
4369 | /* If visiting nonexistent file, return nil. */ | |
e5e9d610 | 4370 | errno = save_errno; |
f8569325 | 4371 | report_file_error ("Opening input file", Fcons (orig_filename, Qnil)); |
d6a3cc15 RS |
4372 | } |
4373 | ||
1b978129 GM |
4374 | if (read_quit) |
4375 | Fsignal (Qquit, Qnil); | |
4376 | ||
ec7adf26 | 4377 | /* ??? Retval needs to be dealt with in all cases consistently. */ |
a1d2b64a | 4378 | if (NILP (val)) |
b1d1b865 | 4379 | val = Fcons (orig_filename, |
a1d2b64a RS |
4380 | Fcons (make_number (inserted), |
4381 | Qnil)); | |
4382 | ||
4383 | RETURN_UNGCPRO (unbind_to (count, val)); | |
570d7624 | 4384 | } |
7fded690 | 4385 | \f |
f57e2426 | 4386 | static Lisp_Object build_annotations (Lisp_Object, Lisp_Object); |
d6a3cc15 | 4387 | |
199607e4 | 4388 | static Lisp_Object |
971de7fb | 4389 | build_annotations_unwind (Lisp_Object arg) |
6fc6f94b | 4390 | { |
67fbc0cb | 4391 | Vwrite_region_annotation_buffers = arg; |
6fc6f94b RS |
4392 | return Qnil; |
4393 | } | |
4394 | ||
7c82a4a9 SM |
4395 | /* Decide the coding-system to encode the data with. */ |
4396 | ||
c934586d | 4397 | static Lisp_Object |
dd4c5104 DN |
4398 | choose_write_coding_system (Lisp_Object start, Lisp_Object end, Lisp_Object filename, |
4399 | Lisp_Object append, Lisp_Object visit, Lisp_Object lockname, | |
4400 | struct coding_system *coding) | |
7c82a4a9 SM |
4401 | { |
4402 | Lisp_Object val; | |
75421805 | 4403 | Lisp_Object eol_parent = Qnil; |
7c82a4a9 | 4404 | |
6b61353c | 4405 | if (auto_saving |
4b4deea2 TT |
4406 | && NILP (Fstring_equal (BVAR (current_buffer, filename), |
4407 | BVAR (current_buffer, auto_save_file_name)))) | |
75421805 KH |
4408 | { |
4409 | val = Qutf_8_emacs; | |
4410 | eol_parent = Qunix; | |
4411 | } | |
7c82a4a9 | 4412 | else if (!NILP (Vcoding_system_for_write)) |
42b01e1e KH |
4413 | { |
4414 | val = Vcoding_system_for_write; | |
4415 | if (coding_system_require_warning | |
4416 | && !NILP (Ffboundp (Vselect_safe_coding_system_function))) | |
4417 | /* Confirm that VAL can surely encode the current region. */ | |
4418 | val = call5 (Vselect_safe_coding_system_function, | |
4419 | start, end, Fcons (Qt, Fcons (val, Qnil)), | |
4420 | Qnil, filename); | |
4421 | } | |
7c82a4a9 SM |
4422 | else |
4423 | { | |
4424 | /* If the variable `buffer-file-coding-system' is set locally, | |
4425 | it means that the file was read with some kind of code | |
4426 | conversion or the variable is explicitly set by users. We | |
4427 | had better write it out with the same coding system even if | |
4428 | `enable-multibyte-characters' is nil. | |
4429 | ||
4430 | If it is not set locally, we anyway have to convert EOL | |
4431 | format if the default value of `buffer-file-coding-system' | |
4432 | tells that it is not Unix-like (LF only) format. */ | |
f75d7a91 PE |
4433 | bool using_default_coding = 0; |
4434 | bool force_raw_text = 0; | |
7c82a4a9 | 4435 | |
4b4deea2 | 4436 | val = BVAR (current_buffer, buffer_file_coding_system); |
7c82a4a9 SM |
4437 | if (NILP (val) |
4438 | || NILP (Flocal_variable_p (Qbuffer_file_coding_system, Qnil))) | |
4439 | { | |
4440 | val = Qnil; | |
4b4deea2 | 4441 | if (NILP (BVAR (current_buffer, enable_multibyte_characters))) |
7c82a4a9 SM |
4442 | force_raw_text = 1; |
4443 | } | |
efdc16c9 | 4444 | |
7c82a4a9 SM |
4445 | if (NILP (val)) |
4446 | { | |
4447 | /* Check file-coding-system-alist. */ | |
4448 | Lisp_Object args[7], coding_systems; | |
4449 | ||
4450 | args[0] = Qwrite_region; args[1] = start; args[2] = end; | |
4451 | args[3] = filename; args[4] = append; args[5] = visit; | |
4452 | args[6] = lockname; | |
4453 | coding_systems = Ffind_operation_coding_system (7, args); | |
4454 | if (CONSP (coding_systems) && !NILP (XCDR (coding_systems))) | |
4455 | val = XCDR (coding_systems); | |
4456 | } | |
4457 | ||
c934586d | 4458 | if (NILP (val)) |
7c82a4a9 SM |
4459 | { |
4460 | /* If we still have not decided a coding system, use the | |
4461 | default value of buffer-file-coding-system. */ | |
4b4deea2 | 4462 | val = BVAR (current_buffer, buffer_file_coding_system); |
7c82a4a9 SM |
4463 | using_default_coding = 1; |
4464 | } | |
efdc16c9 | 4465 | |
db327c7e KH |
4466 | if (! NILP (val) && ! force_raw_text) |
4467 | { | |
4468 | Lisp_Object spec, attrs; | |
4469 | ||
4470 | CHECK_CODING_SYSTEM_GET_SPEC (val, spec); | |
4471 | attrs = AREF (spec, 0); | |
4472 | if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text)) | |
4473 | force_raw_text = 1; | |
4474 | } | |
4475 | ||
7c82a4a9 SM |
4476 | if (!force_raw_text |
4477 | && !NILP (Ffboundp (Vselect_safe_coding_system_function))) | |
4478 | /* Confirm that VAL can surely encode the current region. */ | |
905a4276 PJ |
4479 | val = call5 (Vselect_safe_coding_system_function, |
4480 | start, end, val, Qnil, filename); | |
7c82a4a9 | 4481 | |
db327c7e KH |
4482 | /* If the decided coding-system doesn't specify end-of-line |
4483 | format, we use that of | |
4484 | `default-buffer-file-coding-system'. */ | |
c934586d | 4485 | if (! using_default_coding |
4b4deea2 | 4486 | && ! NILP (BVAR (&buffer_defaults, buffer_file_coding_system))) |
db327c7e | 4487 | val = (coding_inherit_eol_type |
4b4deea2 | 4488 | (val, BVAR (&buffer_defaults, buffer_file_coding_system))); |
7c82a4a9 | 4489 | |
db327c7e KH |
4490 | /* If we decide not to encode text, use `raw-text' or one of its |
4491 | subsidiaries. */ | |
7c82a4a9 | 4492 | if (force_raw_text) |
db327c7e | 4493 | val = raw_text_coding_system (val); |
7c82a4a9 SM |
4494 | } |
4495 | ||
75421805 | 4496 | val = coding_inherit_eol_type (val, eol_parent); |
c934586d | 4497 | setup_coding_system (val, coding); |
7c82a4a9 | 4498 | |
4b4deea2 | 4499 | if (!STRINGP (start) && !NILP (BVAR (current_buffer, selective_display))) |
7c82a4a9 | 4500 | coding->mode |= CODING_MODE_SELECTIVE_DISPLAY; |
c934586d | 4501 | return val; |
7c82a4a9 SM |
4502 | } |
4503 | ||
a7ca3326 | 4504 | DEFUN ("write-region", Fwrite_region, Swrite_region, 3, 7, |
8c1a1077 PJ |
4505 | "r\nFWrite region to file: \ni\ni\ni\np", |
4506 | doc: /* Write current region into specified file. | |
c2efea25 RS |
4507 | When called from a program, requires three arguments: |
4508 | START, END and FILENAME. START and END are normally buffer positions | |
4509 | specifying the part of the buffer to write. | |
4510 | If START is nil, that means to use the entire buffer contents. | |
4511 | If START is a string, then output that string to the file | |
4512 | instead of any buffer contents; END is ignored. | |
4513 | ||
8c1a1077 PJ |
4514 | Optional fourth argument APPEND if non-nil means |
4515 | append to existing file contents (if any). If it is an integer, | |
4516 | seek to that offset in the file before writing. | |
36e50520 | 4517 | Optional fifth argument VISIT, if t or a string, means |
8c1a1077 PJ |
4518 | set the last-save-file-modtime of buffer to this file's modtime |
4519 | and mark buffer not modified. | |
4520 | If VISIT is a string, it is a second file name; | |
4521 | the output goes to FILENAME, but the buffer is marked as visiting VISIT. | |
4522 | VISIT is also the file name to lock and unlock for clash detection. | |
4523 | If VISIT is neither t nor nil nor a string, | |
5f4e6aa9 | 4524 | that means do not display the \"Wrote file\" message. |
8c1a1077 PJ |
4525 | The optional sixth arg LOCKNAME, if non-nil, specifies the name to |
4526 | use for locking and unlocking, overriding FILENAME and VISIT. | |
4527 | The optional seventh arg MUSTBENEW, if non-nil, insists on a check | |
4528 | for an existing file with the same name. If MUSTBENEW is `excl', | |
4529 | that means to get an error if the file already exists; never overwrite. | |
4530 | If MUSTBENEW is neither nil nor `excl', that means ask for | |
4531 | confirmation before overwriting, but do go ahead and overwrite the file | |
4532 | if the user confirms. | |
8c1a1077 PJ |
4533 | |
4534 | This does code conversion according to the value of | |
4535 | `coding-system-for-write', `buffer-file-coding-system', or | |
4536 | `file-coding-system-alist', and sets the variable | |
aacd8ba1 GM |
4537 | `last-coding-system-used' to the coding system actually used. |
4538 | ||
4539 | This calls `write-region-annotate-functions' at the start, and | |
4540 | `write-region-post-annotation-function' at the end. */) | |
5842a27b | 4541 | (Lisp_Object start, Lisp_Object end, Lisp_Object filename, Lisp_Object append, Lisp_Object visit, Lisp_Object lockname, Lisp_Object mustbenew) |
570d7624 | 4542 | { |
f75d7a91 PE |
4543 | int desc; |
4544 | bool ok; | |
6bbd7a29 | 4545 | int save_errno = 0; |
5976c3fe | 4546 | const char *fn; |
570d7624 | 4547 | struct stat st; |
d311d28c | 4548 | ptrdiff_t count = SPECPDL_INDEX (); |
6fc6f94b | 4549 | int count1; |
3eac9910 | 4550 | Lisp_Object handler; |
4ad827c5 | 4551 | Lisp_Object visit_file; |
65b7d3e7 | 4552 | Lisp_Object annotations; |
b1d1b865 | 4553 | Lisp_Object encoded_filename; |
f75d7a91 PE |
4554 | bool visiting = (EQ (visit, Qt) || STRINGP (visit)); |
4555 | bool quietly = !NILP (visit); | |
7204a979 | 4556 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; |
6fc6f94b | 4557 | struct buffer *given_buffer; |
6fdaa9a0 | 4558 | struct coding_system coding; |
570d7624 | 4559 | |
d3a67486 | 4560 | if (current_buffer->base_buffer && visiting) |
95385625 RS |
4561 | error ("Cannot do file visiting in an indirect buffer"); |
4562 | ||
561cb8e1 | 4563 | if (!NILP (start) && !STRINGP (start)) |
570d7624 JB |
4564 | validate_region (&start, &end); |
4565 | ||
95c1c901 | 4566 | visit_file = Qnil; |
59fac292 | 4567 | GCPRO5 (start, filename, visit, visit_file, lockname); |
b56567b5 | 4568 | |
570d7624 | 4569 | filename = Fexpand_file_name (filename, Qnil); |
de1d0127 | 4570 | |
236a12f2 | 4571 | if (!NILP (mustbenew) && !EQ (mustbenew, Qexcl)) |
b8b29dc9 | 4572 | barf_or_query_if_file_exists (filename, "overwrite", 1, 0, 1); |
de1d0127 | 4573 | |
561cb8e1 | 4574 | if (STRINGP (visit)) |
e5176bae | 4575 | visit_file = Fexpand_file_name (visit, Qnil); |
4ad827c5 RS |
4576 | else |
4577 | visit_file = filename; | |
4578 | ||
7204a979 RS |
4579 | if (NILP (lockname)) |
4580 | lockname = visit_file; | |
4581 | ||
65b7d3e7 RS |
4582 | annotations = Qnil; |
4583 | ||
32f4334d RS |
4584 | /* If the file name has special constructs in it, |
4585 | call the corresponding file handler. */ | |
49307295 | 4586 | handler = Ffind_file_name_handler (filename, Qwrite_region); |
b56ad927 | 4587 | /* If FILENAME has no handler, see if VISIT has one. */ |
93c30b5f | 4588 | if (NILP (handler) && STRINGP (visit)) |
199607e4 | 4589 | handler = Ffind_file_name_handler (visit, Qwrite_region); |
3eac9910 | 4590 | |
32f4334d RS |
4591 | if (!NILP (handler)) |
4592 | { | |
32f4334d | 4593 | Lisp_Object val; |
51cf6d37 RS |
4594 | val = call6 (handler, Qwrite_region, start, end, |
4595 | filename, append, visit); | |
32f4334d | 4596 | |
d6a3cc15 | 4597 | if (visiting) |
32f4334d | 4598 | { |
95385625 | 4599 | SAVE_MODIFF = MODIFF; |
4b4deea2 | 4600 | XSETFASTINT (BVAR (current_buffer, save_length), Z - BEG); |
39eb03f1 | 4601 | bset_filename (current_buffer, visit_file); |
32f4334d | 4602 | } |
09121adc | 4603 | UNGCPRO; |
32f4334d RS |
4604 | return val; |
4605 | } | |
4606 | ||
4a38de71 KH |
4607 | record_unwind_protect (save_restriction_restore, save_restriction_save ()); |
4608 | ||
561cb8e1 RS |
4609 | /* Special kludge to simplify auto-saving. */ |
4610 | if (NILP (start)) | |
4611 | { | |
6b3d752c SM |
4612 | /* Do it later, so write-region-annotate-function can work differently |
4613 | if we save "the buffer" vs "a region". | |
4614 | This is useful in tar-mode. --Stef | |
2acfd7ae | 4615 | XSETFASTINT (start, BEG); |
6b3d752c | 4616 | XSETFASTINT (end, Z); */ |
4a38de71 | 4617 | Fwiden (); |
561cb8e1 RS |
4618 | } |
4619 | ||
67fbc0cb CY |
4620 | record_unwind_protect (build_annotations_unwind, |
4621 | Vwrite_region_annotation_buffers); | |
4622 | Vwrite_region_annotation_buffers = Fcons (Fcurrent_buffer (), Qnil); | |
aed13378 | 4623 | count1 = SPECPDL_INDEX (); |
6fc6f94b RS |
4624 | |
4625 | given_buffer = current_buffer; | |
bf3428a1 RS |
4626 | |
4627 | if (!STRINGP (start)) | |
236a12f2 | 4628 | { |
bf3428a1 RS |
4629 | annotations = build_annotations (start, end); |
4630 | ||
4631 | if (current_buffer != given_buffer) | |
4632 | { | |
4633 | XSETFASTINT (start, BEGV); | |
4634 | XSETFASTINT (end, ZV); | |
4635 | } | |
236a12f2 SM |
4636 | } |
4637 | ||
6b3d752c SM |
4638 | if (NILP (start)) |
4639 | { | |
4640 | XSETFASTINT (start, BEGV); | |
4641 | XSETFASTINT (end, ZV); | |
4642 | } | |
4643 | ||
236a12f2 SM |
4644 | UNGCPRO; |
4645 | ||
4646 | GCPRO5 (start, filename, annotations, visit_file, lockname); | |
4647 | ||
59fac292 SM |
4648 | /* Decide the coding-system to encode the data with. |
4649 | We used to make this choice before calling build_annotations, but that | |
4650 | leads to problems when a write-annotate-function takes care of | |
4651 | unsavable chars (as was the case with X-Symbol). */ | |
c934586d KH |
4652 | Vlast_coding_system_used |
4653 | = choose_write_coding_system (start, end, filename, | |
4654 | append, visit, lockname, &coding); | |
d6a3cc15 | 4655 | |
570d7624 JB |
4656 | #ifdef CLASH_DETECTION |
4657 | if (!auto_saving) | |
67fbc0cb | 4658 | lock_file (lockname); |
570d7624 JB |
4659 | #endif /* CLASH_DETECTION */ |
4660 | ||
b1d1b865 RS |
4661 | encoded_filename = ENCODE_FILE (filename); |
4662 | ||
5976c3fe | 4663 | fn = SSDATA (encoded_filename); |
570d7624 | 4664 | desc = -1; |
265a9e55 | 4665 | if (!NILP (append)) |
5e570b75 | 4666 | #ifdef DOS_NT |
05c65251 | 4667 | desc = emacs_open (fn, O_WRONLY | O_BINARY, 0); |
5e570b75 | 4668 | #else /* not DOS_NT */ |
68c45bf0 | 4669 | desc = emacs_open (fn, O_WRONLY, 0); |
5e570b75 | 4670 | #endif /* not DOS_NT */ |
570d7624 | 4671 | |
b1d1b865 | 4672 | if (desc < 0 && (NILP (append) || errno == ENOENT)) |
5e570b75 | 4673 | #ifdef DOS_NT |
68c45bf0 | 4674 | desc = emacs_open (fn, |
05c65251 | 4675 | O_WRONLY | O_CREAT | O_BINARY |
95522746 | 4676 | | (EQ (mustbenew, Qexcl) ? O_EXCL : O_TRUNC), |
68c45bf0 | 4677 | S_IREAD | S_IWRITE); |
5e570b75 | 4678 | #else /* not DOS_NT */ |
68c45bf0 | 4679 | desc = emacs_open (fn, O_WRONLY | O_TRUNC | O_CREAT |
7c752c80 | 4680 | | (EQ (mustbenew, Qexcl) ? O_EXCL : 0), |
68c45bf0 | 4681 | auto_saving ? auto_save_mode_bits : 0666); |
5e570b75 | 4682 | #endif /* not DOS_NT */ |
570d7624 JB |
4683 | |
4684 | if (desc < 0) | |
4685 | { | |
4686 | #ifdef CLASH_DETECTION | |
4687 | save_errno = errno; | |
7204a979 | 4688 | if (!auto_saving) unlock_file (lockname); |
570d7624 JB |
4689 | errno = save_errno; |
4690 | #endif /* CLASH_DETECTION */ | |
43fb7d9a | 4691 | UNGCPRO; |
570d7624 JB |
4692 | report_file_error ("Opening output file", Fcons (filename, Qnil)); |
4693 | } | |
4694 | ||
4695 | record_unwind_protect (close_file_unwind, make_number (desc)); | |
4696 | ||
c1c4693e | 4697 | if (!NILP (append) && !NILP (Ffile_regular_p (filename))) |
43fb7d9a | 4698 | { |
728f8f0a | 4699 | off_t ret; |
efdc16c9 | 4700 | |
43fb7d9a | 4701 | if (NUMBERP (append)) |
e69dafad | 4702 | ret = emacs_lseek (desc, XINT (append), SEEK_CUR); |
43fb7d9a | 4703 | else |
e69dafad | 4704 | ret = lseek (desc, 0, SEEK_END); |
43fb7d9a DL |
4705 | if (ret < 0) |
4706 | { | |
570d7624 | 4707 | #ifdef CLASH_DETECTION |
e5e9d610 | 4708 | save_errno = errno; |
43fb7d9a | 4709 | if (!auto_saving) unlock_file (lockname); |
e5e9d610 | 4710 | errno = save_errno; |
570d7624 | 4711 | #endif /* CLASH_DETECTION */ |
43fb7d9a DL |
4712 | UNGCPRO; |
4713 | report_file_error ("Lseek error", Fcons (filename, Qnil)); | |
4714 | } | |
4715 | } | |
efdc16c9 | 4716 | |
43fb7d9a | 4717 | UNGCPRO; |
570d7624 | 4718 | |
570d7624 JB |
4719 | immediate_quit = 1; |
4720 | ||
561cb8e1 | 4721 | if (STRINGP (start)) |
f75d7a91 | 4722 | ok = a_write (desc, start, 0, SCHARS (start), &annotations, &coding); |
570d7624 | 4723 | else if (XINT (start) != XINT (end)) |
f75d7a91 PE |
4724 | ok = a_write (desc, Qnil, XINT (start), XINT (end) - XINT (start), |
4725 | &annotations, &coding); | |
69f6e679 RS |
4726 | else |
4727 | { | |
f75d7a91 | 4728 | /* If file was empty, still need to write the annotations. */ |
c8a6d68a | 4729 | coding.mode |= CODING_MODE_LAST_BLOCK; |
f75d7a91 | 4730 | ok = a_write (desc, Qnil, XINT (end), 0, &annotations, &coding); |
6fdaa9a0 | 4731 | } |
f75d7a91 | 4732 | save_errno = errno; |
6fdaa9a0 | 4733 | |
f75d7a91 PE |
4734 | if (ok && CODING_REQUIRE_FLUSHING (&coding) |
4735 | && !(coding.mode & CODING_MODE_LAST_BLOCK)) | |
6fdaa9a0 KH |
4736 | { |
4737 | /* We have to flush out a data. */ | |
c8a6d68a | 4738 | coding.mode |= CODING_MODE_LAST_BLOCK; |
f75d7a91 | 4739 | ok = e_write (desc, Qnil, 1, 1, &coding); |
69f6e679 | 4740 | save_errno = errno; |
570d7624 JB |
4741 | } |
4742 | ||
4743 | immediate_quit = 0; | |
4744 | ||
6e23c83e | 4745 | #ifdef HAVE_FSYNC |
570d7624 JB |
4746 | /* Note fsync appears to change the modtime on BSD4.2 (both vax and sun). |
4747 | Disk full in NFS may be reported here. */ | |
1daffa1c RS |
4748 | /* mib says that closing the file will try to write as fast as NFS can do |
4749 | it, and that means the fsync here is not crucial for autosave files. */ | |
ccf61795 | 4750 | if (!auto_saving && !write_region_inhibit_fsync && fsync (desc) < 0) |
cb33c142 | 4751 | { |
6cff77fd AS |
4752 | /* If fsync fails with EINTR, don't treat that as serious. Also |
4753 | ignore EINVAL which happens when fsync is not supported on this | |
4754 | file. */ | |
4755 | if (errno != EINTR && errno != EINVAL) | |
f75d7a91 | 4756 | ok = 0, save_errno = errno; |
cb33c142 | 4757 | } |
570d7624 JB |
4758 | #endif |
4759 | ||
570d7624 | 4760 | /* NFS can report a write failure now. */ |
68c45bf0 | 4761 | if (emacs_close (desc) < 0) |
f75d7a91 | 4762 | ok = 0, save_errno = errno; |
570d7624 | 4763 | |
570d7624 | 4764 | stat (fn, &st); |
67fbc0cb | 4765 | |
6fc6f94b RS |
4766 | /* Discard the unwind protect for close_file_unwind. */ |
4767 | specpdl_ptr = specpdl + count1; | |
67fbc0cb CY |
4768 | |
4769 | /* Call write-region-post-annotation-function. */ | |
294fa707 | 4770 | while (CONSP (Vwrite_region_annotation_buffers)) |
67fbc0cb CY |
4771 | { |
4772 | Lisp_Object buf = XCAR (Vwrite_region_annotation_buffers); | |
4773 | if (!NILP (Fbuffer_live_p (buf))) | |
4774 | { | |
4775 | Fset_buffer (buf); | |
4776 | if (FUNCTIONP (Vwrite_region_post_annotation_function)) | |
4777 | call0 (Vwrite_region_post_annotation_function); | |
4778 | } | |
4779 | Vwrite_region_annotation_buffers | |
4780 | = XCDR (Vwrite_region_annotation_buffers); | |
4781 | } | |
4782 | ||
4783 | unbind_to (count, Qnil); | |
570d7624 JB |
4784 | |
4785 | #ifdef CLASH_DETECTION | |
4786 | if (!auto_saving) | |
7204a979 | 4787 | unlock_file (lockname); |
570d7624 JB |
4788 | #endif /* CLASH_DETECTION */ |
4789 | ||
4790 | /* Do this before reporting IO error | |
4791 | to avoid a "file has changed on disk" warning on | |
4792 | next attempt to save. */ | |
d6a3cc15 | 4793 | if (visiting) |
58b963f7 | 4794 | { |
d35af63c | 4795 | current_buffer->modtime = get_stat_mtime (&st); |
58b963f7 SM |
4796 | current_buffer->modtime_size = st.st_size; |
4797 | } | |
570d7624 | 4798 | |
f75d7a91 | 4799 | if (! ok) |
d5db4077 | 4800 | error ("IO error writing %s: %s", SDATA (filename), |
68c45bf0 | 4801 | emacs_strerror (save_errno)); |
570d7624 | 4802 | |
d6a3cc15 | 4803 | if (visiting) |
570d7624 | 4804 | { |
95385625 | 4805 | SAVE_MODIFF = MODIFF; |
4b4deea2 | 4806 | XSETFASTINT (BVAR (current_buffer, save_length), Z - BEG); |
39eb03f1 | 4807 | bset_filename (current_buffer, visit_file); |
f4226e89 | 4808 | update_mode_lines++; |
570d7624 | 4809 | } |
d6a3cc15 | 4810 | else if (quietly) |
6b61353c KH |
4811 | { |
4812 | if (auto_saving | |
4b4deea2 TT |
4813 | && ! NILP (Fstring_equal (BVAR (current_buffer, filename), |
4814 | BVAR (current_buffer, auto_save_file_name)))) | |
6b61353c KH |
4815 | SAVE_MODIFF = MODIFF; |
4816 | ||
4817 | return Qnil; | |
4818 | } | |
570d7624 JB |
4819 | |
4820 | if (!auto_saving) | |
6b61353c | 4821 | message_with_string ((INTEGERP (append) |
0c328a0e RS |
4822 | ? "Updated %s" |
4823 | : ! NILP (append) | |
4824 | ? "Added to %s" | |
4825 | : "Wrote %s"), | |
4826 | visit_file, 1); | |
570d7624 JB |
4827 | |
4828 | return Qnil; | |
4829 | } | |
ec7adf26 | 4830 | \f |
dd4c5104 | 4831 | Lisp_Object merge (Lisp_Object, Lisp_Object, Lisp_Object); |
d6a3cc15 RS |
4832 | |
4833 | DEFUN ("car-less-than-car", Fcar_less_than_car, Scar_less_than_car, 2, 2, 0, | |
8c1a1077 | 4834 | doc: /* Return t if (car A) is numerically less than (car B). */) |
5842a27b | 4835 | (Lisp_Object a, Lisp_Object b) |
d6a3cc15 RS |
4836 | { |
4837 | return Flss (Fcar (a), Fcar (b)); | |
4838 | } | |
4839 | ||
4840 | /* Build the complete list of annotations appropriate for writing out | |
4841 | the text between START and END, by calling all the functions in | |
6fc6f94b RS |
4842 | write-region-annotate-functions and merging the lists they return. |
4843 | If one of these functions switches to a different buffer, we assume | |
4844 | that buffer contains altered text. Therefore, the caller must | |
4845 | make sure to restore the current buffer in all cases, | |
4846 | as save-excursion would do. */ | |
d6a3cc15 RS |
4847 | |
4848 | static Lisp_Object | |
971de7fb | 4849 | build_annotations (Lisp_Object start, Lisp_Object end) |
d6a3cc15 RS |
4850 | { |
4851 | Lisp_Object annotations; | |
4852 | Lisp_Object p, res; | |
4853 | struct gcpro gcpro1, gcpro2; | |
0a20b684 | 4854 | Lisp_Object original_buffer; |
f75d7a91 PE |
4855 | int i; |
4856 | bool used_global = 0; | |
0a20b684 RS |
4857 | |
4858 | XSETBUFFER (original_buffer, current_buffer); | |
d6a3cc15 RS |
4859 | |
4860 | annotations = Qnil; | |
4861 | p = Vwrite_region_annotate_functions; | |
4862 | GCPRO2 (annotations, p); | |
28c3eb5a | 4863 | while (CONSP (p)) |
d6a3cc15 | 4864 | { |
6fc6f94b | 4865 | struct buffer *given_buffer = current_buffer; |
bd235610 SM |
4866 | if (EQ (Qt, XCAR (p)) && !used_global) |
4867 | { /* Use the global value of the hook. */ | |
4868 | Lisp_Object arg[2]; | |
4869 | used_global = 1; | |
4870 | arg[0] = Fdefault_value (Qwrite_region_annotate_functions); | |
4871 | arg[1] = XCDR (p); | |
4872 | p = Fappend (2, arg); | |
4873 | continue; | |
4874 | } | |
6fc6f94b | 4875 | Vwrite_region_annotations_so_far = annotations; |
28c3eb5a | 4876 | res = call2 (XCAR (p), start, end); |
6fc6f94b RS |
4877 | /* If the function makes a different buffer current, |
4878 | assume that means this buffer contains altered text to be output. | |
4879 | Reset START and END from the buffer bounds | |
4880 | and discard all previous annotations because they should have | |
4881 | been dealt with by this function. */ | |
4882 | if (current_buffer != given_buffer) | |
4883 | { | |
67fbc0cb CY |
4884 | Vwrite_region_annotation_buffers |
4885 | = Fcons (Fcurrent_buffer (), | |
4886 | Vwrite_region_annotation_buffers); | |
3cf29f61 RS |
4887 | XSETFASTINT (start, BEGV); |
4888 | XSETFASTINT (end, ZV); | |
6fc6f94b RS |
4889 | annotations = Qnil; |
4890 | } | |
d6a3cc15 RS |
4891 | Flength (res); /* Check basic validity of return value */ |
4892 | annotations = merge (annotations, res, Qcar_less_than_car); | |
28c3eb5a | 4893 | p = XCDR (p); |
d6a3cc15 | 4894 | } |
0d420e88 BG |
4895 | |
4896 | /* Now do the same for annotation functions implied by the file-format */ | |
4b4deea2 TT |
4897 | if (auto_saving && (!EQ (BVAR (current_buffer, auto_save_file_format), Qt))) |
4898 | p = BVAR (current_buffer, auto_save_file_format); | |
0d420e88 | 4899 | else |
4b4deea2 | 4900 | p = BVAR (current_buffer, file_format); |
28c3eb5a | 4901 | for (i = 0; CONSP (p); p = XCDR (p), ++i) |
0d420e88 BG |
4902 | { |
4903 | struct buffer *given_buffer = current_buffer; | |
efdc16c9 | 4904 | |
0d420e88 | 4905 | Vwrite_region_annotations_so_far = annotations; |
532ed661 GM |
4906 | |
4907 | /* Value is either a list of annotations or nil if the function | |
4908 | has written annotations to a temporary buffer, which is now | |
4909 | current. */ | |
28c3eb5a | 4910 | res = call5 (Qformat_annotate_function, XCAR (p), start, end, |
532ed661 | 4911 | original_buffer, make_number (i)); |
0d420e88 BG |
4912 | if (current_buffer != given_buffer) |
4913 | { | |
3cf29f61 RS |
4914 | XSETFASTINT (start, BEGV); |
4915 | XSETFASTINT (end, ZV); | |
0d420e88 BG |
4916 | annotations = Qnil; |
4917 | } | |
efdc16c9 | 4918 | |
532ed661 GM |
4919 | if (CONSP (res)) |
4920 | annotations = merge (annotations, res, Qcar_less_than_car); | |
0d420e88 | 4921 | } |
6fdaa9a0 | 4922 | |
236a12f2 SM |
4923 | UNGCPRO; |
4924 | return annotations; | |
4925 | } | |
4926 | ||
ec7adf26 | 4927 | \f |
ce51c54c KH |
4928 | /* Write to descriptor DESC the NCHARS chars starting at POS of STRING. |
4929 | If STRING is nil, POS is the character position in the current buffer. | |
d6a3cc15 | 4930 | Intersperse with them the annotations from *ANNOT |
ce51c54c | 4931 | which fall within the range of POS to POS + NCHARS, |
d6a3cc15 RS |
4932 | each at its appropriate position. |
4933 | ||
ec7adf26 RS |
4934 | We modify *ANNOT by discarding elements as we use them up. |
4935 | ||
f75d7a91 | 4936 | Return true if successful. */ |
d6a3cc15 | 4937 | |
f75d7a91 | 4938 | static bool |
d311d28c | 4939 | a_write (int desc, Lisp_Object string, ptrdiff_t pos, |
f75d7a91 | 4940 | ptrdiff_t nchars, Lisp_Object *annot, |
8a2cbd72 | 4941 | struct coding_system *coding) |
d6a3cc15 RS |
4942 | { |
4943 | Lisp_Object tem; | |
d311d28c PE |
4944 | ptrdiff_t nextpos; |
4945 | ptrdiff_t lastpos = pos + nchars; | |
d6a3cc15 | 4946 | |
eb15aa18 | 4947 | while (NILP (*annot) || CONSP (*annot)) |
d6a3cc15 RS |
4948 | { |
4949 | tem = Fcar_safe (Fcar (*annot)); | |
ce51c54c | 4950 | nextpos = pos - 1; |
ec7adf26 | 4951 | if (INTEGERP (tem)) |
ce51c54c | 4952 | nextpos = XFASTINT (tem); |
ec7adf26 RS |
4953 | |
4954 | /* If there are no more annotations in this range, | |
4955 | output the rest of the range all at once. */ | |
ce51c54c KH |
4956 | if (! (nextpos >= pos && nextpos <= lastpos)) |
4957 | return e_write (desc, string, pos, lastpos, coding); | |
ec7adf26 RS |
4958 | |
4959 | /* Output buffer text up to the next annotation's position. */ | |
ce51c54c | 4960 | if (nextpos > pos) |
d6a3cc15 | 4961 | { |
f75d7a91 PE |
4962 | if (!e_write (desc, string, pos, nextpos, coding)) |
4963 | return 0; | |
ce51c54c | 4964 | pos = nextpos; |
d6a3cc15 | 4965 | } |
ec7adf26 | 4966 | /* Output the annotation. */ |
d6a3cc15 RS |
4967 | tem = Fcdr (Fcar (*annot)); |
4968 | if (STRINGP (tem)) | |
4969 | { | |
f75d7a91 PE |
4970 | if (!e_write (desc, tem, 0, SCHARS (tem), coding)) |
4971 | return 0; | |
d6a3cc15 RS |
4972 | } |
4973 | *annot = Fcdr (*annot); | |
4974 | } | |
f75d7a91 | 4975 | return 1; |
d6a3cc15 RS |
4976 | } |
4977 | ||
6fdaa9a0 | 4978 | |
ce51c54c KH |
4979 | /* Write text in the range START and END into descriptor DESC, |
4980 | encoding them with coding system CODING. If STRING is nil, START | |
4981 | and END are character positions of the current buffer, else they | |
f75d7a91 | 4982 | are indexes to the string STRING. Return true if successful. */ |
ec7adf26 | 4983 | |
f75d7a91 | 4984 | static bool |
d311d28c | 4985 | e_write (int desc, Lisp_Object string, ptrdiff_t start, ptrdiff_t end, |
8a2cbd72 | 4986 | struct coding_system *coding) |
570d7624 | 4987 | { |
ce51c54c KH |
4988 | if (STRINGP (string)) |
4989 | { | |
db327c7e | 4990 | start = 0; |
8f924df7 | 4991 | end = SCHARS (string); |
ce51c54c | 4992 | } |
570d7624 | 4993 | |
6fdaa9a0 KH |
4994 | /* We used to have a code for handling selective display here. But, |
4995 | now it is handled within encode_coding. */ | |
01ca97a2 KH |
4996 | |
4997 | while (start < end) | |
570d7624 | 4998 | { |
01ca97a2 | 4999 | if (STRINGP (string)) |
6ad568dd | 5000 | { |
01ca97a2 KH |
5001 | coding->src_multibyte = SCHARS (string) < SBYTES (string); |
5002 | if (CODING_REQUIRE_ENCODING (coding)) | |
5003 | { | |
5004 | encode_coding_object (coding, string, | |
5005 | start, string_char_to_byte (string, start), | |
5006 | end, string_char_to_byte (string, end), Qt); | |
5007 | } | |
5008 | else | |
5009 | { | |
5010 | coding->dst_object = string; | |
5011 | coding->consumed_char = SCHARS (string); | |
5012 | coding->produced = SBYTES (string); | |
5013 | } | |
6ad568dd | 5014 | } |
db327c7e | 5015 | else |
6ad568dd | 5016 | { |
d311d28c PE |
5017 | ptrdiff_t start_byte = CHAR_TO_BYTE (start); |
5018 | ptrdiff_t end_byte = CHAR_TO_BYTE (end); | |
b4132433 | 5019 | |
01ca97a2 KH |
5020 | coding->src_multibyte = (end - start) < (end_byte - start_byte); |
5021 | if (CODING_REQUIRE_ENCODING (coding)) | |
5022 | { | |
5023 | encode_coding_object (coding, Fcurrent_buffer (), | |
5024 | start, start_byte, end, end_byte, Qt); | |
5025 | } | |
5026 | else | |
5027 | { | |
5028 | coding->dst_object = Qnil; | |
5029 | coding->dst_pos_byte = start_byte; | |
5030 | if (start >= GPT || end <= GPT) | |
5031 | { | |
5032 | coding->consumed_char = end - start; | |
5033 | coding->produced = end_byte - start_byte; | |
5034 | } | |
5035 | else | |
5036 | { | |
5037 | coding->consumed_char = GPT - start; | |
5038 | coding->produced = GPT_BYTE - start_byte; | |
5039 | } | |
5040 | } | |
c185d744 | 5041 | } |
01ca97a2 KH |
5042 | |
5043 | if (coding->produced > 0) | |
c185d744 | 5044 | { |
01ca97a2 KH |
5045 | coding->produced -= |
5046 | emacs_write (desc, | |
5047 | STRINGP (coding->dst_object) | |
42a5b22f PE |
5048 | ? SSDATA (coding->dst_object) |
5049 | : (char *) BYTE_POS_ADDR (coding->dst_pos_byte), | |
01ca97a2 KH |
5050 | coding->produced); |
5051 | ||
5052 | if (coding->produced) | |
f75d7a91 | 5053 | return 0; |
570d7624 | 5054 | } |
01ca97a2 | 5055 | start += coding->consumed_char; |
c185d744 KH |
5056 | } |
5057 | ||
f75d7a91 | 5058 | return 1; |
570d7624 | 5059 | } |
ec7adf26 | 5060 | \f |
a7ca3326 | 5061 | DEFUN ("verify-visited-file-modtime", Fverify_visited_file_modtime, |
ec1b9b17 | 5062 | Sverify_visited_file_modtime, 0, 1, 0, |
8c1a1077 | 5063 | doc: /* Return t if last mod time of BUF's visited file matches what BUF records. |
6b61353c | 5064 | This means that the file has not been changed since it was visited or saved. |
ec1b9b17 | 5065 | If BUF is omitted or nil, it defaults to the current buffer. |
6b61353c | 5066 | See Info node `(elisp)Modification Time' for more details. */) |
5842a27b | 5067 | (Lisp_Object buf) |
570d7624 JB |
5068 | { |
5069 | struct buffer *b; | |
5070 | struct stat st; | |
32f4334d | 5071 | Lisp_Object handler; |
b1d1b865 | 5072 | Lisp_Object filename; |
e9a9ae03 | 5073 | EMACS_TIME mtime, diff; |
570d7624 | 5074 | |
ec1b9b17 GM |
5075 | if (NILP (buf)) |
5076 | b = current_buffer; | |
5077 | else | |
5078 | { | |
5079 | CHECK_BUFFER (buf); | |
5080 | b = XBUFFER (buf); | |
5081 | } | |
570d7624 | 5082 | |
4b4deea2 | 5083 | if (!STRINGP (BVAR (b, filename))) return Qt; |
d35af63c | 5084 | if (EMACS_NSECS (b->modtime) == UNKNOWN_MODTIME_NSECS) return Qt; |
570d7624 | 5085 | |
32f4334d RS |
5086 | /* If the file name has special constructs in it, |
5087 | call the corresponding file handler. */ | |
4b4deea2 | 5088 | handler = Ffind_file_name_handler (BVAR (b, filename), |
49307295 | 5089 | Qverify_visited_file_modtime); |
32f4334d | 5090 | if (!NILP (handler)) |
09121adc | 5091 | return call2 (handler, Qverify_visited_file_modtime, buf); |
32f4334d | 5092 | |
4b4deea2 | 5093 | filename = ENCODE_FILE (BVAR (b, filename)); |
b1d1b865 | 5094 | |
dbeed9a6 PE |
5095 | mtime = (stat (SSDATA (filename), &st) == 0 |
5096 | ? get_stat_mtime (&st) | |
f0941253 | 5097 | : time_error_value (errno)); |
d35af63c PE |
5098 | if ((EMACS_TIME_EQ (mtime, b->modtime) |
5099 | /* If both exist, accept them if they are off by one second. */ | |
5100 | || (EMACS_TIME_VALID_P (mtime) && EMACS_TIME_VALID_P (b->modtime) | |
e9a9ae03 PE |
5101 | && ((diff = (EMACS_TIME_LT (mtime, b->modtime) |
5102 | ? sub_emacs_time (b->modtime, mtime) | |
5103 | : sub_emacs_time (mtime, b->modtime))), | |
5104 | EMACS_TIME_LE (diff, make_emacs_time (1, 0))))) | |
58b963f7 SM |
5105 | && (st.st_size == b->modtime_size |
5106 | || b->modtime_size < 0)) | |
570d7624 JB |
5107 | return Qt; |
5108 | return Qnil; | |
5109 | } | |
5110 | ||
5111 | DEFUN ("clear-visited-file-modtime", Fclear_visited_file_modtime, | |
8c1a1077 PJ |
5112 | Sclear_visited_file_modtime, 0, 0, 0, |
5113 | doc: /* Clear out records of last mod time of visited file. | |
5114 | Next attempt to save will certainly not complain of a discrepancy. */) | |
5842a27b | 5115 | (void) |
570d7624 | 5116 | { |
e9a9ae03 | 5117 | current_buffer->modtime = make_emacs_time (0, UNKNOWN_MODTIME_NSECS); |
58b963f7 | 5118 | current_buffer->modtime_size = -1; |
570d7624 JB |
5119 | return Qnil; |
5120 | } | |
5121 | ||
f5d5eccf | 5122 | DEFUN ("visited-file-modtime", Fvisited_file_modtime, |
8c1a1077 PJ |
5123 | Svisited_file_modtime, 0, 0, 0, |
5124 | doc: /* Return the current buffer's recorded visited file modification time. | |
d35af63c | 5125 | The value is a list of the form (HIGH LOW USEC PSEC), like the time values that |
1c4d7f3d LMI |
5126 | `file-attributes' returns. If the current buffer has no recorded file |
5127 | modification time, this function returns 0. If the visited file | |
9d1d6e68 | 5128 | doesn't exist, HIGH will be -1. |
6b61353c | 5129 | See Info node `(elisp)Modification Time' for more details. */) |
5842a27b | 5130 | (void) |
f5d5eccf | 5131 | { |
d35af63c | 5132 | if (EMACS_NSECS (current_buffer->modtime) < 0) |
73ff9d42 | 5133 | return make_number (0); |
d35af63c | 5134 | return make_lisp_time (current_buffer->modtime); |
f5d5eccf RS |
5135 | } |
5136 | ||
570d7624 | 5137 | DEFUN ("set-visited-file-modtime", Fset_visited_file_modtime, |
8c1a1077 PJ |
5138 | Sset_visited_file_modtime, 0, 1, 0, |
5139 | doc: /* Update buffer's recorded modification time from the visited file's time. | |
5140 | Useful if the buffer was not read from the file normally | |
5141 | or if the file itself has been changed for some known benign reason. | |
5142 | An argument specifies the modification time value to use | |
5143 | \(instead of that of the visited file), in the form of a list | |
d35af63c | 5144 | \(HIGH LOW USEC PSEC) as returned by `current-time'. */) |
5842a27b | 5145 | (Lisp_Object time_list) |
570d7624 | 5146 | { |
f5d5eccf | 5147 | if (!NILP (time_list)) |
58b963f7 | 5148 | { |
31571fd7 | 5149 | current_buffer->modtime = lisp_time_argument (time_list); |
58b963f7 SM |
5150 | current_buffer->modtime_size = -1; |
5151 | } | |
f5d5eccf RS |
5152 | else |
5153 | { | |
5154 | register Lisp_Object filename; | |
5155 | struct stat st; | |
5156 | Lisp_Object handler; | |
570d7624 | 5157 | |
4b4deea2 | 5158 | filename = Fexpand_file_name (BVAR (current_buffer, filename), Qnil); |
32f4334d | 5159 | |
f5d5eccf RS |
5160 | /* If the file name has special constructs in it, |
5161 | call the corresponding file handler. */ | |
49307295 | 5162 | handler = Ffind_file_name_handler (filename, Qset_visited_file_modtime); |
f5d5eccf | 5163 | if (!NILP (handler)) |
caf3c431 | 5164 | /* The handler can find the file name the same way we did. */ |
76c881b0 | 5165 | return call2 (handler, Qset_visited_file_modtime, Qnil); |
b1d1b865 RS |
5166 | |
5167 | filename = ENCODE_FILE (filename); | |
5168 | ||
42a5b22f | 5169 | if (stat (SSDATA (filename), &st) >= 0) |
58b963f7 | 5170 | { |
d35af63c | 5171 | current_buffer->modtime = get_stat_mtime (&st); |
58b963f7 SM |
5172 | current_buffer->modtime_size = st.st_size; |
5173 | } | |
f5d5eccf | 5174 | } |
570d7624 JB |
5175 | |
5176 | return Qnil; | |
5177 | } | |
5178 | \f | |
f14b7e14 | 5179 | static Lisp_Object |
f839df0c | 5180 | auto_save_error (Lisp_Object error_val) |
570d7624 | 5181 | { |
d7f31e22 GM |
5182 | Lisp_Object args[3], msg; |
5183 | int i, nbytes; | |
5184 | struct gcpro gcpro1; | |
dfc22242 KS |
5185 | char *msgbuf; |
5186 | USE_SAFE_ALLOCA; | |
efdc16c9 | 5187 | |
ca730bf0 CY |
5188 | auto_save_error_occurred = 1; |
5189 | ||
385ed61f | 5190 | ring_bell (XFRAME (selected_frame)); |
efdc16c9 | 5191 | |
d7f31e22 | 5192 | args[0] = build_string ("Auto-saving %s: %s"); |
4b4deea2 | 5193 | args[1] = BVAR (current_buffer, name); |
f839df0c | 5194 | args[2] = Ferror_message_string (error_val); |
d7f31e22 GM |
5195 | msg = Fformat (3, args); |
5196 | GCPRO1 (msg); | |
d5db4077 | 5197 | nbytes = SBYTES (msg); |
98c6f1e3 | 5198 | msgbuf = SAFE_ALLOCA (nbytes); |
72af86bd | 5199 | memcpy (msgbuf, SDATA (msg), nbytes); |
d7f31e22 GM |
5200 | |
5201 | for (i = 0; i < 3; ++i) | |
5202 | { | |
5203 | if (i == 0) | |
dfc22242 | 5204 | message2 (msgbuf, nbytes, STRING_MULTIBYTE (msg)); |
d7f31e22 | 5205 | else |
dfc22242 | 5206 | message2_nolog (msgbuf, nbytes, STRING_MULTIBYTE (msg)); |
d7f31e22 GM |
5207 | Fsleep_for (make_number (1), Qnil); |
5208 | } | |
5209 | ||
e01f7773 | 5210 | SAFE_FREE (); |
d7f31e22 | 5211 | UNGCPRO; |
570d7624 JB |
5212 | return Qnil; |
5213 | } | |
5214 | ||
f14b7e14 | 5215 | static Lisp_Object |
971de7fb | 5216 | auto_save_1 (void) |
570d7624 | 5217 | { |
570d7624 | 5218 | struct stat st; |
d4a42098 KS |
5219 | Lisp_Object modes; |
5220 | ||
5221 | auto_save_mode_bits = 0666; | |
570d7624 JB |
5222 | |
5223 | /* Get visited file's mode to become the auto save file's mode. */ | |
4b4deea2 | 5224 | if (! NILP (BVAR (current_buffer, filename))) |
d4a42098 | 5225 | { |
4b4deea2 | 5226 | if (stat (SSDATA (BVAR (current_buffer, filename)), &st) >= 0) |
d4a42098 | 5227 | /* But make sure we can overwrite it later! */ |
18c52557 | 5228 | auto_save_mode_bits = (st.st_mode | 0600) & 0777; |
4b4deea2 | 5229 | else if ((modes = Ffile_modes (BVAR (current_buffer, filename)), |
d4a42098 KS |
5230 | INTEGERP (modes))) |
5231 | /* Remote files don't cooperate with stat. */ | |
18c52557 | 5232 | auto_save_mode_bits = (XINT (modes) | 0600) & 0777; |
d4a42098 | 5233 | } |
570d7624 JB |
5234 | |
5235 | return | |
4b4deea2 | 5236 | Fwrite_region (Qnil, Qnil, BVAR (current_buffer, auto_save_file_name), Qnil, |
699b53bc CY |
5237 | NILP (Vauto_save_visited_file_name) ? Qlambda : Qt, |
5238 | Qnil, Qnil); | |
570d7624 JB |
5239 | } |
5240 | ||
e54d3b5d | 5241 | static Lisp_Object |
971de7fb | 5242 | do_auto_save_unwind (Lisp_Object arg) /* used as unwind-protect function */ |
410ed5c3 | 5243 | |
e54d3b5d | 5244 | { |
fff7e982 | 5245 | FILE *stream = (FILE *) XSAVE_VALUE (arg)->pointer; |
3be3c08e | 5246 | auto_saving = 0; |
fff7e982 | 5247 | if (stream != NULL) |
aab12958 | 5248 | { |
4d7e6e51 | 5249 | block_input (); |
aab12958 | 5250 | fclose (stream); |
4d7e6e51 | 5251 | unblock_input (); |
aab12958 | 5252 | } |
e54d3b5d RS |
5253 | return Qnil; |
5254 | } | |
5255 | ||
a8c828be | 5256 | static Lisp_Object |
971de7fb | 5257 | do_auto_save_unwind_1 (Lisp_Object value) /* used as unwind-protect function */ |
410ed5c3 | 5258 | |
a8c828be RS |
5259 | { |
5260 | minibuffer_auto_raise = XINT (value); | |
5261 | return Qnil; | |
5262 | } | |
5263 | ||
5794dd61 | 5264 | static Lisp_Object |
971de7fb | 5265 | do_auto_save_make_dir (Lisp_Object dir) |
5794dd61 | 5266 | { |
09450bae | 5267 | Lisp_Object result; |
26816cbf | 5268 | |
09450bae PE |
5269 | auto_saving_dir_umask = 077; |
5270 | result = call2 (Qmake_directory, dir, Qt); | |
5271 | auto_saving_dir_umask = 0; | |
5272 | return result; | |
5794dd61 RS |
5273 | } |
5274 | ||
5275 | static Lisp_Object | |
971de7fb | 5276 | do_auto_save_eh (Lisp_Object ignore) |
5794dd61 | 5277 | { |
09450bae | 5278 | auto_saving_dir_umask = 0; |
5794dd61 RS |
5279 | return Qnil; |
5280 | } | |
5281 | ||
a7ca3326 | 5282 | DEFUN ("do-auto-save", Fdo_auto_save, Sdo_auto_save, 0, 2, "", |
8c1a1077 PJ |
5283 | doc: /* Auto-save all buffers that need it. |
5284 | This is all buffers that have auto-saving enabled | |
5285 | and are changed since last auto-saved. | |
5286 | Auto-saving writes the buffer into a file | |
5287 | so that your editing is not lost if the system crashes. | |
5288 | This file is not the file you visited; that changes only when you save. | |
5289 | Normally we run the normal hook `auto-save-hook' before saving. | |
5290 | ||
5291 | A non-nil NO-MESSAGE argument means do not print any message if successful. | |
5292 | A non-nil CURRENT-ONLY argument means save only current buffer. */) | |
5842a27b | 5293 | (Lisp_Object no_message, Lisp_Object current_only) |
570d7624 JB |
5294 | { |
5295 | struct buffer *old = current_buffer, *b; | |
dee091a3 | 5296 | Lisp_Object tail, buf, hook; |
f75d7a91 | 5297 | bool auto_saved = 0; |
f14b1c68 | 5298 | int do_handled_files; |
ff4c9993 | 5299 | Lisp_Object oquit; |
fff7e982 | 5300 | FILE *stream = NULL; |
d311d28c | 5301 | ptrdiff_t count = SPECPDL_INDEX (); |
f75d7a91 | 5302 | bool orig_minibuffer_auto_raise = minibuffer_auto_raise; |
fce31d69 | 5303 | bool old_message_p = 0; |
d57563b6 | 5304 | struct gcpro gcpro1, gcpro2; |
38da540d RS |
5305 | |
5306 | if (max_specpdl_size < specpdl_size + 40) | |
5307 | max_specpdl_size = specpdl_size + 40; | |
5308 | ||
5309 | if (minibuf_level) | |
5310 | no_message = Qt; | |
5311 | ||
5794dd61 RS |
5312 | if (NILP (no_message)) |
5313 | { | |
5314 | old_message_p = push_message (); | |
5315 | record_unwind_protect (pop_message_unwind, Qnil); | |
5316 | } | |
efdc16c9 | 5317 | |
ff4c9993 RS |
5318 | /* Ordinarily don't quit within this function, |
5319 | but don't make it impossible to quit (in case we get hung in I/O). */ | |
5320 | oquit = Vquit_flag; | |
5321 | Vquit_flag = Qnil; | |
570d7624 JB |
5322 | |
5323 | /* No GCPRO needed, because (when it matters) all Lisp_Object variables | |
5324 | point to non-strings reached from Vbuffer_alist. */ | |
5325 | ||
dee091a3 JD |
5326 | hook = intern ("auto-save-hook"); |
5327 | Frun_hooks (1, &hook); | |
570d7624 | 5328 | |
e54d3b5d RS |
5329 | if (STRINGP (Vauto_save_list_file_name)) |
5330 | { | |
0894672f | 5331 | Lisp_Object listfile; |
efdc16c9 | 5332 | |
258fd2cb | 5333 | listfile = Fexpand_file_name (Vauto_save_list_file_name, Qnil); |
0894672f GM |
5334 | |
5335 | /* Don't try to create the directory when shutting down Emacs, | |
5336 | because creating the directory might signal an error, and | |
5337 | that would leave Emacs in a strange state. */ | |
5338 | if (!NILP (Vrun_hooks)) | |
5339 | { | |
5340 | Lisp_Object dir; | |
d57563b6 RS |
5341 | dir = Qnil; |
5342 | GCPRO2 (dir, listfile); | |
0894672f GM |
5343 | dir = Ffile_name_directory (listfile); |
5344 | if (NILP (Ffile_directory_p (dir))) | |
5794dd61 | 5345 | internal_condition_case_1 (do_auto_save_make_dir, |
09450bae | 5346 | dir, Qt, |
5794dd61 | 5347 | do_auto_save_eh); |
d57563b6 | 5348 | UNGCPRO; |
0894672f | 5349 | } |
efdc16c9 | 5350 | |
42a5b22f | 5351 | stream = fopen (SSDATA (listfile), "w"); |
1b335d29 | 5352 | } |
199607e4 | 5353 | |
fff7e982 KS |
5354 | record_unwind_protect (do_auto_save_unwind, |
5355 | make_save_value (stream, 0)); | |
a8c828be RS |
5356 | record_unwind_protect (do_auto_save_unwind_1, |
5357 | make_number (minibuffer_auto_raise)); | |
5358 | minibuffer_auto_raise = 0; | |
3be3c08e | 5359 | auto_saving = 1; |
ca730bf0 | 5360 | auto_save_error_occurred = 0; |
3be3c08e | 5361 | |
6b61353c KH |
5362 | /* On first pass, save all files that don't have handlers. |
5363 | On second pass, save all files that do have handlers. | |
5364 | ||
5365 | If Emacs is crashing, the handlers may tweak what is causing | |
5366 | Emacs to crash in the first place, and it would be a shame if | |
5367 | Emacs failed to autosave perfectly ordinary files because it | |
5368 | couldn't handle some ange-ftp'd file. */ | |
5369 | ||
f14b1c68 | 5370 | for (do_handled_files = 0; do_handled_files < 2; do_handled_files++) |
8e50cc2d | 5371 | for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail)) |
f14b1c68 | 5372 | { |
03699b14 | 5373 | buf = XCDR (XCAR (tail)); |
f14b1c68 | 5374 | b = XBUFFER (buf); |
199607e4 | 5375 | |
e54d3b5d | 5376 | /* Record all the buffers that have auto save mode |
258fd2cb RS |
5377 | in the special file that lists them. For each of these buffers, |
5378 | Record visited name (if any) and auto save name. */ | |
4b4deea2 | 5379 | if (STRINGP (BVAR (b, auto_save_file_name)) |
1b335d29 | 5380 | && stream != NULL && do_handled_files == 0) |
e54d3b5d | 5381 | { |
4d7e6e51 | 5382 | block_input (); |
4b4deea2 | 5383 | if (!NILP (BVAR (b, filename))) |
258fd2cb | 5384 | { |
4b4deea2 TT |
5385 | fwrite (SDATA (BVAR (b, filename)), 1, |
5386 | SBYTES (BVAR (b, filename)), stream); | |
258fd2cb | 5387 | } |
1b335d29 | 5388 | putc ('\n', stream); |
4b4deea2 TT |
5389 | fwrite (SDATA (BVAR (b, auto_save_file_name)), 1, |
5390 | SBYTES (BVAR (b, auto_save_file_name)), stream); | |
1b335d29 | 5391 | putc ('\n', stream); |
4d7e6e51 | 5392 | unblock_input (); |
e54d3b5d | 5393 | } |
17857782 | 5394 | |
f14b1c68 JB |
5395 | if (!NILP (current_only) |
5396 | && b != current_buffer) | |
5397 | continue; | |
e54d3b5d | 5398 | |
95385625 RS |
5399 | /* Don't auto-save indirect buffers. |
5400 | The base buffer takes care of it. */ | |
5401 | if (b->base_buffer) | |
5402 | continue; | |
5403 | ||
f14b1c68 JB |
5404 | /* Check for auto save enabled |
5405 | and file changed since last auto save | |
5406 | and file changed since last real save. */ | |
4b4deea2 | 5407 | if (STRINGP (BVAR (b, auto_save_file_name)) |
95385625 | 5408 | && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b) |
0b5397c2 | 5409 | && BUF_AUTOSAVE_MODIFF (b) < BUF_MODIFF (b) |
82c2d839 | 5410 | /* -1 means we've turned off autosaving for a while--see below. */ |
4b4deea2 | 5411 | && XINT (BVAR (b, save_length)) >= 0 |
f14b1c68 | 5412 | && (do_handled_files |
4b4deea2 | 5413 | || NILP (Ffind_file_name_handler (BVAR (b, auto_save_file_name), |
49307295 | 5414 | Qwrite_region)))) |
f14b1c68 | 5415 | { |
e9a9ae03 PE |
5416 | EMACS_TIME before_time = current_emacs_time (); |
5417 | EMACS_TIME after_time; | |
b60247d9 RS |
5418 | |
5419 | /* If we had a failure, don't try again for 20 minutes. */ | |
d311d28c | 5420 | if (b->auto_save_failure_time > 0 |
b60247d9 RS |
5421 | && EMACS_SECS (before_time) - b->auto_save_failure_time < 1200) |
5422 | continue; | |
5423 | ||
090101cf CY |
5424 | set_buffer_internal (b); |
5425 | if (NILP (Vauto_save_include_big_deletions) | |
4b4deea2 | 5426 | && (XFASTINT (BVAR (b, save_length)) * 10 |
4be941e3 | 5427 | > (BUF_Z (b) - BUF_BEG (b)) * 13) |
f14b1c68 JB |
5428 | /* A short file is likely to change a large fraction; |
5429 | spare the user annoying messages. */ | |
4b4deea2 | 5430 | && XFASTINT (BVAR (b, save_length)) > 5000 |
f14b1c68 | 5431 | /* These messages are frequent and annoying for `*mail*'. */ |
4b4deea2 | 5432 | && !EQ (BVAR (b, filename), Qnil) |
f14b1c68 JB |
5433 | && NILP (no_message)) |
5434 | { | |
5435 | /* It has shrunk too much; turn off auto-saving here. */ | |
a8c828be | 5436 | minibuffer_auto_raise = orig_minibuffer_auto_raise; |
fd91d0d4 | 5437 | message_with_string ("Buffer %s has shrunk a lot; auto save disabled in that buffer until next real save", |
4b4deea2 | 5438 | BVAR (b, name), 1); |
a8c828be | 5439 | minibuffer_auto_raise = 0; |
82c2d839 RS |
5440 | /* Turn off auto-saving until there's a real save, |
5441 | and prevent any more warnings. */ | |
4b4deea2 | 5442 | XSETINT (BVAR (b, save_length), -1); |
f14b1c68 JB |
5443 | Fsleep_for (make_number (1), Qnil); |
5444 | continue; | |
5445 | } | |
f14b1c68 JB |
5446 | if (!auto_saved && NILP (no_message)) |
5447 | message1 ("Auto-saving..."); | |
5448 | internal_condition_case (auto_save_1, Qt, auto_save_error); | |
f75d7a91 | 5449 | auto_saved = 1; |
0b5397c2 | 5450 | BUF_AUTOSAVE_MODIFF (b) = BUF_MODIFF (b); |
4b4deea2 | 5451 | XSETFASTINT (BVAR (current_buffer, save_length), Z - BEG); |
f14b1c68 | 5452 | set_buffer_internal (old); |
b60247d9 | 5453 | |
e9a9ae03 | 5454 | after_time = current_emacs_time (); |
b60247d9 RS |
5455 | |
5456 | /* If auto-save took more than 60 seconds, | |
5457 | assume it was an NFS failure that got a timeout. */ | |
5458 | if (EMACS_SECS (after_time) - EMACS_SECS (before_time) > 60) | |
5459 | b->auto_save_failure_time = EMACS_SECS (after_time); | |
f14b1c68 JB |
5460 | } |
5461 | } | |
570d7624 | 5462 | |
b67f2ca5 RS |
5463 | /* Prevent another auto save till enough input events come in. */ |
5464 | record_auto_save (); | |
570d7624 | 5465 | |
17857782 | 5466 | if (auto_saved && NILP (no_message)) |
f05b275b | 5467 | { |
5794dd61 | 5468 | if (old_message_p) |
31f3d831 | 5469 | { |
5794dd61 RS |
5470 | /* If we are going to restore an old message, |
5471 | give time to read ours. */ | |
83f8d903 | 5472 | sit_for (make_number (1), 0, 0); |
c71106e5 | 5473 | restore_message (); |
31f3d831 | 5474 | } |
ca730bf0 | 5475 | else if (!auto_save_error_occurred) |
31e31a15 CY |
5476 | /* Don't overwrite the error message if an error occurred. |
5477 | If we displayed a message and then restored a state | |
5794dd61 | 5478 | with no message, leave a "done" message on the screen. */ |
f05b275b KH |
5479 | message1 ("Auto-saving...done"); |
5480 | } | |
570d7624 | 5481 | |
ff4c9993 RS |
5482 | Vquit_flag = oquit; |
5483 | ||
5794dd61 | 5484 | /* This restores the message-stack status. */ |
e54d3b5d | 5485 | unbind_to (count, Qnil); |
570d7624 JB |
5486 | return Qnil; |
5487 | } | |
5488 | ||
5489 | DEFUN ("set-buffer-auto-saved", Fset_buffer_auto_saved, | |
8c1a1077 PJ |
5490 | Sset_buffer_auto_saved, 0, 0, 0, |
5491 | doc: /* Mark current buffer as auto-saved with its current text. | |
5492 | No auto-save file will be written until the buffer changes again. */) | |
5842a27b | 5493 | (void) |
570d7624 | 5494 | { |
0b5397c2 SM |
5495 | /* FIXME: This should not be called in indirect buffers, since |
5496 | they're not autosaved. */ | |
5497 | BUF_AUTOSAVE_MODIFF (current_buffer) = MODIFF; | |
4b4deea2 | 5498 | XSETFASTINT (BVAR (current_buffer, save_length), Z - BEG); |
d311d28c | 5499 | current_buffer->auto_save_failure_time = 0; |
b60247d9 RS |
5500 | return Qnil; |
5501 | } | |
5502 | ||
5503 | DEFUN ("clear-buffer-auto-save-failure", Fclear_buffer_auto_save_failure, | |
8c1a1077 PJ |
5504 | Sclear_buffer_auto_save_failure, 0, 0, 0, |
5505 | doc: /* Clear any record of a recent auto-save failure in the current buffer. */) | |
5842a27b | 5506 | (void) |
b60247d9 | 5507 | { |
d311d28c | 5508 | current_buffer->auto_save_failure_time = 0; |
570d7624 JB |
5509 | return Qnil; |
5510 | } | |
5511 | ||
5512 | DEFUN ("recent-auto-save-p", Frecent_auto_save_p, Srecent_auto_save_p, | |
8c1a1077 | 5513 | 0, 0, 0, |
68780e2a RS |
5514 | doc: /* Return t if current buffer has been auto-saved recently. |
5515 | More precisely, if it has been auto-saved since last read from or saved | |
5516 | in the visited file. If the buffer has no visited file, | |
5517 | then any auto-save counts as "recent". */) | |
5842a27b | 5518 | (void) |
570d7624 | 5519 | { |
0b5397c2 SM |
5520 | /* FIXME: maybe we should return nil for indirect buffers since |
5521 | they're never autosaved. */ | |
5522 | return (SAVE_MODIFF < BUF_AUTOSAVE_MODIFF (current_buffer) ? Qt : Qnil); | |
570d7624 JB |
5523 | } |
5524 | \f | |
5525 | /* Reading and completing file names */ | |
6e710ae5 | 5526 | |
88208bb8 JD |
5527 | DEFUN ("next-read-file-uses-dialog-p", Fnext_read_file_uses_dialog_p, |
5528 | Snext_read_file_uses_dialog_p, 0, 0, 0, | |
5529 | doc: /* Return t if a call to `read-file-name' will use a dialog. | |
5530 | The return value is only relevant for a call to `read-file-name' that happens | |
1a0de25c | 5531 | before any other event (mouse or keypress) is handled. */) |
5842a27b | 5532 | (void) |
88208bb8 | 5533 | { |
d7e642cc JD |
5534 | #if defined (USE_MOTIF) || defined (HAVE_NTGUI) || defined (USE_GTK) \ |
5535 | || defined (HAVE_NS) | |
88208bb8 JD |
5536 | if ((NILP (last_nonmenu_event) || CONSP (last_nonmenu_event)) |
5537 | && use_dialog_box | |
5538 | && use_file_dialog | |
5539 | && have_menus_p ()) | |
5540 | return Qt; | |
5541 | #endif | |
5542 | return Qnil; | |
5543 | } | |
d4a42098 | 5544 | |
dbd50d4b | 5545 | Lisp_Object |
971de7fb | 5546 | Fread_file_name (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object initial, Lisp_Object predicate) |
570d7624 | 5547 | { |
fd4ead52 | 5548 | struct gcpro gcpro1; |
dbd50d4b | 5549 | Lisp_Object args[7]; |
a79485af | 5550 | |
71e1f69d | 5551 | GCPRO1 (default_filename); |
dbd50d4b SM |
5552 | args[0] = intern ("read-file-name"); |
5553 | args[1] = prompt; | |
5554 | args[2] = dir; | |
5555 | args[3] = default_filename; | |
5556 | args[4] = mustmatch; | |
5557 | args[5] = initial; | |
5558 | args[6] = predicate; | |
5559 | RETURN_UNGCPRO (Ffuncall (7, args)); | |
570d7624 | 5560 | } |
9c856db9 | 5561 | |
570d7624 | 5562 | \f |
dfcf069d | 5563 | void |
971de7fb | 5564 | syms_of_fileio (void) |
570d7624 | 5565 | { |
cd3520a4 JB |
5566 | DEFSYM (Qoperations, "operations"); |
5567 | DEFSYM (Qexpand_file_name, "expand-file-name"); | |
5568 | DEFSYM (Qsubstitute_in_file_name, "substitute-in-file-name"); | |
5569 | DEFSYM (Qdirectory_file_name, "directory-file-name"); | |
5570 | DEFSYM (Qfile_name_directory, "file-name-directory"); | |
5571 | DEFSYM (Qfile_name_nondirectory, "file-name-nondirectory"); | |
5572 | DEFSYM (Qunhandled_file_name_directory, "unhandled-file-name-directory"); | |
5573 | DEFSYM (Qfile_name_as_directory, "file-name-as-directory"); | |
5574 | DEFSYM (Qcopy_file, "copy-file"); | |
5575 | DEFSYM (Qmake_directory_internal, "make-directory-internal"); | |
5576 | DEFSYM (Qmake_directory, "make-directory"); | |
5577 | DEFSYM (Qdelete_directory_internal, "delete-directory-internal"); | |
5578 | DEFSYM (Qdelete_file, "delete-file"); | |
5579 | DEFSYM (Qrename_file, "rename-file"); | |
5580 | DEFSYM (Qadd_name_to_file, "add-name-to-file"); | |
5581 | DEFSYM (Qmake_symbolic_link, "make-symbolic-link"); | |
5582 | DEFSYM (Qfile_exists_p, "file-exists-p"); | |
5583 | DEFSYM (Qfile_executable_p, "file-executable-p"); | |
5584 | DEFSYM (Qfile_readable_p, "file-readable-p"); | |
5585 | DEFSYM (Qfile_writable_p, "file-writable-p"); | |
5586 | DEFSYM (Qfile_symlink_p, "file-symlink-p"); | |
5587 | DEFSYM (Qaccess_file, "access-file"); | |
5588 | DEFSYM (Qfile_directory_p, "file-directory-p"); | |
5589 | DEFSYM (Qfile_regular_p, "file-regular-p"); | |
5590 | DEFSYM (Qfile_accessible_directory_p, "file-accessible-directory-p"); | |
5591 | DEFSYM (Qfile_modes, "file-modes"); | |
5592 | DEFSYM (Qset_file_modes, "set-file-modes"); | |
5593 | DEFSYM (Qset_file_times, "set-file-times"); | |
5594 | DEFSYM (Qfile_selinux_context, "file-selinux-context"); | |
5595 | DEFSYM (Qset_file_selinux_context, "set-file-selinux-context"); | |
5596 | DEFSYM (Qfile_newer_than_file_p, "file-newer-than-file-p"); | |
5597 | DEFSYM (Qinsert_file_contents, "insert-file-contents"); | |
5598 | DEFSYM (Qwrite_region, "write-region"); | |
5599 | DEFSYM (Qverify_visited_file_modtime, "verify-visited-file-modtime"); | |
5600 | DEFSYM (Qset_visited_file_modtime, "set-visited-file-modtime"); | |
5601 | DEFSYM (Qauto_save_coding, "auto-save-coding"); | |
5602 | ||
5603 | DEFSYM (Qfile_name_history, "file-name-history"); | |
642ef245 | 5604 | Fset (Qfile_name_history, Qnil); |
15c65264 | 5605 | |
cd3520a4 JB |
5606 | DEFSYM (Qfile_error, "file-error"); |
5607 | DEFSYM (Qfile_already_exists, "file-already-exists"); | |
5608 | DEFSYM (Qfile_date_error, "file-date-error"); | |
5609 | DEFSYM (Qexcl, "excl"); | |
570d7624 | 5610 | |
29208e82 | 5611 | DEFVAR_LISP ("file-name-coding-system", Vfile_name_coding_system, |
fb7ada5f | 5612 | doc: /* Coding system for encoding file names. |
346ebf53 | 5613 | If it is nil, `default-file-name-coding-system' (which see) is used. */); |
b1d1b865 RS |
5614 | Vfile_name_coding_system = Qnil; |
5615 | ||
cd913586 | 5616 | DEFVAR_LISP ("default-file-name-coding-system", |
29208e82 | 5617 | Vdefault_file_name_coding_system, |
8c1a1077 | 5618 | doc: /* Default coding system for encoding file names. |
346ebf53 | 5619 | This variable is used only when `file-name-coding-system' is nil. |
8c1a1077 | 5620 | |
346ebf53 | 5621 | This variable is set/changed by the command `set-language-environment'. |
8c1a1077 | 5622 | User should not set this variable manually, |
346ebf53 | 5623 | instead use `file-name-coding-system' to get a constant encoding |
8c1a1077 | 5624 | of file names regardless of the current language environment. */); |
cd913586 KH |
5625 | Vdefault_file_name_coding_system = Qnil; |
5626 | ||
cd3520a4 JB |
5627 | DEFSYM (Qformat_decode, "format-decode"); |
5628 | DEFSYM (Qformat_annotate_function, "format-annotate-function"); | |
5629 | DEFSYM (Qafter_insert_file_set_coding, "after-insert-file-set-coding"); | |
5630 | DEFSYM (Qcar_less_than_car, "car-less-than-car"); | |
d6a3cc15 | 5631 | |
570d7624 | 5632 | Fput (Qfile_error, Qerror_conditions, |
d67b4f80 | 5633 | Fpurecopy (list2 (Qfile_error, Qerror))); |
570d7624 | 5634 | Fput (Qfile_error, Qerror_message, |
2a0213a6 | 5635 | build_pure_c_string ("File error")); |
570d7624 JB |
5636 | |
5637 | Fput (Qfile_already_exists, Qerror_conditions, | |
d67b4f80 | 5638 | Fpurecopy (list3 (Qfile_already_exists, Qfile_error, Qerror))); |
570d7624 | 5639 | Fput (Qfile_already_exists, Qerror_message, |
2a0213a6 | 5640 | build_pure_c_string ("File already exists")); |
570d7624 | 5641 | |
c0b7b21c | 5642 | Fput (Qfile_date_error, Qerror_conditions, |
d67b4f80 | 5643 | Fpurecopy (list3 (Qfile_date_error, Qfile_error, Qerror))); |
c0b7b21c | 5644 | Fput (Qfile_date_error, Qerror_message, |
2a0213a6 | 5645 | build_pure_c_string ("Cannot set file date")); |
c0b7b21c | 5646 | |
29208e82 | 5647 | DEFVAR_LISP ("file-name-handler-alist", Vfile_name_handler_alist, |
a59225b1 CY |
5648 | doc: /* Alist of elements (REGEXP . HANDLER) for file names handled specially. |
5649 | If a file name matches REGEXP, all I/O on that file is done by calling | |
5650 | HANDLER. If a file name matches more than one handler, the handler | |
5651 | whose match starts last in the file name gets precedence. The | |
5652 | function `find-file-name-handler' checks this list for a handler for | |
5653 | its argument. | |
5654 | ||
5655 | HANDLER should be a function. The first argument given to it is the | |
5656 | name of the I/O primitive to be handled; the remaining arguments are | |
5657 | the arguments that were passed to that primitive. For example, if you | |
5658 | do (file-exists-p FILENAME) and FILENAME is handled by HANDLER, then | |
5659 | HANDLER is called like this: | |
5660 | ||
5661 | (funcall HANDLER 'file-exists-p FILENAME) | |
5662 | ||
5663 | Note that HANDLER must be able to handle all I/O primitives; if it has | |
5664 | nothing special to do for a primitive, it should reinvoke the | |
5665 | primitive to handle the operation \"the usual way\". | |
5666 | See Info node `(elisp)Magic File Names' for more details. */); | |
09121adc RS |
5667 | Vfile_name_handler_alist = Qnil; |
5668 | ||
0414b394 | 5669 | DEFVAR_LISP ("set-auto-coding-function", |
29208e82 | 5670 | Vset_auto_coding_function, |
8c1a1077 PJ |
5671 | doc: /* If non-nil, a function to call to decide a coding system of file. |
5672 | Two arguments are passed to this function: the file name | |
5673 | and the length of a file contents following the point. | |
5674 | This function should return a coding system to decode the file contents. | |
5675 | It should check the file name against `auto-coding-alist'. | |
5676 | If no coding system is decided, it should check a coding system | |
5677 | specified in the heading lines with the format: | |
5678 | -*- ... coding: CODING-SYSTEM; ... -*- | |
5679 | or local variable spec of the tailing lines with `coding:' tag. */); | |
0414b394 | 5680 | Vset_auto_coding_function = Qnil; |
c9e82392 | 5681 | |
29208e82 | 5682 | DEFVAR_LISP ("after-insert-file-functions", Vafter_insert_file_functions, |
8c1a1077 | 5683 | doc: /* A list of functions to be called at the end of `insert-file-contents'. |
0cf9f5b5 RS |
5684 | Each is passed one argument, the number of characters inserted, |
5685 | with point at the start of the inserted text. Each function | |
5686 | should leave point the same, and return the new character count. | |
cf6d2357 RS |
5687 | If `insert-file-contents' is intercepted by a handler from |
5688 | `file-name-handler-alist', that handler is responsible for calling the | |
5689 | functions in `after-insert-file-functions' if appropriate. */); | |
d6a3cc15 RS |
5690 | Vafter_insert_file_functions = Qnil; |
5691 | ||
29208e82 | 5692 | DEFVAR_LISP ("write-region-annotate-functions", Vwrite_region_annotate_functions, |
8c1a1077 PJ |
5693 | doc: /* A list of functions to be called at the start of `write-region'. |
5694 | Each is passed two arguments, START and END as for `write-region'. | |
5695 | These are usually two numbers but not always; see the documentation | |
5696 | for `write-region'. The function should return a list of pairs | |
5697 | of the form (POSITION . STRING), consisting of strings to be effectively | |
5698 | inserted at the specified positions of the file being written (1 means to | |
5699 | insert before the first byte written). The POSITIONs must be sorted into | |
67fbc0cb CY |
5700 | increasing order. |
5701 | ||
5702 | If there are several annotation functions, the lists returned by these | |
5703 | functions are merged destructively. As each annotation function runs, | |
5704 | the variable `write-region-annotations-so-far' contains a list of all | |
5705 | annotations returned by previous annotation functions. | |
5706 | ||
5707 | An annotation function can return with a different buffer current. | |
5708 | Doing so removes the annotations returned by previous functions, and | |
5709 | resets START and END to `point-min' and `point-max' of the new buffer. | |
5710 | ||
5711 | After `write-region' completes, Emacs calls the function stored in | |
5712 | `write-region-post-annotation-function', once for each buffer that was | |
5713 | current when building the annotations (i.e., at least once), with that | |
5714 | buffer current. */); | |
d6a3cc15 | 5715 | Vwrite_region_annotate_functions = Qnil; |
cd3520a4 | 5716 | DEFSYM (Qwrite_region_annotate_functions, "write-region-annotate-functions"); |
d6a3cc15 | 5717 | |
67fbc0cb | 5718 | DEFVAR_LISP ("write-region-post-annotation-function", |
29208e82 | 5719 | Vwrite_region_post_annotation_function, |
67fbc0cb CY |
5720 | doc: /* Function to call after `write-region' completes. |
5721 | The function is called with no arguments. If one or more of the | |
5722 | annotation functions in `write-region-annotate-functions' changed the | |
5723 | current buffer, the function stored in this variable is called for | |
5724 | each of those additional buffers as well, in addition to the original | |
5725 | buffer. The relevant buffer is current during each function call. */); | |
5726 | Vwrite_region_post_annotation_function = Qnil; | |
5727 | staticpro (&Vwrite_region_annotation_buffers); | |
5728 | ||
6fc6f94b | 5729 | DEFVAR_LISP ("write-region-annotations-so-far", |
29208e82 | 5730 | Vwrite_region_annotations_so_far, |
8c1a1077 PJ |
5731 | doc: /* When an annotation function is called, this holds the previous annotations. |
5732 | These are the annotations made by other annotation functions | |
5733 | that were already called. See also `write-region-annotate-functions'. */); | |
6fc6f94b RS |
5734 | Vwrite_region_annotations_so_far = Qnil; |
5735 | ||
29208e82 | 5736 | DEFVAR_LISP ("inhibit-file-name-handlers", Vinhibit_file_name_handlers, |
8c1a1077 PJ |
5737 | doc: /* A list of file name handlers that temporarily should not be used. |
5738 | This applies only to the operation `inhibit-file-name-operation'. */); | |
82c2d839 RS |
5739 | Vinhibit_file_name_handlers = Qnil; |
5740 | ||
29208e82 | 5741 | DEFVAR_LISP ("inhibit-file-name-operation", Vinhibit_file_name_operation, |
8c1a1077 | 5742 | doc: /* The operation for which `inhibit-file-name-handlers' is applicable. */); |
a65970a0 RS |
5743 | Vinhibit_file_name_operation = Qnil; |
5744 | ||
29208e82 | 5745 | DEFVAR_LISP ("auto-save-list-file-name", Vauto_save_list_file_name, |
8c1a1077 PJ |
5746 | doc: /* File name in which we write a list of all auto save file names. |
5747 | This variable is initialized automatically from `auto-save-list-file-prefix' | |
5748 | shortly after Emacs reads your `.emacs' file, if you have not yet given it | |
5749 | a non-nil value. */); | |
e54d3b5d RS |
5750 | Vauto_save_list_file_name = Qnil; |
5751 | ||
29208e82 | 5752 | DEFVAR_LISP ("auto-save-visited-file-name", Vauto_save_visited_file_name, |
699b53bc CY |
5753 | doc: /* Non-nil says auto-save a buffer in the file it is visiting, when practical. |
5754 | Normally auto-save files are written under other names. */); | |
5755 | Vauto_save_visited_file_name = Qnil; | |
5756 | ||
29208e82 | 5757 | DEFVAR_LISP ("auto-save-include-big-deletions", Vauto_save_include_big_deletions, |
090101cf CY |
5758 | doc: /* If non-nil, auto-save even if a large part of the text is deleted. |
5759 | If nil, deleting a substantial portion of the text disables auto-save | |
5760 | in the buffer; this is the default behavior, because the auto-save | |
5761 | file is usually more useful if it contains the deleted text. */); | |
5762 | Vauto_save_include_big_deletions = Qnil; | |
5763 | ||
ccf61795 | 5764 | #ifdef HAVE_FSYNC |
29208e82 | 5765 | DEFVAR_BOOL ("write-region-inhibit-fsync", write_region_inhibit_fsync, |
fb7ada5f | 5766 | doc: /* Non-nil means don't call fsync in `write-region'. |
e3f509dd RF |
5767 | This variable affects calls to `write-region' as well as save commands. |
5768 | A non-nil value may result in data loss! */); | |
ccf61795 RF |
5769 | write_region_inhibit_fsync = 0; |
5770 | #endif | |
5771 | ||
29208e82 | 5772 | DEFVAR_BOOL ("delete-by-moving-to-trash", delete_by_moving_to_trash, |
6cf29fe8 | 5773 | doc: /* Specifies whether to use the system's trash can. |
f1a5d776 CY |
5774 | When non-nil, certain file deletion commands use the function |
5775 | `move-file-to-trash' instead of deleting files outright. | |
5776 | This includes interactive calls to `delete-file' and | |
5777 | `delete-directory' and the Dired deletion commands. */); | |
6cf29fe8 | 5778 | delete_by_moving_to_trash = 0; |
d67b4f80 | 5779 | Qdelete_by_moving_to_trash = intern_c_string ("delete-by-moving-to-trash"); |
cd3520a4 JB |
5780 | |
5781 | DEFSYM (Qmove_file_to_trash, "move-file-to-trash"); | |
5782 | DEFSYM (Qcopy_directory, "copy-directory"); | |
5783 | DEFSYM (Qdelete_directory, "delete-directory"); | |
6cf29fe8 | 5784 | |
642ef245 | 5785 | defsubr (&Sfind_file_name_handler); |
570d7624 JB |
5786 | defsubr (&Sfile_name_directory); |
5787 | defsubr (&Sfile_name_nondirectory); | |
642ef245 | 5788 | defsubr (&Sunhandled_file_name_directory); |
570d7624 JB |
5789 | defsubr (&Sfile_name_as_directory); |
5790 | defsubr (&Sdirectory_file_name); | |
5791 | defsubr (&Smake_temp_name); | |
5792 | defsubr (&Sexpand_file_name); | |
5793 | defsubr (&Ssubstitute_in_file_name); | |
5794 | defsubr (&Scopy_file); | |
9bbe01fb | 5795 | defsubr (&Smake_directory_internal); |
9d8f3bd9 | 5796 | defsubr (&Sdelete_directory_internal); |
570d7624 JB |
5797 | defsubr (&Sdelete_file); |
5798 | defsubr (&Srename_file); | |
5799 | defsubr (&Sadd_name_to_file); | |
570d7624 | 5800 | defsubr (&Smake_symbolic_link); |
570d7624 JB |
5801 | defsubr (&Sfile_name_absolute_p); |
5802 | defsubr (&Sfile_exists_p); | |
5803 | defsubr (&Sfile_executable_p); | |
5804 | defsubr (&Sfile_readable_p); | |
5805 | defsubr (&Sfile_writable_p); | |
1f8653eb | 5806 | defsubr (&Saccess_file); |
570d7624 JB |
5807 | defsubr (&Sfile_symlink_p); |
5808 | defsubr (&Sfile_directory_p); | |
b72dea2a | 5809 | defsubr (&Sfile_accessible_directory_p); |
f793dc6c | 5810 | defsubr (&Sfile_regular_p); |
570d7624 JB |
5811 | defsubr (&Sfile_modes); |
5812 | defsubr (&Sset_file_modes); | |
819da85b | 5813 | defsubr (&Sset_file_times); |
574c05e2 KK |
5814 | defsubr (&Sfile_selinux_context); |
5815 | defsubr (&Sset_file_selinux_context); | |
c24e9a53 RS |
5816 | defsubr (&Sset_default_file_modes); |
5817 | defsubr (&Sdefault_file_modes); | |
570d7624 JB |
5818 | defsubr (&Sfile_newer_than_file_p); |
5819 | defsubr (&Sinsert_file_contents); | |
5820 | defsubr (&Swrite_region); | |
d6a3cc15 | 5821 | defsubr (&Scar_less_than_car); |
570d7624 JB |
5822 | defsubr (&Sverify_visited_file_modtime); |
5823 | defsubr (&Sclear_visited_file_modtime); | |
f5d5eccf | 5824 | defsubr (&Svisited_file_modtime); |
570d7624 JB |
5825 | defsubr (&Sset_visited_file_modtime); |
5826 | defsubr (&Sdo_auto_save); | |
5827 | defsubr (&Sset_buffer_auto_saved); | |
b60247d9 | 5828 | defsubr (&Sclear_buffer_auto_save_failure); |
570d7624 JB |
5829 | defsubr (&Srecent_auto_save_p); |
5830 | ||
88208bb8 | 5831 | defsubr (&Snext_read_file_uses_dialog_p); |
85ffea93 | 5832 | |
697c17a2 | 5833 | #ifdef HAVE_SYNC |
85ffea93 | 5834 | defsubr (&Sunix_sync); |
483a2e10 | 5835 | #endif |
570d7624 | 5836 | } |