Bypass mode when inserting text into a document


I use codemirror for a REPL-style app, where a user can type code and see the results of running it in the same document. I’d like to apply a mode (e.g. syntax highlighting) to user’s code, but not to the code’s output. Since output can be literally anything, this cannot be done based purely on the contents. However, when I insert text into the document, I know whether it comes from the user or from the code.

Is there a way to select or bypass mode based on that? E.g. by passing options into replaceRange or creating markers?

Thanks in advance,

The mode sees just the text content, so if there’s nothing in there that suggests where output starts and ends, it’s hard to make the mode ignore certain parts. The cleanest way would be to introduce some separation markers (or maybe comment it), and wrap the mode in a multiplexing mode that disables it for those regions.

Failing that, you could add markers to the output that styles it with a class that forces the color and style of the text to be standard (with !important CSS rules). That’s a bit messy, but should work.

I see, thanks! So it’s not possible to attach some metadata that can be accessed by the mode via the stream? If it has to go into contents, I can probably insert some invisible Unicode characters as separators, though this seems messy.

I think forcing styles via css can have issues. E.g. if the output contains a start of a multiline string, subsequent code won’t be properly highlighted.

I thought of having no mode in the document, and then running the language mode manually on user’s input via runmode add-on. If that works, it seems cleaner, but it loses incrementally of parsing.

CoCalc (former SageMathCloud) does something like this. In the “sagews” notebooks the whole notebook is one long codemirror; they use 2 unicode chars to delimit start of code / start of output, and modified the python mode to treat output as comment:

(I’m not sure why that suffices for multi-line output)