secret
anonymous / scope-complete.vim
Created

  • Download Gist
scope-complete.vim
VimL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
inoremap " "<esc>:call <SID>ScopeComplete()<cr>a
inoremap ' '<esc>:call <SID>ScopeComplete()<cr>a
inoremap ( (<esc>:call <SID>ScopeComplete()<cr>a
inoremap ) )<esc>:call <SID>ScopeComplete()<cr>a
inoremap { {<esc>:call <SID>ScopeComplete()<cr>a
inoremap } }<esc>:call <SID>ScopeComplete()<cr>a
inoremap [ [<esc>:call <SID>ScopeComplete()<cr>a
inoremap ] ]<esc>:call <SID>ScopeComplete()<cr>a
inoremap > ><esc>:call <SID>ScopeComplete()<cr>a
inoremap < <<esc>:call <SID>ScopeComplete()<cr>a
 
" Toggle scope completion by using either <leader>sc or :ScopeCompleteToggle
nnoremap <leader>sc :call <SID>ScopeCompleteToggle()<cr>
command! ScopeCompleteToggle :call <SID>ScopeCompleteToggle()
 
" Default to off. To turn on by default add "let g:scope_complete=1"
if !exists("g:scope_complete")
let g:scope_complete = 0
endif
 
" When an opening character is typed, automatically insert a closing character
" after it and place the cursor between the opening and closing characters.
" If the user types a closing character before the automatically inserted
" closing character then remove the automatically inserted character.
function! s:ScopeComplete()
if g:scope_complete
" opening chars as keys and closing chars as values
let scope_chs = {'(' : ')', '{' : '}', '[' : ']', '"' : '"', '''' : '''', '<' : '>',}
let line = getline('.')
let pos = getpos('.')
let ch = line[pos[2] - 1]
let next_ch = line[pos[2]]
echom "ScopeComplete() called for: " . ch
" if the ch is a closing ch and is the same as the next ch"
if index(values(scope_chs), ch) >= 0 && ch ==# next_ch
" delete the automatically inserted closing ch
execute "normal! x"
" if the ch is an opening ch
elseif has_key(scope_chs, ch)
" insert the corresponding closing ch and move cursor
" between the opening and closing ch's
execute "normal! a" . get(scope_chs, ch, '') . "\<esc>h"
endif
endif
endfunction
 
" Toggle on and off
function! s:ScopeCompleteToggle()
if g:scope_complete
let g:scope_complete = 0
else
let g:scope_complete = 1
endif
endfunction

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.