Skip to content

Instantly share code, notes, and snippets.

@rntz
Last active April 23, 2022 14:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rntz/914bdb60187858d4a014e82fbcf591c3 to your computer and use it in GitHub Desktop.
Save rntz/914bdb60187858d4a014e82fbcf591c3 to your computer and use it in GitHub Desktop.
Quick macros for talon
search:
edit.find()
user.quick_macro_set("edit.find_next")
go next | later:
edit.find_next()
user.quick_macro_set("edit.find_next")
go last | prior:
edit.find_previous()
user.quick_macro_set("edit.find_previous")
don't | undo [that]:
edit.undo()
user.quick_macro_set("edit.undo")
# Emacs keyboard macro commands
emacs record: key("ctrl-x (")
emacs stop: key("ctrl-x )")
emacs play:
key(ctrl-x e)
user.quick_macro_set("key", "ctrl-x e")
from talon import Module, noise, actions
mod = Module()
@mod.action_class
class Actions:
def pop(): "Action that occurs on pop."
def on_pop(active):
print(f"!!! POP {active} !!!")
actions.user.pop()
noise.register("pop", on_pop)
from talon import Context, Module, actions, noise, ui
from typing import Callable, Union, Any
import logging
mod = Module()
ctx = Context()
# This could be None or a tuple.
# None: unassigned.
# tuple: (actions, *args). Action is the path to an action.
quick_macro = None
@mod.action_class
class Actions:
def quick_macro_clear():
"""Clears the quick macro"""
global quick_macro
# logging.info("== Quick macro cleared ==")
quick_macro = None
def quick_macro_set(action: str, arg: Any = None):
"""Sets the quick macro"""
global quick_macro
quick_macro = (action, arg) if arg is not None else (action,)
logging.info(f"== quick macro set to {quick_macro!r} ==")
def quick_macro_run():
"""Runs the quick macro"""
if not isinstance(quick_macro, tuple):
if quick_macro is None:
logging.info("== quick macro invoked, but no quick macro assigned ==")
else:
logging.warn(f"== unknown quick macro invoked: {quick_macro!r} ==")
return
logging.info(f"== quick macro invoked: {quick_macro!r} ==")
action, *args = quick_macro
func = actions
for pathelt in action.split('.'):
func = getattr(func, pathelt)
func(*args)
ui.register("app_deactivate", lambda app: actions.user.quick_macro_clear())
ui.register("win_focus", lambda win: actions.user.quick_macro_clear())
@ctx.action_class("user")
class NoiseActions:
def pop():
actions.user.quick_macro_run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment