+ invokeRestart(sldbState$restarts[[n+1]])
+ }
+}
+
+`swank:frame-source-location` <- function(slimeConnection, sldbState, n) {
+ call <- sldbState$calls[[n+1]]
+ srcref <- attr(call, "srcref")
+ srcfile <- attr(srcref, "srcfile")
+ if(is.null(srcfile)) {
+ list(quote(`:error`), "no srcfile")
+ } else {
+ filename <- get("filename", srcfile)
+ ## KLUDGE: what this means is "is the srcfile filename
+ ## absolute?"
+ if(substr(filename, 1, 1) == "/") {
+ file <- filename
+ } else {
+ file <- sprintf("%s/%s", srcfile$wd, filename)
+ }
+ list(quote(`:location`),
+ list(quote(`:file`), file),
+ list(quote(`:line`), srcref[[1]], srcref[[2]]-1),
+ FALSE)
+ }
+}
+
+`swank:buffer-first-change` <- function(slimeConnection, sldbState, filename) {
+ FALSE
+}
+
+`swank:eval-string-in-frame` <- function(slimeConnection, sldbState, string, index) {
+ frame <- sldbState$frames[[1+index]]
+ withRetryRestart("retry SLIME interactive evaluation request",
+ value <- eval(parse(text=string), envir=frame))
+ printToString(value)
+}
+
+`swank:frame-locals-and-catch-tags` <- function(slimeConnection, sldbState, index) {
+ frame <- sldbState$frames[[1+index]]
+ objs <- ls(envir=frame)
+ if(identical(frame, globalenv())) {
+ objs <- c()
+ }
+ list(lapply(objs, function(name) { list(quote(`:name`), name,
+ quote(`:id`), 0,
+ quote(`:value`),
+ tryCatch({
+ printToString(eval(parse(text=name), envir=frame))
+ }, error=function(c) {
+ sprintf("error printing object")
+ }))}),
+ list())
+}
+
+symbolFieldsCompletion <- function(object, prefix, rest) {
+ ## FIXME: this is hacky, ignoring several syntax issues (use of
+ ## and/or necessity for backquoting identifiers: e.g. fields
+ ## containing hyphens)
+ if((dollar <- regexpr("$", rest, fixed=TRUE)) == -1) {
+ matches <- grep(sprintf("^%s", literal2rx(rest)), names(object), value=TRUE)
+ matches <- sprintf("%s$%s", gsub("\\$[^$]*$", "", prefix), matches)
+ returnMatches(matches)
+ } else {
+ if(exists(substr(rest, 1, dollar-1), object)) {
+ symbolFieldsCompletion(get(substr(rest, 1, dollar-1), object), prefix, substr(rest, dollar+1, nchar(rest)))
+ } else {
+ returnMatches(character(0))
+ }
+ }
+}
+
+returnMatches <- function(matches) {
+ nmatches <- length(matches)
+ if(nmatches == 0) {
+ list(list(), "")
+ } else {
+ longest <- matches[order(nchar(matches))][1]
+ while(length(grep(sprintf("^%s", literal2rx(longest)), matches)) < nmatches) {
+ longest <- substr(longest, 1, nchar(longest)-1)
+ }
+ list(as.list(matches), longest)
+ }
+}
+
+literal2rx <- function(string) {
+ ## list of ERE metacharacters from ?regexp
+ gsub("([.\\|()[{^$*+?])", "\\\\\\1", string)
+}
+
+`swank:simple-completions` <- function(slimeConnection, sldbState, prefix, package) {
+ matches <- apropos(sprintf("^%s", literal2rx(prefix)), ignore.case=FALSE)
+ nmatches <- length(matches)
+ if((nmatches == 0) && ((dollar <- regexpr("$", prefix, fixed=TRUE)) > -1)) {
+ symbolFieldsCompletion(globalenv(), prefix, prefix)
+ } else {
+ returnMatches(matches)
+ }
+}
+
+`swank:compile-string-for-emacs` <- function(slimeConnection, sldbState, string, buffer, position, filename, policy) {
+ lineOffset <- charOffset <- colOffset <- NULL
+ for(pos in position) {
+ switch(as.character(pos[[1]]),
+ `:position` = {charOffset <- pos[[2]]},
+ `:line` = {lineOffset <- pos[[2]]; colOffset <- pos[[3]]},
+ warning("unknown content in pos", pos))
+ }
+ frob <- function(refs) {
+ lapply(refs,
+ function(x)
+ srcref(attr(x,"srcfile"),
+ c(x[1]+lineOffset-1, ifelse(x[1]==1, x[2]+colOffset-1, x[2]),
+ x[3]+lineOffset-1, ifelse(x[3]==1, x[4]+colOffset-1, x[4]),
+ ifelse(x[1]==1, x[5]+colOffset-1, x[5]),
+ ifelse(x[3]==1, x[6]+colOffset-1, x[6]))))
+ }
+ transformSrcrefs <- function(s) {
+ ## horrendous KLUDGE: we need to short-circuit here for "name"
+ ## objects, rather than having a nice uniform behaviour, because
+ ## for expressions of the form x[y,] there is an empty "name"
+ ## which ends up becoming a `missing' object when passed through
+ ## the switch; why, I do not know, but it is then impossible to
+ ## return it, because returning it attempts to evaluate it and
+ ## evaluating it is an error. Fortunately it appears that names
+ ## don't have srcrefs attached.
+ if(mode(s) == "name") {
+ return(s)
+ }
+ if(is(s, "srcref")) {
+ ## more monumental KLUDGE: parsing (in 2.14, at least) appears
+ ## to put srcrefs directly in `length 2' objects, which we need
+ ## to frob directly.
+ return(frob(list(s))[[1]])
+ }
+ srcrefs <- attr(s, "srcref")
+ attribs <- attributes(s)
+ new <-
+ switch(mode(s),
+ "call"=as.call(lapply(s, transformSrcrefs)),
+ "expression"=as.expression(lapply(s, transformSrcrefs)),
+ s)
+ attributes(new) <- attribs
+ if(!is.null(attr(s, "srcref"))) {
+ attr(new, "srcref") <- frob(srcrefs)
+ }
+ if(!is.null(attr(s, "wholeSrcref"))) {
+ attr(new, "wholeSrcref") <- frob(list(attr(s, "wholeSrcref")))[[1]]
+ }
+ new
+ }
+ withRestarts({
+ times <- system.time({
+ exprs <- parse(text=string, srcfile=srcfile(filename))
+ eval(transformSrcrefs(exprs), envir = globalenv()) })},
+ abort="abort compilation")
+ list(quote(`:compilation-result`), list(), TRUE, times[3], FALSE, FALSE)
+}
+
+withRetryRestart <- function(description, expr) {
+ call <- substitute(expr)
+ retry <- TRUE
+ while(retry) {
+ retry <- FALSE
+ withRestarts(eval.parent(call),
+ retry=list(description=description,
+ handler=function() retry <<- TRUE))
+ }
+}
+
+`swank:interactive-eval` <- function(slimeConnection, sldbState, string) {
+ withRetryRestart("retry SLIME interactive evaluation request",
+ tmp <- withVisible(eval(parse(text=string), envir=globalenv())))
+ if(tmp$visible) {
+ prin1ToString(tmp$value)
+ } else {
+ "# invisible value"
+ }
+}
+
+`swank:eval-and-grab-output` <- function(slimeConnection, sldbState, string) {
+ withRetryRestart("retry SLIME interactive evaluation request",
+ { output <-
+ capture.output(tmp <- withVisible(eval(parse(text=string),
+ envir=globalenv()))) })
+ output <- paste(output, sep="", collapse="\n")
+ if(tmp$visible) {
+ list(output, prin1ToString(tmp$value))
+ } else {
+ list(output, "# invisible value")
+ }
+}
+
+`swank:interactive-eval-region` <- function(slimeConnection, sldbState, string) {
+ withRetryRestart("retry SLIME interactive evaluation request",
+ tmp <- withVisible(eval(parse(text=string), envir=globalenv())))
+ if(tmp$visible) {
+ prin1ToString(tmp$value)
+ } else {
+ "# invisible value"
+ }
+}
+
+`swank:find-definitions-for-emacs` <- function(slimeConnection, sldbState, string) {
+ if(exists(string, envir = globalenv())) {
+ thing <- get(string, envir = globalenv())
+ if(inherits(thing, "function")) {
+ body <- body(thing)
+ srcref <- attr(body, "srcref")
+ srcfile <- attr(body, "srcfile")
+ if(is.null(srcfile)) {
+ list()
+ } else {
+ filename <- get("filename", srcfile)
+ ## KLUDGE: what this means is "is the srcfile filename
+ ## absolute?"
+ if(substr(filename, 1, 1) == "/") {
+ file <- filename
+ } else {
+ file <- sprintf("%s/%s", srcfile$wd, filename)
+ }
+ list(list(sprintf("function %s", string),
+ list(quote(`:location`),
+ list(quote(`:file`), file),
+ list(quote(`:line`), srcref[[2]][[1]], srcref[[2]][[2]]-1),
+ list())))
+ }
+ } else {
+ list()
+ }
+ } else {
+ list()
+ }
+}
+
+`swank:value-for-editing` <- function(slimeConnection, sldbState, string) {
+ paste(deparse(eval(parse(text=string), envir = globalenv()), control="all"),
+ collapse="\n", sep="")
+}
+
+`swank:commit-edited-value` <- function(slimeConnection, sldbState, string, value) {
+ eval(parse(text=sprintf("%s <- %s", string, value)), envir = globalenv())
+ TRUE
+}
+
+resetInspector <- function(slimeConnection) {
+ assign("istate", list(), envir=slimeConnection)
+ assign("inspectorHistory", NULL, envir=slimeConnection)
+}
+
+`swank:init-inspector` <- function(slimeConnection, sldbState, string) {
+ withRetryRestart("retry SLIME inspection request",
+ { resetInspector(slimeConnection)
+ value <- inspectObject(slimeConnection, eval(parse(text=string), envir=globalenv()))
+ })
+ value
+}
+
+inspectObject <- function(slimeConnection, object) {
+ vectorify <- function(x) {
+ if(is.vector(x)) {
+ x
+ } else {
+ list(x)
+ }
+ }
+ previous <- slimeConnection$istate
+ slimeConnection$istate <- new.env()
+ slimeConnection$istate$object <- object
+ slimeConnection$istate$previous <- previous
+ slimeConnection$istate$content <- emacsInspect(object)
+ if(!(vectorify(object) %in% slimeConnection$inspectorHistory)) {
+ slimeConnection$inspectorHistory <- c(slimeConnection$inspectorHistory, object)
+ }
+ if(!is.null(slimeConnection$istate$previous)) {
+ slimeConnection$istate$previous$`next` <- slimeConnection$istate
+ }
+ istateToElisp(slimeConnection$istate)
+}
+
+valuePart <- function(istate, object, string) {
+ list(quote(`:value`),
+ if(is.null(string)) prin1ToString(object) else string,
+ assignIndexInParts(object, istate))
+}
+
+preparePart <- function(istate, part) {
+ if(is.character(part)) {
+ list(part)
+ } else {
+ switch(as.character(part[[1]]),
+ `:newline` = list("\n"),
+ `:value` = valuePart(istate, part[[2]], part[[3]]),
+ `:line` = list(prin1ToString(part[[2]]), ": ",
+ valuePart(istate, part[[3]], NULL), "\n"))
+ }
+}
+
+prepareRange <- function(istate, start, end) {
+ range <- istate$content[start+1:min(end+1, length(istate$content))]
+ ps <- NULL
+ for(part in range) {
+ ps <- c(ps, preparePart(istate, part))