[meld] filediff: Add an encoding kwargs to set_files() that overrides detection



commit 9b92209a15e49b3d55393b0602ea3f3e2508d9d5
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Mon Nov 20 06:26:34 2017 +1000

    filediff: Add an encoding kwargs to set_files() that overrides detection
    
    This could be passed through from a file selector change, or some
    reload-with-new-encoding UI, or  conceivably from an error-case prompt
    (i.e., this failed decoding as UTF-16 do you want to try whatever).

 meld/filediff.py |   35 +++++++++++++++++++++++++++--------
 1 files changed, 27 insertions(+), 8 deletions(-)
---
diff --git a/meld/filediff.py b/meld/filediff.py
index f511c4f..d2e5ae7 100644
--- a/meld/filediff.py
+++ b/meld/filediff.py
@@ -1035,7 +1035,7 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
         self.tooltip_text = self.label_text
         self.label_changed()
 
-    def set_files(self, gfiles):
+    def set_files(self, gfiles, encodings=None):
         """Load the given files
 
         If an element is None, the text of a pane is left as is.
@@ -1047,16 +1047,33 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
         self.undosequence.clear()
         self.linediffer.clear()
 
-        gfiles_enum = [(pane, gfile)
-                       for pane, gfile in enumerate(gfiles) if gfile]
+        encodings = encodings or ((None,) * len(gfiles))
 
-        if not gfiles_enum:
-            self.scheduler.add_task(self._compare_files_internal())
+        files = [
+            (pane, gfile, encoding)
+            for pane, (gfile, encoding) in enumerate(zip(gfiles, encodings))
+            if gfile
+        ]
 
-        for pane, gfile in gfiles_enum:
-            self.load_file_in_pane(pane, gfile)
+        if not files:
+            self.scheduler.add_task(self._compare_files_internal())
 
-    def load_file_in_pane(self, pane: int, gfile: Gio.File):
+        for pane, gfile, encoding in files:
+            self.load_file_in_pane(pane, gfile, encoding)
+
+    def load_file_in_pane(
+            self,
+            pane: int,
+            gfile: Gio.File,
+            encoding: GtkSource.Encoding = None):
+        """Load a file into the given pane
+
+        Don't call this directly; use `set_file()` or `set_files()`,
+        which handle sensitivity and signal connection. Even if you
+        don't care about those things, you need it because they'll be
+        unconditionally added after file load, which will cause
+        duplicate handlers, etc. if you don't do this thing.
+        """
 
         self.fileentry[pane].set_file(gfile)
         # TODO: filentry handling of URIs
@@ -1074,6 +1091,8 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
             loader = GtkSource.FileLoader.new(buf, buf.data.sourcefile)
 
         custom_candidates = get_custom_encoding_candidates()
+        if encoding:
+            custom_candidates = [encoding]
         if custom_candidates:
             loader.set_candidate_encodings(custom_candidates)
 


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