provide experimental pstree option

by setting the experimental flag the focused application will be
determined by using `pstree` instead of relying on dynamic titles.

Theoretically this should work on every system/configuration, but might be a bit
slower due to having pstree to spawn.
This commit is contained in:
BZ 2021-03-29 20:47:09 +02:00
parent 32d02383a0
commit 642f75c60a
2 changed files with 94 additions and 9 deletions

View File

@ -14,8 +14,10 @@ This plugin adds another layer to christoomey's plugin [vim-tmux-navigator](http
How does it work
------------
The plugin sends the correct keypresses based on the focused appplication.
In order to differentitate between (n)vim and tmux clients, the title of your terminal is changed.
Therefore your shell/terminal stack has to support dynamic titles (see Troubleshooting section).
In order to differentitate between (n|g)vim and tmux clients, the title of your terminal is changed.
If your shell/terminal stack is not configured to show dynamic titles, you can set the `experimental` flag, which will try to determine the focused application by using `pstree`.
Installation
------------
@ -36,7 +38,8 @@ require("awesomewm-vim-tmux-navigator") {
left = {"Left", "h"},
right = {"Right", "l"},
mod = "Mod4",
mod_keysym = "Super_L"
mod_keysym = "Super_L",
--experimental = true
}
```
@ -62,7 +65,7 @@ Remove similar plugins (like `christoomey/vim-tmux-navigator`).
### Tmux
Add the following to your `tmux.conf` at the very bottom.
```tmux
# Set Terminal titles where possible
# Set title suffix to "- TMUX"
set-option -g set-titles on
set-option -g set-titles-string '#S: #W - TMUX'
@ -77,12 +80,11 @@ bind -n C-Right if-shell "$is_vim" "send-keys C-l" "run-shell 'sh ~/.config/awes
Troubleshooting
---------------
After a correct installation the title of a tmux session should end with "- TMUX" and "- VIM" or "- NVIM" for vim or nvim sessions respectively.
Check the title of the terminal client in your wm tasklist or by using `xprop` (title is property `WM_NAME`).
1. Make sure there are no conflicting keybindings.
In case your title does not change, your terminal and/or shell do not support dynamic titles or are not configured.
2. Check https://github.com/christoomey/vim-tmux-navigator#troubleshooting.
Try minimal configurations provided for `zsh` or `bash`:
3. Try to enable dynamic titles in your shell. Minimal configurations are provided for `zsh` and `bash`:
```
echo "source ~/.config/awesome/awesomewm-vim-tmux-navigator/dynamictitles.zsh" >> ~/.zshrc
@ -94,4 +96,9 @@ or
echo "source ~/.config/awesome/awesomewm-vim-tmux-navigator/dynamictitles.bash" >> ~/.bashrc
```
I recommend to use `alacritty` with `zsh`.
After a correct installation the title of a tmux session should end with "- TMUX" and "- VIM" or "- NVIM" for vim or nvim sessions respectively.
Check the title of the terminal client in your wm tasklist or by using the terminal application `xprop` (title is property `WM_NAME`).
In case your title does not change, your terminal and/or shell may not support dynamic titles. Try other.
4. Try to set `experimental = true`. This requires you to have the application `pstree` from package `psmisc` installed.

View File

@ -81,8 +81,86 @@ local function new(args)
end
end
-- experimental version that uses pstree to determine the running application
if keys.experimental then
focus = 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, "^[^%s].*%-tmux: client") then
keygrabber.stop()
root.fake_input("key_release", mod_keysym)
root.fake_input("key_press", "Control_L")
if dir == "left" then
root.fake_input("key_release", "Left")
root.fake_input("key_press", "Left")
root.fake_input("key_release", "Left")
else
if dir == "right" then
root.fake_input("key_release", "Right")
root.fake_input("key_press", "Right")
root.fake_input("key_release", "Right")
else
if dir == "up" then
root.fake_input("key_release", "Up")
root.fake_input("key_press", "Up")
root.fake_input("key_release", "Up")
else
if dir == "down" then
root.fake_input("key_release", "Down")
root.fake_input("key_press", "Down")
root.fake_input("key_release", "Down")
end
end
end
end
root.fake_input("key_release", "Control_L")
root.fake_input("key_press", mod_keysym)
return
else
if string.find(out, "^[^%s].*%-n?vim$") or string.find(out, "^[^%s].*%-n?vim%-") or
string.find(out, "^gvim") or string.find(out, "^gvim%-") then
keygrabber.stop()
root.fake_input("key_release", mod_keysym)
root.fake_input("key_release", "Control_L")
root.fake_input("key_press", "Control_L")
if dir == "left" then
root.fake_input("key_release", "h")
root.fake_input("key_press", "h")
root.fake_input("key_release", "h")
else
if dir == "right" then
root.fake_input("key_release", "l")
root.fake_input("key_press", "l")
root.fake_input("key_release", "l")
else
if dir == "up" then
root.fake_input("key_release", "k")
root.fake_input("key_press", "k")
root.fake_input("key_release", "k")
else
if dir == "down" then
root.fake_input("key_release", "j")
root.fake_input("key_press", "j")
root.fake_input("key_release", "j")
end
end
end
end
root.fake_input("key_release", "Control_L")
root.fake_input("key_press", mod_keysym)
return
else
awful.client.focus.global_bydirection(dir)
end
end
end)
end
end
keys.mod = nil
keys.mod_keysym = nil
keys.experimental = nil
local aw = {}
glib.idle_add(glib.PRIORITY_DEFAULT_IDLE, function()