diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2015-03-09 18:57:05 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2015-03-09 18:57:05 -0300 |
commit | 57c0db219b9ee48d951adf68a577b4eec5309e57 (patch) | |
tree | 23afa4ead5703076ba390ab5fbc656911ccd4297 /lua.c | |
parent | 4ba0cb4580969a42748e2f4f617ab16fdcb1d7ca (diff) | |
download | lua-57c0db219b9ee48d951adf68a577b4eec5309e57.tar.gz lua-57c0db219b9ee48d951adf68a577b4eec5309e57.tar.bz2 lua-57c0db219b9ee48d951adf68a577b4eec5309e57.zip |
line history keep lines without added 'return'
Diffstat (limited to 'lua.c')
-rw-r--r-- | lua.c | 19 |
1 files changed, 11 insertions, 8 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lua.c,v 1.221 2014/11/02 19:33:33 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.222 2014/11/11 19:41:27 roberto Exp roberto $ |
3 | ** Lua stand-alone interpreter | 3 | ** Lua stand-alone interpreter |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -80,9 +80,7 @@ | |||
80 | #include <readline/readline.h> | 80 | #include <readline/readline.h> |
81 | #include <readline/history.h> | 81 | #include <readline/history.h> |
82 | #define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) | 82 | #define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) |
83 | #define lua_saveline(L,idx) \ | 83 | #define lua_saveline(L,line) ((void)L, add_history(line)) |
84 | if (lua_rawlen(L,idx) > 0) /* non-empty line? */ \ | ||
85 | add_history(lua_tostring(L, idx)); /* add it to history */ | ||
86 | #define lua_freeline(L,b) ((void)L, free(b)) | 84 | #define lua_freeline(L,b) ((void)L, free(b)) |
87 | 85 | ||
88 | #else /* }{ */ | 86 | #else /* }{ */ |
@@ -90,7 +88,7 @@ | |||
90 | #define lua_readline(L,b,p) \ | 88 | #define lua_readline(L,b,p) \ |
91 | ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \ | 89 | ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \ |
92 | fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */ | 90 | fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */ |
93 | #define lua_saveline(L,idx) { (void)L; (void)idx; } | 91 | #define lua_saveline(L,line) { (void)L; (void)line; } |
94 | #define lua_freeline(L,b) { (void)L; (void)b; } | 92 | #define lua_freeline(L,b) { (void)L; (void)b; } |
95 | 93 | ||
96 | #endif /* } */ | 94 | #endif /* } */ |
@@ -336,8 +334,12 @@ static int addreturn (lua_State *L) { | |||
336 | lua_pushvalue(L, -2); /* duplicate line */ | 334 | lua_pushvalue(L, -2); /* duplicate line */ |
337 | lua_concat(L, 2); /* new line is "return ..." */ | 335 | lua_concat(L, 2); /* new line is "return ..." */ |
338 | line = lua_tolstring(L, -1, &len); | 336 | line = lua_tolstring(L, -1, &len); |
339 | if ((status = luaL_loadbuffer(L, line, len, "=stdin")) == LUA_OK) | 337 | if ((status = luaL_loadbuffer(L, line, len, "=stdin")) == LUA_OK) { |
340 | lua_remove(L, -3); /* remove original line */ | 338 | lua_remove(L, -3); /* remove original line */ |
339 | line += sizeof("return")/sizeof(char); /* remove 'return' for history */ | ||
340 | if (line[0] != '\0') /* non empty? */ | ||
341 | lua_saveline(L, line); /* keep history */ | ||
342 | } | ||
341 | else | 343 | else |
342 | lua_pop(L, 2); /* remove result from 'luaL_loadbuffer' and new line */ | 344 | lua_pop(L, 2); /* remove result from 'luaL_loadbuffer' and new line */ |
343 | return status; | 345 | return status; |
@@ -352,8 +354,10 @@ static int multiline (lua_State *L) { | |||
352 | size_t len; | 354 | size_t len; |
353 | const char *line = lua_tolstring(L, 1, &len); /* get what it has */ | 355 | const char *line = lua_tolstring(L, 1, &len); /* get what it has */ |
354 | int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */ | 356 | int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */ |
355 | if (!incomplete(L, status) || !pushline(L, 0)) | 357 | if (!incomplete(L, status) || !pushline(L, 0)) { |
358 | lua_saveline(L, line); /* keep history */ | ||
356 | return status; /* cannot or should not try to add continuation line */ | 359 | return status; /* cannot or should not try to add continuation line */ |
360 | } | ||
357 | lua_pushliteral(L, "\n"); /* add newline... */ | 361 | lua_pushliteral(L, "\n"); /* add newline... */ |
358 | lua_insert(L, -2); /* ...between the two lines */ | 362 | lua_insert(L, -2); /* ...between the two lines */ |
359 | lua_concat(L, 3); /* join them */ | 363 | lua_concat(L, 3); /* join them */ |
@@ -374,7 +378,6 @@ static int loadline (lua_State *L) { | |||
374 | return -1; /* no input */ | 378 | return -1; /* no input */ |
375 | if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */ | 379 | if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */ |
376 | status = multiline(L); /* try as command, maybe with continuation lines */ | 380 | status = multiline(L); /* try as command, maybe with continuation lines */ |
377 | lua_saveline(L, 1); /* keep history */ | ||
378 | lua_remove(L, 1); /* remove line from the stack */ | 381 | lua_remove(L, 1); /* remove line from the stack */ |
379 | lua_assert(lua_gettop(L) == 1); | 382 | lua_assert(lua_gettop(L) == 1); |
380 | return status; | 383 | return status; |