Polymode and treesit-auto

2024-02-01

I use the package treesit-auto in emacs to automatically switch to the appropriate <name>-ts-mode instead of the default <name>-mode. This doesn’t work, however, with Polymode.

I didn’t go too deep into the source code, but from a cursory glance treesit-auto seems to be locally setting major-mode-remap-alist in order to remap the major mode that emacs selects when opening a file. Polymode inner chunks aren’t opened as files though, and instead the inner chunk mode will be set from regex matching the language name from the source code block delimiter.

As a fix, we can override the inner chunk major mode by setting an alias through the custom variable polymode-mode-name-aliases.

(use-package poly-markdown
  :config
  (add-to-list 'polymode-mode-name-aliases
   '(julia . julia-ts-mode)))

This isn’t as nice as having it automatically set, but I don’t usually work with many languages within a single markdown document at once.

Bonus, by setting polymode-mode-name-aliases this will also fix setting the right inner chunk major mode for quarto-mode as well since that’s just a wrapper that inherits from poly-markdown-mode.