24.6.9.2 Region to Fontify after a Buffer Change

When a buffer is changed, the region that Font Lock refontifies is by default the smallest sequence of whole lines that spans the change. While this works well most of the time, sometimes it doesn’t—for example, when a change alters the syntactic meaning of text on an earlier line.

You can enlarge (or even reduce) the region to refontify by setting the following variable:

Variable: font-lock-extend-after-change-region-function

This buffer-local variable is either nil or a function for Font Lock mode to call to determine the region to scan and fontify.

The function is given three parameters, the standard beg, end, and old-len from after-change-functions (see Change Hooks). It should return either a cons of the beginning and end buffer positions (in that order) of the region to fontify, or nil (which means choose the region in the standard way). This function needs to preserve point, the match-data, and the current restriction. The region it returns may start or end in the middle of a line.

Since this function is called after every buffer change, it should be reasonably fast.