You can obtain the filename and the line number (/search command) from the selected tag.
let tag = taglist()[42]
exe 'e '. (tag.filename)
exe ':'.(tag.cmd)
However, this won't push your 'open-and-go-to-the-right-place' into the tag stack as :tag
would have. I've been able to trick vim into doing it in my lh-tags plugin.
The current version of the (GPLv3) code that do so is the following:
" # Tag push/pop {{{2
" internal tmp tags file {{{3
if !exists('s:tags_jump')
let s:tags_jump = tempname()
let &tags .= ','.s:tags_jump
let s:lines = []
endif
let s:lines = []
" lh#tags#jump {{{3
let s:k_tag_name__ = '__jump_tag__'
let s:k_nb_digits = 5 " works with ~1 million jumps. Should be enough
function! lh#tags#jump(tagentry)
let last = len(s:lines)+1
" Assert s:tagentry.filename == expand('%:p')
let filename = expand('%:p')
let tag_name = s:k_tag_name__.repeat('0', s:k_nb_digits-strlen(last)).last
let l = tag_name
\ . "\t" . (filename)
\ . "\t" . (a:tagentry.cmd)
" test whether a new digit is used. In that case renumber every tags to have
" a lexical order
call add(s:lines, l)
call writefile(s:lines, s:tags_jump)
if exists('&l:tags')
exe 'setlocal tags+='.s:tags_jump
endif
exe 'tag '.tag_name
endfunction
The plugin is maintained there: https://github.com/LucHermitte/lh-tags
Update since the clarifications in the question: You can use readfile()
(IIRC) to load the file associated to the selected tag, without moving the cursor or anything. Then, if the tags file uses line numbers, finding the context will be trivial. If the command is a search command, use filter()
, or better match()
to get the context you are interesting in.