From 1129de5ac09ac5b41a952021cbc7a9752c8c253f Mon Sep 17 00:00:00 2001
From: Paul Sokolovsky <pfalcon@users.sourceforge.net>
Date: Fri, 13 Mar 2015 09:45:42 +0200
Subject: [PATCH] unix: Support readline history saving to file, improves
 interactive usage.

---
 unix/input.c        | 13 +++++++++++++
 unix/input.h        |  3 ++-
 unix/main.c         |  3 +++
 unix/mpconfigport.h |  1 +
 4 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/unix/input.c b/unix/input.c
index 626138b42..2676b2e22 100644
--- a/unix/input.c
+++ b/unix/input.c
@@ -35,6 +35,7 @@
 #if MICROPY_USE_READLINE
 #include <readline/readline.h>
 #include <readline/history.h>
+#include <readline/tilde.h>
 #endif
 
 char *prompt(char *p) {
@@ -62,6 +63,18 @@ char *prompt(char *p) {
     return line;
 }
 
+void prompt_read_history(void) {
+#if MICROPY_USE_READLINE_HISTORY
+    read_history(tilde_expand("~/.micropython.history"));
+#endif
+}
+
+void prompt_write_history(void) {
+#if MICROPY_USE_READLINE_HISTORY
+    write_history(tilde_expand("~/.micropython.history"));
+#endif
+}
+
 STATIC mp_obj_t mp_builtin_input(uint n_args, const mp_obj_t *args) {
     if (n_args == 1) {
         mp_obj_print(args[0], PRINT_STR);
diff --git a/unix/input.h b/unix/input.h
index 7623a8718..7cbee33c5 100644
--- a/unix/input.h
+++ b/unix/input.h
@@ -1,2 +1,3 @@
 char *prompt(char *p);
-
+void prompt_read_history(void);
+void prompt_write_history(void);
diff --git a/unix/main.c b/unix/main.c
index cf8609d9f..6a142f06c 100644
--- a/unix/main.c
+++ b/unix/main.c
@@ -301,6 +301,8 @@ STATIC void set_sys_argv(char *argv[], int argc, int start_arg) {
 #endif
 
 int main(int argc, char **argv) {
+    prompt_read_history();
+
     mp_stack_set_limit(32768);
 
     pre_process_options(argc, argv);
@@ -477,6 +479,7 @@ int main(int argc, char **argv) {
 #endif
 
     //printf("total bytes = %d\n", m_get_total_bytes_allocated());
+    prompt_write_history();
     return ret & 0xff;
 }
 
diff --git a/unix/mpconfigport.h b/unix/mpconfigport.h
index 3cd9d1593..7eb4d548f 100644
--- a/unix/mpconfigport.h
+++ b/unix/mpconfigport.h
@@ -46,6 +46,7 @@
 #define MICROPY_MALLOC_USES_ALLOCATED_SIZE (1)
 #define MICROPY_MEM_STATS           (1)
 #define MICROPY_DEBUG_PRINTERS      (1)
+#define MICROPY_USE_READLINE_HISTORY (1)
 #define MICROPY_HELPER_REPL         (1)
 #define MICROPY_HELPER_LEXER_UNIX   (1)
 #define MICROPY_ENABLE_SOURCE_LINE  (1)
-- 
GitLab