From: Richard M. Stallman Date: Sat, 28 Sep 1996 04:16:00 +0000 (+0000) Subject: (with-temp-file): New macro. X-Git-Tag: emacs-20.1~3585 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=a7ed4c2a6eac9a32100da1215604613baf92bcb3;p=emacs.git (with-temp-file): New macro. --- diff --git a/lisp/subr.el b/lisp/subr.el index a3357c71863..7a5c9c69a6e 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -732,7 +732,7 @@ Wildcards and redirection are handled as usual in the shell." (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 @@ The value returned is the value of the last form in BODY." (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 @@ If SEPARATORS is absent, it defaults to \"[ \\f\\t\\n\\r\\v]+\"." (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)