aboutsummaryrefslogtreecommitdiff
path: root/r/readline.r
blob: 79ababcf3ee5ab85a26e6a9694bca3faf4dac91f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
..readline.. <- TRUE

HISTORY_FILE = paste(path.expand("~"), "/.mal-history", sep="")

library(rdyncall, lib.loc="lib/")

#.rllib <- dynfind(c("edit"))
.rllib <- dynfind(c("readline"))
.call_readline <- .dynsym(.rllib,"readline")
.call_add_history <- .dynsym(.rllib,"add_history")

.state <- new.env()
.state$rl_history_loaded = FALSE

.readline <- function(prompt) {
    res <- .dyncall(.call_readline, "Z)p", prompt)
    if (is.nullptr(res)) {
        return(NULL)
    } else {
        return(ptr2str(res))
    }
}

readline <- function(prompt) {
    if (!.state$rl_history_loaded) {
        .state$rl_history_loaded <- TRUE

        lines <- scan(HISTORY_FILE, what="", sep="\n", quiet=TRUE)
        for(add_line in lines) {
            .dyncall(.call_add_history, "Z)v", add_line)
        }
    }

    line <- .readline(prompt)
    if (is.null(line)) return(NULL)
    .dyncall(.call_add_history, "Z)v", line)
    write(line, file=HISTORY_FILE, append=TRUE)

    line
}