rebuild vim script from scratch

This commit is contained in:
BZ 2023-09-24 20:04:40 +02:00
parent 173d09693a
commit 191326bfb2
1 changed files with 121 additions and 109 deletions

View File

@ -1,71 +1,127 @@
if has('nvim')
set title titlestring=%(%{expand(\"%:~:.:h\")}%)/%t\ -\ NVIM
else
set title titlestring=%(%{expand(\"%:~:.:h\")}%)/%t\ -\ VIM
endif
" Maps <C-h/j/k/l> to switch vim splits in the given direction. If there are
" no more windows in that direction, forwards the operation to tmux.
" Additionally, <C-\> toggles between last active vim splits/tmux panes.
if exists("g:loaded_tmux_navigator") || &cp || v:version < 700 if exists("g:loaded_tmux_navigator") || &cp || v:version < 700
finish finish
endif endif
let g:loaded_tmux_navigator = 1 let g:loaded_tmux_navigator = 1
if !get(g:, 'tmux_navigator_no_dynamic_title', 0)
if has('nvim')
set title titlestring=%(%{expand(\"%:~:.:h\")}%)/%t\ -\ NVIM
else
set title titlestring=%(%{expand(\"%:~:.:h\")}%)/%t\ -\ VIM
endif
endif
let s:dir_from_hjkl = {'h': 'left', 'j': 'down', 'k': 'up', 'l': 'right'}
let s:pane_position_from_hjkl = {'h': 'left', 'j': 'bottom', 'k': 'top', 'l': 'right'}
func! s:SystemWindowNavigate(cmd)
if a:cmd == 'p'
finish
endif
let dir = s:dir_from_hjkl[a:cmd]
call system('awesome-client ''awesome.emit_signal("navigator::focus","' . dir . '")''')
" if !has("gui_running")
" redraw!
" endif
endfunction
function! s:VimNavigate(direction)
try
execute 'wincmd ' . a:direction
catch
echohl ErrorMsg | echo 'E11: Invalid in command-line window; <CR> executes, CTRL-C quits: wincmd k' | echohl None
endtry
endfunction
function! s:AwesomeVimNavigate(direction)
let oldw = winnr()
call s:VimNavigate(a:direction)
if oldw == winnr()
call s:SystemWindowNavigate(a:direction)
endif
endfunction
if !get(g:, 'tmux_navigator_no_mappings', 0)
noremap <silent> <c-h> :<C-U>TmuxNavigateLeft<cr>
noremap <silent> <c-j> :<C-U>TmuxNavigateDown<cr>
noremap <silent> <c-k> :<C-U>TmuxNavigateUp<cr>
noremap <silent> <c-l> :<C-U>TmuxNavigateRight<cr>
noremap <silent> <c-\> :<C-U>TmuxNavigatePrevious<cr>
endif
if empty($TMUX)
command! TmuxNavigateLeft call s:AwesomeVimNavigate('h')
command! TmuxNavigateDown call s:AwesomeVimNavigate('j')
command! TmuxNavigateUp call s:AwesomeVimNavigate('k')
command! TmuxNavigateRight call s:AwesomeVimNavigate('l')
command! TmuxNavigatePrevious call s:AwesomeVimNavigate('p')
finish
endif
command! TmuxNavigateLeft call s:TmuxAwareNavigate('h')
command! TmuxNavigateDown call s:TmuxAwareNavigate('j')
command! TmuxNavigateUp call s:TmuxAwareNavigate('k')
command! TmuxNavigateRight call s:TmuxAwareNavigate('l')
command! TmuxNavigatePrevious call s:TmuxAwareNavigate('p')
if !exists("g:tmux_navigator_save_on_switch") if !exists("g:tmux_navigator_save_on_switch")
let g:tmux_navigator_save_on_switch = 0 let g:tmux_navigator_save_on_switch = 0
endif endif
function! s:UseTmuxNavigatorMappings() if !exists("g:tmux_navigator_disable_when_zoomed")
return !exists("g:tmux_navigator_no_mappings") || !g:tmux_navigator_no_mappings let g:tmux_navigator_disable_when_zoomed = 0
endif
if !exists("g:tmux_navigator_preserve_zoom")
let g:tmux_navigator_preserve_zoom = 0
endif
function! s:TmuxOrTmateExecutable()
return (match($TMUX, 'tmate') != -1 ? 'tmate' : 'tmux')
endfunction endfunction
function! s:UseTmuxNavigatorMappingsInInsertMode() function! s:TmuxVimPaneIsZoomed()
return exists("g:tmux_navigator_insert_mode") return s:TmuxCommand("display-message -p '#{window_zoomed_flag}'") == 1
endfunction endfunction
function! s:InTmuxSession() function! s:TmuxSocket()
return $TMUX != '' " The socket path is the first value in the comma-separated list of $TMUX.
return split($TMUX, ',')[0]
endfunction endfunction
function! s:TmuxPaneCurrentCommand() function! s:TmuxCommand(args)
echo system("tmux display-message -p '#{pane_current_command}'") let cmd = s:TmuxOrTmateExecutable() . ' -S ' . s:TmuxSocket() . ' ' . a:args
let l:x=&shellcmdflag
let &shellcmdflag='-c'
let retval=system(cmd)
let &shellcmdflag=l:x
return retval
endfunction endfunction
command! TmuxPaneCurrentCommand call <SID>TmuxPaneCurrentCommand()
function! s:TmuxNavigatorProcessList()
echo s:TmuxCommand("run-shell 'ps -o state= -o comm= -t ''''#{pane_tty}'''''")
endfunction
command! TmuxNavigatorProcessList call s:TmuxNavigatorProcessList()
let s:tmux_is_last_pane = 0 let s:tmux_is_last_pane = 0
au WinEnter * let s:tmux_is_last_pane = 0 augroup tmux_navigator
au!
" Like `wincmd` but also change tmux panes instead of vim windows when needed. autocmd WinEnter * let s:tmux_is_last_pane = 0
function! s:TmuxWinCmd(direction) augroup END
if s:InTmuxSession()
call s:TmuxAwareNavigate(a:direction)
else
let oldw = winnr()
call s:VimNavigate(a:direction)
if oldw == winnr()
call s:SystemWindowNavigate(a:direction)
endif
endif
endfunction
function! s:NeedsVitalityRedraw() function! s:NeedsVitalityRedraw()
return exists('g:loaded_vitality') && v:version < 704 && !has("patch481") return exists('g:loaded_vitality') && v:version < 704 && !has("patch481")
endfunction endfunction
function! s:TmuxGetActivePaneId() function! s:ShouldForwardNavigationBackToTmux(tmux_last_pane, at_tab_page_edge)
let cmd = "tmux list-panes -F '#P #{?pane_active,active,}'" if g:tmux_navigator_disable_when_zoomed && s:TmuxVimPaneIsZoomed()
let list = split(system(cmd), '\n') return 0
let paneID = '' endif
return a:tmux_last_pane || a:at_tab_page_edge
for pane in list
if match(pane, 'active') != -1
let paneID = pane
endif
endfor
return paneID
endfunction endfunction
function! s:TmuxAwareNavigate(direction) function! s:TmuxAwareNavigate(direction)
@ -74,24 +130,33 @@ function! s:TmuxAwareNavigate(direction)
if !tmux_last_pane if !tmux_last_pane
call s:VimNavigate(a:direction) call s:VimNavigate(a:direction)
endif endif
let at_tab_page_edge = (nr == winnr())
" Forward the switch panes command to tmux if: " Forward the switch panes command to tmux if:
" a) we're toggling between the last tmux pane; " a) we're toggling between the last tmux pane;
" b) we tried switching windows in vim but it didn't have effect. " b) we tried switching windows in vim but it didn't have effect.
if tmux_last_pane || nr == winnr() if s:ShouldForwardNavigationBackToTmux(tmux_last_pane, at_tab_page_edge)
if g:tmux_navigator_save_on_switch if g:tmux_navigator_save_on_switch == 1
update try
update " save the active buffer. See :help update
catch /^Vim\%((\a\+)\)\=:E32/ " catches the no file name error
endtry
elseif g:tmux_navigator_save_on_switch == 2
try
wall " save all the buffers. See :help wall
catch /^Vim\%((\a\+)\)\=:E141/ " catches the no file name error
endtry
endif endif
let args = 'select-pane -t ' . shellescape($TMUX_PANE) . ' -' . tr(a:direction, 'phjkl', 'lLDUR')
if a:direction == 'p' if g:tmux_navigator_preserve_zoom == 1
finish let l:args .= ' -Z'
endif endif
let pos = s:pane_position_from_hjkl[a:direction]
let dir = s:dir_from_hjkl[a:direction]
let dir = s:CmdToDir(a:direction) let navigate = 'awesome-client \"awesome.emit_signal(\"\\\"navigator::focus\"\\\",\"\\\"' . dir . '\"\\\")\"'
let cmd = 'sh ~/.config/awesome/awesomewm-vim-tmux-navigator/tmux_focus.sh '. dir
silent call system(cmd)
let args = 'if -F "#{pane_at_' . pos . '}" "run-shell ''' . navigate . '''" "' . args . '"'
silent call s:TmuxCommand(args)
if s:NeedsVitalityRedraw() if s:NeedsVitalityRedraw()
redraw! redraw!
endif endif
@ -100,56 +165,3 @@ function! s:TmuxAwareNavigate(direction)
let s:tmux_is_last_pane = 0 let s:tmux_is_last_pane = 0
endif endif
endfunction endfunction
function! s:VimNavigate(direction)
try
execute 'wincmd ' . a:direction
catch
echohl ErrorMsg | echo 'E11: Invalid in command-line window; <CR> executes, CTRL-C quits: wincmd k' | echohl None
endtry
endfunction
func! s:SystemWindowNavigate(cmd)
if a:cmd == 'p'
finish
endif
let dir = s:CmdToDir(a:cmd)
call system('awesome-client ''awesome.emit_signal("navigator::focus","' . dir . '")''')
if !has("gui_running")
redraw!
endif
endfunction
func! s:CmdToDir(cmd)
if a:cmd == 'h'
return "left"
elseif a:cmd == 'j'
return "down"
elseif a:cmd == 'k'
return "up"
elseif a:cmd == 'l'
return "right"
endif
endfunction
command! TmuxNavigateLeft call <SID>TmuxWinCmd('h')
command! TmuxNavigateDown call <SID>TmuxWinCmd('j')
command! TmuxNavigateUp call <SID>TmuxWinCmd('k')
command! TmuxNavigateRight call <SID>TmuxWinCmd('l')
command! TmuxNavigatePrevious call <SID>TmuxWinCmd('p')
if s:UseTmuxNavigatorMappings()
nnoremap <silent> <c-h> :TmuxNavigateLeft<cr>
nnoremap <silent> <c-j> :TmuxNavigateDown<cr>
nnoremap <silent> <c-k> :TmuxNavigateUp<cr>
nnoremap <silent> <c-l> :TmuxNavigateRight<cr>
nnoremap <silent> <c-\> :TmuxNavigatePrevious<cr>
if s:UseTmuxNavigatorMappingsInInsertMode()
inoremap <silent> <c-h> <Esc>:TmuxNavigateLeft<cr>
inoremap <silent> <c-j> <Esc>:TmuxNavigateDown<cr>
inoremap <silent> <c-k> <Esc>:TmuxNavigateUp<cr>
inoremap <silent> <c-l> <Esc>:TmuxNavigateRight<cr>
inoremap <silent> <c-\> <Esc>:TmuxNavigatePrevious<cr>
endif
endif