diff options
| author | Erik Naggum | 1996-10-03 02:13:52 +0000 |
|---|---|---|
| committer | Erik Naggum | 1996-10-03 02:13:52 +0000 |
| commit | a2fdb55cd3ac5f08a5ef2d5ab1cd1be071152b92 (patch) | |
| tree | 3ccd633efe93df4788bc4ceba25b7cc7ccc0fc3f | |
| parent | fd402b2a64727fee83dbb092d6df3647a6e6f356 (diff) | |
| download | emacs-a2fdb55cd3ac5f08a5ef2d5ab1cd1be071152b92.tar.gz emacs-a2fdb55cd3ac5f08a5ef2d5ab1cd1be071152b92.zip | |
(with-current-buffer): Minor cleanup.
(with-temp-file): Support for arguments nil and t removed.
(with-temp-buffer): Use this new macro instead.
(with-output-to-string): Rewrite.
| -rw-r--r-- | lisp/subr.el | 94 |
1 files changed, 38 insertions, 56 deletions
diff --git a/lisp/subr.el b/lisp/subr.el index 7a5c9c69a6e..fdb5b757b6d 100644 --- a/lisp/subr.el +++ b/lisp/subr.el | |||
| @@ -735,72 +735,54 @@ Wildcards and redirection are handled as usual in the shell." | |||
| 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. |
| 739 | See also `with-temp-buffer'." | ||
| 739 | `(save-current-buffer | 740 | `(save-current-buffer |
| 740 | (set-buffer ,buffer) | 741 | (set-buffer ,buffer) |
| 741 | . ,body)) | 742 | ,@body)) |
| 742 | 743 | ||
| 743 | (defmacro with-temp-file (file &rest forms) | 744 | (defmacro with-temp-file (file &rest forms) |
| 744 | "Create a new buffer, evaluate FORMS there, and write the buffer to FILE. | 745 | "Create a new buffer, evaluate FORMS there, and write the buffer to FILE. |
| 745 | Return the value of FORMS. | 746 | The value of the last form in FORMS is returned, like `progn'. |
| 746 | If FILE is nil, just evaluate FORMS and don't save anything. | 747 | See also `with-temp-buffer'." |
| 747 | If FILE is t, return the buffer contents as a string." | ||
| 748 | (let ((temp-file (make-symbol "temp-file")) | 748 | (let ((temp-file (make-symbol "temp-file")) |
| 749 | (temp-buffer (make-symbol "temp-buffer")) | 749 | (temp-buffer (make-symbol "temp-buffer"))) |
| 750 | (temp-results (make-symbol "temp-results"))) | 750 | `(let ((,temp-file ,file) |
| 751 | `(save-excursion | 751 | (,temp-buffer |
| 752 | (let* ((,temp-file ,file) | 752 | (get-buffer-create (generate-new-buffer-name " *temp file*")))) |
| 753 | (default-major-mode 'fundamental-mode) | 753 | (unwind-protect |
| 754 | (,temp-buffer | 754 | (prog1 |
| 755 | (progn | 755 | (with-current-buffer ,temp-buffer |
| 756 | (set-buffer | 756 | ,@forms) |
| 757 | (get-buffer-create | 757 | (with-current-buffer ,temp-buffer |
| 758 | (generate-new-buffer-name " *temp file*"))) | 758 | (widen) |
| 759 | (buffer-disable-undo (current-buffer)) | 759 | (write-region (point-min) (point-max) ,temp-file nil 0))) |
| 760 | (current-buffer))) | 760 | (and (buffer-name ,temp-buffer) |
| 761 | ,temp-results) | 761 | (kill-buffer ,temp-buffer)))))) |
| 762 | (unwind-protect | 762 | |
| 763 | (progn | 763 | (defmacro with-temp-buffer (&rest forms) |
| 764 | (setq ,temp-results (progn ,@forms)) | 764 | "Create a temporary buffer, and evaluate FORMS there like `progn'. |
| 765 | (cond | 765 | See also `with-temp-file' and `with-output-to-string'." |
| 766 | ;; Don't save anything. | 766 | (let ((temp-buffer (make-symbol "temp-buffer"))) |
| 767 | ((null ,temp-file) | 767 | `(let ((,temp-buffer |
| 768 | ,temp-results) | 768 | (get-buffer-create (generate-new-buffer-name " *temp*")))) |
| 769 | ;; Return the buffer contents. | 769 | (unwind-protect |
| 770 | ((eq ,temp-file t) | 770 | (with-current-buffer ,temp-buffer |
| 771 | (set-buffer ,temp-buffer) | 771 | ,@forms) |
| 772 | (buffer-string)) | 772 | (and (buffer-name ,temp-buffer) |
| 773 | ;; Save a file. | 773 | (kill-buffer ,temp-buffer)))))) |
| 774 | (t | 774 | |
| 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 | |||
| 789 | (defmacro with-output-to-string (&rest body) | 775 | (defmacro with-output-to-string (&rest body) |
| 790 | "Execute BODY, return the text it sent to `standard-output', as a string." | 776 | "Execute BODY, return the text it sent to `standard-output', as a string." |
| 791 | `(let ((standard-output (get-buffer-create " *string-output*"))) | 777 | `(let ((standard-output |
| 792 | (save-excursion | 778 | (get-buffer-create (generate-new-buffer-name " *string-output*")))) |
| 793 | (set-buffer standard-output) | ||
| 794 | (buffer-disable-undo (current-buffer)) | ||
| 795 | (let ((inhibit-read-only t)) | ||
| 796 | (erase-buffer)) | ||
| 797 | (setq buffer-read-only nil)) | ||
| 798 | (let ((standard-output standard-output)) | 779 | (let ((standard-output standard-output)) |
| 799 | ,@body) | 780 | ,@body) |
| 800 | (save-excursion | 781 | (with-current-buffer standard-output |
| 801 | (set-buffer standard-output) | 782 | (prog1 |
| 802 | (buffer-string)))) | 783 | (buffer-string) |
| 803 | 784 | (kill-buffer nil))))) | |
| 785 | |||
| 804 | (defvar save-match-data-internal) | 786 | (defvar save-match-data-internal) |
| 805 | 787 | ||
| 806 | ;; We use save-match-data-internal as the local variable because | 788 | ;; We use save-match-data-internal as the local variable because |