From 6171918286f7705477c87ace88c46b23215306d0 Mon Sep 17 00:00:00 2001 From: BZ Date: Tue, 30 Mar 2021 11:31:26 +0200 Subject: [PATCH] provide option to define own focus function --- README.md | 2 ++ init.lua | 30 ++++++++++++------------- plugin/awesomewm_vim_tmux_navigator.vim | 2 +- tmux_focus.sh | 2 +- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 5dfde20..c5f0ad0 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,8 @@ For instance you might be using the right windows/super key and have to specify Don't forget to remove your previously used navigation keybinds (or other conflicting keybinds) in `rc.lua`. +It is possible to use a custom focus function by defining `focus`. Default is `awful.client.focus.global_bydirection`. + ### Vim diff --git a/init.lua b/init.lua index 4f25534..78e3128 100644 --- a/init.lua +++ b/init.lua @@ -4,11 +4,12 @@ local unpack = unpack or table.unpack -- luacheck: globals unpack local module = {} local function new(args) - local client, root, keygrabber = client, root, keygrabber -- luacheck: globals client root keygrabber + local awesome, client, root, keygrabber = awesome, client, root, keygrabber -- luacheck: globals client root keygrabber local keys = args or {up = {"k", "Up"}, down = {"j", "Down"}, left = {"h", "Left"}, right = {"l", "Right"}} local mod = keys.mod or "Mod4" local mod_keysym = keys.mod_keysym or "Super_L" + local focus = keys.focus or awful.client.focus.global_bydirection local tmux = {} tmux.left = function() @@ -71,55 +72,54 @@ local function new(args) root.fake_input("key_press", mod_keysym) end - local focus = function(dir) + local navigate = function(dir) local c = client.focus local client_name = c and c.name or "" if string.find(client_name, "- N?VIM$") then - vim.navigate(dir) - return + return vim.navigate(dir) else if string.find(client_name, "- TMUX$") then - tmux.navigate(dir) - return + return tmux.navigate(dir) else - awful.client.focus.global_bydirection(dir) + focus(dir) end end end -- experimental version that uses pstree to determine the running application if keys.experimental then - focus = function(dir) + navigate = function(dir) local c = client.focus local pid = c and c.pid or -1 awful.spawn.easy_async("pstree -A -T " .. pid, function(out) if string.find(out, "[^.*\n]%-tmux: client") then - tmux.navigate(dir) - return + return tmux.navigate(dir) else if string.find(out, "[^.*\n]%-n?vim$") or string.find(out, "[^.*\n]%-n?vim%-") or string.find(out, "^gvim$") or string.find(out, "^gvim%-") then - vim.navigate(dir) - return + return vim.navigate(dir) else - awful.client.focus.global_bydirection(dir) + focus(dir) end end end) end end + awesome.connect_signal("navigator::focus", focus) + keys.mod = nil keys.mod_keysym = nil keys.experimental = nil + keys.focus = nil local aw = {} glib.idle_add(glib.PRIORITY_DEFAULT_IDLE, function() for k, v in pairs(keys) do for _, key_name in ipairs(v) do aw[#aw + 1] = awful.key({mod}, key_name, function() - focus(k) - end, {description = "focus " .. k .. " window", group = "client"}) + navigate(k) + end, {description = "navigate " .. k, group = "client"}) end end root.keys(awful.util.table.join(root.keys(), unpack(aw))) diff --git a/plugin/awesomewm_vim_tmux_navigator.vim b/plugin/awesomewm_vim_tmux_navigator.vim index 1bc60d2..4a44a4d 100644 --- a/plugin/awesomewm_vim_tmux_navigator.vim +++ b/plugin/awesomewm_vim_tmux_navigator.vim @@ -114,7 +114,7 @@ func! s:SystemWindowNavigate(cmd) finish endif let dir = s:CmdToDir(a:cmd) - call system('awesome-client ''require("awful.client").focus.global_bydirection("' . dir . '") ''') + call system('awesome-client ''awesome.emit_signal("navigator::focus","' . dir . '")''') if !has("gui_running") redraw! diff --git a/tmux_focus.sh b/tmux_focus.sh index 5eff402..b1c776f 100755 --- a/tmux_focus.sh +++ b/tmux_focus.sh @@ -3,7 +3,7 @@ dir=$1 wm_focus() { - awesome-client 'require("awful.client").focus.global_bydirection("'"$dir"'")' + awesome-client 'awesome.emit_signal("navigator::focus", "'"$dir"'")' } case "$dir" in