1 /* File IO for GNU Emacs.
2 Copyright (C) 1985,86,87,88,93,94,95,96,97,98,99,2000,01,2003
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
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.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
29 #include <sys/types.h>
36 #if !defined (S_ISLNK) && defined (S_IFLNK)
37 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
40 #if !defined (S_ISFIFO) && defined (S_IFIFO)
41 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
44 #if !defined (S_ISREG) && defined (S_IFREG)
45 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
76 #include "intervals.h"
87 #endif /* not WINDOWSNT */
91 #include <sys/param.h>
99 #define CORRECT_DIR_SEPS(s) \
100 do { if ('/' == DIRECTORY_SEP) dostounix_filename (s); \
101 else unixtodos_filename (s); \
103 /* On Windows, drive letters must be alphabetic - on DOS, the Netware
104 redirector allows the six letters between 'Z' and 'a' as well. */
106 #define IS_DRIVE(x) ((x) >= 'A' && (x) <= 'z')
109 #define IS_DRIVE(x) isalpha (x)
111 /* Need to lower-case the drive letter, or else expanded
112 filenames will sometimes compare inequal, because
113 `expand-file-name' doesn't always down-case the drive letter. */
114 #define DRIVE_LETTER(x) (tolower (x))
135 #include "commands.h"
136 extern int use_dialog_box
;
150 /* Nonzero during writing of auto-save files */
153 /* Set by auto_save_1 to mode of original file so Fwrite_region will create
154 a new file with the same mode as the original */
155 int auto_save_mode_bits
;
157 /* The symbol bound to coding-system-for-read when
158 insert-file-contents is called for recovering a file. This is not
159 an actual coding system name, but just an indicator to tell
160 insert-file-contents to use `emacs-mule' with a special flag for
161 auto saving and recovering a file. */
162 Lisp_Object Qauto_save_coding
;
164 /* Coding system for file names, or nil if none. */
165 Lisp_Object Vfile_name_coding_system
;
167 /* Coding system for file names used only when
168 Vfile_name_coding_system is nil. */
169 Lisp_Object Vdefault_file_name_coding_system
;
171 /* Alist of elements (REGEXP . HANDLER) for file names
172 whose I/O is done with a special handler. */
173 Lisp_Object Vfile_name_handler_alist
;
175 /* Format for auto-save files */
176 Lisp_Object Vauto_save_file_format
;
178 /* Lisp functions for translating file formats */
179 Lisp_Object Qformat_decode
, Qformat_annotate_function
;
181 /* Function to be called to decide a coding system of a reading file. */
182 Lisp_Object Vset_auto_coding_function
;
184 /* Functions to be called to process text properties in inserted file. */
185 Lisp_Object Vafter_insert_file_functions
;
187 /* Lisp function for setting buffer-file-coding-system and the
188 multibyteness of the current buffer after inserting a file. */
189 Lisp_Object Qafter_insert_file_set_coding
;
191 /* Functions to be called to create text property annotations for file. */
192 Lisp_Object Vwrite_region_annotate_functions
;
193 Lisp_Object Qwrite_region_annotate_functions
;
195 /* During build_annotations, each time an annotation function is called,
196 this holds the annotations made by the previous functions. */
197 Lisp_Object Vwrite_region_annotations_so_far
;
199 /* File name in which we write a list of all our auto save files. */
200 Lisp_Object Vauto_save_list_file_name
;
202 /* Function to call to read a file name. */
203 Lisp_Object Vread_file_name_function
;
205 /* Current predicate used by read_file_name_internal. */
206 Lisp_Object Vread_file_name_predicate
;
208 /* Nonzero means, when reading a filename in the minibuffer,
209 start out by inserting the default directory into the minibuffer. */
210 int insert_default_directory
;
212 /* On VMS, nonzero means write new files with record format stmlf.
213 Zero means use var format. */
216 /* On NT, specifies the directory separator character, used (eg.) when
217 expanding file names. This can be bound to / or \. */
218 Lisp_Object Vdirectory_sep_char
;
220 extern Lisp_Object Vuser_login_name
;
223 extern Lisp_Object Vw32_get_true_file_attributes
;
226 extern int minibuf_level
;
228 extern int minibuffer_auto_raise
;
230 /* These variables describe handlers that have "already" had a chance
231 to handle the current operation.
233 Vinhibit_file_name_handlers is a list of file name handlers.
234 Vinhibit_file_name_operation is the operation being handled.
235 If we try to handle that operation, we ignore those handlers. */
237 static Lisp_Object Vinhibit_file_name_handlers
;
238 static Lisp_Object Vinhibit_file_name_operation
;
240 Lisp_Object Qfile_error
, Qfile_already_exists
, Qfile_date_error
;
242 Lisp_Object Qfile_name_history
;
244 Lisp_Object Qcar_less_than_car
;
246 static int a_write
P_ ((int, Lisp_Object
, int, int,
247 Lisp_Object
*, struct coding_system
*));
248 static int e_write
P_ ((int, Lisp_Object
, int, int, struct coding_system
*));
252 report_file_error (string
, data
)
256 Lisp_Object errstring
;
259 synchronize_system_messages_locale ();
260 errstring
= code_convert_string_norecord (build_string (strerror (errorno
)),
261 Vlocale_coding_system
, 0);
267 Fsignal (Qfile_already_exists
, Fcons (errstring
, data
));
270 /* System error messages are capitalized. Downcase the initial
271 unless it is followed by a slash. */
272 if (SREF (errstring
, 1) != '/')
273 SSET (errstring
, 0, DOWNCASE (SREF (errstring
, 0)));
275 Fsignal (Qfile_error
,
276 Fcons (build_string (string
), Fcons (errstring
, data
)));
281 close_file_unwind (fd
)
284 emacs_close (XFASTINT (fd
));
288 /* Restore point, having saved it as a marker. */
291 restore_point_unwind (location
)
292 Lisp_Object location
;
294 Fgoto_char (location
);
295 Fset_marker (location
, Qnil
, Qnil
);
299 Lisp_Object Qexpand_file_name
;
300 Lisp_Object Qsubstitute_in_file_name
;
301 Lisp_Object Qdirectory_file_name
;
302 Lisp_Object Qfile_name_directory
;
303 Lisp_Object Qfile_name_nondirectory
;
304 Lisp_Object Qunhandled_file_name_directory
;
305 Lisp_Object Qfile_name_as_directory
;
306 Lisp_Object Qcopy_file
;
307 Lisp_Object Qmake_directory_internal
;
308 Lisp_Object Qmake_directory
;
309 Lisp_Object Qdelete_directory
;
310 Lisp_Object Qdelete_file
;
311 Lisp_Object Qrename_file
;
312 Lisp_Object Qadd_name_to_file
;
313 Lisp_Object Qmake_symbolic_link
;
314 Lisp_Object Qfile_exists_p
;
315 Lisp_Object Qfile_executable_p
;
316 Lisp_Object Qfile_readable_p
;
317 Lisp_Object Qfile_writable_p
;
318 Lisp_Object Qfile_symlink_p
;
319 Lisp_Object Qaccess_file
;
320 Lisp_Object Qfile_directory_p
;
321 Lisp_Object Qfile_regular_p
;
322 Lisp_Object Qfile_accessible_directory_p
;
323 Lisp_Object Qfile_modes
;
324 Lisp_Object Qset_file_modes
;
325 Lisp_Object Qfile_newer_than_file_p
;
326 Lisp_Object Qinsert_file_contents
;
327 Lisp_Object Qwrite_region
;
328 Lisp_Object Qverify_visited_file_modtime
;
329 Lisp_Object Qset_visited_file_modtime
;
331 DEFUN ("find-file-name-handler", Ffind_file_name_handler
, Sfind_file_name_handler
, 2, 2, 0,
332 doc
: /* Return FILENAME's handler function for OPERATION, if it has one.
333 Otherwise, return nil.
334 A file name is handled if one of the regular expressions in
335 `file-name-handler-alist' matches it.
337 If OPERATION equals `inhibit-file-name-operation', then we ignore
338 any handlers that are members of `inhibit-file-name-handlers',
339 but we still do run any other handlers. This lets handlers
340 use the standard functions without calling themselves recursively. */)
341 (filename
, operation
)
342 Lisp_Object filename
, operation
;
344 /* This function must not munge the match data. */
345 Lisp_Object chain
, inhibited_handlers
, result
;
349 CHECK_STRING (filename
);
351 if (EQ (operation
, Vinhibit_file_name_operation
))
352 inhibited_handlers
= Vinhibit_file_name_handlers
;
354 inhibited_handlers
= Qnil
;
356 for (chain
= Vfile_name_handler_alist
; CONSP (chain
);
357 chain
= XCDR (chain
))
367 && (match_pos
= fast_string_match (string
, filename
)) > pos
)
369 Lisp_Object handler
, tem
;
371 handler
= XCDR (elt
);
372 tem
= Fmemq (handler
, inhibited_handlers
);
386 DEFUN ("file-name-directory", Ffile_name_directory
, Sfile_name_directory
,
388 doc
: /* Return the directory component in file name FILENAME.
389 Return nil if FILENAME does not include a directory.
390 Otherwise return a directory spec.
391 Given a Unix syntax file name, returns a string ending in slash;
392 on VMS, perhaps instead a string ending in `:', `]' or `>'. */)
394 Lisp_Object filename
;
397 register const unsigned char *beg
;
399 register unsigned char *beg
;
401 register const unsigned char *p
;
404 CHECK_STRING (filename
);
406 /* If the file name has special constructs in it,
407 call the corresponding file handler. */
408 handler
= Ffind_file_name_handler (filename
, Qfile_name_directory
);
410 return call2 (handler
, Qfile_name_directory
, filename
);
412 #ifdef FILE_SYSTEM_CASE
413 filename
= FILE_SYSTEM_CASE (filename
);
415 beg
= SDATA (filename
);
417 beg
= strcpy (alloca (strlen (beg
) + 1), beg
);
419 p
= beg
+ SBYTES (filename
);
421 while (p
!= beg
&& !IS_DIRECTORY_SEP (p
[-1])
423 && p
[-1] != ':' && p
[-1] != ']' && p
[-1] != '>'
426 /* only recognise drive specifier at the beginning */
428 /* handle the "/:d:foo" and "/:foo" cases correctly */
429 && ((p
== beg
+ 2 && !IS_DIRECTORY_SEP (*beg
))
430 || (p
== beg
+ 4 && IS_DIRECTORY_SEP (*beg
))))
437 /* Expansion of "c:" to drive and default directory. */
440 /* MAXPATHLEN+1 is guaranteed to be enough space for getdefdir. */
441 unsigned char *res
= alloca (MAXPATHLEN
+ 1);
442 unsigned char *r
= res
;
444 if (p
== beg
+ 4 && IS_DIRECTORY_SEP (*beg
) && beg
[1] == ':')
446 strncpy (res
, beg
, 2);
451 if (getdefdir (toupper (*beg
) - 'A' + 1, r
))
453 if (!IS_DIRECTORY_SEP (res
[strlen (res
) - 1]))
456 p
= beg
+ strlen (beg
);
459 CORRECT_DIR_SEPS (beg
);
462 return make_specified_string (beg
, -1, p
- beg
, STRING_MULTIBYTE (filename
));
465 DEFUN ("file-name-nondirectory", Ffile_name_nondirectory
,
466 Sfile_name_nondirectory
, 1, 1, 0,
467 doc
: /* Return file name FILENAME sans its directory.
468 For example, in a Unix-syntax file name,
469 this is everything after the last slash,
470 or the entire name if it contains no slash. */)
472 Lisp_Object filename
;
474 register const unsigned char *beg
, *p
, *end
;
477 CHECK_STRING (filename
);
479 /* If the file name has special constructs in it,
480 call the corresponding file handler. */
481 handler
= Ffind_file_name_handler (filename
, Qfile_name_nondirectory
);
483 return call2 (handler
, Qfile_name_nondirectory
, filename
);
485 beg
= SDATA (filename
);
486 end
= p
= beg
+ SBYTES (filename
);
488 while (p
!= beg
&& !IS_DIRECTORY_SEP (p
[-1])
490 && p
[-1] != ':' && p
[-1] != ']' && p
[-1] != '>'
493 /* only recognise drive specifier at beginning */
495 /* handle the "/:d:foo" case correctly */
496 && (p
== beg
+ 2 || (p
== beg
+ 4 && IS_DIRECTORY_SEP (*beg
))))
501 return make_specified_string (p
, -1, end
- p
, STRING_MULTIBYTE (filename
));
504 DEFUN ("unhandled-file-name-directory", Funhandled_file_name_directory
,
505 Sunhandled_file_name_directory
, 1, 1, 0,
506 doc
: /* Return a directly usable directory name somehow associated with FILENAME.
507 A `directly usable' directory name is one that may be used without the
508 intervention of any file handler.
509 If FILENAME is a directly usable file itself, return
510 \(file-name-directory FILENAME).
511 The `call-process' and `start-process' functions use this function to
512 get a current directory to run processes in. */)
514 Lisp_Object filename
;
518 /* If the file name has special constructs in it,
519 call the corresponding file handler. */
520 handler
= Ffind_file_name_handler (filename
, Qunhandled_file_name_directory
);
522 return call2 (handler
, Qunhandled_file_name_directory
, filename
);
524 return Ffile_name_directory (filename
);
529 file_name_as_directory (out
, in
)
532 int size
= strlen (in
) - 1;
545 /* Is it already a directory string? */
546 if (in
[size
] == ':' || in
[size
] == ']' || in
[size
] == '>')
548 /* Is it a VMS directory file name? If so, hack VMS syntax. */
549 else if (! index (in
, '/')
550 && ((size
> 3 && ! strcmp (&in
[size
- 3], ".DIR"))
551 || (size
> 3 && ! strcmp (&in
[size
- 3], ".dir"))
552 || (size
> 5 && (! strncmp (&in
[size
- 5], ".DIR", 4)
553 || ! strncmp (&in
[size
- 5], ".dir", 4))
554 && (in
[size
- 1] == '.' || in
[size
- 1] == ';')
555 && in
[size
] == '1')))
557 register char *p
, *dot
;
561 dir:x.dir --> dir:[x]
562 dir:[x]y.dir --> dir:[x.y] */
564 while (p
!= in
&& *p
!= ':' && *p
!= '>' && *p
!= ']') p
--;
567 strncpy (out
, in
, p
- in
);
586 dot
= index (p
, '.');
589 /* blindly remove any extension */
590 size
= strlen (out
) + (dot
- p
);
591 strncat (out
, p
, dot
- p
);
602 /* For Unix syntax, Append a slash if necessary */
603 if (!IS_DIRECTORY_SEP (out
[size
]))
605 /* Cannot use DIRECTORY_SEP, which could have any value */
607 out
[size
+ 2] = '\0';
610 CORRECT_DIR_SEPS (out
);
616 DEFUN ("file-name-as-directory", Ffile_name_as_directory
,
617 Sfile_name_as_directory
, 1, 1, 0,
618 doc
: /* Return a string representing the file name FILE interpreted as a directory.
619 This operation exists because a directory is also a file, but its name as
620 a directory is different from its name as a file.
621 The result can be used as the value of `default-directory'
622 or passed as second argument to `expand-file-name'.
623 For a Unix-syntax file name, just appends a slash.
624 On VMS, converts \"[X]FOO.DIR\" to \"[X.FOO]\", etc. */)
635 /* If the file name has special constructs in it,
636 call the corresponding file handler. */
637 handler
= Ffind_file_name_handler (file
, Qfile_name_as_directory
);
639 return call2 (handler
, Qfile_name_as_directory
, file
);
641 buf
= (char *) alloca (SBYTES (file
) + 10);
642 file_name_as_directory (buf
, SDATA (file
));
643 return make_specified_string (buf
, -1, strlen (buf
),
644 STRING_MULTIBYTE (file
));
648 * Convert from directory name to filename.
650 * xyzzy:[mukesh.emacs] => xyzzy:[mukesh]emacs.dir.1
651 * xyzzy:[mukesh] => xyzzy:[000000]mukesh.dir.1
652 * On UNIX, it's simple: just make sure there isn't a terminating /
654 * Value is nonzero if the string output is different from the input.
658 directory_file_name (src
, dst
)
666 struct FAB fab
= cc$rms_fab
;
667 struct NAM nam
= cc$rms_nam
;
668 char esa
[NAM$C_MAXRSS
];
673 if (! index (src
, '/')
674 && (src
[slen
- 1] == ']'
675 || src
[slen
- 1] == ':'
676 || src
[slen
- 1] == '>'))
678 /* VMS style - convert [x.y.z] to [x.y]z, [x] to [000000]x */
680 fab
.fab$b_fns
= slen
;
681 fab
.fab$l_nam
= &nam
;
682 fab
.fab$l_fop
= FAB$M_NAM
;
685 nam
.nam$b_ess
= sizeof esa
;
686 nam
.nam$b_nop
|= NAM$M_SYNCHK
;
688 /* We call SYS$PARSE to handle such things as [--] for us. */
689 if (SYS$
PARSE (&fab
, 0, 0) == RMS$_NORMAL
)
691 slen
= nam
.nam$b_esl
;
692 if (esa
[slen
- 1] == ';' && esa
[slen
- 2] == '.')
697 if (src
[slen
- 1] != ']' && src
[slen
- 1] != '>')
699 /* what about when we have logical_name:???? */
700 if (src
[slen
- 1] == ':')
701 { /* Xlate logical name and see what we get */
702 ptr
= strcpy (dst
, src
); /* upper case for getenv */
705 if ('a' <= *ptr
&& *ptr
<= 'z')
709 dst
[slen
- 1] = 0; /* remove colon */
710 if (!(src
= egetenv (dst
)))
712 /* should we jump to the beginning of this procedure?
713 Good points: allows us to use logical names that xlate
715 Bad points: can be a problem if we just translated to a device
717 For now, I'll punt and always expect VMS names, and hope for
720 if (src
[slen
- 1] != ']' && src
[slen
- 1] != '>')
721 { /* no recursion here! */
727 { /* not a directory spec */
732 bracket
= src
[slen
- 1];
734 /* If bracket is ']' or '>', bracket - 2 is the corresponding
736 ptr
= index (src
, bracket
- 2);
738 { /* no opening bracket */
742 if (!(rptr
= rindex (src
, '.')))
745 strncpy (dst
, src
, slen
);
749 dst
[slen
++] = bracket
;
754 /* If we have the top-level of a rooted directory (i.e. xx:[000000]),
755 then translate the device and recurse. */
756 if (dst
[slen
- 1] == ':'
757 && dst
[slen
- 2] != ':' /* skip decnet nodes */
758 && strcmp (src
+ slen
, "[000000]") == 0)
760 dst
[slen
- 1] = '\0';
761 if ((ptr
= egetenv (dst
))
762 && (rlen
= strlen (ptr
) - 1) > 0
763 && (ptr
[rlen
] == ']' || ptr
[rlen
] == '>')
764 && ptr
[rlen
- 1] == '.')
766 char * buf
= (char *) alloca (strlen (ptr
) + 1);
770 return directory_file_name (buf
, dst
);
775 strcat (dst
, "[000000]");
779 rlen
= strlen (rptr
) - 1;
780 strncat (dst
, rptr
, rlen
);
781 dst
[slen
+ rlen
] = '\0';
782 strcat (dst
, ".DIR.1");
786 /* Process as Unix format: just remove any final slash.
787 But leave "/" unchanged; do not change it to "". */
790 /* Handle // as root for apollo's. */
791 if ((slen
> 2 && dst
[slen
- 1] == '/')
792 || (slen
> 1 && dst
[0] != '/' && dst
[slen
- 1] == '/'))
796 && IS_DIRECTORY_SEP (dst
[slen
- 1])
798 && !IS_ANY_SEP (dst
[slen
- 2])
804 CORRECT_DIR_SEPS (dst
);
809 DEFUN ("directory-file-name", Fdirectory_file_name
, Sdirectory_file_name
,
811 doc
: /* Returns the file name of the directory named DIRECTORY.
812 This is the name of the file that holds the data for the directory DIRECTORY.
813 This operation exists because a directory is also a file, but its name as
814 a directory is different from its name as a file.
815 In Unix-syntax, this function just removes the final slash.
816 On VMS, given a VMS-syntax directory name such as \"[X.Y]\",
817 it returns a file name such as \"[X]Y.DIR.1\". */)
819 Lisp_Object directory
;
824 CHECK_STRING (directory
);
826 if (NILP (directory
))
829 /* If the file name has special constructs in it,
830 call the corresponding file handler. */
831 handler
= Ffind_file_name_handler (directory
, Qdirectory_file_name
);
833 return call2 (handler
, Qdirectory_file_name
, directory
);
836 /* 20 extra chars is insufficient for VMS, since we might perform a
837 logical name translation. an equivalence string can be up to 255
838 chars long, so grab that much extra space... - sss */
839 buf
= (char *) alloca (SBYTES (directory
) + 20 + 255);
841 buf
= (char *) alloca (SBYTES (directory
) + 20);
843 directory_file_name (SDATA (directory
), buf
);
844 return make_specified_string (buf
, -1, strlen (buf
),
845 STRING_MULTIBYTE (directory
));
848 static char make_temp_name_tbl
[64] =
850 'A','B','C','D','E','F','G','H',
851 'I','J','K','L','M','N','O','P',
852 'Q','R','S','T','U','V','W','X',
853 'Y','Z','a','b','c','d','e','f',
854 'g','h','i','j','k','l','m','n',
855 'o','p','q','r','s','t','u','v',
856 'w','x','y','z','0','1','2','3',
857 '4','5','6','7','8','9','-','_'
860 static unsigned make_temp_name_count
, make_temp_name_count_initialized_p
;
862 /* Value is a temporary file name starting with PREFIX, a string.
864 The Emacs process number forms part of the result, so there is
865 no danger of generating a name being used by another process.
866 In addition, this function makes an attempt to choose a name
867 which has no existing file. To make this work, PREFIX should be
868 an absolute file name.
870 BASE64_P non-zero means add the pid as 3 characters in base64
871 encoding. In this case, 6 characters will be added to PREFIX to
872 form the file name. Otherwise, if Emacs is running on a system
873 with long file names, add the pid as a decimal number.
875 This function signals an error if no unique file name could be
879 make_temp_name (prefix
, base64_p
)
886 unsigned char *p
, *data
;
890 CHECK_STRING (prefix
);
892 /* VAL is created by adding 6 characters to PREFIX. The first
893 three are the PID of this process, in base 64, and the second
894 three are incremented if the file already exists. This ensures
895 262144 unique file names per PID per PREFIX. */
897 pid
= (int) getpid ();
901 pidbuf
[0] = make_temp_name_tbl
[pid
& 63], pid
>>= 6;
902 pidbuf
[1] = make_temp_name_tbl
[pid
& 63], pid
>>= 6;
903 pidbuf
[2] = make_temp_name_tbl
[pid
& 63], pid
>>= 6;
908 #ifdef HAVE_LONG_FILE_NAMES
909 sprintf (pidbuf
, "%d", pid
);
910 pidlen
= strlen (pidbuf
);
912 pidbuf
[0] = make_temp_name_tbl
[pid
& 63], pid
>>= 6;
913 pidbuf
[1] = make_temp_name_tbl
[pid
& 63], pid
>>= 6;
914 pidbuf
[2] = make_temp_name_tbl
[pid
& 63], pid
>>= 6;
919 len
= SCHARS (prefix
);
920 val
= make_uninit_string (len
+ 3 + pidlen
);
922 bcopy(SDATA (prefix
), data
, len
);
925 bcopy (pidbuf
, p
, pidlen
);
928 /* Here we try to minimize useless stat'ing when this function is
929 invoked many times successively with the same PREFIX. We achieve
930 this by initializing count to a random value, and incrementing it
933 We don't want make-temp-name to be called while dumping,
934 because then make_temp_name_count_initialized_p would get set
935 and then make_temp_name_count would not be set when Emacs starts. */
937 if (!make_temp_name_count_initialized_p
)
939 make_temp_name_count
= (unsigned) time (NULL
);
940 make_temp_name_count_initialized_p
= 1;
946 unsigned num
= make_temp_name_count
;
948 p
[0] = make_temp_name_tbl
[num
& 63], num
>>= 6;
949 p
[1] = make_temp_name_tbl
[num
& 63], num
>>= 6;
950 p
[2] = make_temp_name_tbl
[num
& 63], num
>>= 6;
952 /* Poor man's congruential RN generator. Replace with
953 ++make_temp_name_count for debugging. */
954 make_temp_name_count
+= 25229;
955 make_temp_name_count
%= 225307;
957 if (stat (data
, &ignored
) < 0)
959 /* We want to return only if errno is ENOENT. */
963 /* The error here is dubious, but there is little else we
964 can do. The alternatives are to return nil, which is
965 as bad as (and in many cases worse than) throwing the
966 error, or to ignore the error, which will likely result
967 in looping through 225307 stat's, which is not only
968 dog-slow, but also useless since it will fallback to
969 the errow below, anyway. */
970 report_file_error ("Cannot create temporary name for prefix",
971 Fcons (prefix
, Qnil
));
976 error ("Cannot create temporary name for prefix `%s'",
982 DEFUN ("make-temp-name", Fmake_temp_name
, Smake_temp_name
, 1, 1, 0,
983 doc
: /* Generate temporary file name (string) starting with PREFIX (a string).
984 The Emacs process number forms part of the result,
985 so there is no danger of generating a name being used by another process.
987 In addition, this function makes an attempt to choose a name
988 which has no existing file. To make this work,
989 PREFIX should be an absolute file name.
991 There is a race condition between calling `make-temp-name' and creating the
992 file which opens all kinds of security holes. For that reason, you should
993 probably use `make-temp-file' instead, except in three circumstances:
995 * If you are creating the file in the user's home directory.
996 * If you are creating a directory rather than an ordinary file.
997 * If you are taking special precautions as `make-temp-file' does. */)
1001 return make_temp_name (prefix
, 0);
1006 DEFUN ("expand-file-name", Fexpand_file_name
, Sexpand_file_name
, 1, 2, 0,
1007 doc
: /* Convert filename NAME to absolute, and canonicalize it.
1008 Second arg DEFAULT-DIRECTORY is directory to start with if NAME is relative
1009 (does not start with slash); if DEFAULT-DIRECTORY is nil or missing,
1010 the current buffer's value of default-directory is used.
1011 File name components that are `.' are removed, and
1012 so are file name components followed by `..', along with the `..' itself;
1013 note that these simplifications are done without checking the resulting
1014 file names in the file system.
1015 An initial `~/' expands to your home directory.
1016 An initial `~USER/' expands to USER's home directory.
1017 See also the function `substitute-in-file-name'. */)
1018 (name
, default_directory
)
1019 Lisp_Object name
, default_directory
;
1023 register unsigned char *newdir
, *p
, *o
;
1025 unsigned char *target
;
1028 unsigned char * colon
= 0;
1029 unsigned char * close
= 0;
1030 unsigned char * slash
= 0;
1031 unsigned char * brack
= 0;
1032 int lbrack
= 0, rbrack
= 0;
1037 int collapse_newdir
= 1;
1041 Lisp_Object handler
, result
;
1043 CHECK_STRING (name
);
1045 /* If the file name has special constructs in it,
1046 call the corresponding file handler. */
1047 handler
= Ffind_file_name_handler (name
, Qexpand_file_name
);
1048 if (!NILP (handler
))
1049 return call3 (handler
, Qexpand_file_name
, name
, default_directory
);
1051 /* Use the buffer's default-directory if DEFAULT_DIRECTORY is omitted. */
1052 if (NILP (default_directory
))
1053 default_directory
= current_buffer
->directory
;
1054 if (! STRINGP (default_directory
))
1057 /* "/" is not considered a root directory on DOS_NT, so using "/"
1058 here causes an infinite recursion in, e.g., the following:
1060 (let (default-directory)
1061 (expand-file-name "a"))
1063 To avoid this, we set default_directory to the root of the
1065 extern char *emacs_root_dir (void);
1067 default_directory
= build_string (emacs_root_dir ());
1069 default_directory
= build_string ("/");
1073 if (!NILP (default_directory
))
1075 handler
= Ffind_file_name_handler (default_directory
, Qexpand_file_name
);
1076 if (!NILP (handler
))
1077 return call3 (handler
, Qexpand_file_name
, name
, default_directory
);
1080 o
= SDATA (default_directory
);
1082 /* Make sure DEFAULT_DIRECTORY is properly expanded.
1083 It would be better to do this down below where we actually use
1084 default_directory. Unfortunately, calling Fexpand_file_name recursively
1085 could invoke GC, and the strings might be relocated. This would
1086 be annoying because we have pointers into strings lying around
1087 that would need adjusting, and people would add new pointers to
1088 the code and forget to adjust them, resulting in intermittent bugs.
1089 Putting this call here avoids all that crud.
1091 The EQ test avoids infinite recursion. */
1092 if (! NILP (default_directory
) && !EQ (default_directory
, name
)
1093 /* Save time in some common cases - as long as default_directory
1094 is not relative, it can be canonicalized with name below (if it
1095 is needed at all) without requiring it to be expanded now. */
1097 /* Detect MSDOS file names with drive specifiers. */
1098 && ! (IS_DRIVE (o
[0]) && IS_DEVICE_SEP (o
[1]) && IS_DIRECTORY_SEP (o
[2]))
1100 /* Detect Windows file names in UNC format. */
1101 && ! (IS_DIRECTORY_SEP (o
[0]) && IS_DIRECTORY_SEP (o
[1]))
1103 #else /* not DOS_NT */
1104 /* Detect Unix absolute file names (/... alone is not absolute on
1106 && ! (IS_DIRECTORY_SEP (o
[0]))
1107 #endif /* not DOS_NT */
1110 struct gcpro gcpro1
;
1113 default_directory
= Fexpand_file_name (default_directory
, Qnil
);
1118 /* Filenames on VMS are always upper case. */
1119 name
= Fupcase (name
);
1121 #ifdef FILE_SYSTEM_CASE
1122 name
= FILE_SYSTEM_CASE (name
);
1128 /* We will force directory separators to be either all \ or /, so make
1129 a local copy to modify, even if there ends up being no change. */
1130 nm
= strcpy (alloca (strlen (nm
) + 1), nm
);
1132 /* Note if special escape prefix is present, but remove for now. */
1133 if (nm
[0] == '/' && nm
[1] == ':')
1139 /* Find and remove drive specifier if present; this makes nm absolute
1140 even if the rest of the name appears to be relative. Only look for
1141 drive specifier at the beginning. */
1142 if (IS_DRIVE (nm
[0]) && IS_DEVICE_SEP (nm
[1]))
1149 /* If we see "c://somedir", we want to strip the first slash after the
1150 colon when stripping the drive letter. Otherwise, this expands to
1152 if (drive
&& IS_DIRECTORY_SEP (nm
[0]) && IS_DIRECTORY_SEP (nm
[1]))
1154 #endif /* WINDOWSNT */
1158 /* Discard any previous drive specifier if nm is now in UNC format. */
1159 if (IS_DIRECTORY_SEP (nm
[0]) && IS_DIRECTORY_SEP (nm
[1]))
1165 /* If nm is absolute, look for `/./' or `/../' or `//''sequences; if
1166 none are found, we can probably return right away. We will avoid
1167 allocating a new string if name is already fully expanded. */
1169 IS_DIRECTORY_SEP (nm
[0])
1171 && drive
&& !is_escaped
1174 && (drive
|| IS_DIRECTORY_SEP (nm
[1])) && !is_escaped
1181 /* If it turns out that the filename we want to return is just a
1182 suffix of FILENAME, we don't need to go through and edit
1183 things; we just need to construct a new string using data
1184 starting at the middle of FILENAME. If we set lose to a
1185 non-zero value, that means we've discovered that we can't do
1192 /* Since we know the name is absolute, we can assume that each
1193 element starts with a "/". */
1195 /* "." and ".." are hairy. */
1196 if (IS_DIRECTORY_SEP (p
[0])
1198 && (IS_DIRECTORY_SEP (p
[2])
1200 || (p
[2] == '.' && (IS_DIRECTORY_SEP (p
[3])
1203 /* We want to replace multiple `/' in a row with a single
1206 && IS_DIRECTORY_SEP (p
[0])
1207 && IS_DIRECTORY_SEP (p
[1]))
1214 /* if dev:[dir]/, move nm to / */
1215 if (!slash
&& p
> nm
&& (brack
|| colon
)) {
1216 nm
= (brack
? brack
+ 1 : colon
+ 1);
1217 lbrack
= rbrack
= 0;
1225 /* VMS pre V4.4,convert '-'s in filenames. */
1226 if (lbrack
== rbrack
)
1228 if (dots
< 2) /* this is to allow negative version numbers */
1233 if (lbrack
> rbrack
&&
1234 ((p
[-1] == '.' || p
[-1] == '[' || p
[-1] == '<') &&
1235 (p
[1] == '.' || p
[1] == ']' || p
[1] == '>')))
1241 /* count open brackets, reset close bracket pointer */
1242 if (p
[0] == '[' || p
[0] == '<')
1243 lbrack
++, brack
= 0;
1244 /* count close brackets, set close bracket pointer */
1245 if (p
[0] == ']' || p
[0] == '>')
1246 rbrack
++, brack
= p
;
1247 /* detect ][ or >< */
1248 if ((p
[0] == ']' || p
[0] == '>') && (p
[1] == '[' || p
[1] == '<'))
1250 if ((p
[0] == ':' || p
[0] == ']' || p
[0] == '>') && p
[1] == '~')
1251 nm
= p
+ 1, lose
= 1;
1252 if (p
[0] == ':' && (colon
|| slash
))
1253 /* if dev1:[dir]dev2:, move nm to dev2: */
1259 /* if /name/dev:, move nm to dev: */
1262 /* if node::dev:, move colon following dev */
1263 else if (colon
&& colon
[-1] == ':')
1265 /* if dev1:dev2:, move nm to dev2: */
1266 else if (colon
&& colon
[-1] != ':')
1271 if (p
[0] == ':' && !colon
)
1277 if (lbrack
== rbrack
)
1280 else if (p
[0] == '.')
1288 if (index (nm
, '/'))
1290 nm
= sys_translate_unix (nm
);
1291 return make_specified_string (nm
, -1, strlen (nm
),
1292 STRING_MULTIBYTE (name
));
1296 /* Make sure directories are all separated with / or \ as
1297 desired, but avoid allocation of a new string when not
1299 CORRECT_DIR_SEPS (nm
);
1301 if (IS_DIRECTORY_SEP (nm
[1]))
1303 if (strcmp (nm
, SDATA (name
)) != 0)
1304 name
= make_specified_string (nm
, -1, strlen (nm
),
1305 STRING_MULTIBYTE (name
));
1309 /* drive must be set, so this is okay */
1310 if (strcmp (nm
- 2, SDATA (name
)) != 0)
1314 name
= make_specified_string (nm
, -1, p
- nm
,
1315 STRING_MULTIBYTE (name
));
1316 temp
[0] = DRIVE_LETTER (drive
);
1317 name
= concat2 (build_string (temp
), name
);
1320 #else /* not DOS_NT */
1321 if (nm
== SDATA (name
))
1323 return make_specified_string (nm
, -1, strlen (nm
),
1324 STRING_MULTIBYTE (name
));
1325 #endif /* not DOS_NT */
1329 /* At this point, nm might or might not be an absolute file name. We
1330 need to expand ~ or ~user if present, otherwise prefix nm with
1331 default_directory if nm is not absolute, and finally collapse /./
1332 and /foo/../ sequences.
1334 We set newdir to be the appropriate prefix if one is needed:
1335 - the relevant user directory if nm starts with ~ or ~user
1336 - the specified drive's working dir (DOS/NT only) if nm does not
1338 - the value of default_directory.
1340 Note that these prefixes are not guaranteed to be absolute (except
1341 for the working dir of a drive). Therefore, to ensure we always
1342 return an absolute name, if the final prefix is not absolute we
1343 append it to the current working directory. */
1347 if (nm
[0] == '~') /* prefix ~ */
1349 if (IS_DIRECTORY_SEP (nm
[1])
1353 || nm
[1] == 0) /* ~ by itself */
1355 if (!(newdir
= (unsigned char *) egetenv ("HOME")))
1356 newdir
= (unsigned char *) "";
1359 collapse_newdir
= 0;
1362 nm
++; /* Don't leave the slash in nm. */
1365 else /* ~user/filename */
1367 for (p
= nm
; *p
&& (!IS_DIRECTORY_SEP (*p
)
1372 o
= (unsigned char *) alloca (p
- nm
+ 1);
1373 bcopy ((char *) nm
, o
, p
- nm
);
1376 pw
= (struct passwd
*) getpwnam (o
+ 1);
1379 newdir
= (unsigned char *) pw
-> pw_dir
;
1381 nm
= p
+ 1; /* skip the terminator */
1385 collapse_newdir
= 0;
1390 /* If we don't find a user of that name, leave the name
1391 unchanged; don't move nm forward to p. */
1396 /* On DOS and Windows, nm is absolute if a drive name was specified;
1397 use the drive's current directory as the prefix if needed. */
1398 if (!newdir
&& drive
)
1400 /* Get default directory if needed to make nm absolute. */
1401 if (!IS_DIRECTORY_SEP (nm
[0]))
1403 newdir
= alloca (MAXPATHLEN
+ 1);
1404 if (!getdefdir (toupper (drive
) - 'A' + 1, newdir
))
1409 /* Either nm starts with /, or drive isn't mounted. */
1410 newdir
= alloca (4);
1411 newdir
[0] = DRIVE_LETTER (drive
);
1419 /* Finally, if no prefix has been specified and nm is not absolute,
1420 then it must be expanded relative to default_directory. */
1424 /* /... alone is not absolute on DOS and Windows. */
1425 && !IS_DIRECTORY_SEP (nm
[0])
1428 && !(IS_DIRECTORY_SEP (nm
[0]) && IS_DIRECTORY_SEP (nm
[1]))
1435 newdir
= SDATA (default_directory
);
1437 /* Note if special escape prefix is present, but remove for now. */
1438 if (newdir
[0] == '/' && newdir
[1] == ':')
1449 /* First ensure newdir is an absolute name. */
1451 /* Detect MSDOS file names with drive specifiers. */
1452 ! (IS_DRIVE (newdir
[0])
1453 && IS_DEVICE_SEP (newdir
[1]) && IS_DIRECTORY_SEP (newdir
[2]))
1455 /* Detect Windows file names in UNC format. */
1456 && ! (IS_DIRECTORY_SEP (newdir
[0]) && IS_DIRECTORY_SEP (newdir
[1]))
1460 /* Effectively, let newdir be (expand-file-name newdir cwd).
1461 Because of the admonition against calling expand-file-name
1462 when we have pointers into lisp strings, we accomplish this
1463 indirectly by prepending newdir to nm if necessary, and using
1464 cwd (or the wd of newdir's drive) as the new newdir. */
1466 if (IS_DRIVE (newdir
[0]) && newdir
[1] == ':')
1471 if (!IS_DIRECTORY_SEP (nm
[0]))
1473 char * tmp
= alloca (strlen (newdir
) + strlen (nm
) + 2);
1474 file_name_as_directory (tmp
, newdir
);
1478 newdir
= alloca (MAXPATHLEN
+ 1);
1481 if (!getdefdir (toupper (drive
) - 'A' + 1, newdir
))
1488 /* Strip off drive name from prefix, if present. */
1489 if (IS_DRIVE (newdir
[0]) && newdir
[1] == ':')
1495 /* Keep only a prefix from newdir if nm starts with slash
1496 (//server/share for UNC, nothing otherwise). */
1497 if (IS_DIRECTORY_SEP (nm
[0]) && collapse_newdir
)
1500 if (IS_DIRECTORY_SEP (newdir
[0]) && IS_DIRECTORY_SEP (newdir
[1]))
1502 newdir
= strcpy (alloca (strlen (newdir
) + 1), newdir
);
1504 while (*p
&& !IS_DIRECTORY_SEP (*p
)) p
++;
1506 while (*p
&& !IS_DIRECTORY_SEP (*p
)) p
++;
1518 /* Get rid of any slash at the end of newdir, unless newdir is
1519 just / or // (an incomplete UNC name). */
1520 length
= strlen (newdir
);
1521 if (length
> 1 && IS_DIRECTORY_SEP (newdir
[length
- 1])
1523 && !(length
== 2 && IS_DIRECTORY_SEP (newdir
[0]))
1527 unsigned char *temp
= (unsigned char *) alloca (length
);
1528 bcopy (newdir
, temp
, length
- 1);
1529 temp
[length
- 1] = 0;
1537 /* Now concatenate the directory and name to new space in the stack frame */
1538 tlen
+= strlen (nm
) + 1;
1540 /* Reserve space for drive specifier and escape prefix, since either
1541 or both may need to be inserted. (The Microsoft x86 compiler
1542 produces incorrect code if the following two lines are combined.) */
1543 target
= (unsigned char *) alloca (tlen
+ 4);
1545 #else /* not DOS_NT */
1546 target
= (unsigned char *) alloca (tlen
);
1547 #endif /* not DOS_NT */
1553 if (nm
[0] == 0 || IS_DIRECTORY_SEP (nm
[0]))
1556 /* If newdir is effectively "C:/", then the drive letter will have
1557 been stripped and newdir will be "/". Concatenating with an
1558 absolute directory in nm produces "//", which will then be
1559 incorrectly treated as a network share. Ignore newdir in
1560 this case (keeping the drive letter). */
1561 if (!(drive
&& nm
[0] && IS_DIRECTORY_SEP (newdir
[0])
1562 && newdir
[1] == '\0'))
1564 strcpy (target
, newdir
);
1568 file_name_as_directory (target
, newdir
);
1571 strcat (target
, nm
);
1573 if (index (target
, '/'))
1574 strcpy (target
, sys_translate_unix (target
));
1577 /* ASSERT (IS_DIRECTORY_SEP (target[0])) if not VMS */
1579 /* Now canonicalize by removing `//', `/.' and `/foo/..' if they
1588 if (*p
!= ']' && *p
!= '>' && *p
!= '-')
1594 else if ((p
[0] == ']' || p
[0] == '>') && p
[0] == p
[1] + 2)
1595 /* brackets are offset from each other by 2 */
1598 if (*p
!= '.' && *p
!= '-' && o
[-1] != '.')
1599 /* convert [foo][bar] to [bar] */
1600 while (o
[-1] != '[' && o
[-1] != '<')
1602 else if (*p
== '-' && *o
!= '.')
1605 else if (p
[0] == '-' && o
[-1] == '.' &&
1606 (p
[1] == '.' || p
[1] == ']' || p
[1] == '>'))
1607 /* flush .foo.- ; leave - if stopped by '[' or '<' */
1611 while (o
[-1] != '.' && o
[-1] != '[' && o
[-1] != '<');
1612 if (p
[1] == '.') /* foo.-.bar ==> bar. */
1614 else if (o
[-1] == '.') /* '.foo.-]' ==> ']' */
1616 /* else [foo.-] ==> [-] */
1622 o
[-1] != '[' && o
[-1] != '<' && o
[-1] != '.' &&
1623 p
[1] != ']' && p
[1] != '>' && p
[1] != '.')
1629 if (!IS_DIRECTORY_SEP (*p
))
1633 else if (IS_DIRECTORY_SEP (p
[0])
1635 && (IS_DIRECTORY_SEP (p
[2])
1638 /* If "/." is the entire filename, keep the "/". Otherwise,
1639 just delete the whole "/.". */
1640 if (o
== target
&& p
[2] == '\0')
1644 else if (IS_DIRECTORY_SEP (p
[0]) && p
[1] == '.' && p
[2] == '.'
1645 /* `/../' is the "superroot" on certain file systems. */
1647 && (IS_DIRECTORY_SEP (p
[3]) || p
[3] == 0))
1649 while (o
!= target
&& (--o
) && !IS_DIRECTORY_SEP (*o
))
1651 /* Keep initial / only if this is the whole name. */
1652 if (o
== target
&& IS_ANY_SEP (*o
) && p
[3] == 0)
1657 && IS_DIRECTORY_SEP (p
[0]) && IS_DIRECTORY_SEP (p
[1]))
1659 /* Collapse multiple `/' in a row. */
1661 while (IS_DIRECTORY_SEP (*p
))
1668 #endif /* not VMS */
1672 /* At last, set drive name. */
1674 /* Except for network file name. */
1675 if (!(IS_DIRECTORY_SEP (target
[0]) && IS_DIRECTORY_SEP (target
[1])))
1676 #endif /* WINDOWSNT */
1678 if (!drive
) abort ();
1680 target
[0] = DRIVE_LETTER (drive
);
1683 /* Reinsert the escape prefix if required. */
1690 CORRECT_DIR_SEPS (target
);
1693 result
= make_specified_string (target
, -1, o
- target
,
1694 STRING_MULTIBYTE (name
));
1696 /* Again look to see if the file name has special constructs in it
1697 and perhaps call the corresponding file handler. This is needed
1698 for filenames such as "/foo/../user@host:/bar/../baz". Expanding
1699 the ".." component gives us "/user@host:/bar/../baz" which needs
1700 to be expanded again. */
1701 handler
= Ffind_file_name_handler (result
, Qexpand_file_name
);
1702 if (!NILP (handler
))
1703 return call3 (handler
, Qexpand_file_name
, result
, default_directory
);
1709 /* PLEASE DO NOT DELETE THIS COMMENTED-OUT VERSION!
1710 This is the old version of expand-file-name, before it was thoroughly
1711 rewritten for Emacs 10.31. We leave this version here commented-out,
1712 because the code is very complex and likely to have subtle bugs. If
1713 bugs _are_ found, it might be of interest to look at the old code and
1714 see what did it do in the relevant situation.
1716 Don't remove this code: it's true that it will be accessible via CVS,
1717 but a few years from deletion, people will forget it is there. */
1719 /* Changed this DEFUN to a DEAFUN, so as not to confuse `make-docfile'. */
1720 DEAFUN ("expand-file-name", Fexpand_file_name
, Sexpand_file_name
, 1, 2, 0,
1721 "Convert FILENAME to absolute, and canonicalize it.\n\
1722 Second arg DEFAULT is directory to start with if FILENAME is relative\n\
1723 (does not start with slash); if DEFAULT is nil or missing,\n\
1724 the current buffer's value of default-directory is used.\n\
1725 Filenames containing `.' or `..' as components are simplified;\n\
1726 initial `~/' expands to your home directory.\n\
1727 See also the function `substitute-in-file-name'.")
1729 Lisp_Object name
, defalt
;
1733 register unsigned char *newdir
, *p
, *o
;
1735 unsigned char *target
;
1739 unsigned char * colon
= 0;
1740 unsigned char * close
= 0;
1741 unsigned char * slash
= 0;
1742 unsigned char * brack
= 0;
1743 int lbrack
= 0, rbrack
= 0;
1747 CHECK_STRING (name
);
1750 /* Filenames on VMS are always upper case. */
1751 name
= Fupcase (name
);
1756 /* If nm is absolute, flush ...// and detect /./ and /../.
1757 If no /./ or /../ we can return right away. */
1769 if (p
[0] == '/' && p
[1] == '/'
1771 /* // at start of filename is meaningful on Apollo system. */
1776 if (p
[0] == '/' && p
[1] == '~')
1777 nm
= p
+ 1, lose
= 1;
1778 if (p
[0] == '/' && p
[1] == '.'
1779 && (p
[2] == '/' || p
[2] == 0
1780 || (p
[2] == '.' && (p
[3] == '/' || p
[3] == 0))))
1786 /* if dev:[dir]/, move nm to / */
1787 if (!slash
&& p
> nm
&& (brack
|| colon
)) {
1788 nm
= (brack
? brack
+ 1 : colon
+ 1);
1789 lbrack
= rbrack
= 0;
1797 /* VMS pre V4.4,convert '-'s in filenames. */
1798 if (lbrack
== rbrack
)
1800 if (dots
< 2) /* this is to allow negative version numbers */
1805 if (lbrack
> rbrack
&&
1806 ((p
[-1] == '.' || p
[-1] == '[' || p
[-1] == '<') &&
1807 (p
[1] == '.' || p
[1] == ']' || p
[1] == '>')))
1813 /* count open brackets, reset close bracket pointer */
1814 if (p
[0] == '[' || p
[0] == '<')
1815 lbrack
++, brack
= 0;
1816 /* count close brackets, set close bracket pointer */
1817 if (p
[0] == ']' || p
[0] == '>')
1818 rbrack
++, brack
= p
;
1819 /* detect ][ or >< */
1820 if ((p
[0] == ']' || p
[0] == '>') && (p
[1] == '[' || p
[1] == '<'))
1822 if ((p
[0] == ':' || p
[0] == ']' || p
[0] == '>') && p
[1] == '~')
1823 nm
= p
+ 1, lose
= 1;
1824 if (p
[0] == ':' && (colon
|| slash
))
1825 /* if dev1:[dir]dev2:, move nm to dev2: */
1831 /* If /name/dev:, move nm to dev: */
1834 /* If node::dev:, move colon following dev */
1835 else if (colon
&& colon
[-1] == ':')
1837 /* If dev1:dev2:, move nm to dev2: */
1838 else if (colon
&& colon
[-1] != ':')
1843 if (p
[0] == ':' && !colon
)
1849 if (lbrack
== rbrack
)
1852 else if (p
[0] == '.')
1860 if (index (nm
, '/'))
1861 return build_string (sys_translate_unix (nm
));
1863 if (nm
== SDATA (name
))
1865 return build_string (nm
);
1869 /* Now determine directory to start with and put it in NEWDIR */
1873 if (nm
[0] == '~') /* prefix ~ */
1878 || nm
[1] == 0)/* ~/filename */
1880 if (!(newdir
= (unsigned char *) egetenv ("HOME")))
1881 newdir
= (unsigned char *) "";
1884 nm
++; /* Don't leave the slash in nm. */
1887 else /* ~user/filename */
1889 /* Get past ~ to user */
1890 unsigned char *user
= nm
+ 1;
1891 /* Find end of name. */
1892 unsigned char *ptr
= (unsigned char *) index (user
, '/');
1893 int len
= ptr
? ptr
- user
: strlen (user
);
1895 unsigned char *ptr1
= index (user
, ':');
1896 if (ptr1
!= 0 && ptr1
- user
< len
)
1899 /* Copy the user name into temp storage. */
1900 o
= (unsigned char *) alloca (len
+ 1);
1901 bcopy ((char *) user
, o
, len
);
1904 /* Look up the user name. */
1905 pw
= (struct passwd
*) getpwnam (o
+ 1);
1907 error ("\"%s\" isn't a registered user", o
+ 1);
1909 newdir
= (unsigned char *) pw
->pw_dir
;
1911 /* Discard the user name from NM. */
1918 #endif /* not VMS */
1922 defalt
= current_buffer
->directory
;
1923 CHECK_STRING (defalt
);
1924 newdir
= SDATA (defalt
);
1927 /* Now concatenate the directory and name to new space in the stack frame */
1929 tlen
= (newdir
? strlen (newdir
) + 1 : 0) + strlen (nm
) + 1;
1930 target
= (unsigned char *) alloca (tlen
);
1936 if (nm
[0] == 0 || nm
[0] == '/')
1937 strcpy (target
, newdir
);
1940 file_name_as_directory (target
, newdir
);
1943 strcat (target
, nm
);
1945 if (index (target
, '/'))
1946 strcpy (target
, sys_translate_unix (target
));
1949 /* Now canonicalize by removing /. and /foo/.. if they appear */
1957 if (*p
!= ']' && *p
!= '>' && *p
!= '-')
1963 else if ((p
[0] == ']' || p
[0] == '>') && p
[0] == p
[1] + 2)
1964 /* brackets are offset from each other by 2 */
1967 if (*p
!= '.' && *p
!= '-' && o
[-1] != '.')
1968 /* convert [foo][bar] to [bar] */
1969 while (o
[-1] != '[' && o
[-1] != '<')
1971 else if (*p
== '-' && *o
!= '.')
1974 else if (p
[0] == '-' && o
[-1] == '.' &&
1975 (p
[1] == '.' || p
[1] == ']' || p
[1] == '>'))
1976 /* flush .foo.- ; leave - if stopped by '[' or '<' */
1980 while (o
[-1] != '.' && o
[-1] != '[' && o
[-1] != '<');
1981 if (p
[1] == '.') /* foo.-.bar ==> bar. */
1983 else if (o
[-1] == '.') /* '.foo.-]' ==> ']' */
1985 /* else [foo.-] ==> [-] */
1991 o
[-1] != '[' && o
[-1] != '<' && o
[-1] != '.' &&
1992 p
[1] != ']' && p
[1] != '>' && p
[1] != '.')
2002 else if (!strncmp (p
, "//", 2)
2004 /* // at start of filename is meaningful in Apollo system. */
2012 else if (p
[0] == '/' && p
[1] == '.' &&
2013 (p
[2] == '/' || p
[2] == 0))
2015 else if (!strncmp (p
, "/..", 3)
2016 /* `/../' is the "superroot" on certain file systems. */
2018 && (p
[3] == '/' || p
[3] == 0))
2020 while (o
!= target
&& *--o
!= '/')
2023 if (o
== target
+ 1 && o
[-1] == '/' && o
[0] == '/')
2027 if (o
== target
&& *o
== '/')
2035 #endif /* not VMS */
2038 return make_string (target
, o
- target
);
2042 DEFUN ("substitute-in-file-name", Fsubstitute_in_file_name
,
2043 Ssubstitute_in_file_name
, 1, 1, 0,
2044 doc
: /* Substitute environment variables referred to in FILENAME.
2045 `$FOO' where FOO is an environment variable name means to substitute
2046 the value of that variable. The variable name should be terminated
2047 with a character not a letter, digit or underscore; otherwise, enclose
2048 the entire variable name in braces.
2049 If `/~' appears, all of FILENAME through that `/' is discarded.
2051 On VMS, `$' substitution is not done; this function does little and only
2052 duplicates what `expand-file-name' does. */)
2054 Lisp_Object filename
;
2058 register unsigned char *s
, *p
, *o
, *x
, *endp
;
2059 unsigned char *target
= NULL
;
2061 int substituted
= 0;
2064 Lisp_Object handler
;
2066 CHECK_STRING (filename
);
2068 /* If the file name has special constructs in it,
2069 call the corresponding file handler. */
2070 handler
= Ffind_file_name_handler (filename
, Qsubstitute_in_file_name
);
2071 if (!NILP (handler
))
2072 return call2 (handler
, Qsubstitute_in_file_name
, filename
);
2074 nm
= SDATA (filename
);
2076 nm
= strcpy (alloca (strlen (nm
) + 1), nm
);
2077 CORRECT_DIR_SEPS (nm
);
2078 substituted
= (strcmp (nm
, SDATA (filename
)) != 0);
2080 endp
= nm
+ SBYTES (filename
);
2082 /* If /~ or // appears, discard everything through first slash. */
2084 for (p
= nm
; p
!= endp
; p
++)
2087 #if defined (APOLLO) || defined (WINDOWSNT) || defined(CYGWIN)
2088 /* // at start of file name is meaningful in Apollo,
2089 WindowsNT and Cygwin systems. */
2090 || (IS_DIRECTORY_SEP (p
[0]) && p
- 1 != nm
)
2091 #else /* not (APOLLO || WINDOWSNT || CYGWIN) */
2092 || IS_DIRECTORY_SEP (p
[0])
2093 #endif /* not (APOLLO || WINDOWSNT || CYGWIN) */
2098 || p
[-1] == ':' || p
[-1] == ']' || p
[-1] == '>'
2100 || IS_DIRECTORY_SEP (p
[-1])))
2102 for (s
= p
; *s
&& (!IS_DIRECTORY_SEP (*s
)
2107 if (p
[0] == '~' && s
> p
+ 1) /* we've got "/~something/" */
2109 o
= (unsigned char *) alloca (s
- p
+ 1);
2110 bcopy ((char *) p
, o
, s
- p
);
2113 pw
= (struct passwd
*) getpwnam (o
+ 1);
2115 /* If we have ~/ or ~user and `user' exists, discard
2116 everything up to ~. But if `user' does not exist, leave
2117 ~user alone, it might be a literal file name. */
2118 if (IS_DIRECTORY_SEP (p
[0]) || s
== p
+ 1 || pw
)
2125 /* see comment in expand-file-name about drive specifiers */
2126 else if (IS_DRIVE (p
[0]) && p
[1] == ':'
2127 && p
> nm
&& IS_DIRECTORY_SEP (p
[-1]))
2136 return make_specified_string (nm
, -1, strlen (nm
),
2137 STRING_MULTIBYTE (filename
));
2140 /* See if any variables are substituted into the string
2141 and find the total length of their values in `total' */
2143 for (p
= nm
; p
!= endp
;)
2153 /* "$$" means a single "$" */
2162 while (p
!= endp
&& *p
!= '}') p
++;
2163 if (*p
!= '}') goto missingclose
;
2169 while (p
!= endp
&& (isalnum (*p
) || *p
== '_')) p
++;
2173 /* Copy out the variable name */
2174 target
= (unsigned char *) alloca (s
- o
+ 1);
2175 strncpy (target
, o
, s
- o
);
2178 strupr (target
); /* $home == $HOME etc. */
2181 /* Get variable value */
2182 o
= (unsigned char *) egetenv (target
);
2185 total
+= strlen (o
);
2195 /* If substitution required, recopy the string and do it */
2196 /* Make space in stack frame for the new copy */
2197 xnm
= (unsigned char *) alloca (SBYTES (filename
) + total
+ 1);
2200 /* Copy the rest of the name through, replacing $ constructs with values */
2217 while (p
!= endp
&& *p
!= '}') p
++;
2218 if (*p
!= '}') goto missingclose
;
2224 while (p
!= endp
&& (isalnum (*p
) || *p
== '_')) p
++;
2228 /* Copy out the variable name */
2229 target
= (unsigned char *) alloca (s
- o
+ 1);
2230 strncpy (target
, o
, s
- o
);
2233 strupr (target
); /* $home == $HOME etc. */
2236 /* Get variable value */
2237 o
= (unsigned char *) egetenv (target
);
2241 strcpy (x
, target
); x
+= strlen (target
);
2243 else if (STRING_MULTIBYTE (filename
))
2245 /* If the original string is multibyte,
2246 convert what we substitute into multibyte. */
2249 int c
= unibyte_char_to_multibyte (*o
++);
2250 x
+= CHAR_STRING (c
, x
);
2262 /* If /~ or // appears, discard everything through first slash. */
2264 for (p
= xnm
; p
!= x
; p
++)
2266 #if defined (APOLLO) || defined (WINDOWSNT) || defined(CYGWIN)
2267 || (IS_DIRECTORY_SEP (p
[0]) && p
- 1 != xnm
)
2268 #else /* not (APOLLO || WINDOWSNT || CYGWIN) */
2269 || IS_DIRECTORY_SEP (p
[0])
2270 #endif /* not (APOLLO || WINDOWSNT || CYGWIN) */
2272 && p
!= xnm
&& IS_DIRECTORY_SEP (p
[-1]))
2275 else if (IS_DRIVE (p
[0]) && p
[1] == ':'
2276 && p
> xnm
&& IS_DIRECTORY_SEP (p
[-1]))
2280 return make_specified_string (xnm
, -1, x
- xnm
, STRING_MULTIBYTE (filename
));
2283 error ("Bad format environment-variable substitution");
2285 error ("Missing \"}\" in environment-variable substitution");
2287 error ("Substituting nonexistent environment variable \"%s\"", target
);
2290 #endif /* not VMS */
2294 /* A slightly faster and more convenient way to get
2295 (directory-file-name (expand-file-name FOO)). */
2298 expand_and_dir_to_file (filename
, defdir
)
2299 Lisp_Object filename
, defdir
;
2301 register Lisp_Object absname
;
2303 absname
= Fexpand_file_name (filename
, defdir
);
2306 register int c
= SREF (absname
, SBYTES (absname
) - 1);
2307 if (c
== ':' || c
== ']' || c
== '>')
2308 absname
= Fdirectory_file_name (absname
);
2311 /* Remove final slash, if any (unless this is the root dir).
2312 stat behaves differently depending! */
2313 if (SCHARS (absname
) > 1
2314 && IS_DIRECTORY_SEP (SREF (absname
, SBYTES (absname
) - 1))
2315 && !IS_DEVICE_SEP (SREF (absname
, SBYTES (absname
)-2)))
2316 /* We cannot take shortcuts; they might be wrong for magic file names. */
2317 absname
= Fdirectory_file_name (absname
);
2322 /* Signal an error if the file ABSNAME already exists.
2323 If INTERACTIVE is nonzero, ask the user whether to proceed,
2324 and bypass the error if the user says to go ahead.
2325 QUERYSTRING is a name for the action that is being considered
2328 *STATPTR is used to store the stat information if the file exists.
2329 If the file does not exist, STATPTR->st_mode is set to 0.
2330 If STATPTR is null, we don't store into it.
2332 If QUICK is nonzero, we ask for y or n, not yes or no. */
2335 barf_or_query_if_file_exists (absname
, querystring
, interactive
, statptr
, quick
)
2336 Lisp_Object absname
;
2337 unsigned char *querystring
;
2339 struct stat
*statptr
;
2342 register Lisp_Object tem
, encoded_filename
;
2343 struct stat statbuf
;
2344 struct gcpro gcpro1
;
2346 encoded_filename
= ENCODE_FILE (absname
);
2348 /* stat is a good way to tell whether the file exists,
2349 regardless of what access permissions it has. */
2350 if (stat (SDATA (encoded_filename
), &statbuf
) >= 0)
2353 Fsignal (Qfile_already_exists
,
2354 Fcons (build_string ("File already exists"),
2355 Fcons (absname
, Qnil
)));
2357 tem
= format2 ("File %s already exists; %s anyway? ",
2358 absname
, build_string (querystring
));
2360 tem
= Fy_or_n_p (tem
);
2362 tem
= do_yes_or_no_p (tem
);
2365 Fsignal (Qfile_already_exists
,
2366 Fcons (build_string ("File already exists"),
2367 Fcons (absname
, Qnil
)));
2374 statptr
->st_mode
= 0;
2379 DEFUN ("copy-file", Fcopy_file
, Scopy_file
, 2, 4,
2380 "fCopy file: \nFCopy %s to file: \np\nP",
2381 doc
: /* Copy FILE to NEWNAME. Both args must be strings.
2382 If NEWNAME names a directory, copy FILE there.
2383 Signals a `file-already-exists' error if file NEWNAME already exists,
2384 unless a third argument OK-IF-ALREADY-EXISTS is supplied and non-nil.
2385 A number as third arg means request confirmation if NEWNAME already exists.
2386 This is what happens in interactive use with M-x.
2387 Fourth arg KEEP-TIME non-nil means give the new file the same
2388 last-modified time as the old one. (This works on only some systems.)
2389 A prefix arg makes KEEP-TIME non-nil.
2390 Also set the file modes of the target file to match the source file. */)
2391 (file
, newname
, ok_if_already_exists
, keep_time
)
2392 Lisp_Object file
, newname
, ok_if_already_exists
, keep_time
;
2395 char buf
[16 * 1024];
2396 struct stat st
, out_st
;
2397 Lisp_Object handler
;
2398 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
2399 int count
= SPECPDL_INDEX ();
2400 int input_file_statable_p
;
2401 Lisp_Object encoded_file
, encoded_newname
;
2403 encoded_file
= encoded_newname
= Qnil
;
2404 GCPRO4 (file
, newname
, encoded_file
, encoded_newname
);
2405 CHECK_STRING (file
);
2406 CHECK_STRING (newname
);
2408 if (!NILP (Ffile_directory_p (newname
)))
2409 newname
= Fexpand_file_name (file
, newname
);
2411 newname
= Fexpand_file_name (newname
, Qnil
);
2413 file
= Fexpand_file_name (file
, Qnil
);
2415 /* If the input file name has special constructs in it,
2416 call the corresponding file handler. */
2417 handler
= Ffind_file_name_handler (file
, Qcopy_file
);
2418 /* Likewise for output file name. */
2420 handler
= Ffind_file_name_handler (newname
, Qcopy_file
);
2421 if (!NILP (handler
))
2422 RETURN_UNGCPRO (call5 (handler
, Qcopy_file
, file
, newname
,
2423 ok_if_already_exists
, keep_time
));
2425 encoded_file
= ENCODE_FILE (file
);
2426 encoded_newname
= ENCODE_FILE (newname
);
2428 if (NILP (ok_if_already_exists
)
2429 || INTEGERP (ok_if_already_exists
))
2430 barf_or_query_if_file_exists (encoded_newname
, "copy to it",
2431 INTEGERP (ok_if_already_exists
), &out_st
, 0);
2432 else if (stat (SDATA (encoded_newname
), &out_st
) < 0)
2436 if (!CopyFile (SDATA (encoded_file
),
2437 SDATA (encoded_newname
),
2439 report_file_error ("Copying file", Fcons (file
, Fcons (newname
, Qnil
)));
2440 /* CopyFile retains the timestamp by default. */
2441 else if (NILP (keep_time
))
2447 EMACS_GET_TIME (now
);
2448 filename
= SDATA (encoded_newname
);
2450 /* Ensure file is writable while its modified time is set. */
2451 attributes
= GetFileAttributes (filename
);
2452 SetFileAttributes (filename
, attributes
& ~FILE_ATTRIBUTE_READONLY
);
2453 if (set_file_times (filename
, now
, now
))
2455 /* Restore original attributes. */
2456 SetFileAttributes (filename
, attributes
);
2457 Fsignal (Qfile_date_error
,
2458 Fcons (build_string ("Cannot set file date"),
2459 Fcons (newname
, Qnil
)));
2461 /* Restore original attributes. */
2462 SetFileAttributes (filename
, attributes
);
2464 #else /* not WINDOWSNT */
2466 ifd
= emacs_open (SDATA (encoded_file
), O_RDONLY
, 0);
2470 report_file_error ("Opening input file", Fcons (file
, Qnil
));
2472 record_unwind_protect (close_file_unwind
, make_number (ifd
));
2474 /* We can only copy regular files and symbolic links. Other files are not
2476 input_file_statable_p
= (fstat (ifd
, &st
) >= 0);
2478 #if !defined (DOS_NT) || __DJGPP__ > 1
2479 if (out_st
.st_mode
!= 0
2480 && st
.st_dev
== out_st
.st_dev
&& st
.st_ino
== out_st
.st_ino
)
2483 report_file_error ("Input and output files are the same",
2484 Fcons (file
, Fcons (newname
, Qnil
)));
2488 #if defined (S_ISREG) && defined (S_ISLNK)
2489 if (input_file_statable_p
)
2491 if (!(S_ISREG (st
.st_mode
)) && !(S_ISLNK (st
.st_mode
)))
2493 #if defined (EISDIR)
2494 /* Get a better looking error message. */
2497 report_file_error ("Non-regular file", Fcons (file
, Qnil
));
2500 #endif /* S_ISREG && S_ISLNK */
2503 /* Create the copy file with the same record format as the input file */
2504 ofd
= sys_creat (SDATA (encoded_newname
), 0666, ifd
);
2507 /* System's default file type was set to binary by _fmode in emacs.c. */
2508 ofd
= creat (SDATA (encoded_newname
), S_IREAD
| S_IWRITE
);
2509 #else /* not MSDOS */
2510 ofd
= creat (SDATA (encoded_newname
), 0666);
2511 #endif /* not MSDOS */
2514 report_file_error ("Opening output file", Fcons (newname
, Qnil
));
2516 record_unwind_protect (close_file_unwind
, make_number (ofd
));
2520 while ((n
= emacs_read (ifd
, buf
, sizeof buf
)) > 0)
2521 if (emacs_write (ofd
, buf
, n
) != n
)
2522 report_file_error ("I/O error", Fcons (newname
, Qnil
));
2525 /* Closing the output clobbers the file times on some systems. */
2526 if (emacs_close (ofd
) < 0)
2527 report_file_error ("I/O error", Fcons (newname
, Qnil
));
2529 if (input_file_statable_p
)
2531 if (!NILP (keep_time
))
2533 EMACS_TIME atime
, mtime
;
2534 EMACS_SET_SECS_USECS (atime
, st
.st_atime
, 0);
2535 EMACS_SET_SECS_USECS (mtime
, st
.st_mtime
, 0);
2536 if (set_file_times (SDATA (encoded_newname
),
2538 Fsignal (Qfile_date_error
,
2539 Fcons (build_string ("Cannot set file date"),
2540 Fcons (newname
, Qnil
)));
2543 chmod (SDATA (encoded_newname
), st
.st_mode
& 07777);
2545 #if defined (__DJGPP__) && __DJGPP__ > 1
2546 /* In DJGPP v2.0 and later, fstat usually returns true file mode bits,
2547 and if it can't, it tells so. Otherwise, under MSDOS we usually
2548 get only the READ bit, which will make the copied file read-only,
2549 so it's better not to chmod at all. */
2550 if ((_djstat_flags
& _STFAIL_WRITEBIT
) == 0)
2551 chmod (SDATA (encoded_newname
), st
.st_mode
& 07777);
2552 #endif /* DJGPP version 2 or newer */
2557 #endif /* WINDOWSNT */
2559 /* Discard the unwind protects. */
2560 specpdl_ptr
= specpdl
+ count
;
2566 DEFUN ("make-directory-internal", Fmake_directory_internal
,
2567 Smake_directory_internal
, 1, 1, 0,
2568 doc
: /* Create a new directory named DIRECTORY. */)
2570 Lisp_Object directory
;
2572 const unsigned char *dir
;
2573 Lisp_Object handler
;
2574 Lisp_Object encoded_dir
;
2576 CHECK_STRING (directory
);
2577 directory
= Fexpand_file_name (directory
, Qnil
);
2579 handler
= Ffind_file_name_handler (directory
, Qmake_directory_internal
);
2580 if (!NILP (handler
))
2581 return call2 (handler
, Qmake_directory_internal
, directory
);
2583 encoded_dir
= ENCODE_FILE (directory
);
2585 dir
= SDATA (encoded_dir
);
2588 if (mkdir (dir
) != 0)
2590 if (mkdir (dir
, 0777) != 0)
2592 report_file_error ("Creating directory", Flist (1, &directory
));
2597 DEFUN ("delete-directory", Fdelete_directory
, Sdelete_directory
, 1, 1, "FDelete directory: ",
2598 doc
: /* Delete the directory named DIRECTORY. Does not follow symlinks. */)
2600 Lisp_Object directory
;
2602 const unsigned char *dir
;
2603 Lisp_Object handler
;
2604 Lisp_Object encoded_dir
;
2606 CHECK_STRING (directory
);
2607 directory
= Fdirectory_file_name (Fexpand_file_name (directory
, Qnil
));
2609 handler
= Ffind_file_name_handler (directory
, Qdelete_directory
);
2610 if (!NILP (handler
))
2611 return call2 (handler
, Qdelete_directory
, directory
);
2613 encoded_dir
= ENCODE_FILE (directory
);
2615 dir
= SDATA (encoded_dir
);
2617 if (rmdir (dir
) != 0)
2618 report_file_error ("Removing directory", Flist (1, &directory
));
2623 DEFUN ("delete-file", Fdelete_file
, Sdelete_file
, 1, 1, "fDelete file: ",
2624 doc
: /* Delete file named FILENAME. If it is a symlink, remove the symlink.
2625 If file has multiple names, it continues to exist with the other names. */)
2627 Lisp_Object filename
;
2629 Lisp_Object handler
;
2630 Lisp_Object encoded_file
;
2631 struct gcpro gcpro1
;
2634 if (!NILP (Ffile_directory_p (filename
))
2635 && NILP (Ffile_symlink_p (filename
)))
2636 Fsignal (Qfile_error
,
2637 Fcons (build_string ("Removing old name: is a directory"),
2638 Fcons (filename
, Qnil
)));
2640 filename
= Fexpand_file_name (filename
, Qnil
);
2642 handler
= Ffind_file_name_handler (filename
, Qdelete_file
);
2643 if (!NILP (handler
))
2644 return call2 (handler
, Qdelete_file
, filename
);
2646 encoded_file
= ENCODE_FILE (filename
);
2648 if (0 > unlink (SDATA (encoded_file
)))
2649 report_file_error ("Removing old name", Flist (1, &filename
));
2654 internal_delete_file_1 (ignore
)
2660 /* Delete file FILENAME, returning 1 if successful and 0 if failed. */
2663 internal_delete_file (filename
)
2664 Lisp_Object filename
;
2666 return NILP (internal_condition_case_1 (Fdelete_file
, filename
,
2667 Qt
, internal_delete_file_1
));
2670 DEFUN ("rename-file", Frename_file
, Srename_file
, 2, 3,
2671 "fRename file: \nFRename %s to file: \np",
2672 doc
: /* Rename FILE as NEWNAME. Both args strings.
2673 If file has names other than FILE, it continues to have those names.
2674 Signals a `file-already-exists' error if a file NEWNAME already exists
2675 unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.
2676 A number as third arg means request confirmation if NEWNAME already exists.
2677 This is what happens in interactive use with M-x. */)
2678 (file
, newname
, ok_if_already_exists
)
2679 Lisp_Object file
, newname
, ok_if_already_exists
;
2682 Lisp_Object args
[2];
2684 Lisp_Object handler
;
2685 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
2686 Lisp_Object encoded_file
, encoded_newname
;
2688 encoded_file
= encoded_newname
= Qnil
;
2689 GCPRO4 (file
, newname
, encoded_file
, encoded_newname
);
2690 CHECK_STRING (file
);
2691 CHECK_STRING (newname
);
2692 file
= Fexpand_file_name (file
, Qnil
);
2693 newname
= Fexpand_file_name (newname
, Qnil
);
2695 /* If the file name has special constructs in it,
2696 call the corresponding file handler. */
2697 handler
= Ffind_file_name_handler (file
, Qrename_file
);
2699 handler
= Ffind_file_name_handler (newname
, Qrename_file
);
2700 if (!NILP (handler
))
2701 RETURN_UNGCPRO (call4 (handler
, Qrename_file
,
2702 file
, newname
, ok_if_already_exists
));
2704 encoded_file
= ENCODE_FILE (file
);
2705 encoded_newname
= ENCODE_FILE (newname
);
2708 /* If the file names are identical but for the case, don't ask for
2709 confirmation: they simply want to change the letter-case of the
2711 if (NILP (Fstring_equal (Fdowncase (file
), Fdowncase (newname
))))
2713 if (NILP (ok_if_already_exists
)
2714 || INTEGERP (ok_if_already_exists
))
2715 barf_or_query_if_file_exists (encoded_newname
, "rename to it",
2716 INTEGERP (ok_if_already_exists
), 0, 0);
2718 if (0 > rename (SDATA (encoded_file
), SDATA (encoded_newname
)))
2720 if (0 > link (SDATA (encoded_file
), SDATA (encoded_newname
))
2721 || 0 > unlink (SDATA (encoded_file
)))
2726 Fcopy_file (file
, newname
,
2727 /* We have already prompted if it was an integer,
2728 so don't have copy-file prompt again. */
2729 NILP (ok_if_already_exists
) ? Qnil
: Qt
, Qt
);
2730 Fdelete_file (file
);
2737 report_file_error ("Renaming", Flist (2, args
));
2740 report_file_error ("Renaming", Flist (2, &file
));
2747 DEFUN ("add-name-to-file", Fadd_name_to_file
, Sadd_name_to_file
, 2, 3,
2748 "fAdd name to file: \nFName to add to %s: \np",
2749 doc
: /* Give FILE additional name NEWNAME. Both args strings.
2750 Signals a `file-already-exists' error if a file NEWNAME already exists
2751 unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.
2752 A number as third arg means request confirmation if NEWNAME already exists.
2753 This is what happens in interactive use with M-x. */)
2754 (file
, newname
, ok_if_already_exists
)
2755 Lisp_Object file
, newname
, ok_if_already_exists
;
2758 Lisp_Object args
[2];
2760 Lisp_Object handler
;
2761 Lisp_Object encoded_file
, encoded_newname
;
2762 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
2764 GCPRO4 (file
, newname
, encoded_file
, encoded_newname
);
2765 encoded_file
= encoded_newname
= Qnil
;
2766 CHECK_STRING (file
);
2767 CHECK_STRING (newname
);
2768 file
= Fexpand_file_name (file
, Qnil
);
2769 newname
= Fexpand_file_name (newname
, Qnil
);
2771 /* If the file name has special constructs in it,
2772 call the corresponding file handler. */
2773 handler
= Ffind_file_name_handler (file
, Qadd_name_to_file
);
2774 if (!NILP (handler
))
2775 RETURN_UNGCPRO (call4 (handler
, Qadd_name_to_file
, file
,
2776 newname
, ok_if_already_exists
));
2778 /* If the new name has special constructs in it,
2779 call the corresponding file handler. */
2780 handler
= Ffind_file_name_handler (newname
, Qadd_name_to_file
);
2781 if (!NILP (handler
))
2782 RETURN_UNGCPRO (call4 (handler
, Qadd_name_to_file
, file
,
2783 newname
, ok_if_already_exists
));
2785 encoded_file
= ENCODE_FILE (file
);
2786 encoded_newname
= ENCODE_FILE (newname
);
2788 if (NILP (ok_if_already_exists
)
2789 || INTEGERP (ok_if_already_exists
))
2790 barf_or_query_if_file_exists (encoded_newname
, "make it a new name",
2791 INTEGERP (ok_if_already_exists
), 0, 0);
2793 unlink (SDATA (newname
));
2794 if (0 > link (SDATA (encoded_file
), SDATA (encoded_newname
)))
2799 report_file_error ("Adding new name", Flist (2, args
));
2801 report_file_error ("Adding new name", Flist (2, &file
));
2810 DEFUN ("make-symbolic-link", Fmake_symbolic_link
, Smake_symbolic_link
, 2, 3,
2811 "FMake symbolic link to file: \nFMake symbolic link to file %s: \np",
2812 doc
: /* Make a symbolic link to FILENAME, named LINKNAME. Both args strings.
2813 Signals a `file-already-exists' error if a file LINKNAME already exists
2814 unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.
2815 A number as third arg means request confirmation if LINKNAME already exists.
2816 This happens for interactive use with M-x. */)
2817 (filename
, linkname
, ok_if_already_exists
)
2818 Lisp_Object filename
, linkname
, ok_if_already_exists
;
2821 Lisp_Object args
[2];
2823 Lisp_Object handler
;
2824 Lisp_Object encoded_filename
, encoded_linkname
;
2825 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
2827 GCPRO4 (filename
, linkname
, encoded_filename
, encoded_linkname
);
2828 encoded_filename
= encoded_linkname
= Qnil
;
2829 CHECK_STRING (filename
);
2830 CHECK_STRING (linkname
);
2831 /* If the link target has a ~, we must expand it to get
2832 a truly valid file name. Otherwise, do not expand;
2833 we want to permit links to relative file names. */
2834 if (SREF (filename
, 0) == '~')
2835 filename
= Fexpand_file_name (filename
, Qnil
);
2836 linkname
= Fexpand_file_name (linkname
, Qnil
);
2838 /* If the file name has special constructs in it,
2839 call the corresponding file handler. */
2840 handler
= Ffind_file_name_handler (filename
, Qmake_symbolic_link
);
2841 if (!NILP (handler
))
2842 RETURN_UNGCPRO (call4 (handler
, Qmake_symbolic_link
, filename
,
2843 linkname
, ok_if_already_exists
));
2845 /* If the new link name has special constructs in it,
2846 call the corresponding file handler. */
2847 handler
= Ffind_file_name_handler (linkname
, Qmake_symbolic_link
);
2848 if (!NILP (handler
))
2849 RETURN_UNGCPRO (call4 (handler
, Qmake_symbolic_link
, filename
,
2850 linkname
, ok_if_already_exists
));
2852 encoded_filename
= ENCODE_FILE (filename
);
2853 encoded_linkname
= ENCODE_FILE (linkname
);
2855 if (NILP (ok_if_already_exists
)
2856 || INTEGERP (ok_if_already_exists
))
2857 barf_or_query_if_file_exists (encoded_linkname
, "make it a link",
2858 INTEGERP (ok_if_already_exists
), 0, 0);
2859 if (0 > symlink (SDATA (encoded_filename
),
2860 SDATA (encoded_linkname
)))
2862 /* If we didn't complain already, silently delete existing file. */
2863 if (errno
== EEXIST
)
2865 unlink (SDATA (encoded_linkname
));
2866 if (0 <= symlink (SDATA (encoded_filename
),
2867 SDATA (encoded_linkname
)))
2877 report_file_error ("Making symbolic link", Flist (2, args
));
2879 report_file_error ("Making symbolic link", Flist (2, &filename
));
2885 #endif /* S_IFLNK */
2889 DEFUN ("define-logical-name", Fdefine_logical_name
, Sdefine_logical_name
,
2890 2, 2, "sDefine logical name: \nsDefine logical name %s as: ",
2891 doc
: /* Define the job-wide logical name NAME to have the value STRING.
2892 If STRING is nil or a null string, the logical name NAME is deleted. */)
2897 CHECK_STRING (name
);
2899 delete_logical_name (SDATA (name
));
2902 CHECK_STRING (string
);
2904 if (SCHARS (string
) == 0)
2905 delete_logical_name (SDATA (name
));
2907 define_logical_name (SDATA (name
), SDATA (string
));
2916 DEFUN ("sysnetunam", Fsysnetunam
, Ssysnetunam
, 2, 2, 0,
2917 doc
: /* Open a network connection to PATH using LOGIN as the login string. */)
2919 Lisp_Object path
, login
;
2923 CHECK_STRING (path
);
2924 CHECK_STRING (login
);
2926 netresult
= netunam (SDATA (path
), SDATA (login
));
2928 if (netresult
== -1)
2933 #endif /* HPUX_NET */
2935 DEFUN ("file-name-absolute-p", Ffile_name_absolute_p
, Sfile_name_absolute_p
,
2937 doc
: /* Return t if file FILENAME specifies an absolute file name.
2938 On Unix, this is a name starting with a `/' or a `~'. */)
2940 Lisp_Object filename
;
2942 const unsigned char *ptr
;
2944 CHECK_STRING (filename
);
2945 ptr
= SDATA (filename
);
2946 if (IS_DIRECTORY_SEP (*ptr
) || *ptr
== '~'
2948 /* ??? This criterion is probably wrong for '<'. */
2949 || index (ptr
, ':') || index (ptr
, '<')
2950 || (*ptr
== '[' && (ptr
[1] != '-' || (ptr
[2] != '.' && ptr
[2] != ']'))
2954 || (IS_DRIVE (*ptr
) && ptr
[1] == ':' && IS_DIRECTORY_SEP (ptr
[2]))
2962 /* Return nonzero if file FILENAME exists and can be executed. */
2965 check_executable (filename
)
2969 int len
= strlen (filename
);
2972 if (stat (filename
, &st
) < 0)
2974 #if defined (WINDOWSNT) || (defined (MSDOS) && __DJGPP__ > 1)
2975 return ((st
.st_mode
& S_IEXEC
) != 0);
2977 return (S_ISREG (st
.st_mode
)
2979 && (stricmp ((suffix
= filename
+ len
-4), ".com") == 0
2980 || stricmp (suffix
, ".exe") == 0
2981 || stricmp (suffix
, ".bat") == 0)
2982 || (st
.st_mode
& S_IFMT
) == S_IFDIR
);
2983 #endif /* not WINDOWSNT */
2984 #else /* not DOS_NT */
2985 #ifdef HAVE_EUIDACCESS
2986 return (euidaccess (filename
, 1) >= 0);
2988 /* Access isn't quite right because it uses the real uid
2989 and we really want to test with the effective uid.
2990 But Unix doesn't give us a right way to do it. */
2991 return (access (filename
, 1) >= 0);
2993 #endif /* not DOS_NT */
2996 /* Return nonzero if file FILENAME exists and can be written. */
2999 check_writable (filename
)
3004 if (stat (filename
, &st
) < 0)
3006 return (st
.st_mode
& S_IWRITE
|| (st
.st_mode
& S_IFMT
) == S_IFDIR
);
3007 #else /* not MSDOS */
3008 #ifdef HAVE_EUIDACCESS
3009 return (euidaccess (filename
, 2) >= 0);
3011 /* Access isn't quite right because it uses the real uid
3012 and we really want to test with the effective uid.
3013 But Unix doesn't give us a right way to do it.
3014 Opening with O_WRONLY could work for an ordinary file,
3015 but would lose for directories. */
3016 return (access (filename
, 2) >= 0);
3018 #endif /* not MSDOS */
3021 DEFUN ("file-exists-p", Ffile_exists_p
, Sfile_exists_p
, 1, 1, 0,
3022 doc
: /* Return t if file FILENAME exists. (This does not mean you can read it.)
3023 See also `file-readable-p' and `file-attributes'. */)
3025 Lisp_Object filename
;
3027 Lisp_Object absname
;
3028 Lisp_Object handler
;
3029 struct stat statbuf
;
3031 CHECK_STRING (filename
);
3032 absname
= Fexpand_file_name (filename
, Qnil
);
3034 /* If the file name has special constructs in it,
3035 call the corresponding file handler. */
3036 handler
= Ffind_file_name_handler (absname
, Qfile_exists_p
);
3037 if (!NILP (handler
))
3038 return call2 (handler
, Qfile_exists_p
, absname
);
3040 absname
= ENCODE_FILE (absname
);
3042 return (stat (SDATA (absname
), &statbuf
) >= 0) ? Qt
: Qnil
;
3045 DEFUN ("file-executable-p", Ffile_executable_p
, Sfile_executable_p
, 1, 1, 0,
3046 doc
: /* Return t if FILENAME can be executed by you.
3047 For a directory, this means you can access files in that directory. */)
3049 Lisp_Object filename
;
3051 Lisp_Object absname
;
3052 Lisp_Object handler
;
3054 CHECK_STRING (filename
);
3055 absname
= Fexpand_file_name (filename
, Qnil
);
3057 /* If the file name has special constructs in it,
3058 call the corresponding file handler. */
3059 handler
= Ffind_file_name_handler (absname
, Qfile_executable_p
);
3060 if (!NILP (handler
))
3061 return call2 (handler
, Qfile_executable_p
, absname
);
3063 absname
= ENCODE_FILE (absname
);
3065 return (check_executable (SDATA (absname
)) ? Qt
: Qnil
);
3068 DEFUN ("file-readable-p", Ffile_readable_p
, Sfile_readable_p
, 1, 1, 0,
3069 doc
: /* Return t if file FILENAME exists and you can read it.
3070 See also `file-exists-p' and `file-attributes'. */)
3072 Lisp_Object filename
;
3074 Lisp_Object absname
;
3075 Lisp_Object handler
;
3078 struct stat statbuf
;
3080 CHECK_STRING (filename
);
3081 absname
= Fexpand_file_name (filename
, Qnil
);
3083 /* If the file name has special constructs in it,
3084 call the corresponding file handler. */
3085 handler
= Ffind_file_name_handler (absname
, Qfile_readable_p
);
3086 if (!NILP (handler
))
3087 return call2 (handler
, Qfile_readable_p
, absname
);
3089 absname
= ENCODE_FILE (absname
);
3091 #if defined(DOS_NT) || defined(macintosh)
3092 /* Under MS-DOS, Windows, and Macintosh, open does not work for
3094 if (access (SDATA (absname
), 0) == 0)
3097 #else /* not DOS_NT and not macintosh */
3099 #if defined (S_ISFIFO) && defined (O_NONBLOCK)
3100 /* Opening a fifo without O_NONBLOCK can wait.
3101 We don't want to wait. But we don't want to mess wth O_NONBLOCK
3102 except in the case of a fifo, on a system which handles it. */
3103 desc
= stat (SDATA (absname
), &statbuf
);
3106 if (S_ISFIFO (statbuf
.st_mode
))
3107 flags
|= O_NONBLOCK
;
3109 desc
= emacs_open (SDATA (absname
), flags
, 0);
3114 #endif /* not DOS_NT and not macintosh */
3117 /* Having this before file-symlink-p mysteriously caused it to be forgotten
3119 DEFUN ("file-writable-p", Ffile_writable_p
, Sfile_writable_p
, 1, 1, 0,
3120 doc
: /* Return t if file FILENAME can be written or created by you. */)
3122 Lisp_Object filename
;
3124 Lisp_Object absname
, dir
, encoded
;
3125 Lisp_Object handler
;
3126 struct stat statbuf
;
3128 CHECK_STRING (filename
);
3129 absname
= Fexpand_file_name (filename
, Qnil
);
3131 /* If the file name has special constructs in it,
3132 call the corresponding file handler. */
3133 handler
= Ffind_file_name_handler (absname
, Qfile_writable_p
);
3134 if (!NILP (handler
))
3135 return call2 (handler
, Qfile_writable_p
, absname
);
3137 encoded
= ENCODE_FILE (absname
);
3138 if (stat (SDATA (encoded
), &statbuf
) >= 0)
3139 return (check_writable (SDATA (encoded
))
3142 dir
= Ffile_name_directory (absname
);
3145 dir
= Fdirectory_file_name (dir
);
3149 dir
= Fdirectory_file_name (dir
);
3152 dir
= ENCODE_FILE (dir
);
3154 /* The read-only attribute of the parent directory doesn't affect
3155 whether a file or directory can be created within it. Some day we
3156 should check ACLs though, which do affect this. */
3157 if (stat (SDATA (dir
), &statbuf
) < 0)
3159 return (statbuf
.st_mode
& S_IFMT
) == S_IFDIR
? Qt
: Qnil
;
3161 return (check_writable (!NILP (dir
) ? (char *) SDATA (dir
) : "")
3166 DEFUN ("access-file", Faccess_file
, Saccess_file
, 2, 2, 0,
3167 doc
: /* Access file FILENAME, and get an error if that does not work.
3168 The second argument STRING is used in the error message.
3169 If there is no error, we return nil. */)
3171 Lisp_Object filename
, string
;
3173 Lisp_Object handler
, encoded_filename
, absname
;
3176 CHECK_STRING (filename
);
3177 absname
= Fexpand_file_name (filename
, Qnil
);
3179 CHECK_STRING (string
);
3181 /* If the file name has special constructs in it,
3182 call the corresponding file handler. */
3183 handler
= Ffind_file_name_handler (absname
, Qaccess_file
);
3184 if (!NILP (handler
))
3185 return call3 (handler
, Qaccess_file
, absname
, string
);
3187 encoded_filename
= ENCODE_FILE (absname
);
3189 fd
= emacs_open (SDATA (encoded_filename
), O_RDONLY
, 0);
3191 report_file_error (SDATA (string
), Fcons (filename
, Qnil
));
3197 DEFUN ("file-symlink-p", Ffile_symlink_p
, Sfile_symlink_p
, 1, 1, 0,
3198 doc
: /* Return non-nil if file FILENAME is the name of a symbolic link.
3199 The value is the link target, as a string.
3200 Otherwise returns nil. */)
3202 Lisp_Object filename
;
3204 Lisp_Object handler
;
3206 CHECK_STRING (filename
);
3207 filename
= Fexpand_file_name (filename
, Qnil
);
3209 /* If the file name has special constructs in it,
3210 call the corresponding file handler. */
3211 handler
= Ffind_file_name_handler (filename
, Qfile_symlink_p
);
3212 if (!NILP (handler
))
3213 return call2 (handler
, Qfile_symlink_p
, filename
);
3222 filename
= ENCODE_FILE (filename
);
3229 buf
= (char *) xrealloc (buf
, bufsize
);
3230 bzero (buf
, bufsize
);
3233 valsize
= readlink (SDATA (filename
), buf
, bufsize
);
3237 /* HP-UX reports ERANGE if buffer is too small. */
3238 if (errno
== ERANGE
)
3248 while (valsize
>= bufsize
);
3250 val
= make_string (buf
, valsize
);
3251 if (buf
[0] == '/' && index (buf
, ':'))
3252 val
= concat2 (build_string ("/:"), val
);
3254 val
= DECODE_FILE (val
);
3257 #else /* not S_IFLNK */
3259 #endif /* not S_IFLNK */
3262 DEFUN ("file-directory-p", Ffile_directory_p
, Sfile_directory_p
, 1, 1, 0,
3263 doc
: /* Return t if FILENAME names an existing directory.
3264 Symbolic links to directories count as directories.
3265 See `file-symlink-p' to distinguish symlinks. */)
3267 Lisp_Object filename
;
3269 register Lisp_Object absname
;
3271 Lisp_Object handler
;
3273 absname
= expand_and_dir_to_file (filename
, current_buffer
->directory
);
3275 /* If the file name has special constructs in it,
3276 call the corresponding file handler. */
3277 handler
= Ffind_file_name_handler (absname
, Qfile_directory_p
);
3278 if (!NILP (handler
))
3279 return call2 (handler
, Qfile_directory_p
, absname
);
3281 absname
= ENCODE_FILE (absname
);
3283 if (stat (SDATA (absname
), &st
) < 0)
3285 return (st
.st_mode
& S_IFMT
) == S_IFDIR
? Qt
: Qnil
;
3288 DEFUN ("file-accessible-directory-p", Ffile_accessible_directory_p
, Sfile_accessible_directory_p
, 1, 1, 0,
3289 doc
: /* Return t if file FILENAME names a directory you can open.
3290 For the value to be t, FILENAME must specify the name of a directory as a file,
3291 and the directory must allow you to open files in it. In order to use a
3292 directory as a buffer's current directory, this predicate must return true.
3293 A directory name spec may be given instead; then the value is t
3294 if the directory so specified exists and really is a readable and
3295 searchable directory. */)
3297 Lisp_Object filename
;
3299 Lisp_Object handler
;
3301 struct gcpro gcpro1
;
3303 /* If the file name has special constructs in it,
3304 call the corresponding file handler. */
3305 handler
= Ffind_file_name_handler (filename
, Qfile_accessible_directory_p
);
3306 if (!NILP (handler
))
3307 return call2 (handler
, Qfile_accessible_directory_p
, filename
);
3310 tem
= (NILP (Ffile_directory_p (filename
))
3311 || NILP (Ffile_executable_p (filename
)));
3313 return tem
? Qnil
: Qt
;
3316 DEFUN ("file-regular-p", Ffile_regular_p
, Sfile_regular_p
, 1, 1, 0,
3317 doc
: /* Return t if file FILENAME is the name of a regular file.
3318 This is the sort of file that holds an ordinary stream of data bytes. */)
3320 Lisp_Object filename
;
3322 register Lisp_Object absname
;
3324 Lisp_Object handler
;
3326 absname
= expand_and_dir_to_file (filename
, current_buffer
->directory
);
3328 /* If the file name has special constructs in it,
3329 call the corresponding file handler. */
3330 handler
= Ffind_file_name_handler (absname
, Qfile_regular_p
);
3331 if (!NILP (handler
))
3332 return call2 (handler
, Qfile_regular_p
, absname
);
3334 absname
= ENCODE_FILE (absname
);
3339 Lisp_Object tem
= Vw32_get_true_file_attributes
;
3341 /* Tell stat to use expensive method to get accurate info. */
3342 Vw32_get_true_file_attributes
= Qt
;
3343 result
= stat (SDATA (absname
), &st
);
3344 Vw32_get_true_file_attributes
= tem
;
3348 return (st
.st_mode
& S_IFMT
) == S_IFREG
? Qt
: Qnil
;
3351 if (stat (SDATA (absname
), &st
) < 0)
3353 return (st
.st_mode
& S_IFMT
) == S_IFREG
? Qt
: Qnil
;
3357 DEFUN ("file-modes", Ffile_modes
, Sfile_modes
, 1, 1, 0,
3358 doc
: /* Return mode bits of file named FILENAME, as an integer. */)
3360 Lisp_Object filename
;
3362 Lisp_Object absname
;
3364 Lisp_Object handler
;
3366 absname
= expand_and_dir_to_file (filename
, current_buffer
->directory
);
3368 /* If the file name has special constructs in it,
3369 call the corresponding file handler. */
3370 handler
= Ffind_file_name_handler (absname
, Qfile_modes
);
3371 if (!NILP (handler
))
3372 return call2 (handler
, Qfile_modes
, absname
);
3374 absname
= ENCODE_FILE (absname
);
3376 if (stat (SDATA (absname
), &st
) < 0)
3378 #if defined (MSDOS) && __DJGPP__ < 2
3379 if (check_executable (SDATA (absname
)))
3380 st
.st_mode
|= S_IEXEC
;
3381 #endif /* MSDOS && __DJGPP__ < 2 */
3383 return make_number (st
.st_mode
& 07777);
3386 DEFUN ("set-file-modes", Fset_file_modes
, Sset_file_modes
, 2, 2, 0,
3387 doc
: /* Set mode bits of file named FILENAME to MODE (an integer).
3388 Only the 12 low bits of MODE are used. */)
3390 Lisp_Object filename
, mode
;
3392 Lisp_Object absname
, encoded_absname
;
3393 Lisp_Object handler
;
3395 absname
= Fexpand_file_name (filename
, current_buffer
->directory
);
3396 CHECK_NUMBER (mode
);
3398 /* If the file name has special constructs in it,
3399 call the corresponding file handler. */
3400 handler
= Ffind_file_name_handler (absname
, Qset_file_modes
);
3401 if (!NILP (handler
))
3402 return call3 (handler
, Qset_file_modes
, absname
, mode
);
3404 encoded_absname
= ENCODE_FILE (absname
);
3406 if (chmod (SDATA (encoded_absname
), XINT (mode
)) < 0)
3407 report_file_error ("Doing chmod", Fcons (absname
, Qnil
));
3412 DEFUN ("set-default-file-modes", Fset_default_file_modes
, Sset_default_file_modes
, 1, 1, 0,
3413 doc
: /* Set the file permission bits for newly created files.
3414 The argument MODE should be an integer; only the low 9 bits are used.
3415 This setting is inherited by subprocesses. */)
3419 CHECK_NUMBER (mode
);
3421 umask ((~ XINT (mode
)) & 0777);
3426 DEFUN ("default-file-modes", Fdefault_file_modes
, Sdefault_file_modes
, 0, 0, 0,
3427 doc
: /* Return the default file protection for created files.
3428 The value is an integer. */)
3434 realmask
= umask (0);
3437 XSETINT (value
, (~ realmask
) & 0777);
3447 DEFUN ("unix-sync", Funix_sync
, Sunix_sync
, 0, 0, "",
3448 doc
: /* Tell Unix to finish all pending disk updates. */)
3457 DEFUN ("file-newer-than-file-p", Ffile_newer_than_file_p
, Sfile_newer_than_file_p
, 2, 2, 0,
3458 doc
: /* Return t if file FILE1 is newer than file FILE2.
3459 If FILE1 does not exist, the answer is nil;
3460 otherwise, if FILE2 does not exist, the answer is t. */)
3462 Lisp_Object file1
, file2
;
3464 Lisp_Object absname1
, absname2
;
3467 Lisp_Object handler
;
3468 struct gcpro gcpro1
, gcpro2
;
3470 CHECK_STRING (file1
);
3471 CHECK_STRING (file2
);
3474 GCPRO2 (absname1
, file2
);
3475 absname1
= expand_and_dir_to_file (file1
, current_buffer
->directory
);
3476 absname2
= expand_and_dir_to_file (file2
, current_buffer
->directory
);
3479 /* If the file name has special constructs in it,
3480 call the corresponding file handler. */
3481 handler
= Ffind_file_name_handler (absname1
, Qfile_newer_than_file_p
);
3483 handler
= Ffind_file_name_handler (absname2
, Qfile_newer_than_file_p
);
3484 if (!NILP (handler
))
3485 return call3 (handler
, Qfile_newer_than_file_p
, absname1
, absname2
);
3487 GCPRO2 (absname1
, absname2
);
3488 absname1
= ENCODE_FILE (absname1
);
3489 absname2
= ENCODE_FILE (absname2
);
3492 if (stat (SDATA (absname1
), &st
) < 0)
3495 mtime1
= st
.st_mtime
;
3497 if (stat (SDATA (absname2
), &st
) < 0)
3500 return (mtime1
> st
.st_mtime
) ? Qt
: Qnil
;
3504 Lisp_Object Qfind_buffer_file_type
;
3507 #ifndef READ_BUF_SIZE
3508 #define READ_BUF_SIZE (64 << 10)
3511 extern void adjust_markers_for_delete
P_ ((int, int, int, int));
3513 /* This function is called after Lisp functions to decide a coding
3514 system are called, or when they cause an error. Before they are
3515 called, the current buffer is set unibyte and it contains only a
3516 newly inserted text (thus the buffer was empty before the
3519 The functions may set markers, overlays, text properties, or even
3520 alter the buffer contents, change the current buffer.
3522 Here, we reset all those changes by:
3523 o set back the current buffer.
3524 o move all markers and overlays to BEG.
3525 o remove all text properties.
3526 o set back the buffer multibyteness. */
3529 decide_coding_unwind (unwind_data
)
3530 Lisp_Object unwind_data
;
3532 Lisp_Object multibyte
, undo_list
, buffer
;
3534 multibyte
= XCAR (unwind_data
);
3535 unwind_data
= XCDR (unwind_data
);
3536 undo_list
= XCAR (unwind_data
);
3537 buffer
= XCDR (unwind_data
);
3539 if (current_buffer
!= XBUFFER (buffer
))
3540 set_buffer_internal (XBUFFER (buffer
));
3541 adjust_markers_for_delete (BEG
, BEG_BYTE
, Z
, Z_BYTE
);
3542 adjust_overlays_for_delete (BEG
, Z
- BEG
);
3543 BUF_INTERVALS (current_buffer
) = 0;
3544 TEMP_SET_PT_BOTH (BEG
, BEG_BYTE
);
3546 /* Now we are safe to change the buffer's multibyteness directly. */
3547 current_buffer
->enable_multibyte_characters
= multibyte
;
3548 current_buffer
->undo_list
= undo_list
;
3554 /* Used to pass values from insert-file-contents to read_non_regular. */
3556 static int non_regular_fd
;
3557 static int non_regular_inserted
;
3558 static int non_regular_nbytes
;
3561 /* Read from a non-regular file.
3562 Read non_regular_trytry bytes max from non_regular_fd.
3563 Non_regular_inserted specifies where to put the read bytes.
3564 Value is the number of bytes read. */
3573 nbytes
= emacs_read (non_regular_fd
,
3574 BEG_ADDR
+ PT_BYTE
- BEG_BYTE
+ non_regular_inserted
,
3575 non_regular_nbytes
);
3577 return make_number (nbytes
);
3581 /* Condition-case handler used when reading from non-regular files
3582 in insert-file-contents. */
3585 read_non_regular_quit ()
3591 DEFUN ("insert-file-contents", Finsert_file_contents
, Sinsert_file_contents
,
3593 doc
: /* Insert contents of file FILENAME after point.
3594 Returns list of absolute file name and number of characters inserted.
3595 If second argument VISIT is non-nil, the buffer's visited filename
3596 and last save file modtime are set, and it is marked unmodified.
3597 If visiting and the file does not exist, visiting is completed
3598 before the error is signaled.
3599 The optional third and fourth arguments BEG and END
3600 specify what portion of the file to insert.
3601 These arguments count bytes in the file, not characters in the buffer.
3602 If VISIT is non-nil, BEG and END must be nil.
3604 If optional fifth argument REPLACE is non-nil,
3605 it means replace the current buffer contents (in the accessible portion)
3606 with the file contents. This is better than simply deleting and inserting
3607 the whole thing because (1) it preserves some marker positions
3608 and (2) it puts less data in the undo list.
3609 When REPLACE is non-nil, the value is the number of characters actually read,
3610 which is often less than the number of characters to be read.
3612 This does code conversion according to the value of
3613 `coding-system-for-read' or `file-coding-system-alist',
3614 and sets the variable `last-coding-system-used' to the coding system
3616 (filename
, visit
, beg
, end
, replace
)
3617 Lisp_Object filename
, visit
, beg
, end
, replace
;
3622 register int how_much
;
3623 register int unprocessed
;
3624 int count
= SPECPDL_INDEX ();
3625 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
3626 Lisp_Object handler
, val
, insval
, orig_filename
;
3629 int not_regular
= 0;
3630 unsigned char read_buf
[READ_BUF_SIZE
];
3631 struct coding_system coding
;
3632 unsigned char buffer
[1 << 14];
3633 int replace_handled
= 0;
3634 int set_coding_system
= 0;
3635 int coding_system_decided
= 0;
3638 if (current_buffer
->base_buffer
&& ! NILP (visit
))
3639 error ("Cannot do file visiting in an indirect buffer");
3641 if (!NILP (current_buffer
->read_only
))
3642 Fbarf_if_buffer_read_only ();
3646 orig_filename
= Qnil
;
3648 GCPRO4 (filename
, val
, p
, orig_filename
);
3650 CHECK_STRING (filename
);
3651 filename
= Fexpand_file_name (filename
, Qnil
);
3653 /* If the file name has special constructs in it,
3654 call the corresponding file handler. */
3655 handler
= Ffind_file_name_handler (filename
, Qinsert_file_contents
);
3656 if (!NILP (handler
))
3658 val
= call6 (handler
, Qinsert_file_contents
, filename
,
3659 visit
, beg
, end
, replace
);
3660 if (CONSP (val
) && CONSP (XCDR (val
)))
3661 inserted
= XINT (XCAR (XCDR (val
)));
3665 orig_filename
= filename
;
3666 filename
= ENCODE_FILE (filename
);
3672 Lisp_Object tem
= Vw32_get_true_file_attributes
;
3674 /* Tell stat to use expensive method to get accurate info. */
3675 Vw32_get_true_file_attributes
= Qt
;
3676 total
= stat (SDATA (filename
), &st
);
3677 Vw32_get_true_file_attributes
= tem
;
3682 if (stat (SDATA (filename
), &st
) < 0)
3684 if ((fd
= emacs_open (SDATA (filename
), O_RDONLY
, 0)) < 0
3685 || fstat (fd
, &st
) < 0)
3686 #endif /* not APOLLO */
3687 #endif /* WINDOWSNT */
3689 if (fd
>= 0) emacs_close (fd
);
3692 report_file_error ("Opening input file", Fcons (orig_filename
, Qnil
));
3695 if (!NILP (Vcoding_system_for_read
))
3696 Fset (Qbuffer_file_coding_system
, Vcoding_system_for_read
);
3701 /* This code will need to be changed in order to work on named
3702 pipes, and it's probably just not worth it. So we should at
3703 least signal an error. */
3704 if (!S_ISREG (st
.st_mode
))
3711 if (! NILP (replace
) || ! NILP (beg
) || ! NILP (end
))
3712 Fsignal (Qfile_error
,
3713 Fcons (build_string ("not a regular file"),
3714 Fcons (orig_filename
, Qnil
)));
3719 if ((fd
= emacs_open (SDATA (filename
), O_RDONLY
, 0)) < 0)
3722 /* Replacement should preserve point as it preserves markers. */
3723 if (!NILP (replace
))
3724 record_unwind_protect (restore_point_unwind
, Fpoint_marker ());
3726 record_unwind_protect (close_file_unwind
, make_number (fd
));
3728 /* Supposedly happens on VMS. */
3729 /* Can happen on any platform that uses long as type of off_t, but allows
3730 file sizes to exceed 2Gb. VMS is no longer officially supported, so
3731 give a message suitable for the latter case. */
3732 if (! not_regular
&& st
.st_size
< 0)
3733 error ("Maximum buffer size exceeded");
3735 /* Prevent redisplay optimizations. */
3736 current_buffer
->clip_changed
= 1;
3740 if (!NILP (beg
) || !NILP (end
))
3741 error ("Attempt to visit less than an entire file");
3742 if (BEG
< Z
&& NILP (replace
))
3743 error ("Cannot do file visiting in a non-empty buffer");
3749 XSETFASTINT (beg
, 0);
3757 XSETINT (end
, st
.st_size
);
3759 /* Arithmetic overflow can occur if an Emacs integer cannot
3760 represent the file size, or if the calculations below
3761 overflow. The calculations below double the file size
3762 twice, so check that it can be multiplied by 4 safely. */
3763 if (XINT (end
) != st
.st_size
3764 || ((int) st
.st_size
* 4) / 4 != st
.st_size
)
3765 error ("Maximum buffer size exceeded");
3767 /* The file size returned from stat may be zero, but data
3768 may be readable nonetheless, for example when this is a
3769 file in the /proc filesystem. */
3770 if (st
.st_size
== 0)
3771 XSETINT (end
, READ_BUF_SIZE
);
3775 if (EQ (Vcoding_system_for_read
, Qauto_save_coding
))
3777 /* We use emacs-mule for auto saving... */
3778 setup_coding_system (Qemacs_mule
, &coding
);
3779 /* ... but with the special flag to indicate to read in a
3780 multibyte sequence for eight-bit-control char as is. */
3782 coding
.src_multibyte
= 0;
3783 coding
.dst_multibyte
3784 = !NILP (current_buffer
->enable_multibyte_characters
);
3785 coding
.eol_type
= CODING_EOL_LF
;
3786 coding_system_decided
= 1;
3790 /* Decide the coding system to use for reading the file now
3791 because we can't use an optimized method for handling
3792 `coding:' tag if the current buffer is not empty. */
3796 if (!NILP (Vcoding_system_for_read
))
3797 val
= Vcoding_system_for_read
;
3798 else if (! NILP (replace
))
3799 /* In REPLACE mode, we can use the same coding system
3800 that was used to visit the file. */
3801 val
= current_buffer
->buffer_file_coding_system
;
3804 /* Don't try looking inside a file for a coding system
3805 specification if it is not seekable. */
3806 if (! not_regular
&& ! NILP (Vset_auto_coding_function
))
3808 /* Find a coding system specified in the heading two
3809 lines or in the tailing several lines of the file.
3810 We assume that the 1K-byte and 3K-byte for heading
3811 and tailing respectively are sufficient for this
3815 if (st
.st_size
<= (1024 * 4))
3816 nread
= emacs_read (fd
, read_buf
, 1024 * 4);
3819 nread
= emacs_read (fd
, read_buf
, 1024);
3822 if (lseek (fd
, st
.st_size
- (1024 * 3), 0) < 0)
3823 report_file_error ("Setting file position",
3824 Fcons (orig_filename
, Qnil
));
3825 nread
+= emacs_read (fd
, read_buf
+ nread
, 1024 * 3);
3830 error ("IO error reading %s: %s",
3831 SDATA (orig_filename
), emacs_strerror (errno
));
3834 struct buffer
*prev
= current_buffer
;
3838 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
3840 buffer
= Fget_buffer_create (build_string (" *code-converting-work*"));
3841 buf
= XBUFFER (buffer
);
3843 buf
->directory
= current_buffer
->directory
;
3844 buf
->read_only
= Qnil
;
3845 buf
->filename
= Qnil
;
3846 buf
->undo_list
= Qt
;
3847 buf
->overlays_before
= NULL
;
3848 buf
->overlays_after
= NULL
;
3850 set_buffer_internal (buf
);
3852 buf
->enable_multibyte_characters
= Qnil
;
3854 insert_1_both (read_buf
, nread
, nread
, 0, 0, 0);
3855 TEMP_SET_PT_BOTH (BEG
, BEG_BYTE
);
3856 val
= call2 (Vset_auto_coding_function
,
3857 filename
, make_number (nread
));
3858 set_buffer_internal (prev
);
3860 /* Discard the unwind protect for recovering the
3864 /* Rewind the file for the actual read done later. */
3865 if (lseek (fd
, 0, 0) < 0)
3866 report_file_error ("Setting file position",
3867 Fcons (orig_filename
, Qnil
));
3873 /* If we have not yet decided a coding system, check
3874 file-coding-system-alist. */
3875 Lisp_Object args
[6], coding_systems
;
3877 args
[0] = Qinsert_file_contents
, args
[1] = orig_filename
;
3878 args
[2] = visit
, args
[3] = beg
, args
[4] = end
, args
[5] = replace
;
3879 coding_systems
= Ffind_operation_coding_system (6, args
);
3880 if (CONSP (coding_systems
))
3881 val
= XCAR (coding_systems
);
3885 setup_coding_system (Fcheck_coding_system (val
), &coding
);
3886 /* Ensure we set Vlast_coding_system_used. */
3887 set_coding_system
= 1;
3889 if (NILP (current_buffer
->enable_multibyte_characters
)
3891 /* We must suppress all character code conversion except for
3892 end-of-line conversion. */
3893 setup_raw_text_coding_system (&coding
);
3895 coding
.src_multibyte
= 0;
3896 coding
.dst_multibyte
3897 = !NILP (current_buffer
->enable_multibyte_characters
);
3898 coding_system_decided
= 1;
3901 /* If requested, replace the accessible part of the buffer
3902 with the file contents. Avoid replacing text at the
3903 beginning or end of the buffer that matches the file contents;
3904 that preserves markers pointing to the unchanged parts.
3906 Here we implement this feature in an optimized way
3907 for the case where code conversion is NOT needed.
3908 The following if-statement handles the case of conversion
3909 in a less optimal way.
3911 If the code conversion is "automatic" then we try using this
3912 method and hope for the best.
3913 But if we discover the need for conversion, we give up on this method
3914 and let the following if-statement handle the replace job. */
3917 && !(coding
.common_flags
& CODING_REQUIRE_DECODING_MASK
))
3919 /* same_at_start and same_at_end count bytes,
3920 because file access counts bytes
3921 and BEG and END count bytes. */
3922 int same_at_start
= BEGV_BYTE
;
3923 int same_at_end
= ZV_BYTE
;
3925 /* There is still a possibility we will find the need to do code
3926 conversion. If that happens, we set this variable to 1 to
3927 give up on handling REPLACE in the optimized way. */
3928 int giveup_match_end
= 0;
3930 if (XINT (beg
) != 0)
3932 if (lseek (fd
, XINT (beg
), 0) < 0)
3933 report_file_error ("Setting file position",
3934 Fcons (orig_filename
, Qnil
));
3939 /* Count how many chars at the start of the file
3940 match the text at the beginning of the buffer. */
3945 nread
= emacs_read (fd
, buffer
, sizeof buffer
);
3947 error ("IO error reading %s: %s",
3948 SDATA (orig_filename
), emacs_strerror (errno
));
3949 else if (nread
== 0)
3952 if (coding
.type
== coding_type_undecided
)
3953 detect_coding (&coding
, buffer
, nread
);
3954 if (coding
.common_flags
& CODING_REQUIRE_DECODING_MASK
)
3955 /* We found that the file should be decoded somehow.
3956 Let's give up here. */
3958 giveup_match_end
= 1;
3962 if (coding
.eol_type
== CODING_EOL_UNDECIDED
)
3963 detect_eol (&coding
, buffer
, nread
);
3964 if (coding
.eol_type
!= CODING_EOL_UNDECIDED
3965 && coding
.eol_type
!= CODING_EOL_LF
)
3966 /* We found that the format of eol should be decoded.
3967 Let's give up here. */
3969 giveup_match_end
= 1;
3974 while (bufpos
< nread
&& same_at_start
< ZV_BYTE
3975 && FETCH_BYTE (same_at_start
) == buffer
[bufpos
])
3976 same_at_start
++, bufpos
++;
3977 /* If we found a discrepancy, stop the scan.
3978 Otherwise loop around and scan the next bufferful. */
3979 if (bufpos
!= nread
)
3983 /* If the file matches the buffer completely,
3984 there's no need to replace anything. */
3985 if (same_at_start
- BEGV_BYTE
== XINT (end
))
3989 /* Truncate the buffer to the size of the file. */
3990 del_range_1 (same_at_start
, same_at_end
, 0, 0);
3995 /* Count how many chars at the end of the file
3996 match the text at the end of the buffer. But, if we have
3997 already found that decoding is necessary, don't waste time. */
3998 while (!giveup_match_end
)
4000 int total_read
, nread
, bufpos
, curpos
, trial
;
4002 /* At what file position are we now scanning? */
4003 curpos
= XINT (end
) - (ZV_BYTE
- same_at_end
);
4004 /* If the entire file matches the buffer tail, stop the scan. */
4007 /* How much can we scan in the next step? */
4008 trial
= min (curpos
, sizeof buffer
);
4009 if (lseek (fd
, curpos
- trial
, 0) < 0)
4010 report_file_error ("Setting file position",
4011 Fcons (orig_filename
, Qnil
));
4013 total_read
= nread
= 0;
4014 while (total_read
< trial
)
4016 nread
= emacs_read (fd
, buffer
+ total_read
, trial
- total_read
);
4018 error ("IO error reading %s: %s",
4019 SDATA (orig_filename
), emacs_strerror (errno
));
4020 else if (nread
== 0)
4022 total_read
+= nread
;
4025 /* Scan this bufferful from the end, comparing with
4026 the Emacs buffer. */
4027 bufpos
= total_read
;
4029 /* Compare with same_at_start to avoid counting some buffer text
4030 as matching both at the file's beginning and at the end. */
4031 while (bufpos
> 0 && same_at_end
> same_at_start
4032 && FETCH_BYTE (same_at_end
- 1) == buffer
[bufpos
- 1])
4033 same_at_end
--, bufpos
--;
4035 /* If we found a discrepancy, stop the scan.
4036 Otherwise loop around and scan the preceding bufferful. */
4039 /* If this discrepancy is because of code conversion,
4040 we cannot use this method; giveup and try the other. */
4041 if (same_at_end
> same_at_start
4042 && FETCH_BYTE (same_at_end
- 1) >= 0200
4043 && ! NILP (current_buffer
->enable_multibyte_characters
)
4044 && (CODING_MAY_REQUIRE_DECODING (&coding
)))
4045 giveup_match_end
= 1;
4054 if (! giveup_match_end
)
4058 /* We win! We can handle REPLACE the optimized way. */
4060 /* Extend the start of non-matching text area to multibyte
4061 character boundary. */
4062 if (! NILP (current_buffer
->enable_multibyte_characters
))
4063 while (same_at_start
> BEGV_BYTE
4064 && ! CHAR_HEAD_P (FETCH_BYTE (same_at_start
)))
4067 /* Extend the end of non-matching text area to multibyte
4068 character boundary. */
4069 if (! NILP (current_buffer
->enable_multibyte_characters
))
4070 while (same_at_end
< ZV_BYTE
4071 && ! CHAR_HEAD_P (FETCH_BYTE (same_at_end
)))
4074 /* Don't try to reuse the same piece of text twice. */
4075 overlap
= (same_at_start
- BEGV_BYTE
4076 - (same_at_end
+ st
.st_size
- ZV
));
4078 same_at_end
+= overlap
;
4080 /* Arrange to read only the nonmatching middle part of the file. */
4081 XSETFASTINT (beg
, XINT (beg
) + (same_at_start
- BEGV_BYTE
));
4082 XSETFASTINT (end
, XINT (end
) - (ZV_BYTE
- same_at_end
));
4084 del_range_byte (same_at_start
, same_at_end
, 0);
4085 /* Insert from the file at the proper position. */
4086 temp
= BYTE_TO_CHAR (same_at_start
);
4087 SET_PT_BOTH (temp
, same_at_start
);
4089 /* If display currently starts at beginning of line,
4090 keep it that way. */
4091 if (XBUFFER (XWINDOW (selected_window
)->buffer
) == current_buffer
)
4092 XWINDOW (selected_window
)->start_at_line_beg
= Fbolp ();
4094 replace_handled
= 1;
4098 /* If requested, replace the accessible part of the buffer
4099 with the file contents. Avoid replacing text at the
4100 beginning or end of the buffer that matches the file contents;
4101 that preserves markers pointing to the unchanged parts.
4103 Here we implement this feature for the case where code conversion
4104 is needed, in a simple way that needs a lot of memory.
4105 The preceding if-statement handles the case of no conversion
4106 in a more optimized way. */
4107 if (!NILP (replace
) && ! replace_handled
&& BEGV
< ZV
)
4109 int same_at_start
= BEGV_BYTE
;
4110 int same_at_end
= ZV_BYTE
;
4113 /* Make sure that the gap is large enough. */
4114 int bufsize
= 2 * st
.st_size
;
4115 unsigned char *conversion_buffer
= (unsigned char *) xmalloc (bufsize
);
4118 /* First read the whole file, performing code conversion into
4119 CONVERSION_BUFFER. */
4121 if (lseek (fd
, XINT (beg
), 0) < 0)
4123 xfree (conversion_buffer
);
4124 report_file_error ("Setting file position",
4125 Fcons (orig_filename
, Qnil
));
4128 total
= st
.st_size
; /* Total bytes in the file. */
4129 how_much
= 0; /* Bytes read from file so far. */
4130 inserted
= 0; /* Bytes put into CONVERSION_BUFFER so far. */
4131 unprocessed
= 0; /* Bytes not processed in previous loop. */
4133 while (how_much
< total
)
4135 /* try is reserved in some compilers (Microsoft C) */
4136 int trytry
= min (total
- how_much
, READ_BUF_SIZE
- unprocessed
);
4137 unsigned char *destination
= read_buf
+ unprocessed
;
4140 /* Allow quitting out of the actual I/O. */
4143 this = emacs_read (fd
, destination
, trytry
);
4146 if (this < 0 || this + unprocessed
== 0)
4154 if (CODING_MAY_REQUIRE_DECODING (&coding
))
4156 int require
, result
;
4158 this += unprocessed
;
4160 /* If we are using more space than estimated,
4161 make CONVERSION_BUFFER bigger. */
4162 require
= decoding_buffer_size (&coding
, this);
4163 if (inserted
+ require
+ 2 * (total
- how_much
) > bufsize
)
4165 bufsize
= inserted
+ require
+ 2 * (total
- how_much
);
4166 conversion_buffer
= (unsigned char *) xrealloc (conversion_buffer
, bufsize
);
4169 /* Convert this batch with results in CONVERSION_BUFFER. */
4170 if (how_much
>= total
) /* This is the last block. */
4171 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
4172 if (coding
.composing
!= COMPOSITION_DISABLED
)
4173 coding_allocate_composition_data (&coding
, BEGV
);
4174 result
= decode_coding (&coding
, read_buf
,
4175 conversion_buffer
+ inserted
,
4176 this, bufsize
- inserted
);
4178 /* Save for next iteration whatever we didn't convert. */
4179 unprocessed
= this - coding
.consumed
;
4180 bcopy (read_buf
+ coding
.consumed
, read_buf
, unprocessed
);
4181 if (!NILP (current_buffer
->enable_multibyte_characters
))
4182 this = coding
.produced
;
4184 this = str_as_unibyte (conversion_buffer
+ inserted
,
4191 /* At this point, INSERTED is how many characters (i.e. bytes)
4192 are present in CONVERSION_BUFFER.
4193 HOW_MUCH should equal TOTAL,
4194 or should be <= 0 if we couldn't read the file. */
4198 xfree (conversion_buffer
);
4201 error ("IO error reading %s: %s",
4202 SDATA (orig_filename
), emacs_strerror (errno
));
4203 else if (how_much
== -2)
4204 error ("maximum buffer size exceeded");
4207 /* Compare the beginning of the converted file
4208 with the buffer text. */
4211 while (bufpos
< inserted
&& same_at_start
< same_at_end
4212 && FETCH_BYTE (same_at_start
) == conversion_buffer
[bufpos
])
4213 same_at_start
++, bufpos
++;
4215 /* If the file matches the buffer completely,
4216 there's no need to replace anything. */
4218 if (bufpos
== inserted
)
4220 xfree (conversion_buffer
);
4223 /* Truncate the buffer to the size of the file. */
4224 del_range_byte (same_at_start
, same_at_end
, 0);
4229 /* Extend the start of non-matching text area to multibyte
4230 character boundary. */
4231 if (! NILP (current_buffer
->enable_multibyte_characters
))
4232 while (same_at_start
> BEGV_BYTE
4233 && ! CHAR_HEAD_P (FETCH_BYTE (same_at_start
)))
4236 /* Scan this bufferful from the end, comparing with
4237 the Emacs buffer. */
4240 /* Compare with same_at_start to avoid counting some buffer text
4241 as matching both at the file's beginning and at the end. */
4242 while (bufpos
> 0 && same_at_end
> same_at_start
4243 && FETCH_BYTE (same_at_end
- 1) == conversion_buffer
[bufpos
- 1])
4244 same_at_end
--, bufpos
--;
4246 /* Extend the end of non-matching text area to multibyte
4247 character boundary. */
4248 if (! NILP (current_buffer
->enable_multibyte_characters
))
4249 while (same_at_end
< ZV_BYTE
4250 && ! CHAR_HEAD_P (FETCH_BYTE (same_at_end
)))
4253 /* Don't try to reuse the same piece of text twice. */
4254 overlap
= same_at_start
- BEGV_BYTE
- (same_at_end
+ inserted
- ZV_BYTE
);
4256 same_at_end
+= overlap
;
4258 /* If display currently starts at beginning of line,
4259 keep it that way. */
4260 if (XBUFFER (XWINDOW (selected_window
)->buffer
) == current_buffer
)
4261 XWINDOW (selected_window
)->start_at_line_beg
= Fbolp ();
4263 /* Replace the chars that we need to replace,
4264 and update INSERTED to equal the number of bytes
4265 we are taking from the file. */
4266 inserted
-= (Z_BYTE
- same_at_end
) + (same_at_start
- BEG_BYTE
);
4268 if (same_at_end
!= same_at_start
)
4270 del_range_byte (same_at_start
, same_at_end
, 0);
4272 same_at_start
= GPT_BYTE
;
4276 temp
= BYTE_TO_CHAR (same_at_start
);
4278 /* Insert from the file at the proper position. */
4279 SET_PT_BOTH (temp
, same_at_start
);
4280 insert_1 (conversion_buffer
+ same_at_start
- BEG_BYTE
, inserted
,
4282 if (coding
.cmp_data
&& coding
.cmp_data
->used
)
4283 coding_restore_composition (&coding
, Fcurrent_buffer ());
4284 coding_free_composition_data (&coding
);
4286 /* Set `inserted' to the number of inserted characters. */
4287 inserted
= PT
- temp
;
4289 xfree (conversion_buffer
);
4298 register Lisp_Object temp
;
4300 total
= XINT (end
) - XINT (beg
);
4302 /* Make sure point-max won't overflow after this insertion. */
4303 XSETINT (temp
, total
);
4304 if (total
!= XINT (temp
))
4305 error ("Maximum buffer size exceeded");
4308 /* For a special file, all we can do is guess. */
4309 total
= READ_BUF_SIZE
;
4311 if (NILP (visit
) && total
> 0)
4312 prepare_to_modify_buffer (PT
, PT
, NULL
);
4315 if (GAP_SIZE
< total
)
4316 make_gap (total
- GAP_SIZE
);
4318 if (XINT (beg
) != 0 || !NILP (replace
))
4320 if (lseek (fd
, XINT (beg
), 0) < 0)
4321 report_file_error ("Setting file position",
4322 Fcons (orig_filename
, Qnil
));
4325 /* In the following loop, HOW_MUCH contains the total bytes read so
4326 far for a regular file, and not changed for a special file. But,
4327 before exiting the loop, it is set to a negative value if I/O
4331 /* Total bytes inserted. */
4334 /* Here, we don't do code conversion in the loop. It is done by
4335 code_convert_region after all data are read into the buffer. */
4337 int gap_size
= GAP_SIZE
;
4339 while (how_much
< total
)
4341 /* try is reserved in some compilers (Microsoft C) */
4342 int trytry
= min (total
- how_much
, READ_BUF_SIZE
);
4349 /* Maybe make more room. */
4350 if (gap_size
< trytry
)
4352 make_gap (total
- gap_size
);
4353 gap_size
= GAP_SIZE
;
4356 /* Read from the file, capturing `quit'. When an
4357 error occurs, end the loop, and arrange for a quit
4358 to be signaled after decoding the text we read. */
4359 non_regular_fd
= fd
;
4360 non_regular_inserted
= inserted
;
4361 non_regular_nbytes
= trytry
;
4362 val
= internal_condition_case_1 (read_non_regular
, Qnil
, Qerror
,
4363 read_non_regular_quit
);
4374 /* Allow quitting out of the actual I/O. We don't make text
4375 part of the buffer until all the reading is done, so a C-g
4376 here doesn't do any harm. */
4379 this = emacs_read (fd
, BEG_ADDR
+ PT_BYTE
- BEG_BYTE
+ inserted
, trytry
);
4391 /* For a regular file, where TOTAL is the real size,
4392 count HOW_MUCH to compare with it.
4393 For a special file, where TOTAL is just a buffer size,
4394 so don't bother counting in HOW_MUCH.
4395 (INSERTED is where we count the number of characters inserted.) */
4402 /* Make the text read part of the buffer. */
4403 GAP_SIZE
-= inserted
;
4405 GPT_BYTE
+= inserted
;
4407 ZV_BYTE
+= inserted
;
4412 /* Put an anchor to ensure multi-byte form ends at gap. */
4417 /* Discard the unwind protect for closing the file. */
4421 error ("IO error reading %s: %s",
4422 SDATA (orig_filename
), emacs_strerror (errno
));
4426 if (! coding_system_decided
)
4428 /* The coding system is not yet decided. Decide it by an
4429 optimized method for handling `coding:' tag.
4431 Note that we can get here only if the buffer was empty
4432 before the insertion. */
4436 if (!NILP (Vcoding_system_for_read
))
4437 val
= Vcoding_system_for_read
;
4440 /* Since we are sure that the current buffer was empty
4441 before the insertion, we can toggle
4442 enable-multibyte-characters directly here without taking
4443 care of marker adjustment and byte combining problem. By
4444 this way, we can run Lisp program safely before decoding
4445 the inserted text. */
4446 Lisp_Object unwind_data
;
4447 int count
= SPECPDL_INDEX ();
4449 unwind_data
= Fcons (current_buffer
->enable_multibyte_characters
,
4450 Fcons (current_buffer
->undo_list
,
4451 Fcurrent_buffer ()));
4452 current_buffer
->enable_multibyte_characters
= Qnil
;
4453 current_buffer
->undo_list
= Qt
;
4454 record_unwind_protect (decide_coding_unwind
, unwind_data
);
4456 if (inserted
> 0 && ! NILP (Vset_auto_coding_function
))
4458 val
= call2 (Vset_auto_coding_function
,
4459 filename
, make_number (inserted
));
4464 /* If the coding system is not yet decided, check
4465 file-coding-system-alist. */
4466 Lisp_Object args
[6], coding_systems
;
4468 args
[0] = Qinsert_file_contents
, args
[1] = orig_filename
;
4469 args
[2] = visit
, args
[3] = beg
, args
[4] = end
, args
[5] = Qnil
;
4470 coding_systems
= Ffind_operation_coding_system (6, args
);
4471 if (CONSP (coding_systems
))
4472 val
= XCAR (coding_systems
);
4475 unbind_to (count
, Qnil
);
4476 inserted
= Z_BYTE
- BEG_BYTE
;
4479 /* The following kludgy code is to avoid some compiler bug.
4481 setup_coding_system (val, &coding);
4484 struct coding_system temp_coding
;
4485 setup_coding_system (val
, &temp_coding
);
4486 bcopy (&temp_coding
, &coding
, sizeof coding
);
4488 /* Ensure we set Vlast_coding_system_used. */
4489 set_coding_system
= 1;
4491 if (NILP (current_buffer
->enable_multibyte_characters
)
4493 /* We must suppress all character code conversion except for
4494 end-of-line conversion. */
4495 setup_raw_text_coding_system (&coding
);
4496 coding
.src_multibyte
= 0;
4497 coding
.dst_multibyte
4498 = !NILP (current_buffer
->enable_multibyte_characters
);
4502 /* Can't do this if part of the buffer might be preserved. */
4504 && (coding
.type
== coding_type_no_conversion
4505 || coding
.type
== coding_type_raw_text
))
4507 /* Visiting a file with these coding system makes the buffer
4509 current_buffer
->enable_multibyte_characters
= Qnil
;
4510 coding
.dst_multibyte
= 0;
4513 if (inserted
> 0 || coding
.type
== coding_type_ccl
)
4515 if (CODING_MAY_REQUIRE_DECODING (&coding
))
4517 code_convert_region (PT
, PT_BYTE
, PT
+ inserted
, PT_BYTE
+ inserted
,
4519 inserted
= coding
.produced_char
;
4522 adjust_after_insert (PT
, PT_BYTE
, PT
+ inserted
, PT_BYTE
+ inserted
,
4526 /* Now INSERTED is measured in characters. */
4529 /* Use the conversion type to determine buffer-file-type
4530 (find-buffer-file-type is now used to help determine the
4532 if ((coding
.eol_type
== CODING_EOL_UNDECIDED
4533 || coding
.eol_type
== CODING_EOL_LF
)
4534 && ! CODING_REQUIRE_DECODING (&coding
))
4535 current_buffer
->buffer_file_type
= Qt
;
4537 current_buffer
->buffer_file_type
= Qnil
;
4544 if (!EQ (current_buffer
->undo_list
, Qt
))
4545 current_buffer
->undo_list
= Qnil
;
4547 stat (SDATA (filename
), &st
);
4552 current_buffer
->modtime
= st
.st_mtime
;
4553 current_buffer
->filename
= orig_filename
;
4556 SAVE_MODIFF
= MODIFF
;
4557 current_buffer
->auto_save_modified
= MODIFF
;
4558 XSETFASTINT (current_buffer
->save_length
, Z
- BEG
);
4559 #ifdef CLASH_DETECTION
4562 if (!NILP (current_buffer
->file_truename
))
4563 unlock_file (current_buffer
->file_truename
);
4564 unlock_file (filename
);
4566 #endif /* CLASH_DETECTION */
4568 Fsignal (Qfile_error
,
4569 Fcons (build_string ("not a regular file"),
4570 Fcons (orig_filename
, Qnil
)));
4573 if (set_coding_system
)
4574 Vlast_coding_system_used
= coding
.symbol
;
4576 if (! NILP (Ffboundp (Qafter_insert_file_set_coding
)))
4578 insval
= call1 (Qafter_insert_file_set_coding
, make_number (inserted
));
4579 if (! NILP (insval
))
4581 CHECK_NUMBER (insval
);
4582 inserted
= XFASTINT (insval
);
4586 /* Decode file format */
4589 int empty_undo_list_p
= 0;
4591 /* If we're anyway going to discard undo information, don't
4592 record it in the first place. The buffer's undo list at this
4593 point is either nil or t when visiting a file. */
4596 empty_undo_list_p
= NILP (current_buffer
->undo_list
);
4597 current_buffer
->undo_list
= Qt
;
4600 insval
= call3 (Qformat_decode
,
4601 Qnil
, make_number (inserted
), visit
);
4602 CHECK_NUMBER (insval
);
4603 inserted
= XFASTINT (insval
);
4606 current_buffer
->undo_list
= empty_undo_list_p
? Qnil
: Qt
;
4609 /* Call after-change hooks for the inserted text, aside from the case
4610 of normal visiting (not with REPLACE), which is done in a new buffer
4611 "before" the buffer is changed. */
4612 if (inserted
> 0 && total
> 0
4613 && (NILP (visit
) || !NILP (replace
)))
4615 signal_after_change (PT
, 0, inserted
);
4616 update_compositions (PT
, PT
, CHECK_BORDER
);
4619 p
= Vafter_insert_file_functions
;
4622 insval
= call1 (XCAR (p
), make_number (inserted
));
4625 CHECK_NUMBER (insval
);
4626 inserted
= XFASTINT (insval
);
4633 && current_buffer
->modtime
== -1)
4635 /* If visiting nonexistent file, return nil. */
4636 report_file_error ("Opening input file", Fcons (orig_filename
, Qnil
));
4640 Fsignal (Qquit
, Qnil
);
4642 /* ??? Retval needs to be dealt with in all cases consistently. */
4644 val
= Fcons (orig_filename
,
4645 Fcons (make_number (inserted
),
4648 RETURN_UNGCPRO (unbind_to (count
, val
));
4651 static Lisp_Object build_annotations
P_ ((Lisp_Object
, Lisp_Object
));
4652 static Lisp_Object build_annotations_2
P_ ((Lisp_Object
, Lisp_Object
,
4653 Lisp_Object
, Lisp_Object
));
4655 /* If build_annotations switched buffers, switch back to BUF.
4656 Kill the temporary buffer that was selected in the meantime.
4658 Since this kill only the last temporary buffer, some buffers remain
4659 not killed if build_annotations switched buffers more than once.
4663 build_annotations_unwind (buf
)
4668 if (XBUFFER (buf
) == current_buffer
)
4670 tembuf
= Fcurrent_buffer ();
4672 Fkill_buffer (tembuf
);
4676 /* Decide the coding-system to encode the data with. */
4679 choose_write_coding_system (start
, end
, filename
,
4680 append
, visit
, lockname
, coding
)
4681 Lisp_Object start
, end
, filename
, append
, visit
, lockname
;
4682 struct coding_system
*coding
;
4688 /* We use emacs-mule for auto saving... */
4689 setup_coding_system (Qemacs_mule
, coding
);
4690 /* ... but with the special flag to indicate not to strip off
4691 leading code of eight-bit-control chars. */
4693 goto done_setup_coding
;
4695 else if (!NILP (Vcoding_system_for_write
))
4697 val
= Vcoding_system_for_write
;
4698 if (coding_system_require_warning
4699 && !NILP (Ffboundp (Vselect_safe_coding_system_function
)))
4700 /* Confirm that VAL can surely encode the current region. */
4701 val
= call5 (Vselect_safe_coding_system_function
,
4702 start
, end
, Fcons (Qt
, Fcons (val
, Qnil
)),
4707 /* If the variable `buffer-file-coding-system' is set locally,
4708 it means that the file was read with some kind of code
4709 conversion or the variable is explicitly set by users. We
4710 had better write it out with the same coding system even if
4711 `enable-multibyte-characters' is nil.
4713 If it is not set locally, we anyway have to convert EOL
4714 format if the default value of `buffer-file-coding-system'
4715 tells that it is not Unix-like (LF only) format. */
4716 int using_default_coding
= 0;
4717 int force_raw_text
= 0;
4719 val
= current_buffer
->buffer_file_coding_system
;
4721 || NILP (Flocal_variable_p (Qbuffer_file_coding_system
, Qnil
)))
4724 if (NILP (current_buffer
->enable_multibyte_characters
))
4730 /* Check file-coding-system-alist. */
4731 Lisp_Object args
[7], coding_systems
;
4733 args
[0] = Qwrite_region
; args
[1] = start
; args
[2] = end
;
4734 args
[3] = filename
; args
[4] = append
; args
[5] = visit
;
4736 coding_systems
= Ffind_operation_coding_system (7, args
);
4737 if (CONSP (coding_systems
) && !NILP (XCDR (coding_systems
)))
4738 val
= XCDR (coding_systems
);
4742 && !NILP (current_buffer
->buffer_file_coding_system
))
4744 /* If we still have not decided a coding system, use the
4745 default value of buffer-file-coding-system. */
4746 val
= current_buffer
->buffer_file_coding_system
;
4747 using_default_coding
= 1;
4751 && !NILP (Ffboundp (Vselect_safe_coding_system_function
)))
4752 /* Confirm that VAL can surely encode the current region. */
4753 val
= call5 (Vselect_safe_coding_system_function
,
4754 start
, end
, val
, Qnil
, filename
);
4756 setup_coding_system (Fcheck_coding_system (val
), coding
);
4757 if (coding
->eol_type
== CODING_EOL_UNDECIDED
4758 && !using_default_coding
)
4760 if (! EQ (default_buffer_file_coding
.symbol
,
4761 buffer_defaults
.buffer_file_coding_system
))
4762 setup_coding_system (buffer_defaults
.buffer_file_coding_system
,
4763 &default_buffer_file_coding
);
4764 if (default_buffer_file_coding
.eol_type
!= CODING_EOL_UNDECIDED
)
4766 Lisp_Object subsidiaries
;
4768 coding
->eol_type
= default_buffer_file_coding
.eol_type
;
4769 subsidiaries
= Fget (coding
->symbol
, Qeol_type
);
4770 if (VECTORP (subsidiaries
)
4771 && XVECTOR (subsidiaries
)->size
== 3)
4773 = XVECTOR (subsidiaries
)->contents
[coding
->eol_type
];
4778 setup_raw_text_coding_system (coding
);
4779 goto done_setup_coding
;
4782 setup_coding_system (Fcheck_coding_system (val
), coding
);
4785 if (!STRINGP (start
) && !NILP (current_buffer
->selective_display
))
4786 coding
->mode
|= CODING_MODE_SELECTIVE_DISPLAY
;
4789 DEFUN ("write-region", Fwrite_region
, Swrite_region
, 3, 7,
4790 "r\nFWrite region to file: \ni\ni\ni\np",
4791 doc
: /* Write current region into specified file.
4792 When called from a program, requires three arguments:
4793 START, END and FILENAME. START and END are normally buffer positions
4794 specifying the part of the buffer to write.
4795 If START is nil, that means to use the entire buffer contents.
4796 If START is a string, then output that string to the file
4797 instead of any buffer contents; END is ignored.
4799 Optional fourth argument APPEND if non-nil means
4800 append to existing file contents (if any). If it is an integer,
4801 seek to that offset in the file before writing.
4802 Optional fifth argument VISIT if t means
4803 set the last-save-file-modtime of buffer to this file's modtime
4804 and mark buffer not modified.
4805 If VISIT is a string, it is a second file name;
4806 the output goes to FILENAME, but the buffer is marked as visiting VISIT.
4807 VISIT is also the file name to lock and unlock for clash detection.
4808 If VISIT is neither t nor nil nor a string,
4809 that means do not display the \"Wrote file\" message.
4810 The optional sixth arg LOCKNAME, if non-nil, specifies the name to
4811 use for locking and unlocking, overriding FILENAME and VISIT.
4812 The optional seventh arg MUSTBENEW, if non-nil, insists on a check
4813 for an existing file with the same name. If MUSTBENEW is `excl',
4814 that means to get an error if the file already exists; never overwrite.
4815 If MUSTBENEW is neither nil nor `excl', that means ask for
4816 confirmation before overwriting, but do go ahead and overwrite the file
4817 if the user confirms.
4819 This does code conversion according to the value of
4820 `coding-system-for-write', `buffer-file-coding-system', or
4821 `file-coding-system-alist', and sets the variable
4822 `last-coding-system-used' to the coding system actually used. */)
4823 (start
, end
, filename
, append
, visit
, lockname
, mustbenew
)
4824 Lisp_Object start
, end
, filename
, append
, visit
, lockname
, mustbenew
;
4829 const unsigned char *fn
;
4832 int count
= SPECPDL_INDEX ();
4835 unsigned char *fname
= 0; /* If non-0, original filename (must rename) */
4837 Lisp_Object handler
;
4838 Lisp_Object visit_file
;
4839 Lisp_Object annotations
;
4840 Lisp_Object encoded_filename
;
4841 int visiting
= (EQ (visit
, Qt
) || STRINGP (visit
));
4842 int quietly
= !NILP (visit
);
4843 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
4844 struct buffer
*given_buffer
;
4846 int buffer_file_type
= O_BINARY
;
4848 struct coding_system coding
;
4850 if (current_buffer
->base_buffer
&& visiting
)
4851 error ("Cannot do file visiting in an indirect buffer");
4853 if (!NILP (start
) && !STRINGP (start
))
4854 validate_region (&start
, &end
);
4856 GCPRO5 (start
, filename
, visit
, visit_file
, lockname
);
4858 filename
= Fexpand_file_name (filename
, Qnil
);
4860 if (!NILP (mustbenew
) && !EQ (mustbenew
, Qexcl
))
4861 barf_or_query_if_file_exists (filename
, "overwrite", 1, 0, 1);
4863 if (STRINGP (visit
))
4864 visit_file
= Fexpand_file_name (visit
, Qnil
);
4866 visit_file
= filename
;
4868 if (NILP (lockname
))
4869 lockname
= visit_file
;
4873 /* If the file name has special constructs in it,
4874 call the corresponding file handler. */
4875 handler
= Ffind_file_name_handler (filename
, Qwrite_region
);
4876 /* If FILENAME has no handler, see if VISIT has one. */
4877 if (NILP (handler
) && STRINGP (visit
))
4878 handler
= Ffind_file_name_handler (visit
, Qwrite_region
);
4880 if (!NILP (handler
))
4883 val
= call6 (handler
, Qwrite_region
, start
, end
,
4884 filename
, append
, visit
);
4888 SAVE_MODIFF
= MODIFF
;
4889 XSETFASTINT (current_buffer
->save_length
, Z
- BEG
);
4890 current_buffer
->filename
= visit_file
;
4896 record_unwind_protect (save_restriction_restore
, save_restriction_save ());
4898 /* Special kludge to simplify auto-saving. */
4901 XSETFASTINT (start
, BEG
);
4902 XSETFASTINT (end
, Z
);
4906 record_unwind_protect (build_annotations_unwind
, Fcurrent_buffer ());
4907 count1
= SPECPDL_INDEX ();
4909 given_buffer
= current_buffer
;
4911 if (!STRINGP (start
))
4913 annotations
= build_annotations (start
, end
);
4915 if (current_buffer
!= given_buffer
)
4917 XSETFASTINT (start
, BEGV
);
4918 XSETFASTINT (end
, ZV
);
4924 GCPRO5 (start
, filename
, annotations
, visit_file
, lockname
);
4926 /* Decide the coding-system to encode the data with.
4927 We used to make this choice before calling build_annotations, but that
4928 leads to problems when a write-annotate-function takes care of
4929 unsavable chars (as was the case with X-Symbol). */
4930 choose_write_coding_system (start
, end
, filename
,
4931 append
, visit
, lockname
, &coding
);
4932 Vlast_coding_system_used
= coding
.symbol
;
4934 given_buffer
= current_buffer
;
4935 if (! STRINGP (start
))
4937 annotations
= build_annotations_2 (start
, end
,
4938 coding
.pre_write_conversion
, annotations
);
4939 if (current_buffer
!= given_buffer
)
4941 XSETFASTINT (start
, BEGV
);
4942 XSETFASTINT (end
, ZV
);
4946 #ifdef CLASH_DETECTION
4949 #if 0 /* This causes trouble for GNUS. */
4950 /* If we've locked this file for some other buffer,
4951 query before proceeding. */
4952 if (!visiting
&& EQ (Ffile_locked_p (lockname
), Qt
))
4953 call2 (intern ("ask-user-about-lock"), filename
, Vuser_login_name
);
4956 lock_file (lockname
);
4958 #endif /* CLASH_DETECTION */
4960 encoded_filename
= ENCODE_FILE (filename
);
4962 fn
= SDATA (encoded_filename
);
4966 desc
= emacs_open (fn
, O_WRONLY
| buffer_file_type
, 0);
4967 #else /* not DOS_NT */
4968 desc
= emacs_open (fn
, O_WRONLY
, 0);
4969 #endif /* not DOS_NT */
4971 if (desc
< 0 && (NILP (append
) || errno
== ENOENT
))
4973 if (auto_saving
) /* Overwrite any previous version of autosave file */
4975 vms_truncate (fn
); /* if fn exists, truncate to zero length */
4976 desc
= emacs_open (fn
, O_RDWR
, 0);
4978 desc
= creat_copy_attrs (STRINGP (current_buffer
->filename
)
4979 ? SDATA (current_buffer
->filename
) : 0,
4982 else /* Write to temporary name and rename if no errors */
4984 Lisp_Object temp_name
;
4985 temp_name
= Ffile_name_directory (filename
);
4987 if (!NILP (temp_name
))
4989 temp_name
= Fmake_temp_name (concat2 (temp_name
,
4990 build_string ("$$SAVE$$")));
4991 fname
= SDATA (filename
);
4992 fn
= SDATA (temp_name
);
4993 desc
= creat_copy_attrs (fname
, fn
);
4996 /* If we can't open the temporary file, try creating a new
4997 version of the original file. VMS "creat" creates a
4998 new version rather than truncating an existing file. */
5001 desc
= creat (fn
, 0666);
5002 #if 0 /* This can clobber an existing file and fail to replace it,
5003 if the user runs out of space. */
5006 /* We can't make a new version;
5007 try to truncate and rewrite existing version if any. */
5009 desc
= emacs_open (fn
, O_RDWR
, 0);
5015 desc
= creat (fn
, 0666);
5019 desc
= emacs_open (fn
,
5020 O_WRONLY
| O_CREAT
| buffer_file_type
5021 | (EQ (mustbenew
, Qexcl
) ? O_EXCL
: O_TRUNC
),
5022 S_IREAD
| S_IWRITE
);
5023 #else /* not DOS_NT */
5024 desc
= emacs_open (fn
, O_WRONLY
| O_TRUNC
| O_CREAT
5025 | (EQ (mustbenew
, Qexcl
) ? O_EXCL
: 0),
5026 auto_saving
? auto_save_mode_bits
: 0666);
5027 #endif /* not DOS_NT */
5028 #endif /* not VMS */
5032 #ifdef CLASH_DETECTION
5034 if (!auto_saving
) unlock_file (lockname
);
5036 #endif /* CLASH_DETECTION */
5038 report_file_error ("Opening output file", Fcons (filename
, Qnil
));
5041 record_unwind_protect (close_file_unwind
, make_number (desc
));
5043 if (!NILP (append
) && !NILP (Ffile_regular_p (filename
)))
5047 if (NUMBERP (append
))
5048 ret
= lseek (desc
, XINT (append
), 1);
5050 ret
= lseek (desc
, 0, 2);
5053 #ifdef CLASH_DETECTION
5054 if (!auto_saving
) unlock_file (lockname
);
5055 #endif /* CLASH_DETECTION */
5057 report_file_error ("Lseek error", Fcons (filename
, Qnil
));
5065 * Kludge Warning: The VMS C RTL likes to insert carriage returns
5066 * if we do writes that don't end with a carriage return. Furthermore
5067 * it cannot handle writes of more then 16K. The modified
5068 * version of "sys_write" in SYSDEP.C (see comment there) copes with
5069 * this EXCEPT for the last record (iff it doesn't end with a carriage
5070 * return). This implies that if your buffer doesn't end with a carriage
5071 * return, you get one free... tough. However it also means that if
5072 * we make two calls to sys_write (a la the following code) you can
5073 * get one at the gap as well. The easiest way to fix this (honest)
5074 * is to move the gap to the next newline (or the end of the buffer).
5079 if (GPT
> BEG
&& GPT_ADDR
[-1] != '\n')
5080 move_gap (find_next_newline (GPT
, 1));
5082 /* Whether VMS or not, we must move the gap to the next of newline
5083 when we must put designation sequences at beginning of line. */
5084 if (INTEGERP (start
)
5085 && coding
.type
== coding_type_iso2022
5086 && coding
.flags
& CODING_FLAG_ISO_DESIGNATE_AT_BOL
5087 && GPT
> BEG
&& GPT_ADDR
[-1] != '\n')
5089 int opoint
= PT
, opoint_byte
= PT_BYTE
;
5090 scan_newline (PT
, PT_BYTE
, ZV
, ZV_BYTE
, 1, 0);
5091 move_gap_both (PT
, PT_BYTE
);
5092 SET_PT_BOTH (opoint
, opoint_byte
);
5099 if (STRINGP (start
))
5101 failure
= 0 > a_write (desc
, start
, 0, SCHARS (start
),
5102 &annotations
, &coding
);
5105 else if (XINT (start
) != XINT (end
))
5107 tem
= CHAR_TO_BYTE (XINT (start
));
5109 if (XINT (start
) < GPT
)
5111 failure
= 0 > a_write (desc
, Qnil
, XINT (start
),
5112 min (GPT
, XINT (end
)) - XINT (start
),
5113 &annotations
, &coding
);
5117 if (XINT (end
) > GPT
&& !failure
)
5119 tem
= max (XINT (start
), GPT
);
5120 failure
= 0 > a_write (desc
, Qnil
, tem
, XINT (end
) - tem
,
5121 &annotations
, &coding
);
5127 /* If file was empty, still need to write the annotations */
5128 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
5129 failure
= 0 > a_write (desc
, Qnil
, XINT (end
), 0, &annotations
, &coding
);
5133 if (CODING_REQUIRE_FLUSHING (&coding
)
5134 && !(coding
.mode
& CODING_MODE_LAST_BLOCK
)
5137 /* We have to flush out a data. */
5138 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
5139 failure
= 0 > e_write (desc
, Qnil
, 0, 0, &coding
);
5146 /* Note fsync appears to change the modtime on BSD4.2 (both vax and sun).
5147 Disk full in NFS may be reported here. */
5148 /* mib says that closing the file will try to write as fast as NFS can do
5149 it, and that means the fsync here is not crucial for autosave files. */
5150 if (!auto_saving
&& fsync (desc
) < 0)
5152 /* If fsync fails with EINTR, don't treat that as serious. */
5154 failure
= 1, save_errno
= errno
;
5158 /* Spurious "file has changed on disk" warnings have been
5159 observed on Suns as well.
5160 It seems that `close' can change the modtime, under nfs.
5162 (This has supposedly been fixed in Sunos 4,
5163 but who knows about all the other machines with NFS?) */
5166 /* On VMS and APOLLO, must do the stat after the close
5167 since closing changes the modtime. */
5170 /* Recall that #if defined does not work on VMS. */
5177 /* NFS can report a write failure now. */
5178 if (emacs_close (desc
) < 0)
5179 failure
= 1, save_errno
= errno
;
5182 /* If we wrote to a temporary name and had no errors, rename to real name. */
5186 failure
= (rename (fn
, fname
) != 0), save_errno
= errno
;
5194 /* Discard the unwind protect for close_file_unwind. */
5195 specpdl_ptr
= specpdl
+ count1
;
5196 /* Restore the original current buffer. */
5197 visit_file
= unbind_to (count
, visit_file
);
5199 #ifdef CLASH_DETECTION
5201 unlock_file (lockname
);
5202 #endif /* CLASH_DETECTION */
5204 /* Do this before reporting IO error
5205 to avoid a "file has changed on disk" warning on
5206 next attempt to save. */
5208 current_buffer
->modtime
= st
.st_mtime
;
5211 error ("IO error writing %s: %s", SDATA (filename
),
5212 emacs_strerror (save_errno
));
5216 SAVE_MODIFF
= MODIFF
;
5217 XSETFASTINT (current_buffer
->save_length
, Z
- BEG
);
5218 current_buffer
->filename
= visit_file
;
5219 update_mode_lines
++;
5225 message_with_string ((! INTEGERP (append
)
5235 Lisp_Object
merge ();
5237 DEFUN ("car-less-than-car", Fcar_less_than_car
, Scar_less_than_car
, 2, 2, 0,
5238 doc
: /* Return t if (car A) is numerically less than (car B). */)
5242 return Flss (Fcar (a
), Fcar (b
));
5245 /* Build the complete list of annotations appropriate for writing out
5246 the text between START and END, by calling all the functions in
5247 write-region-annotate-functions and merging the lists they return.
5248 If one of these functions switches to a different buffer, we assume
5249 that buffer contains altered text. Therefore, the caller must
5250 make sure to restore the current buffer in all cases,
5251 as save-excursion would do. */
5254 build_annotations (start
, end
)
5255 Lisp_Object start
, end
;
5257 Lisp_Object annotations
;
5259 struct gcpro gcpro1
, gcpro2
;
5260 Lisp_Object original_buffer
;
5261 int i
, used_global
= 0;
5263 XSETBUFFER (original_buffer
, current_buffer
);
5266 p
= Vwrite_region_annotate_functions
;
5267 GCPRO2 (annotations
, p
);
5270 struct buffer
*given_buffer
= current_buffer
;
5271 if (EQ (Qt
, XCAR (p
)) && !used_global
)
5272 { /* Use the global value of the hook. */
5275 arg
[0] = Fdefault_value (Qwrite_region_annotate_functions
);
5277 p
= Fappend (2, arg
);
5280 Vwrite_region_annotations_so_far
= annotations
;
5281 res
= call2 (XCAR (p
), start
, end
);
5282 /* If the function makes a different buffer current,
5283 assume that means this buffer contains altered text to be output.
5284 Reset START and END from the buffer bounds
5285 and discard all previous annotations because they should have
5286 been dealt with by this function. */
5287 if (current_buffer
!= given_buffer
)
5289 XSETFASTINT (start
, BEGV
);
5290 XSETFASTINT (end
, ZV
);
5293 Flength (res
); /* Check basic validity of return value */
5294 annotations
= merge (annotations
, res
, Qcar_less_than_car
);
5298 /* Now do the same for annotation functions implied by the file-format */
5299 if (auto_saving
&& (!EQ (Vauto_save_file_format
, Qt
)))
5300 p
= Vauto_save_file_format
;
5302 p
= current_buffer
->file_format
;
5303 for (i
= 0; CONSP (p
); p
= XCDR (p
), ++i
)
5305 struct buffer
*given_buffer
= current_buffer
;
5307 Vwrite_region_annotations_so_far
= annotations
;
5309 /* Value is either a list of annotations or nil if the function
5310 has written annotations to a temporary buffer, which is now
5312 res
= call5 (Qformat_annotate_function
, XCAR (p
), start
, end
,
5313 original_buffer
, make_number (i
));
5314 if (current_buffer
!= given_buffer
)
5316 XSETFASTINT (start
, BEGV
);
5317 XSETFASTINT (end
, ZV
);
5322 annotations
= merge (annotations
, res
, Qcar_less_than_car
);
5330 build_annotations_2 (start
, end
, pre_write_conversion
, annotations
)
5331 Lisp_Object start
, end
, pre_write_conversion
, annotations
;
5333 struct gcpro gcpro1
;
5336 GCPRO1 (annotations
);
5337 /* At last, do the same for the function PRE_WRITE_CONVERSION
5338 implied by the current coding-system. */
5339 if (!NILP (pre_write_conversion
))
5341 struct buffer
*given_buffer
= current_buffer
;
5342 Vwrite_region_annotations_so_far
= annotations
;
5343 res
= call2 (pre_write_conversion
, start
, end
);
5345 annotations
= (current_buffer
!= given_buffer
5347 : merge (annotations
, res
, Qcar_less_than_car
));
5354 /* Write to descriptor DESC the NCHARS chars starting at POS of STRING.
5355 If STRING is nil, POS is the character position in the current buffer.
5356 Intersperse with them the annotations from *ANNOT
5357 which fall within the range of POS to POS + NCHARS,
5358 each at its appropriate position.
5360 We modify *ANNOT by discarding elements as we use them up.
5362 The return value is negative in case of system call failure. */
5365 a_write (desc
, string
, pos
, nchars
, annot
, coding
)
5368 register int nchars
;
5371 struct coding_system
*coding
;
5375 int lastpos
= pos
+ nchars
;
5377 while (NILP (*annot
) || CONSP (*annot
))
5379 tem
= Fcar_safe (Fcar (*annot
));
5382 nextpos
= XFASTINT (tem
);
5384 /* If there are no more annotations in this range,
5385 output the rest of the range all at once. */
5386 if (! (nextpos
>= pos
&& nextpos
<= lastpos
))
5387 return e_write (desc
, string
, pos
, lastpos
, coding
);
5389 /* Output buffer text up to the next annotation's position. */
5392 if (0 > e_write (desc
, string
, pos
, nextpos
, coding
))
5396 /* Output the annotation. */
5397 tem
= Fcdr (Fcar (*annot
));
5400 if (0 > e_write (desc
, tem
, 0, SCHARS (tem
), coding
))
5403 *annot
= Fcdr (*annot
);
5408 #ifndef WRITE_BUF_SIZE
5409 #define WRITE_BUF_SIZE (16 * 1024)
5412 /* Write text in the range START and END into descriptor DESC,
5413 encoding them with coding system CODING. If STRING is nil, START
5414 and END are character positions of the current buffer, else they
5415 are indexes to the string STRING. */
5418 e_write (desc
, string
, start
, end
, coding
)
5422 struct coding_system
*coding
;
5424 register char *addr
;
5425 register int nbytes
;
5426 char buf
[WRITE_BUF_SIZE
];
5430 coding
->composing
= COMPOSITION_DISABLED
;
5431 if (coding
->composing
!= COMPOSITION_DISABLED
)
5432 coding_save_composition (coding
, start
, end
, string
);
5434 if (STRINGP (string
))
5436 addr
= SDATA (string
);
5437 nbytes
= SBYTES (string
);
5438 coding
->src_multibyte
= STRING_MULTIBYTE (string
);
5440 else if (start
< end
)
5442 /* It is assured that the gap is not in the range START and END-1. */
5443 addr
= CHAR_POS_ADDR (start
);
5444 nbytes
= CHAR_TO_BYTE (end
) - CHAR_TO_BYTE (start
);
5445 coding
->src_multibyte
5446 = !NILP (current_buffer
->enable_multibyte_characters
);
5452 coding
->src_multibyte
= 1;
5455 /* We used to have a code for handling selective display here. But,
5456 now it is handled within encode_coding. */
5461 result
= encode_coding (coding
, addr
, buf
, nbytes
, WRITE_BUF_SIZE
);
5462 if (coding
->produced
> 0)
5464 coding
->produced
-= emacs_write (desc
, buf
, coding
->produced
);
5465 if (coding
->produced
)
5471 nbytes
-= coding
->consumed
;
5472 addr
+= coding
->consumed
;
5473 if (result
== CODING_FINISH_INSUFFICIENT_SRC
5476 /* The source text ends by an incomplete multibyte form.
5477 There's no way other than write it out as is. */
5478 nbytes
-= emacs_write (desc
, addr
, nbytes
);
5487 start
+= coding
->consumed_char
;
5488 if (coding
->cmp_data
)
5489 coding_adjust_composition_offset (coding
, start
);
5492 if (coding
->cmp_data
)
5493 coding_free_composition_data (coding
);
5498 DEFUN ("verify-visited-file-modtime", Fverify_visited_file_modtime
,
5499 Sverify_visited_file_modtime
, 1, 1, 0,
5500 doc
: /* Return t if last mod time of BUF's visited file matches what BUF records.
5501 This means that the file has not been changed since it was visited or saved. */)
5507 Lisp_Object handler
;
5508 Lisp_Object filename
;
5513 if (!STRINGP (b
->filename
)) return Qt
;
5514 if (b
->modtime
== 0) return Qt
;
5516 /* If the file name has special constructs in it,
5517 call the corresponding file handler. */
5518 handler
= Ffind_file_name_handler (b
->filename
,
5519 Qverify_visited_file_modtime
);
5520 if (!NILP (handler
))
5521 return call2 (handler
, Qverify_visited_file_modtime
, buf
);
5523 filename
= ENCODE_FILE (b
->filename
);
5525 if (stat (SDATA (filename
), &st
) < 0)
5527 /* If the file doesn't exist now and didn't exist before,
5528 we say that it isn't modified, provided the error is a tame one. */
5529 if (errno
== ENOENT
|| errno
== EACCES
|| errno
== ENOTDIR
)
5534 if (st
.st_mtime
== b
->modtime
5535 /* If both are positive, accept them if they are off by one second. */
5536 || (st
.st_mtime
> 0 && b
->modtime
> 0
5537 && (st
.st_mtime
== b
->modtime
+ 1
5538 || st
.st_mtime
== b
->modtime
- 1)))
5543 DEFUN ("clear-visited-file-modtime", Fclear_visited_file_modtime
,
5544 Sclear_visited_file_modtime
, 0, 0, 0,
5545 doc
: /* Clear out records of last mod time of visited file.
5546 Next attempt to save will certainly not complain of a discrepancy. */)
5549 current_buffer
->modtime
= 0;
5553 DEFUN ("visited-file-modtime", Fvisited_file_modtime
,
5554 Svisited_file_modtime
, 0, 0, 0,
5555 doc
: /* Return the current buffer's recorded visited file modification time.
5556 The value is a list of the form (HIGH . LOW), like the time values
5557 that `file-attributes' returns. */)
5560 return long_to_cons ((unsigned long) current_buffer
->modtime
);
5563 DEFUN ("set-visited-file-modtime", Fset_visited_file_modtime
,
5564 Sset_visited_file_modtime
, 0, 1, 0,
5565 doc
: /* Update buffer's recorded modification time from the visited file's time.
5566 Useful if the buffer was not read from the file normally
5567 or if the file itself has been changed for some known benign reason.
5568 An argument specifies the modification time value to use
5569 \(instead of that of the visited file), in the form of a list
5570 \(HIGH . LOW) or (HIGH LOW). */)
5572 Lisp_Object time_list
;
5574 if (!NILP (time_list
))
5575 current_buffer
->modtime
= cons_to_long (time_list
);
5578 register Lisp_Object filename
;
5580 Lisp_Object handler
;
5582 filename
= Fexpand_file_name (current_buffer
->filename
, Qnil
);
5584 /* If the file name has special constructs in it,
5585 call the corresponding file handler. */
5586 handler
= Ffind_file_name_handler (filename
, Qset_visited_file_modtime
);
5587 if (!NILP (handler
))
5588 /* The handler can find the file name the same way we did. */
5589 return call2 (handler
, Qset_visited_file_modtime
, Qnil
);
5591 filename
= ENCODE_FILE (filename
);
5593 if (stat (SDATA (filename
), &st
) >= 0)
5594 current_buffer
->modtime
= st
.st_mtime
;
5601 auto_save_error (error
)
5604 Lisp_Object args
[3], msg
;
5606 struct gcpro gcpro1
;
5610 args
[0] = build_string ("Auto-saving %s: %s");
5611 args
[1] = current_buffer
->name
;
5612 args
[2] = Ferror_message_string (error
);
5613 msg
= Fformat (3, args
);
5615 nbytes
= SBYTES (msg
);
5617 for (i
= 0; i
< 3; ++i
)
5620 message2 (SDATA (msg
), nbytes
, STRING_MULTIBYTE (msg
));
5622 message2_nolog (SDATA (msg
), nbytes
, STRING_MULTIBYTE (msg
));
5623 Fsleep_for (make_number (1), Qnil
);
5635 /* Get visited file's mode to become the auto save file's mode. */
5636 if (! NILP (current_buffer
->filename
)
5637 && stat (SDATA (current_buffer
->filename
), &st
) >= 0)
5638 /* But make sure we can overwrite it later! */
5639 auto_save_mode_bits
= st
.st_mode
| 0600;
5641 auto_save_mode_bits
= 0666;
5644 Fwrite_region (Qnil
, Qnil
,
5645 current_buffer
->auto_save_file_name
,
5646 Qnil
, Qlambda
, Qnil
, Qnil
);
5650 do_auto_save_unwind (stream
) /* used as unwind-protect function */
5655 fclose ((FILE *) (XFASTINT (XCAR (stream
)) << 16
5656 | XFASTINT (XCDR (stream
))));
5661 do_auto_save_unwind_1 (value
) /* used as unwind-protect function */
5664 minibuffer_auto_raise
= XINT (value
);
5669 do_auto_save_make_dir (dir
)
5672 return call2 (Qmake_directory
, dir
, Qt
);
5676 do_auto_save_eh (ignore
)
5682 DEFUN ("do-auto-save", Fdo_auto_save
, Sdo_auto_save
, 0, 2, "",
5683 doc
: /* Auto-save all buffers that need it.
5684 This is all buffers that have auto-saving enabled
5685 and are changed since last auto-saved.
5686 Auto-saving writes the buffer into a file
5687 so that your editing is not lost if the system crashes.
5688 This file is not the file you visited; that changes only when you save.
5689 Normally we run the normal hook `auto-save-hook' before saving.
5691 A non-nil NO-MESSAGE argument means do not print any message if successful.
5692 A non-nil CURRENT-ONLY argument means save only current buffer. */)
5693 (no_message
, current_only
)
5694 Lisp_Object no_message
, current_only
;
5696 struct buffer
*old
= current_buffer
, *b
;
5697 Lisp_Object tail
, buf
;
5699 int do_handled_files
;
5702 Lisp_Object lispstream
;
5703 int count
= SPECPDL_INDEX ();
5704 int orig_minibuffer_auto_raise
= minibuffer_auto_raise
;
5705 int old_message_p
= 0;
5706 struct gcpro gcpro1
, gcpro2
;
5708 if (max_specpdl_size
< specpdl_size
+ 40)
5709 max_specpdl_size
= specpdl_size
+ 40;
5714 if (NILP (no_message
))
5716 old_message_p
= push_message ();
5717 record_unwind_protect (pop_message_unwind
, Qnil
);
5720 /* Ordinarily don't quit within this function,
5721 but don't make it impossible to quit (in case we get hung in I/O). */
5725 /* No GCPRO needed, because (when it matters) all Lisp_Object variables
5726 point to non-strings reached from Vbuffer_alist. */
5728 if (!NILP (Vrun_hooks
))
5729 call1 (Vrun_hooks
, intern ("auto-save-hook"));
5731 if (STRINGP (Vauto_save_list_file_name
))
5733 Lisp_Object listfile
;
5735 listfile
= Fexpand_file_name (Vauto_save_list_file_name
, Qnil
);
5737 /* Don't try to create the directory when shutting down Emacs,
5738 because creating the directory might signal an error, and
5739 that would leave Emacs in a strange state. */
5740 if (!NILP (Vrun_hooks
))
5744 GCPRO2 (dir
, listfile
);
5745 dir
= Ffile_name_directory (listfile
);
5746 if (NILP (Ffile_directory_p (dir
)))
5747 internal_condition_case_1 (do_auto_save_make_dir
,
5748 dir
, Fcons (Fcons (Qfile_error
, Qnil
), Qnil
),
5753 stream
= fopen (SDATA (listfile
), "w");
5756 /* Arrange to close that file whether or not we get an error.
5757 Also reset auto_saving to 0. */
5758 lispstream
= Fcons (Qnil
, Qnil
);
5759 XSETCARFASTINT (lispstream
, (EMACS_UINT
)stream
>> 16);
5760 XSETCDRFASTINT (lispstream
, (EMACS_UINT
)stream
& 0xffff);
5771 record_unwind_protect (do_auto_save_unwind
, lispstream
);
5772 record_unwind_protect (do_auto_save_unwind_1
,
5773 make_number (minibuffer_auto_raise
));
5774 minibuffer_auto_raise
= 0;
5777 /* First, save all files which don't have handlers. If Emacs is
5778 crashing, the handlers may tweak what is causing Emacs to crash
5779 in the first place, and it would be a shame if Emacs failed to
5780 autosave perfectly ordinary files because it couldn't handle some
5782 for (do_handled_files
= 0; do_handled_files
< 2; do_handled_files
++)
5783 for (tail
= Vbuffer_alist
; GC_CONSP (tail
); tail
= XCDR (tail
))
5785 buf
= XCDR (XCAR (tail
));
5788 /* Record all the buffers that have auto save mode
5789 in the special file that lists them. For each of these buffers,
5790 Record visited name (if any) and auto save name. */
5791 if (STRINGP (b
->auto_save_file_name
)
5792 && stream
!= NULL
&& do_handled_files
== 0)
5794 if (!NILP (b
->filename
))
5796 fwrite (SDATA (b
->filename
), 1,
5797 SBYTES (b
->filename
), stream
);
5799 putc ('\n', stream
);
5800 fwrite (SDATA (b
->auto_save_file_name
), 1,
5801 SBYTES (b
->auto_save_file_name
), stream
);
5802 putc ('\n', stream
);
5805 if (!NILP (current_only
)
5806 && b
!= current_buffer
)
5809 /* Don't auto-save indirect buffers.
5810 The base buffer takes care of it. */
5814 /* Check for auto save enabled
5815 and file changed since last auto save
5816 and file changed since last real save. */
5817 if (STRINGP (b
->auto_save_file_name
)
5818 && BUF_SAVE_MODIFF (b
) < BUF_MODIFF (b
)
5819 && b
->auto_save_modified
< BUF_MODIFF (b
)
5820 /* -1 means we've turned off autosaving for a while--see below. */
5821 && XINT (b
->save_length
) >= 0
5822 && (do_handled_files
5823 || NILP (Ffind_file_name_handler (b
->auto_save_file_name
,
5826 EMACS_TIME before_time
, after_time
;
5828 EMACS_GET_TIME (before_time
);
5830 /* If we had a failure, don't try again for 20 minutes. */
5831 if (b
->auto_save_failure_time
>= 0
5832 && EMACS_SECS (before_time
) - b
->auto_save_failure_time
< 1200)
5835 if ((XFASTINT (b
->save_length
) * 10
5836 > (BUF_Z (b
) - BUF_BEG (b
)) * 13)
5837 /* A short file is likely to change a large fraction;
5838 spare the user annoying messages. */
5839 && XFASTINT (b
->save_length
) > 5000
5840 /* These messages are frequent and annoying for `*mail*'. */
5841 && !EQ (b
->filename
, Qnil
)
5842 && NILP (no_message
))
5844 /* It has shrunk too much; turn off auto-saving here. */
5845 minibuffer_auto_raise
= orig_minibuffer_auto_raise
;
5846 message_with_string ("Buffer %s has shrunk a lot; auto save disabled in that buffer until next real save",
5848 minibuffer_auto_raise
= 0;
5849 /* Turn off auto-saving until there's a real save,
5850 and prevent any more warnings. */
5851 XSETINT (b
->save_length
, -1);
5852 Fsleep_for (make_number (1), Qnil
);
5855 set_buffer_internal (b
);
5856 if (!auto_saved
&& NILP (no_message
))
5857 message1 ("Auto-saving...");
5858 internal_condition_case (auto_save_1
, Qt
, auto_save_error
);
5860 b
->auto_save_modified
= BUF_MODIFF (b
);
5861 XSETFASTINT (current_buffer
->save_length
, Z
- BEG
);
5862 set_buffer_internal (old
);
5864 EMACS_GET_TIME (after_time
);
5866 /* If auto-save took more than 60 seconds,
5867 assume it was an NFS failure that got a timeout. */
5868 if (EMACS_SECS (after_time
) - EMACS_SECS (before_time
) > 60)
5869 b
->auto_save_failure_time
= EMACS_SECS (after_time
);
5873 /* Prevent another auto save till enough input events come in. */
5874 record_auto_save ();
5876 if (auto_saved
&& NILP (no_message
))
5880 /* If we are going to restore an old message,
5881 give time to read ours. */
5882 sit_for (1, 0, 0, 0, 0);
5886 /* If we displayed a message and then restored a state
5887 with no message, leave a "done" message on the screen. */
5888 message1 ("Auto-saving...done");
5893 /* This restores the message-stack status. */
5894 unbind_to (count
, Qnil
);
5898 DEFUN ("set-buffer-auto-saved", Fset_buffer_auto_saved
,
5899 Sset_buffer_auto_saved
, 0, 0, 0,
5900 doc
: /* Mark current buffer as auto-saved with its current text.
5901 No auto-save file will be written until the buffer changes again. */)
5904 current_buffer
->auto_save_modified
= MODIFF
;
5905 XSETFASTINT (current_buffer
->save_length
, Z
- BEG
);
5906 current_buffer
->auto_save_failure_time
= -1;
5910 DEFUN ("clear-buffer-auto-save-failure", Fclear_buffer_auto_save_failure
,
5911 Sclear_buffer_auto_save_failure
, 0, 0, 0,
5912 doc
: /* Clear any record of a recent auto-save failure in the current buffer. */)
5915 current_buffer
->auto_save_failure_time
= -1;
5919 DEFUN ("recent-auto-save-p", Frecent_auto_save_p
, Srecent_auto_save_p
,
5921 doc
: /* Return t if buffer has been auto-saved since last read in or saved. */)
5924 return (SAVE_MODIFF
< current_buffer
->auto_save_modified
) ? Qt
: Qnil
;
5927 /* Reading and completing file names */
5928 extern Lisp_Object
Ffile_name_completion (), Ffile_name_all_completions ();
5930 /* In the string VAL, change each $ to $$ and return the result. */
5933 double_dollars (val
)
5936 register const unsigned char *old
;
5937 register unsigned char *new;
5941 osize
= SBYTES (val
);
5943 /* Count the number of $ characters. */
5944 for (n
= osize
, count
= 0, old
= SDATA (val
); n
> 0; n
--)
5945 if (*old
++ == '$') count
++;
5949 val
= make_uninit_multibyte_string (SCHARS (val
) + count
,
5952 for (n
= osize
; n
> 0; n
--)
5966 read_file_name_cleanup (arg
)
5969 return (current_buffer
->directory
= arg
);
5972 DEFUN ("read-file-name-internal", Fread_file_name_internal
, Sread_file_name_internal
,
5974 doc
: /* Internal subroutine for read-file-name. Do not call this. */)
5975 (string
, dir
, action
)
5976 Lisp_Object string
, dir
, action
;
5977 /* action is nil for complete, t for return list of completions,
5978 lambda for verify final value */
5980 Lisp_Object name
, specdir
, realdir
, val
, orig_string
;
5982 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
5984 CHECK_STRING (string
);
5991 /* No need to protect ACTION--we only compare it with t and nil. */
5992 GCPRO5 (string
, realdir
, name
, specdir
, orig_string
);
5994 if (SCHARS (string
) == 0)
5996 if (EQ (action
, Qlambda
))
6004 orig_string
= string
;
6005 string
= Fsubstitute_in_file_name (string
);
6006 changed
= NILP (Fstring_equal (string
, orig_string
));
6007 name
= Ffile_name_nondirectory (string
);
6008 val
= Ffile_name_directory (string
);
6010 realdir
= Fexpand_file_name (val
, realdir
);
6015 specdir
= Ffile_name_directory (string
);
6016 val
= Ffile_name_completion (name
, realdir
);
6021 return double_dollars (string
);
6025 if (!NILP (specdir
))
6026 val
= concat2 (specdir
, val
);
6028 return double_dollars (val
);
6031 #endif /* not VMS */
6035 if (EQ (action
, Qt
))
6037 Lisp_Object all
= Ffile_name_all_completions (name
, realdir
);
6041 if (NILP (Vread_file_name_predicate
)
6042 || EQ (Vread_file_name_predicate
, Qfile_exists_p
))
6046 if (EQ (Vread_file_name_predicate
, Qfile_directory_p
))
6048 /* Brute-force speed up for directory checking:
6049 Discard strings which don't end in a slash. */
6050 for (comp
= Qnil
; CONSP (all
); all
= XCDR (all
))
6052 Lisp_Object tem
= XCAR (all
);
6054 if (STRINGP (tem
) &&
6055 (len
= SCHARS (tem
), len
> 0) &&
6056 IS_DIRECTORY_SEP (SREF (tem
, len
-1)))
6057 comp
= Fcons (tem
, comp
);
6063 /* Must do it the hard (and slow) way. */
6064 GCPRO3 (all
, comp
, specdir
);
6065 count
= SPECPDL_INDEX ();
6066 record_unwind_protect (read_file_name_cleanup
, current_buffer
->directory
);
6067 current_buffer
->directory
= realdir
;
6068 for (comp
= Qnil
; CONSP (all
); all
= XCDR (all
))
6069 if (!NILP (call1 (Vread_file_name_predicate
, XCAR (all
))))
6070 comp
= Fcons (XCAR (all
), comp
);
6071 unbind_to (count
, Qnil
);
6074 return Fnreverse (comp
);
6077 /* Only other case actually used is ACTION = lambda */
6079 /* Supposedly this helps commands such as `cd' that read directory names,
6080 but can someone explain how it helps them? -- RMS */
6081 if (SCHARS (name
) == 0)
6084 if (!NILP (Vread_file_name_predicate
))
6085 return call1 (Vread_file_name_predicate
, string
);
6086 return Ffile_exists_p (string
);
6089 DEFUN ("read-file-name", Fread_file_name
, Sread_file_name
, 1, 6, 0,
6090 doc
: /* Read file name, prompting with PROMPT and completing in directory DIR.
6091 Value is not expanded---you must call `expand-file-name' yourself.
6092 Default name to DEFAULT-FILENAME if user enters a null string.
6093 (If DEFAULT-FILENAME is omitted, the visited file name is used,
6094 except that if INITIAL is specified, that combined with DIR is used.)
6095 Fourth arg MUSTMATCH non-nil means require existing file's name.
6096 Non-nil and non-t means also require confirmation after completion.
6097 Fifth arg INITIAL specifies text to start with.
6098 If optional sixth arg PREDICATE is non-nil, possible completions and the
6099 resulting file name must satisfy (funcall PREDICATE NAME).
6100 DIR defaults to current buffer's directory default.
6102 If this command was invoked with the mouse, use a file dialog box if
6103 `use-dialog-box' is non-nil, and the window system or X toolkit in use
6104 provides a file dialog box. */)
6105 (prompt
, dir
, default_filename
, mustmatch
, initial
, predicate
)
6106 Lisp_Object prompt
, dir
, default_filename
, mustmatch
, initial
, predicate
;
6108 Lisp_Object val
, insdef
, tem
;
6109 struct gcpro gcpro1
, gcpro2
;
6110 register char *homedir
;
6111 Lisp_Object decoded_homedir
;
6112 int replace_in_history
= 0;
6113 int add_to_history
= 0;
6117 dir
= current_buffer
->directory
;
6118 if (NILP (default_filename
))
6119 default_filename
= !NILP (initial
)
6120 ? Fexpand_file_name (initial
, dir
)
6121 : current_buffer
->filename
;
6123 /* If dir starts with user's homedir, change that to ~. */
6124 homedir
= (char *) egetenv ("HOME");
6126 /* homedir can be NULL in temacs, since Vprocess_environment is not
6127 yet set up. We shouldn't crash in that case. */
6130 homedir
= strcpy (alloca (strlen (homedir
) + 1), homedir
);
6131 CORRECT_DIR_SEPS (homedir
);
6136 = DECODE_FILE (make_unibyte_string (homedir
, strlen (homedir
)));
6139 && !strncmp (SDATA (decoded_homedir
), SDATA (dir
),
6140 SBYTES (decoded_homedir
))
6141 && IS_DIRECTORY_SEP (SREF (dir
, SBYTES (decoded_homedir
))))
6143 dir
= Fsubstring (dir
, make_number (SCHARS (decoded_homedir
)), Qnil
);
6144 dir
= concat2 (build_string ("~"), dir
);
6146 /* Likewise for default_filename. */
6148 && STRINGP (default_filename
)
6149 && !strncmp (SDATA (decoded_homedir
), SDATA (default_filename
),
6150 SBYTES (decoded_homedir
))
6151 && IS_DIRECTORY_SEP (SREF (default_filename
, SBYTES (decoded_homedir
))))
6154 = Fsubstring (default_filename
,
6155 make_number (SCHARS (decoded_homedir
)), Qnil
);
6156 default_filename
= concat2 (build_string ("~"), default_filename
);
6158 if (!NILP (default_filename
))
6160 CHECK_STRING (default_filename
);
6161 default_filename
= double_dollars (default_filename
);
6164 if (insert_default_directory
&& STRINGP (dir
))
6167 if (!NILP (initial
))
6169 Lisp_Object args
[2], pos
;
6173 insdef
= Fconcat (2, args
);
6174 pos
= make_number (SCHARS (double_dollars (dir
)));
6175 insdef
= Fcons (double_dollars (insdef
), pos
);
6178 insdef
= double_dollars (insdef
);
6180 else if (STRINGP (initial
))
6181 insdef
= Fcons (double_dollars (initial
), make_number (0));
6185 if (!NILP (Vread_file_name_function
))
6187 Lisp_Object args
[7];
6189 GCPRO2 (insdef
, default_filename
);
6190 args
[0] = Vread_file_name_function
;
6193 args
[3] = default_filename
;
6194 args
[4] = mustmatch
;
6196 args
[6] = predicate
;
6197 RETURN_UNGCPRO (Ffuncall (7, args
));
6200 count
= SPECPDL_INDEX ();
6202 specbind (intern ("completion-ignore-case"), Qt
);
6205 specbind (intern ("minibuffer-completing-file-name"), Qt
);
6206 specbind (intern ("read-file-name-predicate"),
6207 (NILP (predicate
) ? Qfile_exists_p
: predicate
));
6209 GCPRO2 (insdef
, default_filename
);
6211 #if defined (USE_MOTIF) || defined (HAVE_NTGUI) || defined (USE_GTK)
6212 if ((NILP (last_nonmenu_event
) || CONSP (last_nonmenu_event
))
6216 /* If DIR contains a file name, split it. */
6218 file
= Ffile_name_nondirectory (dir
);
6219 if (SCHARS (file
) && NILP (default_filename
))
6221 default_filename
= file
;
6222 dir
= Ffile_name_directory (dir
);
6224 if (!NILP(default_filename
))
6225 default_filename
= Fexpand_file_name (default_filename
, dir
);
6226 val
= Fx_file_dialog (prompt
, dir
, default_filename
, mustmatch
);
6231 val
= Fcompleting_read (prompt
, intern ("read-file-name-internal"),
6232 dir
, mustmatch
, insdef
,
6233 Qfile_name_history
, default_filename
, Qnil
);
6235 tem
= Fsymbol_value (Qfile_name_history
);
6236 if (CONSP (tem
) && EQ (XCAR (tem
), val
))
6237 replace_in_history
= 1;
6239 /* If Fcompleting_read returned the inserted default string itself
6240 (rather than a new string with the same contents),
6241 it has to mean that the user typed RET with the minibuffer empty.
6242 In that case, we really want to return ""
6243 so that commands such as set-visited-file-name can distinguish. */
6244 if (EQ (val
, default_filename
))
6246 /* In this case, Fcompleting_read has not added an element
6247 to the history. Maybe we should. */
6248 if (! replace_in_history
)
6254 unbind_to (count
, Qnil
);
6257 error ("No file name specified");
6259 tem
= Fstring_equal (val
, CONSP (insdef
) ? XCAR (insdef
) : insdef
);
6261 if (!NILP (tem
) && !NILP (default_filename
))
6262 val
= default_filename
;
6263 else if (SCHARS (val
) == 0 && NILP (insdef
))
6265 if (!NILP (default_filename
))
6266 val
= default_filename
;
6268 error ("No default file name");
6270 val
= Fsubstitute_in_file_name (val
);
6272 if (replace_in_history
)
6273 /* Replace what Fcompleting_read added to the history
6274 with what we will actually return. */
6275 XSETCAR (Fsymbol_value (Qfile_name_history
), double_dollars (val
));
6276 else if (add_to_history
)
6278 /* Add the value to the history--but not if it matches
6279 the last value already there. */
6280 Lisp_Object val1
= double_dollars (val
);
6281 tem
= Fsymbol_value (Qfile_name_history
);
6282 if (! CONSP (tem
) || NILP (Fequal (XCAR (tem
), val1
)))
6283 Fset (Qfile_name_history
,
6294 /* Must be set before any path manipulation is performed. */
6295 XSETFASTINT (Vdirectory_sep_char
, '/');
6302 Qexpand_file_name
= intern ("expand-file-name");
6303 Qsubstitute_in_file_name
= intern ("substitute-in-file-name");
6304 Qdirectory_file_name
= intern ("directory-file-name");
6305 Qfile_name_directory
= intern ("file-name-directory");
6306 Qfile_name_nondirectory
= intern ("file-name-nondirectory");
6307 Qunhandled_file_name_directory
= intern ("unhandled-file-name-directory");
6308 Qfile_name_as_directory
= intern ("file-name-as-directory");
6309 Qcopy_file
= intern ("copy-file");
6310 Qmake_directory_internal
= intern ("make-directory-internal");
6311 Qmake_directory
= intern ("make-directory");
6312 Qdelete_directory
= intern ("delete-directory");
6313 Qdelete_file
= intern ("delete-file");
6314 Qrename_file
= intern ("rename-file");
6315 Qadd_name_to_file
= intern ("add-name-to-file");
6316 Qmake_symbolic_link
= intern ("make-symbolic-link");
6317 Qfile_exists_p
= intern ("file-exists-p");
6318 Qfile_executable_p
= intern ("file-executable-p");
6319 Qfile_readable_p
= intern ("file-readable-p");
6320 Qfile_writable_p
= intern ("file-writable-p");
6321 Qfile_symlink_p
= intern ("file-symlink-p");
6322 Qaccess_file
= intern ("access-file");
6323 Qfile_directory_p
= intern ("file-directory-p");
6324 Qfile_regular_p
= intern ("file-regular-p");
6325 Qfile_accessible_directory_p
= intern ("file-accessible-directory-p");
6326 Qfile_modes
= intern ("file-modes");
6327 Qset_file_modes
= intern ("set-file-modes");
6328 Qfile_newer_than_file_p
= intern ("file-newer-than-file-p");
6329 Qinsert_file_contents
= intern ("insert-file-contents");
6330 Qwrite_region
= intern ("write-region");
6331 Qverify_visited_file_modtime
= intern ("verify-visited-file-modtime");
6332 Qset_visited_file_modtime
= intern ("set-visited-file-modtime");
6333 Qauto_save_coding
= intern ("auto-save-coding");
6335 staticpro (&Qexpand_file_name
);
6336 staticpro (&Qsubstitute_in_file_name
);
6337 staticpro (&Qdirectory_file_name
);
6338 staticpro (&Qfile_name_directory
);
6339 staticpro (&Qfile_name_nondirectory
);
6340 staticpro (&Qunhandled_file_name_directory
);
6341 staticpro (&Qfile_name_as_directory
);
6342 staticpro (&Qcopy_file
);
6343 staticpro (&Qmake_directory_internal
);
6344 staticpro (&Qmake_directory
);
6345 staticpro (&Qdelete_directory
);
6346 staticpro (&Qdelete_file
);
6347 staticpro (&Qrename_file
);
6348 staticpro (&Qadd_name_to_file
);
6349 staticpro (&Qmake_symbolic_link
);
6350 staticpro (&Qfile_exists_p
);
6351 staticpro (&Qfile_executable_p
);
6352 staticpro (&Qfile_readable_p
);
6353 staticpro (&Qfile_writable_p
);
6354 staticpro (&Qaccess_file
);
6355 staticpro (&Qfile_symlink_p
);
6356 staticpro (&Qfile_directory_p
);
6357 staticpro (&Qfile_regular_p
);
6358 staticpro (&Qfile_accessible_directory_p
);
6359 staticpro (&Qfile_modes
);
6360 staticpro (&Qset_file_modes
);
6361 staticpro (&Qfile_newer_than_file_p
);
6362 staticpro (&Qinsert_file_contents
);
6363 staticpro (&Qwrite_region
);
6364 staticpro (&Qverify_visited_file_modtime
);
6365 staticpro (&Qset_visited_file_modtime
);
6366 staticpro (&Qauto_save_coding
);
6368 Qfile_name_history
= intern ("file-name-history");
6369 Fset (Qfile_name_history
, Qnil
);
6370 staticpro (&Qfile_name_history
);
6372 Qfile_error
= intern ("file-error");
6373 staticpro (&Qfile_error
);
6374 Qfile_already_exists
= intern ("file-already-exists");
6375 staticpro (&Qfile_already_exists
);
6376 Qfile_date_error
= intern ("file-date-error");
6377 staticpro (&Qfile_date_error
);
6378 Qexcl
= intern ("excl");
6382 Qfind_buffer_file_type
= intern ("find-buffer-file-type");
6383 staticpro (&Qfind_buffer_file_type
);
6386 DEFVAR_LISP ("file-name-coding-system", &Vfile_name_coding_system
,
6387 doc
: /* *Coding system for encoding file names.
6388 If it is nil, `default-file-name-coding-system' (which see) is used. */);
6389 Vfile_name_coding_system
= Qnil
;
6391 DEFVAR_LISP ("default-file-name-coding-system",
6392 &Vdefault_file_name_coding_system
,
6393 doc
: /* Default coding system for encoding file names.
6394 This variable is used only when `file-name-coding-system' is nil.
6396 This variable is set/changed by the command `set-language-environment'.
6397 User should not set this variable manually,
6398 instead use `file-name-coding-system' to get a constant encoding
6399 of file names regardless of the current language environment. */);
6400 Vdefault_file_name_coding_system
= Qnil
;
6402 DEFVAR_LISP ("auto-save-file-format", &Vauto_save_file_format
,
6403 doc
: /* *Format in which to write auto-save files.
6404 Should be a list of symbols naming formats that are defined in `format-alist'.
6405 If it is t, which is the default, auto-save files are written in the
6406 same format as a regular save would use. */);
6407 Vauto_save_file_format
= Qt
;
6409 Qformat_decode
= intern ("format-decode");
6410 staticpro (&Qformat_decode
);
6411 Qformat_annotate_function
= intern ("format-annotate-function");
6412 staticpro (&Qformat_annotate_function
);
6413 Qafter_insert_file_set_coding
= intern ("after-insert-file-set-coding");
6414 staticpro (&Qafter_insert_file_set_coding
);
6416 Qcar_less_than_car
= intern ("car-less-than-car");
6417 staticpro (&Qcar_less_than_car
);
6419 Fput (Qfile_error
, Qerror_conditions
,
6420 Fcons (Qfile_error
, Fcons (Qerror
, Qnil
)));
6421 Fput (Qfile_error
, Qerror_message
,
6422 build_string ("File error"));
6424 Fput (Qfile_already_exists
, Qerror_conditions
,
6425 Fcons (Qfile_already_exists
,
6426 Fcons (Qfile_error
, Fcons (Qerror
, Qnil
))));
6427 Fput (Qfile_already_exists
, Qerror_message
,
6428 build_string ("File already exists"));
6430 Fput (Qfile_date_error
, Qerror_conditions
,
6431 Fcons (Qfile_date_error
,
6432 Fcons (Qfile_error
, Fcons (Qerror
, Qnil
))));
6433 Fput (Qfile_date_error
, Qerror_message
,
6434 build_string ("Cannot set file date"));
6436 DEFVAR_LISP ("read-file-name-function", &Vread_file_name_function
,
6437 doc
: /* If this is non-nil, `read-file-name' does its work by calling this function. */);
6438 Vread_file_name_function
= Qnil
;
6440 DEFVAR_LISP ("read-file-name-predicate", &Vread_file_name_predicate
,
6441 doc
: /* Current predicate used by `read-file-name-internal'. */);
6442 Vread_file_name_predicate
= Qnil
;
6444 DEFVAR_BOOL ("insert-default-directory", &insert_default_directory
,
6445 doc
: /* *Non-nil means when reading a filename start with default dir in minibuffer. */);
6446 insert_default_directory
= 1;
6448 DEFVAR_BOOL ("vms-stmlf-recfm", &vms_stmlf_recfm
,
6449 doc
: /* *Non-nil means write new files with record format `stmlf'.
6450 nil means use format `var'. This variable is meaningful only on VMS. */);
6451 vms_stmlf_recfm
= 0;
6453 DEFVAR_LISP ("directory-sep-char", &Vdirectory_sep_char
,
6454 doc
: /* Directory separator character for built-in functions that return file names.
6455 The value is always ?/. Don't use this variable, just use `/'. */);
6457 DEFVAR_LISP ("file-name-handler-alist", &Vfile_name_handler_alist
,
6458 doc
: /* *Alist of elements (REGEXP . HANDLER) for file names handled specially.
6459 If a file name matches REGEXP, then all I/O on that file is done by calling
6462 The first argument given to HANDLER is the name of the I/O primitive
6463 to be handled; the remaining arguments are the arguments that were
6464 passed to that primitive. For example, if you do
6465 (file-exists-p FILENAME)
6466 and FILENAME is handled by HANDLER, then HANDLER is called like this:
6467 (funcall HANDLER 'file-exists-p FILENAME)
6468 The function `find-file-name-handler' checks this list for a handler
6469 for its argument. */);
6470 Vfile_name_handler_alist
= Qnil
;
6472 DEFVAR_LISP ("set-auto-coding-function",
6473 &Vset_auto_coding_function
,
6474 doc
: /* If non-nil, a function to call to decide a coding system of file.
6475 Two arguments are passed to this function: the file name
6476 and the length of a file contents following the point.
6477 This function should return a coding system to decode the file contents.
6478 It should check the file name against `auto-coding-alist'.
6479 If no coding system is decided, it should check a coding system
6480 specified in the heading lines with the format:
6481 -*- ... coding: CODING-SYSTEM; ... -*-
6482 or local variable spec of the tailing lines with `coding:' tag. */);
6483 Vset_auto_coding_function
= Qnil
;
6485 DEFVAR_LISP ("after-insert-file-functions", &Vafter_insert_file_functions
,
6486 doc
: /* A list of functions to be called at the end of `insert-file-contents'.
6487 Each is passed one argument, the number of characters inserted.
6488 It should return the new character count, and leave point the same.
6489 If `insert-file-contents' is intercepted by a handler from
6490 `file-name-handler-alist', that handler is responsible for calling the
6491 functions in `after-insert-file-functions' if appropriate. */);
6492 Vafter_insert_file_functions
= Qnil
;
6494 DEFVAR_LISP ("write-region-annotate-functions", &Vwrite_region_annotate_functions
,
6495 doc
: /* A list of functions to be called at the start of `write-region'.
6496 Each is passed two arguments, START and END as for `write-region'.
6497 These are usually two numbers but not always; see the documentation
6498 for `write-region'. The function should return a list of pairs
6499 of the form (POSITION . STRING), consisting of strings to be effectively
6500 inserted at the specified positions of the file being written (1 means to
6501 insert before the first byte written). The POSITIONs must be sorted into
6502 increasing order. If there are several functions in the list, the several
6503 lists are merged destructively. Alternatively, the function can return
6504 with a different buffer current; in that case it should pay attention
6505 to the annotations returned by previous functions and listed in
6506 `write-region-annotations-so-far'.*/);
6507 Vwrite_region_annotate_functions
= Qnil
;
6508 staticpro (&Qwrite_region_annotate_functions
);
6509 Qwrite_region_annotate_functions
6510 = intern ("write-region-annotate-functions");
6512 DEFVAR_LISP ("write-region-annotations-so-far",
6513 &Vwrite_region_annotations_so_far
,
6514 doc
: /* When an annotation function is called, this holds the previous annotations.
6515 These are the annotations made by other annotation functions
6516 that were already called. See also `write-region-annotate-functions'. */);
6517 Vwrite_region_annotations_so_far
= Qnil
;
6519 DEFVAR_LISP ("inhibit-file-name-handlers", &Vinhibit_file_name_handlers
,
6520 doc
: /* A list of file name handlers that temporarily should not be used.
6521 This applies only to the operation `inhibit-file-name-operation'. */);
6522 Vinhibit_file_name_handlers
= Qnil
;
6524 DEFVAR_LISP ("inhibit-file-name-operation", &Vinhibit_file_name_operation
,
6525 doc
: /* The operation for which `inhibit-file-name-handlers' is applicable. */);
6526 Vinhibit_file_name_operation
= Qnil
;
6528 DEFVAR_LISP ("auto-save-list-file-name", &Vauto_save_list_file_name
,
6529 doc
: /* File name in which we write a list of all auto save file names.
6530 This variable is initialized automatically from `auto-save-list-file-prefix'
6531 shortly after Emacs reads your `.emacs' file, if you have not yet given it
6532 a non-nil value. */);
6533 Vauto_save_list_file_name
= Qnil
;
6535 defsubr (&Sfind_file_name_handler
);
6536 defsubr (&Sfile_name_directory
);
6537 defsubr (&Sfile_name_nondirectory
);
6538 defsubr (&Sunhandled_file_name_directory
);
6539 defsubr (&Sfile_name_as_directory
);
6540 defsubr (&Sdirectory_file_name
);
6541 defsubr (&Smake_temp_name
);
6542 defsubr (&Sexpand_file_name
);
6543 defsubr (&Ssubstitute_in_file_name
);
6544 defsubr (&Scopy_file
);
6545 defsubr (&Smake_directory_internal
);
6546 defsubr (&Sdelete_directory
);
6547 defsubr (&Sdelete_file
);
6548 defsubr (&Srename_file
);
6549 defsubr (&Sadd_name_to_file
);
6551 defsubr (&Smake_symbolic_link
);
6552 #endif /* S_IFLNK */
6554 defsubr (&Sdefine_logical_name
);
6557 defsubr (&Ssysnetunam
);
6558 #endif /* HPUX_NET */
6559 defsubr (&Sfile_name_absolute_p
);
6560 defsubr (&Sfile_exists_p
);
6561 defsubr (&Sfile_executable_p
);
6562 defsubr (&Sfile_readable_p
);
6563 defsubr (&Sfile_writable_p
);
6564 defsubr (&Saccess_file
);
6565 defsubr (&Sfile_symlink_p
);
6566 defsubr (&Sfile_directory_p
);
6567 defsubr (&Sfile_accessible_directory_p
);
6568 defsubr (&Sfile_regular_p
);
6569 defsubr (&Sfile_modes
);
6570 defsubr (&Sset_file_modes
);
6571 defsubr (&Sset_default_file_modes
);
6572 defsubr (&Sdefault_file_modes
);
6573 defsubr (&Sfile_newer_than_file_p
);
6574 defsubr (&Sinsert_file_contents
);
6575 defsubr (&Swrite_region
);
6576 defsubr (&Scar_less_than_car
);
6577 defsubr (&Sverify_visited_file_modtime
);
6578 defsubr (&Sclear_visited_file_modtime
);
6579 defsubr (&Svisited_file_modtime
);
6580 defsubr (&Sset_visited_file_modtime
);
6581 defsubr (&Sdo_auto_save
);
6582 defsubr (&Sset_buffer_auto_saved
);
6583 defsubr (&Sclear_buffer_auto_save_failure
);
6584 defsubr (&Srecent_auto_save_p
);
6586 defsubr (&Sread_file_name_internal
);
6587 defsubr (&Sread_file_name
);
6590 defsubr (&Sunix_sync
);