comparison lisp/subr.el @ 1037:3a01f3148bff

[xemacs-hg @ 2002-10-08 03:24:18 by youngs] 2002-09-03 John Paul Wallington <jpw@shootybangbang.com> * subr.el (with-output-to-string): Synch with GNU Emacs 21; avoids leaking temp buffers by killing rather than erasing them, and doesn't execute BODY in temporary `standard-output' buffer.
author youngs
date Tue, 08 Oct 2002 03:24:22 +0000
parents 1b114504fa80
children 1b0339b048ce
comparison
equal deleted inserted replaced
1036:b33a835c21cc 1037:3a01f3148bff
444 ; (setq path-separator (signal 'error (list "\ 444 ; (setq path-separator (signal 'error (list "\
445 ;`path-separator' should be set to a single-character string" 445 ;`path-separator' should be set to a single-character string"
446 ; path-separator)))) 446 ; path-separator))))
447 ; (split-string-by-char path (aref separator 0))) 447 ; (split-string-by-char path (aref separator 0)))
448 448
449 (defmacro with-output-to-string (&rest forms) 449 (defmacro with-output-to-string (&rest body)
450 "Collect output to `standard-output' while evaluating FORMS and return 450 "Execute BODY, return the text it sent to `standard-output', as a string."
451 it as a string." 451 `(let ((standard-output
452 ;; by "William G. Dubuque" <wgd@zurich.ai.mit.edu> w/ mods from Stig 452 (get-buffer-create (generate-new-buffer-name " *string-output*"))))
453 `(with-current-buffer (get-buffer-create 453 (let ((standard-output standard-output))
454 (generate-new-buffer-name " *string-output*")) 454 ,@body)
455 (setq buffer-read-only nil) 455 (with-current-buffer standard-output
456 (buffer-disable-undo (current-buffer)) 456 (prog1
457 (erase-buffer) 457 (buffer-string)
458 (let ((standard-output (current-buffer))) 458 (kill-buffer nil)))))
459 ,@forms)
460 (prog1
461 (buffer-string)
462 (erase-buffer))))
463 459
464 (defmacro with-current-buffer (buffer &rest body) 460 (defmacro with-current-buffer (buffer &rest body)
465 "Temporarily make BUFFER the current buffer and execute the forms in BODY. 461 "Temporarily make BUFFER the current buffer and execute the forms in BODY.
466 The value returned is the value of the last form in BODY. 462 The value returned is the value of the last form in BODY.
467 See also `with-temp-buffer'." 463 See also `with-temp-buffer'."