[slime-devel] wishlist feature: swank:make-emacs-buffer-stream

Madhu enometh at meer.net
Wed Feb 3 17:36:14 UTC 2010


* Nathan Bird <4B699950.6080504 at acceleration.net> :
Wrote on Wed, 03 Feb 2010 10:42:08 -0500:

| It would be pretty cool to do something like this:
| (with-open-stream (str (swank:make-emacs-buffer-stream "*app-logger*"))
|     (princ "Woot!"))
|
| I'm thinking things like test-report streams, log streams etc.
|
| Does swank already have something like this?

[Michael Weber gave you a better "correct" answer, but you should be
 aware of what slime already provides:]

First,

;; In Emacs if you:
(setq slime-enable-evaluate-in-emacs t)

;; Then in lisp you can define:
(defun append-to-buffer (string buffer-name)
  (swank::eval-in-emacs
   `(progn
      (with-current-buffer (get-buffer-create ,buffer-name)
	(insert ,string)))))

;; so (append-to-buffer "foo\n" "*app-logger*") will write `foo' to a
;; buffer called *app-logger*.

Next, If this is not sufficient, and you want a "REAL output stream, you
can use the SWANK portablitly layer to open a socket connection to lisp,
and have lisp write to the associated emacs buffer.  Wrapped up in one
function, it may look like this:

(defun swank-make-emacs-output-stream (port buffer-name)
  (let (socket)
    (unwind-protect
	 (progn
	   (setq socket (swank-backend:create-socket
			 swank::*loopback-interface* port))
	   (swank::eval-in-emacs
	    `(progn
	       (open-network-stream "Lisp output buffer"
				    ,buffer-name ,swank::*loopback-interface*
				    ,port)
	       nil))	    ; SLIME BUG: else invalid protocol message
	   ;; return a stream
	   (swank-backend:accept-connection
	    socket :external-format
	    (swank-backend::find-external-format "iso-latin-1")))
      (when socket
	(swank-backend:close-socket socket)))))

;; Then your example would look like

(with-open-stream (stream (swank-make-emacs-output-stream 7008 "*foo*"))
  (write-line "Woot!" stream ))

;; And this would print "Woot" in a buffer called "*foo*" in emacs

;; This is a rough sketch, and could be cleaned up.

--
Madhu





More information about the slime-devel mailing list