Hrvoje Niksic <hniksic(a)xemacs.org> wrote:
[...]
As for `with-current-buffer', the GNU version uses it to get the
buffer contents, but we can do that with (buffer-string buffer). One
should keep in mind that changing the current buffer is a fairly
expensive operation; it requires, among other things, rebinding all
buffer-local variables to their new values. If the result is
available without changing the current buffer, the alternative method
should be used.
I propose this version, which is similar to FSF's, but with additional
safety of using unwind-protect to guard against temp buffer leakage.
(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 (generate-new-buffer-name " *string-output*"))))
(unwind-protect
(progn
;; LET guards the value of standard-output should BODY
;; change it.
(let ((standard-output standard-output))
,@body)
(buffer-string standard-output))
(kill-buffer standard-output))))
Looks good. Someone should install it.
--
John Paul Wallington