Use fonts.monospace properly

This commit is contained in:
Florian Bruhin 2017-07-03 16:51:51 +02:00
parent 4562a3574b
commit 78d7ac311f
5 changed files with 23 additions and 6 deletions

View File

@ -611,6 +611,8 @@ def init(parent=None):
val = ConfigContainer(instance)
key_instance = KeyConfig(instance)
configtypes.Font.monospace_fonts = val.fonts.monospace
config_commands = ConfigCommands(instance, key_instance)
objreg.register('config-commands', config_commands)

View File

@ -39,11 +39,6 @@ DATA = None
# FIXME:conf what to do about this?
DEFAULT_FONT_SIZE = '10pt' if sys.platform == 'darwin' else '8pt'
MONOSPACE = (' xos4 Terminus, Terminus, Monospace, '
'"DejaVu Sans Mono", Monaco, '
'"Bitstream Vera Sans Mono", "Andale Mono", '
'"Courier New", Courier, "Liberation Mono", '
'monospace, Fixed, Consolas, Terminal')
Option = collections.namedtuple('Option', ['name', 'typ', 'default',

View File

@ -1685,7 +1685,11 @@ fonts.monospace:
Vera Sans Mono", "Andale Mono", "Courier New", Courier, "Liberation Mono", monospace,
Fixed, Consolas, Terminal
type: Font
desc: Default monospace fonts.
desc: >-
Default monospace fonts.
Whenever "monospace" is used in a font setting, it\'s replaced with the
fonts listed here.
fonts.completion.entry:
default: 8pt monospace

View File

@ -849,6 +849,8 @@ class Font(BaseType):
* Size: _number_ `px`/`pt`
"""
# Gets set when the config is initialized.
monospace_fonts = None
font_regex = re.compile(r"""
^(
(
@ -875,6 +877,8 @@ class Font(BaseType):
# as family.
raise configexc.ValidationError(value, "must be a valid font")
if value.endswith(' monospace') and self.monospace_fonts is not None:
return value.replace('monospace', self.monospace_fonts)
return value
@ -955,6 +959,8 @@ class QtFont(Font):
# hopefully nobody will ever have a font with quotes in the family (if
# that's even possible), we take a much more naive approach.
family = family.replace('"', '').replace("'", '')
if family == 'monospace':
family = self.monospace_fonts
font.setFamily(family)
return font

View File

@ -1260,6 +1260,16 @@ class TestFont:
with pytest.raises(configexc.ValidationError):
klass().to_py(val)
def test_monospace_replacement(self, klass, monkeypatch):
monkeypatch.setattr(configtypes.Font, 'monospace_fonts', 'Terminus')
if klass is configtypes.Font:
expected = '10pt Terminus'
elif klass is configtypes.QtFont:
desc = FontDesc(QFont.StyleNormal, QFont.Normal, 10, None,
'Terminus'),
expected = Font.fromdesc(*desc)
assert klass().to_py('10pt monospace') == expected
class TestFontFamily: