Skip to content

Commit

Permalink
Version 1.1.2
Browse files Browse the repository at this point in the history
Fixes a rather nasty bug that sometimes deletes closing chars on the same line. Add double brace auto-indent. 1.1.2 contains fixes for a mapping typo and auto-indent edge case.
  • Loading branch information
Karl Guertin authored and vim-scripts committed Oct 18, 2010
1 parent dc9a632 commit 9b74540
Showing 1 changed file with 41 additions and 9 deletions.
50 changes: 41 additions & 9 deletions plugin/autoclose.vim
@@ -1,10 +1,11 @@
" AutoClose, closes what's opened.
"
" Karl Guertin <grayrest@gr.ayre.st>
" 1.0.1 released April 3, 2007
" 1.1.2 released September 20, 2007
"
" This plugin closes opened parenthesis, braces, brackets, quotes as you
" type them.
" type them. As of 1.1, if you type the open brace twice ({{), the closing
" brace will be pushed down to a new line.
"
" You can enable or disable this plugin by typing \a (or <Leader>a if
" you've redefined your leader character) in normal mode. You'll also
Expand All @@ -13,14 +14,19 @@
" want to insert only an opening paren or something.
"
" Version Changes:
" 1.1.2 -- Fixed a mapping typo and caught a double brace problem
" 1.1.1 -- Missed a bug in 1.1, September 19, 2007
" 1.1 -- When not inserting at the end, previous version would eat chars
" at end of line, added double open->newline, September 19, 2007
" 1.0.1 -- Cruft from other parts of the mapping, knew I shouldn't have
" released the first as 1.0
" released the first as 1.0, April 3, 2007

" Setup -----------------------------------------------------{{{1
if exists('g:autoclose_loaded') || &cp
finish
endif


let g:autoclose_loaded = 1
let s:omni_active = 0
let s:cotstate = &completeopt
Expand All @@ -46,6 +52,7 @@ fun <SID>ToggleAutoCloseMappings() " --- {{{2
iunmap {
iunmap }
iunmap <BS>
iunmap <C-h>
iunmap <Esc>
""iunmap <C-[>
let g:autoclose_on = 0
Expand All @@ -57,9 +64,10 @@ fun <SID>ToggleAutoCloseMappings() " --- {{{2
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> <Esc> <C-R>=<SID>CloseStackPop('')<CR><Esc>
inoremap <silent> <C-[> <C-R>=<SID>CloseStackPop('')<CR><C-[>
let g:autoclose_on = 1
Expand All @@ -72,12 +80,30 @@ endf
let s:closeStack = []

" AutoClose Utilities -----------------------------------------{{{1
function <SID>OpenSpecial(ochar,cchar) " ---{{{2
let line = getline('.')
let col = col('.') - 2
"echom string(col).':'.line[:(col)].'|'.line[(col+1):]
if a:ochar == line[(col)] && a:cchar == line[(col+1)] "&& strlen(line) - (col) == 2
"echom string(s:closeStack)
while len(s:closeStack) > 0
call remove(s:closeStack, 0)
endwhile
return "\<esc>a\<CR>a\<CR>".a:cchar."\<esc>\"_xk$\"_xa"
endif
return a:ochar.<SID>CloseStackPush(a:cchar)
endfunction

function <SID>CloseStackPush(char) " ---{{{2
echom "push"
let line = getline('.')
let col = col('.')-2
echom string(col).':'.line[:(col)].'|'.line[(col+1):]
call setline('.',line[:(col)].a:char.line[(col+1):])
if (col) < 0
call setline('.',a:char.line)
else
echom string(col).':'.line[:(col)].'|'.line[(col+1):]
call setline('.',line[:(col)].a:char.line[(col+1):])
endif
call insert(s:closeStack, a:char)
echom join(s:closeStack,'').' -- '.a:char
return ''
Expand All @@ -97,8 +123,12 @@ function <SID>CloseStackPop(char) " ---{{{2
echom join(s:closeStack,'').' || '.lastpop.' || '.popped
endwhile
echom ' --> '.popped
let splits = split(getline('.'),popped,1)
call setline('.',join(splits[:-2],popped).splits[-1])
let col = col('.') - 2
let line = getline('.')
let splits = split(line[:col],popped,1)
echom string(splits)
echom col.' '.line[(col+2):].' '.popped
call setline('.',join(splits,popped).line[(col+strlen(popped)+1):])
return popped
endf

Expand Down Expand Up @@ -138,7 +168,9 @@ function <SID>OpenCloseBackspace() " ---{{{2
\ (prevletter == "(" && curletter == ")") ||
\ (prevletter == "{" && curletter == "}") ||
\ (prevletter == "[" && curletter == "]")
call remove(s:closeStack,0)
if len(s:closeStack) > 0
call remove(s:closeStack,0)
endif
return "\<Delete>\<BS>"
else
return "\<BS>"
Expand Down

0 comments on commit 9b74540

Please sign in to comment.