aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTed Zlatanov2017-08-18 21:55:11 -0400
committerTed Zlatanov2017-08-18 21:59:39 -0400
commit94f3f13d6db0103267c514133109aebee6efb023 (patch)
treed2222abba85db8a446cadc06f9e84a0149924a1d
parent10cde01c5e39f13287c64ec53adb191b8331a6cf (diff)
downloademacs-94f3f13d6db0103267c514133109aebee6efb023.tar.gz
emacs-94f3f13d6db0103267c514133109aebee6efb023.zip
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.
-rw-r--r--doc/lispref/files.texi7
-rw-r--r--etc/NEWS2
-rw-r--r--lisp/files.el27
-rw-r--r--test/lisp/auth-source-tests.el6
4 files changed, 25 insertions, 17 deletions
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:
2467The job of @code{make-temp-file} is to prevent two different users or 2467The job of @code{make-temp-file} is to prevent two different users or
2468two different jobs from trying to use the exact same file name. 2468two different jobs from trying to use the exact same file name.
2469 2469
2470@defun make-temp-file prefix &optional dir-flag suffix 2470@defun make-temp-file prefix &optional dir-flag suffix text
2471This function creates a temporary file and returns its name. Emacs 2471This function creates a temporary file and returns its name. Emacs
2472creates the temporary file's name by adding to @var{prefix} some 2472creates the temporary file's name by adding to @var{prefix} some
2473random characters that are different in each Emacs job. The result is 2473random characters that are different in each Emacs job. The result is
2474guaranteed to be a newly created empty file. On MS-DOS, this function 2474guaranteed to be a newly created file, containing @var{text} if that's
2475given as a string and empty otherwise. On MS-DOS, this function
2475can truncate the @var{string} prefix to fit into the 8+3 file-name 2476can truncate the @var{string} prefix to fit into the 8+3 file-name
2476limits. If @var{prefix} is a relative file name, it is expanded 2477limits. If @var{prefix} is a relative file name, it is expanded
2477against @code{temporary-file-directory}. 2478against @code{temporary-file-directory}.
@@ -2494,6 +2495,8 @@ not the directory name, of that directory. @xref{Directory Names}.
2494If @var{suffix} is non-@code{nil}, @code{make-temp-file} adds it at 2495If @var{suffix} is non-@code{nil}, @code{make-temp-file} adds it at
2495the end of the file name. 2496the end of the file name.
2496 2497
2498If @var{text} is a string, @code{make-temp-file} inserts it in the file.
2499
2497To prevent conflicts among different libraries running in the same 2500To prevent conflicts among different libraries running in the same
2498Emacs, each Lisp program that uses @code{make-temp-file} should have its 2501Emacs, each Lisp program that uses @code{make-temp-file} should have its
2499own @var{prefix}. The number added to the end of @var{prefix} 2502own @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.
1210 1210
1211* Lisp Changes in Emacs 26.1 1211* Lisp Changes in Emacs 26.1
1212 1212
1213** New optional argument TEXT in 'make-temp-file'.
1214
1213** New function `define-symbol-prop'. 1215** New function `define-symbol-prop'.
1214 1216
1215+++ 1217+++
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),
1404is guaranteed to point to a newly created file. 1404is guaranteed to point to a newly created file.
1405You can then use `write-region' to write new data into the file. 1405You can then use `write-region' to write new data into the file.
1406 1406
1407If TEXT is non-nil, it will be inserted in the new 1407If TEXT is non-nil, it will be inserted in the new file. Otherwise
1408file. Otherwise the file will be empty. 1408the file will be empty.
1409 1409
1410If DIR-FLAG is non-nil, create a new empty directory instead of a file. 1410If DIR-FLAG is non-nil, create a new empty directory instead of a file.
1411 1411
@@ -1413,20 +1413,25 @@ If SUFFIX is non-nil, add that at the end of the file name."
1413 (let ((absolute-prefix 1413 (let ((absolute-prefix
1414 (if (or (zerop (length prefix)) (member prefix '("." ".."))) 1414 (if (or (zerop (length prefix)) (member prefix '("." "..")))
1415 (concat (file-name-as-directory temporary-file-directory) prefix) 1415 (concat (file-name-as-directory temporary-file-directory) prefix)
1416 (expand-file-name prefix temporary-file-directory)))) 1416 (expand-file-name prefix temporary-file-directory)))
1417 (contents (if (stringp text) text "")))
1417 (if (find-file-name-handler absolute-prefix 'write-region) 1418 (if (find-file-name-handler absolute-prefix 'write-region)
1418 (files--make-magic-temp-file absolute-prefix dir-flag suffix) 1419 (files--make-magic-temp-file absolute-prefix dir-flag suffix contents)
1419 (make-temp-file-internal absolute-prefix 1420 (let ((file (make-temp-file-internal absolute-prefix
1420 (if dir-flag t) (or suffix ""))))) 1421 (if dir-flag t) (or suffix ""))))
1421 1422 (write-region contents nil file nil 'silent)
1422(defun files--make-magic-temp-file (absolute-prefix &optional dir-flag suffix) 1423 file))))
1423 "Implement (make-temp-file ABSOLUTE-PREFIX DIR-FLAG SUFFIX). 1424
1425(defun files--make-magic-temp-file (absolute-prefix
1426 &optional dir-flag suffix text)
1427 "Implement (make-temp-file ABSOLUTE-PREFIX DIR-FLAG SUFFIX TEXT).
1424This implementation works on magic file names." 1428This implementation works on magic file names."
1425 ;; Create temp files with strict access rights. It's easy to 1429 ;; Create temp files with strict access rights. It's easy to
1426 ;; loosen them later, whereas it's impossible to close the 1430 ;; loosen them later, whereas it's impossible to close the
1427 ;; time-window of loose permissions otherwise. 1431 ;; time-window of loose permissions otherwise.
1428 (with-file-modes ?\700 1432 (with-file-modes ?\700
1429 (let (file) 1433 (let ((contents (if (stringp text) text ""))
1434 file)
1430 (while (condition-case () 1435 (while (condition-case ()
1431 (progn 1436 (progn
1432 (setq file (make-temp-name absolute-prefix)) 1437 (setq file (make-temp-name absolute-prefix))
@@ -1434,7 +1439,7 @@ This implementation works on magic file names."
1434 (setq file (concat file suffix))) 1439 (setq file (concat file suffix)))
1435 (if dir-flag 1440 (if dir-flag
1436 (make-directory file) 1441 (make-directory file)
1437 (write-region (or text "") nil file nil 'silent nil 'excl)) 1442 (write-region contents nil file nil 'silent nil 'excl))
1438 nil) 1443 nil)
1439 (file-already-exists t)) 1444 (file-already-exists t))
1440 ;; the file was somehow created by someone else between 1445 ;; 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 @@
267 :host "b1" :port "b2" :user "b3") 267 :host "b1" :port "b2" :user "b3")
268 )) 268 ))
269 269
270 (netrc-file (make-temp-file 270 (netrc-file (make-temp-file "auth-source-test" nil nil
271 "auth-source-test" 271 (mapconcat 'identity entries "\n")))
272 nil nil
273 (mapconcat 'identity entries "\n")))
274 (auth-sources (list netrc-file)) 272 (auth-sources (list netrc-file))
275 (auth-source-do-cache nil) 273 (auth-source-do-cache nil)
276 found found-as-string) 274 found found-as-string)