[gedit-list] Auto-closing brackets in selected area



Hi, this is my first post in the list.
Christiano Farina Haesbaert ask about a plugin that can enclose text
in brakets, sometime ago. I wrote a plugin that do exactly that
sometime ago, so i decide to share it.
it's in python, i attach the files.

some notes on gtbtextwraper:
in my pc this module is in other directory, thats the reason i modify
the python path in the plugin so i can import it, feel free to edit it

Attachment: textwraper.gedit-plugin
Description: Binary data

# Copyright (C) 2008 Jorge Cob
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

import gtk

class GTBTextWraper:
    '''GtkTextBuffer text wraper'''
    def __init__(self, start, end):
        self.start = start
        self.end = end
    
    def wrap(self, buff):
        '''Encierra entre self.start y self.end el texto seleccionado en buff
        Devuelve True si se realizo la operacion'''
        bounds = buff.get_selection_bounds()
        
        if len(bounds) != 0:
            buff.begin_user_action()
            
            offsets = bounds[0].get_offset(), bounds[1].get_offset()
                        
            buff.insert(buff.get_iter_at_offset(offsets[1]), self.end)
            buff.insert(buff.get_iter_at_offset(offsets[0]), self.start)
            
            # poner self.end fuera de la seleccion
            bounds = buff.get_selection_bounds()
            bounds[1].backward_chars(len(self.end))
            
            if bounds[0].equal(buff.get_iter_at_mark(buff.get_insert())):
                buff.select_range(bounds[0], bounds[1])
            else:
                buff.select_range(bounds[1], bounds[0])
            
            buff.end_user_action()
            
            return True
        
        else:
            buff.begin_user_action()
            
            buff.insert_at_cursor(self.start)
            buff.insert_at_cursor(self.end)
            
            # poner el cursor entre self.start y start.end
            insert = buff.get_iter_at_mark(buff.get_insert())
            insert.backward_chars(len(self.end))
            
            buff.place_cursor(insert)
            
            buff.end_user_action()
            
            return True

        return False

# -*- coding: utf-8 -*-
# Copyright (C) 2008 Jorge Cob
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

from gtk import gdk
import gedit

import sys, os
sys.path.append(os.path.expanduser('~/lib/python'))

from gtbtextwraper import GTBTextWraper

class TextWraperPlugin(gedit.Plugin):

    def activate(self, window):
        self.views = {}
        self.wrapers = {
            40: GTBTextWraper('(', ')'),
            91: GTBTextWraper('[', ']'),
            123: GTBTextWraper('{', '}'),
            39: GTBTextWraper("'", "'"),
            34: GTBTextWraper('"', '"')
        }

    def deactivate(self, window):
        for view, signal in self.views.items():
            view.disconnect(signal)

    def update_ui(self, window):
        buffer = window.get_active_document()
        view = window.get_active_view()

        if buffer == None or view == None:
            return

        if view not in self.views:
            self.views[view] = (view.connect('key-press-event',
                                self.on_key_press, buffer))

    def on_key_press(self, view, event, buffer):
        if event.keyval in self.wrapers:
            return self.wrapers[event.keyval].wrap(buffer)
        
        return False




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]