Welcome to mirror list, hosted at ThFree Co, Russian Federation.

plugin_config.py « syntax_highlight - dev.gajim.org/gajim/gajim-plugins.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 97a36a0f1d981ab94d7aa05c0953d8101981b90a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from gajim.plugins.helpers import log_calls, log

from pygments.lexers import get_lexer_by_name, get_all_lexers
from pygments.styles import get_all_styles

from .types import MatchType, LineBreakOptions, CodeMarkerOptions

class SyntaxHighlighterConfig:
    def _create_lexer_list(self):
        lexers = []

        # Iteration over get_all_lexers() seems to be broken somehow. Workarround
        all_lexers = get_all_lexers()
        for lexer in all_lexers:
            # We don't want to add lexers that we cant identify by name later
            if lexer[1] is not None and lexer[1]:
                lexers.append((lexer[0], lexer[1][0]))
        lexers.sort()
        return lexers

    def get_lexer_by_name(self, name):
        lexer = None
        try:
            lexer = get_lexer_by_name(name)
        except:
            pass
        return lexer

    def get_lexer_with_fallback(self, language):
        lexer = self.get_lexer_by_name(language)
        if lexer is None:
            log.info("Falling back to default lexer for %s.",
                    self.get_default_lexer_name())
            lexer = self.default_lexer[1]
        return lexer

    def set_font(self, font):
        if font is not None and font != "":
            self.config['font'] = font

    def set_style(self, style):
        if style is not None and style != "":
            self.config['style'] = style

    def set_line_break_action(self, option):
        if isinstance(option, int):
            option = LineBreakOptions(option)
        self.config['line_break'] = option

    def set_default_lexer(self, name):
        lexer = get_lexer_by_name(name)

        if lexer is None and self.default_lexer is None:
            log.error("Failed to get default lexer by name."\
                    "Falling back to simply using the first in the list.")
            lexer = self.lexer_list[0]
            name  = lexer[0]
            self.default_lexer = (name, lexer)
        if lexer is None and self.default_lexer is not None:
            log.info("Failed to get default lexer by name, keeping previous"\
                    "setting (lexer = %s).", self.default_lexer[0])
            name = self.default_lexer[0]
        else:
            self.default_lexer = (name, lexer)

        self.config['default_lexer'] = name

    def set_bgcolor_override_enabled(self, state):
        self.config['bgcolor_override'] = state

    def set_bgcolor(self, color):
        if isinstance(color, Gdk.Color):
            color = color.to_string()
        self.config['bgcolor'] = color

    def set_code_marker_setting(self, option):
        if isinstance(option, int):
            option = CodeMarkerOptions(option)
        self.config['code_marker'] = option

    def set_pygments_path(self, path):
        self.config['pygments_path'] = path

    def get_default_lexer(self):
        return self.default_lexer[1]

    def get_default_lexer_name(self):
        return self.default_lexer[0]

    def get_lexer_list(self):
        return self.lexer_list

    def get_line_break_action(self):
        # return int only
        if isinstance(self.config['line_break'], int):
            # in case of legacy settings, convert.
            action = self.config['line_break']
            self.set_line_break_action(action)
        else:
            action = self.config['line_break'].value

        return action

    def get_pygments_path(self):
        return self.config['pygments_path']

    def get_font(self):
        return self.config['font']

    def get_style_name(self):
        return self.config['style']

    def is_bgcolor_override_enabled(self):
        return self.config['bgcolor_override']

    def get_bgcolor(self):
        return self.config['bgcolor']

    def get_code_marker_setting(self):
        return self.config['code_marker']

    def get_styles_list(self):
        return self.style_list

    def init_pygments(self):
        """
        Initialize all config variables that depend directly on pygments being
        available.
        """
        self.lexer_list     = self._create_lexer_list()
        self.style_list     = [s for s in get_all_styles()]
        self.style_list.sort()
        self.set_default_lexer(self.config['default_lexer'])

    def __init__(self, config):
        self.lexer_list     = []
        self.style_list     = []
        self.config         = config
        self.default_lexer  = None