Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nickspoons committed Jul 22, 2020
0 parents commit 2b60653
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tags
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Move Fast

This plugin provides an extensible way of navigating in a repetitive fashion using single keys.

Some movements are included in the plugin:

- Buffer: window-specific lists of buffers are maintained, allowing navigation back through the window history
- Scroll: vertical (`<C-d>`/`<C-u>`) and horizontal (`zH`/`zL`) scrolling
- Tab: move through tabs with `gt` and `gT`

No mappings are provided automatically.
Add mappings in your .vimrc to add the movefast navigations you are interested in:

```vim
nmap <Space>bh <Plug>(movefast_buffer_prev)
nmap <Space>bl <Plug>(movefast_buffer_next)
nmap <Space>bH <Plug>(movefast_buffer_prev_global)
nmap <Space>bL <Plug>(movefast_buffer_next_global)
nmap <Space>j <Plug>(movefast_scroll_down)
nmap <Space>k <Plug>(movefast_scroll_up)
nmap <Space>h <Plug>(movefast_scroll_left)
nmap <Space>l <Plug>(movefast_scroll_right)
nmap <Space>th <Plug>(movefast_tab_prev)
nmap <Space>tl <Plug>(movefast_tab_next)
```

These mappings work well with the default movefast "directions" for these navigations.
The directions can be customised using these variables:

```vim
" Default movefast directions
let g:movefast_buffer_prev = 'h'
let g:movefast_buffer_next = 'l'
let g:movefast_scroll_down = 'j'
let g:movefast_scroll_up = 'k'
let g:movefast_scroll_left = 'h'
let g:movefast_scroll_right = 'l'
let g:movefast_tab_prev = 'h'
let g:movefast_tab_next = 'l'
```

To use the movefast buffer navigation with `<<`/`>>` to start navigating and `<`/`>` to continue, add this to your .vimrc:

```vim
nmap << <Plug>(movefast_buffer_prev)
nmap >> <Plug>(movefast_buffer_next)
let g:movefast_buffer_prev = '<'
let g:movefast_buffer_next = '>'
```
17 changes: 17 additions & 0 deletions autoload/movefast.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function! movefast#Next(direction, opts) abort
call a:opts.next(a:direction)
let directions = get(a:opts, 'directions', ['h', 'l'])
if has_key(a:opts, 'title')
echohl Identifier | echo a:opts.title | echohl None
endif
let c = nr2char(getchar())
if index(directions, c) >= 0
call movefast#Next(c, a:opts)
else
echo ''
if has_key(a:opts, 'complete')
call a:opts.complete()
endif
redraw
endif
endfunction
67 changes: 67 additions & 0 deletions autoload/movefast/buffer.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
scriptencoding utf-8

let g:movefast_buffer_history = get(g:, 'movefast_buffer_history', [])

function! movefast#buffer#AddToHistory() abort
if get(s:, 'buffer_navigating', 0) | return | endif
let w:movefast_buffer_history = get(w:, 'movefast_buffer_history', [])
let bufnr = bufnr('%')
for scope in [g:, w:]
let idx = index(get(scope, 'movefast_buffer_history'), bufnr)
if idx != -1
call remove(get(scope, 'movefast_buffer_history'), idx)
endif
call add(get(scope, 'movefast_buffer_history'), bufnr)
endfor
endfunction

function! s:FastBuffer(scope, direction) abort
let buffers = get(a:scope, 'movefast_buffer_history', [])
let idx = index(buffers, bufnr('%'))
if a:direction ==# get(g:, 'movefast_buffer_prev', 'h')
let idx = idx == 0 ? 0 : idx - 1
elseif a:direction ==# get(g:, 'movefast_buffer_next', 'l')
let idx = idx == len(buffers) - 1 ? len(buffers) - 1 : idx + 1
endif
let s:buffer_navigating = 1
execute 'buffer' buffers[idx]
redraw
endfunction

function! s:FastBufferComplete() abort
let s:buffer_navigating = 0
" Move the newly selected buffer to the top of the list
call movefast#buffer#AddToHistory()
if len(w:movefast_buffer_history) > 1
" Set the previous top of the list as the alternate buffer
let @# = w:movefast_buffer_history[-2]
endif
endfunction

function! s:FastBufferInit(global, direction) abort
" Legal buftypes for fast-buffer switching
let bts = ['', 'help']
" Filter movefast_buffer_history in both scopes - do not use copy() here
for scope in [g:, w:]
call filter(get(scope, 'movefast_buffer_history', []),
\ 'bufexists(v:val) && index(bts, getbufvar(v:val, "&buftype")) >= 0')
endfor
let scope = a:global ? g: : w:
if len(get(scope, 'movefast_buffer_history', [])) < 2 | return | endif
call movefast#Next(a:direction, {
\ 'directions': [
\ get(g:, 'movefast_buffer_prev', 'h'),
\ get(g:, 'movefast_buffer_next', 'l')
\ ],
\ 'title': (a:global ? 'Global ' : '') . 'FastBuffering…',
\ 'next': function('s:FastBuffer', [scope]),
\ 'complete': function('s:FastBufferComplete')
\})
endfunction

