Thursday, March 24, 2016

Re: Build cexpr from result of python code.

On Monday, March 21, 2016 at 4:45:45 PM UTC+1, ZyX wrote:
> 2016-03-21 17:15 GMT+03:00 Ni Va <nivaemail@gmail.com>:
> > Hi,
> >
> > I am using Execute-selection-in-Python-and-append-master plugin in order to test python code directly through vim. Now the python code is working and return me a list that contains strings typed like "file linenumber linecontent".
> >
> > I would like not to replace buffer content by result of python code but affect cexpr to the list content in order to navigate through results with quickfix list.
>
> You can use
>
> vim.Function('setqflist')([{'filename': file, 'lnum': linenumber,
> 'text': linecontent}])
>
> to set quickfix list from Python code. This assumes that you are doing
> parsing yourself.
>
> Or pyeval() in :cexpr to make Vim do parsing. Just return `output`
> directly without doing anything with `r` and your function should be
> ready for use in :cexpr assuming correct &errorformat value.
>
> >
> > Can you help me to build that cexpr list ?
> >
> > By advance Thank you
> >
> >
> > Here it is the code of PyExecReplace that replace content of my buffer (python code) by result of execution code.
> >
> >
> > python << EOL
> > import vim, StringIO,sys
> >
> > def PyExecReplace(line1,line2):
> > r = vim.current.buffer.range(int(line1),int(line2))
> > redirected = StringIO.StringIO()
> > sys.stdout = redirected
> > exec('\n'.join(r[:]) + '\n')
> > sys.stdout = sys.__stdout__
> > output = redirected.getvalue().split('\n')
> > r[:] = output[:-1] # the -1 is to remove the final blank line
> > # vim.command("echo "+"-".join(("a", "b", "c")))
> > redirected.close()
> > EOL
> > command! -range Pyer python PyExecReplace(<f-line1>,<f-line2>)
> > command! -range Pyea python PyExecAppend(<f-line1>,<f-line2>)
> >
> > --
> > --
> > You received this message from the "vim_use" maillist.
> > Do not top-post! Type your reply below the text you are replying to.
> > For more information, visit http://www.vim.org/maillist.php
> >
> > ---
> > You received this message because you are subscribed to the Google Groups "vim_use" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
> > For more options, visit https://groups.google.com/d/optout.


Thank you ZyX !

I give you the following code if it can help for someone else. Execute-selection-in-Python-and-append-master with a third function that returns only output :
python << EOL
import vim, StringIO, string, sys
def PyExecAppend(line1,line2):
r = vim.current.buffer.range(int(line1),int(line2))
redirected = StringIO.StringIO()
sys.stdout = redirected
exec('\n'.join(r[:]) + '\n')
sys.stdout = sys.__stdout__
output = redirected.getvalue().split('\n')
r.append(output[:-1]) # the -1 is to remove the final blank line
redirected.close()

def PyExecReplace(line1,line2):
r = vim.current.buffer.range(int(line1),int(line2))
redirected = StringIO.StringIO()
sys.stdout = redirected
exec('\n'.join(r[:]) + '\n')
sys.stdout = sys.__stdout__
output = redirected.getvalue().split('\n')
r[:] = output[:-1] # the -1 is to remove the final blank line
redirected.close()

def PyExecOut(line1,line2):
r = vim.current.buffer.range(int(line1),int(line2))
redirected = StringIO.StringIO()
sys.stdout = redirected
exec('\n'.join(r[:]) + '\n')
sys.stdout = sys.__stdout__
output = redirected.getvalue().split('\n')
redirected.close()
return output
EOL
command! -range Pyeo python PyExecOut(<f-line1>,<f-line2>)
command! -range Pyer python PyExecReplace(<f-line1>,<f-line2>)
command! -range Pyea python PyExecAppend(<f-line1>,<f-line2>)




Then I have just a last problem, in my main code that build setqflist I have only the last occurence and not all (6 occurences) despite the 'a' option.


for filepath in myDict.keys():
for line in myDict[filepath]:
print filepath + " line " + '{:>5}'.format(str(line[0])) + " : " + line[1]
vim.Function('setqflist')([{'filename': filepath, 'lnum': line[0], 'text': line[1]},'a'])

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment