Skip to content

Commit

Permalink
formatting: add user friendly names for built-in metas
Browse files Browse the repository at this point in the history
Using the `{:meta:args}` syntax.
  • Loading branch information
benoit-pierre committed Apr 4, 2020
1 parent 433799c commit 23db9f3
Show file tree
Hide file tree
Showing 13 changed files with 398 additions and 270 deletions.
376 changes: 115 additions & 261 deletions plover/formatting.py

Large diffs are not rendered by default.

Empty file added plover/meta/__init__.py
Empty file.
63 changes: 63 additions & 0 deletions plover/meta/attach.py
@@ -0,0 +1,63 @@
from os.path import commonprefix

from plover.formatting import (
Case,
META_ATTACH_FLAG,
META_CARRY_CAPITALIZATION,
has_word_boundary,
rightmost_word,
)
from plover.orthography import add_suffix


def meta_attach(ctx, meta):
action = ctx.new_action()
begin = meta.startswith(META_ATTACH_FLAG)
end = meta.endswith(META_ATTACH_FLAG)
if begin:
meta = meta[len(META_ATTACH_FLAG):]
action.prev_attach = True
if end:
meta = meta[:-len(META_ATTACH_FLAG)]
action.next_attach = True
last_word = ctx.last_action.word or ''
if not meta:
# We use an empty connection to indicate a "break" in the
# application of orthography rules. This allows the
# stenographer to tell Plover not to auto-correct a word.
action.orthography = False
elif (
last_word and
not meta.isspace() and
ctx.last_action.orthography and
begin and (not end or has_word_boundary(meta))
):
new_word = add_suffix(last_word, meta)
common_len = len(commonprefix([last_word, new_word]))
replaced = last_word[common_len:]
action.prev_replace = ctx.last_text(len(replaced))
assert replaced.lower() == action.prev_replace.lower()
last_word = last_word[:common_len]
meta = new_word[common_len:]
action.text = meta
if action.prev_attach:
action.word = rightmost_word(last_word + meta)
return action

def meta_carry_capitalize(ctx, meta):
# Meta format: ^~|content^ (attach flags are optional)
action = ctx.new_action()
if ctx.last_action.next_case == Case.CAP_FIRST_WORD:
action.next_case = Case.CAP_FIRST_WORD
begin = meta.startswith(META_ATTACH_FLAG)
if begin:
meta = meta[len(META_ATTACH_FLAG):]
action.prev_attach = True
meta = meta[len(META_CARRY_CAPITALIZATION):]
end = meta.endswith(META_ATTACH_FLAG)
if end:
meta = meta[:-len(META_ATTACH_FLAG)]
action.next_attach = True
if meta or begin or end:
action.text = meta
return action
20 changes: 20 additions & 0 deletions plover/meta/case.py
@@ -0,0 +1,20 @@
from plover.formatting import Case, apply_case


def meta_case(ctx, case):
case = Case(case)
action = ctx.copy_last_action()
action.next_case = case
return action

def meta_retro_case(ctx, case):
case = Case(case)
action = ctx.copy_last_action()
action.prev_attach = True
last_words = ctx.last_words(count=1)
if last_words:
action.prev_replace = last_words[0]
action.text = apply_case(last_words[0], case)
else:
action.text = ''
return action
4 changes: 4 additions & 0 deletions plover/meta/command.py
@@ -0,0 +1,4 @@
def meta_command(ctx, command):
action = ctx.copy_last_action()
action.command = command
return action
20 changes: 20 additions & 0 deletions plover/meta/currency.py
@@ -0,0 +1,20 @@
def meta_retro_currency(ctx, dict_format):
action = ctx.copy_last_action()
last_words = ctx.last_words(count=1)
if not last_words:
return action
for cast, fmt in (
(float, '{:,.2f}'),
(int, '{:,}' ),
):
try:
cast_input = cast(last_words[0])
except ValueError:
pass
else:
currency_format = dict_format.replace('c', fmt)
action.prev_attach = True
action.prev_replace = last_words[0]
action.text = currency_format.format(cast_input)
action.word = None
return action
7 changes: 7 additions & 0 deletions plover/meta/glue.py
@@ -0,0 +1,7 @@
def meta_glue(ctx, text):
action = ctx.new_action()
action.glue = True
action.text = text
if ctx.last_action.glue:
action.prev_attach = True
return action
4 changes: 4 additions & 0 deletions plover/meta/key_combo.py
@@ -0,0 +1,4 @@
def meta_key_combo(ctx, combo):
action = ctx.copy_last_action()
action.combo = combo
return action
55 changes: 55 additions & 0 deletions plover/meta/mode.py
@@ -0,0 +1,55 @@
from plover.formatting import Case, SPACE


MODE_CAMEL = 'CAMEL'
MODE_CAPS = 'CAPS'
MODE_LOWER = 'LOWER'
MODE_RESET = 'RESET'
MODE_RESET_CASE = 'RESET_CASE'
MODE_RESET_SPACE = 'RESET_SPACE'
MODE_SET_SPACE = 'SET_SPACE:'
MODE_SNAKE = 'SNAKE'
MODE_TITLE = 'TITLE'