function! movefast#buffer#Prev(global) abort
call s:FastBufferInit(a:global, get(g:, 'movefast_buffer_prev', 'h'))
endfunction
function! movefast#buffer#Next(global) abort
call s:FastBufferInit(a:global, get(g:, 'movefast_buffer_next', 'l'))
endfunction
55 changes: 55 additions & 0 deletions autoload/movefast/scroll.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
scriptencoding utf-8

function s:FastScroll(direction) abort
if a:direction ==# get(g:, 'movefast_scroll_down', 'j')
execute "normal! \<C-d>"
elseif a:direction ==# get(g:, 'movefast_scroll_up', 'k')
execute "normal! \<C-u>"
endif
redraw
endfunction

function s:FastScrollHorizontal(direction) abort
if a:direction ==# get(g:, 'movefast_scroll_left', 'h')
execute 'normal! zH'
elseif a:direction ==# get(g:, 'movefast_scroll_right', 'l')
execute 'normal! zL'
endif
redraw
endfunction

function! s:FastScrollInit(direction) abort
call movefast#Next(a:direction, {
\ 'directions': [
\ get(g:, 'movefast_scroll_down', 'j'),
\ get(g:, 'movefast_scroll_up', 'k')
\ ],
\ 'title': 'FastScrolling…',
\ 'next': function('s:FastScroll')
\})
endfunction

function! s:FastScrollInitHorizontal(direction) abort
call movefast#Next(a:direction, {
\ 'directions': [
\ get(g:, 'movefast_scroll_left', 'h'),
\ get(g:, 'movefast_scroll_right', 'l')
\ ],
\ 'title': 'FastScrolling…',
\ 'next': function('s:FastScrollHorizontal')
\})
endfunction

function! movefast#scroll#Down() abort
call s:FastScrollInit(get(g:, 'movefast_scroll_down', 'j'))
endfunction
function! movefast#scroll#Up() abort
call s:FastScrollInit(get(g:, 'movefast_scroll_up', 'k'))
endfunction

function! movefast#scroll#Left() abort
call s:FastScrollInitHorizontal(get(g:, 'movefast_scroll_left', 'h'))
endfunction
function! movefast#scroll#Right() abort
call s:FastScrollInitHorizontal(get(g:, 'movefast_scroll_right', 'l'))
endfunction
28 changes: 28 additions & 0 deletions autoload/movefast/tab.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
scriptencoding utf-8

function s:FastTab(direction) abort
if a:direction ==# get(g:, 'movefast_tab_prev', 'h')
tabprev
elseif a:direction ==# get(g:, 'movefast_tab_prev', 'l')
tabnext
endif
redraw
endfunction

function! s:FastTabInit(direction) abort
call movefast#Next(a:direction, {
\ 'directions': [
\ get(g:, 'movefast_tab_prev', 'h'),
\ get(g:, 'movefast_tab_next', 'l')
\ ],
\ 'title': 'FastTabbing…',
\ 'next': function('s:FastTab')
\})
endfunction

function! movefast#tab#Prev() abort
call s:FastTabInit(get(g:, 'movefast_tab_prev', 'h'))
endfunction
function! movefast#tab#Next() abort
call s:FastTabInit(get(g:, 'movefast_tab_next', 'l'))
endfunction
18 changes: 18 additions & 0 deletions plugin/movefast.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
nnoremap <silent> <Plug>(movefast_buffer_prev) :call movefast#buffer#Prev(0)<CR>
nnoremap <silent> <Plug>(movefast_buffer_next) :call movefast#buffer#Next(0)<CR>
nnoremap <silent> <Plug>(movefast_buffer_prev_global) :call movefast#buffer#Prev(1)<CR>
nnoremap <silent> <Plug>(movefast_buffer_next_global) :call movefast#buffer#Next(1)<CR>
nnoremap <silent> <Plug>(movefast_scroll_down) :call movefast#scroll#Down()<CR>
nnoremap <silent> <Plug>(movefast_scroll_up) :call movefast#scroll#Up()<CR>
nnoremap <silent> <Plug>(movefast_scroll_left) :call movefast#scroll#Left()<CR>
nnoremap <silent> <Plug>(movefast_scroll_right) :call movefast#scroll#Right()<CR>
nnoremap <silent> <Plug>(movefast_tab_prev) :call movefast#tab#Prev()<CR>
nnoremap <silent> <Plug>(movefast_tab_next) :call movefast#tab#Next()<CR>
augroup MoveFast
autocmd!
autocmd BufWinEnter * call movefast#buffer#AddToHistory()
augroup END

0 comments on commit 2b60653

Please sign in to comment.