# HG changeset patch # User Richard M. Stallman # Date 843884160 0 # Node ID 18cc78dc8b186637c3d5df31b2e217cc420d8a98 # Parent 8b36369ba4de600f20882c5dd94d7ffc244c304b (with-temp-file): New macro. diff -r 8b36369ba4de -r 18cc78dc8b18 lisp/subr.el --- a/lisp/subr.el Sat Sep 28 03:17:21 1996 +0000 +++ b/lisp/subr.el Sat Sep 28 04:16:00 1996 +0000 @@ -732,7 +732,7 @@ (t (start-process name buffer shell-file-name shell-command-switch (mapconcat 'identity args " "))))) - + (defmacro with-current-buffer (buffer &rest body) "Execute the forms in BODY with BUFFER as the current buffer. The value returned is the value of the last form in BODY." @@ -740,6 +740,52 @@ (set-buffer ,buffer) . ,body)) +(defmacro with-temp-file (file &rest forms) + "Create a new buffer, evaluate FORMS there, and write the buffer to FILE. +Return the value of FORMS. +If FILE is nil, just evaluate FORMS and don't save anything. +If FILE is t, return the buffer contents as a string." + (let ((temp-file (make-symbol "temp-file")) + (temp-buffer (make-symbol "temp-buffer")) + (temp-results (make-symbol "temp-results"))) + `(save-excursion + (let* ((,temp-file ,file) + (default-major-mode 'fundamental-mode) + (,temp-buffer + (progn + (set-buffer + (get-buffer-create + (generate-new-buffer-name " *temp file*"))) + (buffer-disable-undo (current-buffer)) + (current-buffer))) + ,temp-results) + (unwind-protect + (progn + (setq ,temp-results (progn ,@forms)) + (cond + ;; Don't save anything. + ((null ,temp-file) + ,temp-results) + ;; Return the buffer contents. + ((eq ,temp-file t) + (set-buffer ,temp-buffer) + (buffer-string)) + ;; Save a file. + (t + (set-buffer ,temp-buffer) + ;; Make sure the directory where this file is + ;; to be saved exists. + (when (not (file-directory-p + (file-name-directory ,temp-file))) + (make-directory (file-name-directory ,temp-file) t)) + ;; Save the file. + (write-region (point-min) (point-max) + ,temp-file nil 'nomesg) + ,temp-results))) + ;; Kill the buffer. + (when (buffer-name ,temp-buffer) + (kill-buffer ,temp-buffer))))))) + (defmacro with-output-to-string (&rest body) "Execute BODY, return the text it sent to `standard-output', as a string." `(let ((standard-output (get-buffer-create " *string-output*"))) @@ -799,7 +845,7 @@ (cons (substring string start) list))) (nreverse list))) - + (defun shell-quote-argument (argument) "Quote an argument for passing as argument to an inferior shell." (if (eq system-type 'ms-dos)