BiagioF
BiagioF

Reputation: 9735

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.

Upvotes: 2

Views: 191

Answers (2)

phils
phils

Reputation: 73345

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))

Upvotes: 1

Stefan
Stefan

Reputation: 28571

After running jit-lock-register in foo, buffer foo is marked as fully "jit-locked" (more specifically, it's done in the redisplay cycle that immediately follows, and it's done because the buffer is presumably fully displayed). Running jit-lock-fontify-now after that will do nothing because there's nothing there that needs to be "jit-locked". Running font-lock-flush won't make a difference either because presumably foo is in fundamental-mode so font-lock-mode is not enabled so font-lock-flush sees that there's no font-lock highlighting applied yet, and hence nothing to flush.

Upvotes: 0

Related Questions