improve latex detection by looking only in latest message. Fixes #49

This commit is contained in:
Yann Leboulanger
2013-02-21 23:03:00 +01:00
parent a5731e7202
commit b2ae21be0c
2 changed files with 39 additions and 14 deletions

View File

@@ -293,6 +293,8 @@ class LatexPlugin(GajimPlugin):
self.disconnect_from_chat_control_base) self.disconnect_from_chat_control_base)
} }
self.test_activatable() self.test_activatable()
self.timeout_id = None
self.last_eol_offset = -1
def test_activatable(self): def test_activatable(self):
""" """
@@ -360,24 +362,47 @@ class LatexPlugin(GajimPlugin):
""" """
called when conversation text widget changes called when conversation text widget changes
""" """
def split_list(list): def split_list(list_):
newlist = [] newlist = []
for i in range(0, len(list)-1, 2): for i in range(0, len(list_)-1, 2):
newlist.append( [ list[i], list[i+1], ] ) newlist.append( [ list_[i], list_[i+1], ] )
return newlist return newlist
assert isinstance(tb, Gtk.TextBuffer) def detect_tags(tb, start_it=None, end_it=None):
start_iter = tb.get_start_iter() self.timeout_id = None
points = [] if not end_it:
tuple_found = start_iter.forward_search('$$', end_it = tb.get_end_iter()
Gtk.TextSearchFlags.TEXT_ONLY, None) if not start_it:
while tuple_found != None: eol_tag = tb.get_tag_table().lookup('eol')
points.append(tuple_found) start_it = end_it.copy()
tuple_found = tuple_found[1].forward_search('$$', start_it.backward_to_tag_toggle(eol_tag)
points = []
tuple_found = start_it.forward_search('$$',
Gtk.TextSearchFlags.TEXT_ONLY, None) Gtk.TextSearchFlags.TEXT_ONLY, None)
while tuple_found != None:
points.append(tuple_found)
tuple_found = tuple_found[1].forward_search('$$',
Gtk.TextSearchFlags.TEXT_ONLY, None)
for pair in split_list(points): for pair in split_list(points):
tb.apply_tag_by_name('latex', pair[0][1], pair[1][0]) tb.apply_tag_by_name('latex', pair[0][1], pair[1][0])
end_iter = tb.get_end_iter()
eol_tag = tb.get_tag_table().lookup('eol')
it = end_iter.copy()
it.backward_to_tag_toggle(eol_tag)
if it.get_offset() == self.last_eol_offset:
if self.timeout_id:
GObject.source_remove(self.timeout_id)
self.timeout_id = Geobject.timeout_add(100, detect_tags, traceback, it, end_iter)
else:
if self.timeout_id:
GObject.source_remove(self.timeout_id)
it1 = it.copy()
it1.backward_char()
it1.backward_to_tag_toggle(eol_tag)
detect_tags(traceback, it1, it)
self.last_eol_offset = it.get_offset()
def connect_with_chat_control_base(self, chat_control): def connect_with_chat_control_base(self, chat_control):
d = {} d = {}

View File

@@ -1,7 +1,7 @@
[info] [info]
name: Latex name: Latex
short_name: latex short_name: latex
version: 0.3 version: 0.3.1
description: render received latex code description: render received latex code
authors: Yves Fischer <yvesf@xapek.org> authors: Yves Fischer <yvesf@xapek.org>
Yann Leboulanger <asterix@lagaule.org> Yann Leboulanger <asterix@lagaule.org>