Ubuntu – Per file-type preferences in gedit

geditpreferences

I'm trying to use gedit for two different tasks: developing Python software and writing Latex documents. Both are based on pure text files, but they are very different: e.g. Python code needs to be aligned to be able to see the structure of the code (loops, functions, …). Therefore a Monospace font is a good thing. For writing a Latex document, I mainly need readability. This is best achieved with a font that is used in printing and definitly NOT a monospace font.

Is there a way to tell gedit to use per-file-type preferences? E.g. Garamond for *.tex and Monospace for *.py? (But this problem is not limited to the fonts, and also not to Latex and Python).

Best Answer

ok, since it seems not possible, I put together a proto-plugin for gedit2 that works for me at the moment. I still hope that someone has a better answer...

~/.gnome2/gedit/plugins/mimeprefs.py

import gedit

class MimePrefsPlugin(gedit.Plugin):
    def __init__(self):
        gedit.Plugin.__init__(self)

    def activate(self, window):
        pass

    def deactivate(self, window):
        pass

    def update_ui(self, window):
        doc = window.get_active_document()
        try:
            mt = doc.get_mime_type()
        except AttributeError:
            return
        view = window.get_active_view()
        if 'x-tex' in mt:
            view.set_font(False, 'Garamond 14')
        elif 'x-python' in mt:
            view.set_font(False, 'Monospace 12')
        else:
            view.set_font(True, 'Monospace 10')

~/.gnome2/gedit/plugins/mimeprefs.gedit-plugin

[Gedit Plugin]
Loader=python
Module=mimeprefs
IAge=2
Name=Mime-Prefs v1
Description=A plugin to set font preferences based on the mimetype of the document
Authors=-
Copyright=Public Domain
Website=None

EDIT: update for gedit3: plugin files go in ~/.local/share/gedit/plugins/and look like this:

mimeprefs.plugin:

[Plugin]
Loader=python
Module=mimeprefs
IAge=3
Name=Mime-Prefs
Description=A plugin to set font preferences based on the mimetype of the document
Authors=Stefan Schwarzburg
Copyright=Public Domain
Website=None
Version=1.0.0

mimeprefs.py:

from gi.repository import GObject, Gedit


class MimePrefsPlugin(GObject.Object, Gedit.WindowActivatable):
    __gtype_name__ = "MimePrefsPlugin"
    window = GObject.property(type=Gedit.Window)

    def __init__(self):
        GObject.Object.__init__(self)

    def do_activate(self):
        pass

    def do_deactivate(self):
        pass

    def do_update_state(self):
        doc = self.window.get_active_document()
        try:
            mt = doc.get_mime_type()
        except AttributeError:
            return
        view = self.window.get_active_view()
        if 'x-tex' in mt:
            view.set_font(False, 'Garamond 14')
        elif 'x-python' in mt:
            view.set_font(False, 'Monospace 12')
        else:
            view.set_font(True, 'Monospace 10')