diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2015-09-07 20:47:57 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2015-09-07 20:47:57 -0700 |
commit | b1c2b8641f584ddeaef872ee6425cadca706e97d (patch) | |
tree | 6d01c1623c6368e3a12456e23944892fd5324d7e | |
parent | ac22923c691dfaf40830bedf056ee8948427dced (diff) | |
download | txr-b1c2b8641f584ddeaef872ee6425cadca706e97d.tar.gz txr-b1c2b8641f584ddeaef872ee6425cadca706e97d.tar.bz2 txr-b1c2b8641f584ddeaef872ee6425cadca706e97d.zip |
Load and save repl history.
* parser.c (repl): If a HOME environment variable
exists, then load the .txr_history file from the user's home
directory and save it when exiting.
* sysif.c (getenv_wrap): Static function changed to extern.
* sysif.h (getenv_wrap): Declared.
-rw-r--r-- | parser.c | 14 | ||||
-rw-r--r-- | sysif.c | 2 | ||||
-rw-r--r-- | sysif.h | 1 |
3 files changed, 16 insertions, 1 deletions
@@ -48,6 +48,7 @@ #include "eval.h" #include "stream.h" #include "y.tab.h" +#include "sysif.h" #include "parser.h" #if HAVE_TERMIOS #include "linenoise/linenoise.h" @@ -515,12 +516,18 @@ val repl(val bindings, val in_stream, val out_stream) val result_hash = make_hash(nil, nil, nil); val done = nil; val counter = one; + val home = getenv_wrap(lit("HOME")); + val histfile = if2(home, format(nil, lit("~a/.txr_history"), home, nao)); + char *histfile_u8 = utf8_dup_to(c_str(histfile)); val old_sig_handler = set_sig_handler(num(SIGINT), func_n2(repl_intr)); reg_varl(result_hash_sym, result_hash); lino_set_completion_cb(ls, provide_completions, 0); + if (histfile) + lino_hist_load(ls, histfile_u8); + while (!done) { val prompt = format(nil, lit("~a> "), counter, nao); val prev_counter = counter; @@ -597,9 +604,16 @@ val repl(val bindings, val in_stream, val out_stream) } set_sig_handler(num(SIGINT), old_sig_handler); + + + if (histfile) + lino_hist_save(ls, histfile_u8); + + free(histfile_u8); free(prompt_u8); free(line_u8); lino_free(ls); + gc_hint(histfile); return nil; } @@ -671,7 +671,7 @@ static val pipe_wrap(void) #endif -static val getenv_wrap(val name) +val getenv_wrap(val name) { char *nameu8 = utf8_dup_to(c_str(name)); char *lookup = getenv(nameu8); @@ -32,4 +32,5 @@ extern val dev_s, ino_s, mode_s, nlink_s, uid_s; extern val gid_s, rdev_s, size_s, blksize_s, blocks_s; extern val atime_s, mtime_s, ctime_s; +val getenv_wrap(val name); void sysif_init(void); |