Merge pull request #370 from asarhaddon/hide-gensym-counter
[jackhill/mal.git] / r / readline.r
CommitLineData
4d1456b9
JM
1..readline.. <- TRUE
2
9b3362e8
JM
3HISTORY_FILE = paste(path.expand("~"), "/.mal-history", sep="")
4
4d1456b9
JM
5library(rdyncall, lib.loc="lib/")
6
9b3362e8
JM
7#.rllib <- dynfind(c("edit"))
8.rllib <- dynfind(c("readline"))
9.call_readline <- .dynsym(.rllib,"readline")
10.call_add_history <- .dynsym(.rllib,"add_history")
4d1456b9 11
9b3362e8
JM
12.state <- new.env()
13.state$rl_history_loaded = FALSE
14
15.readline <- function(prompt) {
16 res <- .dyncall(.call_readline, "Z)p", prompt)
4d1456b9
JM
17 if (is.nullptr(res)) {
18 return(NULL)
19 } else {
20 return(ptr2str(res))
21 }
22}
9b3362e8
JM
23
24readline <- function(prompt) {
25 if (!.state$rl_history_loaded) {
26 .state$rl_history_loaded <- TRUE
27
c9de2e82
JM
28 if (file.access(HISTORY_FILE, 4) == 0) {
29 lines <- scan(HISTORY_FILE, what="", sep="\n", quiet=TRUE)
30 for(add_line in lines) {
31 .dyncall(.call_add_history, "Z)v", add_line)
32 }
9b3362e8
JM
33 }
34 }
35
36 line <- .readline(prompt)
37 if (is.null(line)) return(NULL)
38 .dyncall(.call_add_history, "Z)v", line)
c9de2e82
JM
39 if (file.access(HISTORY_FILE, 2) == 0) {
40 write(line, file=HISTORY_FILE, append=TRUE)
41 }
9b3362e8
JM
42
43 line
44}