Skip to content

Instantly share code, notes, and snippets.

@eagletmt
Created May 22, 2010 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eagletmt/410116 to your computer and use it in GitHub Desktop.
Save eagletmt/410116 to your computer and use it in GitHub Desktop.
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
@tyru
Copy link

tyru commented May 22, 2010

なんとなく気になった点を。

  1. :commandでは<SID>じゃなくてs:でいい
  2. -nargs=0はデフォルトなのでつけなくてもいい
  3. コマンドじゃなくてマッピングで提供した方がいいかも?
    というか僕自身何をするコマンドなのかまだよく分かってないですが...
    if文をvisual modeで選択するやつかなーとなんとなくコマンド名から察してみました。

(ちなみにfiletype=vimのifってどんなシンタックスグループ名なんだろうと確認してみたら"vimNotFunc"という実もフタもないものでした...)

@eagletmt
Copy link
Author

はい,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