Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for repeating previous/next commands #146

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions plugin/unimpaired.vim
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ function! s:MapNextFamily(map,cmd) abort
let map = '<Plug>unimpaired'.toupper(a:map)
let cmd = '".(v:count ? v:count : "")."'.a:cmd
let end = '"<CR>'.(a:cmd ==# 'l' || a:cmd ==# 'c' ? 'zv' : '')
execute 'nnoremap <silent> '.map.'Previous :<C-U>exe "'.cmd.'previous'.end
execute 'nnoremap <silent> '.map.'Next :<C-U>exe "'.cmd.'next'.end
let prevnext = printf('["normal \%sPrevious", "normal \%sNext"]', map, map)
execute 'nnoremap <silent> '.map.'Previous :<C-U>exe <SID>prevnext("'.a:cmd.'", "previous", '.prevnext.')<CR>'
execute 'nnoremap <silent> '.map.'Next :<C-U>exe <SID>prevnext("'.a:cmd.'", "next", '.prevnext.')<CR>'
execute 'nnoremap <silent> '.map.'First :<C-U>exe "'.cmd.'first'.end
execute 'nnoremap <silent> '.map.'Last :<C-U>exe "'.cmd.'last'.end
call s:map('n', '['. a:map , map.'Previous')
Expand Down Expand Up @@ -84,6 +85,36 @@ function! s:entries(path) abort
return files
endfunction

" Handle previous/next movements, storing the last used one.
function! s:prevnext(cmd, dir, prevnext) abort
let g:unimpaired_prevnext = a:prevnext
let cmd = (v:count ? v:count : '').a:cmd.a:dir
if a:cmd ==# 'l' || a:cmd ==# 'c'
let cmd .= '|normal zv'
endif
return cmd
endfunction
function! s:last_prevnext_cmd(dir) abort
let prevnext = get(g:, 'unimpaired_prevnext', [])
if empty(prevnext)
if &diff
let m = 'c'
elseif !empty(getloclist(0))
let m = 'l'
elseif !empty(getqflist())
let m = 'q'
else
let m = 'n'
endif
return 'normal '.(a:dir ==# 'previous' ? '[' : ']').m
endif
return (a:dir ==# 'previous' ? prevnext[0] : prevnext[1])
endfunction
execute 'nnoremap <silent> <Plug>unimpairedRepeatPrevious :<C-U>exe <SID>last_prevnext_cmd("previous")<CR>'
execute 'nnoremap <silent> <Plug>unimpairedRepeatNext :<C-U>exe <SID>last_prevnext_cmd("next")<CR>'
call s:map('n', '[.', '<Plug>unimpairedRepeatPrevious')
call s:map('n', '].', '<Plug>unimpairedRepeatNext')

function! s:FileByOffset(num) abort
let file = expand('%:p')
if empty(file)
Expand Down