From 2ad432088fa3228319e85564f0735e33ea3b6e2f Mon Sep 17 00:00:00 2001 From: jdhao Date: Sat, 14 Nov 2020 14:29:37 +0800 Subject: [PATCH] move function to autoload dir --- autoload/buf_utils.vim | 26 ++++++++++++++++++++++++++ core/mappings.vim | 31 ++----------------------------- 2 files changed, 28 insertions(+), 29 deletions(-) create mode 100644 autoload/buf_utils.vim diff --git a/autoload/buf_utils.vim b/autoload/buf_utils.vim new file mode 100644 index 0000000..db34ae0 --- /dev/null +++ b/autoload/buf_utils.vim @@ -0,0 +1,26 @@ +function! buf_utils#GoToBuffer(count, direction) abort + if a:count == 0 + if a:direction ==# 'forward' + bnext + elseif a:direction ==# 'backward' + bprevious + else + echoerr 'Bad argument ' a:direction + endif + return + endif + " Check the validity of buffer number. + if index(s:GetBufNums(), a:count) == -1 + echohl WarningMsg | echomsg 'Invalid bufnr: ' a:count | echohl None + return + endif + + " Do not use {count} for gB (it is less useful) + if a:direction ==# 'forward' + silent execute('buffer' . a:count) + endif +endfunction + +function! s:GetBufNums() abort + return map(copy(getbufinfo({'buflisted':1})), 'v:val.bufnr') +endfunction diff --git a/core/mappings.vim b/core/mappings.vim index 2aa9723..bda37d2 100644 --- a/core/mappings.vim +++ b/core/mappings.vim @@ -176,35 +176,8 @@ xnoremap :call utils#MoveSelection('down') " register, see also https://stackoverflow.com/q/10723700/6064933. xnoremap p "_cp -nnoremap gb :call GoToBuffer(v:count, 'forward') -nnoremap gB :call GoToBuffer(v:count, 'backward') - -function! s:GoToBuffer(count, direction) abort - if a:count == 0 - if a:direction ==# 'forward' - bnext - elseif a:direction ==# 'backward' - bprevious - else - echoerr 'Bad argument ' a:direction - endif - return - endif - " Check the validity of buffer number. - if index(s:GetBufNums(), a:count) == -1 - echohl WarningMsg | echomsg 'Invalid bufnr: ' a:count | echohl None - return - endif - - " Do not use {count} for gB (it is less useful) - if a:direction ==# 'forward' - silent execute('buffer' . a:count) - endif -endfunction - -function! s:GetBufNums() abort - return map(copy(getbufinfo({'buflisted':1})), 'v:val.bufnr') -endfunction +nnoremap gb :call buf_utils#GoToBuffer(v:count, 'forward') +nnoremap gB :call buf_utils#GoToBuffer(v:count, 'backward') nnoremap h nnoremap l