Source

codeaide / codeaide / modes / language / __init__.py

Full commit
from codeaide.base import *


class LanguageModeBase(ModeBase):

    mimetype = None
    pattern = None


    @classmethod
    def find_by_pattern(cls, filename):
        for impl in cls.implementations():
            if re.match(impl.pattern, filename):
                return impl


    @classmethod
    def find_by_mimetype(cls, mimetype):
        for impl in cls.implementations():
            if impl.mimetype == mimetype:
                return impl


    def init(self):
        self.init_language()


    def init_language(self):
        pass



    

    
class AutoloadLanguage(ModeBase):


    def init(self):
        lang = self.current_language()
        if lang:
            lang.uninstall()
        self.textedit.connect(
            self.textedit, SIGNAL("documentChanged()"), self.on_document_changed)


    def on_document_changed(self):
        try:
            filename = self.textedit.document().filename
        except AttributeError, _err:
            filename = None
        if filename:
            mode = LanguageModeBase.find_by_pattern(filename)
            if mode:
                self.textedit.install(mode)

    
    def current_language(self):
        for obj in self.textedit.installed_modes:
            if isinstance(obj, LanguageModeBase):
                return obj