Re: [code] [textadept] Dynamic keychains

From: Mitchell <m.att.foicica.com>
Date: Sun, 1 Dec 2013 14:06:24 -0500 (EST)

Chris,

On Fri, 29 Nov 2013, Chris Emerson wrote:

> Hi Mitchell,
>
> I've been making use of keychains a lot for my vi bindings, rather than
> keeping state on what keys have been pressed somewhere (with more risk of
> it getting out of sync).
>
> One awkward case is where I want to allow an arbitrary numeric prefix to
> a command (eg "1 2 3 d w" to delete 123 words).
>
> My current solution involves the bindings for digits returning a new proxy
> table wrapping the main binding table which keeps track of the count, and
> modifies the result. It works, but it's not for the faint of heart!

I actually like this approach. I agree it's not for the faint of heart,
but it's quite elegant.

> What would be nice is if the end key handler function had access to the
> keychain (which is currently a local in core/keys). What do you think
> about passing the key chain to plain functions in the bindings? It would
> also be handy for related bindings (eg binding all letters to functions
> which do something with that letter), rather than wrapping each in a closure.

Are you suggesting something like the following example for something
like "123dw"?

     keys['w'] = function(keychain)
       local repetitions = get_number_prefix(keychain)
       local action = get_action(keychain)
       if action == 'd' then
         for i = 1, repetitions do
           buffer:del_word_right()
         end
       elseif ... then
         ...
       end
     end

I'm not sure I like the idea of magic parameters... Plus, consider this:

     function foo(bar)
       if bar then
         -- do something
       else
         -- do something else
       end
     end
     keys['ca'] = foo -- no args
     keys['cb'] = {foo, 'arg'}

The 'ca' binding will error mysteriously with the magic parameter.

> Alternatively, using the table form you could define a special parameter
> which is replaced by the key chain (just like run_command swaps in the
> current buffer/view).

I think the only real solution is to make 'keychain' accessible (e.g.
global). This would require a read-only proxy. Would this work for you?
e.g.

     keys['w'] = function()
       local repetitions = get_repetitions(keys.keychain)
       local action = get_action(keys.keychain)
       ...
       keys.keychain[1] = nil -- throws an error
     end

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 Sun 01 Dec 2013 - 14:06:24 EST

This archive was generated by hypermail 2.2.0 : Mon 02 Dec 2013 - 06:51:35 EST