class AtomMapping(MappingRule): mapping = { #"[go to | show] project window": Key("a-1"), # Search. #"replace": Key("c-h"), "show find": Key("c-f"), "find <text>": Key("c-f/25") + Text("%(text)s"), # "find next": Key("f3"), # "find (prev | previous)": Key("s-f3"), # "find in files": Key("cs-f"), # Code. "[shoreline | show] line <w> [<x>] [<y>] [<z>]": Key("c-g/25") + Function(printNumber) + Key("enter"), "show white space": Key("cs-w"), # Window handling. "new tab": Key("c-n"), "next tab [<t>]": Key("c-pagedown:%(t)d"), "(preev | previous) tab [<t>]": Key("c-pageup:%(t)d"), "close tab": Key("c-w"), "(full-screen | full screen)": Key("cs-x"), } extras = [ Integer("t", 1, 50), Dictation("text"), IntegerRef("n", 1, 50000), Integer("w", 0, 10), Integer("x", 0, 10), Integer("y", 0, 10), Integer("z", 0, 10) ] defaults = { "t": 1, }
class lyx_mathematics(MergeRule): non = lyx_mathematicsNon pronunciation = BINDINGS["pronunciation"] mapping = { BINDINGS["symbol1_prefix"] + " <symbol1>": Text("\\%(symbol1)s "), BINDINGS["symbol2_prefix"] + " <symbol2>": Text("\\%(symbol2)s "), BINDINGS["accent_prefix"] + " <accent>": Key("a-m, %(accent)s"), BINDINGS["text_prefix"] + " <text_modes>": Text("\\%(text_modes)s "), BINDINGS["greek_prefix"] + " [<big>] <greek_letter>": Function(greek), "<misc_lyx_keys>": Key("%(misc_lyx_keys)s"), "<command>": Function(execution.alternating_command), "matrix <rows> by <cols>": Function(matrix), "<numbers> <denominator>": Key("a-m, f, %(numbers)s, down, %(denominator)s, right"), } extras = [ IntegerRef("rows", 1, BINDINGS["max_matrix_size"]), IntegerRef("cols", 1, BINDINGS["max_matrix_size"]), IntegerRef("numbers", 0, CORE["numbers_max"]), Choice("big", {CORE["capitals_prefix"]: True}), Choice("greek_letter", BINDINGS["greek_letters"]), Choice("symbol1", BINDINGS["tex_symbols1"]), Choice("symbol2", BINDINGS["tex_symbols2"]), Choice("accent", BINDINGS["accents"]), Choice("text_modes", BINDINGS["text_modes"]), Choice("misc_lyx_keys", BINDINGS["misc_lyx_keys"]), Choice("command", BINDINGS["misc_lyx_commands"]), Choice("denominator", BINDINGS["denominators"]), ] defaults = { "big": False, }
class Punctuation(MappingRule): """ Not implemented yet: # Append punctuation at end of line and shock "(tell | tau) <semi>": Function(navigation.next_line), # Append punctuation at end of line and create new line above "(hark | heart) <semi>": Function(navigation.previous_line), """ mapping = { "[<long>] <text_punc> [<n>]": Text("%(long)s" + "%(text_punc)s" + "%(long)s") * Repeat(extra="n"), "<double_text_punc> [<n>]": Text("%(double_text_punc)s") + Key("left") * Repeat(extra="n"), "tabby [<n>]": Key("tab") * Repeat(extra="n"), "(back | shin) tabby [<n>]": Key("s-tab") * Repeat(extra="n"), "boom [<n>]": Text(", ") * Repeat(extra="n"), "bam [<n>]": Text(". ") * Repeat(extra="n"), "ace [<n100>]": Text(" ") * Repeat(extra="n100"), } extras = [ IntegerRef("n", 0, 10), IntegerRef("n100", 0, 100), Choice( "long", { "long": " ", }), Choice( "text_punc", text_punc_dict()), Choice( "double_text_punc", double_text_punc_dict()) ] defaults = { "n": 1, "n100": 1, "long": "", }
def _select_and_cut_text(wordCount): """Selects wordCount number of words to the left of the cursor and cuts them out of the text. Returns the text from the system clip board. """ clipboard = Clipboard() clipboard.set_system_text('') try: # Try selecting n number of words. Key('ctrl:down, shift:down').execute() Key('left:%s' % wordCount).execute() Key('shift:up').execute() finally: # It is important to make sure that the buttons are released. # Otherwise you get stuck in an unpleasant situation. Key('shift:up, ctrl:up').execute() Pause("10").execute() Key('c-x').execute() # Cut out the selected words. Pause("20").execute() return clipboard.get_system_text()
def _execute(self, data=None): win32clipboard.OpenClipboard() win32clipboard.EmptyClipboard() try: win32clipboard.SetClipboardData(win32clipboard.CF_UNICODETEXT, unicode(" " + data["emoji"])) finally: win32clipboard.CloseClipboard() action = Key("c-v/5") + self.enter action.execute()
def _process_recognition(self, value, extras): node = extras['_node'] if 'minus' in node.words(): count = extras['n'] direction = 'left' else: count = extras['n'] - 1 direction = 'right' action = Key("w-b/10, s-tab/10, " + direction + ":%d/10" % count) + value action.execute()
class Tmux(MergeRule): pronunciation = "multiplex" mapping = { "plex": R(Key("c-b")), "plex pain": R(Key("c-b,q")), "plex flip": R(Key("c-b,o")), "plex flip run": R(Key("c-b,o") + Key("up") + Key("enter") + Key("c-b,o")), "plex flop": R(Key("c-b,l")), "plex new tab": R(Key("c-b,c")), "plex rename tab": R(Key("c-b,comma")), } extras = [ Dictation("text"), Dictation("mim"), IntegerRefST("n", 1, 1000), ] defaults = {"n": 1, "mim": ""}
def _execute(self, data=None): win32clipboard.OpenClipboard() win32clipboard.EmptyClipboard() try: win32clipboard.SetClipboardData(win32clipboard.CF_UNICODETEXT, unicode(self.clipboard)) finally: win32clipboard.CloseClipboard() action = self.open_run + Key("c-v/5, enter/100") + self.action action.execute()
class BufferRule(MappingRule): mapping = { "file save": Key("colon, w, exclamation, enter"), "file save all": Key("colon, w, a, exclamation, enter"), "file quit": Key("colon, q, exclamation, enter"), "file quit all": Key("colon, q, a, exclamation, enter"), "file done": Key("colon, x, exclamation, enter"), "file done all": Key("colon, x, a, exclamation, enter"), "file reload": Key("colon, e, exclamation, enter"), "file open [<text>]": Key("colon, e") + Text(" <text>"), "buffer next": Key('colon, b, n, e, x, t, enter'), "buffer previous": Key('colon, b, p, r, e, v, i, o, u, s, enter'), } extras = [ Dictation("text"), ] defaults = { "text": "", }
class CommandRule(MappingRule): mapping = { "insert image": R(Key("alt, n, p"), rdescript="Word: Insert Image"), } extras = [ Dictation("dict"), IntegerRef("n", 1, 100), ] defaults = {"n": 1, "dict": "nothing"}
class MainRule(MappingRule): mapping = { # it is this section that you want to fiddle around with if you're new: mapping, extras, and defaults # in the next line, there are two things to observe: # the first is the use of parentheses and the pipe symbol (|) # --this lets me use either "lock dragon" or "deactivate" to trigger that command. # The next is the playback action, which lets me tell Dragon to simulate me speaking some words. #'(lock Dragon | deactivate)': # Playback([(["go", "to", "sleep"], 0.0)]), # Here I'm using BringApp-- this is the same as typing what goes in between the parentheses # into the Windows command prompt, without the quotes and commas, like: # explorer C:\NatLink\NatLink\MacroSystem # -- (which would open Windows Explorer at the specified location). Anything you can do with the command line can be done this way # IMPORTANT: If you don't have Dragonfly 6.6 or later, lines 40 and 46 will cause this file to crash and should be commented out "ploy": Key('c-m'), # here I'm using the Key action to press some keys -- see the documentation here: http://dragonfly.readthedocs.org/en/latest/actions.html?highlight=key#module-dragonfly.actions.action_key #"remax": # Key("a-space/10,r/10,a-space/10,x"), # here I'm chaining a bunch of different actions together to do a complex task #"(show | open) documentation": # BringApp('C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe') + # WaitWindow(executable="chrome.exe") + Key('c-t') + # WaitWindow(title="New Tab") + # Text('http://dragonfly.readthedocs.org/en/latest') + Key('enter'), # # # here I'm just saying one word to trigger some other words # "hotel": # Text("hotels are not cheap"), # # # If you need to do more complicated tasks, or use external resources, a function might be what you need. # # note that here, I'm using extras: "n" and "text" # # The angle brackets <> meaning I'm using an extra, and the square brackets [] mean that I don't have to speak that word, it's optional. # # Advice: if you use an optional extra, like I am with "text", you should set a default value in the defaults section down below. # # To trigger the following command, you would have to say the word "function" followed by a number between 1 and 1000. # '[use] function <n> [<text>]': # Function(my_function, extra={'n', 'text'}), } extras = [ IntegerRef("n", 1, 1000), Dictation("text"), Choice("choice", { "alarm": "alarm", "custom grid": "CustomGrid", "element": "e" }), ] defaults = { # "n": 1, # "text": "", }
class MainRule(MappingRule): mapping = { # It is this section that you want to fiddle around with if you're new: mapping, extras, and defaults # In the next line, there are two things to observe: # the first is the use of parentheses and the pipe symbol (|) # --this lets me use either "lock dragon" or "deactivate" to trigger that command. # The next is the playback action, which lets me tell Dragon to simulate me speaking some words. '(lock Dragon | deactivate)': Playback([(["go", "to", "sleep"], 0.0)]), # Here I'm using BringApp-- this is the same as typing what goes in between the parentheses # Into the Windows command prompt, without the quotes and commas, like: # explorer C:\NatLink\NatLink\MacroSystem # -- (which would open Windows Explorer at the specified location). Anything you can do with the command line can be done this way "open natlink folder": BringApp("explorer", r"C:\NatLink\NatLink\MacroSystem"), # Here I'm using the Key action to press some keys -- see the documentation here: https://dragonfly2.readthedocs.io/en/latest/actions.html?highlight=key#module-dragonfly.actions.action_key "remax": Key("a-space/10,r/10,a-space/10,x"), # Here I'm chaining a bunch of different actions together to do a complex task "(show | open) documentation": BringApp('C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe') + WaitWindow(executable="chrome.exe") + Key('c-t') + WaitWindow(title="New Tab") + Text('https://dragonfly2.readthedocs.io/en/latest') + Key('enter'), # Here I'm just saying one word to trigger some other words "hotel": Text("hotels are not cheap"), # If you need to do more complicated tasks, or use external resources, a function might be what you need. # Note that here, I'm using extras: "n" and "text" # The angle brackets <> meaning I'm using an extra, and the square brackets [] mean that I don't have to speak that word, it's optional. # Advice: if you use an optional extra, like I am with "text", you should set a default value in the defaults section down below. # To trigger the following command, you would have to say the word "function" followed by a number between 1 and 1000. '[use] function <n> [<text>]': Function(my_function, extra={'n', 'text'}), # Sometimes it's easier to have things as a list in a command as a choice that do different things. # That's what `<choice>` Is defined in `extras` allows you define that list. If you dictate `i choose custom grid` Then `CustomGrid` will be printed as text. # Items in the list are pairs. e.g `{"custom grid": "CustomGrid"}` The first item of a pair is the command "custom grid" and the second "CustomGrid" output text action. "i choose <choice>": Text("<choice>"), } extras = [ IntegerRef("n", 1, 1000), Dictation("text"), Choice("choice", { "alarm": "alarm", "custom grid": "CustomGrid", "element": "e" }), ] defaults = { "n": 1, "text": "", }
def copypaste_delete_until_phrase(direction, phrase, number_of_lines_to_search, before_after, occurrence_number, dictation_versus_character): if direction == "up" or direction == "down": number_of_lines_to_search, direction = deal_with_up_down_directions( direction, number_of_lines_to_search) application = get_application() if not before_after: # default to delete all the way through the phrase not just up until it if direction == "left": before_after = "before" if direction == "right": before_after = "after" selected_text = select_text_and_return_it(direction, number_of_lines_to_search, application) if not selected_text: return phrase = str(phrase) new_text = delete_until_phrase(selected_text, phrase, direction, before_after, occurrence_number, dictation_versus_character) if new_text is None: # do NOT use `if not new_text` because that will pick up the case where new_text="" which # occurs if the phrase is at the beginning of the line # phrase not found # deal_with_phrase_not_found(selected_text, temp_for_previous_clipboard_item, application, direction) deal_with_phrase_not_found(selected_text, application, direction) return if new_text == "": # phrase is at the beginning of the line Key("del").execute() return else: text_manipulation_paste(new_text, application) if direction == "right": offset = len(new_text) Key("left:%d" % offset).execute()
class Searching(MappingRule): mapping = { "search [<text_transform> [case]] <search_term>": Text("/") + Function(search) + Key("enter"), "search this word": Text("#"), "no (highlight | H L)": Text(":nohl\n"), "next": Key("n"), "previous": Key("N"), } extras = [ Dictation("search_term"), Choice("text_transform", { "snake": "snake", "pascal": "pascal", "kebab": "kebab", "camel": "camel", }, default="") ]
def show_natlink_messages_window(duration=3, msg="show_natlink_messages_window"): """ bring the natlink messages window to focus/front to see what is being printed """ sound.play(sound.SND_MESSAGE) FocusWindow("natspeak", "Messages from Python Macros").execute() print(msg) if duration > 0: time.sleep(duration) Key("alt:down, tab:down/5, alt:up").execute()
def paste_string(content): time.sleep(0.05) cb = Clipboard(from_system=True) try: Clipboard.set_system_text(str(content)) Key("c-v").execute() time.sleep(0.05) cb.copy_to_system() except Exception: return False return True
class RustNon(MappingRule): mapping = { "macro format string": R(Text("format!()") + Key("left"), rdescript="Rust: Format String"), "macro panic": R(Text("panic!()") + Key("left"), rdescript="Rust: Panic"), "macro assertion": R(Text("assert_eq!()") + Key("left"), rdescript="Rust: Assertion"), "ternary": R(Text("if TOKEN == TOKEN { TOKEN } else { TOKEN }"), rdescript="Rust: Ternary"), "function [<return>]": R(Text("fn TOKEN(TOKEN)%(return)s{}"), rdescript="Rust: Function"), "infinite loop": R(Text("loop {}") + Key("left"), rdescript="Rust: Infinite Loop"), } extras = [ Choice("return", {"return": " -> TOKEN "}), ] defaults = {"return": " "}
def get_selected_files(folders=False): ''' Copy selected (text or file is subsequently of interest) to a fresh clipboard ''' cb = Clipboard(from_system=True) cb.clear_clipboard() Key("c-c").execute() time.sleep(0.1) files = get_clipboard_files(folders) cb.copy_to_system() return files
def launch(hmc_type, data=None): from dragonfly import (WaitWindow, FocusWindow, Key) instructions = _get_instructions(hmc_type) if data is not None: # and callback!=None: instructions.append(data) Popen(instructions) hmc_title = _get_title(hmc_type) WaitWindow(title=hmc_title, timeout=5).execute() FocusWindow(title=hmc_title).execute() Key("tab").execute()
def select_text_and_return_it(direction, number_of_lines_to_search, application): if direction == "left": if number_of_lines_to_search > 0: Key("s-up:%d" % number_of_lines_to_search).execute() Key("s-home").execute() if direction == "right": if number_of_lines_to_search > 0: Key("s-down:%d" % number_of_lines_to_search).execute() Key("s-end").execute() selected_text = text_manipulation_copy(application) if selected_text == None: # failed to copy return if selected_text == "": print("no text to select") return return selected_text
class NavigationRule(MappingRule): mapping = { "move up [<n>]": Key("ctrl:down, shift:down, tab:%(n)d, shift:up, ctrl:up"), "move down [<n>]": Key("ctrl:down, tab:%(n)d, ctrl:up"), "close tab": Key("ctrl:down, f4, ctrl:up"), "(join room | private message) <room>": Key("c-j/25") + Text("%(room)s") + Key("enter"), } extras = [ IntegerRef("n", 1, 10), Dictation("room"), ] defaults = { "n": 1, }
class FoxitRule(MergeRule): pronunciation = "fox it reader" mapping = { "next tab [<n>]": R(Key("c-tab"), rdescript="Foxit Reader: Next Tab") * Repeat(extra="n"), "prior tab [<n>]": R(Key("cs-tab"), rdescript="Foxit Reader: Previous Tab") * Repeat(extra="n"), "close tab [<n>]": R(Key("c-f4/20"), rdescript="Foxit Reader: Close Tab") * Repeat(extra="n"), } extras = [ Dictation("text"), Dictation("mim"), IntegerRefST("n", 1, 1000), ] defaults = {"n": 1, "mim": ""}
class MSWordRule(MergeRule): pronunciation = "Microsoft Word" mapping = { "insert image": R(Key("alt, n, p"), rdescript="Word: Insert Image"), } extras = [ Dictation("dict"), IntegerRefST("n", 1, 100), ] defaults = {"n": 1, "dict": "nothing"}
class SnipMateRule(MappingRule): name = "snipmate" mapping = { "snip <text>": Text("%(text)s") + Key("tab"), "snip alias <alias>": Text("%(alias)s") + Key("tab"), # "snip letters <letter_sequence>": ... } extras = [ Dictation("text"), Choice("alias", { # Whatever snippets become useful but hard to say? "fixture": "fix", "method": "defs", "function": "def", "class": "cl", "while loop": "wh", "for loop": "for", "mark code block": "codeblock", }) ]
def kick_dragon(): """ sending dragon to sleep and up again to reload macros """ Key("c-s").execute() show_natlink_messages_window( duration=2, msg="** saved notepad, reloading by mic sleeping then on **") setMicState("sleeping") setMicState("on")
class EasyMotionRule(MappingRule): name = "gvim_EasyMotion" mapping = { "queasy <letter>": Key("backslash,backslash") + Key('%(letter)s'), "queasy <motion>": Key("backslash,backslash") + Key("%(motion)s"), "queasy <find> <letter>": Key("backslash,backslash") + Key("%(find)s,%(letter)s") + Key("enter"), "queasy <search> <text>": Key("backslash,backslash") + Key("%(search)s,%(text)s") + Key("enter"), } extras = [ findChoice('find'), searchChoice('search'), # FIXME: Some of these motion choices should maybe be purged? motionChoice('motion'), letterChoice('letter'), Dictation("text"), ]
class CSSTags(MappingRule): mapping = { "property <command>": Function(middle_slash_format) + Text(":;") + Key("left"), "comment": Text("/* */"), } extras = [ Dictation("command"), ]
class sn_mathematics(MergeRule): non = sn_mathematicsNon pronunciation = BINDINGS["pronunciation"] mapping = { BINDINGS["symbol_prefix"] + " <symbol>": Function(texchar), # BINDINGS["greek_prefix"] + " [<big>] <greek_letter>": Function(greek), BINDINGS["accent_prefix"] + " <accent>": Key("%(accent)s"), "<misc_sn_keys>": Key("%(misc_sn_keys)s"), "<misc_sn_text>": Text("%(misc_sn_text)s"), # "matrix <rows> by <cols>": Function(matrix), "<numbers> <denominator>": Key("c-f, %(numbers)s, down, %(denominator)s, right"), } extras = [ IntegerRef("rows", 1, 6), IntegerRef("cols", 1, 6), IntegerRef("numbers", 0, CORE["numbers_max"]), Choice("big", {CORE["capitals_prefix"]: True}), Choice("greek_letter", BINDINGS["greek_letters"]), Choice("symbol", BINDINGS["tex_symbols"]), Choice("accent", BINDINGS["accents"]), Choice("misc_sn_keys", BINDINGS["misc_sn_keys"]), Choice("misc_sn_text", BINDINGS["misc_sn_text"]), Choice("denominator", BINDINGS["denominators"]), ] defaults = { "big": False, }
def add_onclick(text): Text('// Onclick handler for ').execute() lib.format.camel_case_text(text) Text('\nvar ').execute() lib.format.camel_case_text(text) Text(' = getElementById("#').execute() lib.format.camel_case_text(text) Text('");\n').execute() lib.format.camel_case_text(text) Text('.onclick = function() {\n\n}').execute() Key('up').execute() Text(' ').execute()
def copy_modify_paste(modifying_function): selected_text = utils.getSelectedText() if not selected_text: print "No selected text?" return modified_text = modifying_function(str(selected_text)) # tried to use the original clipboard here, but couldn't get it to "clear" -- some apps would # somehow get the original, unmodified text when the paste happened new_clipboard = Clipboard() new_clipboard.set_text(modified_text) new_clipboard.copy_to_system() Key("c-v").execute()