Commit c21a78b7 authored by Florian Münchbach's avatar Florian Münchbach

[syntax_highlight] By default, do not highlight but only set monospaced font

parent 7e068624
......@@ -62,7 +62,7 @@ class ChatSyntaxHighlighter:
if lexer is None:
iterator = buf.get_iter_at_mark(start_mark)
buf.insert(iterator, '\n')
else:
elif not self.config.is_internal_none_lexer(lexer):
tokens = pygments.lex(code, lexer)
formatter = GTKFormatter(style=style, start_mark=start_mark)
......
......@@ -3,10 +3,15 @@ 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
from .types import MatchType, LineBreakOptions, CodeMarkerOptions, \
PLUGIN_INTERNAL_NONE_LEXER_ID
class SyntaxHighlighterConfig:
PLUGIN_INTERNAL_NONE_LEXER=('None (monospace only)', PLUGIN_INTERNAL_NONE_LEXER_ID)
def _create_lexer_list(self):
# The list we create here contains the plain text name and the lexer's
# id string
lexers = []
# Iteration over get_all_lexers() seems to be broken somehow. Workarround
......@@ -16,8 +21,17 @@ class SyntaxHighlighterConfig:
if lexer[1] is not None and lexer[1]:
lexers.append((lexer[0], lexer[1][0]))
lexers.sort()
# Insert our internal "none" type at top of the list.
lexers.insert(0, self.PLUGIN_INTERNAL_NONE_LEXER)
return lexers
def is_internal_none_lexer(self, lexer):
return (lexer == PLUGIN_INTERNAL_NONE_LEXER_ID)
def get_internal_none_lexer(self, lexer):
return self.PLUGIN_INTERNAL_NONE_LEXER
def get_lexer_by_name(self, name):
lexer = None
try:
......@@ -48,20 +62,23 @@ class SyntaxHighlighterConfig:
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]
if not self.is_internal_none_lexer(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)
else:
self.default_lexer = (name, lexer)
self.default_lexer = self.PLUGIN_INTERNAL_NONE_LEXER
self.config['default_lexer'] = name
......
......@@ -136,9 +136,10 @@ class SyntaxHighlighterPluginConfiguration(GajimPluginConfigDialog):
code = start_iter.get_text(buf.get_end_iter())
lexer = self.config.get_default_lexer()
tokens = pygments.lex(code, lexer)
if not self.config.is_internal_none_lexer(lexer):
tokens = pygments.lex(code, lexer)
pygments.format(tokens, formatter, buf)
pygments.format(tokens, formatter, buf)
buf.delete_mark(start_mark)
......
......@@ -6,7 +6,8 @@ import importlib
from gajim.plugins.helpers import log_calls, log
from gajim.plugins import GajimPlugin
from .types import MatchType, LineBreakOptions, CodeMarkerOptions
from .types import MatchType, LineBreakOptions, CodeMarkerOptions, \
PLUGIN_INTERNAL_NONE_LEXER_ID
log = logging.getLogger('gajim.plugin_system.syntax_highlight')
......@@ -93,7 +94,7 @@ class SyntaxHighlighterPlugin(GajimPlugin):
self.ccontrol = {}
self.config_default_values = {
'default_lexer' : ('python', ''),
'default_lexer' : (PLUGIN_INTERNAL_NONE_LEXER_ID, ''),
'line_break' : (LineBreakOptions.MULTILINE, ''),
'style' : ('default', ''),
'font' : ('Monospace 10', ''),
......
from enum import Enum, IntEnum, unique
PLUGIN_INTERNAL_NONE_LEXER_ID='_syntax_highlight_internal_none_type'
class MatchType(Enum):
INLINE = 0
MULTILINE = 1
......
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