Thursday, June 21, 2012

Re: Going back to empty brackets and quotes

Hi Dotan!

On Do, 21 Jun 2012, Dotan Cohen wrote:

> Hello, I have this terrific mapping which takes me back to the
> previous empty brackets or quotes:
> inoremap jk <c-o>?\({}\\|\[]\\|<>\\|><\\|()\\|""\\|''\\|><lt>\)?s+1<Return>
>
> This works great if there is no whitespace between the empty brackets
> / quotes, so I added the whitespace check:
> inoremap jk <c-o>?\({\s*}\\|\[\s*]\\|<\s*>\\|>\s*<\\|(\s*)\\|"\s*"\\|'\s*'\\|>
> \s*<lt>\)?s+1<Return>
>
> However, this new regex leaves the cursor at the first character
> inside the brackets. I would like it to be at the half-way point, as
> sometimes there is no whitespace, sometimes a single space, and
> sometimes a double space depending on nestling:
>
> if (something) {}
> if ( someFunc(something) ) {}
> if ( someFunc(something) || otherFunc(something) )
>
> To write that code, I will do:
> if () {}|
> ^ Here I press jk
> if ( ) {}|
> ^ Here I press jk
> if ( ) {}|
> ^ Here I press jk
>
> Is there any way to put the cursor right in the middle of the
> brackets? I tried to write a function which would calculate the amount
> of whitespace characters between the brackets and would then press
> <Right> half that many times, but I completely failed. I then tried to
> select until the next bracket, replace all double whitespace
> characters with a single whitespace, set a mark, paste the
> now-half-size selection again, and then return to the mark. That
> didn't work out so well either! What approach should I be taking?

I am not exactly sure, what an empty bracket is supposed to be, but this
should get you a start:

fun! s:SearchPair() abort
let spat='\([[({<>''"]\)'
let epat='\([])}<>''"]\)'
call search(spat. '\(\s\+'. epat. '\)\@=', 'bW')
exe ':norm! v/'. epat. "\<CR>\<ESC>"
let len = getpos("'>")[2] - getpos("'<")[2]
exe ":norm! ". (getpos("'<")[2] + len/2 + 1). "|"
endfu


inoremap jk <c-\><c-o>:call <sid>SearchPair()<cr>


regards,
Christian
--

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment