Function: make-temp-file

make-temp-file is a byte-compiled function defined in files.el.gz.

Signature

(make-temp-file PREFIX &optional DIR-FLAG SUFFIX TEXT)

Documentation

Create a temporary file.

The returned file name (created by appending some random characters at the end of PREFIX, and expanding against temporary-file-directory(var)/temporary-file-directory(fun) if necessary), is guaranteed to point to a newly created file. You can then use write-region to write new data into the file.

If DIR-FLAG is non-nil, create a new empty directory instead of a file.

If SUFFIX is non-nil, add that at the end of the file name.

If TEXT is a string, insert it into the new file; DIR-FLAG should be nil. Otherwise the file will be empty.

Other relevant functions are documented in the file group.

View in manual

Probably introduced at or before Emacs version 21.1.

Shortdoc

;; file
(make-temp-file "/tmp/foo-")
    e.g. => "/tmp/foo-ZcXFMj"

Aliases

emerge-make-temp-file (obsolete since 30.1) pgg-make-temp-file idlwave-shell-make-temp-file (obsolete since 27.1) mm-make-temp-file (obsolete since 26.1)

Source Code

;; Defined in /usr/src/emacs/lisp/files.el.gz
(defun make-temp-file (prefix &optional dir-flag suffix text)
  "Create a temporary file.
The returned file name (created by appending some random characters at the end
of PREFIX, and expanding against `temporary-file-directory' if necessary),
is guaranteed to point to a newly created file.
You can then use `write-region' to write new data into the file.

If DIR-FLAG is non-nil, create a new empty directory instead of a file.

If SUFFIX is non-nil, add that at the end of the file name.

If TEXT is a string, insert it into the new file; DIR-FLAG should be nil.
Otherwise the file will be empty."
  (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))))
    (if (find-file-name-handler absolute-prefix 'write-region)
        (files--make-magic-temp-file absolute-prefix dir-flag suffix text)
      (make-temp-file-internal absolute-prefix
			       (if dir-flag t) (or suffix "") text))))