Returning a value from internal Lua scripts to TCC

Dec 16, 2009
9
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
12,367
150
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
9
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.
 
Similar threads
Thread starter Title Forum Replies Date
A v25.00.10 hangs when returning %_ipdnsserver Support 7
MickeyF running program from script and (not) returning to script Support 12
M @Files, @FileSize returning nothing... Support 7
vefatica Setting the default value with @REGSET? Support 1
Fross Deleting Registry DWord Value Support 2
R Problem with @INT[ value] in V26 Support 9
Peter Murschall OPTION //directive=value is a ENDLOCAL Support 3
A How to safely print variable's value without quotes? Support 9
D Envar _tcexit has value, even with /IX startup parameter Support 0
Steve Pitts Maximum value of Buffer Rows Support 7
I Change value of a variable while debugging Support 3
S How to? Monitor shutdown event and write registry value Support 2
G WAD _DOS and VER/r returns incorect value Support 2
D Minor problem. Incorrect value internal var _dir_files Support 7
B Why Does @GETDIR Repeat Selected Directory Name In Returned Value Support 7
samintz How to? Sort on hex value Support 7
T Strange CPU value Support 3
vefatica DO variable left with empty value Support 0
vefatica Is this of any value? Support 2
D Bad CPU value in status bar Support 2
J How to use RegQuery for value with \name Support 2
Charles Dye Create empty registry value? Support 5
fpefpe How to? free (internal) command Support 0
Joe Caverly Documentation Replace internal command with LIBRARY command Support 0
rps For loop internal %_for_files variable? Support 2
Joe Caverly Why does TCCRT return VIEW as an internal command? Support 6
F How to? Disable TCC's internal response to Alt-F4 Support 8
vefatica @EVAL's internal precision? Support 9
jfalch WAD internal "which" should check for FILEEXIST when using AppPaths entry Support 8
P Can't disable internal tar, at least in pipes Support 3
M Keywords for commands, functions, internal variables Support 3
D WAD Internal variable _cpu is undefined Support 2
E View (internal command) does nothing now Support 3
jbanaszczyk Alias for internal command Support 8
Joe Caverly Internal Lua and require Support 0
R Fixed Internal ZIP command fails to process multiple files Support 3
D Internal variable _tctabs within TCExit Support 9
mikea Questions re: Internal ZIP command Support 5
epement no internal help for 'v' in TCmd v14 Support 6
R Override TCC internal commands Support 2
H [4NT8] Internal Start-command and _? variable Support 5
G 4NT 9.02 _shell internal variable Support 50

Similar threads