The thing to look at is the LUA_INIT (or LUA_INIT_5_3, …) environment variable:
When called without option -E, the interpreter checks for an environment variable LUA_INIT_5_3 (or LUA_INIT if the versioned name is not defined) before running any argument. If the variable content has the format @filename, then lua executes the file. Otherwise, lua executes the string itself.
– https://www.lua.org/manual/5.3/manual.html#7
If you have a fixed list of functions, you can simply create a file (e.g. ${HOME}/.lua_init.lua, on Windows maybe try %APPDATA%\something or %USERPROFILE%\something.) Then, put your functions in that file and set one of the LUA_INIT environment variables pointing at that file, prefixing the file path with an '@'. Small example for unixoid OSen:
$ cd # just to ensure that we are in ${HOME}
$ echo "function ping( ) print 'pong' end" >> .lua_init.lua
$ echo 'export LUA_INIT="@${HOME}/.lua_init.lua"' >> .profile
$ source .profile
$ lua
Lua 5.3.3 Copyright (C) 1994-2016 Lua.org, PUC-Rio
> ping()
pong
(For Windows, see the comment by Egor Skriptunoff below.)
If you want to automatically load stuff from the current directory, that will be harder. A simple way would be to set up the above, then add e.g.
-- autoload '.autoload.lua' in current directory if present
if io.open( ".autoload.lua" ) then -- exists, run it
-- use pcall so we don't brick the interpreter if the
-- file contains an error but can continue anyway
local ok, err = pcall( dofile, ".autoload.lua" )
if not ok then print( "AUTOLOAD ERROR: ", err ) end
end
-- GAPING SECURITY HOLE WARNING: automatically running a file
-- with the right name in any folder could run untrusted code.
-- If you actually use this, add a whitelist of known-good
-- project directories or at the very least blacklist your
-- downloads folder, /tmp, and whatever else might end up
-- containing a file with the right name but not written by you.
into the LUA_INIT file. Then, to get project/directory-specific functions to load automatically, create a .autoload.lua that dofiles or requires files as needed, defines functions, ...
Fancier solutions (not requiring an extra file per folder) will be harder to do, but you can run arbitrary Lua code to build whatever you need.