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:
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.
Copyright © 1990-1996, 1998-2019 Free Software Foundation, Inc.
Licensed under the GNU GPL license.
https://www.gnu.org/software/emacs/manual/html_node/elisp/Region-to-Refontify.html