Re: [code] keybindings

From: Mitchell <m.att.foicica.com>
Date: Wed, 11 Jan 2017 23:53:04 -0500 (EST)

Hi Joseph,

On Tue, 10 Jan 2017, Joseph McDonald wrote:

>
>
> On 01/10/2017 07:54 AM, Robert Gieseke wrote:
>> Am 09/01/17 um 14:52 schrieb Mitchell:
>>>> Is there a way to bring up a screen that shows all
>>>> keybindings in current context
>>>> and/or a way to show what is bound to
>>>> a keystroke or keystroke sequence (like emacs describe-key)
>>>
>>> No, this feature does not exist. You can use the Key Bindings reference[1]
>>> in conjunction with your *~/.textadept/init.lua*'s key bindings.
>>>
>>
>> Not exactly what you asked for but the Tools/Select Command
>> (Meta-Shift-C in the terminal version) is useful for finding commands
>> and their short cuts.
>
> Wow that's fabulous! I wonder if it could be tweaked
> to also search the "Key Command" as well, so typing
> "mC" would show "Tools: Select Command" (on curses).

You can copy the `M.select_command()` function from
*modules/textadept/menu.lua* into your *~/.textadept/init.lua* with a
couple of small tweaks (please forgive any bad formatting):

     local function select_command()
       local items, commands = {}, {}
       local key_shortcuts = {}
       for key, f in pairs(keys) do key_shortcuts[tostring(f)] = key end
       -- Builds the item and commands tables for the filtered list dialog.
       -- @param menu The menu to read from.
       local function build_command_tables(menu)
         for i = 1, #menu do
           if menu[i].title then
             build_command_tables(menu[i])
           elseif menu[i][1] ~= '' then
             local label = menu.title and menu.title..': '..menu[i][1] or menu[i][1]
             items[#items + 1] = label:gsub('_([^_])', '%1')
             items[#items + 1] = key_shortcuts[tostring(menu[i][2])] or ''
             commands[#commands + 1] = menu[i][2]
           end
         end
       end
       build_command_tables(getmetatable(textadept.menu.menubar).menu)
       local button, i = ui.dialogs.filteredlist{
         title = _L['Run Command'], columns = {_L['Command'], _L['Key Binding']},
         items = items, width = CURSES and ui.size[1] - 2 or nil,
         search_column = 2
       }
       if button ~= 1 or not i then return end
       assert(type(commands[i]) == 'function',
              _L['Unknown command:']..' '..tostring(commands[i]))
       commands[i]()
     end

Note the "search_column = 2" in the `ui.dialogs.filteredlist()` call --
that changes the search column for typed text to key bindings. Now either
bind a key to your new `select_command()` function, or replace the
existing menu item:

     textadept.menu.menubar[_L['_Tools']][_L['Select Co_mmand']][2] = select_command

> Also would be great if it gave the actual function
> name to aid people in tweaking their init.lua, for
> instance I'd like to set it to cE in curses as well
> to match the GUI version but don't know the function
> name.

Lua doesn't allow for an easy way to reference functions by name. Instead
you can reference functions bound to keys by their key bindings:

     keys.ce = keys.mC

(In curses, M-C is bound to "select command" by default, so the above
binds ^E to "select command" too. Note that `keys.cE` will not work in
curses since Ctrl+Shift+[letter] combinations cannot be recognized.)

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 Wed 11 Jan 2017 - 23:53:04 EST

This archive was generated by hypermail 2.2.0 : Thu 12 Jan 2017 - 06:30:50 EST