1 swank <- function(port=4005) {
2 acceptConnections(port, FALSE)
5 startSwank <- function(portFile) {
6 acceptConnections(FALSE, portFile)
9 acceptConnections <- function(port, portFile) {
10 s <- socketConnection(host="localhost", server=TRUE, port=port, open="r+b")
15 serve <- function(io) {
19 mainLoop <- function(io) {
21 withRestarts(tryCatch(dispatch(io, readPacket(io)),
22 swankTopLevel=function(c) NULL),
23 abort="return to SLIME's toplevel")
27 dispatch <- function(io, event, sldbState=NULL) {
30 if(kind == quote(`:emacs-rex`)) {
31 do.call("emacsRex", c(list(io), list(sldbState), event[-1]))
35 sendToEmacs <- function(io, obj) {
37 payload <- writeSexpToString(obj)
38 writeChar(sprintf("%06x", nchar(payload)), io, eos=NULL)
39 writeChar(payload, io, eos=NULL)
41 cat(sprintf("%06x", nchar(payload)), payload, sep="")
44 emacsRex <- function(io, sldbState, form, pkg, thread, id, level=0) {
49 value <- do.call(eval(form[[1]]), c(list(io), list(sldbState), form[-1]))
51 }, error=function(c) {
52 newSldbState <- makeSldbState(c, if(is.null(sldbState)) 0 else sldbState$level+1, id)
53 withRestarts(sldbLoop(io, newSldbState, id), abort=paste("return to sldb level", newSldbState$level)) })},
54 finally=sendToEmacs(io, list(quote(`:return`), if(ok) list(quote(`:ok`), value) else list(quote(`:abort`)), id)))
57 makeSldbState <- function(condition, level, id) {
58 calls <- rev(sys.calls())[-1]
59 frames <- rev(sys.frames())[-1]
60 restarts <- rev(computeRestarts(condition))[-1]
61 ret <- list(condition=condition, level=level, id=id, restarts=restarts, calls=calls, frames=frames)
62 class(ret) <- c("sldbState", class(ret))
66 sldbLoop <- function(io, sldbState, id) {
68 sendToEmacs(io, c(list(quote(`:debug`), id, sldbState$level), debuggerInfoForEmacs(sldbState)))
69 sendToEmacs(io, list(quote(`:debug-activate`), id, sldbState$level, FALSE))
71 dispatch(io, readPacket(io), sldbState)
73 }, finally=sendToEmacs(io, c(list(quote(`:debug-return`), id, sldbState$level, FALSE))))
76 debuggerInfoForEmacs <- function(sldbState, from=0, to=NULL) {
77 backtraceForEmacs <- function() {
78 calls <- sldbState$calls
79 if(is.null(to)) to <- length(calls)
81 calls <- lapply(calls[from:to], { frameNumber <- from-1;
82 function (x) { ret <- list(frameNumber, paste(format(x), sep="", collapse=" ")); frameNumber <<- 1+frameNumber; ret }})
84 computeRestartsForEmacs <- function () {
85 lapply(sldbState$restarts,
87 ## this is all a little bit internalsy
88 restartName <- x[[1]][[1]]
89 description <- restartDescription(x)
90 list(restartName, if(is.null(description)) restartName else description)
93 list(list(as.character(sldbState$condition), sprintf(" [%s]", class(sldbState$condition)[[1]]), FALSE),
94 computeRestartsForEmacs(),
99 readPacket <- function(io) {
100 header <- readChunk(io, 6)
101 len <- strtoi(header, base=16)
102 payload <- readChunk(io, len)
103 readSexpFromString(payload)
106 readChunk <- function(io, len) {
107 buffer <- readChar(io, len)
108 if(nchar(buffer) != len) {
109 stop("short read in readChunk")
114 readSexpFromString <- function(string) {
118 char <- substr(string, pos, pos)
124 if(pos > nchar(string))
125 stop("EOF during read")
126 obj <- readNumberOrSymbol()
127 if(obj == quote(`.`)) {
128 stop("Consing dot not implemented")
133 skipWhitespace <- function() {
134 while(substr(string, pos, pos) %in% c(" ", "\t", "\n")) {
138 readList <- function() {
143 char <- substr(string, pos, pos)
149 if(length(obj) == 1 && obj == quote(`.`)) {
150 stop("Consing dot not implemented")
152 ret <- c(ret, list(obj))
157 readString <- function() {
159 addChar <- function(c) { ret <<- paste(ret, c, sep="") }
162 char <- substr(string, pos, pos)
164 "\""={ pos <<- pos + 1; break },
165 "\\"={ pos <<- pos + 1
166 char2 <- substr(string, pos, pos)
170 stop("Unrecognized escape character")) },
175 readNumberOrSymbol <- function() {
177 if(nchar(token)==0) {
178 stop("End of file reading token")
179 } else if(grepl("^[0-9]+$", token)) {
181 } else if(grepl("^[0-9]+\\.[0-9]+$", token)) {
187 readToken <- function() {
190 char <- substr(string, pos, pos)
193 } else if(char %in% c(" ", "\n", "\t", "(", ")", "\"", "'")) {
196 token <- paste(token, char, sep="")
205 writeSexpToString <- function(obj) {
206 writeSexpToStringLoop <- function(obj) {
208 "character"={ string <- paste(string, "\"", gsub("([\"\\])", "\\\\\\1", obj), "\"", sep="") },
209 "list"={ string <- paste(string, "(", sep="")
213 string <- paste(string, writeSexpToString(obj[[i]]), sep="")
215 string <- paste(string, " ", sep="")
219 string <- paste(string, ")", sep="") },
220 "symbol"={ string <- paste(string, as.character(obj), sep="") },
221 "logical"={ string <- if(obj) { paste(string, "t", sep="") } else { paste(string, "nil", sep="") }},
222 "double"={ string <- paste(string, as.character(obj), sep="") },
223 "integer"={ string <- paste(string, as.character(obj), sep="") },
224 stop(paste("can't write object ", obj, sep="")))
228 writeSexpToStringLoop(obj)
231 printToString <- function(val) {
239 `swank:connection-info` <- function (io, sldbState) {
240 list(quote(`:pid`), Sys.getpid(),
241 quote(`:package`), list(quote(`:name`), "R", quote(`:prompt`), "R> "),
242 quote(`:lisp-implementation`), list(quote(`:type`), "R",
244 quote(`:version`), paste(R.version$major, R.version$minor, sep=".")))
247 `swank:swank-require` <- function (io, sldbState, contribs) {
251 `swank:create-repl` <- function(io, sldbState, env, ...) {
255 `swank:listener-eval` <- function(io, sldbState, string) {
256 val <- eval(parse(text=string), envir = globalenv())
257 string <- printToString(val)
258 list(quote(`:values`), paste(string, collapse="\n"))
261 `swank:autodoc` <- function(io, sldbState, rawForm, ...) {
262 "No Arglist Information"
265 `swank:operator-arglist` <- function(io, sldbState, op, package) {
269 `swank:throw-to-toplevel` <- function(io, sldbState) {
270 condition <- simpleCondition("Throw to toplevel")
271 class(condition) <- c("swankTopLevel", class(condition))
272 signalCondition(condition)
275 `swank:debugger-info-for-emacs` <- function(io, sldbState, from, to) {
276 debuggerInfoForEmacs(sldbState, from=from, to=to)
279 `swank:invoke-nth-restart-for-emacs` <- function(io, sldbState, level, n) {
280 if(sldbState$level == level) {
281 invokeRestart(sldbState$restarts[[n+1]])
285 `swank:buffer-first-change` <- function(io, sldbState, filename) {
289 `swank:frame-locals-and-catch-tags` <- function(io, sldbState, index) {
290 str(sldbState$frames)
291 frame <- sldbState$frames[[1+index]]
292 objs <- ls(envir=frame)
293 list(lapply(objs, function(name) { list(quote(`:name`), name,
295 quote(`:value`), paste(printToString(eval(parse(text=name), envir=frame)), sep="", collapse="\n")) }),
299 `swank:simple-completions` <- function(io, sldbState, prefix, package) {
300 ## fails multiply if prefix contains regexp metacharacters
301 matches <- apropos(sprintf("^%s", prefix), ignore.case=FALSE)
302 nmatches <- length(matches)
306 longest <- matches[order(nchar(matches))][1]
307 while(length(grep(sprintf("^%s", longest), matches)) < nmatches) {
308 longest <- substr(longest, 1, nchar(longest)-1)
310 list(as.list(matches), longest)