Skip to content
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
52 changes: 36 additions & 16 deletions plugin/autoclose.vim
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ nmap <Plug>ToggleAutoCloseMappings :call <SID>ToggleAutoCloseMappings()<CR>
if (!hasmapto( '<Plug>ToggleAutoCloseMappings', 'n' ))
nmap <unique> <Leader>a <Plug>ToggleAutoCloseMappings
endif
fun <SID>ToggleAutoCloseMappings() " --- {{{2
fun! <SID>ToggleAutoCloseMappings() " --- {{{2
if g:autoclose_on
iunmap "
iunmap '
Expand All @@ -75,28 +75,36 @@ fun <SID>ToggleAutoCloseMappings() " --- {{{2
iunmap }
iunmap <BS>
iunmap <C-h>
iunmap <Del>
iunmap <Esc>
iunmap `
iunmap <
iunmap >
let g:autoclose_on = 0
echo "AutoClose Off"
else
inoremap <silent> " <C-R>=<SID>QuoteDelim('"')<CR>
inoremap <silent> ` <C-R>=<SID>QuoteDelim('`')<CR>
inoremap <silent> ' <C-R>=match(getline('.')[col('.') - 2],'\w') == 0 && getline('.')[col('.')-1] != "'" ? "'" : <SID>QuoteDelim("'")<CR>
inoremap <silent> ( (<C-R>=<SID>CloseStackPush(')')<CR>
inoremap ) <C-R>=<SID>CloseStackPop(')')<CR>
inoremap <silent> [ [<C-R>=<SID>CloseStackPush(']')<CR>
inoremap <silent> ] <C-R>=<SID>CloseStackPop(']')<CR>
inoremap <silent> < <<C-R>=<SID>CloseStackPush('>')<CR>
inoremap <silent> > <C-R>=<SID>CloseStackPop('>')<CR>
"inoremap <silent> { {<C-R>=<SID>CloseStackPush('}')<CR>
inoremap <silent> { <C-R>=<SID>OpenSpecial('{','}')<CR>
inoremap <silent> } <C-R>=<SID>CloseStackPop('}')<CR>
inoremap <silent> <BS> <C-R>=<SID>OpenCloseBackspace()<CR>
inoremap <silent> <C-h> <C-R>=<SID>OpenCloseBackspace()<CR>
inoremap <silent> <BS> <C-R>=<SID>OpenCloseBackspaceOrDel("BS")<CR>
inoremap <silent> <C-h> <C-R>=<SID>OpenCloseBackspaceOrDel("BS")<CR>
inoremap <silent> <Del> <C-R>=<SID>OpenCloseBackspaceOrDel("Del")<CR>
inoremap <silent> <Esc> <C-R>=<SID>CloseStackPop('')<CR><Esc>
inoremap <silent> <C-[> <C-R>=<SID>CloseStackPop('')<CR><C-[>
"the following simply creates an ambiguous mapping so vim fully
"processes the escape sequence for terminal keys, see 'ttimeout' for a
"rough explanation, this just forces it to work
if &term[:4] == "xterm"
inoremap <silent> <C-[>OC <RIGHT>
if &term[:4] == "xterm" || &term[:5] == 'screen'
inoremap <silent> <C-[>OC <RIGHT>
endif
let g:autoclose_on = 1
if a:0 == 0
Expand All @@ -108,7 +116,7 @@ endf
let s:closeStack = []

" AutoClose Utilities -----------------------------------------{{{1
function <SID>OpenSpecial(ochar,cchar) " ---{{{2
function! <SID>OpenSpecial(ochar,cchar) " ---{{{2
let line = getline('.')
let col = col('.') - 2
"echom string(col).':'.line[:(col)].'|'.line[(col+1):]
Expand All @@ -122,7 +130,7 @@ function <SID>OpenSpecial(ochar,cchar) " ---{{{2
return a:ochar.<SID>CloseStackPush(a:cchar)
endfunction

function <SID>CloseStackPush(char) " ---{{{2
function! <SID>CloseStackPush(char) " ---{{{2
"echom "push"
let line = getline('.')
let col = col('.')-2
Expand All @@ -137,7 +145,7 @@ function <SID>CloseStackPush(char) " ---{{{2
return ''
endf

function <SID>JumpOut(char) " ----------{{{2
function! <SID>JumpOut(char) " ----------{{{2
let column = col('.') - 1
let line = getline('.')
let mcol = match(line[column :], a:char)
Expand All @@ -156,7 +164,7 @@ function <SID>JumpOut(char) " ----------{{{2
return a:char
endif
endf
function <SID>CloseStackPop(char) " ---{{{2
function! <SID>CloseStackPop(char) " ---{{{2
"echom "pop"
if(a:char == '')
pclose
Expand Down Expand Up @@ -186,7 +194,8 @@ function <SID>CloseStackPop(char) " ---{{{2
return popped
endf

function <SID>QuoteDelim(char) " ---{{{2
function! <SID>QuoteDelim(char) " ---{{{2
" TODO: handle &commentstring at the beginning of a line (e.g. VIM mode)
let line = getline('.')
let col = col('.')
if line[col - 2] == "\\"
Expand All @@ -203,32 +212,43 @@ endf

" The strings returned from QuoteDelim aren't in scope for <SID>, so I
" have to fake it using this function (from the Vim help, but tweaked)
function s:SID()
function! s:SID()
return matchstr(expand('<sfile>'), '<SNR>\d\+_\zeSID$')
endfun

function <SID>OpenCloseBackspace() " ---{{{2
function! <SID>OpenCloseBackspaceOrDel(map) " ---{{{2
"if pumvisible()
" pclose
" call <SID>StopOmni()
" return "\<C-E>"
"else
let curline = getline('.')
let curpos = col('.')
if a:map == 'Del'
let curpos -= 1
end
let curletter = curline[curpos-1]
let prevletter = curline[curpos-2]
if (prevletter == '"' && curletter == '"') ||
\ (prevletter == "'" && curletter == "'") ||
\ (prevletter == "(" && curletter == ")") ||
\ (prevletter == "{" && curletter == "}") ||
\ (prevletter == "[" && curletter == "]")
\ (prevletter == "[" && curletter == "]") ||
\ (prevletter == "`" && curletter == "`") ||
\ (prevletter == "<" && curletter == ">")
if a:map == 'Del'
call insert(s:closeStack, curletter)
return "\<Del>"
end
" undo insert
if len(s:closeStack) > 0
call remove(s:closeStack,0)
endif
return "\<Delete>\<BS>"
else
return "\<BS>"
return "\<Del>\<BS>"
endif

" return key as is (Del or BS)
if a:map == 'Del' | return "\<Del>" | else | return "\<BS>" | endif
"endif
endf

Expand Down