diff --git a/plugin/vim-erlang-tags.vim b/plugin/vim-erlang-tags.vim index 839b699..67af195 100644 --- a/plugin/vim-erlang-tags.vim +++ b/plugin/vim-erlang-tags.vim @@ -50,25 +50,73 @@ endfunction command! ErlangTags call VimErlangTags() -function! VimErlangTagsSelect(split) - if a:split - split - endif - let orig_isk = &isk - set isk+=: - normal "_vawo - if getline('.')[col('.') - 2] =~# '[#?]' - normal h +" Execute the given tag lookup for current word, where 'iskeyword' is +" temporarily set such that modules, records, and macros are included. +" +" Accepts a count as an optional second parameter which will be the window +" height for split or preview commands like 'stag', 'ptjump', etc., or the +" match index to jump to for 'tag'. +function! s:GoToErlangTag(cmd, ...) + let orig_isk = &l:isk + setl isk+=:,#,? + let ident = expand('') + let &l:isk = orig_isk + + " With no count arg or zero count, we can execute directly + if a:0 == 0 || a:1 == 0 + execute a:cmd ident + else + let cnt = a:1 + + if a:cmd ==# 'tag' + execute cnt a:cmd ident + elseif a:cmd =~# '^s' + let cmd = 'split +' . a:cmd[1:] . '\ ' . ident + execute cnt cmd + elseif a:cmd =~# '^p' + " :pedit has a cursor movement bug that we work around with a mark + " http://bit.ly/1F59kWA :-( Affects YouCompleteMe users, for one. + normal! m` + + let cmd = 'pedit +' . a:cmd[1:] . '\ ' . ident + call s:ExecWithPreviewHeight(cmd, cnt) + + normal! `` + endif endif - let &isk = orig_isk +endfunction + +" Because :pedit can't take height as count like :split does, ugh why. +function! s:ExecWithPreviewHeight(cmd, height) + let orig_height = &previewheight + let &previewheight = a:height + + try + execute a:cmd + catch /E426/ " tag not found + pclose " Spurious preview window is left open, close it. + echohl WarningMsg | echom v:exception | echohl None + finally + let &previewheight = orig_height + endtry endfunction function! VimErlangTagsDefineMappings() - nnoremap :call VimErlangTagsSelect(0) - nnoremap g :call VimErlangTagsSelect(0) - nnoremap :call VimErlangTagsSelect(0) - nnoremap g] :call VimErlangTagsSelect(0)g] - nnoremap g :call VimErlangTagsSelect(0)g - nnoremap :call VimErlangTagsSelect(1) - nnoremap ] :call VimErlangTagsSelect(1) + " Count is index of match to jump to, as with :tag + nnoremap :call GoToErlangTag('tag', v:count1) + nnoremap g :call GoToErlangTag('tag', v:count1) + nnoremap :call GoToErlangTag('tag', v:count1) + + nnoremap g] :call GoToErlangTag('tselect') + nnoremap g :call GoToErlangTag('tjump') + + " Count is window height for split and preview mappings + nnoremap :call GoToErlangTag('stag', v:count) + nnoremap ] :call GoToErlangTag('stag', v:count) + nnoremap g] :call GoToErlangTag('stselect', v:count) + nnoremap g :call GoToErlangTag('stjump', v:count) + nnoremap } :call GoToErlangTag('ptag', v:count) + nnoremap g} :call GoToErlangTag('ptjump', v:count) endfunction + +" vim:set expandtab sw=4 ts=4: