poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 2/5] Save the readline history in ~/.poke_history


From: John Darrington
Subject: [PATCH 2/5] Save the readline history in ~/.poke_history
Date: Mon, 11 Nov 2019 15:00:43 +0100

2019-11-11 John Darrington <address@hidden>

        * src/pk-repl.c (pk_repl): Use ~/.poke_history to persist repl commands.
        * doc/poke.texi (The REPL): Document the use of readline and the 
.poke_history
        file.
---
 doc/poke.texi | 11 +++++++++++
 src/pk-repl.c | 23 +++++++++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/doc/poke.texi b/doc/poke.texi
index 2502d0e..d0d369f 100644
--- a/doc/poke.texi
+++ b/doc/poke.texi
@@ -230,6 +230,17 @@ poke will examine the command, notify the user if there is 
some error
 condition, process the line and maybe displaying something in the
 terminal.
 
+Repeatedly typing complex commands can be tiresome.
+To help you, Poke uses the readline library
+@xref{Top,,,rluserman,GNU Readline Library}.
+This provides shortcuts and simple keystrokes to repeat
+previous commands with or without modification, fast selection of
+filenames and entries from other multiple choice contexts, and
+navigation within a command and among previous commands.
+When the REPL starts, the history of your previous sessions
+are loaded from the file @file{.poke_history} located in your home
+directory (if it exists).
+
 There are several kinds of lines that can be provided in the REPL:
 
 @itemize @bullet
diff --git a/src/pk-repl.c b/src/pk-repl.c
index 22d2e30..1c68ba5 100644
--- a/src/pk-repl.c
+++ b/src/pk-repl.c
@@ -33,6 +33,7 @@
 #endif
 
 #include <signal.h>
+#include <unistd.h>
 
 static void
 banner (void)
@@ -90,6 +91,24 @@ pk_repl (void)
   sigemptyset (&sa.sa_mask);
   sigaction (SIGINT, &sa, 0);
 
+#if defined HAVE_READLINE_HISTORY_H
+  char *poke_history = NULL;
+  /* Load the user's history file ~/.poke_history, if it exists
+     in the HOME directory.  */
+  char *homedir = getenv ("HOME");
+
+  if (homedir != NULL)
+    {
+      poke_history = xmalloc (strlen (homedir)
+                             + strlen ("/.poke_history") + 1);
+      strcpy (poke_history, homedir);
+      strcat (poke_history, "/.poke_history");
+
+      if (access (poke_history, R_OK) == 0)
+       read_history (poke_history);
+    }
+#endif
+
   while (!poke_exit_p)
     {
       int ret;
@@ -118,6 +137,10 @@ pk_repl (void)
         /* Avoid gcc warning here.  */ ;
       free (line);
     }
+#if defined HAVE_READLINE_HISTORY_H
+  if (poke_history)
+    write_history (poke_history);
+#endif
 }
 
 static int saved_point;
-- 
2.11.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]