Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
formatting: add user friendly names for built-in metas
Using the `{:meta:args}` syntax.
- Loading branch information
1 parent
433799c
commit 23db9f3
Showing
13 changed files
with
398 additions
and
270 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
def meta_command(ctx, command): | ||
action = ctx.copy_last_action() | ||
action.command = command | ||
return action |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
def meta_key_combo(ctx, combo): | ||
action = ctx.copy_last_action() | ||
action.combo = combo | ||
return action |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.