Ihor Radchenko writes: > Nathaniel Nicandro writes: > >> Attached is the patch. Without this patch, ANSI escape sequences >> generated by the output of a source block will be left in the buffer >> without any fontification. With this patch, the escaped text is nicely >> colored and escape sequences hidden using overlays. >> >> It works for Emacs versions which have the `PRESERVE-SEQUENCES` argument >> to the `ansi-color-apply-on-region` function. It's a bit slow due to >> the use of overlays. My implementation of this feature in Emacs-Jupyter >> supports older versions of Emacs without that argument, it relies on a >> custom version of that function though and uses text properties instead >> of overlays. >> >> Let me know what else could be done on my end to get this patch in. >> Thanks. > > Thanks for the patch! > > This is an interesting idea, but I am not sure if we want to use this > colouring by default. At least, it should be a minor mode. Probably > enabled by default. Because not every possible user may want to have the > escape sequences hidden away. > > Further, your patch only allows fontifying ANSI sequences in fixed-width > elements, example blocks, export blocks, and src blocks without known > major mode that does the fontification. I doubt that fontifying ANSI > sequences in this specific subset of elements always makes sense - > example blocks are not always used as src block output; bash code blocks > may purposely contain escape sequences, but your patch will not handle > them; inline src block output is not covered at all. > > Ideally, fontifying ANSI sequences should be fully controlled by users: > 1. We may not want to touch src blocks by default, when > `org-src-fontify-natively' is set to t. Only, maybe, provide an > option. Or you may better publish a minor mode that does this for > shell scripts. > 2. We may allow all the ANSI sequences to be fontified in the whole > buffer. I've updated my patch to be a combination of (1) and (2), see the attached patch. Essentially every sequence is fontified except those in source blocks and a minor mode has been created to allow users to disable or enable fontification whenever they want. I've also attached an example Org file with some ANSI sequences in it for testing purposes that you can try out. One issue that remains is how to handle sequences within inline source blocks. Those don't have a src-block property so any sequences within an inline source block are currently fontified. > 3. We may limit ANSI sequence fontification to results and only results. > Or just certain types of results. > > The easiest will be implementing fontification in the whole buffer, > early during fontification (and early in org-font-lock-keywords; see > org-font-lock-set-keywords-hook).