Gedit 3 Plugin Sample

Required bindings not yet included in vala:
Gedit >= 3.0 (gedit-3.0.vapi)
GtkSource View >= 3.0 (gtksourceview-3.0.vapi)
PeasGtk-1.0 gobject introspection package of your distro
…and Vala >= 0.11

Source & support files
A Gedit plugin is composed of just two files: a library (.so) and the plugin file definition.

Our example plugin will be contained in just one vala soure file, so with all the dependencies in place this should be the directory listing:

$ ls
gedit-3-example.plugin
gedit-3-example-plugin.vala
gedit-3.0.vapi
gtksourceview-3.0.vapi

In order to make the example really simple we decided to implement a very basic function: the plugin will just close an xml tag upon writing the ‘>’. Eg. if you write it will add the corresponding close tag.

Source code for the file: gedit-3-example-plugin.vala

using GLib;
 
namespace GeditPluginExample
{
        /*
         * This class will be instantiated and activated for each Gedit View
         */
        public class View : Gedit.ViewActivatable, Peas.ExtensionBase
        {
                public View ()
                {
                        GLib.Object ();
                }
 
                public Gedit.View view {
                         get; construct;
                }
 
                public void activate ()
                {
                        print ("View: activated\n");
                        view.key_release_event.connect (this.on_key_release);
                }
 
                public void deactivate ()
                {
                        print ("View: deactivated\n");
                        view.key_release_event.disconnect (this.on_key_release);
                }
 
                private bool on_key_release (Gtk.Widget sender, Gdk.EventKey event)
                {
                        if (event.str == ">") {
                                // Close the tag
                                Gedit.View view = (Gedit.View)sender;
                                Gtk.TextBuffer buffer = view.get_buffer ();
                                Gtk.TextIter end, start;
 
                                buffer.get_iter_at_mark (out end, (Gtk.TextMark) buffer.get_insert ());
                                if (end.backward_char ()) {
                                        start = end;
                                        if (start.backward_word_start ()) {
                                                string tag = "</%s>".printf (buffer.get_text (start, end, false));
 
                                                // add the closing tag
                                                buffer.begin_user_action ();
                                                buffer.insert_interactive_at_cursor (tag, -1, true);
                                                buffer.end_user_action ();
 
                                                // move cursor back
                                                buffer.get_iter_at_mark (out end, (Gtk.TextMark) buffer.get_insert ());
                                                end.backward_chars (tag.length);
                                                buffer.place_cursor (end);
                                        }
                                }
                        }
                        return true;
                }
        }
 
        /*
         * Plugin config dialog
         */
        public class Config : Peas.ExtensionBase, PeasGtk.Configurable
        {
                public Config () 
                {
                        Object ();
                }
 
                public Gtk.Widget create_configure_widget () 
                {
                        return new Gtk.Label (" Gedit 3.0 Example Vala Plugin ");
                }
        }
}
 
[ModuleInit]
public void peas_register_types (TypeModule module) 
{
        var objmodule = module as Peas.ObjectModule;
 
        // Register my plugin extension
        objmodule.register_extension_type (typeof (Gedit.ViewActivatable), typeof (GeditPluginExample.View));
        // Register my config dialog
        objmodule.register_extension_type (typeof (PeasGtk.Configurable), typeof (GeditPluginExample.Config));
}

Contents of the plugin definition file: gedit-3-example.plugin

[Plugin]
Module=gedit-3-example-plugin.so
IAge=2
Name=Vala Example Plugin
Description=A simple Vala Example Plugin
Authors=Andrea Del Signore <sejerpz@tin.it>
Copyright=Copyright © 2011 Andrea Del Signore
Website=http://live.gnome.org/action/Vala/Gedit3PluginSample

Compiling & Installing

$ valac --vapidir . -C gedit-3-example-plugin.vala --pkg gtk+-3.0 --pkg gedit-3.0 --pkg PeasGtk-1.0 --pkg GtkSource-3.0
$ gcc --shared -o libgedit-3-example-plugin.so gedit-3-example-plugin.c `pkg-config --cflags --libs gedit gtk+-3.0 gtksourceview-3.0 libpeas-gtk-1.0`
$ cp libgedit-3-example-plugin.so gedit-3-example.plugin ~/.local/share/gedit/plugins/

Running
Start GEdit 3 and enable the plugin from the edit -> preference menu

Over!

From: https://live.gnome.org/Vala/Gedit3PluginSample

Leave a Reply

Your email address will not be published. Required fields are marked *