1 /* File IO for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1996,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
30 #include <sys/types.h>
37 #if !defined (S_ISLNK) && defined (S_IFLNK)
38 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
41 #if !defined (S_ISFIFO) && defined (S_IFIFO)
42 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
45 #if !defined (S_ISREG) && defined (S_IFREG)
46 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
75 #include "intervals.h"
86 #endif /* not WINDOWSNT */
90 #include <sys/param.h>
98 #define CORRECT_DIR_SEPS(s) \
99 do { if ('/' == DIRECTORY_SEP) dostounix_filename (s); \
100 else unixtodos_filename (s); \
102 /* On Windows, drive letters must be alphabetic - on DOS, the Netware
103 redirector allows the six letters between 'Z' and 'a' as well. */
105 #define IS_DRIVE(x) ((x) >= 'A' && (x) <= 'z')
108 #define IS_DRIVE(x) isalpha (x)
110 /* Need to lower-case the drive letter, or else expanded
111 filenames will sometimes compare inequal, because
112 `expand-file-name' doesn't always down-case the drive letter. */
113 #define DRIVE_LETTER(x) (tolower (x))
134 #include "commands.h"
135 extern int use_dialog_box
;
136 extern int use_file_dialog
;
150 #ifndef FILE_SYSTEM_CASE
151 #define FILE_SYSTEM_CASE(filename) (filename)
154 /* Nonzero during writing of auto-save files */
157 /* Set by auto_save_1 to mode of original file so Fwrite_region will create
158 a new file with the same mode as the original */
159 int auto_save_mode_bits
;
161 /* The symbol bound to coding-system-for-read when
162 insert-file-contents is called for recovering a file. This is not
163 an actual coding system name, but just an indicator to tell
164 insert-file-contents to use `emacs-mule' with a special flag for
165 auto saving and recovering a file. */
166 Lisp_Object Qauto_save_coding
;
168 /* Coding system for file names, or nil if none. */
169 Lisp_Object Vfile_name_coding_system
;
171 /* Coding system for file names used only when
172 Vfile_name_coding_system is nil. */
173 Lisp_Object Vdefault_file_name_coding_system
;
175 /* Alist of elements (REGEXP . HANDLER) for file names
176 whose I/O is done with a special handler. */
177 Lisp_Object Vfile_name_handler_alist
;
179 /* Property name of a file name handler,
180 which gives a list of operations it handles.. */
181 Lisp_Object Qoperations
;
183 /* Lisp functions for translating file formats */
184 Lisp_Object Qformat_decode
, Qformat_annotate_function
;
186 /* Function to be called to decide a coding system of a reading file. */
187 Lisp_Object Vset_auto_coding_function
;
189 /* Functions to be called to process text properties in inserted file. */
190 Lisp_Object Vafter_insert_file_functions
;
192 /* Lisp function for setting buffer-file-coding-system and the
193 multibyteness of the current buffer after inserting a file. */
194 Lisp_Object Qafter_insert_file_set_coding
;
196 /* Functions to be called to create text property annotations for file. */
197 Lisp_Object Vwrite_region_annotate_functions
;
198 Lisp_Object Qwrite_region_annotate_functions
;
200 /* During build_annotations, each time an annotation function is called,
201 this holds the annotations made by the previous functions. */
202 Lisp_Object Vwrite_region_annotations_so_far
;
204 /* File name in which we write a list of all our auto save files. */
205 Lisp_Object Vauto_save_list_file_name
;
207 /* Function to call to read a file name. */
208 Lisp_Object Vread_file_name_function
;
210 /* Current predicate used by read_file_name_internal. */
211 Lisp_Object Vread_file_name_predicate
;
213 /* Nonzero means completion ignores case when reading file name. */
214 int read_file_name_completion_ignore_case
;
216 /* Nonzero means, when reading a filename in the minibuffer,
217 start out by inserting the default directory into the minibuffer. */
218 int insert_default_directory
;
220 /* On VMS, nonzero means write new files with record format stmlf.
221 Zero means use var format. */
224 /* On NT, specifies the directory separator character, used (eg.) when
225 expanding file names. This can be bound to / or \. */
226 Lisp_Object Vdirectory_sep_char
;
228 extern Lisp_Object Vuser_login_name
;
231 extern Lisp_Object Vw32_get_true_file_attributes
;
234 extern int minibuf_level
;
236 extern int minibuffer_auto_raise
;
238 extern int history_delete_duplicates
;
240 /* These variables describe handlers that have "already" had a chance
241 to handle the current operation.
243 Vinhibit_file_name_handlers is a list of file name handlers.
244 Vinhibit_file_name_operation is the operation being handled.
245 If we try to handle that operation, we ignore those handlers. */
247 static Lisp_Object Vinhibit_file_name_handlers
;
248 static Lisp_Object Vinhibit_file_name_operation
;
250 Lisp_Object Qfile_error
, Qfile_already_exists
, Qfile_date_error
;
252 Lisp_Object Qfile_name_history
;
254 Lisp_Object Qcar_less_than_car
;
256 static int a_write
P_ ((int, Lisp_Object
, int, int,
257 Lisp_Object
*, struct coding_system
*));
258 static int e_write
P_ ((int, Lisp_Object
, int, int, struct coding_system
*));
262 report_file_error (string
, data
)
266 Lisp_Object errstring
;
269 synchronize_system_messages_locale ();
270 errstring
= code_convert_string_norecord (build_string (strerror (errorno
)),
271 Vlocale_coding_system
, 0);
277 Fsignal (Qfile_already_exists
, Fcons (errstring
, data
));
280 /* System error messages are capitalized. Downcase the initial
281 unless it is followed by a slash. */
282 if (SREF (errstring
, 1) != '/')
283 SSET (errstring
, 0, DOWNCASE (SREF (errstring
, 0)));
285 Fsignal (Qfile_error
,
286 Fcons (build_string (string
), Fcons (errstring
, data
)));
291 close_file_unwind (fd
)
294 emacs_close (XFASTINT (fd
));
298 /* Restore point, having saved it as a marker. */
301 restore_point_unwind (location
)
302 Lisp_Object location
;
304 Fgoto_char (location
);
305 Fset_marker (location
, Qnil
, Qnil
);
309 Lisp_Object Qexpand_file_name
;
310 Lisp_Object Qsubstitute_in_file_name
;
311 Lisp_Object Qdirectory_file_name
;
312 Lisp_Object Qfile_name_directory
;
313 Lisp_Object Qfile_name_nondirectory
;
314 Lisp_Object Qunhandled_file_name_directory
;
315 Lisp_Object Qfile_name_as_directory
;
316 Lisp_Object Qcopy_file
;
317 Lisp_Object Qmake_directory_internal
;
318 Lisp_Object Qmake_directory
;
319 Lisp_Object Qdelete_directory
;
320 Lisp_Object Qdelete_file
;
321 Lisp_Object Qrename_file
;
322 Lisp_Object Qadd_name_to_file
;
323 Lisp_Object Qmake_symbolic_link
;
324 Lisp_Object Qfile_exists_p
;
325 Lisp_Object Qfile_executable_p
;
326 Lisp_Object Qfile_readable_p
;
327 Lisp_Object Qfile_writable_p
;
328 Lisp_Object Qfile_symlink_p
;
329 Lisp_Object Qaccess_file
;
330 Lisp_Object Qfile_directory_p
;
331 Lisp_Object Qfile_regular_p
;
332 Lisp_Object Qfile_accessible_directory_p
;
333 Lisp_Object Qfile_modes
;
334 Lisp_Object Qset_file_modes
;
335 Lisp_Object Qset_file_times
;
336 Lisp_Object Qfile_newer_than_file_p
;
337 Lisp_Object Qinsert_file_contents
;
338 Lisp_Object Qwrite_region
;
339 Lisp_Object Qverify_visited_file_modtime
;
340 Lisp_Object Qset_visited_file_modtime
;
342 DEFUN ("find-file-name-handler", Ffind_file_name_handler
, Sfind_file_name_handler
, 2, 2, 0,
343 doc
: /* Return FILENAME's handler function for OPERATION, if it has one.
344 Otherwise, return nil.
345 A file name is handled if one of the regular expressions in
346 `file-name-handler-alist' matches it.
348 If OPERATION equals `inhibit-file-name-operation', then we ignore
349 any handlers that are members of `inhibit-file-name-handlers',
350 but we still do run any other handlers. This lets handlers
351 use the standard functions without calling themselves recursively. */)
352 (filename
, operation
)
353 Lisp_Object filename
, operation
;
355 /* This function must not munge the match data. */
356 Lisp_Object chain
, inhibited_handlers
, result
;
360 CHECK_STRING (filename
);
362 if (EQ (operation
, Vinhibit_file_name_operation
))
363 inhibited_handlers
= Vinhibit_file_name_handlers
;
365 inhibited_handlers
= Qnil
;
367 for (chain
= Vfile_name_handler_alist
; CONSP (chain
);
368 chain
= XCDR (chain
))
374 Lisp_Object string
= XCAR (elt
);
376 Lisp_Object handler
= XCDR (elt
);
377 Lisp_Object operations
= Qnil
;
379 if (SYMBOLP (handler
))
380 operations
= Fget (handler
, Qoperations
);
383 && (match_pos
= fast_string_match (string
, filename
)) > pos
384 && (NILP (operations
) || ! NILP (Fmemq (operation
, operations
))))
388 handler
= XCDR (elt
);
389 tem
= Fmemq (handler
, inhibited_handlers
);
403 DEFUN ("file-name-directory", Ffile_name_directory
, Sfile_name_directory
,
405 doc
: /* Return the directory component in file name FILENAME.
406 Return nil if FILENAME does not include a directory.
407 Otherwise return a directory spec.
408 Given a Unix syntax file name, returns a string ending in slash;
409 on VMS, perhaps instead a string ending in `:', `]' or `>'. */)
411 Lisp_Object filename
;
414 register const unsigned char *beg
;
416 register unsigned char *beg
;
418 register const unsigned char *p
;
421 CHECK_STRING (filename
);
423 /* If the file name has special constructs in it,
424 call the corresponding file handler. */
425 handler
= Ffind_file_name_handler (filename
, Qfile_name_directory
);
427 return call2 (handler
, Qfile_name_directory
, filename
);
429 filename
= FILE_SYSTEM_CASE (filename
);
430 beg
= SDATA (filename
);
432 beg
= strcpy (alloca (strlen (beg
) + 1), beg
);
434 p
= beg
+ SBYTES (filename
);
436 while (p
!= beg
&& !IS_DIRECTORY_SEP (p
[-1])
438 && p
[-1] != ':' && p
[-1] != ']' && p
[-1] != '>'
441 /* only recognise drive specifier at the beginning */
443 /* handle the "/:d:foo" and "/:foo" cases correctly */
444 && ((p
== beg
+ 2 && !IS_DIRECTORY_SEP (*beg
))
445 || (p
== beg
+ 4 && IS_DIRECTORY_SEP (*beg
))))
452 /* Expansion of "c:" to drive and default directory. */
455 /* MAXPATHLEN+1 is guaranteed to be enough space for getdefdir. */
456 unsigned char *res
= alloca (MAXPATHLEN
+ 1);
457 unsigned char *r
= res
;
459 if (p
== beg
+ 4 && IS_DIRECTORY_SEP (*beg
) && beg
[1] == ':')
461 strncpy (res
, beg
, 2);
466 if (getdefdir (toupper (*beg
) - 'A' + 1, r
))
468 if (!IS_DIRECTORY_SEP (res
[strlen (res
) - 1]))
471 p
= beg
+ strlen (beg
);
474 CORRECT_DIR_SEPS (beg
);
477 return make_specified_string (beg
, -1, p
- beg
, STRING_MULTIBYTE (filename
));
480 DEFUN ("file-name-nondirectory", Ffile_name_nondirectory
,
481 Sfile_name_nondirectory
, 1, 1, 0,
482 doc
: /* Return file name FILENAME sans its directory.
483 For example, in a Unix-syntax file name,
484 this is everything after the last slash,
485 or the entire name if it contains no slash. */)
487 Lisp_Object filename
;
489 register const unsigned char *beg
, *p
, *end
;
492 CHECK_STRING (filename
);
494 /* If the file name has special constructs in it,
495 call the corresponding file handler. */
496 handler
= Ffind_file_name_handler (filename
, Qfile_name_nondirectory
);
498 return call2 (handler
, Qfile_name_nondirectory
, filename
);
500 beg
= SDATA (filename
);
501 end
= p
= beg
+ SBYTES (filename
);
503 while (p
!= beg
&& !IS_DIRECTORY_SEP (p
[-1])
505 && p
[-1] != ':' && p
[-1] != ']' && p
[-1] != '>'
508 /* only recognise drive specifier at beginning */
510 /* handle the "/:d:foo" case correctly */
511 && (p
== beg
+ 2 || (p
== beg
+ 4 && IS_DIRECTORY_SEP (*beg
))))
516 return make_specified_string (p
, -1, end
- p
, STRING_MULTIBYTE (filename
));
519 DEFUN ("unhandled-file-name-directory", Funhandled_file_name_directory
,
520 Sunhandled_file_name_directory
, 1, 1, 0,
521 doc
: /* Return a directly usable directory name somehow associated with FILENAME.
522 A `directly usable' directory name is one that may be used without the
523 intervention of any file handler.
524 If FILENAME is a directly usable file itself, return
525 \(file-name-directory FILENAME).
526 The `call-process' and `start-process' functions use this function to
527 get a current directory to run processes in. */)
529 Lisp_Object filename
;
533 /* If the file name has special constructs in it,
534 call the corresponding file handler. */
535 handler
= Ffind_file_name_handler (filename
, Qunhandled_file_name_directory
);
537 return call2 (handler
, Qunhandled_file_name_directory
, filename
);
539 return Ffile_name_directory (filename
);
544 file_name_as_directory (out
, in
)
547 int size
= strlen (in
) - 1;
560 /* Is it already a directory string? */
561 if (in
[size
] == ':' || in
[size
] == ']' || in
[size
] == '>')
563 /* Is it a VMS directory file name? If so, hack VMS syntax. */
564 else if (! index (in
, '/')
565 && ((size
> 3 && ! strcmp (&in
[size
- 3], ".DIR"))
566 || (size
> 3 && ! strcmp (&in
[size
- 3], ".dir"))
567 || (size
> 5 && (! strncmp (&in
[size
- 5], ".DIR", 4)
568 || ! strncmp (&in
[size
- 5], ".dir", 4))
569 && (in
[size
- 1] == '.' || in
[size
- 1] == ';')
570 && in
[size
] == '1')))
572 register char *p
, *dot
;
576 dir:x.dir --> dir:[x]
577 dir:[x]y.dir --> dir:[x.y] */
579 while (p
!= in
&& *p
!= ':' && *p
!= '>' && *p
!= ']') p
--;
582 strncpy (out
, in
, p
- in
);
601 dot
= index (p
, '.');
604 /* blindly remove any extension */
605 size
= strlen (out
) + (dot
- p
);
606 strncat (out
, p
, dot
- p
);
617 /* For Unix syntax, Append a slash if necessary */
618 if (!IS_DIRECTORY_SEP (out
[size
]))
620 /* Cannot use DIRECTORY_SEP, which could have any value */
622 out
[size
+ 2] = '\0';
625 CORRECT_DIR_SEPS (out
);
631 DEFUN ("file-name-as-directory", Ffile_name_as_directory
,
632 Sfile_name_as_directory
, 1, 1, 0,
633 doc
: /* Return a string representing the file name FILE interpreted as a directory.
634 This operation exists because a directory is also a file, but its name as
635 a directory is different from its name as a file.
636 The result can be used as the value of `default-directory'
637 or passed as second argument to `expand-file-name'.
638 For a Unix-syntax file name, just appends a slash.
639 On VMS, converts \"[X]FOO.DIR\" to \"[X.FOO]\", etc. */)
650 /* If the file name has special constructs in it,
651 call the corresponding file handler. */
652 handler
= Ffind_file_name_handler (file
, Qfile_name_as_directory
);
654 return call2 (handler
, Qfile_name_as_directory
, file
);
656 buf
= (char *) alloca (SBYTES (file
) + 10);
657 file_name_as_directory (buf
, SDATA (file
));
658 return make_specified_string (buf
, -1, strlen (buf
),
659 STRING_MULTIBYTE (file
));
663 * Convert from directory name to filename.
665 * xyzzy:[mukesh.emacs] => xyzzy:[mukesh]emacs.dir.1
666 * xyzzy:[mukesh] => xyzzy:[000000]mukesh.dir.1
667 * On UNIX, it's simple: just make sure there isn't a terminating /
669 * Value is nonzero if the string output is different from the input.
673 directory_file_name (src
, dst
)
681 struct FAB fab
= cc$rms_fab
;
682 struct NAM nam
= cc$rms_nam
;
683 char esa
[NAM$C_MAXRSS
];
688 if (! index (src
, '/')
689 && (src
[slen
- 1] == ']'
690 || src
[slen
- 1] == ':'
691 || src
[slen
- 1] == '>'))
693 /* VMS style - convert [x.y.z] to [x.y]z, [x] to [000000]x */
695 fab
.fab$b_fns
= slen
;
696 fab
.fab$l_nam
= &nam
;
697 fab
.fab$l_fop
= FAB$M_NAM
;
700 nam
.nam$b_ess
= sizeof esa
;
701 nam
.nam$b_nop
|= NAM$M_SYNCHK
;
703 /* We call SYS$PARSE to handle such things as [--] for us. */
704 if (SYS$
PARSE (&fab
, 0, 0) == RMS$_NORMAL
)
706 slen
= nam
.nam$b_esl
;
707 if (esa
[slen
- 1] == ';' && esa
[slen
- 2] == '.')
712 if (src
[slen
- 1] != ']' && src
[slen
- 1] != '>')
714 /* what about when we have logical_name:???? */
715 if (src
[slen
- 1] == ':')
716 { /* Xlate logical name and see what we get */
717 ptr
= strcpy (dst
, src
); /* upper case for getenv */
720 if ('a' <= *ptr
&& *ptr
<= 'z')
724 dst
[slen
- 1] = 0; /* remove colon */
725 if (!(src
= egetenv (dst
)))
727 /* should we jump to the beginning of this procedure?
728 Good points: allows us to use logical names that xlate
730 Bad points: can be a problem if we just translated to a device
732 For now, I'll punt and always expect VMS names, and hope for
735 if (src
[slen
- 1] != ']' && src
[slen
- 1] != '>')
736 { /* no recursion here! */
742 { /* not a directory spec */
747 bracket
= src
[slen
- 1];
749 /* If bracket is ']' or '>', bracket - 2 is the corresponding
751 ptr
= index (src
, bracket
- 2);
753 { /* no opening bracket */
757 if (!(rptr
= rindex (src
, '.')))
760 strncpy (dst
, src
, slen
);
764 dst
[slen
++] = bracket
;
769 /* If we have the top-level of a rooted directory (i.e. xx:[000000]),
770 then translate the device and recurse. */
771 if (dst
[slen
- 1] == ':'
772 && dst
[slen
- 2] != ':' /* skip decnet nodes */
773 && strcmp (src
+ slen
, "[000000]") == 0)
775 dst
[slen
- 1] = '\0';
776 if ((ptr
= egetenv (dst
))
777 && (rlen
= strlen (ptr
) - 1) > 0
778 && (ptr
[rlen
] == ']' || ptr
[rlen
] == '>')
779 && ptr
[rlen
- 1] == '.')
781 char * buf
= (char *) alloca (strlen (ptr
) + 1);
785 return directory_file_name (buf
, dst
);
790 strcat (dst
, "[000000]");
794 rlen
= strlen (rptr
) - 1;
795 strncat (dst
, rptr
, rlen
);
796 dst
[slen
+ rlen
] = '\0';
797 strcat (dst
, ".DIR.1");
801 /* Process as Unix format: just remove any final slash.
802 But leave "/" unchanged; do not change it to "". */
805 /* Handle // as root for apollo's. */
806 if ((slen
> 2 && dst
[slen
- 1] == '/')
807 || (slen
> 1 && dst
[0] != '/' && dst
[slen
- 1] == '/'))
811 && IS_DIRECTORY_SEP (dst
[slen
- 1])
813 && !IS_ANY_SEP (dst
[slen
- 2])
819 CORRECT_DIR_SEPS (dst
);
824 DEFUN ("directory-file-name", Fdirectory_file_name
, Sdirectory_file_name
,
826 doc
: /* Returns the file name of the directory named DIRECTORY.
827 This is the name of the file that holds the data for the directory DIRECTORY.
828 This operation exists because a directory is also a file, but its name as
829 a directory is different from its name as a file.
830 In Unix-syntax, this function just removes the final slash.
831 On VMS, given a VMS-syntax directory name such as \"[X.Y]\",
832 it returns a file name such as \"[X]Y.DIR.1\". */)
834 Lisp_Object directory
;
839 CHECK_STRING (directory
);
841 if (NILP (directory
))
844 /* If the file name has special constructs in it,
845 call the corresponding file handler. */
846 handler
= Ffind_file_name_handler (directory
, Qdirectory_file_name
);
848 return call2 (handler
, Qdirectory_file_name
, directory
);
851 /* 20 extra chars is insufficient for VMS, since we might perform a
852 logical name translation. an equivalence string can be up to 255
853 chars long, so grab that much extra space... - sss */
854 buf
= (char *) alloca (SBYTES (directory
) + 20 + 255);
856 buf
= (char *) alloca (SBYTES (directory
) + 20);
858 directory_file_name (SDATA (directory
), buf
);
859 return make_specified_string (buf
, -1, strlen (buf
),
860 STRING_MULTIBYTE (directory
));
863 static char make_temp_name_tbl
[64] =
865 'A','B','C','D','E','F','G','H',
866 'I','J','K','L','M','N','O','P',
867 'Q','R','S','T','U','V','W','X',
868 'Y','Z','a','b','c','d','e','f',
869 'g','h','i','j','k','l','m','n',
870 'o','p','q','r','s','t','u','v',
871 'w','x','y','z','0','1','2','3',
872 '4','5','6','7','8','9','-','_'
875 static unsigned make_temp_name_count
, make_temp_name_count_initialized_p
;
877 /* Value is a temporary file name starting with PREFIX, a string.
879 The Emacs process number forms part of the result, so there is
880 no danger of generating a name being used by another process.
881 In addition, this function makes an attempt to choose a name
882 which has no existing file. To make this work, PREFIX should be
883 an absolute file name.
885 BASE64_P non-zero means add the pid as 3 characters in base64
886 encoding. In this case, 6 characters will be added to PREFIX to
887 form the file name. Otherwise, if Emacs is running on a system
888 with long file names, add the pid as a decimal number.
890 This function signals an error if no unique file name could be
894 make_temp_name (prefix
, base64_p
)
901 unsigned char *p
, *data
;
905 CHECK_STRING (prefix
);
907 /* VAL is created by adding 6 characters to PREFIX. The first
908 three are the PID of this process, in base 64, and the second
909 three are incremented if the file already exists. This ensures
910 262144 unique file names per PID per PREFIX. */
912 pid
= (int) getpid ();
916 pidbuf
[0] = make_temp_name_tbl
[pid
& 63], pid
>>= 6;
917 pidbuf
[1] = make_temp_name_tbl
[pid
& 63], pid
>>= 6;
918 pidbuf
[2] = make_temp_name_tbl
[pid
& 63], pid
>>= 6;
923 #ifdef HAVE_LONG_FILE_NAMES
924 sprintf (pidbuf
, "%d", pid
);
925 pidlen
= strlen (pidbuf
);
927 pidbuf
[0] = make_temp_name_tbl
[pid
& 63], pid
>>= 6;
928 pidbuf
[1] = make_temp_name_tbl
[pid
& 63], pid
>>= 6;
929 pidbuf
[2] = make_temp_name_tbl
[pid
& 63], pid
>>= 6;
934 len
= SBYTES (prefix
); clen
= SCHARS (prefix
);
935 val
= make_uninit_multibyte_string (clen
+ 3 + pidlen
, len
+ 3 + pidlen
);
936 if (!STRING_MULTIBYTE (prefix
))
937 STRING_SET_UNIBYTE (val
);
939 bcopy(SDATA (prefix
), data
, len
);
942 bcopy (pidbuf
, p
, pidlen
);
945 /* Here we try to minimize useless stat'ing when this function is
946 invoked many times successively with the same PREFIX. We achieve
947 this by initializing count to a random value, and incrementing it
950 We don't want make-temp-name to be called while dumping,
951 because then make_temp_name_count_initialized_p would get set
952 and then make_temp_name_count would not be set when Emacs starts. */
954 if (!make_temp_name_count_initialized_p
)
956 make_temp_name_count
= (unsigned) time (NULL
);
957 make_temp_name_count_initialized_p
= 1;
963 unsigned num
= make_temp_name_count
;
965 p
[0] = make_temp_name_tbl
[num
& 63], num
>>= 6;
966 p
[1] = make_temp_name_tbl
[num
& 63], num
>>= 6;
967 p
[2] = make_temp_name_tbl
[num
& 63], num
>>= 6;
969 /* Poor man's congruential RN generator. Replace with
970 ++make_temp_name_count for debugging. */
971 make_temp_name_count
+= 25229;
972 make_temp_name_count
%= 225307;
974 if (stat (data
, &ignored
) < 0)
976 /* We want to return only if errno is ENOENT. */
980 /* The error here is dubious, but there is little else we
981 can do. The alternatives are to return nil, which is
982 as bad as (and in many cases worse than) throwing the
983 error, or to ignore the error, which will likely result
984 in looping through 225307 stat's, which is not only
985 dog-slow, but also useless since it will fallback to
986 the errow below, anyway. */
987 report_file_error ("Cannot create temporary name for prefix",
988 Fcons (prefix
, Qnil
));
993 error ("Cannot create temporary name for prefix `%s'",
999 DEFUN ("make-temp-name", Fmake_temp_name
, Smake_temp_name
, 1, 1, 0,
1000 doc
: /* Generate temporary file name (string) starting with PREFIX (a string).
1001 The Emacs process number forms part of the result,
1002 so there is no danger of generating a name being used by another process.
1004 In addition, this function makes an attempt to choose a name
1005 which has no existing file. To make this work,
1006 PREFIX should be an absolute file name.
1008 There is a race condition between calling `make-temp-name' and creating the
1009 file which opens all kinds of security holes. For that reason, you should
1010 probably use `make-temp-file' instead, except in three circumstances:
1012 * If you are creating the file in the user's home directory.
1013 * If you are creating a directory rather than an ordinary file.
1014 * If you are taking special precautions as `make-temp-file' does. */)
1018 return make_temp_name (prefix
, 0);
1023 DEFUN ("expand-file-name", Fexpand_file_name
, Sexpand_file_name
, 1, 2, 0,
1024 doc
: /* Convert filename NAME to absolute, and canonicalize it.
1025 Second arg DEFAULT-DIRECTORY is directory to start with if NAME is relative
1026 \(does not start with slash); if DEFAULT-DIRECTORY is nil or missing,
1027 the current buffer's value of `default-directory' is used.
1028 File name components that are `.' are removed, and
1029 so are file name components followed by `..', along with the `..' itself;
1030 note that these simplifications are done without checking the resulting
1031 file names in the file system.
1032 An initial `~/' expands to your home directory.
1033 An initial `~USER/' expands to USER's home directory.
1034 See also the function `substitute-in-file-name'. */)
1035 (name
, default_directory
)
1036 Lisp_Object name
, default_directory
;
1040 register unsigned char *newdir
, *p
, *o
;
1042 unsigned char *target
;
1045 unsigned char * colon
= 0;
1046 unsigned char * close
= 0;
1047 unsigned char * slash
= 0;
1048 unsigned char * brack
= 0;
1049 int lbrack
= 0, rbrack
= 0;
1054 int collapse_newdir
= 1;
1058 Lisp_Object handler
, result
;
1061 CHECK_STRING (name
);
1063 /* If the file name has special constructs in it,
1064 call the corresponding file handler. */
1065 handler
= Ffind_file_name_handler (name
, Qexpand_file_name
);
1066 if (!NILP (handler
))
1067 return call3 (handler
, Qexpand_file_name
, name
, default_directory
);
1069 /* Use the buffer's default-directory if DEFAULT_DIRECTORY is omitted. */
1070 if (NILP (default_directory
))
1071 default_directory
= current_buffer
->directory
;
1072 if (! STRINGP (default_directory
))
1075 /* "/" is not considered a root directory on DOS_NT, so using "/"
1076 here causes an infinite recursion in, e.g., the following:
1078 (let (default-directory)
1079 (expand-file-name "a"))
1081 To avoid this, we set default_directory to the root of the
1083 extern char *emacs_root_dir (void);
1085 default_directory
= build_string (emacs_root_dir ());
1087 default_directory
= build_string ("/");
1091 if (!NILP (default_directory
))
1093 handler
= Ffind_file_name_handler (default_directory
, Qexpand_file_name
);
1094 if (!NILP (handler
))
1095 return call3 (handler
, Qexpand_file_name
, name
, default_directory
);
1098 o
= SDATA (default_directory
);
1100 /* Make sure DEFAULT_DIRECTORY is properly expanded.
1101 It would be better to do this down below where we actually use
1102 default_directory. Unfortunately, calling Fexpand_file_name recursively
1103 could invoke GC, and the strings might be relocated. This would
1104 be annoying because we have pointers into strings lying around
1105 that would need adjusting, and people would add new pointers to
1106 the code and forget to adjust them, resulting in intermittent bugs.
1107 Putting this call here avoids all that crud.
1109 The EQ test avoids infinite recursion. */
1110 if (! NILP (default_directory
) && !EQ (default_directory
, name
)
1111 /* Save time in some common cases - as long as default_directory
1112 is not relative, it can be canonicalized with name below (if it
1113 is needed at all) without requiring it to be expanded now. */
1115 /* Detect MSDOS file names with drive specifiers. */
1116 && ! (IS_DRIVE (o
[0]) && IS_DEVICE_SEP (o
[1]) && IS_DIRECTORY_SEP (o
[2]))
1118 /* Detect Windows file names in UNC format. */
1119 && ! (IS_DIRECTORY_SEP (o
[0]) && IS_DIRECTORY_SEP (o
[1]))
1121 #else /* not DOS_NT */
1122 /* Detect Unix absolute file names (/... alone is not absolute on
1124 && ! (IS_DIRECTORY_SEP (o
[0]))
1125 #endif /* not DOS_NT */
1128 struct gcpro gcpro1
;
1131 default_directory
= Fexpand_file_name (default_directory
, Qnil
);
1135 name
= FILE_SYSTEM_CASE (name
);
1137 multibyte
= STRING_MULTIBYTE (name
);
1140 /* We will force directory separators to be either all \ or /, so make
1141 a local copy to modify, even if there ends up being no change. */
1142 nm
= strcpy (alloca (strlen (nm
) + 1), nm
);
1144 /* Note if special escape prefix is present, but remove for now. */
1145 if (nm
[0] == '/' && nm
[1] == ':')
1151 /* Find and remove drive specifier if present; this makes nm absolute
1152 even if the rest of the name appears to be relative. Only look for
1153 drive specifier at the beginning. */
1154 if (IS_DRIVE (nm
[0]) && IS_DEVICE_SEP (nm
[1]))
1161 /* If we see "c://somedir", we want to strip the first slash after the
1162 colon when stripping the drive letter. Otherwise, this expands to
1164 if (drive
&& IS_DIRECTORY_SEP (nm
[0]) && IS_DIRECTORY_SEP (nm
[1]))
1166 #endif /* WINDOWSNT */
1170 /* Discard any previous drive specifier if nm is now in UNC format. */
1171 if (IS_DIRECTORY_SEP (nm
[0]) && IS_DIRECTORY_SEP (nm
[1]))
1177 /* If nm is absolute, look for `/./' or `/../' or `//''sequences; if
1178 none are found, we can probably return right away. We will avoid
1179 allocating a new string if name is already fully expanded. */
1181 IS_DIRECTORY_SEP (nm
[0])
1183 && drive
&& !is_escaped
1186 && (drive
|| IS_DIRECTORY_SEP (nm
[1])) && !is_escaped
1193 /* If it turns out that the filename we want to return is just a
1194 suffix of FILENAME, we don't need to go through and edit
1195 things; we just need to construct a new string using data
1196 starting at the middle of FILENAME. If we set lose to a
1197 non-zero value, that means we've discovered that we can't do
1204 /* Since we know the name is absolute, we can assume that each
1205 element starts with a "/". */
1207 /* "." and ".." are hairy. */
1208 if (IS_DIRECTORY_SEP (p
[0])
1210 && (IS_DIRECTORY_SEP (p
[2])
1212 || (p
[2] == '.' && (IS_DIRECTORY_SEP (p
[3])
1215 /* We want to replace multiple `/' in a row with a single
1218 && IS_DIRECTORY_SEP (p
[0])
1219 && IS_DIRECTORY_SEP (p
[1]))
1226 /* if dev:[dir]/, move nm to / */
1227 if (!slash
&& p
> nm
&& (brack
|| colon
)) {
1228 nm
= (brack
? brack
+ 1 : colon
+ 1);
1229 lbrack
= rbrack
= 0;
1236 #ifdef NO_HYPHENS_IN_FILENAMES
1237 if (lbrack
== rbrack
)
1239 /* Avoid clobbering negative version numbers. */
1244 #endif /* NO_HYPHENS_IN_FILENAMES */
1245 if (lbrack
> rbrack
&&
1246 ((p
[-1] == '.' || p
[-1] == '[' || p
[-1] == '<') &&
1247 (p
[1] == '.' || p
[1] == ']' || p
[1] == '>')))
1249 #ifdef NO_HYPHENS_IN_FILENAMES
1252 #endif /* NO_HYPHENS_IN_FILENAMES */
1253 /* count open brackets, reset close bracket pointer */
1254 if (p
[0] == '[' || p
[0] == '<')
1255 lbrack
++, brack
= 0;
1256 /* count close brackets, set close bracket pointer */
1257 if (p
[0] == ']' || p
[0] == '>')
1258 rbrack
++, brack
= p
;
1259 /* detect ][ or >< */
1260 if ((p
[0] == ']' || p
[0] == '>') && (p
[1] == '[' || p
[1] == '<'))
1262 if ((p
[0] == ':' || p
[0] == ']' || p
[0] == '>') && p
[1] == '~')
1263 nm
= p
+ 1, lose
= 1;
1264 if (p
[0] == ':' && (colon
|| slash
))
1265 /* if dev1:[dir]dev2:, move nm to dev2: */
1271 /* if /name/dev:, move nm to dev: */
1274 /* if node::dev:, move colon following dev */
1275 else if (colon
&& colon
[-1] == ':')
1277 /* if dev1:dev2:, move nm to dev2: */
1278 else if (colon
&& colon
[-1] != ':')
1283 if (p
[0] == ':' && !colon
)
1289 if (lbrack
== rbrack
)
1292 else if (p
[0] == '.')
1300 if (index (nm
, '/'))
1302 nm
= sys_translate_unix (nm
);
1303 return make_specified_string (nm
, -1, strlen (nm
), multibyte
);
1307 /* Make sure directories are all separated with / or \ as
1308 desired, but avoid allocation of a new string when not
1310 CORRECT_DIR_SEPS (nm
);
1312 if (IS_DIRECTORY_SEP (nm
[1]))
1314 if (strcmp (nm
, SDATA (name
)) != 0)
1315 name
= make_specified_string (nm
, -1, strlen (nm
), multibyte
);
1319 /* drive must be set, so this is okay */
1320 if (strcmp (nm
- 2, SDATA (name
)) != 0)
1324 name
= make_specified_string (nm
, -1, p
- nm
, multibyte
);
1325 temp
[0] = DRIVE_LETTER (drive
);
1326 name
= concat2 (build_string (temp
), name
);
1329 #else /* not DOS_NT */
1330 if (nm
== SDATA (name
))
1332 return make_specified_string (nm
, -1, strlen (nm
), multibyte
);
1333 #endif /* not DOS_NT */
1337 /* At this point, nm might or might not be an absolute file name. We
1338 need to expand ~ or ~user if present, otherwise prefix nm with
1339 default_directory if nm is not absolute, and finally collapse /./
1340 and /foo/../ sequences.
1342 We set newdir to be the appropriate prefix if one is needed:
1343 - the relevant user directory if nm starts with ~ or ~user
1344 - the specified drive's working dir (DOS/NT only) if nm does not
1346 - the value of default_directory.
1348 Note that these prefixes are not guaranteed to be absolute (except
1349 for the working dir of a drive). Therefore, to ensure we always
1350 return an absolute name, if the final prefix is not absolute we
1351 append it to the current working directory. */
1355 if (nm
[0] == '~') /* prefix ~ */
1357 if (IS_DIRECTORY_SEP (nm
[1])
1361 || nm
[1] == 0) /* ~ by itself */
1363 if (!(newdir
= (unsigned char *) egetenv ("HOME")))
1364 newdir
= (unsigned char *) "";
1367 collapse_newdir
= 0;
1370 nm
++; /* Don't leave the slash in nm. */
1373 else /* ~user/filename */
1375 for (p
= nm
; *p
&& (!IS_DIRECTORY_SEP (*p
)
1380 o
= (unsigned char *) alloca (p
- nm
+ 1);
1381 bcopy ((char *) nm
, o
, p
- nm
);
1384 pw
= (struct passwd
*) getpwnam (o
+ 1);
1387 newdir
= (unsigned char *) pw
-> pw_dir
;
1389 nm
= p
+ 1; /* skip the terminator */
1393 collapse_newdir
= 0;
1398 /* If we don't find a user of that name, leave the name
1399 unchanged; don't move nm forward to p. */
1404 /* On DOS and Windows, nm is absolute if a drive name was specified;
1405 use the drive's current directory as the prefix if needed. */
1406 if (!newdir
&& drive
)
1408 /* Get default directory if needed to make nm absolute. */
1409 if (!IS_DIRECTORY_SEP (nm
[0]))
1411 newdir
= alloca (MAXPATHLEN
+ 1);
1412 if (!getdefdir (toupper (drive
) - 'A' + 1, newdir
))
1417 /* Either nm starts with /, or drive isn't mounted. */
1418 newdir
= alloca (4);
1419 newdir
[0] = DRIVE_LETTER (drive
);
1427 /* Finally, if no prefix has been specified and nm is not absolute,
1428 then it must be expanded relative to default_directory. */
1432 /* /... alone is not absolute on DOS and Windows. */
1433 && !IS_DIRECTORY_SEP (nm
[0])
1436 && !(IS_DIRECTORY_SEP (nm
[0]) && IS_DIRECTORY_SEP (nm
[1]))
1443 newdir
= SDATA (default_directory
);
1444 multibyte
|= STRING_MULTIBYTE (default_directory
);
1446 /* Note if special escape prefix is present, but remove for now. */
1447 if (newdir
[0] == '/' && newdir
[1] == ':')
1458 /* First ensure newdir is an absolute name. */
1460 /* Detect MSDOS file names with drive specifiers. */
1461 ! (IS_DRIVE (newdir
[0])
1462 && IS_DEVICE_SEP (newdir
[1]) && IS_DIRECTORY_SEP (newdir
[2]))
1464 /* Detect Windows file names in UNC format. */
1465 && ! (IS_DIRECTORY_SEP (newdir
[0]) && IS_DIRECTORY_SEP (newdir
[1]))
1469 /* Effectively, let newdir be (expand-file-name newdir cwd).
1470 Because of the admonition against calling expand-file-name
1471 when we have pointers into lisp strings, we accomplish this
1472 indirectly by prepending newdir to nm if necessary, and using
1473 cwd (or the wd of newdir's drive) as the new newdir. */
1475 if (IS_DRIVE (newdir
[0]) && IS_DEVICE_SEP (newdir
[1]))
1480 if (!IS_DIRECTORY_SEP (nm
[0]))
1482 char * tmp
= alloca (strlen (newdir
) + strlen (nm
) + 2);
1483 file_name_as_directory (tmp
, newdir
);
1487 newdir
= alloca (MAXPATHLEN
+ 1);
1490 if (!getdefdir (toupper (drive
) - 'A' + 1, newdir
))
1497 /* Strip off drive name from prefix, if present. */
1498 if (IS_DRIVE (newdir
[0]) && IS_DEVICE_SEP (newdir
[1]))
1504 /* Keep only a prefix from newdir if nm starts with slash
1505 (//server/share for UNC, nothing otherwise). */
1506 if (IS_DIRECTORY_SEP (nm
[0]) && collapse_newdir
)
1509 if (IS_DIRECTORY_SEP (newdir
[0]) && IS_DIRECTORY_SEP (newdir
[1]))
1511 newdir
= strcpy (alloca (strlen (newdir
) + 1), newdir
);
1513 while (*p
&& !IS_DIRECTORY_SEP (*p
)) p
++;
1515 while (*p
&& !IS_DIRECTORY_SEP (*p
)) p
++;
1527 /* Get rid of any slash at the end of newdir, unless newdir is
1528 just / or // (an incomplete UNC name). */
1529 length
= strlen (newdir
);
1530 if (length
> 1 && IS_DIRECTORY_SEP (newdir
[length
- 1])
1532 && !(length
== 2 && IS_DIRECTORY_SEP (newdir
[0]))
1536 unsigned char *temp
= (unsigned char *) alloca (length
);
1537 bcopy (newdir
, temp
, length
- 1);
1538 temp
[length
- 1] = 0;
1546 /* Now concatenate the directory and name to new space in the stack frame */
1547 tlen
+= strlen (nm
) + 1;
1549 /* Reserve space for drive specifier and escape prefix, since either
1550 or both may need to be inserted. (The Microsoft x86 compiler
1551 produces incorrect code if the following two lines are combined.) */
1552 target
= (unsigned char *) alloca (tlen
+ 4);
1554 #else /* not DOS_NT */
1555 target
= (unsigned char *) alloca (tlen
);
1556 #endif /* not DOS_NT */
1562 if (nm
[0] == 0 || IS_DIRECTORY_SEP (nm
[0]))
1565 /* If newdir is effectively "C:/", then the drive letter will have
1566 been stripped and newdir will be "/". Concatenating with an
1567 absolute directory in nm produces "//", which will then be
1568 incorrectly treated as a network share. Ignore newdir in
1569 this case (keeping the drive letter). */
1570 if (!(drive
&& nm
[0] && IS_DIRECTORY_SEP (newdir
[0])
1571 && newdir
[1] == '\0'))
1573 strcpy (target
, newdir
);
1577 file_name_as_directory (target
, newdir
);
1580 strcat (target
, nm
);
1582 if (index (target
, '/'))
1583 strcpy (target
, sys_translate_unix (target
));
1586 /* ASSERT (IS_DIRECTORY_SEP (target[0])) if not VMS */
1588 /* Now canonicalize by removing `//', `/.' and `/foo/..' if they
1597 if (*p
!= ']' && *p
!= '>' && *p
!= '-')
1603 else if ((p
[0] == ']' || p
[0] == '>') && p
[0] == p
[1] + 2)
1604 /* brackets are offset from each other by 2 */
1607 if (*p
!= '.' && *p
!= '-' && o
[-1] != '.')
1608 /* convert [foo][bar] to [bar] */
1609 while (o
[-1] != '[' && o
[-1] != '<')
1611 else if (*p
== '-' && *o
!= '.')
1614 else if (p
[0] == '-' && o
[-1] == '.' &&
1615 (p
[1] == '.' || p
[1] == ']' || p
[1] == '>'))
1616 /* flush .foo.- ; leave - if stopped by '[' or '<' */
1620 while (o
[-1] != '.' && o
[-1] != '[' && o
[-1] != '<');
1621 if (p
[1] == '.') /* foo.-.bar ==> bar. */
1623 else if (o
[-1] == '.') /* '.foo.-]' ==> ']' */
1625 /* else [foo.-] ==> [-] */
1629 #ifdef NO_HYPHENS_IN_FILENAMES
1631 o
[-1] != '[' && o
[-1] != '<' && o
[-1] != '.' &&
1632 p
[1] != ']' && p
[1] != '>' && p
[1] != '.')
1634 #endif /* NO_HYPHENS_IN_FILENAMES */
1638 if (!IS_DIRECTORY_SEP (*p
))
1642 else if (IS_DIRECTORY_SEP (p
[0])
1644 && (IS_DIRECTORY_SEP (p
[2])
1647 /* If "/." is the entire filename, keep the "/". Otherwise,
1648 just delete the whole "/.". */
1649 if (o
== target
&& p
[2] == '\0')
1653 else if (IS_DIRECTORY_SEP (p
[0]) && p
[1] == '.' && p
[2] == '.'
1654 /* `/../' is the "superroot" on certain file systems.
1655 Turned off on DOS_NT systems because they have no
1656 "superroot" and because this causes us to produce
1657 file names like "d:/../foo" which fail file-related
1658 functions of the underlying OS. (To reproduce, try a
1659 long series of "../../" in default_directory, longer
1660 than the number of levels from the root.) */
1664 && (IS_DIRECTORY_SEP (p
[3]) || p
[3] == 0))
1666 while (o
!= target
&& (--o
) && !IS_DIRECTORY_SEP (*o
))
1668 /* Keep initial / only if this is the whole name. */
1669 if (o
== target
&& IS_ANY_SEP (*o
) && p
[3] == 0)
1674 && IS_DIRECTORY_SEP (p
[0]) && IS_DIRECTORY_SEP (p
[1]))
1676 /* Collapse multiple `/' in a row. */
1678 while (IS_DIRECTORY_SEP (*p
))
1685 #endif /* not VMS */
1689 /* At last, set drive name. */
1691 /* Except for network file name. */
1692 if (!(IS_DIRECTORY_SEP (target
[0]) && IS_DIRECTORY_SEP (target
[1])))
1693 #endif /* WINDOWSNT */
1695 if (!drive
) abort ();
1697 target
[0] = DRIVE_LETTER (drive
);
1700 /* Reinsert the escape prefix if required. */
1707 CORRECT_DIR_SEPS (target
);
1710 result
= make_specified_string (target
, -1, o
- target
, multibyte
);
1712 /* Again look to see if the file name has special constructs in it
1713 and perhaps call the corresponding file handler. This is needed
1714 for filenames such as "/foo/../user@host:/bar/../baz". Expanding
1715 the ".." component gives us "/user@host:/bar/../baz" which needs
1716 to be expanded again. */
1717 handler
= Ffind_file_name_handler (result
, Qexpand_file_name
);
1718 if (!NILP (handler
))
1719 return call3 (handler
, Qexpand_file_name
, result
, default_directory
);
1725 /* PLEASE DO NOT DELETE THIS COMMENTED-OUT VERSION!
1726 This is the old version of expand-file-name, before it was thoroughly
1727 rewritten for Emacs 10.31. We leave this version here commented-out,
1728 because the code is very complex and likely to have subtle bugs. If
1729 bugs _are_ found, it might be of interest to look at the old code and
1730 see what did it do in the relevant situation.
1732 Don't remove this code: it's true that it will be accessible via CVS,
1733 but a few years from deletion, people will forget it is there. */
1735 /* Changed this DEFUN to a DEAFUN, so as not to confuse `make-docfile'. */
1736 DEAFUN ("expand-file-name", Fexpand_file_name
, Sexpand_file_name
, 1, 2, 0,
1737 "Convert FILENAME to absolute, and canonicalize it.\n\
1738 Second arg DEFAULT is directory to start with if FILENAME is relative\n\
1739 \(does not start with slash); if DEFAULT is nil or missing,\n\
1740 the current buffer's value of default-directory is used.\n\
1741 Filenames containing `.' or `..' as components are simplified;\n\
1742 initial `~/' expands to your home directory.\n\
1743 See also the function `substitute-in-file-name'.")
1745 Lisp_Object name
, defalt
;
1749 register unsigned char *newdir
, *p
, *o
;
1751 unsigned char *target
;
1755 unsigned char * colon
= 0;
1756 unsigned char * close
= 0;
1757 unsigned char * slash
= 0;
1758 unsigned char * brack
= 0;
1759 int lbrack
= 0, rbrack
= 0;
1763 CHECK_STRING (name
);
1766 /* Filenames on VMS are always upper case. */
1767 name
= Fupcase (name
);
1772 /* If nm is absolute, flush ...// and detect /./ and /../.
1773 If no /./ or /../ we can return right away. */
1785 if (p
[0] == '/' && p
[1] == '/'
1787 /* // at start of filename is meaningful on Apollo system. */
1792 if (p
[0] == '/' && p
[1] == '~')
1793 nm
= p
+ 1, lose
= 1;
1794 if (p
[0] == '/' && p
[1] == '.'
1795 && (p
[2] == '/' || p
[2] == 0
1796 || (p
[2] == '.' && (p
[3] == '/' || p
[3] == 0))))
1802 /* if dev:[dir]/, move nm to / */
1803 if (!slash
&& p
> nm
&& (brack
|| colon
)) {
1804 nm
= (brack
? brack
+ 1 : colon
+ 1);
1805 lbrack
= rbrack
= 0;
1813 /* VMS pre V4.4,convert '-'s in filenames. */
1814 if (lbrack
== rbrack
)
1816 if (dots
< 2) /* this is to allow negative version numbers */
1821 if (lbrack
> rbrack
&&
1822 ((p
[-1] == '.' || p
[-1] == '[' || p
[-1] == '<') &&
1823 (p
[1] == '.' || p
[1] == ']' || p
[1] == '>')))
1829 /* count open brackets, reset close bracket pointer */
1830 if (p
[0] == '[' || p
[0] == '<')
1831 lbrack
++, brack
= 0;
1832 /* count close brackets, set close bracket pointer */
1833 if (p
[0] == ']' || p
[0] == '>')
1834 rbrack
++, brack
= p
;
1835 /* detect ][ or >< */
1836 if ((p
[0] == ']' || p
[0] == '>') && (p
[1] == '[' || p
[1] == '<'))
1838 if ((p
[0] == ':' || p
[0] == ']' || p
[0] == '>') && p
[1] == '~')
1839 nm
= p
+ 1, lose
= 1;
1840 if (p
[0] == ':' && (colon
|| slash
))
1841 /* if dev1:[dir]dev2:, move nm to dev2: */
1847 /* If /name/dev:, move nm to dev: */
1850 /* If node::dev:, move colon following dev */
1851 else if (colon
&& colon
[-1] == ':')
1853 /* If dev1:dev2:, move nm to dev2: */
1854 else if (colon
&& colon
[-1] != ':')
1859 if (p
[0] == ':' && !colon
)
1865 if (lbrack
== rbrack
)
1868 else if (p
[0] == '.')
1876 if (index (nm
, '/'))
1877 return build_string (sys_translate_unix (nm
));
1879 if (nm
== SDATA (name
))
1881 return build_string (nm
);
1885 /* Now determine directory to start with and put it in NEWDIR */
1889 if (nm
[0] == '~') /* prefix ~ */
1894 || nm
[1] == 0)/* ~/filename */
1896 if (!(newdir
= (unsigned char *) egetenv ("HOME")))
1897 newdir
= (unsigned char *) "";
1900 nm
++; /* Don't leave the slash in nm. */
1903 else /* ~user/filename */
1905 /* Get past ~ to user */
1906 unsigned char *user
= nm
+ 1;
1907 /* Find end of name. */
1908 unsigned char *ptr
= (unsigned char *) index (user
, '/');
1909 int len
= ptr
? ptr
- user
: strlen (user
);
1911 unsigned char *ptr1
= index (user
, ':');
1912 if (ptr1
!= 0 && ptr1
- user
< len
)
1915 /* Copy the user name into temp storage. */
1916 o
= (unsigned char *) alloca (len
+ 1);
1917 bcopy ((char *) user
, o
, len
);
1920 /* Look up the user name. */
1921 pw
= (struct passwd
*) getpwnam (o
+ 1);
1923 error ("\"%s\" isn't a registered user", o
+ 1);
1925 newdir
= (unsigned char *) pw
->pw_dir
;
1927 /* Discard the user name from NM. */
1934 #endif /* not VMS */
1938 defalt
= current_buffer
->directory
;
1939 CHECK_STRING (defalt
);
1940 newdir
= SDATA (defalt
);
1943 /* Now concatenate the directory and name to new space in the stack frame */
1945 tlen
= (newdir
? strlen (newdir
) + 1 : 0) + strlen (nm
) + 1;
1946 target
= (unsigned char *) alloca (tlen
);
1952 if (nm
[0] == 0 || nm
[0] == '/')
1953 strcpy (target
, newdir
);
1956 file_name_as_directory (target
, newdir
);
1959 strcat (target
, nm
);
1961 if (index (target
, '/'))
1962 strcpy (target
, sys_translate_unix (target
));
1965 /* Now canonicalize by removing /. and /foo/.. if they appear */
1973 if (*p
!= ']' && *p
!= '>' && *p
!= '-')
1979 else if ((p
[0] == ']' || p
[0] == '>') && p
[0] == p
[1] + 2)
1980 /* brackets are offset from each other by 2 */
1983 if (*p
!= '.' && *p
!= '-' && o
[-1] != '.')
1984 /* convert [foo][bar] to [bar] */
1985 while (o
[-1] != '[' && o
[-1] != '<')
1987 else if (*p
== '-' && *o
!= '.')
1990 else if (p
[0] == '-' && o
[-1] == '.' &&
1991 (p
[1] == '.' || p
[1] == ']' || p
[1] == '>'))
1992 /* flush .foo.- ; leave - if stopped by '[' or '<' */
1996 while (o
[-1] != '.' && o
[-1] != '[' && o
[-1] != '<');
1997 if (p
[1] == '.') /* foo.-.bar ==> bar. */
1999 else if (o
[-1] == '.') /* '.foo.-]' ==> ']' */
2001 /* else [foo.-] ==> [-] */
2007 o
[-1] != '[' && o
[-1] != '<' && o
[-1] != '.' &&
2008 p
[1] != ']' && p
[1] != '>' && p
[1] != '.')
2018 else if (!strncmp (p
, "//", 2)
2020 /* // at start of filename is meaningful in Apollo system. */
2028 else if (p
[0] == '/' && p
[1] == '.' &&
2029 (p
[2] == '/' || p
[2] == 0))
2031 else if (!strncmp (p
, "/..", 3)
2032 /* `/../' is the "superroot" on certain file systems. */
2034 && (p
[3] == '/' || p
[3] == 0))
2036 while (o
!= target
&& *--o
!= '/')
2039 if (o
== target
+ 1 && o
[-1] == '/' && o
[0] == '/')
2043 if (o
== target
&& *o
== '/')
2051 #endif /* not VMS */
2054 return make_string (target
, o
- target
);
2058 /* If /~ or // appears, discard everything through first slash. */
2060 file_name_absolute_p (filename
)
2061 const unsigned char *filename
;
2064 (IS_DIRECTORY_SEP (*filename
) || *filename
== '~'
2066 /* ??? This criterion is probably wrong for '<'. */
2067 || index (filename
, ':') || index (filename
, '<')
2068 || (*filename
== '[' && (filename
[1] != '-'
2069 || (filename
[2] != '.' && filename
[2] != ']'))
2070 && filename
[1] != '.')
2073 || (IS_DRIVE (*filename
) && IS_DEVICE_SEP (filename
[1])
2074 && IS_DIRECTORY_SEP (filename
[2]))
2079 static unsigned char *
2080 search_embedded_absfilename (nm
, endp
)
2081 unsigned char *nm
, *endp
;
2083 unsigned char *p
, *s
;
2085 for (p
= nm
+ 1; p
< endp
; p
++)
2089 || p
[-1] == ':' || p
[-1] == ']' || p
[-1] == '>'
2091 || IS_DIRECTORY_SEP (p
[-1]))
2092 && file_name_absolute_p (p
)
2093 #if defined (APOLLO) || defined (WINDOWSNT) || defined(CYGWIN)
2094 /* // at start of file name is meaningful in Apollo,
2095 WindowsNT and Cygwin systems. */
2096 && !(IS_DIRECTORY_SEP (p
[0]) && p
- 1 == nm
)
2097 #endif /* not (APOLLO || WINDOWSNT || CYGWIN) */
2100 for (s
= p
; *s
&& (!IS_DIRECTORY_SEP (*s
)
2105 if (p
[0] == '~' && s
> p
+ 1) /* we've got "/~something/" */
2107 unsigned char *o
= alloca (s
- p
+ 1);
2109 bcopy (p
, o
, s
- p
);
2112 /* If we have ~user and `user' exists, discard
2113 everything up to ~. But if `user' does not exist, leave
2114 ~user alone, it might be a literal file name. */
2115 if ((pw
= getpwnam (o
+ 1)))
2127 DEFUN ("substitute-in-file-name", Fsubstitute_in_file_name
,
2128 Ssubstitute_in_file_name
, 1, 1, 0,
2129 doc
: /* Substitute environment variables referred to in FILENAME.
2130 `$FOO' where FOO is an environment variable name means to substitute
2131 the value of that variable. The variable name should be terminated
2132 with a character not a letter, digit or underscore; otherwise, enclose
2133 the entire variable name in braces.
2134 If `/~' appears, all of FILENAME through that `/' is discarded.
2136 On VMS, `$' substitution is not done; this function does little and only
2137 duplicates what `expand-file-name' does. */)
2139 Lisp_Object filename
;
2143 register unsigned char *s
, *p
, *o
, *x
, *endp
;
2144 unsigned char *target
= NULL
;
2146 int substituted
= 0;
2148 Lisp_Object handler
;
2150 CHECK_STRING (filename
);
2152 /* If the file name has special constructs in it,
2153 call the corresponding file handler. */
2154 handler
= Ffind_file_name_handler (filename
, Qsubstitute_in_file_name
);
2155 if (!NILP (handler
))
2156 return call2 (handler
, Qsubstitute_in_file_name
, filename
);
2158 nm
= SDATA (filename
);
2160 nm
= strcpy (alloca (strlen (nm
) + 1), nm
);
2161 CORRECT_DIR_SEPS (nm
);
2162 substituted
= (strcmp (nm
, SDATA (filename
)) != 0);
2164 endp
= nm
+ SBYTES (filename
);
2166 /* If /~ or // appears, discard everything through first slash. */
2167 p
= search_embedded_absfilename (nm
, endp
);
2169 /* Start over with the new string, so we check the file-name-handler
2170 again. Important with filenames like "/home/foo//:/hello///there"
2171 which whould substitute to "/:/hello///there" rather than "/there". */
2172 return Fsubstitute_in_file_name
2173 (make_specified_string (p
, -1, endp
- p
,
2174 STRING_MULTIBYTE (filename
)));
2180 /* See if any variables are substituted into the string
2181 and find the total length of their values in `total' */
2183 for (p
= nm
; p
!= endp
;)
2193 /* "$$" means a single "$" */
2202 while (p
!= endp
&& *p
!= '}') p
++;
2203 if (*p
!= '}') goto missingclose
;
2209 while (p
!= endp
&& (isalnum (*p
) || *p
== '_')) p
++;
2213 /* Copy out the variable name */
2214 target
= (unsigned char *) alloca (s
- o
+ 1);
2215 strncpy (target
, o
, s
- o
);
2218 strupr (target
); /* $home == $HOME etc. */
2221 /* Get variable value */
2222 o
= (unsigned char *) egetenv (target
);
2225 total
+= strlen (o
);
2235 /* If substitution required, recopy the string and do it */
2236 /* Make space in stack frame for the new copy */
2237 xnm
= (unsigned char *) alloca (SBYTES (filename
) + total
+ 1);
2240 /* Copy the rest of the name through, replacing $ constructs with values */
2257 while (p
!= endp
&& *p
!= '}') p
++;
2258 if (*p
!= '}') goto missingclose
;
2264 while (p
!= endp
&& (isalnum (*p
) || *p
== '_')) p
++;
2268 /* Copy out the variable name */
2269 target
= (unsigned char *) alloca (s
- o
+ 1);
2270 strncpy (target
, o
, s
- o
);
2273 strupr (target
); /* $home == $HOME etc. */
2276 /* Get variable value */
2277 o
= (unsigned char *) egetenv (target
);
2281 strcpy (x
, target
); x
+= strlen (target
);
2283 else if (STRING_MULTIBYTE (filename
))
2285 /* If the original string is multibyte,
2286 convert what we substitute into multibyte. */
2289 int c
= unibyte_char_to_multibyte (*o
++);
2290 x
+= CHAR_STRING (c
, x
);
2302 /* If /~ or // appears, discard everything through first slash. */
2303 while ((p
= search_embedded_absfilename (xnm
, x
)))
2304 /* This time we do not start over because we've already expanded envvars
2305 and replaced $$ with $. Maybe we should start over as well, but we'd
2306 need to quote some $ to $$ first. */
2309 return make_specified_string (xnm
, -1, x
- xnm
, STRING_MULTIBYTE (filename
));
2312 error ("Bad format environment-variable substitution");
2314 error ("Missing \"}\" in environment-variable substitution");
2316 error ("Substituting nonexistent environment variable \"%s\"", target
);
2319 #endif /* not VMS */
2323 /* A slightly faster and more convenient way to get
2324 (directory-file-name (expand-file-name FOO)). */
2327 expand_and_dir_to_file (filename
, defdir
)
2328 Lisp_Object filename
, defdir
;
2330 register Lisp_Object absname
;
2332 absname
= Fexpand_file_name (filename
, defdir
);
2335 register int c
= SREF (absname
, SBYTES (absname
) - 1);
2336 if (c
== ':' || c
== ']' || c
== '>')
2337 absname
= Fdirectory_file_name (absname
);
2340 /* Remove final slash, if any (unless this is the root dir).
2341 stat behaves differently depending! */
2342 if (SCHARS (absname
) > 1
2343 && IS_DIRECTORY_SEP (SREF (absname
, SBYTES (absname
) - 1))
2344 && !IS_DEVICE_SEP (SREF (absname
, SBYTES (absname
)-2)))
2345 /* We cannot take shortcuts; they might be wrong for magic file names. */
2346 absname
= Fdirectory_file_name (absname
);
2351 /* Signal an error if the file ABSNAME already exists.
2352 If INTERACTIVE is nonzero, ask the user whether to proceed,
2353 and bypass the error if the user says to go ahead.
2354 QUERYSTRING is a name for the action that is being considered
2357 *STATPTR is used to store the stat information if the file exists.
2358 If the file does not exist, STATPTR->st_mode is set to 0.
2359 If STATPTR is null, we don't store into it.
2361 If QUICK is nonzero, we ask for y or n, not yes or no. */
2364 barf_or_query_if_file_exists (absname
, querystring
, interactive
, statptr
, quick
)
2365 Lisp_Object absname
;
2366 unsigned char *querystring
;
2368 struct stat
*statptr
;
2371 register Lisp_Object tem
, encoded_filename
;
2372 struct stat statbuf
;
2373 struct gcpro gcpro1
;
2375 encoded_filename
= ENCODE_FILE (absname
);
2377 /* stat is a good way to tell whether the file exists,
2378 regardless of what access permissions it has. */
2379 if (lstat (SDATA (encoded_filename
), &statbuf
) >= 0)
2382 Fsignal (Qfile_already_exists
,
2383 Fcons (build_string ("File already exists"),
2384 Fcons (absname
, Qnil
)));
2386 tem
= format2 ("File %s already exists; %s anyway? ",
2387 absname
, build_string (querystring
));
2389 tem
= Fy_or_n_p (tem
);
2391 tem
= do_yes_or_no_p (tem
);
2394 Fsignal (Qfile_already_exists
,
2395 Fcons (build_string ("File already exists"),
2396 Fcons (absname
, Qnil
)));
2403 statptr
->st_mode
= 0;
2408 DEFUN ("copy-file", Fcopy_file
, Scopy_file
, 2, 6,
2409 "fCopy file: \nGCopy %s to file: \np\nP",
2410 doc
: /* Copy FILE to NEWNAME. Both args must be strings.
2411 If NEWNAME names a directory, copy FILE there.
2412 Signals a `file-already-exists' error if file NEWNAME already exists,
2413 unless a third argument OK-IF-ALREADY-EXISTS is supplied and non-nil.
2414 A number as third arg means request confirmation if NEWNAME already exists.
2415 This is what happens in interactive use with M-x.
2416 Always sets the file modes of the output file to match the input file.
2418 Fourth arg KEEP-TIME non-nil means give the output file the same
2419 last-modified time as the old one. (This works on only some systems.)
2421 A prefix arg makes KEEP-TIME non-nil.
2423 The optional fifth arg MUSTBENEW, if non-nil, insists on a check
2424 for an existing file with the same name. If MUSTBENEW is `excl',
2425 that means to get an error if the file already exists; never overwrite.
2426 If MUSTBENEW is neither nil nor `excl', that means ask for
2427 confirmation before overwriting, but do go ahead and overwrite the file
2428 if the user confirms.
2430 If PRESERVE-UID-GID is non-nil, we try to transfer the
2431 uid and gid of FILE to NEWNAME. */)
2432 (file
, newname
, ok_if_already_exists
, keep_time
, mustbenew
, preserve_uid_gid
)
2433 Lisp_Object file
, newname
, ok_if_already_exists
, keep_time
, mustbenew
;
2434 Lisp_Object preserve_uid_gid
;
2437 char buf
[16 * 1024];
2438 struct stat st
, out_st
;
2439 Lisp_Object handler
;
2440 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
2441 int count
= SPECPDL_INDEX ();
2442 int input_file_statable_p
;
2443 Lisp_Object encoded_file
, encoded_newname
;
2445 encoded_file
= encoded_newname
= Qnil
;
2446 GCPRO4 (file
, newname
, encoded_file
, encoded_newname
);
2447 CHECK_STRING (file
);
2448 CHECK_STRING (newname
);
2450 if (!NILP (mustbenew
) && !EQ (mustbenew
, Qexcl
))
2451 barf_or_query_if_file_exists (newname
, "overwrite", 1, 0, 1);
2453 if (!NILP (Ffile_directory_p (newname
)))
2454 newname
= Fexpand_file_name (Ffile_name_nondirectory (file
), newname
);
2456 newname
= Fexpand_file_name (newname
, Qnil
);
2458 file
= Fexpand_file_name (file
, Qnil
);
2460 /* If the input file name has special constructs in it,
2461 call the corresponding file handler. */
2462 handler
= Ffind_file_name_handler (file
, Qcopy_file
);
2463 /* Likewise for output file name. */
2465 handler
= Ffind_file_name_handler (newname
, Qcopy_file
);
2466 if (!NILP (handler
))
2467 RETURN_UNGCPRO (call5 (handler
, Qcopy_file
, file
, newname
,
2468 ok_if_already_exists
, keep_time
));
2470 encoded_file
= ENCODE_FILE (file
);
2471 encoded_newname
= ENCODE_FILE (newname
);
2473 if (NILP (ok_if_already_exists
)
2474 || INTEGERP (ok_if_already_exists
))
2475 barf_or_query_if_file_exists (encoded_newname
, "copy to it",
2476 INTEGERP (ok_if_already_exists
), &out_st
, 0);
2477 else if (stat (SDATA (encoded_newname
), &out_st
) < 0)
2481 if (!CopyFile (SDATA (encoded_file
),
2482 SDATA (encoded_newname
),
2484 report_file_error ("Copying file", Fcons (file
, Fcons (newname
, Qnil
)));
2485 /* CopyFile retains the timestamp by default. */
2486 else if (NILP (keep_time
))
2492 EMACS_GET_TIME (now
);
2493 filename
= SDATA (encoded_newname
);
2495 /* Ensure file is writable while its modified time is set. */
2496 attributes
= GetFileAttributes (filename
);
2497 SetFileAttributes (filename
, attributes
& ~FILE_ATTRIBUTE_READONLY
);
2498 if (set_file_times (filename
, now
, now
))
2500 /* Restore original attributes. */
2501 SetFileAttributes (filename
, attributes
);
2502 Fsignal (Qfile_date_error
,
2503 Fcons (build_string ("Cannot set file date"),
2504 Fcons (newname
, Qnil
)));
2506 /* Restore original attributes. */
2507 SetFileAttributes (filename
, attributes
);
2509 #else /* not WINDOWSNT */
2511 ifd
= emacs_open (SDATA (encoded_file
), O_RDONLY
, 0);
2515 report_file_error ("Opening input file", Fcons (file
, Qnil
));
2517 record_unwind_protect (close_file_unwind
, make_number (ifd
));
2519 /* We can only copy regular files and symbolic links. Other files are not
2521 input_file_statable_p
= (fstat (ifd
, &st
) >= 0);
2523 #if !defined (MSDOS) || __DJGPP__ > 1
2524 if (out_st
.st_mode
!= 0
2525 && st
.st_dev
== out_st
.st_dev
&& st
.st_ino
== out_st
.st_ino
)
2528 report_file_error ("Input and output files are the same",
2529 Fcons (file
, Fcons (newname
, Qnil
)));
2533 #if defined (S_ISREG) && defined (S_ISLNK)
2534 if (input_file_statable_p
)
2536 if (!(S_ISREG (st
.st_mode
)) && !(S_ISLNK (st
.st_mode
)))
2538 #if defined (EISDIR)
2539 /* Get a better looking error message. */
2542 report_file_error ("Non-regular file", Fcons (file
, Qnil
));
2545 #endif /* S_ISREG && S_ISLNK */
2548 /* Create the copy file with the same record format as the input file */
2549 ofd
= sys_creat (SDATA (encoded_newname
), 0666, ifd
);
2552 /* System's default file type was set to binary by _fmode in emacs.c. */
2553 ofd
= emacs_open (SDATA (encoded_newname
),
2554 O_WRONLY
| O_TRUNC
| O_CREAT
2555 | (EQ (mustbenew
, Qexcl
) ? O_EXCL
: 0),
2556 S_IREAD
| S_IWRITE
);
2557 #else /* not MSDOS */
2558 ofd
= emacs_open (SDATA (encoded_newname
),
2559 O_WRONLY
| O_TRUNC
| O_CREAT
2560 | (EQ (mustbenew
, Qexcl
) ? O_EXCL
: 0),
2562 #endif /* not MSDOS */
2565 report_file_error ("Opening output file", Fcons (newname
, Qnil
));
2567 record_unwind_protect (close_file_unwind
, make_number (ofd
));
2571 while ((n
= emacs_read (ifd
, buf
, sizeof buf
)) > 0)
2572 if (emacs_write (ofd
, buf
, n
) != n
)
2573 report_file_error ("I/O error", Fcons (newname
, Qnil
));
2577 /* Preserve the original file modes, and if requested, also its
2579 if (input_file_statable_p
)
2581 if (! NILP (preserve_uid_gid
))
2582 fchown (ofd
, st
.st_uid
, st
.st_gid
);
2583 fchmod (ofd
, st
.st_mode
& 07777);
2585 #endif /* not MSDOS */
2587 /* Closing the output clobbers the file times on some systems. */
2588 if (emacs_close (ofd
) < 0)
2589 report_file_error ("I/O error", Fcons (newname
, Qnil
));
2591 if (input_file_statable_p
)
2593 if (!NILP (keep_time
))
2595 EMACS_TIME atime
, mtime
;
2596 EMACS_SET_SECS_USECS (atime
, st
.st_atime
, 0);
2597 EMACS_SET_SECS_USECS (mtime
, st
.st_mtime
, 0);
2598 if (set_file_times (SDATA (encoded_newname
),
2600 Fsignal (Qfile_date_error
,
2601 Fcons (build_string ("Cannot set file date"),
2602 Fcons (newname
, Qnil
)));
2608 #if defined (__DJGPP__) && __DJGPP__ > 1
2609 if (input_file_statable_p
)
2611 /* In DJGPP v2.0 and later, fstat usually returns true file mode bits,
2612 and if it can't, it tells so. Otherwise, under MSDOS we usually
2613 get only the READ bit, which will make the copied file read-only,
2614 so it's better not to chmod at all. */
2615 if ((_djstat_flags
& _STFAIL_WRITEBIT
) == 0)
2616 chmod (SDATA (encoded_newname
), st
.st_mode
& 07777);
2618 #endif /* DJGPP version 2 or newer */
2619 #endif /* not WINDOWSNT */
2621 /* Discard the unwind protects. */
2622 specpdl_ptr
= specpdl
+ count
;
2628 DEFUN ("make-directory-internal", Fmake_directory_internal
,
2629 Smake_directory_internal
, 1, 1, 0,
2630 doc
: /* Create a new directory named DIRECTORY. */)
2632 Lisp_Object directory
;
2634 const unsigned char *dir
;
2635 Lisp_Object handler
;
2636 Lisp_Object encoded_dir
;
2638 CHECK_STRING (directory
);
2639 directory
= Fexpand_file_name (directory
, Qnil
);
2641 handler
= Ffind_file_name_handler (directory
, Qmake_directory_internal
);
2642 if (!NILP (handler
))
2643 return call2 (handler
, Qmake_directory_internal
, directory
);
2645 encoded_dir
= ENCODE_FILE (directory
);
2647 dir
= SDATA (encoded_dir
);
2650 if (mkdir (dir
) != 0)
2652 if (mkdir (dir
, 0777) != 0)
2654 report_file_error ("Creating directory", Flist (1, &directory
));
2659 DEFUN ("delete-directory", Fdelete_directory
, Sdelete_directory
, 1, 1, "FDelete directory: ",
2660 doc
: /* Delete the directory named DIRECTORY. Does not follow symlinks. */)
2662 Lisp_Object directory
;
2664 const unsigned char *dir
;
2665 Lisp_Object handler
;
2666 Lisp_Object encoded_dir
;
2668 CHECK_STRING (directory
);
2669 directory
= Fdirectory_file_name (Fexpand_file_name (directory
, Qnil
));
2671 handler
= Ffind_file_name_handler (directory
, Qdelete_directory
);
2672 if (!NILP (handler
))
2673 return call2 (handler
, Qdelete_directory
, directory
);
2675 encoded_dir
= ENCODE_FILE (directory
);
2677 dir
= SDATA (encoded_dir
);
2679 if (rmdir (dir
) != 0)
2680 report_file_error ("Removing directory", Flist (1, &directory
));
2685 DEFUN ("delete-file", Fdelete_file
, Sdelete_file
, 1, 1, "fDelete file: ",
2686 doc
: /* Delete file named FILENAME. If it is a symlink, remove the symlink.
2687 If file has multiple names, it continues to exist with the other names. */)
2689 Lisp_Object filename
;
2691 Lisp_Object handler
;
2692 Lisp_Object encoded_file
;
2693 struct gcpro gcpro1
;
2696 if (!NILP (Ffile_directory_p (filename
))
2697 && NILP (Ffile_symlink_p (filename
)))
2698 Fsignal (Qfile_error
,
2699 Fcons (build_string ("Removing old name: is a directory"),
2700 Fcons (filename
, Qnil
)));
2702 filename
= Fexpand_file_name (filename
, Qnil
);
2704 handler
= Ffind_file_name_handler (filename
, Qdelete_file
);
2705 if (!NILP (handler
))
2706 return call2 (handler
, Qdelete_file
, filename
);
2708 encoded_file
= ENCODE_FILE (filename
);
2710 if (0 > unlink (SDATA (encoded_file
)))
2711 report_file_error ("Removing old name", Flist (1, &filename
));
2716 internal_delete_file_1 (ignore
)
2722 /* Delete file FILENAME, returning 1 if successful and 0 if failed. */
2725 internal_delete_file (filename
)
2726 Lisp_Object filename
;
2728 return NILP (internal_condition_case_1 (Fdelete_file
, filename
,
2729 Qt
, internal_delete_file_1
));
2732 DEFUN ("rename-file", Frename_file
, Srename_file
, 2, 3,
2733 "fRename file: \nGRename %s to file: \np",
2734 doc
: /* Rename FILE as NEWNAME. Both args must be strings.
2735 If file has names other than FILE, it continues to have those names.
2736 Signals a `file-already-exists' error if a file NEWNAME already exists
2737 unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.
2738 A number as third arg means request confirmation if NEWNAME already exists.
2739 This is what happens in interactive use with M-x. */)
2740 (file
, newname
, ok_if_already_exists
)
2741 Lisp_Object file
, newname
, ok_if_already_exists
;
2744 Lisp_Object args
[2];
2746 Lisp_Object handler
;
2747 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
2748 Lisp_Object encoded_file
, encoded_newname
, symlink_target
;
2750 symlink_target
= encoded_file
= encoded_newname
= Qnil
;
2751 GCPRO5 (file
, newname
, encoded_file
, encoded_newname
, symlink_target
);
2752 CHECK_STRING (file
);
2753 CHECK_STRING (newname
);
2754 file
= Fexpand_file_name (file
, Qnil
);
2756 if (!NILP (Ffile_directory_p (newname
)))
2757 newname
= Fexpand_file_name (Ffile_name_nondirectory (file
), newname
);
2759 newname
= Fexpand_file_name (newname
, Qnil
);
2761 /* If the file name has special constructs in it,
2762 call the corresponding file handler. */
2763 handler
= Ffind_file_name_handler (file
, Qrename_file
);
2765 handler
= Ffind_file_name_handler (newname
, Qrename_file
);
2766 if (!NILP (handler
))
2767 RETURN_UNGCPRO (call4 (handler
, Qrename_file
,
2768 file
, newname
, ok_if_already_exists
));
2770 encoded_file
= ENCODE_FILE (file
);
2771 encoded_newname
= ENCODE_FILE (newname
);
2774 /* If the file names are identical but for the case, don't ask for
2775 confirmation: they simply want to change the letter-case of the
2777 if (NILP (Fstring_equal (Fdowncase (file
), Fdowncase (newname
))))
2779 if (NILP (ok_if_already_exists
)
2780 || INTEGERP (ok_if_already_exists
))
2781 barf_or_query_if_file_exists (encoded_newname
, "rename to it",
2782 INTEGERP (ok_if_already_exists
), 0, 0);
2784 if (0 > rename (SDATA (encoded_file
), SDATA (encoded_newname
)))
2786 if (0 > link (SDATA (encoded_file
), SDATA (encoded_newname
))
2787 || 0 > unlink (SDATA (encoded_file
)))
2793 symlink_target
= Ffile_symlink_p (file
);
2794 if (! NILP (symlink_target
))
2795 Fmake_symbolic_link (symlink_target
, newname
,
2796 NILP (ok_if_already_exists
) ? Qnil
: Qt
);
2799 Fcopy_file (file
, newname
,
2800 /* We have already prompted if it was an integer,
2801 so don't have copy-file prompt again. */
2802 NILP (ok_if_already_exists
) ? Qnil
: Qt
,
2805 Fdelete_file (file
);
2812 report_file_error ("Renaming", Flist (2, args
));
2815 report_file_error ("Renaming", Flist (2, &file
));
2822 DEFUN ("add-name-to-file", Fadd_name_to_file
, Sadd_name_to_file
, 2, 3,
2823 "fAdd name to file: \nGName to add to %s: \np",
2824 doc
: /* Give FILE additional name NEWNAME. Both args must be strings.
2825 Signals a `file-already-exists' error if a file NEWNAME already exists
2826 unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.
2827 A number as third arg means request confirmation if NEWNAME already exists.
2828 This is what happens in interactive use with M-x. */)
2829 (file
, newname
, ok_if_already_exists
)
2830 Lisp_Object file
, newname
, ok_if_already_exists
;
2833 Lisp_Object args
[2];
2835 Lisp_Object handler
;
2836 Lisp_Object encoded_file
, encoded_newname
;
2837 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
2839 GCPRO4 (file
, newname
, encoded_file
, encoded_newname
);
2840 encoded_file
= encoded_newname
= Qnil
;
2841 CHECK_STRING (file
);
2842 CHECK_STRING (newname
);
2843 file
= Fexpand_file_name (file
, Qnil
);
2845 if (!NILP (Ffile_directory_p (newname
)))
2846 newname
= Fexpand_file_name (Ffile_name_nondirectory (file
), newname
);
2848 newname
= Fexpand_file_name (newname
, Qnil
);
2850 /* If the file name has special constructs in it,
2851 call the corresponding file handler. */
2852 handler
= Ffind_file_name_handler (file
, Qadd_name_to_file
);
2853 if (!NILP (handler
))
2854 RETURN_UNGCPRO (call4 (handler
, Qadd_name_to_file
, file
,
2855 newname
, ok_if_already_exists
));
2857 /* If the new name has special constructs in it,
2858 call the corresponding file handler. */
2859 handler
= Ffind_file_name_handler (newname
, Qadd_name_to_file
);
2860 if (!NILP (handler
))
2861 RETURN_UNGCPRO (call4 (handler
, Qadd_name_to_file
, file
,
2862 newname
, ok_if_already_exists
));
2864 encoded_file
= ENCODE_FILE (file
);
2865 encoded_newname
= ENCODE_FILE (newname
);
2867 if (NILP (ok_if_already_exists
)
2868 || INTEGERP (ok_if_already_exists
))
2869 barf_or_query_if_file_exists (encoded_newname
, "make it a new name",
2870 INTEGERP (ok_if_already_exists
), 0, 0);
2872 unlink (SDATA (newname
));
2873 if (0 > link (SDATA (encoded_file
), SDATA (encoded_newname
)))
2878 report_file_error ("Adding new name", Flist (2, args
));
2880 report_file_error ("Adding new name", Flist (2, &file
));
2889 DEFUN ("make-symbolic-link", Fmake_symbolic_link
, Smake_symbolic_link
, 2, 3,
2890 "FMake symbolic link to file: \nGMake symbolic link to file %s: \np",
2891 doc
: /* Make a symbolic link to FILENAME, named LINKNAME.
2892 Both args must be strings.
2893 Signals a `file-already-exists' error if a file LINKNAME already exists
2894 unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.
2895 A number as third arg means request confirmation if LINKNAME already exists.
2896 This happens for interactive use with M-x. */)
2897 (filename
, linkname
, ok_if_already_exists
)
2898 Lisp_Object filename
, linkname
, ok_if_already_exists
;
2901 Lisp_Object args
[2];
2903 Lisp_Object handler
;
2904 Lisp_Object encoded_filename
, encoded_linkname
;
2905 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
2907 GCPRO4 (filename
, linkname
, encoded_filename
, encoded_linkname
);
2908 encoded_filename
= encoded_linkname
= Qnil
;
2909 CHECK_STRING (filename
);
2910 CHECK_STRING (linkname
);
2911 /* If the link target has a ~, we must expand it to get
2912 a truly valid file name. Otherwise, do not expand;
2913 we want to permit links to relative file names. */
2914 if (SREF (filename
, 0) == '~')
2915 filename
= Fexpand_file_name (filename
, Qnil
);
2917 if (!NILP (Ffile_directory_p (linkname
)))
2918 linkname
= Fexpand_file_name (Ffile_name_nondirectory (filename
), linkname
);
2920 linkname
= Fexpand_file_name (linkname
, Qnil
);
2922 /* If the file name has special constructs in it,
2923 call the corresponding file handler. */
2924 handler
= Ffind_file_name_handler (filename
, Qmake_symbolic_link
);
2925 if (!NILP (handler
))
2926 RETURN_UNGCPRO (call4 (handler
, Qmake_symbolic_link
, filename
,
2927 linkname
, ok_if_already_exists
));
2929 /* If the new link name has special constructs in it,
2930 call the corresponding file handler. */
2931 handler
= Ffind_file_name_handler (linkname
, Qmake_symbolic_link
);
2932 if (!NILP (handler
))
2933 RETURN_UNGCPRO (call4 (handler
, Qmake_symbolic_link
, filename
,
2934 linkname
, ok_if_already_exists
));
2936 encoded_filename
= ENCODE_FILE (filename
);
2937 encoded_linkname
= ENCODE_FILE (linkname
);
2939 if (NILP (ok_if_already_exists
)
2940 || INTEGERP (ok_if_already_exists
))
2941 barf_or_query_if_file_exists (encoded_linkname
, "make it a link",
2942 INTEGERP (ok_if_already_exists
), 0, 0);
2943 if (0 > symlink (SDATA (encoded_filename
),
2944 SDATA (encoded_linkname
)))
2946 /* If we didn't complain already, silently delete existing file. */
2947 if (errno
== EEXIST
)
2949 unlink (SDATA (encoded_linkname
));
2950 if (0 <= symlink (SDATA (encoded_filename
),
2951 SDATA (encoded_linkname
)))
2961 report_file_error ("Making symbolic link", Flist (2, args
));
2963 report_file_error ("Making symbolic link", Flist (2, &filename
));
2969 #endif /* S_IFLNK */
2973 DEFUN ("define-logical-name", Fdefine_logical_name
, Sdefine_logical_name
,
2974 2, 2, "sDefine logical name: \nsDefine logical name %s as: ",
2975 doc
: /* Define the job-wide logical name NAME to have the value STRING.
2976 If STRING is nil or a null string, the logical name NAME is deleted. */)
2981 CHECK_STRING (name
);
2983 delete_logical_name (SDATA (name
));
2986 CHECK_STRING (string
);
2988 if (SCHARS (string
) == 0)
2989 delete_logical_name (SDATA (name
));
2991 define_logical_name (SDATA (name
), SDATA (string
));
3000 DEFUN ("sysnetunam", Fsysnetunam
, Ssysnetunam
, 2, 2, 0,
3001 doc
: /* Open a network connection to PATH using LOGIN as the login string. */)
3003 Lisp_Object path
, login
;
3007 CHECK_STRING (path
);
3008 CHECK_STRING (login
);
3010 netresult
= netunam (SDATA (path
), SDATA (login
));
3012 if (netresult
== -1)
3017 #endif /* HPUX_NET */
3019 DEFUN ("file-name-absolute-p", Ffile_name_absolute_p
, Sfile_name_absolute_p
,
3021 doc
: /* Return t if file FILENAME specifies an absolute file name.
3022 On Unix, this is a name starting with a `/' or a `~'. */)
3024 Lisp_Object filename
;
3026 CHECK_STRING (filename
);
3027 return file_name_absolute_p (SDATA (filename
)) ? Qt
: Qnil
;
3030 /* Return nonzero if file FILENAME exists and can be executed. */
3033 check_executable (filename
)
3037 int len
= strlen (filename
);
3040 if (stat (filename
, &st
) < 0)
3042 #if defined (WINDOWSNT) || (defined (MSDOS) && __DJGPP__ > 1)
3043 return ((st
.st_mode
& S_IEXEC
) != 0);
3045 return (S_ISREG (st
.st_mode
)
3047 && (stricmp ((suffix
= filename
+ len
-4), ".com") == 0
3048 || stricmp (suffix
, ".exe") == 0
3049 || stricmp (suffix
, ".bat") == 0)
3050 || (st
.st_mode
& S_IFMT
) == S_IFDIR
);
3051 #endif /* not WINDOWSNT */
3052 #else /* not DOS_NT */
3053 #ifdef HAVE_EUIDACCESS
3054 return (euidaccess (filename
, 1) >= 0);
3056 /* Access isn't quite right because it uses the real uid
3057 and we really want to test with the effective uid.
3058 But Unix doesn't give us a right way to do it. */
3059 return (access (filename
, 1) >= 0);
3061 #endif /* not DOS_NT */
3064 /* Return nonzero if file FILENAME exists and can be written. */
3067 check_writable (filename
)
3072 if (stat (filename
, &st
) < 0)
3074 return (st
.st_mode
& S_IWRITE
|| (st
.st_mode
& S_IFMT
) == S_IFDIR
);
3075 #else /* not MSDOS */
3076 #ifdef HAVE_EUIDACCESS
3077 return (euidaccess (filename
, 2) >= 0);
3079 /* Access isn't quite right because it uses the real uid
3080 and we really want to test with the effective uid.
3081 But Unix doesn't give us a right way to do it.
3082 Opening with O_WRONLY could work for an ordinary file,
3083 but would lose for directories. */
3084 return (access (filename
, 2) >= 0);
3086 #endif /* not MSDOS */
3089 DEFUN ("file-exists-p", Ffile_exists_p
, Sfile_exists_p
, 1, 1, 0,
3090 doc
: /* Return t if file FILENAME exists (whether or not you can read it.)
3091 See also `file-readable-p' and `file-attributes'.
3092 This returns nil for a symlink to a nonexistent file.
3093 Use `file-symlink-p' to test for such links. */)
3095 Lisp_Object filename
;
3097 Lisp_Object absname
;
3098 Lisp_Object handler
;
3099 struct stat statbuf
;
3101 CHECK_STRING (filename
);
3102 absname
= Fexpand_file_name (filename
, Qnil
);
3104 /* If the file name has special constructs in it,
3105 call the corresponding file handler. */
3106 handler
= Ffind_file_name_handler (absname
, Qfile_exists_p
);
3107 if (!NILP (handler
))
3108 return call2 (handler
, Qfile_exists_p
, absname
);
3110 absname
= ENCODE_FILE (absname
);
3112 return (stat (SDATA (absname
), &statbuf
) >= 0) ? Qt
: Qnil
;
3115 DEFUN ("file-executable-p", Ffile_executable_p
, Sfile_executable_p
, 1, 1, 0,
3116 doc
: /* Return t if FILENAME can be executed by you.
3117 For a directory, this means you can access files in that directory. */)
3119 Lisp_Object filename
;
3121 Lisp_Object absname
;
3122 Lisp_Object handler
;
3124 CHECK_STRING (filename
);
3125 absname
= Fexpand_file_name (filename
, Qnil
);
3127 /* If the file name has special constructs in it,
3128 call the corresponding file handler. */
3129 handler
= Ffind_file_name_handler (absname
, Qfile_executable_p
);
3130 if (!NILP (handler
))
3131 return call2 (handler
, Qfile_executable_p
, absname
);
3133 absname
= ENCODE_FILE (absname
);
3135 return (check_executable (SDATA (absname
)) ? Qt
: Qnil
);
3138 DEFUN ("file-readable-p", Ffile_readable_p
, Sfile_readable_p
, 1, 1, 0,
3139 doc
: /* Return t if file FILENAME exists and you can read it.
3140 See also `file-exists-p' and `file-attributes'. */)
3142 Lisp_Object filename
;
3144 Lisp_Object absname
;
3145 Lisp_Object handler
;
3148 struct stat statbuf
;
3150 CHECK_STRING (filename
);
3151 absname
= Fexpand_file_name (filename
, Qnil
);
3153 /* If the file name has special constructs in it,
3154 call the corresponding file handler. */
3155 handler
= Ffind_file_name_handler (absname
, Qfile_readable_p
);
3156 if (!NILP (handler
))
3157 return call2 (handler
, Qfile_readable_p
, absname
);
3159 absname
= ENCODE_FILE (absname
);
3161 #if defined(DOS_NT) || defined(macintosh)
3162 /* Under MS-DOS, Windows, and Macintosh, open does not work for
3164 if (access (SDATA (absname
), 0) == 0)
3167 #else /* not DOS_NT and not macintosh */
3169 #if defined (S_ISFIFO) && defined (O_NONBLOCK)
3170 /* Opening a fifo without O_NONBLOCK can wait.
3171 We don't want to wait. But we don't want to mess wth O_NONBLOCK
3172 except in the case of a fifo, on a system which handles it. */
3173 desc
= stat (SDATA (absname
), &statbuf
);
3176 if (S_ISFIFO (statbuf
.st_mode
))
3177 flags
|= O_NONBLOCK
;
3179 desc
= emacs_open (SDATA (absname
), flags
, 0);
3184 #endif /* not DOS_NT and not macintosh */
3187 /* Having this before file-symlink-p mysteriously caused it to be forgotten
3189 DEFUN ("file-writable-p", Ffile_writable_p
, Sfile_writable_p
, 1, 1, 0,
3190 doc
: /* Return t if file FILENAME can be written or created by you. */)
3192 Lisp_Object filename
;
3194 Lisp_Object absname
, dir
, encoded
;
3195 Lisp_Object handler
;
3196 struct stat statbuf
;
3198 CHECK_STRING (filename
);
3199 absname
= Fexpand_file_name (filename
, Qnil
);
3201 /* If the file name has special constructs in it,
3202 call the corresponding file handler. */
3203 handler
= Ffind_file_name_handler (absname
, Qfile_writable_p
);
3204 if (!NILP (handler
))
3205 return call2 (handler
, Qfile_writable_p
, absname
);
3207 encoded
= ENCODE_FILE (absname
);
3208 if (stat (SDATA (encoded
), &statbuf
) >= 0)
3209 return (check_writable (SDATA (encoded
))
3212 dir
= Ffile_name_directory (absname
);
3215 dir
= Fdirectory_file_name (dir
);
3219 dir
= Fdirectory_file_name (dir
);
3222 dir
= ENCODE_FILE (dir
);
3224 /* The read-only attribute of the parent directory doesn't affect
3225 whether a file or directory can be created within it. Some day we
3226 should check ACLs though, which do affect this. */
3227 if (stat (SDATA (dir
), &statbuf
) < 0)
3229 return (statbuf
.st_mode
& S_IFMT
) == S_IFDIR
? Qt
: Qnil
;
3231 return (check_writable (!NILP (dir
) ? (char *) SDATA (dir
) : "")
3236 DEFUN ("access-file", Faccess_file
, Saccess_file
, 2, 2, 0,
3237 doc
: /* Access file FILENAME, and get an error if that does not work.
3238 The second argument STRING is used in the error message.
3239 If there is no error, returns nil. */)
3241 Lisp_Object filename
, string
;
3243 Lisp_Object handler
, encoded_filename
, absname
;
3246 CHECK_STRING (filename
);
3247 absname
= Fexpand_file_name (filename
, Qnil
);
3249 CHECK_STRING (string
);
3251 /* If the file name has special constructs in it,
3252 call the corresponding file handler. */
3253 handler
= Ffind_file_name_handler (absname
, Qaccess_file
);
3254 if (!NILP (handler
))
3255 return call3 (handler
, Qaccess_file
, absname
, string
);
3257 encoded_filename
= ENCODE_FILE (absname
);
3259 fd
= emacs_open (SDATA (encoded_filename
), O_RDONLY
, 0);
3261 report_file_error (SDATA (string
), Fcons (filename
, Qnil
));
3267 DEFUN ("file-symlink-p", Ffile_symlink_p
, Sfile_symlink_p
, 1, 1, 0,
3268 doc
: /* Return non-nil if file FILENAME is the name of a symbolic link.
3269 The value is the link target, as a string.
3270 Otherwise it returns nil.
3272 This function returns t when given the name of a symlink that
3273 points to a nonexistent file. */)
3275 Lisp_Object filename
;
3277 Lisp_Object handler
;
3279 CHECK_STRING (filename
);
3280 filename
= Fexpand_file_name (filename
, Qnil
);
3282 /* If the file name has special constructs in it,
3283 call the corresponding file handler. */
3284 handler
= Ffind_file_name_handler (filename
, Qfile_symlink_p
);
3285 if (!NILP (handler
))
3286 return call2 (handler
, Qfile_symlink_p
, filename
);
3295 filename
= ENCODE_FILE (filename
);
3302 buf
= (char *) xrealloc (buf
, bufsize
);
3303 bzero (buf
, bufsize
);
3306 valsize
= readlink (SDATA (filename
), buf
, bufsize
);
3310 /* HP-UX reports ERANGE if buffer is too small. */
3311 if (errno
== ERANGE
)
3321 while (valsize
>= bufsize
);
3323 val
= make_string (buf
, valsize
);
3324 if (buf
[0] == '/' && index (buf
, ':'))
3325 val
= concat2 (build_string ("/:"), val
);
3327 val
= DECODE_FILE (val
);
3330 #else /* not S_IFLNK */
3332 #endif /* not S_IFLNK */
3335 DEFUN ("file-directory-p", Ffile_directory_p
, Sfile_directory_p
, 1, 1, 0,
3336 doc
: /* Return t if FILENAME names an existing directory.
3337 Symbolic links to directories count as directories.
3338 See `file-symlink-p' to distinguish symlinks. */)
3340 Lisp_Object filename
;
3342 register Lisp_Object absname
;
3344 Lisp_Object handler
;
3346 absname
= expand_and_dir_to_file (filename
, current_buffer
->directory
);
3348 /* If the file name has special constructs in it,
3349 call the corresponding file handler. */
3350 handler
= Ffind_file_name_handler (absname
, Qfile_directory_p
);
3351 if (!NILP (handler
))
3352 return call2 (handler
, Qfile_directory_p
, absname
);
3354 absname
= ENCODE_FILE (absname
);
3356 if (stat (SDATA (absname
), &st
) < 0)
3358 return (st
.st_mode
& S_IFMT
) == S_IFDIR
? Qt
: Qnil
;
3361 DEFUN ("file-accessible-directory-p", Ffile_accessible_directory_p
, Sfile_accessible_directory_p
, 1, 1, 0,
3362 doc
: /* Return t if file FILENAME names a directory you can open.
3363 For the value to be t, FILENAME must specify the name of a directory as a file,
3364 and the directory must allow you to open files in it. In order to use a
3365 directory as a buffer's current directory, this predicate must return true.
3366 A directory name spec may be given instead; then the value is t
3367 if the directory so specified exists and really is a readable and
3368 searchable directory. */)
3370 Lisp_Object filename
;
3372 Lisp_Object handler
;
3374 struct gcpro gcpro1
;
3376 /* If the file name has special constructs in it,
3377 call the corresponding file handler. */
3378 handler
= Ffind_file_name_handler (filename
, Qfile_accessible_directory_p
);
3379 if (!NILP (handler
))
3380 return call2 (handler
, Qfile_accessible_directory_p
, filename
);
3383 tem
= (NILP (Ffile_directory_p (filename
))
3384 || NILP (Ffile_executable_p (filename
)));
3386 return tem
? Qnil
: Qt
;
3389 DEFUN ("file-regular-p", Ffile_regular_p
, Sfile_regular_p
, 1, 1, 0,
3390 doc
: /* Return t if file FILENAME is the name of a regular file.
3391 This is the sort of file that holds an ordinary stream of data bytes. */)
3393 Lisp_Object filename
;
3395 register Lisp_Object absname
;
3397 Lisp_Object handler
;
3399 absname
= expand_and_dir_to_file (filename
, current_buffer
->directory
);
3401 /* If the file name has special constructs in it,
3402 call the corresponding file handler. */
3403 handler
= Ffind_file_name_handler (absname
, Qfile_regular_p
);
3404 if (!NILP (handler
))
3405 return call2 (handler
, Qfile_regular_p
, absname
);
3407 absname
= ENCODE_FILE (absname
);
3412 Lisp_Object tem
= Vw32_get_true_file_attributes
;
3414 /* Tell stat to use expensive method to get accurate info. */
3415 Vw32_get_true_file_attributes
= Qt
;
3416 result
= stat (SDATA (absname
), &st
);
3417 Vw32_get_true_file_attributes
= tem
;
3421 return (st
.st_mode
& S_IFMT
) == S_IFREG
? Qt
: Qnil
;
3424 if (stat (SDATA (absname
), &st
) < 0)
3426 return (st
.st_mode
& S_IFMT
) == S_IFREG
? Qt
: Qnil
;
3430 DEFUN ("file-modes", Ffile_modes
, Sfile_modes
, 1, 1, 0,
3431 doc
: /* Return mode bits of file named FILENAME, as an integer.
3432 Return nil, if file does not exist or is not accessible. */)
3434 Lisp_Object filename
;
3436 Lisp_Object absname
;
3438 Lisp_Object handler
;
3440 absname
= expand_and_dir_to_file (filename
, current_buffer
->directory
);
3442 /* If the file name has special constructs in it,
3443 call the corresponding file handler. */
3444 handler
= Ffind_file_name_handler (absname
, Qfile_modes
);
3445 if (!NILP (handler
))
3446 return call2 (handler
, Qfile_modes
, absname
);
3448 absname
= ENCODE_FILE (absname
);
3450 if (stat (SDATA (absname
), &st
) < 0)
3452 #if defined (MSDOS) && __DJGPP__ < 2
3453 if (check_executable (SDATA (absname
)))
3454 st
.st_mode
|= S_IEXEC
;
3455 #endif /* MSDOS && __DJGPP__ < 2 */
3457 return make_number (st
.st_mode
& 07777);
3460 DEFUN ("set-file-modes", Fset_file_modes
, Sset_file_modes
, 2, 2, 0,
3461 doc
: /* Set mode bits of file named FILENAME to MODE (an integer).
3462 Only the 12 low bits of MODE are used. */)
3464 Lisp_Object filename
, mode
;
3466 Lisp_Object absname
, encoded_absname
;
3467 Lisp_Object handler
;
3469 absname
= Fexpand_file_name (filename
, current_buffer
->directory
);
3470 CHECK_NUMBER (mode
);
3472 /* If the file name has special constructs in it,
3473 call the corresponding file handler. */
3474 handler
= Ffind_file_name_handler (absname
, Qset_file_modes
);
3475 if (!NILP (handler
))
3476 return call3 (handler
, Qset_file_modes
, absname
, mode
);
3478 encoded_absname
= ENCODE_FILE (absname
);
3480 if (chmod (SDATA (encoded_absname
), XINT (mode
)) < 0)
3481 report_file_error ("Doing chmod", Fcons (absname
, Qnil
));
3486 DEFUN ("set-default-file-modes", Fset_default_file_modes
, Sset_default_file_modes
, 1, 1, 0,
3487 doc
: /* Set the file permission bits for newly created files.
3488 The argument MODE should be an integer; only the low 9 bits are used.
3489 This setting is inherited by subprocesses. */)
3493 CHECK_NUMBER (mode
);
3495 umask ((~ XINT (mode
)) & 0777);
3500 DEFUN ("default-file-modes", Fdefault_file_modes
, Sdefault_file_modes
, 0, 0, 0,
3501 doc
: /* Return the default file protection for created files.
3502 The value is an integer. */)
3508 realmask
= umask (0);
3511 XSETINT (value
, (~ realmask
) & 0777);
3515 extern int lisp_time_argument
P_ ((Lisp_Object
, time_t *, int *));
3517 DEFUN ("set-file-times", Fset_file_times
, Sset_file_times
, 1, 2, 0,
3518 doc
: /* Set times of file FILENAME to TIME.
3519 Set both access and modification times.
3520 Return t on success, else nil.
3521 Use the current time if TIME is nil. TIME is in the format of
3524 Lisp_Object filename
, time
;
3526 Lisp_Object absname
, encoded_absname
;
3527 Lisp_Object handler
;
3531 if (! lisp_time_argument (time
, &sec
, &usec
))
3532 error ("Invalid time specification");
3534 absname
= Fexpand_file_name (filename
, current_buffer
->directory
);
3536 /* If the file name has special constructs in it,
3537 call the corresponding file handler. */
3538 handler
= Ffind_file_name_handler (absname
, Qset_file_times
);
3539 if (!NILP (handler
))
3540 return call3 (handler
, Qset_file_times
, absname
, time
);
3542 encoded_absname
= ENCODE_FILE (absname
);
3547 EMACS_SET_SECS (t
, sec
);
3548 EMACS_SET_USECS (t
, usec
);
3550 if (set_file_times (SDATA (encoded_absname
), t
, t
))
3555 /* Setting times on a directory always fails. */
3556 if (stat (SDATA (encoded_absname
), &st
) == 0
3557 && (st
.st_mode
& S_IFMT
) == S_IFDIR
)
3560 report_file_error ("Setting file times", Fcons (absname
, Qnil
));
3573 DEFUN ("unix-sync", Funix_sync
, Sunix_sync
, 0, 0, "",
3574 doc
: /* Tell Unix to finish all pending disk updates. */)
3583 DEFUN ("file-newer-than-file-p", Ffile_newer_than_file_p
, Sfile_newer_than_file_p
, 2, 2, 0,
3584 doc
: /* Return t if file FILE1 is newer than file FILE2.
3585 If FILE1 does not exist, the answer is nil;
3586 otherwise, if FILE2 does not exist, the answer is t. */)
3588 Lisp_Object file1
, file2
;
3590 Lisp_Object absname1
, absname2
;
3593 Lisp_Object handler
;
3594 struct gcpro gcpro1
, gcpro2
;
3596 CHECK_STRING (file1
);
3597 CHECK_STRING (file2
);
3600 GCPRO2 (absname1
, file2
);
3601 absname1
= expand_and_dir_to_file (file1
, current_buffer
->directory
);
3602 absname2
= expand_and_dir_to_file (file2
, current_buffer
->directory
);
3605 /* If the file name has special constructs in it,
3606 call the corresponding file handler. */
3607 handler
= Ffind_file_name_handler (absname1
, Qfile_newer_than_file_p
);
3609 handler
= Ffind_file_name_handler (absname2
, Qfile_newer_than_file_p
);
3610 if (!NILP (handler
))
3611 return call3 (handler
, Qfile_newer_than_file_p
, absname1
, absname2
);
3613 GCPRO2 (absname1
, absname2
);
3614 absname1
= ENCODE_FILE (absname1
);
3615 absname2
= ENCODE_FILE (absname2
);
3618 if (stat (SDATA (absname1
), &st
) < 0)
3621 mtime1
= st
.st_mtime
;
3623 if (stat (SDATA (absname2
), &st
) < 0)
3626 return (mtime1
> st
.st_mtime
) ? Qt
: Qnil
;
3630 Lisp_Object Qfind_buffer_file_type
;
3633 #ifndef READ_BUF_SIZE
3634 #define READ_BUF_SIZE (64 << 10)
3637 extern void adjust_markers_for_delete
P_ ((int, int, int, int));
3639 /* This function is called after Lisp functions to decide a coding
3640 system are called, or when they cause an error. Before they are
3641 called, the current buffer is set unibyte and it contains only a
3642 newly inserted text (thus the buffer was empty before the
3645 The functions may set markers, overlays, text properties, or even
3646 alter the buffer contents, change the current buffer.
3648 Here, we reset all those changes by:
3649 o set back the current buffer.
3650 o move all markers and overlays to BEG.
3651 o remove all text properties.
3652 o set back the buffer multibyteness. */
3655 decide_coding_unwind (unwind_data
)
3656 Lisp_Object unwind_data
;
3658 Lisp_Object multibyte
, undo_list
, buffer
;
3660 multibyte
= XCAR (unwind_data
);
3661 unwind_data
= XCDR (unwind_data
);
3662 undo_list
= XCAR (unwind_data
);
3663 buffer
= XCDR (unwind_data
);
3665 if (current_buffer
!= XBUFFER (buffer
))
3666 set_buffer_internal (XBUFFER (buffer
));
3667 adjust_markers_for_delete (BEG
, BEG_BYTE
, Z
, Z_BYTE
);
3668 adjust_overlays_for_delete (BEG
, Z
- BEG
);
3669 BUF_INTERVALS (current_buffer
) = 0;
3670 TEMP_SET_PT_BOTH (BEG
, BEG_BYTE
);
3672 /* Now we are safe to change the buffer's multibyteness directly. */
3673 current_buffer
->enable_multibyte_characters
= multibyte
;
3674 current_buffer
->undo_list
= undo_list
;
3680 /* Used to pass values from insert-file-contents to read_non_regular. */
3682 static int non_regular_fd
;
3683 static int non_regular_inserted
;
3684 static int non_regular_nbytes
;
3687 /* Read from a non-regular file.
3688 Read non_regular_trytry bytes max from non_regular_fd.
3689 Non_regular_inserted specifies where to put the read bytes.
3690 Value is the number of bytes read. */
3699 nbytes
= emacs_read (non_regular_fd
,
3700 BEG_ADDR
+ PT_BYTE
- BEG_BYTE
+ non_regular_inserted
,
3701 non_regular_nbytes
);
3703 return make_number (nbytes
);
3707 /* Condition-case handler used when reading from non-regular files
3708 in insert-file-contents. */
3711 read_non_regular_quit ()
3717 DEFUN ("insert-file-contents", Finsert_file_contents
, Sinsert_file_contents
,
3719 doc
: /* Insert contents of file FILENAME after point.
3720 Returns list of absolute file name and number of characters inserted.
3721 If second argument VISIT is non-nil, the buffer's visited filename
3722 and last save file modtime are set, and it is marked unmodified.
3723 If visiting and the file does not exist, visiting is completed
3724 before the error is signaled.
3725 The optional third and fourth arguments BEG and END
3726 specify what portion of the file to insert.
3727 These arguments count bytes in the file, not characters in the buffer.
3728 If VISIT is non-nil, BEG and END must be nil.
3730 If optional fifth argument REPLACE is non-nil,
3731 it means replace the current buffer contents (in the accessible portion)
3732 with the file contents. This is better than simply deleting and inserting
3733 the whole thing because (1) it preserves some marker positions
3734 and (2) it puts less data in the undo list.
3735 When REPLACE is non-nil, the value is the number of characters actually read,
3736 which is often less than the number of characters to be read.
3738 This does code conversion according to the value of
3739 `coding-system-for-read' or `file-coding-system-alist',
3740 and sets the variable `last-coding-system-used' to the coding system
3742 (filename
, visit
, beg
, end
, replace
)
3743 Lisp_Object filename
, visit
, beg
, end
, replace
;
3748 register int how_much
;
3749 register int unprocessed
;
3750 int count
= SPECPDL_INDEX ();
3751 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
3752 Lisp_Object handler
, val
, insval
, orig_filename
;
3755 int not_regular
= 0;
3756 unsigned char read_buf
[READ_BUF_SIZE
];
3757 struct coding_system coding
;
3758 unsigned char buffer
[1 << 14];
3759 int replace_handled
= 0;
3760 int set_coding_system
= 0;
3761 int coding_system_decided
= 0;
3763 Lisp_Object old_Vdeactivate_mark
= Vdeactivate_mark
;
3764 int we_locked_file
= 0;
3766 if (current_buffer
->base_buffer
&& ! NILP (visit
))
3767 error ("Cannot do file visiting in an indirect buffer");
3769 if (!NILP (current_buffer
->read_only
))
3770 Fbarf_if_buffer_read_only ();
3774 orig_filename
= Qnil
;
3776 GCPRO4 (filename
, val
, p
, orig_filename
);
3778 CHECK_STRING (filename
);
3779 filename
= Fexpand_file_name (filename
, Qnil
);
3781 /* If the file name has special constructs in it,
3782 call the corresponding file handler. */
3783 handler
= Ffind_file_name_handler (filename
, Qinsert_file_contents
);
3784 if (!NILP (handler
))
3786 val
= call6 (handler
, Qinsert_file_contents
, filename
,
3787 visit
, beg
, end
, replace
);
3788 if (CONSP (val
) && CONSP (XCDR (val
)))
3789 inserted
= XINT (XCAR (XCDR (val
)));
3793 orig_filename
= filename
;
3794 filename
= ENCODE_FILE (filename
);
3800 Lisp_Object tem
= Vw32_get_true_file_attributes
;
3802 /* Tell stat to use expensive method to get accurate info. */
3803 Vw32_get_true_file_attributes
= Qt
;
3804 total
= stat (SDATA (filename
), &st
);
3805 Vw32_get_true_file_attributes
= tem
;
3810 if (stat (SDATA (filename
), &st
) < 0)
3812 if ((fd
= emacs_open (SDATA (filename
), O_RDONLY
, 0)) < 0
3813 || fstat (fd
, &st
) < 0)
3814 #endif /* not APOLLO */
3815 #endif /* WINDOWSNT */
3817 if (fd
>= 0) emacs_close (fd
);
3820 report_file_error ("Opening input file", Fcons (orig_filename
, Qnil
));
3823 if (!NILP (Vcoding_system_for_read
))
3824 Fset (Qbuffer_file_coding_system
, Vcoding_system_for_read
);
3829 /* This code will need to be changed in order to work on named
3830 pipes, and it's probably just not worth it. So we should at
3831 least signal an error. */
3832 if (!S_ISREG (st
.st_mode
))
3839 if (! NILP (replace
) || ! NILP (beg
) || ! NILP (end
))
3840 Fsignal (Qfile_error
,
3841 Fcons (build_string ("not a regular file"),
3842 Fcons (orig_filename
, Qnil
)));
3847 if ((fd
= emacs_open (SDATA (filename
), O_RDONLY
, 0)) < 0)
3850 /* Replacement should preserve point as it preserves markers. */
3851 if (!NILP (replace
))
3852 record_unwind_protect (restore_point_unwind
, Fpoint_marker ());
3854 record_unwind_protect (close_file_unwind
, make_number (fd
));
3856 /* Supposedly happens on VMS. */
3857 /* Can happen on any platform that uses long as type of off_t, but allows
3858 file sizes to exceed 2Gb. VMS is no longer officially supported, so
3859 give a message suitable for the latter case. */
3860 if (! not_regular
&& st
.st_size
< 0)
3861 error ("Maximum buffer size exceeded");
3863 /* Prevent redisplay optimizations. */
3864 current_buffer
->clip_changed
= 1;
3868 if (!NILP (beg
) || !NILP (end
))
3869 error ("Attempt to visit less than an entire file");
3870 if (BEG
< Z
&& NILP (replace
))
3871 error ("Cannot do file visiting in a non-empty buffer");
3877 XSETFASTINT (beg
, 0);
3885 XSETINT (end
, st
.st_size
);
3887 /* Arithmetic overflow can occur if an Emacs integer cannot
3888 represent the file size, or if the calculations below
3889 overflow. The calculations below double the file size
3890 twice, so check that it can be multiplied by 4 safely. */
3891 if (XINT (end
) != st
.st_size
3892 || ((int) st
.st_size
* 4) / 4 != st
.st_size
)
3893 error ("Maximum buffer size exceeded");
3895 /* The file size returned from stat may be zero, but data
3896 may be readable nonetheless, for example when this is a
3897 file in the /proc filesystem. */
3898 if (st
.st_size
== 0)
3899 XSETINT (end
, READ_BUF_SIZE
);
3903 if (EQ (Vcoding_system_for_read
, Qauto_save_coding
))
3905 /* We use emacs-mule for auto saving... */
3906 setup_coding_system (Qemacs_mule
, &coding
);
3907 /* ... but with the special flag to indicate to read in a
3908 multibyte sequence for eight-bit-control char as is. */
3910 coding
.src_multibyte
= 0;
3911 coding
.dst_multibyte
3912 = !NILP (current_buffer
->enable_multibyte_characters
);
3913 coding
.eol_type
= CODING_EOL_LF
;
3914 coding_system_decided
= 1;
3918 /* Decide the coding system to use for reading the file now
3919 because we can't use an optimized method for handling
3920 `coding:' tag if the current buffer is not empty. */
3924 if (!NILP (Vcoding_system_for_read
))
3925 val
= Vcoding_system_for_read
;
3928 /* Don't try looking inside a file for a coding system
3929 specification if it is not seekable. */
3930 if (! not_regular
&& ! NILP (Vset_auto_coding_function
))
3932 /* Find a coding system specified in the heading two
3933 lines or in the tailing several lines of the file.
3934 We assume that the 1K-byte and 3K-byte for heading
3935 and tailing respectively are sufficient for this
3939 if (st
.st_size
<= (1024 * 4))
3940 nread
= emacs_read (fd
, read_buf
, 1024 * 4);
3943 nread
= emacs_read (fd
, read_buf
, 1024);
3946 if (lseek (fd
, st
.st_size
- (1024 * 3), 0) < 0)
3947 report_file_error ("Setting file position",
3948 Fcons (orig_filename
, Qnil
));
3949 nread
+= emacs_read (fd
, read_buf
+ nread
, 1024 * 3);
3954 error ("IO error reading %s: %s",
3955 SDATA (orig_filename
), emacs_strerror (errno
));
3958 struct buffer
*prev
= current_buffer
;
3962 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
3964 buffer
= Fget_buffer_create (build_string (" *code-converting-work*"));
3965 buf
= XBUFFER (buffer
);
3967 delete_all_overlays (buf
);
3968 buf
->directory
= current_buffer
->directory
;
3969 buf
->read_only
= Qnil
;
3970 buf
->filename
= Qnil
;
3971 buf
->undo_list
= Qt
;
3972 eassert (buf
->overlays_before
== NULL
);
3973 eassert (buf
->overlays_after
== NULL
);
3975 set_buffer_internal (buf
);
3977 buf
->enable_multibyte_characters
= Qnil
;
3979 insert_1_both (read_buf
, nread
, nread
, 0, 0, 0);
3980 TEMP_SET_PT_BOTH (BEG
, BEG_BYTE
);
3981 val
= call2 (Vset_auto_coding_function
,
3982 filename
, make_number (nread
));
3983 set_buffer_internal (prev
);
3985 /* Discard the unwind protect for recovering the
3989 /* Rewind the file for the actual read done later. */
3990 if (lseek (fd
, 0, 0) < 0)
3991 report_file_error ("Setting file position",
3992 Fcons (orig_filename
, Qnil
));
3998 /* If we have not yet decided a coding system, check
3999 file-coding-system-alist. */
4000 Lisp_Object args
[6], coding_systems
;
4002 args
[0] = Qinsert_file_contents
, args
[1] = orig_filename
;
4003 args
[2] = visit
, args
[3] = beg
, args
[4] = end
, args
[5] = replace
;
4004 coding_systems
= Ffind_operation_coding_system (6, args
);
4005 if (CONSP (coding_systems
))
4006 val
= XCAR (coding_systems
);
4010 setup_coding_system (Fcheck_coding_system (val
), &coding
);
4011 /* Ensure we set Vlast_coding_system_used. */
4012 set_coding_system
= 1;
4014 if (NILP (current_buffer
->enable_multibyte_characters
)
4016 /* We must suppress all character code conversion except for
4017 end-of-line conversion. */
4018 setup_raw_text_coding_system (&coding
);
4020 coding
.src_multibyte
= 0;
4021 coding
.dst_multibyte
4022 = !NILP (current_buffer
->enable_multibyte_characters
);
4023 coding_system_decided
= 1;
4026 /* If requested, replace the accessible part of the buffer
4027 with the file contents. Avoid replacing text at the
4028 beginning or end of the buffer that matches the file contents;
4029 that preserves markers pointing to the unchanged parts.
4031 Here we implement this feature in an optimized way
4032 for the case where code conversion is NOT needed.
4033 The following if-statement handles the case of conversion
4034 in a less optimal way.
4036 If the code conversion is "automatic" then we try using this
4037 method and hope for the best.
4038 But if we discover the need for conversion, we give up on this method
4039 and let the following if-statement handle the replace job. */
4042 && !(coding
.common_flags
& CODING_REQUIRE_DECODING_MASK
))
4044 /* same_at_start and same_at_end count bytes,
4045 because file access counts bytes
4046 and BEG and END count bytes. */
4047 int same_at_start
= BEGV_BYTE
;
4048 int same_at_end
= ZV_BYTE
;
4050 /* There is still a possibility we will find the need to do code
4051 conversion. If that happens, we set this variable to 1 to
4052 give up on handling REPLACE in the optimized way. */
4053 int giveup_match_end
= 0;
4055 if (XINT (beg
) != 0)
4057 if (lseek (fd
, XINT (beg
), 0) < 0)
4058 report_file_error ("Setting file position",
4059 Fcons (orig_filename
, Qnil
));
4064 /* Count how many chars at the start of the file
4065 match the text at the beginning of the buffer. */
4070 nread
= emacs_read (fd
, buffer
, sizeof buffer
);
4072 error ("IO error reading %s: %s",
4073 SDATA (orig_filename
), emacs_strerror (errno
));
4074 else if (nread
== 0)
4077 if (coding
.type
== coding_type_undecided
)
4078 detect_coding (&coding
, buffer
, nread
);
4079 if (coding
.common_flags
& CODING_REQUIRE_DECODING_MASK
)
4080 /* We found that the file should be decoded somehow.
4081 Let's give up here. */
4083 giveup_match_end
= 1;
4087 if (coding
.eol_type
== CODING_EOL_UNDECIDED
)
4088 detect_eol (&coding
, buffer
, nread
);
4089 if (coding
.eol_type
!= CODING_EOL_UNDECIDED
4090 && coding
.eol_type
!= CODING_EOL_LF
)
4091 /* We found that the format of eol should be decoded.
4092 Let's give up here. */
4094 giveup_match_end
= 1;
4099 while (bufpos
< nread
&& same_at_start
< ZV_BYTE
4100 && FETCH_BYTE (same_at_start
) == buffer
[bufpos
])
4101 same_at_start
++, bufpos
++;
4102 /* If we found a discrepancy, stop the scan.
4103 Otherwise loop around and scan the next bufferful. */
4104 if (bufpos
!= nread
)
4108 /* If the file matches the buffer completely,
4109 there's no need to replace anything. */
4110 if (same_at_start
- BEGV_BYTE
== XINT (end
))
4114 /* Truncate the buffer to the size of the file. */
4115 del_range_1 (same_at_start
, same_at_end
, 0, 0);
4120 /* Count how many chars at the end of the file
4121 match the text at the end of the buffer. But, if we have
4122 already found that decoding is necessary, don't waste time. */
4123 while (!giveup_match_end
)
4125 int total_read
, nread
, bufpos
, curpos
, trial
;
4127 /* At what file position are we now scanning? */
4128 curpos
= XINT (end
) - (ZV_BYTE
- same_at_end
);
4129 /* If the entire file matches the buffer tail, stop the scan. */
4132 /* How much can we scan in the next step? */
4133 trial
= min (curpos
, sizeof buffer
);
4134 if (lseek (fd
, curpos
- trial
, 0) < 0)
4135 report_file_error ("Setting file position",
4136 Fcons (orig_filename
, Qnil
));
4138 total_read
= nread
= 0;
4139 while (total_read
< trial
)
4141 nread
= emacs_read (fd
, buffer
+ total_read
, trial
- total_read
);
4143 error ("IO error reading %s: %s",
4144 SDATA (orig_filename
), emacs_strerror (errno
));
4145 else if (nread
== 0)
4147 total_read
+= nread
;
4150 /* Scan this bufferful from the end, comparing with
4151 the Emacs buffer. */
4152 bufpos
= total_read
;
4154 /* Compare with same_at_start to avoid counting some buffer text
4155 as matching both at the file's beginning and at the end. */
4156 while (bufpos
> 0 && same_at_end
> same_at_start
4157 && FETCH_BYTE (same_at_end
- 1) == buffer
[bufpos
- 1])
4158 same_at_end
--, bufpos
--;
4160 /* If we found a discrepancy, stop the scan.
4161 Otherwise loop around and scan the preceding bufferful. */
4164 /* If this discrepancy is because of code conversion,
4165 we cannot use this method; giveup and try the other. */
4166 if (same_at_end
> same_at_start
4167 && FETCH_BYTE (same_at_end
- 1) >= 0200
4168 && ! NILP (current_buffer
->enable_multibyte_characters
)
4169 && (CODING_MAY_REQUIRE_DECODING (&coding
)))
4170 giveup_match_end
= 1;
4179 if (! giveup_match_end
)
4183 /* We win! We can handle REPLACE the optimized way. */
4185 /* Extend the start of non-matching text area to multibyte
4186 character boundary. */
4187 if (! NILP (current_buffer
->enable_multibyte_characters
))
4188 while (same_at_start
> BEGV_BYTE
4189 && ! CHAR_HEAD_P (FETCH_BYTE (same_at_start
)))
4192 /* Extend the end of non-matching text area to multibyte
4193 character boundary. */
4194 if (! NILP (current_buffer
->enable_multibyte_characters
))
4195 while (same_at_end
< ZV_BYTE
4196 && ! CHAR_HEAD_P (FETCH_BYTE (same_at_end
)))
4199 /* Don't try to reuse the same piece of text twice. */
4200 overlap
= (same_at_start
- BEGV_BYTE
4201 - (same_at_end
+ st
.st_size
- ZV
));
4203 same_at_end
+= overlap
;
4205 /* Arrange to read only the nonmatching middle part of the file. */
4206 XSETFASTINT (beg
, XINT (beg
) + (same_at_start
- BEGV_BYTE
));
4207 XSETFASTINT (end
, XINT (end
) - (ZV_BYTE
- same_at_end
));
4209 del_range_byte (same_at_start
, same_at_end
, 0);
4210 /* Insert from the file at the proper position. */
4211 temp
= BYTE_TO_CHAR (same_at_start
);
4212 SET_PT_BOTH (temp
, same_at_start
);
4214 /* If display currently starts at beginning of line,
4215 keep it that way. */
4216 if (XBUFFER (XWINDOW (selected_window
)->buffer
) == current_buffer
)
4217 XWINDOW (selected_window
)->start_at_line_beg
= Fbolp ();
4219 replace_handled
= 1;
4223 /* If requested, replace the accessible part of the buffer
4224 with the file contents. Avoid replacing text at the
4225 beginning or end of the buffer that matches the file contents;
4226 that preserves markers pointing to the unchanged parts.
4228 Here we implement this feature for the case where code conversion
4229 is needed, in a simple way that needs a lot of memory.
4230 The preceding if-statement handles the case of no conversion
4231 in a more optimized way. */
4232 if (!NILP (replace
) && ! replace_handled
&& BEGV
< ZV
)
4234 int same_at_start
= BEGV_BYTE
;
4235 int same_at_end
= ZV_BYTE
;
4238 /* Make sure that the gap is large enough. */
4239 int bufsize
= 2 * st
.st_size
;
4240 unsigned char *conversion_buffer
= (unsigned char *) xmalloc (bufsize
);
4243 /* First read the whole file, performing code conversion into
4244 CONVERSION_BUFFER. */
4246 if (lseek (fd
, XINT (beg
), 0) < 0)
4248 xfree (conversion_buffer
);
4249 report_file_error ("Setting file position",
4250 Fcons (orig_filename
, Qnil
));
4253 total
= st
.st_size
; /* Total bytes in the file. */
4254 how_much
= 0; /* Bytes read from file so far. */
4255 inserted
= 0; /* Bytes put into CONVERSION_BUFFER so far. */
4256 unprocessed
= 0; /* Bytes not processed in previous loop. */
4258 while (how_much
< total
)
4260 /* try is reserved in some compilers (Microsoft C) */
4261 int trytry
= min (total
- how_much
, READ_BUF_SIZE
- unprocessed
);
4262 unsigned char *destination
= read_buf
+ unprocessed
;
4265 /* Allow quitting out of the actual I/O. */
4268 this = emacs_read (fd
, destination
, trytry
);
4271 if (this < 0 || this + unprocessed
== 0)
4279 if (CODING_MAY_REQUIRE_DECODING (&coding
))
4281 int require
, result
;
4283 this += unprocessed
;
4285 /* If we are using more space than estimated,
4286 make CONVERSION_BUFFER bigger. */
4287 require
= decoding_buffer_size (&coding
, this);
4288 if (inserted
+ require
+ 2 * (total
- how_much
) > bufsize
)
4290 bufsize
= inserted
+ require
+ 2 * (total
- how_much
);
4291 conversion_buffer
= (unsigned char *) xrealloc (conversion_buffer
, bufsize
);
4294 /* Convert this batch with results in CONVERSION_BUFFER. */
4295 if (how_much
>= total
) /* This is the last block. */
4296 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
4297 if (coding
.composing
!= COMPOSITION_DISABLED
)
4298 coding_allocate_composition_data (&coding
, BEGV
);
4299 result
= decode_coding (&coding
, read_buf
,
4300 conversion_buffer
+ inserted
,
4301 this, bufsize
- inserted
);
4303 /* Save for next iteration whatever we didn't convert. */
4304 unprocessed
= this - coding
.consumed
;
4305 bcopy (read_buf
+ coding
.consumed
, read_buf
, unprocessed
);
4306 if (!NILP (current_buffer
->enable_multibyte_characters
))
4307 this = coding
.produced
;
4309 this = str_as_unibyte (conversion_buffer
+ inserted
,
4316 /* At this point, INSERTED is how many characters (i.e. bytes)
4317 are present in CONVERSION_BUFFER.
4318 HOW_MUCH should equal TOTAL,
4319 or should be <= 0 if we couldn't read the file. */
4323 xfree (conversion_buffer
);
4324 coding_free_composition_data (&coding
);
4326 error ("IO error reading %s: %s",
4327 SDATA (orig_filename
), emacs_strerror (errno
));
4328 else if (how_much
== -2)
4329 error ("maximum buffer size exceeded");
4332 /* Compare the beginning of the converted file
4333 with the buffer text. */
4336 while (bufpos
< inserted
&& same_at_start
< same_at_end
4337 && FETCH_BYTE (same_at_start
) == conversion_buffer
[bufpos
])
4338 same_at_start
++, bufpos
++;
4340 /* If the file matches the buffer completely,
4341 there's no need to replace anything. */
4343 if (bufpos
== inserted
)
4345 xfree (conversion_buffer
);
4346 coding_free_composition_data (&coding
);
4349 /* Truncate the buffer to the size of the file. */
4350 del_range_byte (same_at_start
, same_at_end
, 0);
4355 /* Extend the start of non-matching text area to multibyte
4356 character boundary. */
4357 if (! NILP (current_buffer
->enable_multibyte_characters
))
4358 while (same_at_start
> BEGV_BYTE
4359 && ! CHAR_HEAD_P (FETCH_BYTE (same_at_start
)))
4362 /* Scan this bufferful from the end, comparing with
4363 the Emacs buffer. */
4366 /* Compare with same_at_start to avoid counting some buffer text
4367 as matching both at the file's beginning and at the end. */
4368 while (bufpos
> 0 && same_at_end
> same_at_start
4369 && FETCH_BYTE (same_at_end
- 1) == conversion_buffer
[bufpos
- 1])
4370 same_at_end
--, bufpos
--;
4372 /* Extend the end of non-matching text area to multibyte
4373 character boundary. */
4374 if (! NILP (current_buffer
->enable_multibyte_characters
))
4375 while (same_at_end
< ZV_BYTE
4376 && ! CHAR_HEAD_P (FETCH_BYTE (same_at_end
)))
4379 /* Don't try to reuse the same piece of text twice. */
4380 overlap
= same_at_start
- BEGV_BYTE
- (same_at_end
+ inserted
- ZV_BYTE
);
4382 same_at_end
+= overlap
;
4384 /* If display currently starts at beginning of line,
4385 keep it that way. */
4386 if (XBUFFER (XWINDOW (selected_window
)->buffer
) == current_buffer
)
4387 XWINDOW (selected_window
)->start_at_line_beg
= Fbolp ();
4389 /* Replace the chars that we need to replace,
4390 and update INSERTED to equal the number of bytes
4391 we are taking from the file. */
4392 inserted
-= (ZV_BYTE
- same_at_end
) + (same_at_start
- BEGV_BYTE
);
4394 if (same_at_end
!= same_at_start
)
4396 del_range_byte (same_at_start
, same_at_end
, 0);
4398 same_at_start
= GPT_BYTE
;
4402 temp
= BYTE_TO_CHAR (same_at_start
);
4404 /* Insert from the file at the proper position. */
4405 SET_PT_BOTH (temp
, same_at_start
);
4406 insert_1 (conversion_buffer
+ same_at_start
- BEGV_BYTE
, inserted
,
4408 if (coding
.cmp_data
&& coding
.cmp_data
->used
)
4409 coding_restore_composition (&coding
, Fcurrent_buffer ());
4410 coding_free_composition_data (&coding
);
4412 /* Set `inserted' to the number of inserted characters. */
4413 inserted
= PT
- temp
;
4415 xfree (conversion_buffer
);
4424 register Lisp_Object temp
;
4426 total
= XINT (end
) - XINT (beg
);
4428 /* Make sure point-max won't overflow after this insertion. */
4429 XSETINT (temp
, total
);
4430 if (total
!= XINT (temp
))
4431 error ("Maximum buffer size exceeded");
4434 /* For a special file, all we can do is guess. */
4435 total
= READ_BUF_SIZE
;
4437 if (NILP (visit
) && inserted
> 0)
4439 #ifdef CLASH_DETECTION
4440 if (!NILP (current_buffer
->file_truename
)
4441 /* Make binding buffer-file-name to nil effective. */
4442 && !NILP (current_buffer
->filename
)
4443 && SAVE_MODIFF
>= MODIFF
)
4445 #endif /* CLASH_DETECTION */
4446 prepare_to_modify_buffer (GPT
, GPT
, NULL
);
4450 if (GAP_SIZE
< total
)
4451 make_gap (total
- GAP_SIZE
);
4453 if (XINT (beg
) != 0 || !NILP (replace
))
4455 if (lseek (fd
, XINT (beg
), 0) < 0)
4456 report_file_error ("Setting file position",
4457 Fcons (orig_filename
, Qnil
));
4460 /* In the following loop, HOW_MUCH contains the total bytes read so
4461 far for a regular file, and not changed for a special file. But,
4462 before exiting the loop, it is set to a negative value if I/O
4466 /* Total bytes inserted. */
4469 /* Here, we don't do code conversion in the loop. It is done by
4470 code_convert_region after all data are read into the buffer. */
4472 int gap_size
= GAP_SIZE
;
4474 while (how_much
< total
)
4476 /* try is reserved in some compilers (Microsoft C) */
4477 int trytry
= min (total
- how_much
, READ_BUF_SIZE
);
4484 /* Maybe make more room. */
4485 if (gap_size
< trytry
)
4487 make_gap (total
- gap_size
);
4488 gap_size
= GAP_SIZE
;
4491 /* Read from the file, capturing `quit'. When an
4492 error occurs, end the loop, and arrange for a quit
4493 to be signaled after decoding the text we read. */
4494 non_regular_fd
= fd
;
4495 non_regular_inserted
= inserted
;
4496 non_regular_nbytes
= trytry
;
4497 val
= internal_condition_case_1 (read_non_regular
, Qnil
, Qerror
,
4498 read_non_regular_quit
);
4509 /* Allow quitting out of the actual I/O. We don't make text
4510 part of the buffer until all the reading is done, so a C-g
4511 here doesn't do any harm. */
4514 this = emacs_read (fd
, BEG_ADDR
+ PT_BYTE
- BEG_BYTE
+ inserted
, trytry
);
4526 /* For a regular file, where TOTAL is the real size,
4527 count HOW_MUCH to compare with it.
4528 For a special file, where TOTAL is just a buffer size,
4529 so don't bother counting in HOW_MUCH.
4530 (INSERTED is where we count the number of characters inserted.) */
4537 /* Now we have read all the file data into the gap.
4538 If it was empty, undo marking the buffer modified. */
4542 #ifdef CLASH_DETECTION
4544 unlock_file (current_buffer
->file_truename
);
4546 Vdeactivate_mark
= old_Vdeactivate_mark
;
4549 /* Make the text read part of the buffer. */
4550 GAP_SIZE
-= inserted
;
4552 GPT_BYTE
+= inserted
;
4554 ZV_BYTE
+= inserted
;
4559 /* Put an anchor to ensure multi-byte form ends at gap. */
4564 /* Discard the unwind protect for closing the file. */
4568 error ("IO error reading %s: %s",
4569 SDATA (orig_filename
), emacs_strerror (errno
));
4573 if (! coding_system_decided
)
4575 /* The coding system is not yet decided. Decide it by an
4576 optimized method for handling `coding:' tag.
4578 Note that we can get here only if the buffer was empty
4579 before the insertion. */
4583 if (!NILP (Vcoding_system_for_read
))
4584 val
= Vcoding_system_for_read
;
4587 /* Since we are sure that the current buffer was empty
4588 before the insertion, we can toggle
4589 enable-multibyte-characters directly here without taking
4590 care of marker adjustment and byte combining problem. By
4591 this way, we can run Lisp program safely before decoding
4592 the inserted text. */
4593 Lisp_Object unwind_data
;
4594 int count
= SPECPDL_INDEX ();
4596 unwind_data
= Fcons (current_buffer
->enable_multibyte_characters
,
4597 Fcons (current_buffer
->undo_list
,
4598 Fcurrent_buffer ()));
4599 current_buffer
->enable_multibyte_characters
= Qnil
;
4600 current_buffer
->undo_list
= Qt
;
4601 record_unwind_protect (decide_coding_unwind
, unwind_data
);
4603 if (inserted
> 0 && ! NILP (Vset_auto_coding_function
))
4605 val
= call2 (Vset_auto_coding_function
,
4606 filename
, make_number (inserted
));
4611 /* If the coding system is not yet decided, check
4612 file-coding-system-alist. */
4613 Lisp_Object args
[6], coding_systems
;
4615 args
[0] = Qinsert_file_contents
, args
[1] = orig_filename
;
4616 args
[2] = visit
, args
[3] = beg
, args
[4] = end
, args
[5] = Qnil
;
4617 coding_systems
= Ffind_operation_coding_system (6, args
);
4618 if (CONSP (coding_systems
))
4619 val
= XCAR (coding_systems
);
4621 unbind_to (count
, Qnil
);
4622 inserted
= Z_BYTE
- BEG_BYTE
;
4625 /* The following kludgy code is to avoid some compiler bug.
4627 setup_coding_system (val, &coding);
4630 struct coding_system temp_coding
;
4631 setup_coding_system (Fcheck_coding_system (val
), &temp_coding
);
4632 bcopy (&temp_coding
, &coding
, sizeof coding
);
4634 /* Ensure we set Vlast_coding_system_used. */
4635 set_coding_system
= 1;
4637 if (NILP (current_buffer
->enable_multibyte_characters
)
4639 /* We must suppress all character code conversion except for
4640 end-of-line conversion. */
4641 setup_raw_text_coding_system (&coding
);
4642 coding
.src_multibyte
= 0;
4643 coding
.dst_multibyte
4644 = !NILP (current_buffer
->enable_multibyte_characters
);
4648 /* Can't do this if part of the buffer might be preserved. */
4650 && (coding
.type
== coding_type_no_conversion
4651 || coding
.type
== coding_type_raw_text
))
4653 /* Visiting a file with these coding system makes the buffer
4655 current_buffer
->enable_multibyte_characters
= Qnil
;
4656 coding
.dst_multibyte
= 0;
4659 if (inserted
> 0 || coding
.type
== coding_type_ccl
)
4661 if (CODING_MAY_REQUIRE_DECODING (&coding
))
4663 code_convert_region (PT
, PT_BYTE
, PT
+ inserted
, PT_BYTE
+ inserted
,
4665 inserted
= coding
.produced_char
;
4668 adjust_after_insert (PT
, PT_BYTE
, PT
+ inserted
, PT_BYTE
+ inserted
,
4672 /* Now INSERTED is measured in characters. */
4675 /* Use the conversion type to determine buffer-file-type
4676 (find-buffer-file-type is now used to help determine the
4678 if ((coding
.eol_type
== CODING_EOL_UNDECIDED
4679 || coding
.eol_type
== CODING_EOL_LF
)
4680 && ! CODING_REQUIRE_DECODING (&coding
))
4681 current_buffer
->buffer_file_type
= Qt
;
4683 current_buffer
->buffer_file_type
= Qnil
;
4690 if (!EQ (current_buffer
->undo_list
, Qt
))
4691 current_buffer
->undo_list
= Qnil
;
4693 stat (SDATA (filename
), &st
);
4698 current_buffer
->modtime
= st
.st_mtime
;
4699 current_buffer
->filename
= orig_filename
;
4702 SAVE_MODIFF
= MODIFF
;
4703 current_buffer
->auto_save_modified
= MODIFF
;
4704 XSETFASTINT (current_buffer
->save_length
, Z
- BEG
);
4705 #ifdef CLASH_DETECTION
4708 if (!NILP (current_buffer
->file_truename
))
4709 unlock_file (current_buffer
->file_truename
);
4710 unlock_file (filename
);
4712 #endif /* CLASH_DETECTION */
4714 Fsignal (Qfile_error
,
4715 Fcons (build_string ("not a regular file"),
4716 Fcons (orig_filename
, Qnil
)));
4719 if (set_coding_system
)
4720 Vlast_coding_system_used
= coding
.symbol
;
4722 if (! NILP (Ffboundp (Qafter_insert_file_set_coding
)))
4724 insval
= call2 (Qafter_insert_file_set_coding
, make_number (inserted
),
4726 if (! NILP (insval
))
4728 CHECK_NUMBER (insval
);
4729 inserted
= XFASTINT (insval
);
4733 /* Decode file format */
4736 int empty_undo_list_p
= 0;
4738 /* If we're anyway going to discard undo information, don't
4739 record it in the first place. The buffer's undo list at this
4740 point is either nil or t when visiting a file. */
4743 empty_undo_list_p
= NILP (current_buffer
->undo_list
);
4744 current_buffer
->undo_list
= Qt
;
4747 insval
= call3 (Qformat_decode
,
4748 Qnil
, make_number (inserted
), visit
);
4749 CHECK_NUMBER (insval
);
4750 inserted
= XFASTINT (insval
);
4753 current_buffer
->undo_list
= empty_undo_list_p
? Qnil
: Qt
;
4756 /* Call after-change hooks for the inserted text, aside from the case
4757 of normal visiting (not with REPLACE), which is done in a new buffer
4758 "before" the buffer is changed. */
4759 if (inserted
> 0 && total
> 0
4760 && (NILP (visit
) || !NILP (replace
)))
4762 signal_after_change (PT
, 0, inserted
);
4763 update_compositions (PT
, PT
, CHECK_BORDER
);
4766 p
= Vafter_insert_file_functions
;
4769 insval
= call1 (XCAR (p
), make_number (inserted
));
4772 CHECK_NUMBER (insval
);
4773 inserted
= XFASTINT (insval
);
4780 && current_buffer
->modtime
== -1)
4782 /* If visiting nonexistent file, return nil. */
4783 report_file_error ("Opening input file", Fcons (orig_filename
, Qnil
));
4787 Fsignal (Qquit
, Qnil
);
4789 /* ??? Retval needs to be dealt with in all cases consistently. */
4791 val
= Fcons (orig_filename
,
4792 Fcons (make_number (inserted
),
4795 RETURN_UNGCPRO (unbind_to (count
, val
));
4798 static Lisp_Object build_annotations
P_ ((Lisp_Object
, Lisp_Object
));
4799 static Lisp_Object build_annotations_2
P_ ((Lisp_Object
, Lisp_Object
,
4800 Lisp_Object
, Lisp_Object
));
4802 /* If build_annotations switched buffers, switch back to BUF.
4803 Kill the temporary buffer that was selected in the meantime.
4805 Since this kill only the last temporary buffer, some buffers remain
4806 not killed if build_annotations switched buffers more than once.
4810 build_annotations_unwind (buf
)
4815 if (XBUFFER (buf
) == current_buffer
)
4817 tembuf
= Fcurrent_buffer ();
4819 Fkill_buffer (tembuf
);
4823 /* Decide the coding-system to encode the data with. */
4826 choose_write_coding_system (start
, end
, filename
,
4827 append
, visit
, lockname
, coding
)
4828 Lisp_Object start
, end
, filename
, append
, visit
, lockname
;
4829 struct coding_system
*coding
;
4834 && NILP (Fstring_equal (current_buffer
->filename
,
4835 current_buffer
->auto_save_file_name
)))
4837 /* We use emacs-mule for auto saving... */
4838 setup_coding_system (Qemacs_mule
, coding
);
4839 /* ... but with the special flag to indicate not to strip off
4840 leading code of eight-bit-control chars. */
4842 goto done_setup_coding
;
4844 else if (!NILP (Vcoding_system_for_write
))
4846 val
= Vcoding_system_for_write
;
4847 if (coding_system_require_warning
4848 && !NILP (Ffboundp (Vselect_safe_coding_system_function
)))
4849 /* Confirm that VAL can surely encode the current region. */
4850 val
= call5 (Vselect_safe_coding_system_function
,
4851 start
, end
, Fcons (Qt
, Fcons (val
, Qnil
)),
4856 /* If the variable `buffer-file-coding-system' is set locally,
4857 it means that the file was read with some kind of code
4858 conversion or the variable is explicitly set by users. We
4859 had better write it out with the same coding system even if
4860 `enable-multibyte-characters' is nil.
4862 If it is not set locally, we anyway have to convert EOL
4863 format if the default value of `buffer-file-coding-system'
4864 tells that it is not Unix-like (LF only) format. */
4865 int using_default_coding
= 0;
4866 int force_raw_text
= 0;
4868 val
= current_buffer
->buffer_file_coding_system
;
4870 || NILP (Flocal_variable_p (Qbuffer_file_coding_system
, Qnil
)))
4873 if (NILP (current_buffer
->enable_multibyte_characters
))
4879 /* Check file-coding-system-alist. */
4880 Lisp_Object args
[7], coding_systems
;
4882 args
[0] = Qwrite_region
; args
[1] = start
; args
[2] = end
;
4883 args
[3] = filename
; args
[4] = append
; args
[5] = visit
;
4885 coding_systems
= Ffind_operation_coding_system (7, args
);
4886 if (CONSP (coding_systems
) && !NILP (XCDR (coding_systems
)))
4887 val
= XCDR (coding_systems
);
4891 && !NILP (current_buffer
->buffer_file_coding_system
))
4893 /* If we still have not decided a coding system, use the
4894 default value of buffer-file-coding-system. */
4895 val
= current_buffer
->buffer_file_coding_system
;
4896 using_default_coding
= 1;
4900 && !NILP (Ffboundp (Vselect_safe_coding_system_function
)))
4901 /* Confirm that VAL can surely encode the current region. */
4902 val
= call5 (Vselect_safe_coding_system_function
,
4903 start
, end
, val
, Qnil
, filename
);
4905 setup_coding_system (Fcheck_coding_system (val
), coding
);
4906 if (coding
->eol_type
== CODING_EOL_UNDECIDED
4907 && !using_default_coding
)
4909 if (! EQ (default_buffer_file_coding
.symbol
,
4910 buffer_defaults
.buffer_file_coding_system
))
4911 setup_coding_system (buffer_defaults
.buffer_file_coding_system
,
4912 &default_buffer_file_coding
);
4913 if (default_buffer_file_coding
.eol_type
!= CODING_EOL_UNDECIDED
)
4915 Lisp_Object subsidiaries
;
4917 coding
->eol_type
= default_buffer_file_coding
.eol_type
;
4918 subsidiaries
= Fget (coding
->symbol
, Qeol_type
);
4919 if (VECTORP (subsidiaries
)
4920 && XVECTOR (subsidiaries
)->size
== 3)
4922 = XVECTOR (subsidiaries
)->contents
[coding
->eol_type
];
4927 setup_raw_text_coding_system (coding
);
4928 goto done_setup_coding
;
4931 setup_coding_system (Fcheck_coding_system (val
), coding
);
4934 if (!STRINGP (start
) && !NILP (current_buffer
->selective_display
))
4935 coding
->mode
|= CODING_MODE_SELECTIVE_DISPLAY
;
4938 DEFUN ("write-region", Fwrite_region
, Swrite_region
, 3, 7,
4939 "r\nFWrite region to file: \ni\ni\ni\np",
4940 doc
: /* Write current region into specified file.
4941 When called from a program, requires three arguments:
4942 START, END and FILENAME. START and END are normally buffer positions
4943 specifying the part of the buffer to write.
4944 If START is nil, that means to use the entire buffer contents.
4945 If START is a string, then output that string to the file
4946 instead of any buffer contents; END is ignored.
4948 Optional fourth argument APPEND if non-nil means
4949 append to existing file contents (if any). If it is an integer,
4950 seek to that offset in the file before writing.
4951 Optional fifth argument VISIT, if t or a string, means
4952 set the last-save-file-modtime of buffer to this file's modtime
4953 and mark buffer not modified.
4954 If VISIT is a string, it is a second file name;
4955 the output goes to FILENAME, but the buffer is marked as visiting VISIT.
4956 VISIT is also the file name to lock and unlock for clash detection.
4957 If VISIT is neither t nor nil nor a string,
4958 that means do not display the \"Wrote file\" message.
4959 The optional sixth arg LOCKNAME, if non-nil, specifies the name to
4960 use for locking and unlocking, overriding FILENAME and VISIT.
4961 The optional seventh arg MUSTBENEW, if non-nil, insists on a check
4962 for an existing file with the same name. If MUSTBENEW is `excl',
4963 that means to get an error if the file already exists; never overwrite.
4964 If MUSTBENEW is neither nil nor `excl', that means ask for
4965 confirmation before overwriting, but do go ahead and overwrite the file
4966 if the user confirms.
4968 This does code conversion according to the value of
4969 `coding-system-for-write', `buffer-file-coding-system', or
4970 `file-coding-system-alist', and sets the variable
4971 `last-coding-system-used' to the coding system actually used. */)
4972 (start
, end
, filename
, append
, visit
, lockname
, mustbenew
)
4973 Lisp_Object start
, end
, filename
, append
, visit
, lockname
, mustbenew
;
4978 const unsigned char *fn
;
4981 int count
= SPECPDL_INDEX ();
4984 unsigned char *fname
= 0; /* If non-0, original filename (must rename) */
4986 Lisp_Object handler
;
4987 Lisp_Object visit_file
;
4988 Lisp_Object annotations
;
4989 Lisp_Object encoded_filename
;
4990 int visiting
= (EQ (visit
, Qt
) || STRINGP (visit
));
4991 int quietly
= !NILP (visit
);
4992 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
4993 struct buffer
*given_buffer
;
4995 int buffer_file_type
= O_BINARY
;
4997 struct coding_system coding
;
4999 if (current_buffer
->base_buffer
&& visiting
)
5000 error ("Cannot do file visiting in an indirect buffer");
5002 if (!NILP (start
) && !STRINGP (start
))
5003 validate_region (&start
, &end
);
5005 GCPRO5 (start
, filename
, visit
, visit_file
, lockname
);
5007 filename
= Fexpand_file_name (filename
, Qnil
);
5009 if (!NILP (mustbenew
) && !EQ (mustbenew
, Qexcl
))
5010 barf_or_query_if_file_exists (filename
, "overwrite", 1, 0, 1);
5012 if (STRINGP (visit
))
5013 visit_file
= Fexpand_file_name (visit
, Qnil
);
5015 visit_file
= filename
;
5017 if (NILP (lockname
))
5018 lockname
= visit_file
;
5022 /* If the file name has special constructs in it,
5023 call the corresponding file handler. */
5024 handler
= Ffind_file_name_handler (filename
, Qwrite_region
);
5025 /* If FILENAME has no handler, see if VISIT has one. */
5026 if (NILP (handler
) && STRINGP (visit
))
5027 handler
= Ffind_file_name_handler (visit
, Qwrite_region
);
5029 if (!NILP (handler
))
5032 val
= call6 (handler
, Qwrite_region
, start
, end
,
5033 filename
, append
, visit
);
5037 SAVE_MODIFF
= MODIFF
;
5038 XSETFASTINT (current_buffer
->save_length
, Z
- BEG
);
5039 current_buffer
->filename
= visit_file
;
5045 record_unwind_protect (save_restriction_restore
, save_restriction_save ());
5047 /* Special kludge to simplify auto-saving. */
5050 XSETFASTINT (start
, BEG
);
5051 XSETFASTINT (end
, Z
);
5055 record_unwind_protect (build_annotations_unwind
, Fcurrent_buffer ());
5056 count1
= SPECPDL_INDEX ();
5058 given_buffer
= current_buffer
;
5060 if (!STRINGP (start
))
5062 annotations
= build_annotations (start
, end
);
5064 if (current_buffer
!= given_buffer
)
5066 XSETFASTINT (start
, BEGV
);
5067 XSETFASTINT (end
, ZV
);
5073 GCPRO5 (start
, filename
, annotations
, visit_file
, lockname
);
5075 /* Decide the coding-system to encode the data with.
5076 We used to make this choice before calling build_annotations, but that
5077 leads to problems when a write-annotate-function takes care of
5078 unsavable chars (as was the case with X-Symbol). */
5079 choose_write_coding_system (start
, end
, filename
,
5080 append
, visit
, lockname
, &coding
);
5081 Vlast_coding_system_used
= coding
.symbol
;
5083 given_buffer
= current_buffer
;
5084 if (! STRINGP (start
))
5086 annotations
= build_annotations_2 (start
, end
,
5087 coding
.pre_write_conversion
, annotations
);
5088 if (current_buffer
!= given_buffer
)
5090 XSETFASTINT (start
, BEGV
);
5091 XSETFASTINT (end
, ZV
);
5095 #ifdef CLASH_DETECTION
5098 #if 0 /* This causes trouble for GNUS. */
5099 /* If we've locked this file for some other buffer,
5100 query before proceeding. */
5101 if (!visiting
&& EQ (Ffile_locked_p (lockname
), Qt
))
5102 call2 (intern ("ask-user-about-lock"), filename
, Vuser_login_name
);
5105 lock_file (lockname
);
5107 #endif /* CLASH_DETECTION */
5109 encoded_filename
= ENCODE_FILE (filename
);
5111 fn
= SDATA (encoded_filename
);
5115 desc
= emacs_open (fn
, O_WRONLY
| buffer_file_type
, 0);
5116 #else /* not DOS_NT */
5117 desc
= emacs_open (fn
, O_WRONLY
, 0);
5118 #endif /* not DOS_NT */
5120 if (desc
< 0 && (NILP (append
) || errno
== ENOENT
))
5122 if (auto_saving
) /* Overwrite any previous version of autosave file */
5124 vms_truncate (fn
); /* if fn exists, truncate to zero length */
5125 desc
= emacs_open (fn
, O_RDWR
, 0);
5127 desc
= creat_copy_attrs (STRINGP (current_buffer
->filename
)
5128 ? SDATA (current_buffer
->filename
) : 0,
5131 else /* Write to temporary name and rename if no errors */
5133 Lisp_Object temp_name
;
5134 temp_name
= Ffile_name_directory (filename
);
5136 if (!NILP (temp_name
))
5138 temp_name
= Fmake_temp_name (concat2 (temp_name
,
5139 build_string ("$$SAVE$$")));
5140 fname
= SDATA (filename
);
5141 fn
= SDATA (temp_name
);
5142 desc
= creat_copy_attrs (fname
, fn
);
5145 /* If we can't open the temporary file, try creating a new
5146 version of the original file. VMS "creat" creates a
5147 new version rather than truncating an existing file. */
5150 desc
= creat (fn
, 0666);
5151 #if 0 /* This can clobber an existing file and fail to replace it,
5152 if the user runs out of space. */
5155 /* We can't make a new version;
5156 try to truncate and rewrite existing version if any. */
5158 desc
= emacs_open (fn
, O_RDWR
, 0);
5164 desc
= creat (fn
, 0666);
5168 desc
= emacs_open (fn
,
5169 O_WRONLY
| O_CREAT
| buffer_file_type
5170 | (EQ (mustbenew
, Qexcl
) ? O_EXCL
: O_TRUNC
),
5171 S_IREAD
| S_IWRITE
);
5172 #else /* not DOS_NT */
5173 desc
= emacs_open (fn
, O_WRONLY
| O_TRUNC
| O_CREAT
5174 | (EQ (mustbenew
, Qexcl
) ? O_EXCL
: 0),
5175 auto_saving
? auto_save_mode_bits
: 0666);
5176 #endif /* not DOS_NT */
5177 #endif /* not VMS */
5181 #ifdef CLASH_DETECTION
5183 if (!auto_saving
) unlock_file (lockname
);
5185 #endif /* CLASH_DETECTION */
5187 report_file_error ("Opening output file", Fcons (filename
, Qnil
));
5190 record_unwind_protect (close_file_unwind
, make_number (desc
));
5192 if (!NILP (append
) && !NILP (Ffile_regular_p (filename
)))
5196 if (NUMBERP (append
))
5197 ret
= lseek (desc
, XINT (append
), 1);
5199 ret
= lseek (desc
, 0, 2);
5202 #ifdef CLASH_DETECTION
5203 if (!auto_saving
) unlock_file (lockname
);
5204 #endif /* CLASH_DETECTION */
5206 report_file_error ("Lseek error", Fcons (filename
, Qnil
));
5214 * Kludge Warning: The VMS C RTL likes to insert carriage returns
5215 * if we do writes that don't end with a carriage return. Furthermore
5216 * it cannot handle writes of more then 16K. The modified
5217 * version of "sys_write" in SYSDEP.C (see comment there) copes with
5218 * this EXCEPT for the last record (iff it doesn't end with a carriage
5219 * return). This implies that if your buffer doesn't end with a carriage
5220 * return, you get one free... tough. However it also means that if
5221 * we make two calls to sys_write (a la the following code) you can
5222 * get one at the gap as well. The easiest way to fix this (honest)
5223 * is to move the gap to the next newline (or the end of the buffer).
5228 if (GPT
> BEG
&& GPT_ADDR
[-1] != '\n')
5229 move_gap (find_next_newline (GPT
, 1));
5231 /* Whether VMS or not, we must move the gap to the next of newline
5232 when we must put designation sequences at beginning of line. */
5233 if (INTEGERP (start
)
5234 && coding
.type
== coding_type_iso2022
5235 && coding
.flags
& CODING_FLAG_ISO_DESIGNATE_AT_BOL
5236 && GPT
> BEG
&& GPT_ADDR
[-1] != '\n')
5238 int opoint
= PT
, opoint_byte
= PT_BYTE
;
5239 scan_newline (PT
, PT_BYTE
, ZV
, ZV_BYTE
, 1, 0);
5240 move_gap_both (PT
, PT_BYTE
);
5241 SET_PT_BOTH (opoint
, opoint_byte
);
5248 if (STRINGP (start
))
5250 failure
= 0 > a_write (desc
, start
, 0, SCHARS (start
),
5251 &annotations
, &coding
);
5254 else if (XINT (start
) != XINT (end
))
5256 tem
= CHAR_TO_BYTE (XINT (start
));
5258 if (XINT (start
) < GPT
)
5260 failure
= 0 > a_write (desc
, Qnil
, XINT (start
),
5261 min (GPT
, XINT (end
)) - XINT (start
),
5262 &annotations
, &coding
);
5266 if (XINT (end
) > GPT
&& !failure
)
5268 tem
= max (XINT (start
), GPT
);
5269 failure
= 0 > a_write (desc
, Qnil
, tem
, XINT (end
) - tem
,
5270 &annotations
, &coding
);
5276 /* If file was empty, still need to write the annotations */
5277 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
5278 failure
= 0 > a_write (desc
, Qnil
, XINT (end
), 0, &annotations
, &coding
);
5282 if (CODING_REQUIRE_FLUSHING (&coding
)
5283 && !(coding
.mode
& CODING_MODE_LAST_BLOCK
)
5286 /* We have to flush out a data. */
5287 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
5288 failure
= 0 > e_write (desc
, Qnil
, 0, 0, &coding
);
5295 /* Note fsync appears to change the modtime on BSD4.2 (both vax and sun).
5296 Disk full in NFS may be reported here. */
5297 /* mib says that closing the file will try to write as fast as NFS can do
5298 it, and that means the fsync here is not crucial for autosave files. */
5299 if (!auto_saving
&& fsync (desc
) < 0)
5301 /* If fsync fails with EINTR, don't treat that as serious. */
5303 failure
= 1, save_errno
= errno
;
5307 /* Spurious "file has changed on disk" warnings have been
5308 observed on Suns as well.
5309 It seems that `close' can change the modtime, under nfs.
5311 (This has supposedly been fixed in Sunos 4,
5312 but who knows about all the other machines with NFS?) */
5315 /* On VMS and APOLLO, must do the stat after the close
5316 since closing changes the modtime. */
5319 /* Recall that #if defined does not work on VMS. */
5326 /* NFS can report a write failure now. */
5327 if (emacs_close (desc
) < 0)
5328 failure
= 1, save_errno
= errno
;
5331 /* If we wrote to a temporary name and had no errors, rename to real name. */
5335 failure
= (rename (fn
, fname
) != 0), save_errno
= errno
;
5343 /* Discard the unwind protect for close_file_unwind. */
5344 specpdl_ptr
= specpdl
+ count1
;
5345 /* Restore the original current buffer. */
5346 visit_file
= unbind_to (count
, visit_file
);
5348 #ifdef CLASH_DETECTION
5350 unlock_file (lockname
);
5351 #endif /* CLASH_DETECTION */
5353 /* Do this before reporting IO error
5354 to avoid a "file has changed on disk" warning on
5355 next attempt to save. */
5357 current_buffer
->modtime
= st
.st_mtime
;
5360 error ("IO error writing %s: %s", SDATA (filename
),
5361 emacs_strerror (save_errno
));
5365 SAVE_MODIFF
= MODIFF
;
5366 XSETFASTINT (current_buffer
->save_length
, Z
- BEG
);
5367 current_buffer
->filename
= visit_file
;
5368 update_mode_lines
++;
5373 && ! NILP (Fstring_equal (current_buffer
->filename
,
5374 current_buffer
->auto_save_file_name
)))
5375 SAVE_MODIFF
= MODIFF
;
5381 message_with_string ((INTEGERP (append
)
5391 Lisp_Object
merge ();
5393 DEFUN ("car-less-than-car", Fcar_less_than_car
, Scar_less_than_car
, 2, 2, 0,
5394 doc
: /* Return t if (car A) is numerically less than (car B). */)
5398 return Flss (Fcar (a
), Fcar (b
));
5401 /* Build the complete list of annotations appropriate for writing out
5402 the text between START and END, by calling all the functions in
5403 write-region-annotate-functions and merging the lists they return.
5404 If one of these functions switches to a different buffer, we assume
5405 that buffer contains altered text. Therefore, the caller must
5406 make sure to restore the current buffer in all cases,
5407 as save-excursion would do. */
5410 build_annotations (start
, end
)
5411 Lisp_Object start
, end
;
5413 Lisp_Object annotations
;
5415 struct gcpro gcpro1
, gcpro2
;
5416 Lisp_Object original_buffer
;
5417 int i
, used_global
= 0;
5419 XSETBUFFER (original_buffer
, current_buffer
);
5422 p
= Vwrite_region_annotate_functions
;
5423 GCPRO2 (annotations
, p
);
5426 struct buffer
*given_buffer
= current_buffer
;
5427 if (EQ (Qt
, XCAR (p
)) && !used_global
)
5428 { /* Use the global value of the hook. */
5431 arg
[0] = Fdefault_value (Qwrite_region_annotate_functions
);
5433 p
= Fappend (2, arg
);
5436 Vwrite_region_annotations_so_far
= annotations
;
5437 res
= call2 (XCAR (p
), start
, end
);
5438 /* If the function makes a different buffer current,
5439 assume that means this buffer contains altered text to be output.
5440 Reset START and END from the buffer bounds
5441 and discard all previous annotations because they should have
5442 been dealt with by this function. */
5443 if (current_buffer
!= given_buffer
)
5445 XSETFASTINT (start
, BEGV
);
5446 XSETFASTINT (end
, ZV
);
5449 Flength (res
); /* Check basic validity of return value */
5450 annotations
= merge (annotations
, res
, Qcar_less_than_car
);
5454 /* Now do the same for annotation functions implied by the file-format */
5455 if (auto_saving
&& (!EQ (current_buffer
->auto_save_file_format
, Qt
)))
5456 p
= current_buffer
->auto_save_file_format
;
5458 p
= current_buffer
->file_format
;
5459 for (i
= 0; CONSP (p
); p
= XCDR (p
), ++i
)
5461 struct buffer
*given_buffer
= current_buffer
;
5463 Vwrite_region_annotations_so_far
= annotations
;
5465 /* Value is either a list of annotations or nil if the function
5466 has written annotations to a temporary buffer, which is now
5468 res
= call5 (Qformat_annotate_function
, XCAR (p
), start
, end
,
5469 original_buffer
, make_number (i
));
5470 if (current_buffer
!= given_buffer
)
5472 XSETFASTINT (start
, BEGV
);
5473 XSETFASTINT (end
, ZV
);
5478 annotations
= merge (annotations
, res
, Qcar_less_than_car
);
5486 build_annotations_2 (start
, end
, pre_write_conversion
, annotations
)
5487 Lisp_Object start
, end
, pre_write_conversion
, annotations
;
5489 struct gcpro gcpro1
;
5492 GCPRO1 (annotations
);
5493 /* At last, do the same for the function PRE_WRITE_CONVERSION
5494 implied by the current coding-system. */
5495 if (!NILP (pre_write_conversion
))
5497 struct buffer
*given_buffer
= current_buffer
;
5498 Vwrite_region_annotations_so_far
= annotations
;
5499 res
= call2 (pre_write_conversion
, start
, end
);
5501 annotations
= (current_buffer
!= given_buffer
5503 : merge (annotations
, res
, Qcar_less_than_car
));
5510 /* Write to descriptor DESC the NCHARS chars starting at POS of STRING.
5511 If STRING is nil, POS is the character position in the current buffer.
5512 Intersperse with them the annotations from *ANNOT
5513 which fall within the range of POS to POS + NCHARS,
5514 each at its appropriate position.
5516 We modify *ANNOT by discarding elements as we use them up.
5518 The return value is negative in case of system call failure. */
5521 a_write (desc
, string
, pos
, nchars
, annot
, coding
)
5524 register int nchars
;
5527 struct coding_system
*coding
;
5531 int lastpos
= pos
+ nchars
;
5533 while (NILP (*annot
) || CONSP (*annot
))
5535 tem
= Fcar_safe (Fcar (*annot
));
5538 nextpos
= XFASTINT (tem
);
5540 /* If there are no more annotations in this range,
5541 output the rest of the range all at once. */
5542 if (! (nextpos
>= pos
&& nextpos
<= lastpos
))
5543 return e_write (desc
, string
, pos
, lastpos
, coding
);
5545 /* Output buffer text up to the next annotation's position. */
5548 if (0 > e_write (desc
, string
, pos
, nextpos
, coding
))
5552 /* Output the annotation. */
5553 tem
= Fcdr (Fcar (*annot
));
5556 if (0 > e_write (desc
, tem
, 0, SCHARS (tem
), coding
))
5559 *annot
= Fcdr (*annot
);
5564 #ifndef WRITE_BUF_SIZE
5565 #define WRITE_BUF_SIZE (16 * 1024)
5568 /* Write text in the range START and END into descriptor DESC,
5569 encoding them with coding system CODING. If STRING is nil, START
5570 and END are character positions of the current buffer, else they
5571 are indexes to the string STRING. */
5574 e_write (desc
, string
, start
, end
, coding
)
5578 struct coding_system
*coding
;
5580 register char *addr
;
5581 register int nbytes
;
5582 char buf
[WRITE_BUF_SIZE
];
5586 coding
->composing
= COMPOSITION_DISABLED
;
5587 if (coding
->composing
!= COMPOSITION_DISABLED
)
5588 coding_save_composition (coding
, start
, end
, string
);
5590 if (STRINGP (string
))
5592 addr
= SDATA (string
);
5593 nbytes
= SBYTES (string
);
5594 coding
->src_multibyte
= STRING_MULTIBYTE (string
);
5596 else if (start
< end
)
5598 /* It is assured that the gap is not in the range START and END-1. */
5599 addr
= CHAR_POS_ADDR (start
);
5600 nbytes
= CHAR_TO_BYTE (end
) - CHAR_TO_BYTE (start
);
5601 coding
->src_multibyte
5602 = !NILP (current_buffer
->enable_multibyte_characters
);
5608 coding
->src_multibyte
= 1;
5611 /* We used to have a code for handling selective display here. But,
5612 now it is handled within encode_coding. */
5617 result
= encode_coding (coding
, addr
, buf
, nbytes
, WRITE_BUF_SIZE
);
5618 if (coding
->produced
> 0)
5620 coding
->produced
-= emacs_write (desc
, buf
, coding
->produced
);
5621 if (coding
->produced
)
5627 nbytes
-= coding
->consumed
;
5628 addr
+= coding
->consumed
;
5629 if (result
== CODING_FINISH_INSUFFICIENT_SRC
5632 /* The source text ends by an incomplete multibyte form.
5633 There's no way other than write it out as is. */
5634 nbytes
-= emacs_write (desc
, addr
, nbytes
);
5643 start
+= coding
->consumed_char
;
5644 if (coding
->cmp_data
)
5645 coding_adjust_composition_offset (coding
, start
);
5648 if (coding
->cmp_data
)
5649 coding_free_composition_data (coding
);
5654 DEFUN ("verify-visited-file-modtime", Fverify_visited_file_modtime
,
5655 Sverify_visited_file_modtime
, 1, 1, 0,
5656 doc
: /* Return t if last mod time of BUF's visited file matches what BUF records.
5657 This means that the file has not been changed since it was visited or saved.
5658 See Info node `(elisp)Modification Time' for more details. */)
5664 Lisp_Object handler
;
5665 Lisp_Object filename
;
5670 if (!STRINGP (b
->filename
)) return Qt
;
5671 if (b
->modtime
== 0) return Qt
;
5673 /* If the file name has special constructs in it,
5674 call the corresponding file handler. */
5675 handler
= Ffind_file_name_handler (b
->filename
,
5676 Qverify_visited_file_modtime
);
5677 if (!NILP (handler
))
5678 return call2 (handler
, Qverify_visited_file_modtime
, buf
);
5680 filename
= ENCODE_FILE (b
->filename
);
5682 if (stat (SDATA (filename
), &st
) < 0)
5684 /* If the file doesn't exist now and didn't exist before,
5685 we say that it isn't modified, provided the error is a tame one. */
5686 if (errno
== ENOENT
|| errno
== EACCES
|| errno
== ENOTDIR
)
5691 if (st
.st_mtime
== b
->modtime
5692 /* If both are positive, accept them if they are off by one second. */
5693 || (st
.st_mtime
> 0 && b
->modtime
> 0
5694 && (st
.st_mtime
== b
->modtime
+ 1
5695 || st
.st_mtime
== b
->modtime
- 1)))
5700 DEFUN ("clear-visited-file-modtime", Fclear_visited_file_modtime
,
5701 Sclear_visited_file_modtime
, 0, 0, 0,
5702 doc
: /* Clear out records of last mod time of visited file.
5703 Next attempt to save will certainly not complain of a discrepancy. */)
5706 current_buffer
->modtime
= 0;
5710 DEFUN ("visited-file-modtime", Fvisited_file_modtime
,
5711 Svisited_file_modtime
, 0, 0, 0,
5712 doc
: /* Return the current buffer's recorded visited file modification time.
5713 The value is a list of the form (HIGH LOW), like the time values
5714 that `file-attributes' returns. If the current buffer has no recorded
5715 file modification time, this function returns 0.
5716 See Info node `(elisp)Modification Time' for more details. */)
5720 tcons
= long_to_cons ((unsigned long) current_buffer
->modtime
);
5722 return list2 (XCAR (tcons
), XCDR (tcons
));
5726 DEFUN ("set-visited-file-modtime", Fset_visited_file_modtime
,
5727 Sset_visited_file_modtime
, 0, 1, 0,
5728 doc
: /* Update buffer's recorded modification time from the visited file's time.
5729 Useful if the buffer was not read from the file normally
5730 or if the file itself has been changed for some known benign reason.
5731 An argument specifies the modification time value to use
5732 \(instead of that of the visited file), in the form of a list
5733 \(HIGH . LOW) or (HIGH LOW). */)
5735 Lisp_Object time_list
;
5737 if (!NILP (time_list
))
5738 current_buffer
->modtime
= cons_to_long (time_list
);
5741 register Lisp_Object filename
;
5743 Lisp_Object handler
;
5745 filename
= Fexpand_file_name (current_buffer
->filename
, Qnil
);
5747 /* If the file name has special constructs in it,
5748 call the corresponding file handler. */
5749 handler
= Ffind_file_name_handler (filename
, Qset_visited_file_modtime
);
5750 if (!NILP (handler
))
5751 /* The handler can find the file name the same way we did. */
5752 return call2 (handler
, Qset_visited_file_modtime
, Qnil
);
5754 filename
= ENCODE_FILE (filename
);
5756 if (stat (SDATA (filename
), &st
) >= 0)
5757 current_buffer
->modtime
= st
.st_mtime
;
5764 auto_save_error (error
)
5767 Lisp_Object args
[3], msg
;
5769 struct gcpro gcpro1
;
5775 args
[0] = build_string ("Auto-saving %s: %s");
5776 args
[1] = current_buffer
->name
;
5777 args
[2] = Ferror_message_string (error
);
5778 msg
= Fformat (3, args
);
5780 nbytes
= SBYTES (msg
);
5781 SAFE_ALLOCA (msgbuf
, char *, nbytes
);
5782 bcopy (SDATA (msg
), msgbuf
, nbytes
);
5784 for (i
= 0; i
< 3; ++i
)
5787 message2 (msgbuf
, nbytes
, STRING_MULTIBYTE (msg
));
5789 message2_nolog (msgbuf
, nbytes
, STRING_MULTIBYTE (msg
));
5790 Fsleep_for (make_number (1), Qnil
);
5803 auto_save_mode_bits
= 0666;
5805 /* Get visited file's mode to become the auto save file's mode. */
5806 if (! NILP (current_buffer
->filename
))
5808 if (stat (SDATA (current_buffer
->filename
), &st
) >= 0)
5809 /* But make sure we can overwrite it later! */
5810 auto_save_mode_bits
= st
.st_mode
| 0600;
5811 else if ((modes
= Ffile_modes (current_buffer
->filename
),
5813 /* Remote files don't cooperate with stat. */
5814 auto_save_mode_bits
= XINT (modes
) | 0600;
5818 Fwrite_region (Qnil
, Qnil
,
5819 current_buffer
->auto_save_file_name
,
5820 Qnil
, Qlambda
, Qnil
, Qnil
);
5824 do_auto_save_unwind (arg
) /* used as unwind-protect function */
5827 FILE *stream
= (FILE *) XSAVE_VALUE (arg
)->pointer
;
5835 do_auto_save_unwind_1 (value
) /* used as unwind-protect function */
5838 minibuffer_auto_raise
= XINT (value
);
5843 do_auto_save_make_dir (dir
)
5846 return call2 (Qmake_directory
, dir
, Qt
);
5850 do_auto_save_eh (ignore
)
5856 DEFUN ("do-auto-save", Fdo_auto_save
, Sdo_auto_save
, 0, 2, "",
5857 doc
: /* Auto-save all buffers that need it.
5858 This is all buffers that have auto-saving enabled
5859 and are changed since last auto-saved.
5860 Auto-saving writes the buffer into a file
5861 so that your editing is not lost if the system crashes.
5862 This file is not the file you visited; that changes only when you save.
5863 Normally we run the normal hook `auto-save-hook' before saving.
5865 A non-nil NO-MESSAGE argument means do not print any message if successful.
5866 A non-nil CURRENT-ONLY argument means save only current buffer. */)
5867 (no_message
, current_only
)
5868 Lisp_Object no_message
, current_only
;
5870 struct buffer
*old
= current_buffer
, *b
;
5871 Lisp_Object tail
, buf
;
5873 int do_handled_files
;
5875 FILE *stream
= NULL
;
5876 int count
= SPECPDL_INDEX ();
5877 int orig_minibuffer_auto_raise
= minibuffer_auto_raise
;
5878 int old_message_p
= 0;
5879 struct gcpro gcpro1
, gcpro2
;
5881 if (max_specpdl_size
< specpdl_size
+ 40)
5882 max_specpdl_size
= specpdl_size
+ 40;
5887 if (NILP (no_message
))
5889 old_message_p
= push_message ();
5890 record_unwind_protect (pop_message_unwind
, Qnil
);
5893 /* Ordinarily don't quit within this function,
5894 but don't make it impossible to quit (in case we get hung in I/O). */
5898 /* No GCPRO needed, because (when it matters) all Lisp_Object variables
5899 point to non-strings reached from Vbuffer_alist. */
5901 if (!NILP (Vrun_hooks
))
5902 call1 (Vrun_hooks
, intern ("auto-save-hook"));
5904 if (STRINGP (Vauto_save_list_file_name
))
5906 Lisp_Object listfile
;
5908 listfile
= Fexpand_file_name (Vauto_save_list_file_name
, Qnil
);
5910 /* Don't try to create the directory when shutting down Emacs,
5911 because creating the directory might signal an error, and
5912 that would leave Emacs in a strange state. */
5913 if (!NILP (Vrun_hooks
))
5917 GCPRO2 (dir
, listfile
);
5918 dir
= Ffile_name_directory (listfile
);
5919 if (NILP (Ffile_directory_p (dir
)))
5920 internal_condition_case_1 (do_auto_save_make_dir
,
5921 dir
, Fcons (Fcons (Qfile_error
, Qnil
), Qnil
),
5926 stream
= fopen (SDATA (listfile
), "w");
5929 record_unwind_protect (do_auto_save_unwind
,
5930 make_save_value (stream
, 0));
5931 record_unwind_protect (do_auto_save_unwind_1
,
5932 make_number (minibuffer_auto_raise
));
5933 minibuffer_auto_raise
= 0;
5936 /* On first pass, save all files that don't have handlers.
5937 On second pass, save all files that do have handlers.
5939 If Emacs is crashing, the handlers may tweak what is causing
5940 Emacs to crash in the first place, and it would be a shame if
5941 Emacs failed to autosave perfectly ordinary files because it
5942 couldn't handle some ange-ftp'd file. */
5944 for (do_handled_files
= 0; do_handled_files
< 2; do_handled_files
++)
5945 for (tail
= Vbuffer_alist
; GC_CONSP (tail
); tail
= XCDR (tail
))
5947 buf
= XCDR (XCAR (tail
));
5950 /* Record all the buffers that have auto save mode
5951 in the special file that lists them. For each of these buffers,
5952 Record visited name (if any) and auto save name. */
5953 if (STRINGP (b
->auto_save_file_name
)
5954 && stream
!= NULL
&& do_handled_files
== 0)
5956 if (!NILP (b
->filename
))
5958 fwrite (SDATA (b
->filename
), 1,
5959 SBYTES (b
->filename
), stream
);
5961 putc ('\n', stream
);
5962 fwrite (SDATA (b
->auto_save_file_name
), 1,
5963 SBYTES (b
->auto_save_file_name
), stream
);
5964 putc ('\n', stream
);
5967 if (!NILP (current_only
)
5968 && b
!= current_buffer
)
5971 /* Don't auto-save indirect buffers.
5972 The base buffer takes care of it. */
5976 /* Check for auto save enabled
5977 and file changed since last auto save
5978 and file changed since last real save. */
5979 if (STRINGP (b
->auto_save_file_name
)
5980 && BUF_SAVE_MODIFF (b
) < BUF_MODIFF (b
)
5981 && b
->auto_save_modified
< BUF_MODIFF (b
)
5982 /* -1 means we've turned off autosaving for a while--see below. */
5983 && XINT (b
->save_length
) >= 0
5984 && (do_handled_files
5985 || NILP (Ffind_file_name_handler (b
->auto_save_file_name
,
5988 EMACS_TIME before_time
, after_time
;
5990 EMACS_GET_TIME (before_time
);
5992 /* If we had a failure, don't try again for 20 minutes. */
5993 if (b
->auto_save_failure_time
>= 0
5994 && EMACS_SECS (before_time
) - b
->auto_save_failure_time
< 1200)
5997 if ((XFASTINT (b
->save_length
) * 10
5998 > (BUF_Z (b
) - BUF_BEG (b
)) * 13)
5999 /* A short file is likely to change a large fraction;
6000 spare the user annoying messages. */
6001 && XFASTINT (b
->save_length
) > 5000
6002 /* These messages are frequent and annoying for `*mail*'. */
6003 && !EQ (b
->filename
, Qnil
)
6004 && NILP (no_message
))
6006 /* It has shrunk too much; turn off auto-saving here. */
6007 minibuffer_auto_raise
= orig_minibuffer_auto_raise
;
6008 message_with_string ("Buffer %s has shrunk a lot; auto save disabled in that buffer until next real save",
6010 minibuffer_auto_raise
= 0;
6011 /* Turn off auto-saving until there's a real save,
6012 and prevent any more warnings. */
6013 XSETINT (b
->save_length
, -1);
6014 Fsleep_for (make_number (1), Qnil
);
6017 set_buffer_internal (b
);
6018 if (!auto_saved
&& NILP (no_message
))
6019 message1 ("Auto-saving...");
6020 internal_condition_case (auto_save_1
, Qt
, auto_save_error
);
6022 b
->auto_save_modified
= BUF_MODIFF (b
);
6023 XSETFASTINT (current_buffer
->save_length
, Z
- BEG
);
6024 set_buffer_internal (old
);
6026 EMACS_GET_TIME (after_time
);
6028 /* If auto-save took more than 60 seconds,
6029 assume it was an NFS failure that got a timeout. */
6030 if (EMACS_SECS (after_time
) - EMACS_SECS (before_time
) > 60)
6031 b
->auto_save_failure_time
= EMACS_SECS (after_time
);
6035 /* Prevent another auto save till enough input events come in. */
6036 record_auto_save ();
6038 if (auto_saved
&& NILP (no_message
))
6042 /* If we are going to restore an old message,
6043 give time to read ours. */
6044 sit_for (1, 0, 0, 0, 0);
6048 /* If we displayed a message and then restored a state
6049 with no message, leave a "done" message on the screen. */
6050 message1 ("Auto-saving...done");
6055 /* This restores the message-stack status. */
6056 unbind_to (count
, Qnil
);
6060 DEFUN ("set-buffer-auto-saved", Fset_buffer_auto_saved
,
6061 Sset_buffer_auto_saved
, 0, 0, 0,
6062 doc
: /* Mark current buffer as auto-saved with its current text.
6063 No auto-save file will be written until the buffer changes again. */)
6066 current_buffer
->auto_save_modified
= MODIFF
;
6067 XSETFASTINT (current_buffer
->save_length
, Z
- BEG
);
6068 current_buffer
->auto_save_failure_time
= -1;
6072 DEFUN ("clear-buffer-auto-save-failure", Fclear_buffer_auto_save_failure
,
6073 Sclear_buffer_auto_save_failure
, 0, 0, 0,
6074 doc
: /* Clear any record of a recent auto-save failure in the current buffer. */)
6077 current_buffer
->auto_save_failure_time
= -1;
6081 DEFUN ("recent-auto-save-p", Frecent_auto_save_p
, Srecent_auto_save_p
,
6083 doc
: /* Return t if current buffer has been auto-saved recently.
6084 More precisely, if it has been auto-saved since last read from or saved
6085 in the visited file. If the buffer has no visited file,
6086 then any auto-save counts as "recent". */)
6089 return (SAVE_MODIFF
< current_buffer
->auto_save_modified
) ? Qt
: Qnil
;
6092 /* Reading and completing file names */
6093 extern Lisp_Object
Ffile_name_completion (), Ffile_name_all_completions ();
6095 /* In the string VAL, change each $ to $$ and return the result. */
6098 double_dollars (val
)
6101 register const unsigned char *old
;
6102 register unsigned char *new;
6106 osize
= SBYTES (val
);
6108 /* Count the number of $ characters. */
6109 for (n
= osize
, count
= 0, old
= SDATA (val
); n
> 0; n
--)
6110 if (*old
++ == '$') count
++;
6114 val
= make_uninit_multibyte_string (SCHARS (val
) + count
,
6117 for (n
= osize
; n
> 0; n
--)
6131 read_file_name_cleanup (arg
)
6134 return (current_buffer
->directory
= arg
);
6137 DEFUN ("read-file-name-internal", Fread_file_name_internal
, Sread_file_name_internal
,
6139 doc
: /* Internal subroutine for read-file-name. Do not call this. */)
6140 (string
, dir
, action
)
6141 Lisp_Object string
, dir
, action
;
6142 /* action is nil for complete, t for return list of completions,
6143 lambda for verify final value */
6145 Lisp_Object name
, specdir
, realdir
, val
, orig_string
;
6147 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
6149 CHECK_STRING (string
);
6156 /* No need to protect ACTION--we only compare it with t and nil. */
6157 GCPRO5 (string
, realdir
, name
, specdir
, orig_string
);
6159 if (SCHARS (string
) == 0)
6161 if (EQ (action
, Qlambda
))
6169 orig_string
= string
;
6170 string
= Fsubstitute_in_file_name (string
);
6171 changed
= NILP (Fstring_equal (string
, orig_string
));
6172 name
= Ffile_name_nondirectory (string
);
6173 val
= Ffile_name_directory (string
);
6175 realdir
= Fexpand_file_name (val
, realdir
);
6180 specdir
= Ffile_name_directory (string
);
6181 val
= Ffile_name_completion (name
, realdir
);
6186 return double_dollars (string
);
6190 if (!NILP (specdir
))
6191 val
= concat2 (specdir
, val
);
6193 return double_dollars (val
);
6196 #endif /* not VMS */
6200 if (EQ (action
, Qt
))
6202 Lisp_Object all
= Ffile_name_all_completions (name
, realdir
);
6206 if (NILP (Vread_file_name_predicate
)
6207 || EQ (Vread_file_name_predicate
, Qfile_exists_p
))
6211 if (EQ (Vread_file_name_predicate
, Qfile_directory_p
))
6213 /* Brute-force speed up for directory checking:
6214 Discard strings which don't end in a slash. */
6215 for (comp
= Qnil
; CONSP (all
); all
= XCDR (all
))
6217 Lisp_Object tem
= XCAR (all
);
6219 if (STRINGP (tem
) &&
6220 (len
= SCHARS (tem
), len
> 0) &&
6221 IS_DIRECTORY_SEP (SREF (tem
, len
-1)))
6222 comp
= Fcons (tem
, comp
);
6228 /* Must do it the hard (and slow) way. */
6229 GCPRO3 (all
, comp
, specdir
);
6230 count
= SPECPDL_INDEX ();
6231 record_unwind_protect (read_file_name_cleanup
, current_buffer
->directory
);
6232 current_buffer
->directory
= realdir
;
6233 for (comp
= Qnil
; CONSP (all
); all
= XCDR (all
))
6234 if (!NILP (call1 (Vread_file_name_predicate
, XCAR (all
))))
6235 comp
= Fcons (XCAR (all
), comp
);
6236 unbind_to (count
, Qnil
);
6239 return Fnreverse (comp
);
6242 /* Only other case actually used is ACTION = lambda */
6244 /* Supposedly this helps commands such as `cd' that read directory names,
6245 but can someone explain how it helps them? -- RMS */
6246 if (SCHARS (name
) == 0)
6249 string
= Fexpand_file_name (string
, dir
);
6250 if (!NILP (Vread_file_name_predicate
))
6251 return call1 (Vread_file_name_predicate
, string
);
6252 return Ffile_exists_p (string
);
6255 DEFUN ("next-read-file-uses-dialog-p", Fnext_read_file_uses_dialog_p
,
6256 Snext_read_file_uses_dialog_p
, 0, 0, 0,
6257 doc
: /* Return t if a call to `read-file-name' will use a dialog.
6258 The return value is only relevant for a call to `read-file-name' that happens
6259 before any other event (mouse or keypress) is handeled. */)
6262 #if defined (USE_MOTIF) || defined (HAVE_NTGUI) || defined (USE_GTK) || defined (HAVE_CARBON)
6263 if ((NILP (last_nonmenu_event
) || CONSP (last_nonmenu_event
))
6272 DEFUN ("read-file-name", Fread_file_name
, Sread_file_name
, 1, 6, 0,
6273 doc
: /* Read file name, prompting with PROMPT and completing in directory DIR.
6274 Value is not expanded---you must call `expand-file-name' yourself.
6275 Default name to DEFAULT-FILENAME if user exits the minibuffer with
6276 the same non-empty string that was inserted by this function.
6277 (If DEFAULT-FILENAME is omitted, the visited file name is used,
6278 except that if INITIAL is specified, that combined with DIR is used.)
6279 If the user exits with an empty minibuffer, this function returns
6280 an empty string. (This can only happen if the user erased the
6281 pre-inserted contents or if `insert-default-directory' is nil.)
6282 Fourth arg MUSTMATCH non-nil means require existing file's name.
6283 Non-nil and non-t means also require confirmation after completion.
6284 Fifth arg INITIAL specifies text to start with.
6285 If optional sixth arg PREDICATE is non-nil, possible completions and
6286 the resulting file name must satisfy (funcall PREDICATE NAME).
6287 DIR should be an absolute directory name. It defaults to the value of
6288 `default-directory'.
6290 If this command was invoked with the mouse, use a file dialog box if
6291 `use-dialog-box' is non-nil, and the window system or X toolkit in use
6292 provides a file dialog box.
6294 See also `read-file-name-completion-ignore-case'
6295 and `read-file-name-function'. */)
6296 (prompt
, dir
, default_filename
, mustmatch
, initial
, predicate
)
6297 Lisp_Object prompt
, dir
, default_filename
, mustmatch
, initial
, predicate
;
6299 Lisp_Object val
, insdef
, tem
;
6300 struct gcpro gcpro1
, gcpro2
;
6301 register char *homedir
;
6302 Lisp_Object decoded_homedir
;
6303 int replace_in_history
= 0;
6304 int add_to_history
= 0;
6308 dir
= current_buffer
->directory
;
6309 if (NILP (Ffile_name_absolute_p (dir
)))
6310 dir
= Fexpand_file_name (dir
, Qnil
);
6311 if (NILP (default_filename
))
6314 ? Fexpand_file_name (initial
, dir
)
6315 : current_buffer
->filename
);
6317 /* If dir starts with user's homedir, change that to ~. */
6318 homedir
= (char *) egetenv ("HOME");
6320 /* homedir can be NULL in temacs, since Vprocess_environment is not
6321 yet set up. We shouldn't crash in that case. */
6324 homedir
= strcpy (alloca (strlen (homedir
) + 1), homedir
);
6325 CORRECT_DIR_SEPS (homedir
);
6330 = DECODE_FILE (make_unibyte_string (homedir
, strlen (homedir
)));
6333 && !strncmp (SDATA (decoded_homedir
), SDATA (dir
),
6334 SBYTES (decoded_homedir
))
6335 && IS_DIRECTORY_SEP (SREF (dir
, SBYTES (decoded_homedir
))))
6337 dir
= Fsubstring (dir
, make_number (SCHARS (decoded_homedir
)), Qnil
);
6338 dir
= concat2 (build_string ("~"), dir
);
6340 /* Likewise for default_filename. */
6342 && STRINGP (default_filename
)
6343 && !strncmp (SDATA (decoded_homedir
), SDATA (default_filename
),
6344 SBYTES (decoded_homedir
))
6345 && IS_DIRECTORY_SEP (SREF (default_filename
, SBYTES (decoded_homedir
))))
6348 = Fsubstring (default_filename
,
6349 make_number (SCHARS (decoded_homedir
)), Qnil
);
6350 default_filename
= concat2 (build_string ("~"), default_filename
);
6352 if (!NILP (default_filename
))
6354 CHECK_STRING (default_filename
);
6355 default_filename
= double_dollars (default_filename
);
6358 if (insert_default_directory
&& STRINGP (dir
))
6361 if (!NILP (initial
))
6363 Lisp_Object args
[2], pos
;
6367 insdef
= Fconcat (2, args
);
6368 pos
= make_number (SCHARS (double_dollars (dir
)));
6369 insdef
= Fcons (double_dollars (insdef
), pos
);
6372 insdef
= double_dollars (insdef
);
6374 else if (STRINGP (initial
))
6375 insdef
= Fcons (double_dollars (initial
), make_number (0));
6379 if (!NILP (Vread_file_name_function
))
6381 Lisp_Object args
[7];
6383 GCPRO2 (insdef
, default_filename
);
6384 args
[0] = Vread_file_name_function
;
6387 args
[3] = default_filename
;
6388 args
[4] = mustmatch
;
6390 args
[6] = predicate
;
6391 RETURN_UNGCPRO (Ffuncall (7, args
));
6394 count
= SPECPDL_INDEX ();
6395 specbind (intern ("completion-ignore-case"),
6396 read_file_name_completion_ignore_case
? Qt
: Qnil
);
6397 specbind (intern ("minibuffer-completing-file-name"), Qt
);
6398 specbind (intern ("read-file-name-predicate"),
6399 (NILP (predicate
) ? Qfile_exists_p
: predicate
));
6401 GCPRO2 (insdef
, default_filename
);
6403 #if defined (USE_MOTIF) || defined (HAVE_NTGUI) || defined (USE_GTK) || defined (HAVE_CARBON)
6404 if (! NILP (Fnext_read_file_uses_dialog_p ()))
6406 /* If DIR contains a file name, split it. */
6408 file
= Ffile_name_nondirectory (dir
);
6409 if (SCHARS (file
) && NILP (default_filename
))
6411 default_filename
= file
;
6412 dir
= Ffile_name_directory (dir
);
6414 if (!NILP(default_filename
))
6415 default_filename
= Fexpand_file_name (default_filename
, dir
);
6416 val
= Fx_file_dialog (prompt
, dir
, default_filename
, mustmatch
,
6417 EQ (predicate
, Qfile_directory_p
) ? Qt
: Qnil
);
6422 val
= Fcompleting_read (prompt
, intern ("read-file-name-internal"),
6423 dir
, mustmatch
, insdef
,
6424 Qfile_name_history
, default_filename
, Qnil
);
6426 tem
= Fsymbol_value (Qfile_name_history
);
6427 if (CONSP (tem
) && EQ (XCAR (tem
), val
))
6428 replace_in_history
= 1;
6430 /* If Fcompleting_read returned the inserted default string itself
6431 (rather than a new string with the same contents),
6432 it has to mean that the user typed RET with the minibuffer empty.
6433 In that case, we really want to return ""
6434 so that commands such as set-visited-file-name can distinguish. */
6435 if (EQ (val
, default_filename
))
6437 /* In this case, Fcompleting_read has not added an element
6438 to the history. Maybe we should. */
6439 if (! replace_in_history
)
6445 unbind_to (count
, Qnil
);
6448 error ("No file name specified");
6450 tem
= Fstring_equal (val
, CONSP (insdef
) ? XCAR (insdef
) : insdef
);
6452 if (!NILP (tem
) && !NILP (default_filename
))
6453 val
= default_filename
;
6454 val
= Fsubstitute_in_file_name (val
);
6456 if (replace_in_history
)
6457 /* Replace what Fcompleting_read added to the history
6458 with what we will actually return. */
6460 Lisp_Object val1
= double_dollars (val
);
6461 tem
= Fsymbol_value (Qfile_name_history
);
6462 if (history_delete_duplicates
)
6463 XSETCDR (tem
, Fdelete (val1
, XCDR(tem
)));
6464 XSETCAR (tem
, val1
);
6466 else if (add_to_history
)
6468 /* Add the value to the history--but not if it matches
6469 the last value already there. */
6470 Lisp_Object val1
= double_dollars (val
);
6471 tem
= Fsymbol_value (Qfile_name_history
);
6472 if (! CONSP (tem
) || NILP (Fequal (XCAR (tem
), val1
)))
6474 if (history_delete_duplicates
) tem
= Fdelete (val1
, tem
);
6475 Fset (Qfile_name_history
, Fcons (val1
, tem
));
6486 /* Must be set before any path manipulation is performed. */
6487 XSETFASTINT (Vdirectory_sep_char
, '/');
6494 Qoperations
= intern ("operations");
6495 Qexpand_file_name
= intern ("expand-file-name");
6496 Qsubstitute_in_file_name
= intern ("substitute-in-file-name");
6497 Qdirectory_file_name
= intern ("directory-file-name");
6498 Qfile_name_directory
= intern ("file-name-directory");
6499 Qfile_name_nondirectory
= intern ("file-name-nondirectory");
6500 Qunhandled_file_name_directory
= intern ("unhandled-file-name-directory");
6501 Qfile_name_as_directory
= intern ("file-name-as-directory");
6502 Qcopy_file
= intern ("copy-file");
6503 Qmake_directory_internal
= intern ("make-directory-internal");
6504 Qmake_directory
= intern ("make-directory");
6505 Qdelete_directory
= intern ("delete-directory");
6506 Qdelete_file
= intern ("delete-file");
6507 Qrename_file
= intern ("rename-file");
6508 Qadd_name_to_file
= intern ("add-name-to-file");
6509 Qmake_symbolic_link
= intern ("make-symbolic-link");
6510 Qfile_exists_p
= intern ("file-exists-p");
6511 Qfile_executable_p
= intern ("file-executable-p");
6512 Qfile_readable_p
= intern ("file-readable-p");
6513 Qfile_writable_p
= intern ("file-writable-p");
6514 Qfile_symlink_p
= intern ("file-symlink-p");
6515 Qaccess_file
= intern ("access-file");
6516 Qfile_directory_p
= intern ("file-directory-p");
6517 Qfile_regular_p
= intern ("file-regular-p");
6518 Qfile_accessible_directory_p
= intern ("file-accessible-directory-p");
6519 Qfile_modes
= intern ("file-modes");
6520 Qset_file_modes
= intern ("set-file-modes");
6521 Qset_file_times
= intern ("set-file-times");
6522 Qfile_newer_than_file_p
= intern ("file-newer-than-file-p");
6523 Qinsert_file_contents
= intern ("insert-file-contents");
6524 Qwrite_region
= intern ("write-region");
6525 Qverify_visited_file_modtime
= intern ("verify-visited-file-modtime");
6526 Qset_visited_file_modtime
= intern ("set-visited-file-modtime");
6527 Qauto_save_coding
= intern ("auto-save-coding");
6529 staticpro (&Qoperations
);
6530 staticpro (&Qexpand_file_name
);
6531 staticpro (&Qsubstitute_in_file_name
);
6532 staticpro (&Qdirectory_file_name
);
6533 staticpro (&Qfile_name_directory
);
6534 staticpro (&Qfile_name_nondirectory
);
6535 staticpro (&Qunhandled_file_name_directory
);
6536 staticpro (&Qfile_name_as_directory
);
6537 staticpro (&Qcopy_file
);
6538 staticpro (&Qmake_directory_internal
);
6539 staticpro (&Qmake_directory
);
6540 staticpro (&Qdelete_directory
);
6541 staticpro (&Qdelete_file
);
6542 staticpro (&Qrename_file
);
6543 staticpro (&Qadd_name_to_file
);
6544 staticpro (&Qmake_symbolic_link
);
6545 staticpro (&Qfile_exists_p
);
6546 staticpro (&Qfile_executable_p
);
6547 staticpro (&Qfile_readable_p
);
6548 staticpro (&Qfile_writable_p
);
6549 staticpro (&Qaccess_file
);
6550 staticpro (&Qfile_symlink_p
);
6551 staticpro (&Qfile_directory_p
);
6552 staticpro (&Qfile_regular_p
);
6553 staticpro (&Qfile_accessible_directory_p
);
6554 staticpro (&Qfile_modes
);
6555 staticpro (&Qset_file_modes
);
6556 staticpro (&Qset_file_times
);
6557 staticpro (&Qfile_newer_than_file_p
);
6558 staticpro (&Qinsert_file_contents
);
6559 staticpro (&Qwrite_region
);
6560 staticpro (&Qverify_visited_file_modtime
);
6561 staticpro (&Qset_visited_file_modtime
);
6562 staticpro (&Qauto_save_coding
);
6564 Qfile_name_history
= intern ("file-name-history");
6565 Fset (Qfile_name_history
, Qnil
);
6566 staticpro (&Qfile_name_history
);
6568 Qfile_error
= intern ("file-error");
6569 staticpro (&Qfile_error
);
6570 Qfile_already_exists
= intern ("file-already-exists");
6571 staticpro (&Qfile_already_exists
);
6572 Qfile_date_error
= intern ("file-date-error");
6573 staticpro (&Qfile_date_error
);
6574 Qexcl
= intern ("excl");
6578 Qfind_buffer_file_type
= intern ("find-buffer-file-type");
6579 staticpro (&Qfind_buffer_file_type
);
6582 DEFVAR_LISP ("file-name-coding-system", &Vfile_name_coding_system
,
6583 doc
: /* *Coding system for encoding file names.
6584 If it is nil, `default-file-name-coding-system' (which see) is used. */);
6585 Vfile_name_coding_system
= Qnil
;
6587 DEFVAR_LISP ("default-file-name-coding-system",
6588 &Vdefault_file_name_coding_system
,
6589 doc
: /* Default coding system for encoding file names.
6590 This variable is used only when `file-name-coding-system' is nil.
6592 This variable is set/changed by the command `set-language-environment'.
6593 User should not set this variable manually,
6594 instead use `file-name-coding-system' to get a constant encoding
6595 of file names regardless of the current language environment. */);
6596 Vdefault_file_name_coding_system
= Qnil
;
6598 Qformat_decode
= intern ("format-decode");
6599 staticpro (&Qformat_decode
);
6600 Qformat_annotate_function
= intern ("format-annotate-function");
6601 staticpro (&Qformat_annotate_function
);
6602 Qafter_insert_file_set_coding
= intern ("after-insert-file-set-coding");
6603 staticpro (&Qafter_insert_file_set_coding
);
6605 Qcar_less_than_car
= intern ("car-less-than-car");
6606 staticpro (&Qcar_less_than_car
);
6608 Fput (Qfile_error
, Qerror_conditions
,
6609 Fcons (Qfile_error
, Fcons (Qerror
, Qnil
)));
6610 Fput (Qfile_error
, Qerror_message
,
6611 build_string ("File error"));
6613 Fput (Qfile_already_exists
, Qerror_conditions
,
6614 Fcons (Qfile_already_exists
,
6615 Fcons (Qfile_error
, Fcons (Qerror
, Qnil
))));
6616 Fput (Qfile_already_exists
, Qerror_message
,
6617 build_string ("File already exists"));
6619 Fput (Qfile_date_error
, Qerror_conditions
,
6620 Fcons (Qfile_date_error
,
6621 Fcons (Qfile_error
, Fcons (Qerror
, Qnil
))));
6622 Fput (Qfile_date_error
, Qerror_message
,
6623 build_string ("Cannot set file date"));
6625 DEFVAR_LISP ("read-file-name-function", &Vread_file_name_function
,
6626 doc
: /* If this is non-nil, `read-file-name' does its work by calling this function. */);
6627 Vread_file_name_function
= Qnil
;
6629 DEFVAR_LISP ("read-file-name-predicate", &Vread_file_name_predicate
,
6630 doc
: /* Current predicate used by `read-file-name-internal'. */);
6631 Vread_file_name_predicate
= Qnil
;
6633 DEFVAR_BOOL ("read-file-name-completion-ignore-case", &read_file_name_completion_ignore_case
,
6634 doc
: /* *Non-nil means when reading a file name completion ignores case. */);
6635 #if defined VMS || defined DOS_NT || defined MAC_OS
6636 read_file_name_completion_ignore_case
= 1;
6638 read_file_name_completion_ignore_case
= 0;
6641 DEFVAR_BOOL ("insert-default-directory", &insert_default_directory
,
6642 doc
: /* *Non-nil means when reading a filename start with default dir in minibuffer.
6643 If the initial minibuffer contents are non-empty, you can usually
6644 request a default filename by typing RETURN without editing. For some
6645 commands, exiting with an empty minibuffer has a special meaning,
6646 such as making the current buffer visit no file in the case of
6647 `set-visited-file-name'.
6648 If this variable is non-nil, the minibuffer contents are always
6649 initially non-empty and typing RETURN without editing will fetch the
6650 default name, if one is provided. Note however that this default name
6651 is not necessarily the name originally inserted in the minibuffer, if
6652 that is just the default directory.
6653 If this variable is nil, the minibuffer often starts out empty. In
6654 that case you may have to explicitly fetch the next history element to
6655 request the default name. */);
6656 insert_default_directory
= 1;
6658 DEFVAR_BOOL ("vms-stmlf-recfm", &vms_stmlf_recfm
,
6659 doc
: /* *Non-nil means write new files with record format `stmlf'.
6660 nil means use format `var'. This variable is meaningful only on VMS. */);
6661 vms_stmlf_recfm
= 0;
6663 DEFVAR_LISP ("directory-sep-char", &Vdirectory_sep_char
,
6664 doc
: /* Directory separator character for built-in functions that return file names.
6665 The value is always ?/. Don't use this variable, just use `/'. */);
6667 DEFVAR_LISP ("file-name-handler-alist", &Vfile_name_handler_alist
,
6668 doc
: /* *Alist of elements (REGEXP . HANDLER) for file names handled specially.
6669 If a file name matches REGEXP, then all I/O on that file is done by calling
6672 The first argument given to HANDLER is the name of the I/O primitive
6673 to be handled; the remaining arguments are the arguments that were
6674 passed to that primitive. For example, if you do
6675 (file-exists-p FILENAME)
6676 and FILENAME is handled by HANDLER, then HANDLER is called like this:
6677 (funcall HANDLER 'file-exists-p FILENAME)
6678 The function `find-file-name-handler' checks this list for a handler
6679 for its argument. */);
6680 Vfile_name_handler_alist
= Qnil
;
6682 DEFVAR_LISP ("set-auto-coding-function",
6683 &Vset_auto_coding_function
,
6684 doc
: /* If non-nil, a function to call to decide a coding system of file.
6685 Two arguments are passed to this function: the file name
6686 and the length of a file contents following the point.
6687 This function should return a coding system to decode the file contents.
6688 It should check the file name against `auto-coding-alist'.
6689 If no coding system is decided, it should check a coding system
6690 specified in the heading lines with the format:
6691 -*- ... coding: CODING-SYSTEM; ... -*-
6692 or local variable spec of the tailing lines with `coding:' tag. */);
6693 Vset_auto_coding_function
= Qnil
;
6695 DEFVAR_LISP ("after-insert-file-functions", &Vafter_insert_file_functions
,
6696 doc
: /* A list of functions to be called at the end of `insert-file-contents'.
6697 Each is passed one argument, the number of characters inserted.
6698 It should return the new character count, and leave point the same.
6699 If `insert-file-contents' is intercepted by a handler from
6700 `file-name-handler-alist', that handler is responsible for calling the
6701 functions in `after-insert-file-functions' if appropriate. */);
6702 Vafter_insert_file_functions
= Qnil
;
6704 DEFVAR_LISP ("write-region-annotate-functions", &Vwrite_region_annotate_functions
,
6705 doc
: /* A list of functions to be called at the start of `write-region'.
6706 Each is passed two arguments, START and END as for `write-region'.
6707 These are usually two numbers but not always; see the documentation
6708 for `write-region'. The function should return a list of pairs
6709 of the form (POSITION . STRING), consisting of strings to be effectively
6710 inserted at the specified positions of the file being written (1 means to
6711 insert before the first byte written). The POSITIONs must be sorted into
6712 increasing order. If there are several functions in the list, the several
6713 lists are merged destructively. Alternatively, the function can return
6714 with a different buffer current; in that case it should pay attention
6715 to the annotations returned by previous functions and listed in
6716 `write-region-annotations-so-far'.*/);
6717 Vwrite_region_annotate_functions
= Qnil
;
6718 staticpro (&Qwrite_region_annotate_functions
);
6719 Qwrite_region_annotate_functions
6720 = intern ("write-region-annotate-functions");
6722 DEFVAR_LISP ("write-region-annotations-so-far",
6723 &Vwrite_region_annotations_so_far
,
6724 doc
: /* When an annotation function is called, this holds the previous annotations.
6725 These are the annotations made by other annotation functions
6726 that were already called. See also `write-region-annotate-functions'. */);
6727 Vwrite_region_annotations_so_far
= Qnil
;
6729 DEFVAR_LISP ("inhibit-file-name-handlers", &Vinhibit_file_name_handlers
,
6730 doc
: /* A list of file name handlers that temporarily should not be used.
6731 This applies only to the operation `inhibit-file-name-operation'. */);
6732 Vinhibit_file_name_handlers
= Qnil
;
6734 DEFVAR_LISP ("inhibit-file-name-operation", &Vinhibit_file_name_operation
,
6735 doc
: /* The operation for which `inhibit-file-name-handlers' is applicable. */);
6736 Vinhibit_file_name_operation
= Qnil
;
6738 DEFVAR_LISP ("auto-save-list-file-name", &Vauto_save_list_file_name
,
6739 doc
: /* File name in which we write a list of all auto save file names.
6740 This variable is initialized automatically from `auto-save-list-file-prefix'
6741 shortly after Emacs reads your `.emacs' file, if you have not yet given it
6742 a non-nil value. */);
6743 Vauto_save_list_file_name
= Qnil
;
6745 defsubr (&Sfind_file_name_handler
);
6746 defsubr (&Sfile_name_directory
);
6747 defsubr (&Sfile_name_nondirectory
);
6748 defsubr (&Sunhandled_file_name_directory
);
6749 defsubr (&Sfile_name_as_directory
);
6750 defsubr (&Sdirectory_file_name
);
6751 defsubr (&Smake_temp_name
);
6752 defsubr (&Sexpand_file_name
);
6753 defsubr (&Ssubstitute_in_file_name
);
6754 defsubr (&Scopy_file
);
6755 defsubr (&Smake_directory_internal
);
6756 defsubr (&Sdelete_directory
);
6757 defsubr (&Sdelete_file
);
6758 defsubr (&Srename_file
);
6759 defsubr (&Sadd_name_to_file
);
6761 defsubr (&Smake_symbolic_link
);
6762 #endif /* S_IFLNK */
6764 defsubr (&Sdefine_logical_name
);
6767 defsubr (&Ssysnetunam
);
6768 #endif /* HPUX_NET */
6769 defsubr (&Sfile_name_absolute_p
);
6770 defsubr (&Sfile_exists_p
);
6771 defsubr (&Sfile_executable_p
);
6772 defsubr (&Sfile_readable_p
);
6773 defsubr (&Sfile_writable_p
);
6774 defsubr (&Saccess_file
);
6775 defsubr (&Sfile_symlink_p
);
6776 defsubr (&Sfile_directory_p
);
6777 defsubr (&Sfile_accessible_directory_p
);
6778 defsubr (&Sfile_regular_p
);
6779 defsubr (&Sfile_modes
);
6780 defsubr (&Sset_file_modes
);
6781 defsubr (&Sset_file_times
);
6782 defsubr (&Sset_default_file_modes
);
6783 defsubr (&Sdefault_file_modes
);
6784 defsubr (&Sfile_newer_than_file_p
);
6785 defsubr (&Sinsert_file_contents
);
6786 defsubr (&Swrite_region
);
6787 defsubr (&Scar_less_than_car
);
6788 defsubr (&Sverify_visited_file_modtime
);
6789 defsubr (&Sclear_visited_file_modtime
);
6790 defsubr (&Svisited_file_modtime
);
6791 defsubr (&Sset_visited_file_modtime
);
6792 defsubr (&Sdo_auto_save
);
6793 defsubr (&Sset_buffer_auto_saved
);
6794 defsubr (&Sclear_buffer_auto_save_failure
);
6795 defsubr (&Srecent_auto_save_p
);
6797 defsubr (&Sread_file_name_internal
);
6798 defsubr (&Sread_file_name
);
6799 defsubr (&Snext_read_file_uses_dialog_p
);
6802 defsubr (&Sunix_sync
);
6806 /* arch-tag: 64ba3fd7-f844-4fb2-ba4b-427eb928786c
6807 (do not change this comment) */