Skip to content

Instantly share code, notes, and snippets.

@rntz
Last active June 17, 2020 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rntz/94db3c665d4837d58cc1dee763a5dadc to your computer and use it in GitHub Desktop.
Save rntz/94db3c665d4837d58cc1dee763a5dadc to your computer and use it in GitHub Desktop.
Uniform handling of delimiters in talon
from talon import Module, Context, actions
delimiters = {x[0]: x[1] for x in "() [] {} <> “”".split()}
mod = Module()
@mod.action_class
class Actions:
def delimiter_close(delimiter: str) -> str:
"""Turns a delimiter into its corresponding closing delimiter."""
return delimiters.get(delimiter, delimiter)
# Inserts a closing delimiter, e.g. "close paren" -> ")".
# Kind of redundant if you already have names for those.
# Symbols it doesn't recognize it will leave alone, e.g. "close quote" -> "'".
close <user.symbol>:
key(user.delimiter_close(symbol))
# Insert a matched pair of delimiters, e.g.
# empty paren -> ()
# empty angle -> <>
# empty quote -> ''
empty <user.symbol>:
insert(symbol)
insert(user.delimiter_close(symbol))
# The same thing, but leaves the cursor between the delimiters afterwards.
(in|between) <user.symbol>:
insert(symbol)
insert(user.delimiter_close(symbol))
edit.left()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment