Refactor keybinding code

This commit is contained in:
crater2150 2014-04-25 15:13:31 +02:00
parent 1dfd3fd43b
commit 97b844e6fd
5 changed files with 78 additions and 74 deletions

View file

@ -7,7 +7,7 @@ local scratch = require("scratch")
local modkey = conf.modkey or "Mod4"
local mb = require("modalbind")
local bindings = {mb = mb}
local bindings = {modalbind = mb}
-- {{{ Mouse bindings
root.buttons(awful.util.table.join(
@ -17,65 +17,59 @@ awful.button({ }, 5, awful.tag.viewprev)
-- }}}
local function spawnf(cmd) return function() awful.util.spawn(cmd) end end
bindings.spawnf = spawnf
conf.cmd.run = conf.cmd.run or spawnf("dmenu_run")
mpdmap = {
name = "MPD",
m = mpd.ctrl.toggle,
n = mpd.ctrl.next,
N = mpd.ctrl.prev,
s = spawnf("mpd"),
S = spawnf("mpd --kill"),
g = spawnf(conf.cmd.mpd_client)
m = { func = mpd.ctrl.toggle, desc = "Toggle" },
n = { func = mpd.ctrl.next, desc = "Next" },
N = { func = mpd.ctrl.prev, desc = "Prev" },
s = { func = spawnf("mpd"), desc = "start MPD" },
S = { func = spawnf("mpd --kill"), desc = "kill MPD" },
g = { func = spawnf(conf.cmd.mpd_client), desc = "Gmpc" }
}
mpdpromts = {
name = "MPD PROMPTS",
a = mpd.prompt.artist,
b = mpd.prompt.album,
t = mpd.prompt.title,
r = mpd.prompt.toggle_replace_on_search
a = { func = mpd.prompt.artist, desc = "artist" },
b = { func = mpd.prompt.album, desc = "album" },
t = { func = mpd.prompt.title, desc = "title" },
r = { func = mpd.prompt.toggle_replace_on_search, desc = "toggle replacing" }
}
progmap = {
name = "PROGRAMS",
f = spawnf(conf.cmd.browser),
i = spawnf(conf.cmd.im_client),
I = spawnf(conf.cmd.irc_client),
m = spawnf(conf.cmd.mail_client)
f = { func = spawnf(conf.cmd.browser), desc = "Browser" },
i = { func = spawnf(conf.cmd.im_client), desc = "IM Client" },
I = { func = spawnf(conf.cmd.irc_client), desc = "IRC" },
m = { func = spawnf(conf.cmd.mail_client), desc = "Mail" }
}
docmap = {
name = "DOCUMENTS",
u = spawnf("docopen ~/uni pdf"),
b = spawnf("docopen ~/books pdf epub mobi txt lit html htm"),
u = { func = spawnf("docopen ~/uni pdf"), desc = "Uni-Dokumente" },
b = { func = spawnf("docopen ~/books pdf epub mobi txt lit html htm"), desc = "Bücher" }
}
adapters = { u = "wwan", w = "wlan", b = "bluetooth" }
function rfkill(cmd)
map={ name = string.upper(cmd) }
local function rfkill(cmd)
map = {}
for key, adapter in pairs(adapters) do
map[key] = spawnf("sudo rfkill "..cmd.." "..adapter)
map[key] = { func = spawnf("sudo rfkill "..cmd.." "..adapter), desc = adapter }
end
return map
end
connectmap = {
name = "CONNECT",
u = spawnf("umts"),
w = spawnf("wlanacpi")
u = { func = spawnf("umts"), desc = "umts" },
w = { func = spawnf("wlanacpi"), desc = "wlan" }
}
wirelessmap = {
name = "WIRELESS",
b = mb.grabf(rfkill("block")),
u = mb.grabf(rfkill("unblock")),
c = mb.grabf(connectmap)
b = { func = mb.grabf(rfkill("block"),"Block"), desc = "block" },
u = { func = mb.grabf(rfkill("unblock"),"Unblock"), desc = "unblock" },
c = { func = mb.grabf(connectmap, "Connect"), desc = "connect" }
}
function bindings.extend_and_register_key_table(globalkeys)
local totalkeys = globalkeys or {}
totalkeys = awful.util.table.join(totalkeys,
function bindings.extend_key_table(globalkeys)
return awful.util.table.join(globalkeys or {},
awful.key({ }, "Menu", spawnf('wmselect')),
awful.key({ modkey, "Control" }, "r", awesome.restart),
@ -84,11 +78,11 @@ function bindings.extend_and_register_key_table(globalkeys)
--{{{ Modal mappings
awful.key({ modkey }, "m", mb.grabf(mpdmap, true)),
awful.key({ modkey, "Shift" }, "m", mb.grabf(mpdpromts)),
awful.key({ modkey }, "c", mb.grabf(progmap)),
awful.key({ modkey }, "w", mb.grabf(wirelessmap)),
awful.key({ modkey }, "d", mb.grabf(docmap)),
awful.key({ modkey }, "m", mb.grabf(mpdmap, "MPD", true)),
awful.key({ modkey, "Shift" }, "m", mb.grabf(mpdpromts, "MPD - Search for")),
awful.key({ modkey }, "c", mb.grabf(progmap, "Commands")),
awful.key({ modkey }, "w", mb.grabf(wirelessmap, "Wifi")),
awful.key({ modkey }, "d", mb.grabf(docmap, "Documents")),
--}}}
--{{{ Audio control
@ -130,13 +124,10 @@ function bindings.extend_and_register_key_table(globalkeys)
)
--}}}
-- Set keys
root.keys(totalkeys)
end
function client_opacity_set(c, default, max, step)
local function client_opacity_set(c, default, max, step)
if c.opacity < 0 or c.opacity > 1 then
c.opacity = default
end

View file

@ -30,7 +30,6 @@ function layouts.extend_key_table(globalkeys)
end),
-- Layout manipulation
awful.key({ modkey }, "u", awful.client.urgent.jumpto),
awful.key({ modkey }, "Tab", function ()
awful.client.focus.history.previous()
if client.focus then
@ -78,10 +77,7 @@ function layouts.extend_key_table(globalkeys)
end),
awful.key({ modkey, "Control" }, "k", function ()
awful.screen.focus_relative(-1)
end),
awful.key({ modkey }, "Left", awful.tag.viewprev ),
awful.key({ modkey }, "Right", awful.tag.viewnext ),
awful.key({ modkey }, "Escape", awful.tag.history.restore)
end)
);
end

View file

@ -105,7 +105,7 @@ local function set_default(s)
modewibox[s].x = settings.x_offset < 0 and
screen[s].geometry.x - width + settings.x_offset or
settings.x_offset
modewibox[s].y = screen[s].geometry.height - settings.height
modewibox[s].y = screen[s].geometry.height - modewibox[s].height
end
local function ensure_init()
@ -115,7 +115,7 @@ local function ensure_init()
inited = true
for s = 1, screen.count() do
modewidget[s] = wibox.widget.textbox()
modewidget[s]:set_align("center")
modewidget[s]:set_align("left")
modewibox[s] = wibox({
fg = beautiful.fg_normal,
@ -141,16 +141,19 @@ local function ensure_init()
end
end
local function show_box(s, map)
local function show_box(s, map, name)
ensure_init()
modewibox.screen = s
local label = " -- " .. map.name .. " -- "
local label = "<b>" .. name .. "</b>"
if settings.show_options then
for key in pairs(map) do
if key ~= "name" then label = label .. " " .. key end
for key, mapping in pairs(map) do
label = label .. "\n<b>" .. key .. "</b>"
if type(mapping) == "table" then
label = label .. "\t" .. (mapping.desc or "???")
end
end
modewidget[s]:set_text(label)
end
modewidget[s]:set_markup(label)
modewibox[s].visible = true
set_default(s)
end
@ -160,9 +163,9 @@ local function hide_box()
if s ~= -1 then modewibox[s].visible = false end
end
function grab(keymap, stay_in_mode)
if keymap.name then
show_box(mouse.screen, keymap)
function grab(keymap, name, stay_in_mode)
if name then
show_box(mouse.screen, keymap, name)
nesting = nesting + 1
end
@ -178,7 +181,11 @@ function grab(keymap, stay_in_mode)
if keymap[key] then
keygrabber.stop()
if type(keymap[key]) == "table" then
keymap[key].func()
else
keymap[key]()
end
if stay_in_mode then
grab(keymap, true)
else
@ -193,8 +200,8 @@ function grab(keymap, stay_in_mode)
end
modalbind.grab = grab
function grabf(keymap, stay_in_mode)
return function() grab(keymap, stay_in_mode) end
function grabf(keymap, name, stay_in_mode)
return function() grab(keymap, name, stay_in_mode) end
end
modalbind.grabf = grabf

6
rc.lua
View file

@ -64,8 +64,10 @@ globalkeys = layouts.extend_key_table(globalkeys);
globalkeys = tags.extend_key_table(globalkeys);
bindings = require("bindings")
bindings.extend_and_register_key_table(globalkeys)
bindings.mb.set_x_offset(18)
bindings.modalbind.set_x_offset(18)
globalkeys = bindings.extend_key_table(globalkeys)
root.keys(globalkeys)
-- }}}
-- {{{ rules

View file

@ -1,4 +1,4 @@
-- tags and layouts
-- tags
local awful = awful
local conf = conf
local modkey = conf.modkey or "Mod4"
@ -6,7 +6,7 @@ local modkey = conf.modkey or "Mod4"
local tags={ mt={} }
local layouts = layouts
local function funcViewOnly(i)
local function getfunc_viewonly(i)
return function ()
local screen = mouse.screen
if tags[screen][i] then
@ -15,7 +15,7 @@ local function funcViewOnly(i)
end
end
local function funcViewToggle(i)
local function getfunc_viewtoggle(i)
return function ()
local screen = mouse.screen
if tags[screen][i] then
@ -24,7 +24,7 @@ local function funcViewToggle(i)
end
end
local function funcMoveTo(i)
local function getfunc_moveto(i)
return function ()
if client.focus and tags[client.focus.screen][i] then
awful.client.movetotag(tags[client.focus.screen][i])
@ -32,7 +32,7 @@ local function funcMoveTo(i)
end
end
local function funcToggle(i)
local function getfunc_clienttoggle(i)
return function ()
if client.focus and tags[client.focus.screen][i] then
awful.client.toggletag(tags[client.focus.screen][i])
@ -104,12 +104,20 @@ function tags.extend_key_table(globalkeys)
k = "F" .. i - 10 -- F keys
end
tagkeys = awful.util.table.join(tagkeys,
awful.key( { modkey }, k, funcViewOnly(i)),
awful.key( { modkey, "Control" }, k, funcViewToggle(i)),
awful.key( { modkey, "Shift" }, k, funcMoveTo(i)),
awful.key( { modkey, "Control", "Shift" }, k, funcToggle(i))
awful.key( { modkey }, k, getfunc_viewonly(i)),
awful.key( { modkey, "Control" }, k, getfunc_viewtoggle(i)),
awful.key( { modkey, "Shift" }, k, getfunc_moveto(i)),
awful.key( { modkey, "Control", "Shift" }, k, getfunc_clienttoggle(i))
)
end
-- keys for all tags
tagkeys = awful.util.table.join(tagkeys,
awful.key({ modkey }, "u", awful.client.urgent.jumpto),
awful.key({ modkey }, "Left", awful.tag.viewprev ),
awful.key({ modkey }, "Right", awful.tag.viewnext ),
awful.key({ modkey }, "Escape", awful.tag.history.restore)
)
return tagkeys;
end