maandag 25 juni 2007

Finally some time

The exams are over so I have some time again to blog, and to continue work on snipper. I have some big plans for it :-)

I want to add some features,
fix some bugs,
make it easier to adapt,
make it easier to make it work on windows.

When I have done that, I'll try to write a plugin for some other text editor to use snipper, I want to see how hard that is, because I would love it if I could use snipper in more than one editor.
The advantages are obvious: you only need to have on template file per filetype and you can use it on all your editors.
This may be a bit of a dream, but it should be rather easy to let it work with at least some editors so I'll try it!

I still haven't got much time, so the development won't be too fast for the moment.

dinsdag 29 mei 2007

Vim colorsheme

if you're looking for a good colorsheme for vim,
I found this site

Snipper 0.2 released

I've just released snipper 0.2,

snipper 0.2 is a completely refactored version of 0.1,
now it should work with python 2.4 and it also works when editing multiple buffers with different filetypes at the same time

download it here
or on vim.org

After this refactoring, I think I will be able to add new features much easier, so new features will be coming soon (but not too soon because I have to study also)

zondag 27 mei 2007

Snipper

Snipper 0.1 released,

After some days of working on vim scripting in python, I wanted to try the real deal, write a script that is really useful, so I wrote a snippets script for vim.

For all of you who don't know what snippets are: snippets or templates are predefined pieces of text that you would like to insert in your text fast. You basically type an alias and press a button and the complete text is inserted.
I will be making a video of it soon but for now if you want more information, look at http://scribes.sourceforge.net/ or at http://macromates.com/, two cool editors for if you don't know vim :-)

So now everyone knows what snippets are, I can continue my explanation of my script that you can find on https://launchpad.net/snipper/, download it here
Documentation will be available very soon. (for installation and use)

This is the first version of the script, so expect some bugs. If you find any bugs that haven't been posted, please do (https://bugs.launchpad.net/snipper/)

I hope you like this script (if you do, please add a comment) and stay tuned for more info.

woensdag 23 mei 2007

some helpful vim tips

Hi,

In this blog I want to show a few helpful tips about vim, that you can use in my tutorials about vim scripting in python

first of all
everyone should know the help command!
:help somecommand

syntax highlighting on in console
:syn on

set commands based on filetype
first set
:filetype on
now, vim will try to recognize the type of file you are editing,
then you can use
au FileType python source /path/to/python/commands

au, automatically execute on event
source, read the source file with commands

execute command from .vimrc without having to press enter
:someCommand()^M
and ^M you have to enter by typing <ctrl-v><ctrl-m> (so pressing those combinations)

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

My first blog

Hi,

I just started a blog,
I will try to write regular about things I'm most interested in, like linux, ubuntu, python, kde.

Hopefully you like it,

Thomas

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