def meta_mode(ctx, mode):
"""
mode should be:
CAPS, LOWER, TITLE, CAMEL, SNAKE, RESET_SPACE,
RESET_CASE, SET_SPACE or RESET
CAPS: UPPERCASE
LOWER: lowercase
TITLE: Title Case
CAMEL: titleCase, no space, initial lowercase
SNAKE: Underscore_space
RESET_SPACE: Space resets to ' '
RESET_CASE: Reset to normal case
SET_SPACE:xy: Set space to xy
RESET: Reset to normal case, space resets to ' '
"""
action = ctx.copy_last_action()
if mode == MODE_CAPS:
action.case = Case.UPPER
elif mode == MODE_TITLE:
action.case = Case.TITLE
elif mode == MODE_LOWER:
action.case = Case.LOWER
elif mode == MODE_SNAKE:
action.space_char = '_'
elif mode == MODE_CAMEL:
action.case = Case.TITLE
action.space_char = ''
action.next_case = Case.LOWER_FIRST_CHAR
elif mode == MODE_RESET:
action.space_char = SPACE
action.case = None
elif mode == MODE_RESET_SPACE:
action.space_char = SPACE
elif mode == MODE_RESET_CASE:
action.case = None
elif mode.startswith(MODE_SET_SPACE):
action.space_char = mode[len(MODE_SET_SPACE):]
else:
raise ValueError('%r is not a valid mode' % mode)
return action
15 changes: 15 additions & 0 deletions plover/meta/punctuation.py
@@ -0,0 +1,15 @@
from plover.formatting import Case


def meta_comma(ctx, text):
action = ctx.new_action()
action.text = text
action.prev_attach = True
return action

def meta_stop(ctx, text):
action = ctx.new_action()
action.prev_attach = True
action.text = text
action.next_case = Case.CAP_FIRST_WORD
return action
13 changes: 13 additions & 0 deletions setup.cfg
Expand Up @@ -46,6 +46,7 @@ packages =
plover.gui_qt
plover.machine
plover.macro
plover.meta
plover.oslayer
plover.system
plover_build_utils
Expand Down Expand Up @@ -84,6 +85,18 @@ plover.macro =
retrospective_insert_space = plover.macro.retrospective:insert_space
retrospective_toggle_asterisk = plover.macro.retrospective:toggle_asterisk
undo = plover.macro.undo:undo
plover.meta =
attach = plover.meta.attach:meta_attach
case = plover.meta.case:meta_case
carry_capitalize = plover.meta.attach:meta_carry_capitalize
comma = plover.meta.punctuation:meta_comma
command = plover.meta.command:meta_command
glue = plover.meta.glue:meta_glue
key_combo = plover.meta.key_combo:meta_key_combo
mode = plover.meta.mode:meta_mode
retro_case = plover.meta.case:meta_retro_case
retro_currency = plover.meta.currency:meta_retro_currency
stop = plover.meta.punctuation:meta_stop
plover.system =
English Stenotype = plover.system.english_stenotype
setuptools.installation =
Expand Down
61 changes: 61 additions & 0 deletions test/test_blackbox.py
Expand Up @@ -707,6 +707,14 @@ def test_retro_capitalize4(self):
PRE/TPEUBG/RUP ' Prefix'
'''

def test_retro_capitalize5(self):
r'''
'KWEUP': 'equip',
'RUP': '{:retro_case:cap_first_word}',
KWEUP/RUP ' Equip'
'''

def test_retro_currency1(self):
r'''
'TPHAPB': 'notanumber',
Expand Down Expand Up @@ -739,6 +747,13 @@ def test_retro_currency4(self):
0/R-BG '$0 '
'''

def test_retro_currency5(self):
r'''
'R-BG': '{:retro_currency:$c}',
0/R-BG ' $0'
'''

def test_retro_upper1(self):
r'''
'TEFT': 'test',
Expand Down Expand Up @@ -936,6 +951,21 @@ def test_retro_upper20(self):
* "foo "
'''

def test_retro_upper21(self):
r'''
'TEFT': 'test',
'RUP': '{:retro_case:upper_first_word}',
TEFT/RUP " TEST"
'''

def test_lower_first_char_1(self):
r'''
'TEFT': '{:case:lower_first_char}TEST',
TEFT ' tEST'
'''

def test_upper1(self):
r'''
'TP*U': '{<}',
Expand Down Expand Up @@ -1017,6 +1047,14 @@ def test_upper9(self):
TP*U/TEFT/W/W " TEST with with"
'''

def test_upper10(self):
r'''
'TEFT': '{:case:upper_first_word}test',
'-G': '{^ing}',
TEFT/-G ' TESTING'
'''

def test_attach_glue_and_carry_capitalize(self):
r'''
'PH*': '{&m}',
Expand Down Expand Up @@ -1179,6 +1217,17 @@ def test_mode_3b(self):
TEFT 'glueTest'
'''

def test_mode_4(self):
r'''
"PHO*D": "{:mode:CAMEL}",
"TEFT": "test",
"TKPWHRAOU": "{&g}{&l}{&u}{&e}"
PHO*D ''
TKPWHRAOU 'glue'
TEFT 'glueTest'
'''

def test_fingerspelling_1a(self):
r'''
'K': '{&c}',
Expand Down Expand Up @@ -1499,3 +1548,15 @@ def macro(translator, stroke, cmdline):
registry = Registry()
registry.register_plugin('macro', 'macro', macro)
monkeypatch.setattr('plover.translation.registry', registry)

def test_valid_macro_3(self, monkeypatch):
r'''
"TEFT": "=macro",
TEFT ""
'''
def macro(translator, stroke, cmdline):
assert cmdline == ''
registry = Registry()
registry.register_plugin('macro', 'macro', macro)
monkeypatch.setattr('plover.translation.registry', registry)

0 comments on commit 23db9f3

Please sign in to comment.