Hi all,
I have a problem when scripting vim with python which is not covered by the FAQ
(http://vimdoc.sourceforge.net/cgi-bin/vimfaq2html3.pl), and would be grateful for help (or perhaps a redirect to a list
more directly connected to vim scripting ?)
I am using vim 7.0, and python 2.4.2
I have vim (or gvim) running with a python script in the current buffer and try to parse it. When parse errors occur I
would like to move to the line with the error, and then ask the user what to do about it (or maybe just explain what the
error is if I ain't no clue what to do).
So, bypassing a few key-redirects, and extra scripting what happens is
essentially:
:py pim.parseBuffer()<CR>
Which calls the parseBuffer() method within
/usr/lib/python2.4/site-packages/pim.py, and that looks like:
<python>
def normal(command):
vim.command('normal %s' % command)
def parseBuffer():
source = '\n'.join(vim.current.buffer)
try:
code = compile(source, 'file.py', 'exec')
except SyntaxError, e:
msg, (file,row,col,str) = e.args
normal('%dG0%dl' % (row,col))
normal('zz')
line = vim.current.buffer[row - 1]
if col > len(line) and missingColonPattern.match(line):
#x = vim.eval('confirm("Replace missing colon on line
%d","&Yes\n&No\n&Cancel")' % (row))
#x = vim.eval('input("Add missing colon on line %d [Y]
?")' % row)
#if x and (x.lower() == 'y' or x.strip() == ''):
vim.command('s/$/:/c')
#else:
#print >> sys.stderr, 'Answer: %s\n\n\n\n' % x
#etc
</python>
ie: compile the contents of current buffer, if there is an error, find row/col, go to that row, then if the error is a
missing colon, ask whether it should be added.
The problem I have is that "vim.command('s/$/:/c') is the only version which actually looks correct. With the other two
versions (using confirm and input) the question appears first, and *after* it has been answered the cursor moves to the
correct line.
The code above uses "'%dG0%dl' % (row,col)" to move to the required line. I have also tried ''':%d^M''' % row, and
vim.current.window.cursor = (row,col), but the effect is the same.
I have also seen the same effect with output, for example
normal('%dG0%dl' % (row,col))
# other stuff
print "hello world"
and
normal('%dG0%dl' % (row,col))
# other stuff
print >> sys.stderr, "hello world"
it can be seen that the (error) message is printed first, and later the cursor
moves.
Any suggestions as to where I might be going wrong, and how I can get the
instructions to appear back in the right order ?
--
Alan