Polymode: Multiple Major Modes and How to Use SQL and Python in One Buffer
1–9 of 9 posts
Re: Polymode: Multiple Major Modes and How to Use SQL and Python in One Buffer
#2```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
Re: Polymode: Multiple Major Modes and How to Use SQL and Python in One Buffer
#3Re: Polymode: Multiple Major Modes and How to Use SQL and Python in One Buffer
#4The difficulty is that it would need a completely new langue server to support auto-completion. Currently it only does syntax highlighting.
Re: Polymode: Multiple Major Modes and How to Use SQL and Python in One Buffer
#5Of course, now that tree-sitter is embedded in the upcoming Emacs 29, expect even better support than polymode to emerge over time as you can dole out parcels of your buffer to different language parsers.
Re: Polymode: Multiple Major Modes and How to Use SQL and Python in One Buffer
#6Re: Polymode: Multiple Major Modes and How to Use SQL and Python in One Buffer
#7[0] https://lit.dev/
Re: Polymode: Multiple Major Modes and How to Use SQL and Python in One Buffer
#8Of course, now that tree-sitter is embedded in the upcoming Emacs 29, expect even better support than polymode to emerge over time as you can dole out parcels of your buffer to different language parsers.
How does the language detection work?
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/ )Re: Polymode: Multiple Major Modes and How to Use SQL and Python in One Buffer
#9Will definitely give this a spin.