Code: Warning on key shortcut re-assignment

From: HyperHacker <hyperhac....at.gmail.com>
Date: Tue, 21 Feb 2012 21:08:36 -0700

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)

-- 
Sent from my toaster.
Received on Tue 21 Feb 2012 - 23:08:36 EST

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