do window edge detection in Vim; no more timeouts!

This commit is contained in:
Suraj N. Kurapati 2020-07-09 14:29:13 -07:00
parent 053a2463ef
commit 671cfd57ed
3 changed files with 32 additions and 25 deletions

View file

@ -40,16 +40,10 @@ set -g @navigate-down '-n M-n'
set -g @navigate-right '-n M-s'
```
4. Timeout for very slow Vim (optional):
```sh
# set this ONLY IF your Vim is very slow
set -g @navigate-timeout 1.618 # seconds
# propagation delay for Vim title change
```
4. Reload your tmux configuration file.
5. Reload your tmux configuration file.
6. Type <kbd>prefix</kbd>+<kbd>I</kbd>.
5. Type <kbd>prefix</kbd>+<kbd>I</kbd>.
(This makes TPM install the plugin.)
### Vim integration

View file

@ -2,5 +2,22 @@
" This also supports SSH tunnels where Vim is running on a remote host.
" See https://sunaku.github.io/tmux-select-pane.html for documentation.
let progname = substitute($VIM, '.*[/\\]', '', '')
set title titlestring=%{progname}\ %f\ +%l\ #%{tabpagenr()}.%{winnr()}
set title titlestring=%{progname}\ %f\ +%l\ #%{TmuxNavigateDirections()}
if &term =~ '^screen' && !has('nvim') | exe "set t_ts=\e]2; t_fs=\7" | endif
function! TmuxNavigateDirections() abort
let [y, x] = win_screenpos('.')
let h = winheight('.')
let w = winwidth('.')
let can_go_up = y > 2 " +1 for the tabline
let can_go_down = y + h < &lines - &laststatus
let can_go_left = x > 1
let can_go_right = x + w < &columns
return
\ (can_go_up ? 'U' : '') .
\ (can_go_down ? 'D' : '') .
\ (can_go_left ? 'L' : '') .
\ (can_go_right ? 'R' : '')
endfunction

View file

@ -16,18 +16,12 @@
get_tmux_option() { tmux show-option -gqv "$@" | grep . ;}
timeout=$(get_tmux_option '@navigate-timeout') || timeout=0.05 # seconds
navigate=" \
vim_navigation_timeout=$timeout; \
"' \
navigate=' \
pane_title="#{q:pane_title}"; \
pane_current_command="#{q:pane_current_command}"; \
pane_is_zoomed() { \
test #{window_zoomed_flag} -eq 1; \
}; \
pane_title_changed() { \
test "$pane_title" != "$(tmux display -p "##{q:pane_title}")"; \
}; \
command_is_vim() { \
case "${1%% *}" in \
(vi|?vi|vim*|?vim*|view|?view|vi??*) true ;; \
@ -50,18 +44,20 @@ navigate=" \
tmux_navigation_command=$1; \
vim_navigation_command=$2; \
vim_navigation_only_if=${3:-true}; \
navigate_tmux_if_unzoomed=true; \
if pane_contains_vim && eval "$vim_navigation_only_if"; then \
if pane_contains_neovim_terminal; then \
tmux send-keys C-\\ C-n; \
fi; \
eval "$vim_navigation_command"; \
if ! pane_is_zoomed; then \
sleep $vim_navigation_timeout; : wait for Vim to change title; \
if ! pane_title_changed; then \
eval "$tmux_navigation_command"; \
fi; \
fi; \
elif ! pane_is_zoomed; then \
vim_available_directions=l"${pane_title####* }"; \
tmux_navigate_direction=${tmux_navigation_command##* -}; \
tmux_navigate_direction=${tmux_navigate_direction%% *}; \
case "$vim_available_directions" in (*$tmux_navigate_direction*) \
navigate_tmux_if_unzoomed=false; \
eval "$vim_navigation_command";; \
esac; \
fi; \
if $navigate_tmux_if_unzoomed && ! pane_is_zoomed; then \
eval "$tmux_navigation_command"; \
fi; \
}; \