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.

freiksenet
  • 3,569
  • 3
  • 28
  • 28

3 Answers3

5

Ok, how about this instead:

(defun my-font-lock-restart ()
  (interactive)
  (setq font-lock-mode-major-mode nil)
  (font-lock-fontify-buffer))
scottfrazer
  • 17,079
  • 4
  • 51
  • 49
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)))
scottfrazer
  • 17,079
  • 4
  • 51
  • 49
  • Cool, I'll try this. I wonder why it does not work like jrockway described by default :) – freiksenet Sep 16 '09 at 18:37
  • 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. – freiksenet Sep 16 '09 at 18:41
  • 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). – Torsten Anders Jul 27 '21 at 10:14
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.)

jrockway
  • 42,082
  • 9
  • 61
  • 86
  • That's very strange. What do you use to modify keywords? I use "font-lock-add-keywords". – freiksenet Sep 16 '09 at 09:23
  • 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. – freiksenet Sep 16 '09 at 14:05