def repeat_last_translation(translator: Translator, stroke: Stroke, macro_args: str) -> None: ''' Macro to repeat the last translation(s) in Plover. :param translator: The active Plover translator that is executing the macro. :type translator: plover.translation.Translator :param stroke: The current stroke (what invoked this macro). :type stroke: plover.translation.Stroke :param macro_args: The optional arguments specified to the macro as a comma-delimited string. Piece 1: The number of previous translations to repeat. Default is 1. :type macro_args: str ''' # Get the current state translations = translator.get_state().translations if not translations: return # Process input try: num_to_repeat = int(macro_args.split(DELIM_ARGS)[0]) except: num_to_repeat = 1 # Output the new translations for translation in translations[-num_to_repeat:]: repeated_translation = Translation(translation.strokes, translation.english) translator.translate_translation(repeated_translation)
def repeat_last_fragment(translator: Translator, stroke: Stroke, macro_args: str) -> None: ''' Macro to repeat the last fragments(s) in Plover. :param translator: The active Plover translator that is executing the macro. :type translator: plover.translation.Translator :param stroke: The current stroke (what invoked this macro). :type stroke: plover.translation.Stroke :param macro_args: The optional arguments specified to the macro as a comma-delimited string. Piece 1: The number of previous fragments to repeat. Default is 1. :type macro_args: str ''' # Get the current state translations = translator.get_state().translations if not translations: return # Process input try: num_to_repeat = int(macro_args.split(DELIM_ARGS)[0]) except: num_to_repeat = 1 # Output the new translations formatter = RetroFormatter(translations) last_fragments = formatter.last_fragments(num_to_repeat) for fragment in last_fragments: new_translation = Translation([stroke], fragment) translator.translate_translation(new_translation)
def retro_everything(translator: Translator, stroke: Stroke, cmdline: str): print("\n\n\nRetro everything invoked with: " + str(stroke) + ", " + cmdline) args = cmdline.split(",") left_char = args[0] right_char = args[1] all_translations = translator.get_state().translations affected_translation_cnt = len( list( itertools.takewhile(lambda x: x.strokes[-1] == stroke, reversed(all_translations)))) # translations that _will_ be affected affected_translations = all_translations[-(affected_translation_cnt + 1):] affected_strokes = flatten([x.strokes for x in affected_translations]) affected_string = " ".join( flatten([ recursively_get_old_english(stroke, t) for t in affected_translations ])) resulting_translation = left_char + affected_string + right_char my_trans = Translation(affected_strokes + [stroke], resulting_translation) my_trans.replaced = affected_translations translator.translate_translation(my_trans)