The job of @code{make-temp-file} is to prevent two different users or
two different jobs from trying to use the exact same file name.
-@defun make-temp-file prefix &optional dir-flag suffix
+@defun make-temp-file prefix &optional dir-flag suffix text
This function creates a temporary file and returns its name. Emacs
creates the temporary file's name by adding to @var{prefix} some
random characters that are different in each Emacs job. The result is
-guaranteed to be a newly created empty file. On MS-DOS, this function
+guaranteed to be a newly created file, containing @var{text} if that's
+given as a string and empty otherwise. On MS-DOS, this function
can truncate the @var{string} prefix to fit into the 8+3 file-name
limits. If @var{prefix} is a relative file name, it is expanded
against @code{temporary-file-directory}.
If @var{suffix} is non-@code{nil}, @code{make-temp-file} adds it at
the end of the file name.
+If @var{text} is a string, @code{make-temp-file} inserts it in the file.
+
To prevent conflicts among different libraries running in the same
Emacs, each Lisp program that uses @code{make-temp-file} should have its
own @var{prefix}. The number added to the end of @var{prefix}
is guaranteed to point to a newly created file.
You can then use `write-region' to write new data into the file.
-If TEXT is non-nil, it will be inserted in the new
-file. Otherwise the file will be empty.
+If TEXT is non-nil, it will be inserted in the new file. Otherwise
+the file will be empty.
If DIR-FLAG is non-nil, create a new empty directory instead of a file.
(let ((absolute-prefix
(if (or (zerop (length prefix)) (member prefix '("." "..")))
(concat (file-name-as-directory temporary-file-directory) prefix)
- (expand-file-name prefix temporary-file-directory))))
+ (expand-file-name prefix temporary-file-directory)))
+ (contents (if (stringp text) text "")))
(if (find-file-name-handler absolute-prefix 'write-region)
- (files--make-magic-temp-file absolute-prefix dir-flag suffix)
- (make-temp-file-internal absolute-prefix
- (if dir-flag t) (or suffix "")))))
-
-(defun files--make-magic-temp-file (absolute-prefix &optional dir-flag suffix)
- "Implement (make-temp-file ABSOLUTE-PREFIX DIR-FLAG SUFFIX).
+ (files--make-magic-temp-file absolute-prefix dir-flag suffix contents)
+ (let ((file (make-temp-file-internal absolute-prefix
+ (if dir-flag t) (or suffix ""))))
+ (write-region contents nil file nil 'silent)
+ file))))
+
+(defun files--make-magic-temp-file (absolute-prefix
+ &optional dir-flag suffix text)
+ "Implement (make-temp-file ABSOLUTE-PREFIX DIR-FLAG SUFFIX TEXT).
This implementation works on magic file names."
;; Create temp files with strict access rights. It's easy to
;; loosen them later, whereas it's impossible to close the
;; time-window of loose permissions otherwise.
(with-file-modes ?\700
- (let (file)
+ (let ((contents (if (stringp text) text ""))
+ file)
(while (condition-case ()
(progn
(setq file (make-temp-name absolute-prefix))
(setq file (concat file suffix)))
(if dir-flag
(make-directory file)
- (write-region (or text "") nil file nil 'silent nil 'excl))
+ (write-region contents nil file nil 'silent nil 'excl))
nil)
(file-already-exists t))
;; the file was somehow created by someone else between