[python] pygtk text buffer

spooky.ln na tbs-software.com spooky.ln na tbs-software.com
Pondělí Leden 14 15:35:17 CET 2008


Zdravim,
Nenajde se nekdo ,kdo by mi pomohl s jednoduchou syntaxi v
gtk.TextBufferu?
Docela to ted funguje ale narazil jsem na par problemu
a to je napr:
slovo Program obarvi spravne. Program( uz ne.
Pri delete na Progra odbarvi spravne,ale pokud mazu a zbyde
mi jen jeden znak v bufferu program se kousne.
Potrebuju jen jednoduchy "obarvovac" predem definovanych
tagu kde vstup bude slovnik ve tvaru{"keyword": "styl"}

nechci zavadet moc modulu. Ciste jen GTK. Pygtksourceview je
dobrej nicmene ho nechci. Chci multiplatformni reseni.
Pygtkcodeview ujde[ s autorem jsem v kontaktu]
nicmene pouziva xml modul a to se mi zase nelibi. Nehlede na
to ze otevreni a "obarveni" delsiho zdrojaku je k uzoufani.

tady je ten muj malej zdrojak:

#-------------------------------------------------------------------
# Tag Buffer
# Pokus o napsani vlastniho jednoducheho syntax parseru
# vstup: data v podobe slovniku
#        key je Keyword a value je typ tagu pouzity
#        pro syntaxi
# com dot tbs-software at spooky.ln [ reverse it !!! ]
#-------------------------------------------------------------------
import gtk

class TagBuffer(gtk.TextBuffer):
    def __init__(self, keywords):
        gtk.TextBuffer.__init__(self)
        self.keywords = keywords
        
        self.redTag = gtk.TextTag(name="Red")
        self.redTag.set_property("foreground", "dark red")
        self.redTag.props.family="Courier"
        
        self.greenTag = gtk.TextTag(name="Green")
        self.greenTag.set_property("foreground", "dark
green")
        self.greenTag.props.family="Courier"
        
        self.blueTag = gtk.TextTag(name="Blue")
        self.blueTag.set_property("foreground", "dark blue")
        self.blueTag.props.family="Courier"
        
        self.get_tag_table().add(self.redTag)
        self.get_tag_table().add(self.greenTag)
        self.get_tag_table().add(self.blueTag)
        
        self.connect_after("insert-text",
self.__text_insert)
        self.connect_after("delete-range",
self.__text_delete)
        
    def __getWordStart(self, cursor):
        character = cursor.get_char()
        if character == " ":
            return True
            
        if character.isalnum() is False:
            if character in ("-", "_", "^"):
                pass
            else:
                return False
        if cursor.starts_line():
            return True
        cursor.backward_char()
        character = cursor.get_char()
        cursor.forward_char()
        if character.isalnum() or character in ("_", "-",
"^"):
            return False
        return True
        
    def __text_insert(self,buffer, cursor, text, length):
        start = cursor.copy()
        while self.__getWordStart(start) is False:
            start.backward_char()
        if start.get_text(cursor) in self.keywords:
           
self.apply_tag_by_name(self.keywords[start.get_text(cursor)]
, start, cursor)
        else:
            self.remove_all_tags(start, cursor)
            
    def __text_delete(self, buffer, start, end):
        cursor =
buffer.get_iter_at_mark(buffer.get_insert())
        start = cursor.copy()
        while self.__getWordStart(start) is False:
            start.backward_char()
        if start.get_text(cursor) in self.keywords:
           
self.apply_tag_by_name(self.keywords[start.get_text(cursor)]
, start, cursor)
        else:
            self.remove_all_tags(start, cursor)
    
buffer = TagBuffer({"Program": "Red", "Key": "Red",
"Load_AVI": "Green", "^header": "Blue"})
view = gtk.TextView(buffer)
window = gtk.Window()
window.set_default_size(500, 400)
window.connect("destroy", gtk.main_quit)
window.add(view)
window.show_all()

gtk.main()


Za vsechny napady budu rad.
BTW: standartni fce textiter.starts_word() se mi taky
nehodi, protoze nepocita se znakem "_","-","^" ktere
potrebuju.
-- 
MB <spooky.ln na tbs-software.com>



Další informace o konferenci Python