Re: [code] [textadept] Required Modules

From: Mitchell <m.att.foicica.com>
Date: Tue, 20 Aug 2019 09:17:29 -0400 (EDT)

Hi Qwerky,

Sorry for the late reply.

On Fri, 16 Aug 2019, Qwerky wrote:

> Hi Oliver,
>
> Thanks for the feedback:
>
> On 2019-08-16 00:26, Oliver Jan Krylow wrote:
>> 'package.loaded' is used internally by 'require'.
>> So maybe by reading
>> https://www.lua.org/manual/5.3/manual.html#pdf-require%c2%a0you can come up with
>> a way to adjust it to your needs.
>>
> Yes, I had already read this.  To adjust would require much more
> work/understanding than my simple needs demand.  :-0
>>>
>>> Also, to 'require' a module more than once in different modules, caused an
>>> issue for me, when the module being required inserts an entry into the
>>> main menu, in that the entry gets inserted twice when the module is
>>> required twice.
>>>
>>
>> I am a little surprised here, because it was my understanding that require
>> internally caches once required modules.
>> So the code should only be run once.
>> I might be mistaken, but could you show an example, that demonstrates the
>> issue?
>
> Yes, I agree, and that was my understanding also.  To demonstrate, a very
> simple module, 'test.lua':
>
> -- Test double loading.
>
> local M = {}
>
> local parent_menu = textadept.menu.menubar[_L['_View']]
> local test_menu_position = 3
>
> -- Initialize test on startup.
> events.connect(events.INITIALIZED, function() M.insert_test_menu() end, 1)
>
> -- Load test menu item.
> function M.insert_test_menu()
>   local test_menu = {'_Test Menu Item', function() end}
>   table.insert(parent_menu, test_menu_position, test_menu)
>   keys['cT'] = parent_menu['_Test Menu Item'][2]
> end
>
> return M
>
> This is placed in my /modules/common/ directory, where it is automatically
> required in my user init.lua.  Reset, and so far, so good; one added menu
> item.
>
> Then, place in keys.lua, the following 'pcall', an example of code I was
> testing to avoid an error if the module didn't exist:
>
> -- View.
> local m_view = textadept.menu.menubar[_L['_View']]
> if pcall(require, 'common/test') then
> --  Some test key bindings.
> --  keys['cT'] = m_view['_Test Menu Item'][2]
> else
> end
>
> After reset, 'View' contains two '_Test Menu Item's.

The problem is that you are adding your menu on `events.INITIALIZED`, which fires twice: once on init, and once during reset. The module's code is not being run twice.

Cheers,
Mitchell

-- 
You are subscribed to code.att.foicica.com.
To change subscription settings, send an e-mail to code+help.att.foicica.com.
To unsubscribe, send an e-mail to code+unsubscribe.att.foicica.com.
Received on Tue 20 Aug 2019 - 09:17:29 EDT

This archive was generated by hypermail 2.2.0 : Wed 21 Aug 2019 - 06:37:47 EDT