Posts tonen met het label python vim scripts py vi tutorial tutorials howto. Alle posts tonen
Posts tonen met het label python vim scripts py vi tutorial tutorials howto. Alle posts tonen

maandag 21 mei 2007

python scripting in vim (2)

Hi,

Hopefully you all found my first tutorial helpfull,
this will be a second more advanced tutorial about scripting vim in python.
In this tutorial we will make a script to test if your python script doesn't contain syntax errors, we will use pyflakes for this

let' s get started with the module pyflakse,
you can find pyflakes on http://divmod.org/trac/wiki/DivmodPyflakes
install it on ubuntu: apt-get install pyflakes

We will use pyflakes because it is fast, if you're interested you can also look at pylint and pychecker.

first let's do some tests with pyflakes
open some python interpreter:


>>>file = "/path/to/file"
>>>code = open(file, 'r').read()
>>>tree = compiler.parse(code)
>>>w = pyflakes.Checker(tree)
>>>for warning in w.messages: print warning


if your file contains an error, it should be shown. If the code has a syntax or indentation error, the compiler fase will fail but we will handle that in the script.

Now lets write this in a vim script,
we will write this script in a seperate file,
this is because we can keep or .vimrc clean and because we only want to load this file when we are editing python scripts (see this blog)

the script we will write, will toggle a buffer to show the errors. If no errors are found the scirpt will just write a message.
We write 4 functions,
checkSyntax
doSyntaxCheck
closeBuffer
bufferOpen

checkSyntax is the main function that you have to call when you want to have a map to this script


#!/usr/bin/python

import vim
import pyflakes
import compiler

def checkSyntax():
"""This function checks if the output buffer is already open. If it is open,
we will close it (because we want to toggle). If it is not open, we will
create
the output buffer and do the real syntax check with (doSyntaxCheck)
"""
#first save the current buffer
current = vim.current.buffer
#check if the buffer is open
b = bufferOpen()
if not b:
vim.command("bel silent new output")
output = vim.current.buffer
doSyntaxCheck(current, output)

else:
closeBuffer(b)


def doSyntaxCheck(current, output):
"""This function will do the real syntax check, first we will create a string
that contains
the current buffer, we will then try to parse the code with compiler and then
run pyflakes on it.
All errors are printed in the output window, if no errors are found, a message
is printed and the buffer is closed again
"""
#convert the list of lines to a string
code = "\n".join(current)
try:
tree = compiler.parse(code)
except (SyntaxError, IndentationError), inst:
output[0:0] = [str(inst).strip()]
else:
w = pyflakes.Checker(tree)
w.messages.sort(lambda a,b: cmp(a.lineno, b.lineno))

if w.messages:
for warning in w.messages:
output[0:0] = [str(warning).strip()]

else:
closeBuffer(output)
print "code ok"


def bufferOpen():
"""This function checks all the buffers if a buffer with a name containing
output exists. If it exists we return that buffer, else we return None
"""
for b in vim.buffers:
if "output" in b.name:
return b

return None


def closeBuffer(buffer):
"""Completely delete buffer"""
cmd = "bwipeout! " + buffer.name

vim.command(cmd)


Stay tuned...
I hope you all liked this tutorial.
reactions are always welcome, and I'll do my best to publish a new tutorial soon!

zondag 20 mei 2007

python scripting in vim (1)

This is a short tutorial about writing vim scripts in python.
This tutorial is for everyone who likes vim and python.

Preperations:

To write vim scripts in python you need to have vim compiled with +python

Fedora/CentOS: vim-enhanced
Debian/Ubuntu: vim-python

Let's get started:

first a thing about how you can use python for vim-scripting,
you can set these things in your ~/.vimrc
  • autocmd FileType python [commands]
    Run commands when this type of file is edited
  • python [python code]
    Evaluate the python code given
  • python << EOF
    Read python code untill the EOF
  • pyfile [filename]
    Read python commands from a python file
  • map [keys] [command]
    Run the specified command when the key(s) are pressed
Simple start...

"first tutorial hello world
imap & :python pythonHelloWorld()
python << EOF

#start the function
def pythonHelloWorld():
import vim #so you can use the vim functions
buffer = vim.current.buffer #returns the current buffer
buffer[0] = "hello world"
#sets the first line of the buffer to "hello world"

EOF

What we have so far
a line starting with " is a comment in the .vimrc

imap & :python pythonHelloWorld()

press the & in insert mode to execute the command :python pythonHelloWordl(), notice the so you leave insert mode and go to the command mode.

python << EOF

everything between these lines will be read as python code


slightly improved version
We will improve the previous version so that we insert the "hello world" line before the current line, so we don't overwrite anything.

def pythonInsertHelloWorld():
import vim
buffer = vim.current.buffer
#returns the row and column of the current cursor
#position
(row, col) = vim.current.window.cursor
#python starts numbering from 0, vim from 1.
#insert line above current cursor position
#the argument must be a list, or it won't work
buffer[row-1:row-1] = ["hello world"]
EOF


and further...
this was the first really basic tutorial, more will follow soon, but you should have a start now.
If you have questions, see mistakes or have comments, please add your reaction.


see also:
about python scripting:http://www.tummy.com/Community/Presentations/vimpython-20070225/vim.html
in vim :help python
help about map: http://www.linux.com/article.pl?sid=06/06/08/1431210