[code] [textadept] Key Modes

From: Mitchell <m.att.foicica.com>
Date: Fri, 12 Apr 2013 12:26:19 -0400 (Eastern Daylight Time)


Textadept hg[1] now has key modes.

From the core/keys.lua documentation:

   ## Modes

   Sets of key bindings can be grouped together into modes. When a key
[mode](#MODE) is active, all key bindings defined outside the mode are
ignored until the mode is unset. Here is a simple vi mode example:

       keys.command_mode = {
         ['h'] = buffer.char_left,
         ['j'] = buffer.line_up,
         ['k'] = buffer.line_down,
         ['l'] = buffer.char_right,
         ['i'] = function()
           keys.MODE = nil
           gui.statusbar_text = 'INSERT MODE'
       keys['esc'] = function() keys.MODE = 'command_mode' end
       events.connect(events.UPDATE_UI, function()
         if keys.MODE == 'command_mode' then return end
         gui.statusbar_text = 'INSERT MODE'
       keys.MODE = 'command_mode' -- default mode

- - -

The command entry has been updated to use key modes, so the Lua command
entry, filter through, and incremental find modes each have their own sets
of key bindings[2].

From the modules/textadept/command_entry.lua documentation:

   ## Modes

   The command entry supports multiple [modes][] that have their own sets
of key bindings stored in a separate table in `_G.keys` under a mode
prefix key. Mode names are arbitrary, but cannot conflict with lexer
names or key sequence strings (e.g. `'lua'` or `'send'`) due to the method
used for looking up key bindings. An example mode is "lua_command" mode
for executing Lua commands:

       local gui_ce = gui.command_entry
       keys['ce'] = {gui_ce.enter_mode, 'lua_command'}
       keys.lua_command = {
         ['\t'] = gui_ce.complete_lua,
         ['\n'] = {gui_ce.finish_mode, gui_ce.execute_lua}

   In this case, `Ctrl+E` opens the command entry and enters "lua_command"
key mode where the `Tab` and `Enter` keys have special, defined
functionality. (By default, `Esc` is pre-defined to exit any command
entry mode.) `Tab` shows a list of Lua completions for the entry text and
`Enter` exits "lua_command" key mode and executes the entered code. All
other keys are handled normally by the command entry.

   It is important to note that while in any command entry key mode, all
editor key bindings are ignored -- even if the editor, not the command
entry, has focus. You must first exit the key mode by pressing `Esc` (or
`Enter` in the case of the above "lua_command" key mode).

   [modes]: keys.html#Modes

- - -

You will have to recompile Textadept if you want to take advantage of the
command entry mode functionality. You may also have to update any custom
key bindings that open the command entry.

Just for fun:

Textadept 6.5 lines of code:

   Language files blank comment code
   Lua 35 486 2678 3602
   C 1 144 386 1909

Textadept 6.5 lines of code with new key mode feature:

   Language files blank comment code
   Lua 35 483 2786 3591
   C 1 143 384 1899

I love it when new features reduce the code count :)


[1]: http://foicica.com/hg/textadept/rev/47b4b51227c0
[2]: http://foicica.com/hg/textadept/diff/47b4b51227c0/modules/textadept/keys.lua

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 Fri 12 Apr 2013 - 12:26:19 EDT

This archive was generated by hypermail 2.2.0 : Sat 13 Apr 2013 - 06:45:00 EDT