Search code examples
emacsfont-lock

Force fontification of another buffer


Let us assume I have registered a function as fontification in a buffer named foo.

With jit-lock-mode:

(jit-lock-register #'(lambda (start end)
           (message "Jit!")))

From another buffer, I would like to force that fontification function (and all registered fontification functions).

I am using the function font-lock-flush, in the following:

(with-current-buffer "foo"
  (font-lock-flush)) 

Without any success. That is, when I evaluate (with-current-buffer "foo"...) in a buffer different from foo, no message is printed.

I would expect that expression forces the anonymous function registered as fontification in buffer foo. Instead, the function is not invoked -- I don't see any message in the *Message* buffer.


Additional Notes I have also tried other functions to force, such as: jit-lock-fontify-now. Still, no message is printed.


How to reproduce

Simply, open two buffer: foo and bar.

The content of foo:

(jit-lock-register #'(lambda (start end)
           (message "Jit!")))

And evaluate the buffer.

Now, every time the buffer needs to be fontified a message ("Jit!" will be printed).

Instead, the content of the buffer bar:

(with-current-buffer "foo"
  (font-lock-flush))  ;; or jit-lock-fontify-now

Evaluate (from the buffer bar) that expression. You should notice no message is printed, despite the fact that expression should force the fontification inside foo.


Solution

  • jit-lock-refontify is a compiled Lisp function in ‘jit-lock.el’.
    
    (jit-lock-refontify &optional BEG END)
    
    Force refontification of the region BEG..END (default whole buffer).
    

    Experimentally, this does what you want:

    (with-current-buffer "foo"
      (jit-lock-refontify))