[meld: 60/63] filediff, sourceview: Re-add support for old detect-encodings setting



commit dec395e70b5d44ab0e5542ca0f4f8243a1b5655e
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Thu Dec 10 06:54:27 2015 +1000

    filediff, sourceview: Re-add support for old detect-encodings setting
    
    This works similarly to the old setting, but requires GtkSourceView
    3.18 for the default-encodings API. On earlier versions, we'll fall
    back to only using the built-in encodings, since that's likely to be
    a better experience for most users.

 meld/filediff.py   |    7 +++++--
 meld/sourceview.py |   25 ++++++++++++++++++++++++-
 2 files changed, 29 insertions(+), 3 deletions(-)
---
diff --git a/meld/filediff.py b/meld/filediff.py
index 55a21ab..658e427 100644
--- a/meld/filediff.py
+++ b/meld/filediff.py
@@ -48,7 +48,7 @@ from .ui import gnomeglade
 from meld.const import MODE_REPLACE, MODE_DELETE, MODE_INSERT, NEWLINES
 from meld.settings import bind_settings, meldsettings
 from .util.compat import text_type
-from meld.sourceview import LanguageManager
+from meld.sourceview import LanguageManager, get_custom_encoding_candidates
 
 
 def with_focused_pane(function):
@@ -1049,6 +1049,8 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
         self.undosequence.clear()
         self.linediffer.clear()
 
+        custom_candidates = get_custom_encoding_candidates()
+
         files = [(pane, Gio.File.new_for_path(filename))
                  for pane, filename in enumerate(files) if filename]
 
@@ -1058,9 +1060,10 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
 
             self.textbuffer[pane].data.reset(gfile)
 
-            # TODO: Maybe re-add support for the 'detect-encodings' gsetting
             loader = GtkSource.FileLoader.new(
                 self.textbuffer[pane], self.textbuffer[pane].data.sourcefile)
+            if custom_candidates:
+                loader.set_candidate_encodings(custom_candidates)
             loader.load_async(
                 GLib.PRIORITY_HIGH,
                 callback=self.file_loaded,
diff --git a/meld/sourceview.py b/meld/sourceview.py
index c26f82f..2be6ad2 100644
--- a/meld/sourceview.py
+++ b/meld/sourceview.py
@@ -14,6 +14,8 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+import logging
+
 from gi.repository import Gdk
 from gi.repository import Gio
 from gi.repository import GLib
@@ -24,7 +26,28 @@ from gi.repository import GtkSource
 import meldbuffer
 
 from meld.misc import colour_lookup_with_fallback, get_common_theme
-from meld.settings import bind_settings, meldsettings
+from meld.settings import bind_settings, meldsettings, settings
+
+
+log = logging.getLogger(__name__)
+
+
+def get_custom_encoding_candidates():
+    custom_candidates = []
+    try:
+        for charset in settings.get_value('detect-encodings'):
+            encoding = GtkSource.Encoding.get_from_charset(charset)
+            if not encoding:
+                log.warning('Invalid charset "%s" skipped', charset)
+                continue
+            custom_candidates.append(encoding)
+        if custom_candidates:
+            custom_candidates.extend(
+                GtkSource.Encoding.get_default_candidates())
+    except AttributeError:
+        # get_default_candidates() is only available in GtkSourceView 3.18
+        # and we'd rather use their defaults than our old detect list.
+        pass
 
 
 class LanguageManager(object):


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