Re: [code] [textadept] Dynamic keychains

From: Chris Emerson <c-ta.att.mail.nosreme.org>
Date: Sun, 1 Dec 2013 21:04:32 +0000

Hi Mitchell,

On Sun, Dec 01, 2013 at 02:06:24PM -0500, Mitchell wrote:
> Chris,
>
> On Fri, 29 Nov 2013, Chris Emerson wrote:
>
> >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.

I certainly had a lot of fun implementing it. :-)

> 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

Yes, that's the sort of thing I was thinking.

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

No, I didn't like that idea much either.

> 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.

Indeed, it wouldn't be backwards compatible doing that.

> >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

That would work. It's a shame Lua hasn't got something like keyword
parameters to avoid the global variable though!

Thanks,

Chris

-- 
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 - 16:04:32 EST

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