Blame plugins/gedit-plugin/devhelp.py

Packit 116408
# -*- coding: utf-8 py-indent-offset: 4 -*-
Packit 116408
#
Packit 116408
#    Gedit devhelp plugin
Packit 116408
#    Copyright (C) 2006 Imendio AB
Packit 116408
#    Copyright (C) 2011 Red Hat, Inc.
Packit 116408
#
Packit 116408
#    Author: Richard Hult <richard@imendio.com>
Packit 116408
#    Author: Dan Williams <dcbw@redhat.com>
Packit 116408
#
Packit 116408
#    This program is free software; you can redistribute it and/or modify
Packit 116408
#    it under the terms of the GNU General Public License as published by
Packit 116408
#    the Free Software Foundation; either version 2 of the License, or
Packit 116408
#    (at your option) any later version.
Packit 116408
#
Packit 116408
#    This program is distributed in the hope that it will be useful,
Packit 116408
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 116408
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 116408
#    GNU General Public License for more details.
Packit 116408
#
Packit 116408
#    You should have received a copy of the GNU General Public License
Packit 116408
#    along with this program; if not, write to the Free Software
Packit 116408
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Packit 116408
Packit 116408
from gi.repository import GObject, Gio, Gtk, Gedit
Packit 116408
import os
Packit 116408
import gettext
Packit 116408
Packit 116408
class DevhelpAppActivatable(GObject.Object, Gedit.AppActivatable):
Packit 116408
Packit 116408
    app = GObject.Property(type=Gedit.App)
Packit 116408
Packit 116408
    def __init__(self):
Packit 116408
        GObject.Object.__init__(self)
Packit 116408
Packit 116408
    def do_activate(self):
Packit 116408
        self.app.add_accelerator("F2", "win.devhelp", None)
Packit 116408
Packit 116408
        # Translate actions below, hardcoding domain here to avoid complications now
Packit 116408
        _ = lambda s: gettext.dgettext('devhelp', s)
Packit 116408
Packit 116408
        self.menu_ext = self.extend_menu("tools-section")
Packit 116408
        item = Gio.MenuItem.new(_("Show API Documentation"), "win.devhelp")
Packit 116408
        self.menu_ext.prepend_menu_item(item)
Packit 116408
Packit 116408
    def do_deactivate(self):
Packit 116408
        self.app.remove_accelerator("win.devhelp", None)
Packit 116408
        self.menu_ext = None
Packit 116408
Packit 116408
class DevhelpWindowActivatable(GObject.Object, Gedit.WindowActivatable):
Packit 116408
Packit 116408
    window = GObject.Property(type=Gedit.Window)
Packit 116408
Packit 116408
    def __init__(self):
Packit 116408
        GObject.Object.__init__(self)
Packit 116408
Packit 116408
    def do_activate(self):
Packit 116408
        action = Gio.SimpleAction(name="devhelp")
Packit 116408
        action.connect('activate', lambda a, p: self.do_devhelp(self.window.get_active_document()))
Packit 116408
        self.window.add_action(action)
Packit 116408
Packit 116408
    def do_deactivate(self):
Packit 116408
        self.window.remove_action("devhelp")
Packit 116408
Packit 116408
    def do_update_state(self):
Packit 116408
        self.window.lookup_action("devhelp").set_enabled(self.window.get_active_document() is not None)
Packit 116408
Packit 116408
    def _is_word_separator(self, c):
Packit 116408
        return not (c.isalnum() or c == '_')
Packit 116408
Packit 116408
    def do_devhelp(self, document):
Packit 116408
        # Get the word at the cursor
Packit 116408
        start = document.get_iter_at_mark(document.get_insert())
Packit 116408
        end = start.copy()
Packit 116408
Packit 116408
        # If just after a word, move back into it
Packit 116408
        c = start.get_char()
Packit 116408
        if self._is_word_separator(c):
Packit 116408
            start.backward_char()
Packit 116408
Packit 116408
        # Go backward
Packit 116408
        while True:
Packit 116408
            c = start.get_char()
Packit 116408
            if not self._is_word_separator(c):
Packit 116408
                if not start.backward_char():
Packit 116408
                    break
Packit 116408
            else:
Packit 116408
                start.forward_char()
Packit 116408
                break
Packit 116408
Packit 116408
        # Go forward
Packit 116408
        while True:
Packit 116408
            c = end.get_char()
Packit 116408
            if not self._is_word_separator(c):
Packit 116408
                if not end.forward_char():
Packit 116408
                    break
Packit 116408
            else:
Packit 116408
                break
Packit 116408
Packit 116408
        if end.compare(start) > 0:
Packit 116408
            text = document.get_text(start,end,False).strip()
Packit 116408
            if text:
Packit 116408
                # FIXME: We need a dbus interface for devhelp soon...
Packit 116408
                os.spawnlp(os.P_NOWAIT, 'devhelp', 'devhelp', '-s', text)
Packit 116408
Packit 116408
# ex:ts=4:et: