[meld] preferences: Move external editor handling into caller and update



commit e9268b8d9ff4c98a6c2b6bdec6a85963f3f46855
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Sat Nov 23 11:27:50 2013 +1000

    preferences: Move external editor handling into caller and update

 meld/melddoc.py     |   38 ++++++++++++++++++++++++++++++++------
 meld/preferences.py |   42 ------------------------------------------
 2 files changed, 32 insertions(+), 48 deletions(-)
---
diff --git a/meld/melddoc.py b/meld/melddoc.py
index 2d1e581..749e93c 100644
--- a/meld/melddoc.py
+++ b/meld/melddoc.py
@@ -16,7 +16,9 @@
 ### Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 ### USA.
 
-
+import logging
+import shlex
+import string
 import subprocess
 import sys
 
@@ -28,6 +30,24 @@ from gi.repository import Gtk
 from . import task
 
 from gettext import gettext as _
+from meld.settings import settings
+
+log = logging.getLogger(__name__)
+
+
+def make_custom_editor_command(path, line=0):
+    custom_command = settings.get_string('custom-editor-command')
+    fmt = string.Formatter()
+    replacements = [tok[1] for tok in fmt.parse(custom_command)]
+
+    if not any(replacements):
+        cmd = " ".join([custom_command, path])
+    elif not all(r in (None, 'file', 'line') for r in replacements):
+        cmd = " ".join([custom_command, path])
+        log.error("Unsupported fields found", )
+    else:
+        cmd = custom_command.format(file=path, line=line)
+    return shlex.split(cmd)
 
 
 class MeldDoc(GObject.GObject):
@@ -102,12 +122,18 @@ class MeldDoc(GObject.GObject):
                 # FIXME: Content types are broken on Windows with current gio
                 if Gio.content_type_is_a(content_type, "text/plain") or \
                         sys.platform == "win32":
-                    editor = self.prefs.get_editor_command(path, line)
-                    # TODO: If the editor is badly set up, this fails silently
-                    if editor:
-                        subprocess.Popen(editor)
+                    if settings.get_boolean('use-system-editor'):
+                        gfile = Gio.File.new_for_path(path)
+                        Gio.AppInfo.launch_default_for_uri(
+                            gfile.get_uri(), None)
                     else:
-                        os_open(path)
+                        editor = make_custom_editor_command(path, line)
+                        if editor:
+                            # TODO: If the editor is badly set up, this fails
+                            # silently
+                            subprocess.Popen(editor)
+                        else:
+                            os_open(path)
                 else:
                     os_open(path)
             else:
diff --git a/meld/preferences.py b/meld/preferences.py
index bde4412..76b4c3c 100644
--- a/meld/preferences.py
+++ b/meld/preferences.py
@@ -16,10 +16,6 @@
 ### Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 ### USA.
 
-import logging
-import shlex
-import string
-
 from gettext import gettext as _
 
 from gi.repository import Gio
@@ -41,8 +37,6 @@ TIMESTAMP_RESOLUTION_PRESETS = [('1ns (ext4)', 1),
                                 ('1s (ext2/ext3)', 1000000000),
                                 ('2s (VFAT)', 2000000000)]
 
-log = logging.getLogger(__name__)
-
 
 class FilterList(listwidget.ListWidget):
 
@@ -363,39 +357,3 @@ class MeldPreferences(prefs.Preferences):
         if self.use_custom_font:
             return self.custom_font
         return interface_settings.get_string('monospace-font-name')
-
-    def get_editor_command(self, path, line=0):
-        system_editor = settings.get_boolean('use-system-editor')
-        if not system_editor:
-            custom_command = settings.get_string('custom-editor-command')
-            fmt = string.Formatter()
-            replacements = [tok[1] for tok in fmt.parse(custom_command)]
-
-            if not any(replacements):
-                cmd = " ".join([custom_command, path])
-            elif not all(r in (None, 'file', 'line') for r in replacements):
-                cmd = " ".join([custom_command, path])
-                log.error("Unsupported fields found", )
-            else:
-                cmd = custom_command.format(file=path, line=line)
-            return shlex.split(cmd)
-        else:
-            if not hasattr(self, "_gconf"):
-                return []
-
-            editor_path = "/desktop/gnome/applications/editor/"
-            terminal_path = "/desktop/gnome/applications/terminal/"
-            editor = self._gconf.get_string(editor_path + "exec") or "gedit"
-            if self._gconf.get_bool(editor_path + "needs_term"):
-                argv = []
-                texec = self._gconf.get_string(terminal_path + "exec")
-                if texec:
-                    argv.append(texec)
-                    targ = self._gconf.get_string(terminal_path + "exec_arg")
-                    if targ:
-                        argv.append(targ)
-                escaped_path = path.replace(" ", "\\ ")
-                argv.append("%s %s" % (editor, escaped_path))
-                return argv
-            else:
-                return [editor, path]


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