2

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.

Community
  • 1
  • 1
BiagioF
  • 9,368
  • 2
  • 26
  • 50
  • Please clarify "without any success" (especially w.r.t `jit-lock-fontify-now` which would otherwise by my answer). – Stefan Dec 15 '19 at 17:30
  • @Stefan The lambda seems not to be called. I would expect a message in the `*Message*` buffer. In other words, when I evaluate `(with-current-buffer "foo"...)` in a buffer different than `foo`, no message is printed – BiagioF Dec 15 '19 at 18:08
  • 2
    Please clarify it *in your question* rather than in a comment, and please clarify if you see the same thing with `jit-lock-fontify-now`, and if so please give a more detailed recipe. – Stefan Dec 15 '19 at 18:38
  • @Stefan added to the answer. – BiagioF Dec 15 '19 at 19:58

2 Answers2

1
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))
phils
  • 71,335
  • 11
  • 153
  • 198
0

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.

Stefan
  • 27,908
  • 4
  • 53
  • 82
  • `font-lock-mode` is enabled in `foo` buffer. However, do you have suggestions to approach this scenario and force re-fontification of a buffer? – BiagioF Dec 15 '19 at 22:35
  • @BiagioFesta: The fact that `font-lock-mode` is non-nil is misleading: in the absence of fontification rules, `font-lock-flush` knows that there's nothing to flush. As for **re**fontification, `font-lock-flush` was the right choice and its jit-lock equivalent is indeed `jit-lock-refontify` (whereas `jit-lock-fontify-now` is for fontification, like `font-lock-ensure`). – Stefan Dec 16 '19 at 01:00