I have a buffer that shows documentation for Ansible modules. This buffer has its own major mode which provides all the standard stuff. Notably, it fontifies the buffer to highlight inline markup, options, sections, etc. All in all, it is pretty similar to M-x man
.
At the bottom of the documentation there are usually some code examples in YAML. I'd like to fontify these as well. I could add my own font lock keywords for YAML, of course, but I'd rather prefer to re-use the fontification of YAML Mode.
How can I do that? Specifically, how can I use a different major mode to fontify a particular region of a buffer?
Note that YAML Mode doesn't need to be active in this region. The whole buffer is only a passive display buffer, and not intended to be edited. I really only need the fontification produced as if YAML Mode were active in that region.
I presume I could copy the region of the buffer to a temporary buffer, enable YAML Mode, fontify the buffer, and the copy all text properties over. Would that work? If so, how do I copy text properties, and what are the relevant properties to copy? Or is there a better approach?