alexzzp
alexzzp

Reputation: 449

How to manually push stack entry into vim tagstack?

Just like the title says: "How to manually push stack entry into vim tagstack?"

Here is the problem: I have been using gtags/global + unite.vim plugins for a while (btw, these two plugins are awesome!), but it failed to automatically insert a tag entry into the tagstack. Is there any way to fix it?

Upvotes: 2

Views: 1252

Answers (3)

idbrii
idbrii

Reputation: 11946

As of a pretty recent version of vim (use if has('patch-8.2.0077') to check if yours is new enough), it's pretty easy to push locations into the tagstack:

" Store where we're jumping from.
let pos = [bufnr()] + getcurpos()[1:]
let item = {'bufnr': pos[0], 'from': pos, 'tagname': expand('<cword>')}
YourCommandToJumpToCWord

" Assuming jump was successful, write to tag stack.
let winid = win_getid()
let stack = gettagstack(winid)
let stack['items'] = [item]
call settagstack(winid, stack, 't')

Upvotes: 2

Luc Hermitte
Luc Hermitte

Reputation: 32966

In my very confidential lh-tags plugin, I had lh#tags#jump() function that I use to inject tags and jump to them. The function has now been moved to my vim-library: lh#tags#stack#jump()

The idea is to always have a fake tagfile (in a tmpdir) where I add jump locations as forged tags, when need be. From there, it's as simple as to jump to forged_tag_number_000042. Vim will then automatically take care of maintaining the stack for us.

Upvotes: 2

idbrii
idbrii

Reputation: 11946

I use a technique taken from vim-jedi in vim-tagimposter to push tags into the tagstack. (I believe this is the same technique as lh-tags.)

For omnisharp-vim, you could add this map to ** ftplugin/cs.vim**:

nnoremap <buffer> <Leader>jT :<C-u> TagImposterAnticipateJump <Bar> OmniSharpGotoDefinition<CR>

Now you can use <Leader>jT to jump to tags, <C-t> to jump back, and :pop/:tag to navigate up and down the stack. :tags will show your tags prefixed with IMPOSTER_.

I think this should work for gtags.vim:

nnoremap <Leader>jT :<C-u> TagImposterAnticipateJump <Bar> GtagsCursor<CR>

A fancier solution is the proposed 'tagfunc' which would have you implement a function that returns a list of tags (derived from gtags/global/whatever) and vim would take care of populating the tagstack.

Upvotes: 3

Related Questions