Re: [code] [textadept] Key module and Menu module

From: Mitchell <m.att.foicica.com>
Date: Thu, 4 Jul 2019 12:30:49 -0400 (EDT)

Hi,

On Wed, 3 Jul 2019, Qwerky wrote:

>
> On 2019-07-03 16:43, Mitchell wrote:
>> Hi,
>>
>> On Wed, 3 Jul 2019, Qwerky wrote:
>>
>>> Thanks.  Presently, menu.lua calls functions directly, and keys.lua
>>> (besides
>>> already calling some functions directly) calls menu entries which then
>>> call
>>> functions.  What if those keys.lua functions simply called the same
>>> functions
>>> which the menu entries call, so that neither keys.lua or menu.lua would
>>> call
>>> the other?
>>
>> This isn't exactly true. *keys.lua* and *menu.lua* both call the same
>> functions. It's something like:
>>
>>   menu = {'foo', function() print('foo') end}
>>   key = menu[2]
>>
>> Both `menu` and `key` use the same exact function value. It's just that
>> `key` references `menu` to get it. From my understanding, you are proposing
>> something like:
>>
>>   function foo() print('foo') end
>>   menu = {'foo', foo}
>>   key = foo
>>
>> The two examples are equivalent, minus a global `foo()` function. I
>> wouldn't know the best place to define `foo()` outside of *menu.lua* in
>> order for *keys.lua* not to need it. (It certainly cannot be a global
>> function.)
>>
>> Sorry if I'm misunderstanding you.
>>
>> Cheers,
>> Mitchell
>
> This is probably due to my insufficient knowledge of Lua!  :-) But I will try
> to explain what I was thinking.
>
> Presently, keys.lua has a line similar to:
>
>   keys[(GUI and 'c~' or 'f1') or 'mf2'] = textadept.bookmarks.toggle
>
> while menu.lua has the line:
>
>       {_L['_Toggle Bookmark'], textadept.bookmarks.toggle},
>
> so they both call textadept.bookmarks.toggle.
>
> menu.lua also has:
>
>       {_L['_Next Bookmark'], function()
>         textadept.bookmarks.goto_mark(true)
>       end},
>
> (Ah, so that's what you meant be defining a function?)

Yes.

> and keys.lua has a line similar to:
>
>   keys.cadown = m_bookmark[_L['_Next Bookmark']][2]    <-- what does [2]
> signify?

`[2]` means fetch the second member of the table. In this case, the first member is the menu item text, and the second item is the function associated with it.

> so I was wondering whether that line in keys.lua could be changed to:
>
>   keys.cadown = function() textadept.bookmarks.goto_mark(true) end
>
> So I see now that the function would be defined twice--would that be a
> problem? 

It would be an inefficiency. Moreover, now that you've reminded me, Textadepts keyboard shortcut display in the menu depends on keys referencing the same exact function the menu does. If you define a new function, even if it has the same contents, it is a completely different function to Lua. So this would break keybindings showing up correctly in menus.

> At least then keys.lua and menu.lua could be edited independently,
> if one is only changing the keys and/or the menu structure--not the content
> of the function.
>
> Also, I don't understand why that line in keys.lua, or the similar line in
> menu.lua require "function() ... end" around
> "textadept.bookmarks.goto_mark(true)", while the similar lines above in
> menu.lua and keys.lua, do not require "function() ... end" around
> "textadept.bookmarks.toggle"?

`textadept.bookmarks.toggle` is a function, so it can be referenced directly in a table by-value. `textadept.bookmarks.goto_mark(true)` is a function call, so putting that directly in a table would only put the return value (nil) in the table, which is not what you want. You want a function to goto a mark, so you need to wrap your function call within a function that can be referenced directly by-value (phew).

> Thanks for your patience.  :-)

Thanks for yours.

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 Thu 04 Jul 2019 - 12:30:49 EDT

This archive was generated by hypermail 2.2.0 : Fri 05 Jul 2019 - 06:36:17 EDT