Skip to content

Commit 23db9f3

Browse files
committed
formatting: add user friendly names for built-in metas
Using the `{:meta:args}` syntax.
1 parent 433799c commit 23db9f3

File tree

13 files changed

+398
-270
lines changed

13 files changed

+398
-270
lines changed

plover/formatting.py

Lines changed: 115 additions & 261 deletions
Large diffs are not rendered by default.

plover/meta/__init__.py

Whitespace-only changes.

plover/meta/attach.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from os.path import commonprefix
2+
3+
from plover.formatting import (
4+
Case,
5+
META_ATTACH_FLAG,
6+
META_CARRY_CAPITALIZATION,
7+
has_word_boundary,
8+
rightmost_word,
9+
)
10+
from plover.orthography import add_suffix
11+
12+
13+
def meta_attach(ctx, meta):
14+
action = ctx.new_action()
15+
begin = meta.startswith(META_ATTACH_FLAG)
16+
end = meta.endswith(META_ATTACH_FLAG)
17+
if begin:
18+
meta = meta[len(META_ATTACH_FLAG):]
19+
action.prev_attach = True
20+
if end:
21+
meta = meta[:-len(META_ATTACH_FLAG)]
22+
action.next_attach = True
23+
last_word = ctx.last_action.word or ''
24+
if not meta:
25+
# We use an empty connection to indicate a "break" in the
26+
# application of orthography rules. This allows the
27+
# stenographer to tell Plover not to auto-correct a word.
28+
action.orthography = False
29+
elif (
30+
last_word and
31+
not meta.isspace() and
32+
ctx.last_action.orthography and
33+
begin and (not end or has_word_boundary(meta))
34+
):
35+
new_word = add_suffix(last_word, meta)
36+
common_len = len(commonprefix([last_word, new_word]))
37+
replaced = last_word[common_len:]
38+
action.prev_replace = ctx.last_text(len(replaced))
39+
assert replaced.lower() == action.prev_replace.lower()
40+
last_word = last_word[:common_len]
41+
meta = new_word[common_len:]
42+
action.text = meta
43+
if action.prev_attach:
44+
action.word = rightmost_word(last_word + meta)
45+
return action
46+
47+
def meta_carry_capitalize(ctx, meta):
48+
# Meta format: ^~|content^ (attach flags are optional)
49+
action = ctx.new_action()
50+
if ctx.last_action.next_case == Case.CAP_FIRST_WORD:
51+
action.next_case = Case.CAP_FIRST_WORD
52+
begin = meta.startswith(META_ATTACH_FLAG)
53+
if begin:
54+
meta = meta[len(META_ATTACH_FLAG):]
55+
action.prev_attach = True
56+
meta = meta[len(META_CARRY_CAPITALIZATION):]
57+
end = meta.endswith(META_ATTACH_FLAG)
58+
if end:
59+
meta = meta[:-len(META_ATTACH_FLAG)]
60+
action.next_attach = True
61+
if meta or begin or end:
62+
action.text = meta
63+
return action

plover/meta/case.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from plover.formatting import Case, apply_case
2+
3+
4+
def meta_case(ctx, case):
5+
case = Case(case)
6+
action = ctx.copy_last_action()
7+
action.next_case = case
8+
return action
9+
10+
def meta_retro_case(ctx, case):
11+
case = Case(case)
12+
action = ctx.copy_last_action()
13+
action.prev_attach = True
14+
last_words = ctx.last_words(count=1)
15+
if last_words:
16+
action.prev_replace = last_words[0]
17+
action.text = apply_case(last_words[0], case)
18+
else:
19+
action.text = ''
20+
return action

plover/meta/command.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def meta_command(ctx, command):
2+
action = ctx.copy_last_action()
3+
action.command = command
4+
return action

plover/meta/currency.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def meta_retro_currency(ctx, dict_format):
2+
action = ctx.copy_last_action()
3+
last_words = ctx.last_words(count=1)
4+
if not last_words:
5+
return action
6+
for cast, fmt in (
7+
(float, '{:,.2f}'),
8+
(int, '{:,}' ),
9+
):
10+
try:
11+
cast_input = cast(last_words[0])
12+
except ValueError:
13+
pass
14+
else:
15+
currency_format = dict_format.replace('c', fmt)
16+
action.prev_attach = True
17+
action.prev_replace = last_words[0]
18+
action.text = currency_format.format(cast_input)
19+
action.word = None
20+
return action

plover/meta/glue.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def meta_glue(ctx, text):
2+
action = ctx.new_action()
3+
action.glue = True
4+
action.text = text
5+
if ctx.last_action.glue:
6+
action.prev_attach = True
7+
return action

plover/meta/key_combo.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def meta_key_combo(ctx, combo):
2+
action = ctx.copy_last_action()
3+
action.combo = combo
4+
return action

plover/meta/mode.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from plover.formatting import Case, SPACE
2+
3+
4+
MODE_CAMEL = 'CAMEL'
5+
MODE_CAPS = 'CAPS'
6+
MODE_LOWER = 'LOWER'
7+
MODE_RESET = 'RESET'
8+
MODE_RESET_CASE = 'RESET_CASE'
9+
MODE_RESET_SPACE = 'RESET_SPACE'
10+
MODE_SET_SPACE = 'SET_SPACE:'
11+
MODE_SNAKE = 'SNAKE'
12+
MODE_TITLE = 'TITLE'
13+
14+
15+
def meta_mode(ctx, mode):
16+
"""
17+
mode should be:
18+
CAPS, LOWER, TITLE, CAMEL, SNAKE, RESET_SPACE,
19+
RESET_CASE, SET_SPACE or RESET
20+
21+
CAPS: UPPERCASE
22+
LOWER: lowercase
23+
TITLE: Title Case
24+
CAMEL: titleCase, no space, initial lowercase
25+
SNAKE: Underscore_space
26+
RESET_SPACE: Space resets to ' '
27+
RESET_CASE: Reset to normal case
28+
SET_SPACE:xy: Set space to xy
29+
RESET: Reset to normal case, space resets to ' '
30+
"""
31+
action = ctx.copy_last_action()
32+
if mode == MODE_CAPS:
33+
action.case = Case.UPPER
34+
elif mode == MODE_TITLE:
35+
action.case = Case.TITLE
36+
elif mode == MODE_LOWER:
37+
action.case = Case.LOWER
38+
elif mode == MODE_SNAKE:
39+
action.space_char = '_'
40+
elif mode == MODE_CAMEL:
41+
action.case = Case.TITLE
42+
action.space_char = ''
43+
action.next_case = Case.LOWER_FIRST_CHAR
44+
elif mode == MODE_RESET:
45+
action.space_char = SPACE
46+
action.case = None
47+
elif mode == MODE_RESET_SPACE:
48+
action.space_char = SPACE
49+
elif mode == MODE_RESET_CASE:
50+
action.case = None
51+
elif mode.startswith(MODE_SET_SPACE):
52+
action.space_char = mode[len(MODE_SET_SPACE):]
53+
else:
54+
raise ValueError('%r is not a valid mode' % mode)
55+
return action

plover/meta/punctuation.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from plover.formatting import Case
2+
3+
4+
def meta_comma(ctx, text):
5+
action = ctx.new_action()
6+
action.text = text
7+
action.prev_attach = True
8+
return action
9+
10+
def meta_stop(ctx, text):
11+
action = ctx.new_action()
12+
action.prev_attach = True
13+
action.text = text
14+
action.next_case = Case.CAP_FIRST_WORD
15+
return action

0 commit comments

Comments
 (0)