elisp: Multiline Font Lock

 
 22.6.9 Multiline Font Lock Constructs
 -------------------------------------
 
 Normally, elements of ‘font-lock-keywords’ should not match across
 multiple lines; that doesn’t work reliably, because Font Lock usually
 scans just part of the buffer, and it can miss a multi-line construct
 that crosses the line boundary where the scan starts.  (The scan
 normally starts at the beginning of a line.)
 
    Making elements that match multiline constructs work properly has two
 aspects: correct _identification_ and correct _rehighlighting_.  The
 first means that Font Lock finds all multiline constructs.  The second
 means that Font Lock will correctly rehighlight all the relevant text
 when a multiline construct is changed—for example, if some of the text
 that was previously part of a multiline construct ceases to be part of
 it.  The two aspects are closely related, and often getting one of them
 to work will appear to make the other also work.  However, for reliable
 results you must attend explicitly to both aspects.
 
    There are three ways to ensure correct identification of multiline
 constructs:
 
    • Add a function to ‘font-lock-extend-region-functions’ that does the
      _identification_ and extends the scan so that the scanned text
      never starts or ends in the middle of a multiline construct.
    • Use the ‘font-lock-fontify-region-function’ hook similarly to
      extend the scan so that the scanned text never starts or ends in
      the middle of a multiline construct.
    • Somehow identify the multiline construct right when it gets
      inserted into the buffer (or at any point after that but before
      font-lock tries to highlight it), and mark it with a
      ‘font-lock-multiline’ which will instruct font-lock not to start or
      end the scan in the middle of the construct.
 
    There are three ways to do rehighlighting of multiline constructs:
 
    • Place a ‘font-lock-multiline’ property on the construct.  This will
      rehighlight the whole construct if any part of it is changed.  In
      some cases you can do this automatically by setting the
      ‘font-lock-multiline’ variable, which see.
    • Make sure ‘jit-lock-contextually’ is set and rely on it doing its
      job.  This will only rehighlight the part of the construct that
      follows the actual change, and will do it after a short delay.
      This only works if the highlighting of the various parts of your
      multiline construct never depends on text in subsequent lines.
      Since ‘jit-lock-contextually’ is activated by default, this can be
      an attractive solution.
    • Place a ‘jit-lock-defer-multiline’ property on the construct.  This
      works only if ‘jit-lock-contextually’ is used, and with the same
      delay before rehighlighting, but like ‘font-lock-multiline’, it
      also handles the case where highlighting depends on subsequent
      lines.
 

Menu