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

后端 未结 3 893
我在风中等你
我在风中等你 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:22

    In the example below, I use the "anchored" form of font-lock keywords, it allows you to search more than the current line. The "trick" is that the "pre" hook do two things: 1) it allows you to position the point to the start of the search and 2) it allows you to limit the search by returning the end-position. In the example below, I have used the second property.

    Note that this is only a proof-of-concept. You will need to make sure that the font-lock-multiline variable and the font-lock keywords are applied to the correct buffer.

    (defun my-end-of-paragraph-position (&rest foo)
      "Return position of next empty line."
      (save-excursion
        (while (not (or (eobp)             ; Stop at end of buffer.
                        (and (bolp)        ; Or at an empty line.
                             (eolp))))
          (forward-line))
        (point)))
    
    (setq font-lock-multiline t)
    
    (font-lock-add-keywords nil
                            '(("^FOO"
                               (0 font-lock-keyword-face)  ;; Face for FOO
                               ("BAR"
                                (my-end-of-paragraph-position)
                                nil
                                (0 font-lock-variable-name-face)))))
    

    Below, the first two lines of BAR will be highlighted, but not the last:

    FOO BAR BAR BAR BAR
    BAR BAR BAR BAR
    
    BAR BAR BAR BAR
    

提交回复
热议问题