wsdjeg / vim-fetch

Make Vim handle line and column numbers in file names with a minimum of fuss
http://www.vim.org/scripts/script.php?script_id=5089
MIT License
312 stars 17 forks source link

Provide API to just resolve specs #13

Open blueyed opened 8 years ago

blueyed commented 8 years ago

I've thought to use this plugin to get the filename under the cursor in a quickfix list, and then :edit it after wincmd w.

From a quick glance fetch#cfile does this, but the command to be executed is hard-coded to gf/gF already.

It would be great to have an interface where the filename that would be understood by vim-fetch would be returned: empty would mean that vim-fetch (and gf/gF) cannot handle it. For my use case using the current cursor position is good, but I could also imagine passing in a string (and maybe the position in that string).

Another approach would be for the function to return the spec itself: the filename and the jump action (and a way to call / execute those).

kopischke commented 8 years ago

That is a) definitely possible and b) a major refactoring of the codebase. Before undertaking it, I would like to understand your exact use case. Is it just a matter of using fetch to parse a spec’ed <cfile> back into a usable file name? Or is there something else I am missing from your description?

blueyed commented 8 years ago

What I want is to basically use the improved gF from a quickfix window, and control where it gets opened - e.g. in the previous window (after wincmd p), or a new tab. For the most control I would like to get the matching spec (trying all, or by providing a list) and then a method to do what vim-fetch does itself given this spec to open the file.

My hack for this is the following in ~/.vim/after/ftplugin/qf.vim:

function! s:open_in_prev_win()
  " does not contain line number
  let cfile = expand('<cfile>')
  let f = findfile(cfile)
  if !len(f)
    echom "Could not find file at cursor (".cfile.")"
    return
  endif

  " Delegate to vim-fetch.  Could need better API
  " (https://github.com/kopischke/vim-fetch/issues/13).
  let f = expand('<cWORD>')
  wincmd w
  silent exe 'e' f
endfunction

nmap <buffer> o :call <SID>open_in_prev_win()<cr>
wsdjeg commented 5 years ago

yeah, a nice feature request. Maybe be this plugin could provide a function and accept a dict argv:

{
     'filename' : needed,
     'line' :  default 1,
     'col' : default 1,
     'win' : default 0, current Windows,
     'tab' : default 0, current tab
}

with this function, user can create there own mappings.

for example,

nnoremap gf :call fetch#open(
     \ 'filename' : filename,
     \ 'line' : line,
     \ 'col' : col,
     \ 'win' : 'w',
     \ }