+
+" Insert closing brackets and quotes, spaces, stack effects ...
+" examples ("|" is the position of the cursor):
+" |if + [ → [|] if
+" [|] + <Space> → [ | ]
+" [|] + <CR> → [
+" |
+" ]
+" (|) + <Space> → ( |-- )
+" [|] + = → [=|=]
+" [|] + <BS> → |
+if exists("g:EnableFactorAutopairs") &&
+ \g:EnableFactorAutopairs == 1
+
+ function s:RemoveTrailingSpaces()
+ let save_view = winsaveview()
+ %s/ \+$//e
+ call winrestview(save_view)
+ endfunction
+
+ au BufWrite <buffer> :call s:RemoveTrailingSpaces()
+
+ function s:Insert(before, after)
+ return a:before .. a:after ..
+ \repeat("\<C-G>U\<Left>", strlen(a:after))
+ endfunction
+
+ function s:PadAfter()
+ return getline('.')[col('.') - 1] != ' ' ?
+ \s:Insert('', ' ') : ''
+ endfunction
+
+ function s:Context()
+ return strpart(getline('.'), col('.') - 2, 2)
+ endfunction
+
+ function s:WiderContext()
+ return strpart(getline('.'), col('.') - 3, 4)
+ endfunction
+
+ function s:OpenParenthesis()
+ return (s:Context() != '()' ? s:PadAfter() : '') ..
+ \s:Insert('(', ')')
+ endfunction
+
+ function s:OpenBracket()
+ let context = s:Context()
+ return (
+ \context != '==' && context != '[]' ? s:PadAfter() : ''
+ \) .. s:Insert('[', ']')
+ endfunction
+
+ function s:OpenBrace()
+ return s:PadAfter() .. s:Insert('{', '}')
+ endfunction
+
+ function s:Equal()
+ let context = s:Context()
+ if context == '[]' || context == '=='
+ return s:Insert('=', '=')
+ else
+ return s:Insert('=', '')
+ endif
+ endfunction
+
+ function s:Quote()
+ return s:Context() == '""' ? '' :
+ \(s:PadAfter() .. s:Insert('"', '"'))
+ endfunction
+
+ function s:Return()
+ let context = s:Context()
+ let widercontext = s:WiderContext()
+ if context == '[]' || context == '{}' ||
+ \widercontext == '[ ]' || widercontext == '{ }'
+ return "\<CR>\<C-O>O"
+ else
+ return "\<CR>"
+ endif
+ endfunction
+
+ function s:Backspace()
+ let context = s:Context()
+ let widercontext = s:WiderContext()
+ if widercontext == '[ ]' || widercontext == '( )' ||
+ \widercontext == '{ }' || context == '""' ||
+ \context == '==' || context == '()' ||
+ \context == '[]' || context == '{}'
+ return "\<Del>\<BS>"
+ else
+ return "\<BS>"
+ endif
+ endfunction
+
+ function s:Space()
+ let context = s:Context()
+ if context == '[]' || context == '{}' ||
+ \s:WiderContext() == '(())' ||
+ \strpart(getline('.'), col('.') - 5, 5) == ':> ()'
+ return s:Insert(' ', ' ')
+ elseif context == '()'
+ return s:Insert(' ', '-- ')
+ else
+ return s:Insert(' ', '')
+ endif
+ endfunction
+
+ inoremap <buffer> <expr> ( <SID>OpenParenthesis()
+ inoremap <buffer> <expr> [ <SID>OpenBracket()
+ inoremap <buffer> <expr> { <SID>OpenBrace()
+ inoremap <buffer> <expr> = <SID>Equal()
+ inoremap <buffer> <expr> " <SID>Quote()
+ inoremap <buffer> <expr> <CR> <SID>Return()
+ inoremap <buffer> <expr> <BS> <SID>Backspace()
+ inoremap <buffer> <expr> <Space> <SID>Space()
+
+endif
+
+" vim:sw=2:et: