Created
May 22, 2010 14:40
-
-
Save eagletmt/410116 to your computer and use it in GitHub Desktop.
This file contains hidden or 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\\|character"') != 0 | |
let brace_start_pos = getpos('.') | |
normal! ge | |
let save = @" | |
normal! yl | |
let t = @" | |
let @" = save | |
if t == ')' | |
call searchpair('(', '', ')', 'bW', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|character"') | |
normal! b | |
let s = expand('<cword>') | |
if s == 'if' || s == 'elsif' | |
let if_start_pos = getpos('.') | |
break | |
endif | |
elseif expand('<cword>') == 'else' | |
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\\|character"') | |
normal! m] | |
normal! `[v`] | |
endfunction " }}} | |
" example | |
nmap <Space>if <Plug>select_cstyle_if |
はい,C言語的な文法での if 文 (できれば else のほうも)を選択するものを書こうとしました.これだと全然不完全ですけど…
{} 内を選ぶことは標準で可能ですけど,if (foo) { bar; baz; } の全体を選ぶ (text-object っぽく扱えるとなお良い) にはどうすればいいのかなぁと.
マッピングで提供したほうがいいのはその通りだと思います.
-nargs=0 ってデフォルトだったんですね… 見落としてました.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
なんとなく気になった点を。
:command
では<SID>
じゃなくてs:
でいいというか僕自身何をするコマンドなのかまだよく分かってないですが...
if文をvisual modeで選択するやつかなーとなんとなくコマンド名から察してみました。
(ちなみにfiletype=vimのifってどんなシンタックスグループ名なんだろうと確認してみたら"vimNotFunc"という実もフタもないものでした...)