Re: meld doesn't support gtksourceview2 ?
- From: Vincent Legoll <vincent legoll gmail com>
- To: "samuel.yin" <samuel yin 163 com>
- Cc: meld-list gnome org
- Subject: Re: meld doesn't support gtksourceview2 ?
- Date: Thu, 5 Mar 2009 01:28:50 +0100
Here is a patch that works for me, please try it and report.
--
Vincent Legoll
Index: filediff.py
===================================================================
--- filediff.py (revision 1216)
+++ filediff.py (working copy)
@@ -33,30 +33,9 @@
import paths
import cairo
-sourceview_available = 0
+from sourceviewer import srcviewer
+sourceview = srcviewer()
-for sourceview in "gtksourceview sourceview".split():
- try:
- gsv = __import__(sourceview)
- sourceview_available = 1
- break
- except ImportError:
- pass
-
-if sourceview_available:
- def set_highlighting_enabled(buf, fname, enabled):
- if enabled:
- import gnomevfs
- mime_type = gnomevfs.get_mime_type(
- gnomevfs.make_uri_from_input(os.path.abspath(fname)) )
- man = gsv.SourceLanguagesManager()
- gsl = man.get_language_from_mime_type( mime_type )
- if gsl:
- buf.set_language(gsl)
- else:
- enabled = False
- buf.set_highlight(enabled)
-
gdk = gtk.gdk
################################################################################
@@ -80,21 +59,17 @@
"""Start up an filediff with num_panes empty contents.
"""
melddoc.MeldDoc.__init__(self, prefs)
- override = {}
- if sourceview_available:
- override["GtkTextView"] = gsv.SourceView
- override["GtkTextBuffer"] = gsv.SourceBuffer
- gnomeglade.Component.__init__(self, paths.share_dir("glade2/filediff.glade"), "filediff", override)
+ gnomeglade.Component.__init__(self, paths.share_dir("glade2/filediff.glade"), "filediff", sourceview.override)
self.map_widgets_into_lists( ["textview", "fileentry", "diffmap", "scrolledwindow", "linkmap", "statusimage"] )
for d in self.diffmap: d.hide()
self._update_regexes()
self.warned_bad_comparison = False
- if sourceview_available:
+ if sourceview:
for v in self.textview:
- v.set_buffer( gsv.SourceBuffer() )
+ v.set_buffer(sourceview.GtkTextBuffer())
v.set_show_line_numbers(self.prefs.show_line_numbers)
v.set_insert_spaces_instead_of_tabs(self.prefs.spaces_instead_of_tabs)
- v.set_tabs_width(self.prefs.tab_size)
+ sourceview.set_tab_width(v, self.prefs.tab_size)
self.keymask = 0
self.load_font()
self.deleted_lines_pending = -1
@@ -295,19 +270,19 @@
tabs.set_tab(i, pango.TAB_LEFT, i*value*self.pango_char_width)
for i in range(3):
self.textview[i].set_tabs(tabs)
- if sourceview_available:
+ if sourceview:
for t in self.textview:
- t.set_tabs_width(value)
+ sourceview.set_tabs_width(t, value)
elif key == "use_custom_font" or key == "custom_font":
self.load_font()
elif key == "show_line_numbers":
- if sourceview_available:
+ if sourceview:
for t in self.textview:
- t.set_show_line_numbers( value )
+ sourceview.set_show_line_numbers(t, value)
elif key == "use_syntax_highlighting":
- if sourceview_available:
+ if sourceview:
for i in range(self.num_panes):
- set_highlighting_enabled(
+ sourceview.set_highlighting_enabled(
self.textbuffer[i],
self.bufferdata[i].filename,
self.prefs.use_syntax_highlighting )
@@ -317,7 +292,7 @@
for t in self.textview:
t.set_wrap_mode(self.prefs.edit_wrap_lines)
elif key == "spaces_instead_of_tabs":
- if sourceview_available:
+ if sourceview:
for t in self.textview:
t.set_insert_spaces_instead_of_tabs(value)
@@ -653,10 +628,10 @@
lenseq = [len(d) for d in self.linediffer.diffs]
self.scheduler.add_task( self._update_highlighting( (0,lenseq[0]), (0,lenseq[1]) ).next )
self._connect_buffer_handlers()
- if sourceview_available:
+ if sourceview:
for i in range(len(files)):
if files[i]:
- set_highlighting_enabled(self.textbuffer[i], files[i], self.prefs.use_syntax_highlighting)
+ sourceview.set_highlighting_enabled(self.textbuffer[i], files[i], self.prefs.use_syntax_highlighting)
yield 0
def _update_highlighting(self, range0, range1):
@@ -833,11 +808,7 @@
def make_patch(self, *extra):
fontdesc = pango.FontDescription(self.prefs.get_current_font())
- override = {}
- if sourceview_available:
- override["GtkTextView"] = gsv.SourceView
- override["GtkTextBuffer"] = gsv.SourceBuffer
- dialog = gnomeglade.Component( paths.share_dir("glade2/filediff.glade"), "patchdialog", override)
+ dialog = gnomeglade.Component(paths.share_dir("glade2/filediff.glade"), "patchdialog", sourceview.override)
dialog.widget.set_transient_for( self.widget.get_toplevel() )
texts = [b.get_text(*b.get_bounds()).split("\n") for b in self.textbuffer]
texts[0] = [l+"\n" for l in texts[0]]
@@ -845,20 +816,19 @@
names = [self._get_pane_label(i) for i in range(2)]
prefix = os.path.commonprefix( names )
names = [n[prefix.rfind("/") + 1:] for n in names]
- if sourceview_available:
- dialog.textview.set_buffer( gsv.SourceBuffer() )
+ if sourceview:
+ dialog.textview.set_buffer(sourceview.GtkTextBuffer())
dialog.textview.modify_font(fontdesc)
buf = dialog.textview.get_buffer()
lines = []
for line in difflib.unified_diff(texts[0], texts[1], names[0], names[1]):
buf.insert( buf.get_end_iter(), line )
lines.append(line)
- if sourceview_available:
- man = gsv.SourceLanguagesManager()
- gsl = man.get_language_from_mime_type("text/x-diff")
+ if sourceview:
+ gsl = sourceview.get_language_from_mime_type("text/x-diff")
if gsl:
buf.set_language(gsl)
- buf.set_highlight(True)
+ sourceview.set_highlight(buf, True)
result = dialog.widget.run()
dialog.widget.destroy()
if result >= 0:
Index: meldapp.py
===================================================================
--- meldapp.py (revision 1216)
+++ meldapp.py (working copy)
@@ -37,17 +37,9 @@
import dirdiff
import task
-# optional
-sourceview_available = 0
+from sourceviewer import srcviewer
+sourceview_available = srcviewer()
-for sourceview in "gtksourceview sourceview".split():
- try:
- __import__(sourceview)
- sourceview_available = 1
- break
- except ImportError:
- pass
-
version = "1.2.1-svn"
# magic developer switch, changes some behaviour
Index: sourceviewer.py
===================================================================
--- sourceviewer.py (revision 0)
+++ sourceviewer.py (revision 0)
@@ -0,0 +1,89 @@
+'''
+Created on Mar 4, 2009
+
+ author: vince
+
+Abstraction from sourceview version incompatibilities
+'''
+
+import os
+
+class _srcviewer():
+ srcviewer_class = None
+
+ def __init__(self):
+ self.gsv = __import__(self.srcviewer_class)
+ self.glm = None
+ self.overrides()
+ self.override = {}
+ self.override["GtkTextView"] = self.GtkTextView
+ self.override["GtkTextBuffer"] = self.GtkTextBuffer
+
+ def get_language_manager(self):
+ if self.glm is None:
+ self.glm = self.GtkLanguageManager()
+ return self.glm
+
+ def set_highlighting_enabled(self, buf, fname, enabled):
+ if enabled:
+ gsl = self.get_language_from_file(os.path.abspath(fname))
+ if gsl:
+ buf.set_language(gsl)
+ else:
+ enabled = False
+ self.set_highlight(buf, enabled)
+
+class gtksourceviewer(_srcviewer):
+ srcviewer_class = "gtksourceview"
+
+ def overrides(self):
+ self.GtkTextView = self.gsv.SourceView
+ self.GtkTextBuffer = self.gsv.SourceBuffer
+ self.GtkLanguageManager = self.gsv.SourceLanguagesManager
+ self.set_tab_width = self.gsv.SourceView.set_tabs_width
+ self.set_highlight = self.gsv.SourceBuffer.set_highlight
+
+ def get_language_from_mime_type(self, mime_type):
+ return self.get_language_manager().get_language_from_mime_type(mime_type)
+
+ def get_language_from_file(self, filename):
+ import gnomevfs
+ mime_type = gnomevfs.get_mime_type(
+ gnomevfs.make_uri_from_input(os.path.abspath(filename)))
+ return self.get_language_from_mime_type(mime_type)
+
+class gtksourceviewer2(_srcviewer):
+ srcviewer_class = "gtksourceview2"
+
+ def overrides(self):
+ self.GtkTextView = self.gsv.View
+ self.GtkTextBuffer = self.gsv.Buffer
+ self.GtkLanguageManager = self.gsv.LanguageManager
+ self.set_tab_width = self.gsv.View.set_tab_width
+ self.set_highlight = self.gsv.Buffer.set_highlight_syntax
+
+ def get_language_from_mime_type(self, mime_type):
+ raise NotImplemented
+
+ def get_language_from_file(self, filename):
+ return self.get_language_manager().guess_language(filename)
+
+class sourceviewer(gtksourceviewer):
+ srcviewer_class = "sourceview"
+
+class nullsourceviewer(_srcviewer):
+
+ def __init__(self):
+ # No super() call, and nothing else but that
+ self.override = {}
+
+ def __nonzero__(self):
+ return False
+
+def srcviewer():
+ for srcv in (gtksourceviewer2, gtksourceviewer, sourceviewer):
+ try:
+ return srcv()
+ except ImportError:
+ pass
+ return nullsourceviewer()
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]