Re: [code] [textadept] Required Modules

From: Mitchell <>
Date: Wed, 14 Aug 2019 17:06:52 -0400 (EDT)

Hi Qwerky,

On Wed, 14 Aug 2019, Qwerky wrote:

> Hi Mitchell,
> On 2019-08-14 14:24, Mitchell wrote:
>> Hi Qwerky,
>> On Wed, 14 Aug 2019, Qwerky wrote:
>>> Hi.  If the user init.lua requires a module [foo = require('foo')] which
>>> has
>>> a function 'bar', is that module and its function then available to all
>>> other
>>> modules?
>>> So, in keys.lua, can one write 'keys.ab ='?  Or, does 'foo' need
>>> to
>>> be required in keys.lua, as well as every other module which uses
>>> ''?
>> It depends.
>>> From your *~/.textadept/init.lua*, the statement:
>>   foo = require('foo')
>> defines a global variable `foo` that, from this point forward in
>> Textadept's init process, is available to all code that comes after
>> (including required modules). However, any modules loaded prior to that
>> statement (such as *~/.textadept/modules/textadept/keys.lua*) can access
>> global `foo` from within functions (not at top-level file scope).
>> In your case for *keys.lua*, you'd probably need `keys.ab =
>> require('foo').bar` since it's at top-level file scope and Textadept loads
>> that before your *~/.textadept/init.lua*.
>> I hope this makes sense.
>> Cheers,
>> Mitchell
> Yes, it does make sense; thanks for a good explanation.  That fits my
> experience, where keys.lua gave an error on that key binding /unless/ 'foo'
> was required, even though it was required in init.lua.
> What I am trying to accomplish, is key bindings within keys.lua, that do not
> fail with an error when the module containing the function is not loaded;
> they simply do not have any effect.
> So, I tried a global variable within 'foo' (foo_loaded), and then in
> keys.lua:  'keys[foo_loaded and 'ab'] =', so that if 'foo_loaded'
> does not exist, the key binding should do nothing. But this also gave an
> error, even when 'foo' was required in keys.lua.
> And, having to require 'foo' within keys.lua also gives an error when 'foo'
> does not exist; whereas when it is required in init.lua via the
> /modules/common/ directory and code, there is no error when the module isn't
> there.
> So, a way to fail silently when a module doesn't exist, is what I'm looking
> for.
> If the binding 'keys[foo_loaded and 'ab'] =' is within a function
> within keys.lua, then it should work?  So then the question is, how does one
> get a function within a module, to execute when the module is loaded?

You can query `package.loaded['foo']`. It will return a truthy value if the module has been loaded, and `nil` if the module hasn't been loaded.

You can also do `if not pcall(require, 'foo') then ... end` which would catch a require error without propagating it.


You are subscribed to
To change subscription settings, send an e-mail to
To unsubscribe, send an e-mail to
Received on Wed 14 Aug 2019 - 17:06:52 EDT

This archive was generated by hypermail 2.2.0 : Thu 15 Aug 2019 - 06:49:27 EDT