emacs: is there a clear example of multi-line font-locking?

后端 未结 3 896
我在风中等你
我在风中等你 2020-12-15 08:46

Some background, I\'m comfortable with Emacs Lisp, and have written lots of lines of it. However I\'ve never written a major mode, so I\'m fairly new to how the font-lockin

3条回答
  •  爱一瞬间的悲伤
    2020-12-15 09:15

    It's probably not the best possible example, but you can look at how haml-mode has solved the problem of syntax highlighting in submode regions. Here's the blog post with a high-level description.

    Note that the current haml-mode has some problems with Emacs 24 compatibility, but a couple of forks have fixes for this.

    Regarding multiline font-locking, I think you may be asking the wrong question. But basically, this solves the problem of what to do if the user has made an edit in the middle or the end of a multiline syntactic construct. Initially, font-lock starts refontifying the buffer from the position of the point. The two default font-lock-extend-region-functions, font-lock-extend-region-wholelines and font-lock-extend-region-multiline, move the start of the refontification region to the beginning of the line, and then maybe somewhere even further, depending on the font-lock-multiline property. If you need it to move further up, you either add another function to font-lock-region-functions, or make sure to backtrack programmatically while parsing certain constructs, inside font-lock-region-function or syntax-propertize-function.

    One example of the latter approach would be Ruby's heredoc and ruby-syntax-propertize-heredoc in the Emacs trunk. It is called from two places in ruby-syntax-propertize-function. The first time to handle the case when we already are inside of a heredoc literal, and then for any subsequent heredocs.

提交回复
热议问题