Search code examples
pythonpluginsgeditgnome-3

Can Gedit plugins bind to the OnSave event?


I'm writing a gedit 3 plugin, something similar to phsilva's PyLint plugin, which calls out to an external lint program and highlights lines of code in the current document. My question is, if my plugin has a run_lint action is it possible to bind that to the OnSave event in gedit? The list of available signals in the documentation I've linked above still has a FIXME notice against it and I'm struggling to figure out where in the soup of API documentation the full list can be found.


Solution

  • Well, no-one answered this, but I figured it out eventually. There are two steps to this, when a new tab is created in the window that tab contains a document. The document has loaded and saved signals that can be connected to actions. It's important to remember that each tab has a separate document, each of which needs its own set of signals and handlers.

    Here's an outline solution, in case it's useful to others:

    class FooPlugin(GObject.Object, Gedit.WindowActivatable):
    
        __gtype_name__ = 'Foo'
        ...
    
        def do_activate(self):
            self._add_ui()
            self.window.connect('tab-added', self.on_tab_added)
            ...
            return
    
        def on_tab_added(self, window, tab, data=None):
            doc = tab.get_document()
            doc.connect('saved', self.on_document_saved)
            doc.connect('loaded', self.on_document_loaded)
            return
    
        def on_document_loaded(self, document, data=None):
            # do something here...
            return
    
        def on_document_saved(self, document, data=None):
            # do something here...
            return