Returning a value from internal Lua scripts to TCC

Dec 16, 2009
8
0
How does one return a value from internal Lua scripts back to TCC?

I am trying to use Lua scripts for TCC tab completion purposes, but the script invocation seems to return 0 always.

Here is example script:
-- Mercurial (hg) tab completion for Take Command
--
-- Install the script with command:
-- tabcomplete /l C:\..location..\hgcomplete.lua

require "winapi" -- from stevedonovan/winapi
require "os"

command = arg[1]
if command ~= "hg" then
print("NOT hg COMMAND")
return 1
end

print("HG command detected: " .. command)

possible_params = {
"add",
"branch",
"clone",
"commit",
"export",
"fold",
"heads",
"merge",
"status",
"push",
"pull",
"revert",
"rm",
"init" }

winapi.setenv("TABCOMPLETIONRESULT", table.concat(possible_params, " "))
return 0



Running "tabcomplete /l C:\..location..\hgcomplete.lua" and trying to complete "hg" command works, but the script breaks completion for all other commands (since "return 1" seems to be treated as "return 0" by TCC).

I can work-around this by creating a small .btm script and replacing all return statements with os.exit(X) in lua code:
@ECHO OFF
@REM hgcomplete.btm - tabcomplete /l c:\..location..\hgcomplete.btm
@D:\code\3rdparty\lua\lua-5.3.5\src\lua.exe c:\work\lua\tabcomplete_hg\hgcomplete_btm.lua %*
@exit /b %?


But in this case, I am calling external Lua and internal Lua is ignored.

Here is the os.exit() variant of hgcomplete.lua:
require "winapi" -- from stevedonovan/winapi
require "os"

command = arg[1]
if command ~= "hg" then
print("NOT hg COMMAND")
os.exit(1)
end

print("HG command detected: " .. command)

possible_params = {
"add",
"branch",
"clone",
"commit",
"export",
"fold",
"heads",
"merge",
"status",
"push",
"pull",
"revert",
"rm",
"init" }

winapi.setenv("TABCOMPLETIONRESULT", table.concat(possible_params, " "))

os.exit(0)


Also note that os.exit() cannot be called from internal Lua scripts since it will end the complete TCC process!
 

rconn

Administrator
Staff member
May 14, 2008
11,910
133
TCC calls (in the lua.dll; i.e. not our code) lua(argc,argv). That function returns either a 0 for success or a 1 for failure.

If you want to return a value to TCC, you could use an environment variable. Lua is executed in the same process as TCC, so they can share variables.
 
Dec 16, 2009
8
0
Ok, thanks. I was looking for a way to return error value from the script so that the Lua scripts can be used with tabcomplete command.

Passing the data via variables works for my own code, but not for tabcomplete (unless I missed some tabcomplete detail).

My work-around for now is use .btm scripts for tabcomplete and call Lua from .btm scripts (but it isn't fully optimal as there is extra .btm calling step).

Here is another test script:

-- exit_script.lua
if arg[1] == "do_exit_3" then
print("do_exit_3")
os.exit(3)
end
if arg[1] == "do_return_3" then
print("do_return_3")
return 3
end

print("return 0")
return 0


When you run it, it prints:
> which lua
lua is an internal command
> lua exit_script.lua do_return_3 & echo %ERRORLEVEL%
do_return_3
0



It would be nice if the internal Lua scripts could set the ERRORLEVEL based on their return value, but for that you probably need to modify Lua53.dll or change how you call Lua from TCC.