Christophe Weblog Wiki Code Publications Music
give us a chance with utf-8
[swankr.git] / swank.R
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
16 ### KLUDGE: this assumes that we're being sourced with chdir=TRUE.
17 ### (If not, `swank:swank-require` will work under the circumstances
18 ### that it used to work anyway -- i.e. the working directory is the
19 ### swankr directory)
20 swankrPath <- getwd() 
21
22 swank <- function(port=4005) {
23   acceptConnections(port, FALSE)
24 }
25
26 startSwank <- function(portFile) {
27   acceptConnections(4005, portFile)
28 }
29
30 acceptConnections <- function(port, portFile) {
31   if(portFile != FALSE) {
32     f <- file(portFile, open="w+")
33     cat(port, file=f)
34     close(f)
35   }
36   s <- socketConnection(host="localhost", server=TRUE, port=port, open="r+b")
37   on.exit(close(s))
38   serve(s)
39 }
40
41 serve <- function(io) {
42   mainLoop(io)
43 }
44
45 mainLoop <- function(io) {
46   slimeConnection <- new.env()
47   slimeConnection$io <- io
48   while(TRUE) {
49     withRestarts(tryCatch(dispatch(slimeConnection, readPacket(io)),
50                           swankTopLevel=function(c) NULL),
51                  abort="return to SLIME's toplevel")
52   }
53 }
54
55 dispatch <- function(slimeConnection, event, sldbState=NULL) {
56   kind <- event[[1]]
57   if(kind == quote(`:emacs-rex`)) {
58     do.call("emacsRex", c(list(slimeConnection), list(sldbState), event[-1]))
59   }
60 }
61
62 sendToEmacs <- function(slimeConnection, obj) {
63   io <- slimeConnection$io
64   payload <- writeSexpToString(obj)
65   writeChar(sprintf("%06x", nchar(payload, type="bytes")), io, eos=NULL)
66   writeChar(payload, io, eos=NULL)
67   flush(io)
68 }
69
70 callify <- function(form) {
71   ## we implement here the conversion from Lisp S-expression (or list)
72   ## expressions of code into our own, swankr, calling convention,
73   ## with slimeConnection and sldbState as first and second arguments.
74   ## as.call() gets us part of the way, but we need to walk the list
75   ## recursively to mimic CL:EVAL; we need to avoid converting R
76   ## special operators which we are punning (only `quote`, for now)
77   ## into this calling convention.
78   if(is.list(form)) {
79     if(form[[1]] == quote(quote)) {
80       as.call(form)
81     } else {
82       as.call(c(list(form[[1]], quote(slimeConnection), quote(sldbState)), lapply(form[-1], callify)))
83     }
84   } else {
85     form
86   }
87 }
88
89 emacsRex <- function(slimeConnection, sldbState, form, pkg, thread, id, level=0) {
90   ok <- FALSE
91   value <- NULL
92   conn <- textConnection(NULL, open="w")
93   condition <- NULL
94   tryCatch({
95     withCallingHandlers({
96       call <- callify(form)
97       capture.output(value <- eval(call), file=conn)
98       string <- paste(textConnectionValue(conn), sep="", collapse="\n")
99       if(nchar(string) > 0) {
100         sendToEmacs(slimeConnection, list(quote(`:write-string`), string))
101         sendToEmacs(slimeConnection, list(quote(`:write-string`), "\n"))
102       }
103       close(conn)
104       ok <- TRUE
105     }, error=function(c) {
106       condition <<- c
107       string <- paste(textConnectionValue(conn), sep="", collapse="\n")
108       if(nchar(string) > 0) {
109         sendToEmacs(slimeConnection, list(quote(`:write-string`), string))
110         sendToEmacs(slimeConnection, list(quote(`:write-string`), "\n"))
111       }
112       close(conn)
113       newSldbState <- makeSldbState(c, if(is.null(sldbState)) 0 else sldbState$level+1, id)
114       withRestarts(sldbLoop(slimeConnection, newSldbState, id), abort=paste("return to sldb level", newSldbState$level)) })},
115     finally=sendToEmacs(slimeConnection, list(quote(`:return`), if(ok) list(quote(`:ok`), value) else list(quote(`:abort`), as.character(condition)), id)))
116 }
117
118 makeSldbState <- function(condition, level, id) {
119   calls <- rev(sys.calls())[-1]
120   frames <- rev(sys.frames())[-1]
121   restarts <- rev(computeRestarts(condition))[-1]
122   ret <- list(condition=condition, level=level, id=id, restarts=restarts, calls=calls, frames=frames)
123   class(ret) <- c("sldbState", class(ret))
124   ret
125 }
126
127 sldbLoop <- function(slimeConnection, sldbState, id) {
128   tryCatch({
129     io <- slimeConnection$io
130     sendToEmacs(slimeConnection, c(list(quote(`:debug`), id, sldbState$level), `swank:debugger-info-for-emacs`(slimeConnection, sldbState)))
131     sendToEmacs(slimeConnection, list(quote(`:debug-activate`), id, sldbState$level, FALSE))
132     while(TRUE) {
133       dispatch(slimeConnection, readPacket(io), sldbState)
134     }
135   }, finally=sendToEmacs(slimeConnection, c(list(quote(`:debug-return`), id, sldbState$level, FALSE))))
136 }
137
138 readPacket <- function(io) {
139   socketSelect(list(io))
140   header <- readChunk(io, 6)
141   len <- strtoi(header, base=16)
142   payload <- readChunk(io, len)
143   readSexpFromString(payload)
144 }
145
146 readChunk <- function(io, len) {
147   buffer <- readChar(io, len)
148   if(nchar(buffer) != len) {
149     stop("short read in readChunk")
150   }
151   buffer
152 }
153
154 readSexpFromString <- function(string) {
155   pos <- 1
156   read <- function() {
157     skipWhitespace()
158     char <- substr(string, pos, pos)
159     switch(char,
160            "("=readList(),
161            "\""=readString(),
162            "'"=readQuote(),
163            {
164              if(pos > nchar(string))
165                stop("EOF during read")
166              obj <- readNumberOrSymbol()
167              if(obj == quote(`.`)) {
168                stop("Consing dot not implemented")
169              }
170              obj
171            })
172   }
173   skipWhitespace <- function() {
174     while(substr(string, pos, pos) %in% c(" ", "\t", "\n")) {
175       pos <<- pos + 1
176     }
177   }
178   readList <- function() {
179     ret <- list()
180     pos <<- pos + 1
181     while(TRUE) {
182       skipWhitespace()
183       char <- substr(string, pos, pos)
184       if(char == ")") {
185         pos <<- pos + 1
186         break
187       } else {
188         obj <- read()
189         if(length(obj) == 1 && obj == quote(`.`)) {
190           stop("Consing dot not implemented")
191         }
192         ret <- c(ret, list(obj))
193       }
194     }
195     ret
196   }
197   readString <- function() {
198     ret <- ""
199     addChar <- function(c) { ret <<- paste(ret, c, sep="") }
200     while(TRUE) {
201       pos <<- pos + 1
202       char <- substr(string, pos, pos)
203       switch(char,
204              "\""={ pos <<- pos + 1; break },
205              "\\"={ pos <<- pos + 1
206                     char2 <- substr(string, pos, pos)
207                     switch(char2,
208                            "\""=addChar(char2),
209                            "\\"=addChar(char2),
210                            stop("Unrecognized escape character")) },
211              addChar(char))
212     }
213     ret
214   }
215   readNumberOrSymbol <- function() {
216     token <- readToken()
217     if(nchar(token)==0) {
218       stop("End of file reading token")
219     } else if(grepl("^[0-9]+$", token)) {
220       strtoi(token)
221     } else if(grepl("^[0-9]+\\.[0-9]+$", token)) {
222       as.double(token)
223     } else {
224       name <- as.name(token)
225       if(name == quote(t)) {
226         TRUE
227       } else if(name == quote(nil)) {
228         FALSE
229       } else {
230         name
231       }
232     }
233   }
234   readToken <- function() {
235     token <- ""
236     while(TRUE) {
237       char <- substr(string, pos, pos)
238       if(char == "") {
239         break;
240       } else if(char %in% c(" ", "\n", "\t", "(", ")", "\"", "'")) {
241         break;
242       } else {
243         token <- paste(token, char, sep="")
244         pos <<- pos + 1
245       }
246     }
247     token
248   }
249   read()
250 }
251
252 writeSexpToString <- function(obj) {
253   writeSexpToStringLoop <- function(obj) {
254     switch(typeof(obj),
255            "character"={ string <- paste(string, "\"", gsub("([\"\\])", "\\\\\\1", obj), "\"", sep="") },
256            "list"={ string <- paste(string, "(", sep="")
257                     max <- length(obj)
258                     if(max > 0) {
259                       for(i in 1:max) {
260                         string <- paste(string, writeSexpToString(obj[[i]]), sep="")
261                         if(i != max) {
262                           string <- paste(string, " ", sep="")
263                         }
264                       }
265                     }
266                     string <- paste(string, ")", sep="") },
267            "symbol"={ string <- paste(string, as.character(obj), sep="") },
268            "logical"={ string <- if(obj) { paste(string, "t", sep="") } else { paste(string, "nil", sep="") }},
269            "double"={ string <- paste(string, as.character(obj), sep="") },
270            "integer"={ string <- paste(string, as.character(obj), sep="") },
271            stop(paste("can't write object ", obj, sep="")))
272     string
273   }
274   string <- ""
275   writeSexpToStringLoop(obj)
276 }
277
278 prin1ToString <- function(val) {
279   paste(deparse(val, backtick=TRUE, control=c("delayPromises", "keepNA")),
280         sep="", collapse="\n")
281 }
282
283 printToString <- function(val) {
284   paste(capture.output(print(val)), sep="", collapse="\n")
285 }
286
287 `swank:connection-info` <- function (slimeConnection, sldbState) {
288   list(quote(`:pid`), Sys.getpid(),
289        quote(`:package`), list(quote(`:name`), "R", quote(`:prompt`), "R> "),
290        quote(`:encoding`), list(quote(`:coding-systems`), list("utf-8-unix")),
291        quote(`:lisp-implementation`), list(quote(`:type`), "R",
292                                            quote(`:name`), "R",
293                                            quote(`:version`), paste(R.version$major, R.version$minor, sep=".")))
294 }
295
296 `swank:swank-require` <- function (slimeConnection, sldbState, contribs) {
297   for(contrib in contribs) {
298     filename <- sprintf("%s/%s.R", swankrPath, as.character(contrib))
299     if(file.exists(filename)) {
300       source(filename)
301     }
302   }
303   list()
304 }
305
306 `swank:create-repl` <- function(slimeConnection, sldbState, env, ...) {
307   list("R", "R")
308 }
309
310 makeReplResult <- function(value) {
311   string <- printToString(value)
312   list(quote(`:write-string`), string,
313        quote(`:repl-result`))
314 }
315
316 makeReplResultFunction <- makeReplResult
317
318 sendReplResult <- function(slimeConnection, value) {
319   result <- makeReplResultFunction(value)
320   sendToEmacs(slimeConnection, result)
321 }
322
323 sendReplResultFunction <- sendReplResult
324
325 `swank:listener-eval` <- function(slimeConnection, sldbState, string) {
326   ## O how ugly
327   string <- gsub("#\\.\\(swank:lookup-presented-object-or-lose([^)]*)\\)", ".(`swank:lookup-presented-object-or-lose`(slimeConnection, sldbState,\\1))", string)
328   for(expr in parse(text=string)) {
329     expr <- expr
330     ## O maybe this is even uglier
331     lookedup <- do.call("bquote", list(expr))
332     tmp <- withVisible(eval(lookedup, envir = globalenv()))
333     if(tmp$visible) {
334       sendReplResultFunction(slimeConnection, tmp$value)
335     }
336   }
337   list()
338 }
339
340 `swank:autodoc` <- function(slimeConnection, sldbState, rawForm, ...) {
341   "No Arglist Information"
342 }
343
344 `swank:operator-arglist` <- function(slimeConnection, sldbState, op, package) {
345   list()
346 }
347
348 `swank:throw-to-toplevel` <- function(slimeConnection, sldbState) {
349   condition <- simpleCondition("Throw to toplevel")
350   class(condition) <- c("swankTopLevel", class(condition))
351   signalCondition(condition)
352 }
353
354 `swank:backtrace` <- function(slimeConnection, sldbState, from=0, to=NULL) {
355   calls <- sldbState$calls
356   if(is.null(to)) to <- length(calls)
357   from <- from+1
358   calls <- lapply(calls[from:to],
359                   { frameNumber <- from-1;
360                     function (x) {
361                       ret <- list(frameNumber, paste(format(x), sep="", collapse=" "))
362                       frameNumber <<- 1+frameNumber
363                       ret
364                     }
365                   })
366 }
367
368 computeRestartsForEmacs <- function (sldbState) {
369   lapply(sldbState$restarts,
370          function(x) {
371            ## this is all a little bit internalsy
372            restartName <- x[[1]][[1]]
373            description <- restartDescription(x)
374            list(restartName, if(is.null(description)) restartName else description)
375          })
376 }
377
378 `swank:debugger-info-for-emacs` <- function(slimeConnection, sldbState, from=0, to=NULL) {
379   list(list(as.character(sldbState$condition), sprintf("  [%s]", class(sldbState$condition)[[1]]), FALSE),
380        computeRestartsForEmacs(sldbState),
381        `swank:backtrace`(slimeConnection, sldbState, from, to),
382        list(sldbState$id))
383 }
384
385 `swank:invoke-nth-restart-for-emacs` <- function(slimeConnection, sldbState, level, n) {
386   if(sldbState$level == level) {
387     invokeRestart(sldbState$restarts[[n+1]])
388   }
389 }
390
391 `swank:frame-source-location` <- function(slimeConnection, sldbState, n) {
392   call <- sldbState$calls[[n+1]]
393   srcref <- attr(call, "srcref")
394   srcfile <- attr(srcref, "srcfile")
395   if(is.null(srcfile)) {
396     list(quote(`:error`), "no srcfile")
397   } else {
398     filename <- get("filename", srcfile)
399     ## KLUDGE: what this means is "is the srcfile filename
400     ## absolute?"
401     if(substr(filename, 1, 1) == "/") {
402       file <- filename
403     } else {
404       file <- sprintf("%s/%s", srcfile$wd, filename)
405     }
406     list(quote(`:location`),
407          list(quote(`:file`), file),
408          list(quote(`:line`), srcref[[1]], srcref[[2]]-1),
409          FALSE)
410   }
411 }
412
413 `swank:buffer-first-change` <- function(slimeConnection, sldbState, filename) {
414   FALSE
415 }
416
417 `swank:eval-string-in-frame` <- function(slimeConnection, sldbState, string, index) {
418   frame <- sldbState$frames[[1+index]]
419   withRetryRestart("retry SLIME interactive evaluation request",
420                    value <- eval(parse(text=string), envir=frame))
421   printToString(value)
422 }
423
424 `swank:frame-locals-and-catch-tags` <- function(slimeConnection, sldbState, index) {
425   frame <- sldbState$frames[[1+index]]
426   objs <- ls(envir=frame)
427   if(identical(frame, globalenv())) {
428     objs <- c()
429   }
430   list(lapply(objs, function(name) { list(quote(`:name`), name,
431                                           quote(`:id`), 0,
432                                           quote(`:value`),
433                                           tryCatch({
434                                             printToString(eval(parse(text=name), envir=frame))
435                                           }, error=function(c) {
436                                             sprintf("error printing object")
437                                           }))}),
438        list())
439 }
440
441 `swank:simple-completions` <- function(slimeConnection, sldbState, prefix, package) {
442   literal2rx <- function(string) {
443     ## list of ERE metacharacters from ?regexp
444     gsub("([.\\|()[{^$*+?])", "\\\\\\1", string)
445   }
446   matches <- apropos(sprintf("^%s", literal2rx(prefix)), ignore.case=FALSE)
447   nmatches <- length(matches)
448   if(nmatches == 0) {
449     list(list(), "")
450   } else {
451     longest <- matches[order(nchar(matches))][1]
452     while(length(grep(sprintf("^%s", literal2rx(longest)), matches)) < nmatches) {
453       longest <- substr(longest, 1, nchar(longest)-1)
454     }
455     list(as.list(matches), longest)
456   }
457 }
458
459 `swank:compile-string-for-emacs` <- function(slimeConnection, sldbState, string, buffer, position, filename, policy) {
460   lineOffset <- charOffset <- colOffset <- NULL
461   for(pos in position) {
462     switch(as.character(pos[[1]]),
463            `:position` = {charOffset <- pos[[2]]},
464            `:line` = {lineOffset <- pos[[2]]; colOffset <- pos[[3]]},
465            warning("unknown content in pos", pos))
466   }
467   frob <- function(refs) {
468     lapply(refs,
469            function(x)
470            srcref(attr(x,"srcfile"),
471                   c(x[1]+lineOffset-1, ifelse(x[1]==1, x[2]+colOffset-1, x[2]),
472                     x[3]+lineOffset-1, ifelse(x[3]==1, x[4]+colOffset-1, x[4]),
473                     ifelse(x[1]==1, x[5]+colOffset-1, x[5]),
474                     ifelse(x[3]==1, x[6]+colOffset-1, x[6]))))
475   }
476   transformSrcrefs <- function(s) {
477     srcrefs <- attr(s, "srcref")
478     attribs <- attributes(s)
479     new <- 
480       switch(mode(s),
481              "call"=as.call(lapply(s, transformSrcrefs)),
482              "expression"=as.expression(lapply(s, transformSrcrefs)),
483              s)
484     attributes(new) <- attribs
485     if(!is.null(attr(s, "srcref"))) {
486       attr(new, "srcref") <- frob(srcrefs)
487     }
488     new
489   }
490   withRestarts({
491     times <- system.time({
492       exprs <- parse(text=string, srcfile=srcfile(filename))
493       eval(transformSrcrefs(exprs), envir = globalenv()) })},
494                abort="abort compilation")
495   list(quote(`:compilation-result`), list(), TRUE, times[3], FALSE, FALSE)
496 }
497
498 withRetryRestart <- function(description, expr) {
499   call <- substitute(expr)
500   retry <- TRUE
501   while(retry) {
502     retry <- FALSE
503     withRestarts(eval.parent(call),
504                  retry=list(description=description,
505                    handler=function() retry <<- TRUE))
506   }
507 }
508
509 `swank:interactive-eval` <-  function(slimeConnection, sldbState, string) {
510   withRetryRestart("retry SLIME interactive evaluation request",
511                    tmp <- withVisible(eval(parse(text=string), envir=globalenv())))
512   if(tmp$visible) {
513     prin1ToString(tmp$value)
514   } else {
515     "# invisible value"
516   }
517 }
518
519 `swank:eval-and-grab-output` <- function(slimeConnection, sldbState, string) {
520   withRetryRestart("retry SLIME interactive evaluation request",
521                    { output <-
522                        capture.output(tmp <- withVisible(eval(parse(text=string),
523                                                               envir=globalenv()))) })
524   output <- paste(output, sep="", collapse="\n")
525   if(tmp$visible) {
526     list(output, prin1ToString(tmp$value))
527   } else {
528     list(output, "# invisible value")
529   }
530 }
531
532 `swank:interactive-eval-region` <- function(slimeConnection, sldbState, string) {
533   withRetryRestart("retry SLIME interactive evaluation request",
534                    tmp <- withVisible(eval(parse(text=string), envir=globalenv())))
535   if(tmp$visible) {
536     prin1ToString(tmp$value)
537   } else {
538     "# invisible value"
539   }
540 }
541
542 `swank:find-definitions-for-emacs` <- function(slimeConnection, sldbState, string) {
543   if(exists(string, envir = globalenv())) {
544     thing <- get(string, envir = globalenv())
545     if(inherits(thing, "function")) {
546       body <- body(thing)
547       srcref <- attr(body, "srcref")
548       srcfile <- attr(body, "srcfile")
549       if(is.null(srcfile)) {
550         list()
551       } else {
552         filename <- get("filename", srcfile)
553         ## KLUDGE: what this means is "is the srcfile filename
554         ## absolute?"
555         if(substr(filename, 1, 1) == "/") {
556           file <- filename
557         } else {
558           file <- sprintf("%s/%s", srcfile$wd, filename)
559         }
560         list(list(sprintf("function %s", string),
561                   list(quote(`:location`),
562                        list(quote(`:file`), file),
563                        list(quote(`:line`), srcref[[2]][[1]], srcref[[2]][[2]]-1),
564                        list())))
565       }
566     } else {
567       list()
568     }
569   } else {
570     list()
571   }
572 }
573
574 `swank:value-for-editing` <- function(slimeConnection, sldbState, string) {
575   paste(deparse(eval(parse(text=string), envir = globalenv()), control="all"),
576         collapse="\n", sep="")
577 }
578
579 `swank:commit-edited-value` <- function(slimeConnection, sldbState, string, value) {
580   eval(parse(text=sprintf("%s <- %s", string, value)), envir = globalenv())
581   TRUE
582 }
583
584 resetInspector <- function(slimeConnection) {
585   assign("istate", list(), envir=slimeConnection)
586   assign("inspectorHistory", NULL, envir=slimeConnection)
587 }
588
589 `swank:init-inspector` <- function(slimeConnection, sldbState, string) {
590   withRetryRestart("retry SLIME inspection request",
591                    { resetInspector(slimeConnection)
592                      value <- inspectObject(slimeConnection, eval(parse(text=string), envir=globalenv()))
593                    })
594   value
595 }
596
597 inspectObject <- function(slimeConnection, object) {
598   previous <- slimeConnection$istate
599   slimeConnection$istate <- new.env()
600   slimeConnection$istate$object <- object
601   slimeConnection$istate$previous <- previous
602   slimeConnection$istate$content <- emacsInspect(object)
603   if(!(object %in% slimeConnection$inspectorHistory)) {
604     slimeConnection$inspectorHistory <- c(slimeConnection$inspectorHistory, object)
605   }
606   if(!is.null(slimeConnection$istate$previous)) {
607     slimeConnection$istate$previous$`next` <- slimeConnection$istate
608   }
609   istateToElisp(slimeConnection$istate)
610 }
611
612 valuePart <- function(istate, object, string) {
613   list(quote(`:value`),
614        if(is.null(string)) printToString(object) else string,
615        assignIndexInParts(object, istate))
616 }
617
618 preparePart <- function(istate, part) {
619   if(is.character(part)) {
620     list(part)
621   } else {
622     switch(as.character(part[[1]]),
623            `:newline` = list("\n"),
624            `:value` = valuePart(istate, part[[2]], part[[3]]),
625            `:line` = list(printToString(part[[2]]), ": ",
626              valuePart(istate, part[[3]], NULL), "\n"))
627   }
628 }
629
630 prepareRange <- function(istate, start, end) {
631   range <- istate$content[start+1:min(end+1, length(istate$content))]
632   ps <- NULL
633   for(part in range) {
634     ps <- c(ps, preparePart(istate, part))
635   }
636   list(ps, if(length(ps)<end-start) { start+length(ps) } else { end+1000 },
637        start, end)
638 }
639
640 assignIndexInParts <- function(object, istate) {
641   ret <- 1+length(istate$parts)
642   istate$parts <- c(istate$parts, list(object))
643   ret
644 }
645
646 istateToElisp <- function(istate) {
647   list(quote(`:title`), deparse(istate$object, control="all", nlines=1),
648        quote(`:id`), assignIndexInParts(istate$object, istate),
649        quote(`:content`), prepareRange(istate, 0, 500))
650 }
651
652 emacsInspect <- function(object) {
653   UseMethod("emacsInspect")
654 }
655
656 emacsInspect.default <- function(thing) {
657   c(list(paste("a ", class(thing)[[1]], sep=""), list(quote(`:newline`))))
658 }
659
660 emacsInspect.list <- function(list) {
661   c(list("a list", list(quote(`:newline`))),
662     mapply(function(name, value) { list(list(quote(`:line`), name, value)) },
663            names(list), list))
664 }
665
666 emacsInspect.numeric <- function(numeric) {
667   c(list("a numeric", list(quote(`:newline`))),
668     mapply(function(name, value) { list(list(quote(`:line`), name, value)) },
669            (1:length(numeric)), numeric))
670 }
671
672 `swank:quit-inspector` <- function(slimeConnection, sldbState) {
673   resetInspector(slimeConnection)
674   FALSE
675 }
676
677 `swank:inspector-nth-part` <- function(slimeConnection, sldbState, index) {
678   slimeConnection$istate$parts[[index]]
679 }
680
681 `swank:inspect-nth-part` <- function(slimeConnection, sldbState, index) {
682   object <- `swank:inspector-nth-part`(slimeConnection, sldbState, index)
683   inspectObject(slimeConnection, object)
684 }
685
686 `swank:inspector-pop` <- function(slimeConnection, sldbState) {
687   if(!is.null(slimeConnection$istate$previous)) {
688     slimeConnection$istate <- slimeConnection$istate$previous
689     istateToElisp(slimeConnection$istate)
690   } else {
691     FALSE
692   }
693 }
694
695 `swank:inspector-next` <- function(slimeConnection, sldbState) {
696   if(!is.null(slimeConnection$istate$`next`)) {
697     slimeConnection$istate <- slimeConnection$istate$`next`
698     istateToElisp(slimeConnection$istate)
699   } else {
700     FALSE
701   }
702 }
703
704 `swank:inspector-eval` <- function(slimeConnection, sldbState, string) {
705   expr <- parse(text=string)[[1]]
706   object <- slimeConnection$istate$object
707   if(inherits(object, "list")|inherits(object, "environment")) {
708     substituted <- substituteDirect(expr, object)
709     eval(substituted, envir=globalenv())
710   } else {
711     eval(expr, envir=globalenv())
712   }
713 }
714
715 `swank:inspect-current-condition` <- function(slimeConnection, sldbState) {
716   resetInspector(slimeConnection)
717   inspectObject(slimeConnection, sldbState$condition)
718 }
719
720 `swank:inspect-frame-var` <- function(slimeConnection, sldbState, frame, var) {
721   resetInspector(slimeConnection)
722   frame <- sldbState$frames[[1+frame]]
723   name <- ls(envir=frame)[[1+var]]
724   object <- get(name, envir=frame)
725   inspectObject(slimeConnection, object)
726 }
727
728 `swank:default-directory` <- function(slimeConnection, sldbState) {
729   getwd()
730 }
731
732 `swank:set-default-directory` <- function(slimeConnection, sldbState, directory) {
733   setwd(directory)
734   `swank:default-directory`(slimeConnection, sldbState)
735 }
736
737 `swank:load-file` <- function(slimeConnection, sldbState, filename) {
738   source(filename, local=FALSE, keep.source=TRUE)
739   TRUE
740 }
741
742 `swank:compile-file-for-emacs` <- function(slimeConnection, sldbState, filename, loadp, ...) {
743   times <- system.time(parse(filename, srcfile=srcfile(filename)))
744   if(loadp) {
745     ## KLUDGE: inelegant, but works.  It might be more in the spirit
746     ## of things to keep the result of the parse above around to
747     ## evaluate.
748     `swank:load-file`(slimeConnection, sldbState, filename)
749   }
750   list(quote(`:compilation-result`), list(), TRUE, times[3], substitute(loadp), filename)
751 }
752
753 `swank:quit-lisp` <- function(slimeConnection, sldbState) {
754   quit()
755 }