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 tryCatch(dispatch(io, readPacket(io)),
22 swankTopLevel=function(c) NULL)
26 dispatch <- function(io, event, sldbState=NULL) {
29 if(kind == quote(`:emacs-rex`)) {
30 do.call("emacsRex", c(list(io), list(sldbState), event[-1]))
34 sendToEmacs <- function(io, obj) {
36 payload <- writeSexpToString(obj)
37 writeChar(sprintf("%06x", nchar(payload)), io, eos=NULL)
38 writeChar(payload, io, eos=NULL)
40 cat(sprintf("%06x", nchar(payload)), payload, sep="")
43 emacsRex <- function(io, sldbState, form, pkg, thread, id, level=0) {
48 value <- do.call(eval(form[[1]]), c(list(io), list(sldbState), form[-1]))
50 }, error=function(c) {
51 newSldbState <- makeSldbState(c, if(is.null(sldbState)) 0 else sldbState$level+1, id)
52 sldbLoop(io, newSldbState, id) })},
53 finally=sendToEmacs(io, list(quote(`:return`), if(ok) list(quote(`:ok`), value) else list(quote(`:abort`)), id)))
56 makeSldbState <- function(condition, level, id) {
57 calls <- rev(sys.calls())[-1]
58 frames <- rev(sys.frames())[-1]
59 ret <- list(condition=condition, level=level, id=id, calls=calls, frames=frames)
60 class(ret) <- c("sldbState", class(ret))
64 sldbLoop <- function(io, sldbState, id) {
65 sendToEmacs(io, c(list(quote(`:debug`), id, sldbState$level), debuggerInfoForEmacs(sldbState)))
66 sendToEmacs(io, list(quote(`:debug-activate`), id, sldbState$level, FALSE))
68 dispatch(io, readPacket(io), sldbState)
72 debuggerInfoForEmacs <- function(sldbState, from=0, to=NULL) {
73 backtraceForEmacs <- function() {
74 calls <- sldbState$calls
75 if(is.null(to)) to <- length(calls)
77 calls <- lapply(calls[from:to], { frameNumber <- from-1;
78 function (x) { ret <- list(frameNumber, paste(format(x), sep="", collapse=" ")); frameNumber <<- 1+frameNumber; ret }})
80 computeRestartsForEmacs <- function () {
81 lapply(computeRestarts(sldbState$condition),
83 ## this is all a little bit internalsy
84 restartName <- x[[1]][[1]]
85 description <- restartDescription(x)
86 list(restartName, if(is.null(description)) restartName else description)
89 list(list(as.character(sldbState$condition), sprintf(" [%s]", class(sldbState$condition)[[1]]), FALSE),
90 computeRestartsForEmacs(),
95 readPacket <- function(io) {
96 header <- readChunk(io, 6)
97 len <- strtoi(header, base=16)
98 payload <- readChunk(io, len)
99 readSexpFromString(payload)
102 readChunk <- function(io, len) {
103 buffer <- readChar(io, len)
104 if(nchar(buffer) != len) {
105 stop("short read in readChunk")
110 readSexpFromString <- function(string) {
114 char <- substr(string, pos, pos)
120 if(pos > nchar(string))
121 stop("EOF during read")
122 obj <- readNumberOrSymbol()
123 if(obj == quote(`.`)) {
124 stop("Consing dot not implemented")
129 skipWhitespace <- function() {
130 while(substr(string, pos, pos) %in% c(" ", "\t", "\n")) {
134 readList <- function() {
139 char <- substr(string, pos, pos)
145 if(length(obj) == 1 && obj == quote(`.`)) {
146 stop("Consing dot not implemented")
148 ret <- c(ret, list(obj))
153 readString <- function() {
155 addChar <- function(c) { ret <<- paste(ret, c, sep="") }
158 char <- substr(string, pos, pos)
160 "\""={ pos <<- pos + 1; break },
161 "\\"={ pos <<- pos + 1
162 char2 <- substr(string, pos, pos)
166 stop("Unrecognized escape character")) },
171 readNumberOrSymbol <- function() {
173 if(nchar(token)==0) {
174 stop("End of file reading token")
175 } else if(grepl("^[0-9]+$", token)) {
177 } else if(grepl("^[0-9]+\\.[0-9]+$", token)) {
183 readToken <- function() {
186 char <- substr(string, pos, pos)
189 } else if(char %in% c(" ", "\n", "\t", "(", ")", "\"", "'")) {
192 token <- paste(token, char, sep="")
201 writeSexpToString <- function(obj) {
202 writeSexpToStringLoop <- function(obj) {
204 "character"={ string <- paste(string, "\"", gsub("([\"\\])", "\\\\\\1", obj), "\"", sep="") },
205 "list"={ string <- paste(string, "(", sep="")
209 string <- paste(string, writeSexpToString(obj[[i]]), sep="")
211 string <- paste(string, " ", sep="")
215 string <- paste(string, ")", sep="") },
216 "symbol"={ string <- paste(string, as.character(obj), sep="") },
217 "logical"={ string <- if(obj) { paste(string, "t", sep="") } else { paste(string, "nil", sep="") }},
218 "double"={ string <- paste(string, as.character(obj), sep="") },
219 "integer"={ string <- paste(string, as.character(obj), sep="") },
220 stop(paste("can't write object ", obj, sep="")))
224 writeSexpToStringLoop(obj)
227 `swank:connection-info` <- function (io, sldbState) {
228 list(quote(`:pid`), Sys.getpid(),
229 quote(`:package`), list(quote(`:name`), "R", quote(`:prompt`), "R> "),
230 quote(`:lisp-implementation`), list(quote(`:type`), "R",
232 quote(`:version`), paste(R.version$major, R.version$minor, sep=".")))
235 `swank:swank-require` <- function (io, sldbState, contribs) {
239 `swank:create-repl` <- function(io, sldbState, env, ...) {
243 `swank:listener-eval` <- function(io, sldbState, string) {
244 val <- eval(parse(text=string), envir = globalenv())
249 lines <- readLines(f)
250 list(quote(`:values`), paste(lines, collapse="\n"))
253 `swank:autodoc` <- function(io, sldbState, rawForm, ...) {
254 "No Arglist Information"
257 `swank:throw-to-toplevel` <- function(io, sldbState) {
258 condition <- simpleCondition("Throw to toplevel")
259 class(condition) <- c("swankTopLevel", class(condition))
260 signalCondition(condition)
263 `swank:debugger-info-for-emacs` <- function(io, sldbState, from, to) {
264 debuggerInfoForEmacs(sldbState, from=from, to=to)
267 `swank:invoke-nth-restart-for-emacs` <- function(io, sldbState, level, n) {
268 if(sldbState$level == level) {
269 invokeRestart(computeRestarts()[[n+1]])
273 `swank:buffer-first-change` <- function(io, sldbState, filename) {