Commit dec395e7 authored by Kai Willadsen's avatar Kai Willadsen

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.
parent a1cadb7b
......@@ -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,
......
......@@ -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):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment