Set '_' to the first result value for both interactive shortcuts.

Makes it behave sort of like a general 'last result' reference.
develop
Alexander Gavrilov 2012-03-30 12:17:09 +04:00
parent e989ca58db
commit 063099e535
1 changed files with 18 additions and 13 deletions

@ -120,10 +120,11 @@ void InterpreterLoop(color_ostream &out)
DFHack::CommandHistory hist;
lua::state s=lua::glua::Get();
string curline;
out.print("Type quit to exit interactive mode.\n"
out.print("Type quit to exit interactive lua interpreter.\n"
"Shortcuts:\n"
" '= foo' => '_1,_2,... = foo'\n"
" '! foo' => 'print(foo)'\n");
" '! foo' => 'print(foo)'\n"
"Both assign the first result to '_'\n");
assert(out.is_console());
Console &con = static_cast<Console&>(out);
int vcnt = 1;
@ -142,7 +143,9 @@ void InterpreterLoop(color_ostream &out)
try
{
if (curline[0] == '=')
char pfix = curline[0];
if (pfix == '=' || pfix == '!')
{
curline = "return " + curline.substr(1);
@ -150,14 +153,22 @@ void InterpreterLoop(color_ostream &out)
s.pcall(0, LUA_MULTRET);
int numret = s.gettop();
for (int i = 1; i <= numret; i++)
if (numret >= 1)
{
if (i == 1)
s.pushvalue(1);
s.setglobal("_");
if (pfix == '!')
{
s.pushvalue(i);
s.setglobal("_");
s.getglobal("print");
s.insert(1);
s.pcall(numret,0);
numret = 0;
}
}
for (int i = 1; i <= numret; i++)
{
std::string name = stl_sprintf("_%d", vcnt++);
s.pushvalue(i);
s.setglobal(name);
@ -168,12 +179,6 @@ void InterpreterLoop(color_ostream &out)
s.pcall(1,0);
}
}
else if (curline[0] == '!')
{
curline = "print(" + curline.substr(1) + ")";
s.loadstring(curline);
s.pcall();
}
else
{
s.loadstring(curline);