Re: Different comment syntaxes in within the same buffer?

Stéphane Marks <[email protected]>
Newsgroups gmane.emacs.help
Message-ID <CAN+1HbpuC70a6KSBq=hqWR2u4qNPqd+s3Sy=jVUNB7zqLq8m5w@mail.gmail.com>
On Fri, Apr 10, 2026 at 12:02 PM Stefan Monnier via Users list for the GNU
Emacs text editor <[email protected]> wrote:

> > I would like to ask the Lispers here whether there is a good solution
> > for having different comment syntaxes in the same buffer depending on
> > the point's position. For more context, a lot of web templating
> > languages (e.g., Svelte, Astro) have multiple syntaxes in the same file.
> > For example, Astro filles have a "frontmatter" section where JavaScript
> > can go, then after the frontmatter section goes CSS and/or HTML.
>
> Presumably you'd like Emacs to handle correctly not just the comments
> but also the highlighting of the rest, as well as the indentation,
> etc...
>
> IOW, you need a major mode that understands that the file is split into
> several sub parts each one using a different major mode.
> AKA "multi major modes".
>
> There are various packages that provide solutions for that, such as
> `mmm-mode` and `polymode`.
>
> The built-in `mhtml-mode` also does it for the specific case of HTML
> files including CSS and Javascript elements.
>

We've done this to a large degree in the new `markdown-ts-mode`.  All
fenced code blocks are fontified in their mode using either treesit or
conventional font-lock and special handling for embedded markdown code
blocks and also modes like php-ts-mode which itself hosts many
subordinate treesit parsers.  We made it so that commands run in the
context of each code-block's mode with some hacking so
`indent-for-tab-command` `newline` etc., work.  It's not Van Gogh but it
mostly works, so a da Vinci.

The code has not yet been installed and is under discussion.  I can send it
to you or you can look on the list if you want to see how it works.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.