lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 22 November 2011 16:15, Dimiter "malkia" Stanev <malkia@gmail.com> wrote:
>        Probably posted here already, and there were couple of discussions,
> but I still can't get around writing a simple version for require that works
> with local files.
>
>        For example, my project tree is like this:
>
>        tests/zmq/test_hwm.lua
>
> In this file I'm "requiring" tests/zmq/testutil.lua:
>
>        local testutil = require( "tests/zmq/testutil" )
>
> But this seems very fixed to my project. "testutil.lua" is only used by the
> "tests/zmq" folder, and if the whole folder is moved, or LUA_PATH happens to
> be different, it should still work...
>
>        I've looked at "...", arg, and certain debug. facilities, but I can't
> find something that works always fine from the command line, and (in my
> case) from emacs using (lua-mode).
>
>        One thing with emacs's lua-mode is that I can't get the proper
> lua-file name, but a temporary name, but I guess there is not an easy
> workaround.
>
>        Anyone with better ideas?
>
>        And something that is going to be possible (if not easy) to
> remember... a pattern of some kind...
>
>
>

To get the directory of the current script; I use:
local rel_dir = assert ( debug.getinfo ( 1 , "S" ).source:match (
[=[^@(.-[/\]?)[^/\]*$]=] ) , "Current directory unknown" ) .. "./"

Then in your module, add to package.(c)path:
package.path = rel_dir .. "?.lua;" .. package.path