lua-users home
lua-l archive

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


Attached is a minimal patch to add support for saving the command-line history when using Lua standalone. It is implemented for readline. It is configured by the LUA_HISTORY environment variable, which defaults to .lua_history (or lua_history.txt on Windows).

Some things to consider:

1. A better default might be ~/.lua_history, but the user's home directory isn't used anywhere else in Lua, so I didn't use it. This would also need to be adapted for Windows.

2. I don't check the return value from read_history or write_history. Should the user be warned if either operation fails? Should writing the history not be attempted if reading it failed? In any case, the basic history mechanism can still be used even if the history can't be read or written.

3. According to the GNU history manual, using_history should be called before using history functions. Standalone Lua seems to work fine without this, and still works with my additions, but is there a reason why this function is not called?

--
http://rrt.sc3d.org/ | sad, a.  the efforts of musical debutantes (Bierce)
diff -Nur lua-5.1.2/src/lua.c lua-5.1.2-save-readline-history/src/lua.c
--- lua-5.1.2/src/lua.c	2006-06-02 16:34:00.000000000 +0100
+++ lua-5.1.2-save-readline-history/src/lua.c	2007-11-15 18:43:52.000000000 +0000
@@ -214,6 +214,10 @@
 static void dotty (lua_State *L) {
   int status;
   const char *oldprogname = progname;
+  const char *history_path = getenv(LUA_HISTORY);
+  if (history_path == NULL)  /* no environment variable? */
+    history_path = LUA_HISTORY_DEFAULT;  /* use default */
+  lua_readlinehistory(L, history_path);
   progname = NULL;
   while ((status = loadline(L)) != -1) {
     if (status == 0) status = docall(L, 0, 0);
@@ -230,6 +234,7 @@
   lua_settop(L, 0);  /* clear stack */
   fputs("\n", stdout);
   fflush(stdout);
+  lua_writelinehistory(L, history_path);
   progname = oldprogname;
 }
 
diff -Nur lua-5.1.2/src/luaconf.h lua-5.1.2-save-readline-history/src/luaconf.h
--- lua-5.1.2/src/luaconf.h	2007-03-24 03:01:55.000000000 +0000
+++ lua-5.1.2-save-readline-history/src/luaconf.h	2007-11-15 18:36:41.000000000 +0000
@@ -76,6 +76,8 @@
 @* Lua libraries.
 @@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for
 @* C libraries.
+@@ LUA_HISTORY_DEFAULT is the default path that Lua uses to look for
+@* the command-line history.
 ** CHANGE them if your machine has a non-conventional directory
 ** hierarchy or if you want to install your libraries in
 ** non-conventional directories.
@@ -92,6 +94,8 @@
 		             LUA_CDIR"?.lua;"  LUA_CDIR"?\\init.lua"
 #define LUA_CPATH_DEFAULT \
 	".\\?.dll;"  LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll"
+#define LUA_HISTORY_DEFAULT \
+	"lua_history.txt"
 
 #else
 #define LUA_ROOT	"/usr/local/"
@@ -102,6 +106,8 @@
 		            LUA_CDIR"?.lua;"  LUA_CDIR"?/init.lua"
 #define LUA_CPATH_DEFAULT \
 	"./?.so;"  LUA_CDIR"?.so;" LUA_CDIR"loadall.so"
+#define LUA_HISTORY_DEFAULT \
+	".lua_history"
 #endif
 
 
@@ -263,6 +269,14 @@
 
 
 /*
+@@ LUA_HISTORY is the name of the environment variable that Lua checks
+@* to load its command-line history.
+** CHANGE it if you want a different name.
+*/
+#define LUA_HISTORY     "LUA_HISTORY"
+
+
+/*
 @@ lua_readline defines how to show a prompt and then read a line from
 @* the standard input.
 @@ lua_saveline defines how to "save" a read line in a "history".
@@ -279,12 +293,16 @@
 	if (lua_strlen(L,idx) > 0)  /* non-empty line? */ \
 	  add_history(lua_tostring(L, idx));  /* add it to history */
 #define lua_freeline(L,b)	((void)L, free(b))
+#define lua_readlinehistory(L,f)   ((void)L, (void)read_history(f))
+#define lua_writelinehistory(L,f)  ((void)L, (void)write_history(f))
 #else
 #define lua_readline(L,b,p)	\
 	((void)L, fputs(p, stdout), fflush(stdout),  /* show prompt */ \
 	fgets(b, LUA_MAXINPUT, stdin) != NULL)  /* get line */
 #define lua_saveline(L,idx)	{ (void)L; (void)idx; }
 #define lua_freeline(L,b)	{ (void)L; (void)b; }
+#define lua_readlinehistory(L,f)   { (void)L; (void)f; }
+#define lua_writelinehistory(L,f)  { (void)L; (void)f; }
 #endif
 
 #endif