From: Ted Zlatanov Date: Sat, 19 Aug 2017 01:55:11 +0000 (-0400) Subject: Fix and document make-temp-file optional text parameter X-Git-Tag: emacs-26.0.90~418 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=94f3f13d6db0103267c514133109aebee6efb023;p=emacs.git Fix and document make-temp-file optional text parameter * lisp/files.el (make-temp-file): Fix initial TEXT parameter. (files--make-magic-temp-file): Support optional TEXT parameter. * etc/NEWS: Document it. * doc/lispref/files.texi: Document it. * test/lisp/auth-source-tests.el: Minor reformat. --- diff --git a/doc/lispref/files.texi b/doc/lispref/files.texi index 25f32c231ca..9359d3eaa09 100644 --- a/doc/lispref/files.texi +++ b/doc/lispref/files.texi @@ -2467,11 +2467,12 @@ construct a name for such a file: 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}. @@ -2494,6 +2495,8 @@ not the directory name, of that directory. @xref{Directory Names}. 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} diff --git a/etc/NEWS b/etc/NEWS index 9e86af57757..54be0fca4af 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -1210,6 +1210,8 @@ propagated to file name handlers now. * Lisp Changes in Emacs 26.1 +** New optional argument TEXT in 'make-temp-file'. + ** New function `define-symbol-prop'. +++ diff --git a/lisp/files.el b/lisp/files.el index 4dc1238f955..af5d3ba53e1 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -1404,8 +1404,8 @@ 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 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. @@ -1413,20 +1413,25 @@ If SUFFIX is non-nil, add that at the end of the file name." (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)) @@ -1434,7 +1439,7 @@ This implementation works on magic file names." (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 diff --git a/test/lisp/auth-source-tests.el b/test/lisp/auth-source-tests.el index f35c4009535..90a4475ab0d 100644 --- a/test/lisp/auth-source-tests.el +++ b/test/lisp/auth-source-tests.el @@ -267,10 +267,8 @@ :host "b1" :port "b2" :user "b3") )) - (netrc-file (make-temp-file - "auth-source-test" - nil nil - (mapconcat 'identity entries "\n"))) + (netrc-file (make-temp-file "auth-source-test" nil nil + (mapconcat 'identity entries "\n"))) (auth-sources (list netrc-file)) (auth-source-do-cache nil) found found-as-string)