diff options
| author | Richard M. Stallman | 1996-09-28 04:16:00 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 1996-09-28 04:16:00 +0000 |
| commit | a7ed4c2a6eac9a32100da1215604613baf92bcb3 (patch) | |
| tree | 8ae438bd5941f14678e2f9dbd42562cc0ff0a3de | |
| parent | 86175613eacc61c217599d94503a3bcd0354f183 (diff) | |
| download | emacs-a7ed4c2a6eac9a32100da1215604613baf92bcb3.tar.gz emacs-a7ed4c2a6eac9a32100da1215604613baf92bcb3.zip | |
(with-temp-file): New macro.
| -rw-r--r-- | lisp/subr.el | 50 |
1 files changed, 48 insertions, 2 deletions
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." | |||
| 732 | (t | 732 | (t |
| 733 | (start-process name buffer shell-file-name shell-command-switch | 733 | (start-process name buffer shell-file-name shell-command-switch |
| 734 | (mapconcat 'identity args " "))))) | 734 | (mapconcat 'identity args " "))))) |
| 735 | 735 | ||
| 736 | (defmacro with-current-buffer (buffer &rest body) | 736 | (defmacro with-current-buffer (buffer &rest body) |
| 737 | "Execute the forms in BODY with BUFFER as the current buffer. | 737 | "Execute the forms in BODY with BUFFER as the current buffer. |
| 738 | The value returned is the value of the last form in BODY." | 738 | 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." | |||
| 740 | (set-buffer ,buffer) | 740 | (set-buffer ,buffer) |
| 741 | . ,body)) | 741 | . ,body)) |
| 742 | 742 | ||
| 743 | (defmacro with-temp-file (file &rest forms) | ||
| 744 | "Create a new buffer, evaluate FORMS there, and write the buffer to FILE. | ||
| 745 | Return the value of FORMS. | ||
| 746 | If FILE is nil, just evaluate FORMS and don't save anything. | ||
| 747 | If FILE is t, return the buffer contents as a string." | ||
| 748 | (let ((temp-file (make-symbol "temp-file")) | ||
| 749 | (temp-buffer (make-symbol "temp-buffer")) | ||
| 750 | (temp-results (make-symbol "temp-results"))) | ||
| 751 | `(save-excursion | ||
| 752 | (let* ((,temp-file ,file) | ||
| 753 | (default-major-mode 'fundamental-mode) | ||
| 754 | (,temp-buffer | ||
| 755 | (progn | ||
| 756 | (set-buffer | ||
| 757 | (get-buffer-create | ||
| 758 | (generate-new-buffer-name " *temp file*"))) | ||
| 759 | (buffer-disable-undo (current-buffer)) | ||
| 760 | (current-buffer))) | ||
| 761 | ,temp-results) | ||
| 762 | (unwind-protect | ||
| 763 | (progn | ||
| 764 | (setq ,temp-results (progn ,@forms)) | ||
| 765 | (cond | ||
| 766 | ;; Don't save anything. | ||
| 767 | ((null ,temp-file) | ||
| 768 | ,temp-results) | ||
| 769 | ;; Return the buffer contents. | ||
| 770 | ((eq ,temp-file t) | ||
| 771 | (set-buffer ,temp-buffer) | ||
| 772 | (buffer-string)) | ||
| 773 | ;; Save a file. | ||
| 774 | (t | ||
| 775 | (set-buffer ,temp-buffer) | ||
| 776 | ;; Make sure the directory where this file is | ||
| 777 | ;; to be saved exists. | ||
| 778 | (when (not (file-directory-p | ||
| 779 | (file-name-directory ,temp-file))) | ||
| 780 | (make-directory (file-name-directory ,temp-file) t)) | ||
| 781 | ;; Save the file. | ||
| 782 | (write-region (point-min) (point-max) | ||
| 783 | ,temp-file nil 'nomesg) | ||
| 784 | ,temp-results))) | ||
| 785 | ;; Kill the buffer. | ||
| 786 | (when (buffer-name ,temp-buffer) | ||
| 787 | (kill-buffer ,temp-buffer))))))) | ||
| 788 | |||
| 743 | (defmacro with-output-to-string (&rest body) | 789 | (defmacro with-output-to-string (&rest body) |
| 744 | "Execute BODY, return the text it sent to `standard-output', as a string." | 790 | "Execute BODY, return the text it sent to `standard-output', as a string." |
| 745 | `(let ((standard-output (get-buffer-create " *string-output*"))) | 791 | `(let ((standard-output (get-buffer-create " *string-output*"))) |
| @@ -799,7 +845,7 @@ If SEPARATORS is absent, it defaults to \"[ \\f\\t\\n\\r\\v]+\"." | |||
| 799 | (cons (substring string start) | 845 | (cons (substring string start) |
| 800 | list))) | 846 | list))) |
| 801 | (nreverse list))) | 847 | (nreverse list))) |
| 802 | 848 | ||
| 803 | (defun shell-quote-argument (argument) | 849 | (defun shell-quote-argument (argument) |
| 804 | "Quote an argument for passing as argument to an inferior shell." | 850 | "Quote an argument for passing as argument to an inferior shell." |
| 805 | (if (eq system-type 'ms-dos) | 851 | (if (eq system-type 'ms-dos) |