Thursday, 29 December 2011

My first Gedit plugin

Hi :)
Today I am posting my first plugin for Gedit.
It is a word mixer that takes each line of the document and changes the order of the words. It is not very useful, but I learned a lot while developing it:
I've learned how to use the Gtk+ API in Python, specially how GtkTextView, GtkTextBuffer and GtkTextIter work. I've learned how to add a new menu item in the menu bar too, and how a plugin works.

Also, I've realized how to manage a git repository -I have some experience using Subversion and Mercurial, but I have never used git before!-, since I had to use the basic commands to create a repository, commit my changes and push them.

Basically, to create a plugin in Python you need two files: a .plugin and a .py.

Here is my wordMixer.plugin file:

[Plugin]
Loader=python
Module=wordMixer
IAge=3
Name=Word Mixer
Description= A plugin that changes the order of the words of a document.
Authors=Laura Lazzati


An here, my wordMixer.py file:

from gi.repository import GObject, Gtk, Gedit
import re

UI_XML = """<ui>
<menubar name="MenuBar">
<menu name="ToolsMenu" action="Tools">
<placeholder name="ToolsOps_3">
<menu name="Submenu" action="SubmenuAction">
<menuitem name="WordMixer" action='WordMixerAction'/>
</menu>
</placeholder>
</menu>
</menubar>
</ui>"""

class WordMixerPlugin(GObject.Object, Gedit.WindowActivatable):

__gtype_name__ = "WordMixer"
window = GObject.property(type=Gedit.Window)


def __init__(self):
GObject.Object.__init__(self)

# Adds a submenu with the WordMixer option to the Tools menu
def _add_ui(self):
manager = self.window.get_ui_manager()
self._actions = Gtk.ActionGroup("PerformAction")
self._actions.add_actions([ ('SubmenuAction', Gtk.STOCK_INFO, "Submenu",None, "This is a submenu", None),
('WordMixerAction', Gtk.STOCK_INFO, "WordMixer",
None, "Changes the order of the words of the document",
self.on_action_activate),
])
manager.insert_action_group(self._actions)
self._ui_merge_id = manager.add_ui_from_string(UI_XML)
manager.ensure_update()


def do_activate(self):
self._add_ui()


def do_deactivate(self):
self._remove_ui()


# Activates the submenu in the Tools menu only if there is a view. Otherwise it remains insensitive.
def do_update_state(self):
view = self.window.get_active_view()
sensitive = False
if view:
sensitive = True
submenuAction = self._actions.get_action('SubmenuAction')
submenuAction.set_sensitive(sensitive)

# Takes each line of the buffer, changes it order, and replaces the line in the buffer with the mixed one.
def _mixer(self, viewBuffer):
firstIter = viewBuffer.get_start_iter()
nextIter = viewBuffer.get_start_iter()
nextIter.forward_line()
while firstIter.equal(viewBuffer.get_end_iter())<> True:
textBuffer = viewBuffer.get_text(firstIter,nextIter,1)
textList = re.split('\s',textBuffer)
textList.reverse();
newText = " ".join(textList)
newText += "\n"
viewBuffer.delete(firstIter, nextIter)
viewBuffer.insert(firstIter, newText)
nextIter = firstIter.copy()
nextIter.forward_line()

# Mixes the lines of the view's buffer.
def on_action_activate(self, action, data=None):
view = self.window.get_active_view()
if view:
viewBuffer = view.get_buffer()
self._mixer(viewBuffer)

# Removes the submenu from the Tools menu.
def _remove_ui(self):
manager = self.window.get_ui_manager()
manager.remove_ui(self._ui_merge_id)
manager.remove_action_group(self._actions)
manager.ensure_update()



In addition, here is the link to my repository in Github.

2 comments:

  1. Congratulations on your first gEdit plugin!

    ... I recently updated a small gEdit plugin I did (just 60 lines of code) and maybe you'll find it useful to learn some more tricks:

    https://github.com/dfuenzalida/gedit-git-branch-statusbar

    Best regards,

    Denis

    ReplyDelete