Polymode: Multiple Major Modes and How to Use SQL and Python in One Buffer
masteringemacs.org
masteringemacs.org
```ruby
puts "Hi"
```
You may be used to native syntax highlighting take over, but while you're in polymode, moving the cursor into the Ruby portion of the code actually activates Ruby-mode - any code checking (with something like flymake) works as expected, any major-mode specific actions that reformat code like M-q or == (while evil is active) will do what you expect, and polymode isn't just limited to fenced markdown.
Like the linked article demonstrates, polymode is super flexible. I myself wrote my own, small polymode that watches for comments like /* python */ that precede literal string blocks (which look like ''print("Hi")'' and are usually multi-line) in nix code to transform regions into their native modes (it doesn't need to just be python) and it's really great.
You can see an animated description of this here: https://twitter.com/leothrix/status/1597327756102336512
edit: my fenced block example was weird
e.g this matches markdown code blocks (and also extracts the language from the start to select the mode to run)
(define-auto-innermode poly-markdown-fenced-code-innermode
:head-matcher (cons "^[ \t]*\\(```{?[[:alpha:]].*\n\\)" 1)
:tail-matcher (cons "^[ \t]*\\(```\\)[ \t]*$" 1)
:mode-matcher (cons "```[ \t]*{?\\(?:lang *= *\\)?\\([^ \t\n;=,}]+\\)" 1)
:head-mode 'host
:tail-mode 'host)
( via https://polymode.github.io/defining-polymodes/ )Will definitely give this a spin.
The difficulty is that it would need a completely new langue server to support auto-completion. Currently it only does syntax highlighting.
[0] https://lit.dev/