From 4c1c9aff2c4e372d2ea6731a32f425fe2c94c4c2 Mon Sep 17 00:00:00 2001 From: Jeff Knaus Date: Sat, 13 Apr 2024 11:29:25 -0400 Subject: [PATCH] Talon v0.4 support: common commands for talon python / talon files / .talon-list files (#1240) Splitting https://github.com/talonhub/community/pull/1234 into a few pull requests for convenience. This is not fully vetted yet, opening a draft for convenience and initial discussion. - Support for common commands when composing talon-list, .talon, and talon .py - Support for the talon debug window, with commands consistent with other existing commands --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Nicholas Riley --- .../talon_debug_window/talon_debug_window.py | 16 +++++ .../talon_debug_window.talon | 24 +++++++ apps/{ => talon}/talon_repl/talon_repl.talon | 0 core/modes/language_modes.py | 1 + lang/tags/functions_common.py | 6 +- lang/talon/talon-list.talon | 5 ++ lang/talon/talon.py | 65 +++++++++++++++---- lang/talon/talon.talon | 39 ----------- .../talon_code_common_function.talon-list | 7 ++ lang/talon/talon_common.talon | 22 +++++++ lang/talon/talon_context.talon | 22 +++++++ lang/talon/talon_python_activator.talon | 14 ++++ 12 files changed, 168 insertions(+), 53 deletions(-) create mode 100644 apps/talon/talon_debug_window/talon_debug_window.py create mode 100644 apps/talon/talon_debug_window/talon_debug_window.talon rename apps/{ => talon}/talon_repl/talon_repl.talon (100%) create mode 100644 lang/talon/talon-list.talon create mode 100644 lang/talon/talon_code_common_function.talon-list create mode 100644 lang/talon/talon_common.talon create mode 100644 lang/talon/talon_context.talon create mode 100644 lang/talon/talon_python_activator.talon diff --git a/apps/talon/talon_debug_window/talon_debug_window.py b/apps/talon/talon_debug_window/talon_debug_window.py new file mode 100644 index 0000000000..664bdf0c5b --- /dev/null +++ b/apps/talon/talon_debug_window/talon_debug_window.py @@ -0,0 +1,16 @@ +# this functionality is only available in the talon beta +from talon import Module + +mod = Module() +mod.apps.talon_debug_window = """ +os: mac +and app.bundle: com.talonvoice.Talon +win.title: Talon Debug +""" +mod.apps.talon_debug_window = """ +os: windows +and app.name: Talon +os: windows +and app.exe: talon.exe +win.title: Talon Debug +""" diff --git a/apps/talon/talon_debug_window/talon_debug_window.talon b/apps/talon/talon_debug_window/talon_debug_window.talon new file mode 100644 index 0000000000..0c44a6e4a8 --- /dev/null +++ b/apps/talon/talon_debug_window/talon_debug_window.talon @@ -0,0 +1,24 @@ +# this functionality is only available in the talon beta +# note: these commands are only useful when the search box is focused +app: talon_debug_window +- +# uncomment user.talon_populate_lists tag to activate talon-specific lists of actions, scopes, modes etcetera. +# Do not enable this tag with dragon, as it will be unusable. +# with conformer, the latency increase may also be unacceptable depending on your cpu +# see https://github.com/talonhub/community/issues/600 +# tag(): user.talon_populate_lists + +tag {user.talon_tags}: "{talon_tags}" + +#commands for dictating key combos +key over: "{keys}" +key over: "{modifiers}" + +action {user.talon_actions}: "{talon_actions}" +# requires user.talon_populate_lists tag. do not use with dragon +list {user.talon_lists}: "{talon_lists}" + +# requires user.talon_populate_lists tag. do not use with dragon +capture {user.talon_captures}: "{talon_captures}" +set {user.talon_settings}: "{talon_settings}" +application {user.talon_apps}: "{talon_apps}" diff --git a/apps/talon_repl/talon_repl.talon b/apps/talon/talon_repl/talon_repl.talon similarity index 100% rename from apps/talon_repl/talon_repl.talon rename to apps/talon/talon_repl/talon_repl.talon diff --git a/core/modes/language_modes.py b/core/modes/language_modes.py index 9a4f74f7fd..99e4f7043e 100644 --- a/core/modes/language_modes.py +++ b/core/modes/language_modes.py @@ -37,6 +37,7 @@ # 'snippets': 'snippets', "sql": "sql", "talon": "talon", + "talonlist": "talon-list", "terraform": "tf", "tex": "tex", "typescript": "ts", diff --git a/lang/tags/functions_common.py b/lang/tags/functions_common.py index b40ad226e9..d581ee7525 100644 --- a/lang/tags/functions_common.py +++ b/lang/tags/functions_common.py @@ -30,7 +30,7 @@ def code_toggle_functions(): if gui_functions.showing: function_list = [] gui_functions.hide() - ctx.tags.discard("user.code_functions_common_gui_active") + ctx.tags = [] else: update_function_list_and_freeze() @@ -57,7 +57,7 @@ def update_function_list_and_freeze(): function_list = [] gui_functions.show() - ctx.tags.add("user.code_functions_common_gui_active") + ctx.tags = ["user.code_functions_common_gui_active"] @imgui.open() @@ -74,7 +74,7 @@ def gui_functions(gui: imgui.GUI): gui.spacer() if gui.button("Toggle funk (close window)"): - actions.user.code_toggle_functions_hide() + actions.user.code_toggle_functions() def commands_updated(_): diff --git a/lang/talon/talon-list.talon b/lang/talon/talon-list.talon new file mode 100644 index 0000000000..14e2d4d35f --- /dev/null +++ b/lang/talon/talon-list.talon @@ -0,0 +1,5 @@ +code.language: talonlist +- +# requires user.talon_populate_lists tag. do not use with dragon +list [require] {user.talon_lists}: "list: {talon_lists}" +list [require]: "list: " diff --git a/lang/talon/talon.py b/lang/talon/talon.py index a7ec99f0e1..e5c7319792 100644 --- a/lang/talon/talon.py +++ b/lang/talon/talon.py @@ -1,9 +1,8 @@ from talon import Context, Module, actions, app, registry mod = Module() -ctx = Context() - - +ctx_talon = Context() +ctx_talon_python = Context() ctx_talon_lists = Context() # restrict all the talon_* lists to when the user.talon_populate_lists tag @@ -30,15 +29,13 @@ mod.list("talon_scopes") mod.list("talon_modes") -ctx.matches = r""" +ctx_talon.matches = r""" code.language: talon """ -ctx.lists["user.code_common_function"] = { - "insert": "insert", - "key": "key", - "print": "print", - "repeat": "repeat", -} + +ctx_talon_python.matches = r""" +tag: user.talon_python +""" def on_update_decls(decls): @@ -73,7 +70,53 @@ def on_ready(): app.register("ready", on_ready) -@ctx.action_class("user") +@mod.action_class +class Actions: + def talon_code_insert_action_call(text: str, selection: str): + """inserts talon-specific action call""" + actions.user.code_insert_function(text, selection) + + def talon_code_enable_tag(tag: str): + """enables tag in either python or talon files""" + + def talon_code_enable_setting(setting: str): + """asserts setting in either python or talon files""" + + +@ctx_talon.action_class("user") +class TalonActions: + def talon_code_enable_tag(tag: str): + """enables tag in either python or talon files""" + actions.user.paste(f"tag(): {tag}") + + def talon_code_enable_setting(setting: str): + """asserts setting in either python or talon files""" + actions.user.paste(f"{setting} = ") + + +@ctx_talon_python.action_class("user") +class TalonPythonActions: + def talon_code_insert_action_call(text: str, selection: str): + text = f"actions.{text}({selection or ''})" + actions.user.paste(text) + actions.edit.left() + + def talon_code_enable_tag(tag: str): + """enables tag in either python or talon files""" + actions.user.paste(f'ctx.tags = ["{tag}"]') + if not tag: + actions.edit.left() + actions.edit.left() + + def talon_code_enable_setting(setting: str): + """asserts setting in either python or talon files""" + if not setting: + actions.user.insert_between('ctx.settings["', '"] = ') + else: + actions.user.paste(f'ctx.settings["{setting}"] = ') + + +@ctx_talon.action_class("user") class UserActions: def code_operator_and(): actions.auto_insert(" and ") diff --git a/lang/talon/talon.talon b/lang/talon/talon.talon index 88828b9773..df2b7e432b 100644 --- a/lang/talon/talon.talon +++ b/lang/talon/talon.talon @@ -10,44 +10,5 @@ tag(): user.code_functions_common # see https://github.com/talonhub/community/issues/600 # tag(): user.talon_populate_lists -dot talon: insert(".talon") #defintion blocks for the context -action block: user.insert_between("action(", "):") setting block: insert("settings():\n\t") -setting {user.talon_settings}: user.paste("{talon_settings} = ") -#context requirements -win require: insert("os: windows\n") -mac require: insert("os: mac\n") -linux require: insert("os: linux\n") -title require: insert("win.title: ") -application [require] [{user.talon_apps}]: - app = talon_apps or "" - user.paste("app: {app}") -mode require [{user.talon_modes}]: - mode = talon_modes or "" - user.paste("mode: {mode}") -tag require [{user.talon_tags}]: - tag = talon_tags or "" - user.paste("tag: {tag}") -tag set [{user.talon_tags}]: - tag = talon_tags or "" - user.paste("tag(): {tag}") -host require: - hostname = user.talon_get_hostname() - user.paste("hostname: {hostname}\n") -# requires user.talon_populate_lists tag. do not use with dragon -list {user.talon_lists}: "{{{talon_lists}}}" -# requires user.talon_populate_lists tag. do not use with dragon -capture {user.talon_captures}: "<{talon_captures}>" - -#commands for dictating key combos -key over: "{keys}" -key over: "{modifiers}" - -# all actions (requires uncommenting user.talon_populate_lists tag above) -funk {user.talon_actions}: - user.code_insert_function(talon_actions, edit.selected_text()) -funk cell : user.code_select_function(number - 1, "") -funk wrap : - user.code_insert_function(code_common_function, edit.selected_text()) -funk wrap : user.code_select_function(number - 1, edit.selected_text()) diff --git a/lang/talon/talon_code_common_function.talon-list b/lang/talon/talon_code_common_function.talon-list new file mode 100644 index 0000000000..addc97daa6 --- /dev/null +++ b/lang/talon/talon_code_common_function.talon-list @@ -0,0 +1,7 @@ +list: user.code_common_function +code.language: talon +- +insert +key +print +repeat diff --git a/lang/talon/talon_common.talon b/lang/talon/talon_common.talon new file mode 100644 index 0000000000..3bcd4cb183 --- /dev/null +++ b/lang/talon/talon_common.talon @@ -0,0 +1,22 @@ +#Defines commands common to both python and talon files +code.language: talon +code.language: python +and tag: user.talon_python +- +tag set [{user.talon_tags}]: + tag = talon_tags or "" + user.talon_code_enable_tag(tag) + +# requires user.talon_populate_lists tag. do not use with dragon +list {user.talon_lists}: "{{{talon_lists}}}" +# requires user.talon_populate_lists tag. do not use with dragon +capture {user.talon_captures}: "<{talon_captures}>" + +setting {user.talon_settings}: user.talon_code_enable_setting(talon_settings) + +#commands for dictating key combos +key over: "{keys}" +key over: "{modifiers}" + +action {user.talon_actions}: + user.talon_code_insert_action_call(talon_actions, edit.selected_text()) diff --git a/lang/talon/talon_context.talon b/lang/talon/talon_context.talon new file mode 100644 index 0000000000..d001cba186 --- /dev/null +++ b/lang/talon/talon_context.talon @@ -0,0 +1,22 @@ +code.language: talon +code.language: talonlist +code.language: python +and tag: user.talon_python +- +#context requirements +win require: insert("os: windows\n") +mac require: insert("os: mac\n") +linux require: insert("os: linux\n") +title require: insert("win.title: ") +application [require] [{user.talon_apps}]: + app = "{talon_apps}\n" or "" + insert("app: {app}") +mode require [{user.talon_modes}]: + mode = "{talon_modes}\n" or "" + insert("mode: {mode}") +tag require [{user.talon_tags}]: + tag = "{talon_tags}\n" or "" + insert("tag: {tag}") +host require: + hostname = user.talon_get_hostname() + insert("hostname: {hostname}\n") diff --git a/lang/talon/talon_python_activator.talon b/lang/talon/talon_python_activator.talon new file mode 100644 index 0000000000..3f82ea78f0 --- /dev/null +++ b/lang/talon/talon_python_activator.talon @@ -0,0 +1,14 @@ +# This file activates talon-specific python commands +# by default, it simply looks for the python tag to be active +# lines 7-11 provide examples to make the activation more specific +# which may be preferred by people who code in other python projects +# app: vscode +# Mac VSCode uses an em-dash +# win.title: /— user/ +# win.title: /— community/ +# windows VSCode uses an en-dash +# win.title: / - user - Visual Studio Code/ +# win.title: / - community - Visual Studio Code/ +code.language: python +- +tag(): user.talon_python