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) {
20 dispatch <- function(event) {
23 if(kind == quote(`:emacs-rex`)) {
24 do.call("emacsRex", event[-1])
27 sendToEmacs <- function(obj) {
28 payload <- writeSexpToString(obj)
29 writeChar(sprintf("%06x", nchar(payload)), io, eos=NULL)
30 writeChar(payload, io, eos=NULL)
32 cat(sprintf("%06x", nchar(payload)), payload, sep="")
34 emacsRex <- function(form, pkg, thread, id) {
35 value <- do.call(eval(form[[1]]), form[-1])
36 sendToEmacs(list(quote(`:return`), list(quote(`:ok`), value), id))
40 tryCatch(dispatch(readPacket(io)),
45 readPacket <- function(io) {
46 header <- readChunk(io, 6)
47 len <- strtoi(header, base=16)
48 payload <- readChunk(io, len)
49 readSexpFromString(payload)
52 readChunk <- function(io, len) {
53 buffer <- readChar(io, len)
54 if(nchar(buffer) != len) {
55 stop("short read in readChunk")
60 readSexpFromString <- function(string) {
64 char <- substr(string, pos, pos)
70 if(pos > nchar(string))
71 stop("EOF during read")
72 obj <- readNumberOrSymbol()
73 if(obj == quote(`.`)) {
74 stop("Consing dot not implemented")
79 skipWhitespace <- function() {
80 while(substr(string, pos, pos) %in% c(" ", "\t", "\n")) {
84 readList <- function() {
89 char <- substr(string, pos, pos)
95 if(length(obj) == 1 && obj == quote(`.`)) {
96 stop("Consing dot not implemented")
98 ret <- c(ret, list(obj))
103 readString <- function() {
105 addChar <- function(c) { ret <<- paste(ret, c, sep="") }
108 char <- substr(string, pos, pos)
110 "\""={ pos <<- pos + 1; break },
111 "\\"={ pos <<- pos + 1
112 char2 <- substr(string, pos, pos)
116 stop("Unrecognized escape character")) },
121 readNumberOrSymbol <- function() {
123 if(nchar(token)==0) {
124 stop("End of file reading token")
125 } else if(grepl("^[0-9]+$", token)) {
127 } else if(grepl("^[0-9]+\\.[0-9]+$", token)) {
133 readToken <- function() {
136 char <- substr(string, pos, pos)
139 } else if(char %in% c(" ", "\n", "\t", "(", ")", "\"", "'")) {
142 token <- paste(token, char, sep="")
151 writeSexpToString <- function(obj) {
152 writeSexpToStringLoop <- function(obj) {
154 "character"={ string <- paste(string, "\"", gsub("([\"\\])", "\\\\\\1", obj), "\"", sep="") },
155 "list"={ string <- paste(string, "(", sep="")
159 string <- paste(string, writeSexpToString(obj[[i]]), sep="")
161 string <- paste(string, " ", sep="")
165 string <- paste(string, ")", sep="") },
166 "symbol"={ string <- paste(string, as.character(obj), sep="") },
167 "logical"={ if(obj) { paste(string, "t", sep="") } else { paste(string, "nil", sep="") }},
168 "double"={ string <- paste(string, as.character(obj), sep="") },
169 "integer"={ string <- paste(string, as.character(obj), sep="") },
170 stop(paste("can't write object ", obj, sep="")))
174 writeSexpToStringLoop(obj)
177 `swank:connection-info` <- function () {
178 list(quote(`:pid`), Sys.getpid(),
179 quote(`:package`), list(quote(`:name`), "R", quote(`:prompt`), "R> "),
180 quote(`:lisp-implementation`), list(quote(`:type`), "R",
182 quote(`:version`), paste(R.version$major, R.version$minor, sep=".")))
185 `swank:swank-require` <- function (contribs) {
189 `swank:create-repl` <- function(env, ...) {
193 `swank:listener-eval` <- function(string) {
194 val <- eval(parse(text=string))
199 lines <- readLines(f)
200 list(quote(`:values`), paste(lines, collapse="\n"))
203 `swank:autodoc` <- function(rawForm, ...) {
204 "No Arglist Information"