Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 607 lines (545 sloc) 19.908 kb
b7cfe0a @csrhodes Add licence information to swank.R
authored
1 ### This program is free software; you can redistribute it and/or
2 ### modify it under the terms of the GNU General Public Licence as
3 ### published by the Free Software Foundation; either version 2 of the
4 ### Licence, or (at your option) any later version.
5 ###
6 ### This program is distributed in the hope that it will be useful,
7 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
8 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 ### GNU General Public Licence for more details.
10 ###
11 ### A copy of version 2 of the GNU General Public Licence is available
12 ### at <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>; the
13 ### latest version of the GNU General Public Licence is available at
14 ### <http://www.gnu.org/licenses/gpl.txt>.
15
bcf9cc4 @csrhodes include swank.R hacks from today
authored
16 swank <- function(port=4005) {
17 acceptConnections(port, FALSE)
18 }
19
20 startSwank <- function(portFile) {
21 acceptConnections(FALSE, portFile)
22 }
23
24 acceptConnections <- function(port, portFile) {
25 s <- socketConnection(host="localhost", server=TRUE, port=port, open="r+b")
26 on.exit(close(s))
27 serve(s)
28 }
29
30 serve <- function(io) {
31 mainLoop(io)
32 }
33
34 mainLoop <- function(io) {
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
35 slimeConnection <- new.env()
36 slimeConnection$io <- io
e64beb7 @csrhodes rework to support sldb
authored
37 while(TRUE) {
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
38 withRestarts(tryCatch(dispatch(slimeConnection, readPacket(io)),
081a80a @csrhodes put abort restarts around the main and sldb loops
authored
39 swankTopLevel=function(c) NULL),
40 abort="return to SLIME's toplevel")
bcf9cc4 @csrhodes include swank.R hacks from today
authored
41 }
e64beb7 @csrhodes rework to support sldb
authored
42 }
43
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
44 dispatch <- function(slimeConnection, event, sldbState=NULL) {
e64beb7 @csrhodes rework to support sldb
authored
45 str(event)
46 kind <- event[[1]]
47 if(kind == quote(`:emacs-rex`)) {
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
48 do.call("emacsRex", c(list(slimeConnection), list(sldbState), event[-1]))
bcf9cc4 @csrhodes include swank.R hacks from today
authored
49 }
e64beb7 @csrhodes rework to support sldb
authored
50 }
51
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
52 sendToEmacs <- function(slimeConnection, obj) {
53 io <- slimeConnection$io
e64beb7 @csrhodes rework to support sldb
authored
54 str(obj)
55 payload <- writeSexpToString(obj)
56 writeChar(sprintf("%06x", nchar(payload)), io, eos=NULL)
57 writeChar(payload, io, eos=NULL)
58 flush(io)
59 cat(sprintf("%06x", nchar(payload)), payload, sep="")
60 }
61
f5a8e9a @csrhodes implement a mini code-walker to support nested function calls in :ema…
authored
62 callify <- function(form) {
63 ## we implement here the conversion from Lisp S-expression (or list)
64 ## expressions of code into our own, swankr, calling convention,
65 ## with slimeConnection and sldbState as first and second arguments.
66 ## as.call() gets us part of the way, but we need to walk the list
67 ## recursively to mimic CL:EVAL; we need to avoid converting R
68 ## special operators which we are punning (only `quote`, for now)
69 ## into this calling convention.
70 if(is.list(form)) {
71 if(form[[1]] == quote(quote)) {
72 as.call(form)
73 } else {
74 as.call(c(list(form[[1]], quote(slimeConnection), quote(sldbState)), lapply(form[-1], callify)))
75 }
76 } else {
77 form
78 }
79 }
80
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
81 emacsRex <- function(slimeConnection, sldbState, form, pkg, thread, id, level=0) {
e64beb7 @csrhodes rework to support sldb
authored
82 ok <- FALSE
83 value <- NULL
84 tryCatch({
85 withCallingHandlers({
f5a8e9a @csrhodes implement a mini code-walker to support nested function calls in :ema…
authored
86 call <- callify(form)
87 value <- eval(call)
e64beb7 @csrhodes rework to support sldb
authored
88 ok <- TRUE
89 }, error=function(c) {
90 newSldbState <- makeSldbState(c, if(is.null(sldbState)) 0 else sldbState$level+1, id)
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
91 withRestarts(sldbLoop(slimeConnection, newSldbState, id), abort=paste("return to sldb level", newSldbState$level)) })},
92 finally=sendToEmacs(slimeConnection, list(quote(`:return`), if(ok) list(quote(`:ok`), value) else list(quote(`:abort`)), id)))
e64beb7 @csrhodes rework to support sldb
authored
93 }
94
95 makeSldbState <- function(condition, level, id) {
f0e9644 @csrhodes store calls and frames on entry to sldb itself
authored
96 calls <- rev(sys.calls())[-1]
97 frames <- rev(sys.frames())[-1]
081a80a @csrhodes put abort restarts around the main and sldb loops
authored
98 restarts <- rev(computeRestarts(condition))[-1]
99 ret <- list(condition=condition, level=level, id=id, restarts=restarts, calls=calls, frames=frames)
e64beb7 @csrhodes rework to support sldb
authored
100 class(ret) <- c("sldbState", class(ret))
101 ret
102 }
103
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
104 sldbLoop <- function(slimeConnection, sldbState, id) {
19a4e9d @csrhodes return properly from sldbLoop()
authored
105 tryCatch({
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
106 io <- slimeConnection$io
107 sendToEmacs(slimeConnection, c(list(quote(`:debug`), id, sldbState$level), `swank:debugger-info-for-emacs`(slimeConnection, sldbState)))
108 sendToEmacs(slimeConnection, list(quote(`:debug-activate`), id, sldbState$level, FALSE))
19a4e9d @csrhodes return properly from sldbLoop()
authored
109 while(TRUE) {
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
110 dispatch(slimeConnection, readPacket(io), sldbState)
19a4e9d @csrhodes return properly from sldbLoop()
authored
111 }
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
112 }, finally=sendToEmacs(slimeConnection, c(list(quote(`:debug-return`), id, sldbState$level, FALSE))))
bcf9cc4 @csrhodes include swank.R hacks from today
authored
113 }
114
115 readPacket <- function(io) {
ab6021d @csrhodes integrate into R's event loop, possibly
authored
116 socketSelect(list(io))
bcf9cc4 @csrhodes include swank.R hacks from today
authored
117 header <- readChunk(io, 6)
118 len <- strtoi(header, base=16)
119 payload <- readChunk(io, len)
120 readSexpFromString(payload)
121 }
122
123 readChunk <- function(io, len) {
124 buffer <- readChar(io, len)
125 if(nchar(buffer) != len) {
126 stop("short read in readChunk")
127 }
128 buffer
129 }
130
131 readSexpFromString <- function(string) {
132 pos <- 1
133 read <- function() {
134 skipWhitespace()
135 char <- substr(string, pos, pos)
136 switch(char,
137 "("=readList(),
138 "\""=readString(),
139 "'"=readQuote(),
140 {
141 if(pos > nchar(string))
142 stop("EOF during read")
143 obj <- readNumberOrSymbol()
144 if(obj == quote(`.`)) {
145 stop("Consing dot not implemented")
146 }
147 obj
148 })
149 }
150 skipWhitespace <- function() {
151 while(substr(string, pos, pos) %in% c(" ", "\t", "\n")) {
152 pos <<- pos + 1
153 }
154 }
155 readList <- function() {
156 ret <- list()
157 pos <<- pos + 1
158 while(TRUE) {
159 skipWhitespace()
160 char <- substr(string, pos, pos)
161 if(char == ")") {
162 pos <<- pos + 1
163 break
164 } else {
165 obj <- read()
166 if(length(obj) == 1 && obj == quote(`.`)) {
167 stop("Consing dot not implemented")
168 }
169 ret <- c(ret, list(obj))
170 }
171 }
172 ret
173 }
174 readString <- function() {
175 ret <- ""
176 addChar <- function(c) { ret <<- paste(ret, c, sep="") }
177 while(TRUE) {
178 pos <<- pos + 1
179 char <- substr(string, pos, pos)
180 switch(char,
181 "\""={ pos <<- pos + 1; break },
182 "\\"={ pos <<- pos + 1
183 char2 <- substr(string, pos, pos)
184 switch(char2,
185 "\""=addChar(char2),
186 "\\"=addChar(char2),
187 stop("Unrecognized escape character")) },
188 addChar(char))
189 }
190 ret
191 }
192 readNumberOrSymbol <- function() {
193 token <- readToken()
194 if(nchar(token)==0) {
195 stop("End of file reading token")
196 } else if(grepl("^[0-9]+$", token)) {
197 strtoi(token)
198 } else if(grepl("^[0-9]+\\.[0-9]+$", token)) {
199 as.double(token)
200 } else {
201 as.name(token)
202 }
203 }
204 readToken <- function() {
205 token <- ""
206 while(TRUE) {
207 char <- substr(string, pos, pos)
208 if(char == "") {
209 break;
210 } else if(char %in% c(" ", "\n", "\t", "(", ")", "\"", "'")) {
211 break;
212 } else {
213 token <- paste(token, char, sep="")
214 pos <<- pos + 1
215 }
216 }
217 token
218 }
219 read()
220 }
221
222 writeSexpToString <- function(obj) {
223 writeSexpToStringLoop <- function(obj) {
224 switch(typeof(obj),
225 "character"={ string <- paste(string, "\"", gsub("([\"\\])", "\\\\\\1", obj), "\"", sep="") },
226 "list"={ string <- paste(string, "(", sep="")
227 max <- length(obj)
228 if(max > 0) {
229 for(i in 1:max) {
230 string <- paste(string, writeSexpToString(obj[[i]]), sep="")
231 if(i != max) {
232 string <- paste(string, " ", sep="")
233 }
234 }
235 }
236 string <- paste(string, ")", sep="") },
237 "symbol"={ string <- paste(string, as.character(obj), sep="") },
e64beb7 @csrhodes rework to support sldb
authored
238 "logical"={ string <- if(obj) { paste(string, "t", sep="") } else { paste(string, "nil", sep="") }},
bcf9cc4 @csrhodes include swank.R hacks from today
authored
239 "double"={ string <- paste(string, as.character(obj), sep="") },
240 "integer"={ string <- paste(string, as.character(obj), sep="") },
241 stop(paste("can't write object ", obj, sep="")))
242 string
243 }
244 string <- ""
245 writeSexpToStringLoop(obj)
246 }
247
277c935 @csrhodes implement withOutputToString
authored
248 withOutputToString <- function(expr) {
249 call <- substitute(expr)
f96797f @csrhodes The beginnings of a vaguely-useful sldb
authored
250 f <- fifo("")
f3a32de @csrhodes alternative printing for the REPL
authored
251 sink(f)
277c935 @csrhodes implement withOutputToString
authored
252 tryCatch({ tryCatch(eval.parent(call), finally=sink())
253 readLines(f) },
2b8d4ed @csrhodes ensure that the fifo is closed in printToString()
authored
254 finally=close(f))
f96797f @csrhodes The beginnings of a vaguely-useful sldb
authored
255 }
256
277c935 @csrhodes implement withOutputToString
authored
257 printToString <- function(val) {
258 withOutputToString(str(val, indent.str="", list.len=5, max.level=2))
259 }
260
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
261 `swank:connection-info` <- function (slimeConnection, sldbState) {
bcf9cc4 @csrhodes include swank.R hacks from today
authored
262 list(quote(`:pid`), Sys.getpid(),
263 quote(`:package`), list(quote(`:name`), "R", quote(`:prompt`), "R> "),
264 quote(`:lisp-implementation`), list(quote(`:type`), "R",
265 quote(`:name`), "R",
266 quote(`:version`), paste(R.version$major, R.version$minor, sep=".")))
267 }
268
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
269 `swank:swank-require` <- function (slimeConnection, sldbState, contribs) {
2c0d53b @csrhodes A proper implementation of swank:swank-require
authored
270 for(contrib in contribs) {
271 filename <- sprintf("%s.R", as.character(contrib))
272 if(file.exists(filename)) {
273 source(filename, verbose=TRUE)
274 }
275 }
bcf9cc4 @csrhodes include swank.R hacks from today
authored
276 list()
277 }
278
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
279 `swank:create-repl` <- function(slimeConnection, sldbState, env, ...) {
bcf9cc4 @csrhodes include swank.R hacks from today
authored
280 list("R", "R")
281 }
282
e35149b @csrhodes initial implementation of support for REPL presentations
authored
283 sendReplResult <- function(slimeConnection, value) {
284 string <- printToString(value)
285 sendToEmacs(slimeConnection,
286 list(quote(`:write-string`),
287 paste(string, collapse="\n"),
288 quote(`:repl-result`)))
289 }
290
291 sendReplResultFunction <- sendReplResult
292
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
293 `swank:listener-eval` <- function(slimeConnection, sldbState, string) {
e35149b @csrhodes initial implementation of support for REPL presentations
authored
294 string <- gsub("#\\.\\(swank:lookup-presented-object-or-lose([^)]*)\\)", ".(`swank:lookup-presented-object-or-lose`(slimeConnection, sldbState,\\1))", string)
295 expr <- parse(text=string)[[1]]
296 lookedup <- do.call("bquote", list(expr))
297 value <- eval(lookedup, envir = globalenv())
298 sendReplResultFunction(slimeConnection, value)
7683b39 @csrhodes make swank:listener-eval be a bit more like the CL version
authored
299 list()
bcf9cc4 @csrhodes include swank.R hacks from today
authored
300 }
301
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
302 `swank:autodoc` <- function(slimeConnection, sldbState, rawForm, ...) {
bcf9cc4 @csrhodes include swank.R hacks from today
authored
303 "No Arglist Information"
304 }
76be998 @csrhodes implement swank:throw-to-toplevel
authored
305
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
306 `swank:operator-arglist` <- function(slimeConnection, sldbState, op, package) {
f72481f @csrhodes stub for swank:operator-arglist
authored
307 list()
308 }
309
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
310 `swank:throw-to-toplevel` <- function(slimeConnection, sldbState) {
e64beb7 @csrhodes rework to support sldb
authored
311 condition <- simpleCondition("Throw to toplevel")
76be998 @csrhodes implement swank:throw-to-toplevel
authored
312 class(condition) <- c("swankTopLevel", class(condition))
313 signalCondition(condition)
314 }
e64beb7 @csrhodes rework to support sldb
authored
315
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
316 `swank:backtrace` <- function(slimeConnection, sldbState, from=0, to=NULL) {
98e8976 @csrhodes factor out backtraceForEmacs() into `swank:backtrace`
authored
317 calls <- sldbState$calls
318 if(is.null(to)) to <- length(calls)
319 from <- from+1
320 calls <- lapply(calls[from:to],
321 { frameNumber <- from-1;
322 function (x) {
323 ret <- list(frameNumber, paste(format(x), sep="", collapse=" "))
324 frameNumber <<- 1+frameNumber
325 ret
326 }
327 })
328 }
329
330 computeRestartsForEmacs <- function (sldbState) {
331 lapply(sldbState$restarts,
332 function(x) {
333 ## this is all a little bit internalsy
334 restartName <- x[[1]][[1]]
335 description <- restartDescription(x)
336 list(restartName, if(is.null(description)) restartName else description)
337 })
338 }
339
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
340 `swank:debugger-info-for-emacs` <- function(slimeConnection, sldbState, from=0, to=NULL) {
98e8976 @csrhodes factor out backtraceForEmacs() into `swank:backtrace`
authored
341 list(list(as.character(sldbState$condition), sprintf(" [%s]", class(sldbState$condition)[[1]]), FALSE),
342 computeRestartsForEmacs(sldbState),
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
343 `swank:backtrace`(slimeConnection, sldbState, from, to),
98e8976 @csrhodes factor out backtraceForEmacs() into `swank:backtrace`
authored
344 list(sldbState$id))
e64beb7 @csrhodes rework to support sldb
authored
345 }
9950587 @csrhodes support invoking restarts from sldb
authored
346
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
347 `swank:invoke-nth-restart-for-emacs` <- function(slimeConnection, sldbState, level, n) {
9950587 @csrhodes support invoking restarts from sldb
authored
348 if(sldbState$level == level) {
081a80a @csrhodes put abort restarts around the main and sldb loops
authored
349 invokeRestart(sldbState$restarts[[n+1]])
9950587 @csrhodes support invoking restarts from sldb
authored
350 }
351 }
746cf1b @csrhodes implement swank:buffer-first-change
authored
352
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
353 `swank:frame-source-location` <- function(slimeConnection, sldbState, n) {
2d372ae @csrhodes implement swank:frame-source-location
authored
354 call <- sldbState$calls[[n+1]]
355 srcref <- attr(call, "srcref")
356 srcfile <- attr(srcref, "srcfile")
357 if(is.null(srcfile)) {
358 list(quote(`:error`), "no srcfile")
359 } else {
360 list(quote(`:location`),
eb27567 @csrhodes use srcfile$wd when going to source for an sldb frame
authored
361 list(quote(`:file`), sprintf("%s/%s", srcfile$wd, srcfile$filename)),
2d372ae @csrhodes implement swank:frame-source-location
authored
362 list(quote(`:line`), srcref[[1]], srcref[[2]]-1),
363 FALSE)
364 }
365 }
366
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
367 `swank:buffer-first-change` <- function(slimeConnection, sldbState, filename) {
746cf1b @csrhodes implement swank:buffer-first-change
authored
368 FALSE
369 }
f96797f @csrhodes The beginnings of a vaguely-useful sldb
authored
370
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
371 `swank:frame-locals-and-catch-tags` <- function(slimeConnection, sldbState, index) {
f96797f @csrhodes The beginnings of a vaguely-useful sldb
authored
372 str(sldbState$frames)
373 frame <- sldbState$frames[[1+index]]
374 objs <- ls(envir=frame)
375 list(lapply(objs, function(name) { list(quote(`:name`), name,
376 quote(`:id`), 0,
377 quote(`:value`), paste(printToString(eval(parse(text=name), envir=frame)), sep="", collapse="\n")) }),
378 list())
379 }
d572642 @csrhodes implement swank:simple-completions
authored
380
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
381 `swank:simple-completions` <- function(slimeConnection, sldbState, prefix, package) {
d572642 @csrhodes implement swank:simple-completions
authored
382 ## fails multiply if prefix contains regexp metacharacters
383 matches <- apropos(sprintf("^%s", prefix), ignore.case=FALSE)
384 nmatches <- length(matches)
385 if(nmatches == 0) {
386 list(list(), "")
387 } else {
388 longest <- matches[order(nchar(matches))][1]
389 while(length(grep(sprintf("^%s", longest), matches)) < nmatches) {
390 longest <- substr(longest, 1, nchar(longest)-1)
391 }
392 list(as.list(matches), longest)
393 }
394 }
5035036 @csrhodes implement swank:compile-string-for-emacs
authored
395
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
396 `swank:compile-string-for-emacs` <- function(slimeConnection, sldbState, string, buffer, position, filename, policy) {
5035036 @csrhodes implement swank:compile-string-for-emacs
authored
397 # FIXME: I think in parse() here we can use srcref to associate
398 # buffer/filename/position to the objects. Or something.
399 withRestarts({ times <- system.time(eval(parse(text=string), envir = globalenv())) },
400 abort="abort compilation")
401 list(quote(`:compilation-result`), list(), TRUE, times[3])
402 }
574dfdf @csrhodes implement swank:interactive-eval and swank:eval-and-grab-output
authored
403
de85197 @csrhodes implement withRetryRestart
authored
404 withRetryRestart <- function(description, expr) {
405 call <- substitute(expr)
574dfdf @csrhodes implement swank:interactive-eval and swank:eval-and-grab-output
authored
406 retry <- TRUE
407 while(retry) {
408 retry <- FALSE
de85197 @csrhodes implement withRetryRestart
authored
409 withRestarts(eval.parent(call),
410 retry=list(description=description,
411 handler=function() retry <<- TRUE))
574dfdf @csrhodes implement swank:interactive-eval and swank:eval-and-grab-output
authored
412 }
de85197 @csrhodes implement withRetryRestart
authored
413 }
414
415 `swank:interactive-eval` <- function(slimeConnection, sldbState, string) {
416 withRetryRestart("retry SLIME interactive evaluation request",
417 value <- eval(parse(text=string), envir=globalenv()))
574dfdf @csrhodes implement swank:interactive-eval and swank:eval-and-grab-output
authored
418 printToString(value)
419 }
420
1083f53 @csrhodes use an environment holding the Slime I/O connection
authored
421 `swank:eval-and-grab-output` <- function(slimeConnection, sldbState, string) {
de85197 @csrhodes implement withRetryRestart
authored
422 withRetryRestart("retry SLIME interactive evaluation request",
423 { output <-
424 withOutputToString(value <- eval(parse(text=string),
425 envir=globalenv())) })
574dfdf @csrhodes implement swank:interactive-eval and swank:eval-and-grab-output
authored
426 list(output, printToString(value))
427 }
c15f757 @csrhodes implement swank:find-definitions-for-emacs
authored
428
429 `swank:find-definitions-for-emacs` <- function(slimeConnection, sldbState, string) {
430 if(exists(string, envir = globalenv())) {
431 thing <- get(string, envir = globalenv())
432 if(inherits(thing, "function")) {
433 body <- body(thing)
434 srcref <- attr(body, "srcref")
435 srcfile <- attr(body, "srcfile")
436 if(is.null(srcfile)) {
437 list()
438 } else {
439 filename <- get("filename", srcfile)
440 list(list(sprintf("function %s", string),
441 list(quote(`:location`),
442 list(quote(`:file`), sprintf("%s/%s", srcfile$wd, srcfile$filename)),
443 list(quote(`:line`), srcref[[2]][[1]], srcref[[2]][[2]]-1),
444 list())))
445 }
446 } else {
447 list()
448 }
449 } else {
450 list()
451 }
452 }
df9e80e @csrhodes swank:value-for-editing and swank:commit-edited-value
authored
453
454 `swank:value-for-editing` <- function(slimeConnection, sldbState, string) {
455 paste(deparse(eval(parse(text=string), envir = globalenv()), control="all"),
456 collapse="\n", sep="")
457 }
458
459 `swank:commit-edited-value` <- function(slimeConnection, sldbState, string, value) {
460 eval(parse(text=sprintf("%s <- %s", string, value)), envir = globalenv())
461 TRUE
462 }
aa3a57d @csrhodes the beginnings of an inspector
authored
463
464 resetInspector <- function(slimeConnection) {
465 assign("istate", list(), envir=slimeConnection)
466 assign("inspectorHistory", NULL, envir=slimeConnection)
467 }
468
469 `swank:init-inspector` <- function(slimeConnection, sldbState, string) {
470 withRetryRestart("retry SLIME inspection request",
471 { resetInspector(slimeConnection)
472 value <- inspectObject(slimeConnection, eval(parse(text=string), envir=globalenv()))
473 })
474 value
475 }
476
477 inspectObject <- function(slimeConnection, object) {
a2977df @csrhodes slightly more working inspector
authored
478 previous <- slimeConnection$istate
479 slimeConnection$istate <- new.env()
480 slimeConnection$istate$object <- object
481 slimeConnection$istate$previous <- previous
aa3a57d @csrhodes the beginnings of an inspector
authored
482 slimeConnection$istate$content <- emacsInspect(object)
a2977df @csrhodes slightly more working inspector
authored
483 if(!(object %in% slimeConnection$inspectorHistory)) {
aa3a57d @csrhodes the beginnings of an inspector
authored
484 slimeConnection$inspectorHistory <- c(slimeConnection$inspectorHistory, object)
485 }
486 if(!is.null(slimeConnection$istate$previous)) {
487 slimeConnection$istate$previous$`next` <- slimeConnection$istate
488 }
489 istateToElisp(slimeConnection$istate)
490 }
491
492 valuePart <- function(istate, object, string) {
493 list(quote(`:value`),
a8a7e47 @csrhodes fix one multiple-string bug in the inspector
authored
494 if(is.null(string)) paste(printToString(object),collapse=" ") else string,
aa3a57d @csrhodes the beginnings of an inspector
authored
495 assignIndexInParts(object, istate))
496 }
497
498 preparePart <- function(istate, part) {
499 if(is.character(part)) {
500 list(part)
501 } else {
502 switch(as.character(part[[1]]),
503 `:newline` = list("\n"),
504 `:value` = valuePart(istate, part[[2]], part[[3]]),
505 `:line` = list(printToString(part[[2]]), ": ",
506 valuePart(istate, part[[3]], NULL), "\n"))
507 }
508 }
509
510 prepareRange <- function(istate, start, end) {
511 range <- istate$content[start+1:min(end+1, length(istate$content))]
512 ps <- NULL
513 for(part in range) {
514 ps <- c(ps, preparePart(istate, part))
515 }
516 list(ps, if(length(ps)<end-start) { start+length(ps) } else { end+1000 },
517 start, end)
518 }
519
520 assignIndexInParts <- function(object, istate) {
521 ret <- 1+length(istate$parts)
a2977df @csrhodes slightly more working inspector
authored
522 istate$parts <- c(istate$parts, list(object))
aa3a57d @csrhodes the beginnings of an inspector
authored
523 ret
524 }
525
526 istateToElisp <- function(istate) {
527 list(quote(`:title`), deparse(istate$object, control="all", nlines=1),
528 quote(`:id`), assignIndexInParts(istate$object, istate),
529 quote(`:content`), prepareRange(istate, 0, 500))
530 }
531
532 emacsInspect <- function(object) {
533 UseMethod("emacsInspect")
534 }
535
be1f5b0 @csrhodes implement a default method for emacsInspect
authored
536 emacsInspect.default <- function(thing) {
537 c(list(paste("a ", class(thing)[[1]], sep=""), list(quote(`:newline`))))
538 }
539
aa3a57d @csrhodes the beginnings of an inspector
authored
540 emacsInspect.list <- function(list) {
541 c(list("a list", list(quote(`:newline`))),
542 mapply(function(name, value) { list(list(quote(`:line`), name, value)) },
543 names(list), list))
544 }
545
a2977df @csrhodes slightly more working inspector
authored
546 emacsInspect.numeric <- function(numeric) {
547 c(list("a numeric", list(quote(`:newline`))),
548 mapply(function(name, value) { list(list(quote(`:line`), name, value)) },
549 (1:length(numeric)), numeric))
550 }
551
a1e0693 @csrhodes implement `swank:quit-inspector`
authored
552 `swank:quit-inspector` <- function(slimeConnection, sldbState) {
553 resetInspector(slimeConnection)
554 FALSE
555 }
a2977df @csrhodes slightly more working inspector
authored
556
557 `swank:inspector-nth-part` <- function(slimeConnection, sldbState, index) {
558 slimeConnection$istate$parts[[index]]
559 }
560
561 `swank:inspect-nth-part` <- function(slimeConnection, sldbState, index) {
562 object <- `swank:inspector-nth-part`(slimeConnection, sldbState, index)
563 inspectObject(slimeConnection, object)
564 }
0d174fb @csrhodes implement swank:inspector-pop and swank:inspector-next
authored
565
566 `swank:inspector-pop` <- function(slimeConnection, sldbState) {
567 if(!is.null(slimeConnection$istate$previous)) {
568 slimeConnection$istate <- slimeConnection$istate$previous
569 istateToElisp(slimeConnection$istate)
570 } else {
571 FALSE
572 }
573 }
574
575 `swank:inspector-next` <- function(slimeConnection, sldbState) {
576 if(!is.null(slimeConnection$istate$`next`)) {
577 slimeConnection$istate <- slimeConnection$istate$`next`
578 istateToElisp(slimeConnection$istate)
579 } else {
580 FALSE
581 }
582 }
8d84cee @csrhodes implement swank:inspector-eval
authored
583
584 `swank:inspector-eval` <- function(slimeConnection, sldbState, string) {
585 expr <- parse(text=string)[[1]]
586 object <- slimeConnection$istate$object
587 if(inherits(object, "list")|inherits(object, "environment")) {
588 substituted <- substituteDirect(expr, object)
589 eval(substituted, envir=globalenv())
590 } else {
591 eval(expr, envir=globalenv())
592 }
593 }
8cfc9d8 @csrhodes implement swank:inspect-current-condition and swank:inspect-frame-var
authored
594
595 `swank:inspect-current-condition` <- function(slimeConnection, sldbState) {
596 resetInspector(slimeConnection)
597 inspectObject(slimeConnection, sldbState$condition)
598 }
599
600 `swank:inspect-frame-var` <- function(slimeConnection, sldbState, frame, var) {
601 resetInspector(slimeConnection)
602 frame <- sldbState$frames[[1+frame]]
603 name <- ls(envir=frame)[[1+var]]
604 object <- get(name, envir=frame)
605 inspectObject(slimeConnection, object)
606 }
Something went wrong with that request. Please try again.