summaryrefslogtreecommitdiffstats
path: root/vimrc
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2017-10-28 17:12:34 -0400
committerJesse Luehrs <doy@tozt.net>2017-10-29 02:01:52 -0400
commitcade34d8b4fa0963ab837e0c85f578627c9cee68 (patch)
treeb23d932eb229005224f662d52f426218d0424b0a /vimrc
parentd0e9a9d46875a97ad1b95da1c7716ce48a762f75 (diff)
downloadconf-cade34d8b4fa0963ab837e0c85f578627c9cee68.tar.gz
conf-cade34d8b4fa0963ab837e0c85f578627c9cee68.zip
also remove matching parens, even if there is whitespace between
Diffstat (limited to 'vimrc')
-rw-r--r--vimrc47
1 files changed, 47 insertions, 0 deletions
diff --git a/vimrc b/vimrc
index ab72017..f0e672c 100644
--- a/vimrc
+++ b/vimrc
@@ -480,9 +480,23 @@ function s:move_cursor_right()
\]
call setpos('.', l:newpos)
endfunction
+function s:move_cursor_to_pos(lnum, col)
+ let l:pos = getcurpos()
+ let l:newpos = [
+ \ l:pos[0],
+ \ a:lnum,
+ \ a:col,
+ \ 0,
+ \ a:col,
+ \]
+ call setpos('.', l:newpos)
+endfunction
let s:pair_bs_maps = {
\ '"': "<SID>maybe_remove_adjacent_char('\"')",
\ "'": "<SID>maybe_remove_adjacent_char(\"'\")",
+\ '(': "<SID>maybe_remove_empty_pair(')')",
+\ '[': "<SID>maybe_remove_empty_pair(']')",
+\ '{': "<SID>maybe_remove_empty_pair('}')",
\}
function s:maybe_remove_matching_pair()
let l:prevchar = strpart(getline('.'), col('.')-2, 1)
@@ -499,6 +513,39 @@ function s:maybe_remove_adjacent_char(char)
return "\<BS>"
endif
endfunction
+function s:maybe_remove_empty_pair(char)
+ let l:startpos = [line('.'), col('.')]
+ let l:endpos = searchpos('[^ \t]', 'cnWz')
+ if l:endpos == [0, 0]
+ return "\<BS>"
+ endif
+
+ let l:next_nonblank = getline(l:endpos[0])[l:endpos[1]-1]
+ if l:next_nonblank != a:char
+ return "\<BS>"
+ endif
+
+ call s:move_cursor_to_pos(l:endpos[0], l:endpos[1] + 1)
+ return repeat("\<BS>", 2 + s:chars_between(l:startpos, l:endpos))
+endfunction
+function s:chars_between(start, end)
+ if a:start[0] == a:end[0]
+ return a:end[1] - a:start[1]
+ else
+ let l:line = getline(a:start[0])
+ let l:after_first = strpart(l:line, a:start[1] - 1)
+
+ let l:line = getline(a:end[0])
+ let l:before_last = strpart(l:line, 0, a:end[1] - 1)
+
+ let l:nchars = len(l:after_first) + len(l:before_last) + 1
+ for l:idx in range(a:start[0] + 1, a:end[0] - 1)
+ let l:nchars = l:nchars + len(getline(l:idx)) + 1
+ endfor
+
+ return l:nchars
+ endif
+endfunction
for s:pair in [['(', ')'], ['{', '}'], ['[', ']']]
exe "inoremap <silent> " . s:pair[0] . " " . s:pair[0] . s:pair[1] . "<C-R>=<SID>move_cursor_left()?\"\":\"\"<CR>"
exe "inoremap <silent> " . s:pair[0] . "<CR> " . s:pair[0] . "<CR>" . s:pair[1] . "<Esc>O"