My first gedit plugin

[image]

I was looking at the various gedit plugins yesterday, and decided I wanted to write my own. I actually really like gedit as a text editor - it's lightweight and with the plugins provides nearly as much functionality as UltraEdit or Textmate. It's actually amusing that the default text editor in Gnome is as powerful as it is - coming from Windows, you're used to Notepad being a piece of junk and having to find other apps to do any real work. This can be a bit of an issue, as if I've turned on line numbers and highlighting, and then open up a random text file for a README or something, the use cases sort of overlap (not that it bothers me that much).

Anyways, I decided to create a *really simple* plugin for inserting a timestamp - I use text documents to record ideas and todos, and I always put in a timestamp before I start writing. There's already a plugin that's included called "Insert Date" but by default it pops up a dialog to ask you for a format each time. It wasn't until *AFTER* I wrote a simple plugin to do the same thing that I noticed that you can just go into "configure plugin" and choose the default format. I'm glad I didn't notice though, since that allowed me to learn how plugins work and create a sort of "canonical" plugin that I can expand on later.

If you follow the gedit plugin how-to instructions, you can see that the document writers broke the cardinal rule of beginning how-tos by including crap you don't need at first. By looking at some other plugins and stripping away the excess, I got down to a super-simple plugin that simply adds a menu, then does something to the document (in this case adding a timestamp). SIMPLE!

I'll just link to the two files needed to run the plugin: inserttimestamp.gedit-plugin and inserttimestamp.py . (If you want to try them, just put them in ~/.gnome2/gedit/plugins). You can see that it's cut down to just about the bare-minimum. I wish I understood a bit more about the menu stuff - you have to use the GTK API for that, and it uses some default params that I don't understand exactly for where to place the menu. But the code itself doesn't have much more than it needs to work. This is actually useful to me as a reference so that in the future if I find myself needing some sort of automation - it'll be easy to use that as a template to whip up something more complex quickly.

Actually at first what I wanted to write a plugin that would put the word-wrap option in menu, instead of in the preferences like they are now. Sadly it looks like the Python wrapper to access that API isn't available (or at least I couldn't find it). If you know more than me, please educate me: Here's the C API for gedit, and you can see there's a "gedit-prefs-manager" module (I think that's what it's called, it's not a Class), but there is no equivalent if you do a "dir(gedit)" from gedit Python console - the 'utils' is there, and the encoding stuff, but not the pref-manager. Maybe I'm missing how to get to that stuff, but I haven't found it yet after some searching.

Speaking of the Python console, it's pretty interesting if you haven't played with it yet. Turn it on in the Plugins preferences, and then display the bottom pane in the View menu and it's there, and by default it's imported the gedit module and "window" has already been populated with the current window. I've done app automation stuff before using Visual Basic and COM, but having the console actually in the app I'm controlling is quite cool. (And it's Python, not Lua like what Scite uses, which is nice in many ways.)

Try this, open the console, and do the following...


You can access the main window through 'window' :
<gedit.Window object at 0xb65fe694 (GeditWindow at 0x8141000)>
>>> doc  = window.get_active_document()
>>> doc.insert_at_cursor("hello world")
>>> view = window.get_active_view()
>>> view.select_all()
>>> view.copy_clipboard()
>>> view.paste_clipboard()
>>> view.paste_clipboard()

You get the idea, it's basic document automation - but it's nicely done. Do a dir() on App, Window or Document to get an idea on the various things you can do with the API - it seems pretty clear. Though honestly, I wish there was more documentation than just the C API I've found, it's pretty easy to put the pieces together.

:-)

-Russ

< Previous         Next >