From 5b8f2255cca0c646a3c1c41693cc6bba401d6a7c Mon Sep 17 00:00:00 2001 From: DarkWiiPlayer Date: Tue, 7 Nov 2017 15:28:10 +0100 Subject: [PATCH] Added auto-closing feature * Auto-close pairs can be registered with a function call * Typing an "opening" character automatically inserts the matching closing character. * Typing between an opening and a closing character inserts two new lines. Autoindent should take care of indentation. --- vimrc | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/vimrc b/vimrc index d29a73b..b2625a1 100644 --- a/vimrc +++ b/vimrc @@ -96,6 +96,51 @@ else end " === GENERAL UTILITIES === + +function! s:autoClose_HelperOpen(open, close) + let next_c = getline(".")[col(".")-1] + if match(next_c, "\s") + return a:open.a:close."\" + else + return a:open + end +endfunc + +function! s:autoClose_HelperClose(open, close) + let next_c = getline(".")[col(".")-1] + if next_c ==# a:close + return "\" + else + return a:close + end +endfunc + +function! s:autoClose_HelperEnter() + if exists("b:autoClose_Pairs") + let next_c = getline(".")[col(".")-1] + let prev_c = getline(".")[col(".")-2] + + if (next_c !=# "") && (prev_c !=# "") + if exists("b:autoClose_Pairs[prev_c]") + if (next_c ==# b:autoClose_Pairs[prev_c]) + return "\m'\\`'\" + end + end + end + return "\" +endfunc + +function! s:autoClose_AddPair(open, close) "TODO: Improve with expand('') + if !exists("b:autoClose_Pairs") + let b:autoClose_Pairs = {} + end + let b:autoClose_Pairs[a:open] = a:close + + exe "inoremap ".a:open." autoClose_HelperOpen('".a:open."', '".a:close."')" + exe "inoremap ".a:close." autoClose_HelperClose('".a:open."', '".a:close."')" + inoremap autoClose_HelperEnter() +endfunc + function! MatchingLines(pattern) let list = [] let pattern = a:pattern @@ -454,6 +499,9 @@ function! s:init_ruby_file() nnoremap # :call RubyComment(1) vnoremap ~ :call RubyComment(0) vnoremap # :call RubyComment(1) + + call s:autoClose_AddPair("{", "}") + call s:autoClose_AddPair("(", ")") endfunction function! s:RubyComment(a)