Support ! and ~ prefixes in the lua script, and edit readme.

develop
Alexander Gavrilov 2012-11-12 12:48:17 +04:00
parent 6cf85b4318
commit bd75cad508
3 changed files with 59 additions and 18 deletions

@ -2734,15 +2734,22 @@ the creature.</p>
</div> </div>
<div class="section" id="lua"> <div class="section" id="lua">
<h2><a class="toc-backref" href="#id129">lua</a></h2> <h2><a class="toc-backref" href="#id129">lua</a></h2>
<dl class="docutils"> <p>There are the following ways to invoke this command:</p>
<dt>There are three ways to invoke this command:</dt> <ol class="arabic">
<dd><ol class="first last arabic simple"> <li><p class="first"><tt class="docutils literal">lua</tt> (without any parameters)</p>
<li>without any parameters - starts an interactive lua interpreter</li> <p>This starts an interactive lua interpreter.</p>
<li>-f &quot;filename&quot; or --file &quot;filename&quot; - loads and runs the file indicated by filename</li> </li>
<li>-s [&quot;filename&quot;] or --save [&quot;filename&quot;] - loads and runs the file indicated by filename from save directory. If filename is not supplied it loads &quot;dfhack.lua&quot;</li> <li><p class="first"><tt class="docutils literal">lua <span class="pre">-f</span> &quot;filename&quot;</tt> or <tt class="docutils literal">lua <span class="pre">--file</span> &quot;filename&quot;</tt></p>
<p>This loads and runs the file indicated by filename.</p>
</li>
<li><p class="first"><tt class="docutils literal">lua <span class="pre">-s</span> [&quot;filename&quot;]</tt> or <tt class="docutils literal">lua <span class="pre">--save</span> [&quot;filename&quot;]</tt></p>
<p>This loads and runs the file indicated by filename from the save
directory. If the filename is not supplied, it loads &quot;dfhack.lua&quot;.</p>
</li>
<li><p class="first"><tt class="docutils literal">:lua</tt> <em>lua statement...</em></p>
<p>Parses and executes the lua statement like the interactive interpreter would.</p>
</li>
</ol> </ol>
</dd>
</dl>
</div> </div>
<div class="section" id="embark"> <div class="section" id="embark">
<h2><a class="toc-backref" href="#id130">embark</a></h2> <h2><a class="toc-backref" href="#id130">embark</a></h2>

@ -1922,10 +1922,25 @@ the creature.
lua lua
=== ===
There are three ways to invoke this command:
1. without any parameters - starts an interactive lua interpreter There are the following ways to invoke this command:
2. -f "filename" or --file "filename" - loads and runs the file indicated by filename
3. -s ["filename"] or --save ["filename"] - loads and runs the file indicated by filename from save directory. If filename is not supplied it loads "dfhack.lua" 1. ``lua`` (without any parameters)
This starts an interactive lua interpreter.
2. ``lua -f "filename"`` or ``lua --file "filename"``
This loads and runs the file indicated by filename.
3. ``lua -s ["filename"]`` or ``lua --save ["filename"]``
This loads and runs the file indicated by filename from the save
directory. If the filename is not supplied, it loads "dfhack.lua".
4. ``:lua`` *lua statement...*
Parses and executes the lua statement like the interactive interpreter would.
embark embark
====== ======

@ -1,11 +1,15 @@
-- Execute lua commands interactively or from files.
local args={...} local args={...}
if args[1]=="--file" or args[1]=="-f" then local cmd = args[1]
if cmd=="--file" or cmd=="-f" then
local f,err=loadfile (args[2]) local f,err=loadfile (args[2])
if f==nil then if f==nil then
qerror(err) qerror(err)
end end
dfhack.pcall(f,table.unpack(args,3)) dfhack.pcall(f,table.unpack(args,3))
elseif args[1]=="--save" or args[1]=="-s" then elseif cmd=="--save" or cmd=="-s" then
if df.global.world.cur_savegame.save_dir=="" then if df.global.world.cur_savegame.save_dir=="" then
qerror("Savefile not loaded") qerror("Savefile not loaded")
end end
@ -16,12 +20,27 @@ elseif args[1]=="--save" or args[1]=="-s" then
qerror(err) qerror(err)
end end
dfhack.pcall(f,table.unpack(args,3)) dfhack.pcall(f,table.unpack(args,3))
elseif args[1]~=nil then elseif cmd~=nil then
local f,err=load(args[1],'=(lua command)', 't') -- Support some of the prefixes allowed by dfhack.interpreter
local prefix
if string.match(cmd, "^[~!]") then
prefix = string.sub(cmd, 1, 1)
cmd = 'return '..string.sub(cmd, 2)
end
local f,err=load(cmd,'=(lua command)', 't')
if f==nil then if f==nil then
qerror(err) qerror(err)
end end
dfhack.pcall(f,table.unpack(args,2))
local rv = table.pack(dfhack.safecall(f,table.unpack(args,2)))
if rv[1] and prefix then
print(table.unpack(rv,2,rv.n))
if prefix == '~' then
printall(rv[2])
end
end
else else
dfhack.interpreter("lua","lua.history") dfhack.interpreter("lua","lua.history")
end end