-
-
Save anonymous/d784e16d9a2eec79414d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment