Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow apps to force lang via code.language and add special cased files #1451

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions apps/obsidian/obsidian.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from talon import Context, Module

mod = Module()
mod.apps.obsidian = "app.name: Obsidian"

lang_ctx = Context()
lang_ctx.matches = r"""
app: obsidian
not tag: user.code_language_forced
"""


@lang_ctx.action_class("code")
class CodeActions:
def language():
return "markdown"
3 changes: 3 additions & 0 deletions apps/obsidian/obsidian.talon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
app: obsidian
-
tag(): user.tabs
27 changes: 25 additions & 2 deletions core/modes/language_modes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from talon import Context, Module, actions
from talon import Context, Module, actions, app

# Maps language mode names to the extensions that activate them. Only put things
# here which have a supported language mode; that's why there are so many
Expand Down Expand Up @@ -49,6 +49,19 @@
"html": "html",
}

# Files without specific extensions but are associated with languages
special_file_map = {
"CMakeLists.txt": "cmake",
"Makefile": "make",
"Dockerfile": "docker",
"meson.build": "meson",
".bashrc": "bash",
".zshrc": "zsh",
"PKGBUILD": "pkgbuild",
".vimrc": "vimscript",
"vimrc": "vimscript",
}

# Override speakable forms for language modes. If not present, a language mode's
# name is used directly.
language_name_overrides = {
Expand All @@ -62,7 +75,6 @@
}

mod = Module()

ctx = Context()

ctx_forced = Context()
Expand Down Expand Up @@ -95,6 +107,10 @@
@ctx.action_class("code")
class CodeActions:
def language():
file_name = actions.win.filename()
if file_name in special_file_map:
return special_file_map[file_name]

file_extension = actions.win.file_ext()
return extension_lang_map.get(file_extension, "")

Expand Down Expand Up @@ -122,3 +138,10 @@ def code_clear_language_mode():
global forced_language
forced_language = ""
ctx.tags = []

def code_show_forced_language_mode():
"""Show the active language for this context"""
if forced_language:
app.notify(f"Forced language: {forced_language}")
else:
app.notify("No language forced")
1 change: 1 addition & 0 deletions core/modes/language_modes.talon
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
^force {user.language_mode}$: user.code_set_language_mode(language_mode)
show [forced] language modes: user.code_show_forced_language_mode()
^clear language modes$: user.code_clear_language_mode()