Re: [textadept] Code: Warning on key shortcut re-assignment

From: HyperHacker <hyperhac....at.gmail.com>
Date: Fri, 24 Feb 2012 19:15:31 -0700

On Fri, Feb 24, 2012 at 07:34, mitchell <c....at.caladbolg.net> wrote:
> Hi,
>
>
> On Tue, 21 Feb 2012, HyperHacker wrote:
>
>> Just something I cooked up to warn if you assign something to a
>> shortcut key that already had a previous assignment, or assign nil
>> (since that usually would be by assigning something that doesn't
>> really exist). Copy/edit the keys.lua module and replace:
>> local keys = keys
>>
>> with:
>> local _g_keys = keys
>> local key_set_src = {}
>> local keys = setmetatable({}, {
>>        __index = _g_keys,
>>        __newindex = function(t,k,v)
>>                local where = debug.getinfo(2, 'Sl')
>>                if _g_keys[k] then
>>                        io.stderr:write(("Warning: %s:%d: re-assignment to
>> key %s\n\t" ..
>>                                "first defined at
>> %s:%d\n"):format(where.short_src,
>>                                where.currentline, tostring(k),
>> key_set_src[k].short_src,
>>                                key_set_src[k].currentline))
>>                else key_set_src[k] = where
>>                end
>>                if v == nil then io.stderr:write((
>>                        "Warning: %s:%d: assigning nothing to key
>> %s\n"):format(
>>                        where.short_src, where.currentline, tostring(k)))
>>                end
>>                _g_keys[k] = v
>>        end,
>> })
>>
>> This will print warnings to stderr if you overwrite a previous
>> assignment or assign nothing, e.g.:
>> Warning: /home/rena/.textadept/modules/textadept/keys.lua:233:
>> re-assignment to key c/
>>       first defined at
>> /home/rena/.textadept/modules/textadept/keys.lua:190
>>
>> I'm not sure how to print them to the message window this early in
>> initialization, so you have to launch TA from a terminal to see the
>> messages. I found this very handy to debug my custom keybindings. (Why
>> doesn't this key do anything? Where is my custom binding being
>> overwritten? etc)
>
>
> I was able to put this at the top of my ~/.textadept/modules/keys.lua,
> replace 'io.stderr:write' with 'gui.print' and I got your messages to print
> when I redefined keys within said keys.lua. That way Windows and OSX users
> do not need terminals.
>
> This is useful wiki material! :)
>
> mitchell
>
> --
> You received this message because you are subscribed to the Google Groups
> "textadept" group.
> To post to this group, send email to textadept.at.googlegroups.com.
> To unsubscribe from this group, send email to
> textadept+unsubscribe.at.googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/textadept?hl=en.
>

Ah, I didn't realize I could use gui.print in startup scripts. Thanks!

I should warn that this just does a dumb string compare, so it won't
catch things like:
keys['csx'] = foo
keys['cX'] = bar
since 'csx' ~= 'cX', even though they map to the same key combination.

-- 
Sent from my toaster.
Received on Fri 24 Feb 2012 - 21:15:31 EST

This archive was generated by hypermail 2.2.0 : Thu 08 Mar 2012 - 12:31:31 EST