I need to build a quickfix list based on output from some external command. But the command gives me only file names and line numbers, like:
foo.txt:10
bar.txt:20
I'd like to add the actual contents of the specified file into the quickfix list, like in:
foo.txt:10: this is some line from foofile
bar.txt:20: hello world, we're a line from barfile
Can this be done?
Ideally, I'd like this to be cross-platform, so probably in pure VimScript, with no calls to external commands like sed or the likes?
My current behavior can be simulated with a function like:
function! MyFunc()
let mylist = ["foo.txt:10:...", "bar.txt:20:..."]
cgetexpr mylist
copen
endfunction
call MyFunc()
and I'd like the ...
parts to become the contents from the real files...
Here's a solution:
fun! GetFileLine(fn,ln)
return readfile(a:fn,'',a:ln)[a:ln-1]
endfun
fun! AppendLineToFnLn(list)
return map(a:list, 'v:val.'':''.call(''GetFileLine'', split(v:val,'':'') )' )
endfun
fun! QuickFixWithLine(cmd)
cexpr AppendLineToFnLn(split(system(a:cmd),"\n"))
endfun
call QuickFixWithLine('echo myfile:22; echo myfile:40;')
copen
Hmh, based on a partially related question on comp.editors and :help readfile
, I'd say below might work, however wasteful:
function! MyFunc()
let mylist = ["foo.txt:10:...", "bar.txt:20:..."]
let result = []
for elem in mylist
let temp = split(elem, ":")
let line = elem . ":" . readfile(temp[0], "", temp[1])[temp[1]-1]
call add(result, line)
endfor
cgetexpr line
copen
endfunction
call MyFunc()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With