When font-lock-set-defaults is called before org-set-font-lock-defaults, 
font-lock-keywords is reset to global. As a result, font-lock-add-keywords with 
MODE=nil modifies the global variable. This causes keyword highlighting to leak 
across buffers.

Details:

* In font-lock:

- font-lock-set-defaults makes font-lock-keywords buffer-local with (setq-local 
font-lock-keywords ...).
- font-lock-add-keywords with MODE=nil then affects the buffer-local variable.
- font-lock-set-defaults is set to t, so subsequent calls assume this state.

* In org-mode:

- org-set-font-lock-defaults calls (kill-local-variable 'font-lock-keywords), 
which makes font-lock-keywords global again.
- font-lock-set-defaults is not reset, so later calls assume a local variable 
exists when it does not.

If font-lock-set-defaults runs before org-set-font-lock-defaults, 
font-lock-keywords becomes global. A later (font-lock-add-keywords nil ...) 
updates the global variable, affecting all buffers and overriding local 
font-lock settings.

Reproduction:

Not easy to reproduce, but in my setup:
- A text-mode-hook adds keywords to local font-lock.
- While enabling org-mode, during loading of org modules, jit-lock-function may 
run and trigger font-lock-set-defaults.
- Afterwards, org-set-font-lock-defaults runs and kills the local variable.
- My hook then modifies the global font-lock-keywords.

I managed to work-around it by ensuring font-lock-keywords is buffer-local 
before adding keywords. However, the first org-mode buffer may still start with 
(t nil) and no fontification for font-lock-keywords and must be reloaded to fix 
it.

-- Al

Emacs  : GNU Emacs 29.1 (build 1, aarch64-apple-darwin22.5.0, Carbon Version 
169 AppKit 2299.6)
of 2023-09-10
Package: Org mode version 9.7-pre (release_9.6.28-1418-g288e0a)

Reply via email to