Wednesday, 7 October 2009

Making real use of Python in gedit

If you are a fan of gedit, and a fan of Python like me then the chances are that you have tried out the gedit Python console plugin at some point. Years ago I tried it out but realised it was really aimed at plugin developers and not at people wanting a quick and easy way to manipulate data in the text editor.

Last week I wrote a simple gedit plugin and so learned about the API and how to manipulate text in the editor. Even once I had learned about the API it still seemed far to complex to be usefull for quick data manipulation of an open file, so I decided to make a simple script that made things simpler, and makes the gedit Python console do what I wanted all along.

Get It

Save this script as ~/.gnome2/gedit/plugins/
Now open up the Python console in gedit and type import doc

Use It

import doc
d = doc.Doc(window)
d.set_lines( ['one', 'two', 'three'] )
lines = d.get_lines()

Now you can use regular expressions on you're open documents :)


__init__(self, window)
Create Doc instance
append(self, text)
Add text to the bottom of the document
Clear the document
Return the contents of the line that the cursor is currently on
Return a list containing all of the lines in the document
Return the selected text
Return the full text of the document
set_lines(self, lines)
Replace the current document with the lines in the supplied list
set_text(self, text)
Set the full text of the document

No comments:

Post a Comment