Updating font-lock keywords in emacs without major mode reload
Asked Answered
T

3

8

I am doing small modification to SLIME, so that I can get all currently loaded symbols from Lisp, analyze them and make font-lock fontify them.

I managed to do all these steps, but I have a small problem - when keyword list changes in font-lock the buffer is not updated unless you restart the major lisp-mode. I don't want to restart lisp-mode every time I update keywords, because I have several hooks on lisp-mode that I want to run only when I load the file for the first time.

Is there an other way to update font-lock so it reads all then new keywords and fontifies the buffer accordingly? Switching off font-lock and using font-lock-fontify-buffer does not do the trick.

UPD: Added bounty - the question is still up. I need a way to reload font-lock keyword without reloading major mode.

Tetanus answered 16/9, 2009 at 9:7 Comment(0)
C
5

Ok, how about this instead:

(defun my-font-lock-restart ()
  (interactive)
  (setq font-lock-mode-major-mode nil)
  (font-lock-fontify-buffer))
Chambless answered 18/9, 2009 at 15:18 Comment(0)
C
2

You could temporarily clear the mode hook variable and restart it:

(defun my-restart-lisp-mode ()
  (interactive)
  (let ((lisp-mode-hook nil))
    (normal-mode)))
Chambless answered 16/9, 2009 at 17:31 Comment(3)
Cool, I'll try this. I wonder why it does not work like jrockway described by default :)Tetanus
Okay, this is better, but this disables all hooks, including slime hook for example, which is not good :( I really wonder if it is possible to do it without restarting the mode.Tetanus
This is the only suggestion that worked for me. The proposals by jrockway and scottfrazer made no difference on my system (GNU Emacs 25.3 with Aquamacs customisation).Sherbrooke
S
1

Triggering the major-mode is not what makes font-lock do its thing. I am not intimately familiar with the internals of SLIME or lisp-mode, but just setting the variable should make it work. Toggling font-lock-mode will make font-lock start refontifying with the new keywords in mind, as should font-lock-fontify-buffer.

I hack on cperl-mode, mostly, and it is a simple matter of cperl-init-faces (which sets the internal font-lock variables) and a restart of font-lock. lisp-mode should not be much different, except for not needing a call to cperl-init-faces ;)

Edit: some experimentation with lisp-interaction-mode reveals that even restarting font-lock-mode is not strictly necessary. Just changing font-lock-keywords is enough, as long as you re-trigger fontification somehow. (Editing text, font-lock-fontify-buffer, etc.)

Spidery answered 16/9, 2009 at 9:12 Comment(2)
That's very strange. What do you use to modify keywords? I use "font-lock-add-keywords".Tetanus
Font-lock restart does not also help for python mode for example. Maybe it is my version of Emacs? What version do you have? I have GNU Emacs 23.1.50.1 on x86-64 Ubuntu.Tetanus

© 2022 - 2024 — McMap. All rights reserved.