extension.py
Pinchord · pinchord.git · Tickets · Changelog
Branches > main > 67b4fe9 > files / plover_pinchord_31 / extension.py
## Makes the affix keys work on every entry, even user-defined ones
import re
from plover import system
from plover.steno import Stroke
PREFIXES = {(): "{}",
("#-",): "{}{-|}",
("+-",): "{^}",
("#-", "+-"): "{^}{-|}"}
SUFFIXES = {(): "",
("-e",): "{^}e",
("-s",): "{^}s",
("-e", "-s"): "{^}es"}
# Macros don't work with translations like {} (only =undo works, not {}=undo)
MACRO = r"=\w+(:.*)?|\{\*[!?+]?\}"
# Wraps the given translation in its affix translations
# e.g. with_affixes(Stroke("#"), "test", Stroke("-s")) -> "{}{-|}test{^}s"
def with_affixes(prefixes, translation, suffixes):
if translation is None:
return None
elif re.fullmatch(MACRO, translation):
return None if prefixes or suffixes else translation
else:
return PREFIXES[prefixes.keys()] + translation + SUFFIXES[suffixes.keys()]
# Looks up the stroke in the dictionary stack after splitting the affix keys,
# returns the translation joined with the affix key translations
def affixed_lookup(lookup, stroke):
prefixes = stroke & Stroke("#+")
suffixes = stroke & Stroke("-es")
stroke_without_affixes = stroke - prefixes - suffixes
stroke_entry = lookup((stroke.rtfcre,))
if stroke_entry is not None and stroke != stroke_without_affixes:
return stroke_entry
elif not stroke_without_affixes and suffixes:
return with_affixes(prefixes, "", suffixes)
else:
return with_affixes(prefixes, lookup((stroke_without_affixes.rtfcre,)), suffixes)
class Extension:
def __init__(self, engine):
self.dictionaries = engine.dictionaries
self.original_lookup = self.dictionaries.lookup
def start(self):
self.dictionaries.lookup = self.lookup
def stop(self):
self.dictionaries.lookup = self.original_lookup
def lookup(self, outline):
# inlines the system name, be aware!
if system.NAME == "Pinchord" and len(outline) == 1:
return affixed_lookup(self.original_lookup, Stroke.from_steno(outline[0]))
else:
return self.original_lookup(outline)