Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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).

share|improve this question
    
If it's possible to change fonts by modelines in gedit it'd be possible but I doubt that. –  N.N. Sep 29 '11 at 11:14
add comment

2 Answers 2

up vote 3 down vote accepted

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')
share|improve this answer
1  
There is no better answer! That was it, this is it. I am assuming your plugin works (haven't tested yet but looks great), it does the job and keeps a kiss approach! ;) Very, very good! –  Bruno Pereira Oct 1 '11 at 7:48
add comment

As far as I know the answer is "no"... But...

gconf-editor allows you to set a printing font, no mater what font is currently selected on the options from gedit at /apps/gedit-2/preferences/print/fonts, maybe there is an option to select display font also. If that is true a simple script can change that for you depending on the extension of the file.

--Edit--

The configuration part that you are looking for is at /apps/gedit-2/preferences/editor/font

Make a small script to change that according to the files extension and you are done ;)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.