0

I'm diving into the quickfix list of Vim that looks quite awesome, however I am not being able to exploit this because of a relative path problem. Explanation:

My Vim window path is .

I have a custom make command in Vim:

:set makeprg=west\ build\ -b\ nucleo_l552ze_q\ .\

The west program will use CMake and gcc to compile my project. But here is the thing: it changes the current directory to ./build.

So, in Vim, when I run

:make

the quickfix list is filled with references like ../src/main.c instead of src/main.c

As a result, when I try to open a file from the quickfix list, Vim creates a new file using make's relative path instead of opening the file I wanted based on the Vim working directory.

enter image description here

My question is: How do I open :make entries that were compiled in different directory than the vim working directory?

Is it possible to change the root path of the quickfix list after running the make command ?

wxz
  • 2,254
  • 1
  • 10
  • 31
Dali
  • 344
  • 1
  • 10

3 Answers3

4

You could probably use :help getqflist() to retrieve the quickfix list, :help map() over it to change the value of filename or module with :help fnamemodify(), and set the quickfix list again with :help setqflist().

romainl
  • 186,200
  • 21
  • 280
  • 313
  • 1
    Thanks, this is the kind of method I was looking for, even if I hoped if it was possible to do it straight with built-in commands. I'll try to write a command with these functions and keep the thread updated. – Dali Mar 27 '21 at 09:43
  • I might be missing something here. The filename attr is not present in the qf dict entries. When parsing the getqflist() dict, the file name retrived by bufname(dictentry.bufnr) is correct (e.g. src/main.c). However the file path is '../src/main.c' when using the quickfix list window. Would you have an explanation for this path translation ? ('../src/path' is the one that is printed out by gcc) – Dali Mar 27 '21 at 11:38
  • I won't be able to help you with so little actionable info. Ask a separate question with your full code and a sample compiler output so that we can start from the same baseline. – romainl Mar 27 '21 at 12:09
2

IMO, it is easier to do :lcd build in your quickfix buffer to set the current working directory as seen by the tool.

Also, if your "west" tool is capable to print messages as it walks directory tree then quickfix is capable of parsing them too. BTW. this is how quickfix tracks GNU make current directory out-of-the-box. See :h errorformat, :h quickfix-directory-stack etc.

Matt
  • 13,674
  • 1
  • 18
  • 27
  • This is not convenient as the opened window from the quickfix inherits its working directory (so I can not longer use fzf by example). Then if I lcd back the file path '.../src/main.c' become broken, I have to re-open it. I could use another window just for the after-failed-build editions lcd'ed (and keep my regular working files opened), but then I will have to deal with the swap files and window realoading. – Dali Mar 27 '21 at 09:36
1

This vimscript file allows you to open the current entry in the quickfix window in a new tab, complete with substituting the make path with the vim path.

Create a file with exactly this path ~/.vim/after/ftplugin/qf.vim, then put this in the file:

" Get current list of quickfix entries "
" s: - script variable (local to this file) "
let s:qfw_entries = getqflist()

function! CreateTab()
    let l:current_line_no = line('.')
    let l:entry = s:qfw_entries[l:current_line_no - 1]
    let filepath = substitute(bufname(l:entry.bufnr), "../", "", "")
    if l:entry.valid
        " Close quickfix window "
        cclose
        lclose
        pclose

        " Open new tab "
        execute 'tabnew +'.l:entry.lnum.' '.filepath
    endif
endfunction

" Remaps the enter key to call CreateTab() on the current entry in quickfix "
nnoremap <buffer> <CR>    :call CreateTab()<CR>

The file path means this script will load in after the default qf files and the ftplugin means this stuff only modifies the quickfix filetype. This can be modified to open new windows, splits, and use different hotkeys if desired. This substitution will only work if the build path of make is always in the same location.

wxz
  • 2,254
  • 1
  • 10
  • 31