summaryrefslogtreecommitdiff
path: root/lua.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2015-03-09 18:57:05 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2015-03-09 18:57:05 -0300
commit57c0db219b9ee48d951adf68a577b4eec5309e57 (patch)
tree23afa4ead5703076ba390ab5fbc656911ccd4297 /lua.c
parent4ba0cb4580969a42748e2f4f617ab16fdcb1d7ca (diff)
downloadlua-github-57c0db219b9ee48d951adf68a577b4eec5309e57.tar.gz
line history keep lines without added 'return'
Diffstat (limited to 'lua.c')
-rw-r--r--lua.c19
1 files changed, 11 insertions, 8 deletions
diff --git a/lua.c b/lua.c
index 40d664f1..a0e04c8e 100644
--- a/lua.c
+++ b/lua.c
@@ -1,5 +1,5 @@
/*
-** $Id: lua.c,v 1.221 2014/11/02 19:33:33 roberto Exp roberto $
+** $Id: lua.c,v 1.222 2014/11/11 19:41:27 roberto Exp roberto $
** Lua stand-alone interpreter
** See Copyright Notice in lua.h
*/
@@ -80,9 +80,7 @@
#include <readline/readline.h>
#include <readline/history.h>
#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL)
-#define lua_saveline(L,idx) \
- if (lua_rawlen(L,idx) > 0) /* non-empty line? */ \
- add_history(lua_tostring(L, idx)); /* add it to history */
+#define lua_saveline(L,line) ((void)L, add_history(line))
#define lua_freeline(L,b) ((void)L, free(b))
#else /* }{ */
@@ -90,7 +88,7 @@
#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_saveline(L,line) { (void)L; (void)line; }
#define lua_freeline(L,b) { (void)L; (void)b; }
#endif /* } */
@@ -336,8 +334,12 @@ static int addreturn (lua_State *L) {
lua_pushvalue(L, -2); /* duplicate line */
lua_concat(L, 2); /* new line is "return ..." */
line = lua_tolstring(L, -1, &len);
- if ((status = luaL_loadbuffer(L, line, len, "=stdin")) == LUA_OK)
+ if ((status = luaL_loadbuffer(L, line, len, "=stdin")) == LUA_OK) {
lua_remove(L, -3); /* remove original line */
+ line += sizeof("return")/sizeof(char); /* remove 'return' for history */
+ if (line[0] != '\0') /* non empty? */
+ lua_saveline(L, line); /* keep history */
+ }
else
lua_pop(L, 2); /* remove result from 'luaL_loadbuffer' and new line */
return status;
@@ -352,8 +354,10 @@ static int multiline (lua_State *L) {
size_t len;
const char *line = lua_tolstring(L, 1, &len); /* get what it has */
int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */
- if (!incomplete(L, status) || !pushline(L, 0))
+ if (!incomplete(L, status) || !pushline(L, 0)) {
+ lua_saveline(L, line); /* keep history */
return status; /* cannot or should not try to add continuation line */
+ }
lua_pushliteral(L, "\n"); /* add newline... */
lua_insert(L, -2); /* ...between the two lines */
lua_concat(L, 3); /* join them */
@@ -374,7 +378,6 @@ static int loadline (lua_State *L) {
return -1; /* no input */
if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */
status = multiline(L); /* try as command, maybe with continuation lines */
- lua_saveline(L, 1); /* keep history */
lua_remove(L, 1); /* remove line from the stack */
lua_assert(lua_gettop(L) == 1);
return status;