0

I'm writing tests for color-identifiers-mode I co-maintain, and I stumbled upon a problem that after creating a buffer and enabling c-mode in it, there is no highlight over keywords.

For example, if you open a usual C file containing struct foo;, executing a (print (get-text-property (point) 'face)) over the first character will give you font-lock-keyword-face.

However, doing the same in a temporary buffer with c-mode enabled gives instead nil:

(with-temp-buffer
  (insert "struct foo;")
  (c-mode)
  (goto-char 1)
  (print (get-text-property (point) 'face))) ;; evaluating will give you `nil'

Any ideas, how to fix that?

Hi-Angel
  • 525
  • 6
  • 18

1 Answers1

0

Turns out, if you explicitly call either (font-lock-ensure) or (font-lock-fontify-buffer) (the latter is purposed for interactive usage, byte-compilation will give you a warning about that), then at least in case of c-mode it will force fontification of the buffer. So this code works:

(with-temp-buffer
  (insert "struct foo;")
  (c-mode)
  (goto-char 1)
  (font-lock-ensure)
  (print (get-text-property (point) 'face))) ;; evaluating will give you `font-lock-keyword-face'

It's worth noting that some major modes may not require that. But font-lock automates various operations, so most likely will be used.

Hi-Angel
  • 525
  • 6
  • 18