-
-
Save osak/410713 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nnoremap <silent> <Plug>select_cstyle_if :<C-u>call <SID>select_cstyle_if()<CR> | |
function! s:select_cstyle_if() " {{{ | |
let orig_view = winsaveview() | |
let if_start_pos = [] | |
while searchpair('{', '', '}', 'bW', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"') != 0 | |
let brace_start_pos = getpos('.') | |
normal! ge | |
let save = @" | |
normal! yl | |
let t = @" | |
let @" = save | |
if t == ')' | |
"Block which needs () after keyword | |
call searchpair('(', '', ')', 'bW', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"') | |
normal! b | |
let s = expand('<cword>') | |
if s == 'if' || s == 'while' || s == 'for' || s == 'switch' | |
let if_start_pos = getpos('.') | |
break | |
else | |
"It may be function declaration | |
normal! b | |
let if_start_pos = getpos('.') | |
break | |
endif | |
elseif expand('<cword>') == 'else' | |
"Block which doesn't need () after keyword | |
"elseはifごとコピーしたほうがいいのか? | |
normal! b | |
let if_start_pos = getpos('.') | |
break | |
endif | |
endwhile | |
if if_start_pos == [] | |
echohl ErrorMsg | |
echo "'if' not found" | |
echohl None | |
call winrestview(orig_view) | |
return | |
endif | |
normal! m[ | |
call setpos('.', brace_start_pos) | |
call searchpair('{', '', '}', 'W', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"') | |
normal! m] | |
normal! `[v`] | |
endfunction " }}} | |
" example | |
"nmap <Space>if <Plug>select_cstyle_if | |
omap <silent> <Space>if :<C-U>call <SID>select_cstyle_if()<CR> | |
" vim: set fdm=marker : |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment