Here is a solution which seems to be working (note the word seems I didn't test it extensively so it might not work in some cases) :
function! InsertDot(char)
" Save the cursor position
let save_pos=getpos('.')
" Try to yank inner braces
call setreg('z', "")
normal "zyi(
normal "zyi[
normal "zyi"
normal "zyi'
if ( getreg('z') == "" )
" If nothing was yanked insert the character and a new line
execute "normal! i" . a:char . "^M"
execute "startinsert"
else
" If something was yanked insert the dot
" (messy workaround to get back to the correct position)
call setpos('.', save_pos)
execute "normal! i" . a:char
execute "startinsert"
let save_pos[2] = save_pos[2]+1
call setpos('.', save_pos)
endif
endfunction
inoremap . <c-o>:call InsertDot('.')<CR>
inoremap ! <c-o>:call InsertDot('!')<CR>
The characters must be mapped in insert mode to call the function. The function will try to determine if the cursor is between braces and add the character and a new line or only the character accordingly.
Note that the function uses a crappy workaround to preserve the cursor position. I'm pretty sure it can be improved.
Otherwise to follow your comment you can use this:
inoremap . .^M
inoremap \. .
To make . insert a dot and a new line and \ . insert only a dot.
Important note
All the occurences of ^M
in the code must be added with CTRL+vEnter
<CR>
when needed..
I feel that it would be simpler to have vim do it for me. That way, I would not forget it.--word-diff
.