That starts to sound like a lot of property slinging, which might even
dominate the work done.
Indeed, this amount of work could become significant. It's my main
worry, but I don't have a clear feel for how serious it would be
in practice.
In my situation, the most likely scenario is that fontified=nil is noticed during redisplay when there is a fairly large stretch of already-fontified property having the same value. So jit-lock-fontify-now will quickly find a nice large chunk to call my FONTIFICATION-FUNCTION=F-F with.
Since jit-lock-after-change will likely clear away already-fontified and set fontified=nil, a single additional F-F on top of jit-lock-function will probably be very well handled. A good question is how it would scale with more functions all operating in the same region. One idea is to rig up a test file, do some fake jit-lock-flushing on it, and check performance of just subtracting/searching/dividing the already-fontified property as you add more (fake) F-F's. For me, jit-lock-fontify-now of a 2500 char chunk in a heavy treesitter buffer is in the 2-5ms range. Individual F-F's could be much lighter weight.
We could try and unify `fontified` and `jit-lock-already-fontified` by
having a `fontified-done-value` variable and making the redisplay call
jit-lock whenever `fontified` has a value that's not-eq from
`fontified-done-value`.
So jit-lock would set `fontified-done-value` to the list of backends.
Nice idea. Since redisplay just directs jit-lock to a "starting position", it would be free to update however much buffer text it wants. To overcome the issue of "many small domains", jit-lock could cheat, for example checking if ANY chars in the next block need a particular F-F, and running it on the full block if so. That's already implicitly how jit-lock works now if I understand correctly. But things like `text-property-any' will be quickly defeated by the combinatorics of a large F-F set. Also, an advantage of keeping the fontified=nil semantics is that changes to jit-lock could be back-ported to earlier Emacs versions.
So here's an idea. You could invert the logic, and have a set of `fontified-pending' properties which jit-lock-flush adds to as it sets fontified=nil, maintaining one property symbol for each F-F (e.g. fontified-pending-N). Then jit-lock-flush's only job is to select and apply one such property over the region, and fontify-now can use a simple and very fast `text-property-any' as it loops through the list of F-F's, and a final `remove-list-of-text-properties' to strip them all away. For the convenience of jit-lock-after-change, setting a "single property to rule them all" (fontified-pending=t) directs jit-lock to run all the F-F's, there. fontify-now could check for that first, then only bother to look for the individual (-N) properties if it is not set. Is there code out there that sets fontified=nil on its own or is that an internal detail of jit-lock?
I imagine that the functions may also need a way to opt-out of "deferred
contextual refontification", for example if they add some other
properties/overlays orthogonal to face.
`jit-lock.el` already has that info (it's the second arg to
`jit-lock-register`), but it currently doesn't keep track of it
individually for each backend.
Great, so this information could be put into action using one of these approaches.