Gedit Python Plugins

I have been putting together a python plugin system for gedit, so far it seems to work:

Gedit showing the python console

I heavily used the nautilus-python, and epiphany source code to see how this is done, thanks to them !

Notice how the window title has been updated by the command in the console. The clever reader may also recognize the same python console as used in epiphany, simply because i just stole all it’s code, thanks Adam Hooper !

Now for the code, it looks like:

import gedit

# Inheriting from gedit.Plugin is the key here
class MyPlugin(gedit.Plugin):
	def __init__(self):
		gedit.Plugin.__init__(self)

	def activate(self, window):
		pass

	def deactivate(self, window):
		pass

	def update_ui(self, window):
		pass

The three callbacks are the same used for C plugins, you can do your init/deinit stuff and react to UI changes. The other thing to do is to write a xx.gedit-plugin file to tell gedit that there is a plugin:

[Gedit Plugin]
Module=console
IAge=2
Lang=python
Name=Python Console
Description=A Python Console inside gedit.
Authors=Raphael Slinckx 
Copyright=Copyright © 2005 Raphael Slinckx
Website=http://www.gedit.org

You must specify the langage type, so gedit knows which loader to use, and use Module to give the module name of your plugin, generally the filename without the .py extension, but it can also be a directory containing __init__.py !

Now, Paolo if you don’t mind, you can come back on IRC 🙂

11 Responses to “Gedit Python Plugins”

  1. Hey, I don’t know if this is all of the code that’s required, but if not did you make it public anywhere? At least with the code you provide here in this article, I can’t even get it to show up in gedit’s plugin list :-/. Best regards, Raphael

  2. ebassi says:

    now this is it.

    you are forcing me to write a perl plugin for gedit. 😉

  3. kikidonk says:

    Raphael: No this is only a patch for the moment, and it is made for the new 2.14 version of gedit, so nothing to try yet 🙂

    ebassi: Perl aaaarghh 🙂

  4. Has anybody every talked “abstracting” something like this to point where it’s generic enough to be used by any program? Scritpting MS Office with VBA (i know, i know) is really pretty cool if you try it. It’d be damn hot to have python-scriptability all throughout gnome. I’d be particularly interested in scripting apps like gaim and rhythmbox.

    Basically, your plugin constructor gets passed a reference to a top-level apllication object (kind of like the dom, come to think of it) that you just traverse…

    something like this (bad pseudocode):

    def __init__(self, app):
    # find the toolbar
    toolbar = app.toolbar
    item = gtk.Button(‘My Plugins Button’)
    item.connect(‘pushed’, self.on_button_pushed)
    toolbar.add(item)

    def on_button_pushed(self, widget):
    # etc…

    that would be hot! The key there is being passed the ‘root’ application object, not necessarily just a window.

    something for the hopefully reincarnated libgnome library? a feature of GnomeApp?

    though for food 🙂

  5. Police says:

    You are under arrest for stealing, blah blah blah.

  6. Seth Vidal says:

    Any chance of some examples being posted of this plugin doing something we can relate to? Like code folding, for example, would that be possible w/this interface?

  7. lukas sabota says:

    Is this in upstream gnome? Can I write python plugins for gedit? I’m using gnome 2.13, and there is no gedit module…

  8. Raphael Slinckx says:

    Yes it is, there is no gedit module, but you can acces it once you load the plugin through gedit.

  9. I am n00B to gedit plugin development. I have been searching for tutorials online and so far I haven’t found anything worthwhile. I would be glad to port some plugins from vim to gedit. If you could direct me to a gedit-plugin-authoring-quickstart sort of resource, it would be great.

  10. Note that the Lang=python line should read Loader=python for gedit 2.14.

  11. Zac Carter says:

    Have you gotten this to work on gedit 2.8.1? I put my files in ~/.gnome-2/gedit/plugins/ as instructed but no changes appear in the list of plugins in gedit.